Files
ip-server/server.c
2025-10-19 21:43:30 +02:00

201 lines
6.0 KiB
C

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <unistd.h>
#include <time.h>
#define PORT 6008
#define BACKLOG 16
#define RECV_BUF 256
#define DATE_BUF 11
static char current_day[DATE_BUF];
static unsigned long daily_requests;
static bool stats_initialized;
static const char *stats_file = "request_counts.txt";
static void write_day_count(const char *day, unsigned long count)
{
FILE *f = fopen(stats_file, "a");
if (!f)
return;
fprintf(f, "%s %lu\n", day, count);
fclose(f);
}
static void flush_request_stats(void)
{
if (!stats_initialized || daily_requests == 0)
return;
write_day_count(current_day, daily_requests);
}
static void record_request(void)
{
time_t now = time(NULL);
struct tm tm_now;
if (localtime_r(&now, &tm_now) == NULL)
return;
char today[DATE_BUF];
if (strftime(today, sizeof(today), "%Y-%m-%d", &tm_now) == 0)
return;
if (!stats_initialized)
{
strncpy(current_day, today, sizeof(current_day));
current_day[DATE_BUF - 1] = '\0';
daily_requests = 0;
stats_initialized = true;
}
if (strcmp(today, current_day) != 0)
{
flush_request_stats();
strncpy(current_day, today, sizeof(current_day));
current_day[DATE_BUF - 1] = '\0';
daily_requests = 0;
}
daily_requests++;
}
int main()
{
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
bind(server_fd, (struct sockaddr *)&addr, sizeof(addr));
listen(server_fd, BACKLOG);
printf("listening port %d\nbacklog %d\nrecv buffer %d\n", PORT, BACKLOG, RECV_BUF);
atexit(flush_request_stats);
char buf[RECV_BUF];
const char *hdr = "X-Forwarded-For:";
const size_t hdrlen = strlen(hdr);
while (1)
{
int client = accept(server_fd, NULL, NULL);
if (client < 0)
continue;
ssize_t r = recv(client, buf, sizeof(buf) - 1, 0);
if (r <= 0)
{
close(client);
continue;
}
buf[r] = 0;
record_request();
// detect optional JSON response request on the first line
bool wants_json = false;
const char *line_end = strstr(buf, "\r\n");
size_t line_len = line_end ? (size_t)(line_end - buf) : (size_t)r;
const char *space1 = memchr(buf, ' ', line_len);
if (space1 && space1 < buf + line_len)
{
const char *path = space1 + 1;
const char *space2 = memchr(path, ' ', line_len - (size_t)(path - buf));
if (space2 && space2 > path)
{
const char *query = memchr(path, '?', (size_t)(space2 - path));
if (query && ++query < space2)
{
size_t remaining = (size_t)(space2 - query);
const char *token = query;
while (remaining > 0)
{
size_t token_len = 0;
while (token_len < remaining && token[token_len] != '&')
token_len++;
if (token_len == 11 && strncasecmp(token, "format=json", 11) == 0)
{
wants_json = true;
break;
}
if (token_len >= 7 && strncasecmp(token, "format=", 7) == 0)
{
size_t value_len = token_len - 7;
if (value_len == 4 && strncasecmp(token + 7, "json", 4) == 0)
{
wants_json = true;
break;
}
}
if (token_len == remaining)
break;
token += token_len + 1;
remaining -= token_len + 1;
}
}
}
}
// find header
char *ip = NULL;
for (ssize_t i = 0; i < r - (ssize_t)hdrlen; ++i)
{
if (strncasecmp(buf + i, hdr, hdrlen) == 0)
{
ip = buf + i + hdrlen;
while (*ip == ' ' || *ip == '\t')
ip++;
break;
}
}
// find end
char *end = ip;
if (ip)
{
while (*end && *end != '\r' && *end != '\n' && *end != ',')
end++;
while (end > ip && (end[-1] == ' ' || end[-1] == '\t'))
end--;
*end = 0;
}
// response
char head[128];
char jsonbuf[RECV_BUF];
const char *body = ip ? ip : "";
const char *content_type = "text/plain";
int body_len = ip ? (int)strlen(ip) : 0;
if (wants_json)
{
content_type = "application/json";
if (ip && *ip)
{
body_len = snprintf(jsonbuf, sizeof(jsonbuf), "{\"ip\":\"%s\"}", ip);
if (body_len < 0 || body_len >= (int)sizeof(jsonbuf))
body_len = snprintf(jsonbuf, sizeof(jsonbuf), "{\"ip\":\"%s\"}", "");
}
else
{
body_len = snprintf(jsonbuf, sizeof(jsonbuf), "{\"ip\":null}");
}
body = jsonbuf;
}
int head_len = snprintf(head, sizeof(head),
"HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n",
content_type, body_len);
if (head_len > 0)
send(client, head, (size_t)head_len, 0);
if (body_len > 0)
send(client, body, (size_t)body_len, 0);
close(client);
}
}