Files
ip-server/server.c
2025-10-19 20:08:56 +02:00

145 lines
4.7 KiB
C

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 6008
#define BACKLOG 16
#define RECV_BUF 256
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);
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;
// 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);
}
}