forked from kohsuke/udplogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudplogger.c
428 lines (412 loc) · 11.3 KB
/
udplogger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Simple UDP logger - A utility for receiving output from netconsole.
*
* Written by Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
*/
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <poll.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define round_up(size) ((((size) + 4095u) / 4096u) * 4096u)
/* Structure for tracking partially received data. */
static struct client {
struct sockaddr_in addr; /* Sender's IPv4 address and port. */
char *buffer; /* Buffer for holding received data. */
int avail; /* Valid bytes in @buffer . */
char addr_str[24]; /* String representation of @addr . */
time_t stamp; /* Timestamp of receiving the first byte in @buffer . */
FILE *log_fp; /* Handle for today's log file. */
/* Previous time. */
struct tm last_tm;
} *clients = NULL;
/* Current clients. */
static int num_clients = 0;
/* Max clients. */
static int max_clients = 1024;
/* Max write buffer per a client. */
static int wbuf_size = 65536;
/* Max seconds to wait for new line. */
static int wait_timeout = 10;
/* Try to release unused memory? */
static _Bool try_drop_memory_usage = 0;
static void flush_all_and_abort(char *reason);
void sig_handler(int signo)
{
if (signo == SIGINT)
flush_all_and_abort("terminated by user");
}
/**
* switch_logfile - Close yesterday's log file and open today's log file.
*
* @tm: Pointer to "struct tm" holding current time.
*
* Returns nothing.
*/
static void switch_logfile(struct client* client, struct tm *tm)
{
/* Name of today's log file. */
static char filename[128] = { };
mkdir(client->addr_str,0755);
FILE *fp = client->log_fp;
snprintf(filename, sizeof(filename) - 1, "%s/%04u-%02u-%02u.log",
client->addr_str, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
client->log_fp = fopen(filename, "a");
if (!fp) {
if (!client->log_fp) {
char errmsg[255];
snprintf(errmsg, sizeof(errmsg) - 1, "unnabble open file '%s', error because %s",
filename, strerror(errno));
flush_all_and_abort(errmsg);
}
return;
}
/* If open() failed, continue using old one. */
if (client->log_fp)
fclose(fp);
else
client->log_fp = fp;
try_drop_memory_usage = 1;
}
/**
* write_logfile - Write to today's log file.
*
* @ptr: Pointer to "struct client".
* @forced: True if the partial line should be written.
*
* Returns nothing.
*/
static void write_logfile(struct client *ptr, const _Bool forced)
{
static time_t last_time = 0;
static char stamp[24] = { };
char *buffer = ptr->buffer;
int avail = ptr->avail;
const time_t now_time = ptr->stamp;
if (last_time != now_time || ptr->log_fp == 0) {
struct tm *tm = localtime(&now_time);
if (!tm)
tm = &ptr->last_tm;
snprintf(stamp, sizeof(stamp) - 1, "%04u-%02u-%02u "
"%02u:%02u:%02u ", tm->tm_year + 1900, tm->tm_mon + 1,
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
/*
* Switch log file if the day has changed. We can't use
* (last_time / 86400 != now_time / 86400) in order to allow
* switching at 00:00:00 of the local time.
*/
if (tm->tm_mday != ptr->last_tm.tm_mday ||
tm->tm_mon != ptr->last_tm.tm_mon ||
tm->tm_year != ptr->last_tm.tm_year) {
ptr->last_tm = *tm;
switch_logfile(ptr,tm);
}
last_time = now_time;
}
/* Write the completed lines. */
while (1) {
char *cp = memchr(buffer, '\n', avail);
const int len = cp - buffer + 1;
if (!cp)
break;
fprintf(ptr->log_fp, "%s%s ", stamp, ptr->addr_str);
fwrite(buffer, 1, len, ptr->log_fp);
avail -= len;
buffer += len;
}
/* Write the incomplete line if forced. */
if (forced && avail) {
fprintf(ptr->log_fp, "%s%s ", stamp, ptr->addr_str);
fwrite(buffer, 1, avail, ptr->log_fp);
fprintf(ptr->log_fp, "\n");
avail = 0;
}
/* Discard the written data. */
if (ptr->buffer != buffer)
memmove(ptr->buffer, buffer, avail);
ptr->avail = avail;
}
/**
* drop_memory_usage - Try to reduce memory usage.
*
* Returns nothing.
*/
static void drop_memory_usage(void)
{
struct client *ptr;
int i = 0;
if (!try_drop_memory_usage)
return;
try_drop_memory_usage = 0;
while (i < num_clients) {
ptr = &clients[i];
if (ptr->avail) {
char *tmp = realloc(ptr->buffer, round_up(ptr->avail));
if (tmp)
ptr->buffer = tmp;
i++;
continue;
}
free(ptr->buffer);
num_clients--;
memmove(ptr, ptr + 1, (num_clients - i) * sizeof(*ptr));
}
if (num_clients) {
ptr = realloc(clients, round_up(sizeof(*ptr) * num_clients));
if (ptr)
clients = ptr;
} else {
free(clients);
clients = NULL;
}
}
/**
* flush_all_and_abort - Clean up upon out of memory.
*
* @reason: Reason why program aborted.
*
* This function does not return.
*/
static void flush_all_and_abort(char *reason)
{
int i;
for (i = 0; i < num_clients; i++)
if (clients[i].log_fp) {
if (clients[i].avail) {
write_logfile(&clients[i], 1);
free(clients[i].buffer);
}
fprintf(clients[i].log_fp, "[aborted due to %s]\n", reason);
fflush(clients[i].log_fp);
}
exit(1);
}
/**
* find_client - Find the structure for given address.
*
* @addr: Pointer to "struct sockaddr_in".
*
* Returns "struct client" for @addr on success, NULL otherwise.
*/
static struct client *find_client(struct sockaddr_in *addr)
{
struct client *ptr;
int i;
for (i = 0; i < num_clients; i++)
if (!memcmp(&clients[i].addr, addr, sizeof(*addr)))
return &clients[i];
if (i >= max_clients) {
try_drop_memory_usage = 1;
drop_memory_usage();
if (i >= max_clients)
return NULL;
}
ptr = realloc(clients, round_up(sizeof(*ptr) * (num_clients + 1)));
if (!ptr)
return NULL;
clients = ptr;
ptr = &clients[num_clients++];
memset(ptr, 0, sizeof(*ptr));
ptr->addr = *addr;
snprintf(ptr->addr_str, sizeof(ptr->addr_str) - 1, "%s:%u",
inet_ntoa(addr->sin_addr), htons(addr->sin_port));
return ptr;
}
/**
* do_main - The main loop.
*
* @fd: Receiver socket's file descriptor.
*
* Returns nothing.
*/
static void do_main(const int fd)
{
static char buf[65536];
struct sockaddr_in addr;
while (1) {
struct pollfd pfd = { fd, POLLIN, 0 };
socklen_t size = sizeof(addr);
int i;
time_t now;
/* Don't wait forever if checking for timeout. */
for (i = 0; i < num_clients; i++)
if (clients[i].avail)
break;
/* Flush log file and wait for data. */
// fflush(log_fp);
poll(&pfd, 1, i < num_clients ? 1000 : -1);
now = time(NULL);
/* Check for timeout. */
for (i = 0; i < num_clients; i++)
if (clients[i].avail &&
now - clients[i].stamp >= wait_timeout)
write_logfile(&clients[i], 1);
/* Don't receive forever in order to check for timeout. */
while (now == time(NULL)) {
struct client *ptr;
char *tmp;
int len = recvfrom(fd, buf, sizeof(buf), MSG_DONTWAIT,
(struct sockaddr *) &addr, &size);
if (len <= 0 || size != sizeof(addr))
break;
ptr = find_client(&addr);
if (!ptr)
continue;
/* Save current time if receiving the first byte. */
if (!ptr->avail)
ptr->stamp = now;
/* Append data to the line. */
tmp = realloc(ptr->buffer, round_up(ptr->avail + len));
if (!tmp)
flush_all_and_abort("memory allocation failure");
memmove(tmp + ptr->avail, buf, len);
ptr->avail += len;
ptr->buffer = tmp;
/* Write if at least one line completed. */
if (memchr(buf, '\n', len))
write_logfile(ptr, 0);
/* Write if the line is too long. */
if (ptr->avail >= wbuf_size)
write_logfile(ptr, 1);
}
drop_memory_usage();
}
}
/**
* usage - Print usage and exit.
*
* @name: Program's name.
*
* This function does not return.
*/
static void usage(const char *name)
{
fprintf(stderr, "Simple UDP logger\n\n"
"Usage:\n %s [ip=$listen_ip] [port=$listen_port] "
"[dir=$log_dir] [timeout=$seconds_waiting_for_newline] "
"[clients=$max_clients] [wbuf=$write_buffer_size] "
"[rbuf=$receive_buffer_size]\n\n"
"The value of $seconds_waiting_for_newline should be between "
"5 and 600.\nThe value of $max_clients should be between 10 "
"and 65536.\nThe value of $write_buffer_size should be "
"between 1024 and 1048576.\nThe value of $receive_buffer_size "
"should be 65536 and 1073741824 (though actual size might be "
"adjusted by the kernel).\n", name);
exit (1);
}
/**
* do_init - Initialization function.
*
* @argc: Number of arguments.
* @argv: Arguments.
*
* Returns the listener socket's file descriptor.
*/
static int do_init(int argc, char *argv[])
{
struct sockaddr_in addr = { };
char pwd[4096];
/* Max receive buffer size. */
int rbuf_size = 8 * 1048576;
socklen_t size;
int fd;
int i;
/* Directory to save logs. */
const char *log_dir = ".";
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(6666);
for (i = 1; i < argc; i++) {
char *arg = argv[i];
if (!strncmp(arg, "ip=", 3))
addr.sin_addr.s_addr = inet_addr(arg + 3);
else if (!strncmp(arg, "port=", 5))
addr.sin_port = htons(atoi(arg + 5));
else if (!strncmp(arg, "dir=", 4))
log_dir = arg + 4;
else if (!strncmp(arg, "timeout=", 8))
wait_timeout = atoi(arg + 8);
else if (!strncmp(arg, "clients=", 8))
max_clients = atoi(arg + 8);
else if (!strncmp(arg, "wbuf=", 5))
wbuf_size = atoi(arg + 5);
else if (!strncmp(arg, "rbuf=", 5))
rbuf_size = atoi(arg + 5);
else
usage(argv[0]);
}
/* Sanity check. */
if (max_clients < 10)
max_clients = 10;
if (max_clients > 65536)
max_clients = 65536;
if (wait_timeout < 5)
wait_timeout = 5;
if (wait_timeout > 600)
wait_timeout = 600;
if (wbuf_size < 1024)
wbuf_size = 1024;
if (wbuf_size > 1048576)
wbuf_size = 1048576;
if (rbuf_size < 65536)
rbuf_size = 65536;
if (rbuf_size > 1024 * 1048576)
rbuf_size = 1024 * 1048576;
/* Create the listener socket and configure it. */
fd = socket(AF_INET, SOCK_DGRAM, 0);
#ifdef SO_RCVBUFFORCE
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &rbuf_size,
sizeof(rbuf_size))) {
#endif
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rbuf_size,
sizeof(rbuf_size))) {
fprintf(stderr, "Can't set receive buffer size.\n");
exit(1);
}
#ifdef SO_RCVBUFFORCE
}
#endif
size = sizeof(rbuf_size);
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rbuf_size, &size)) {
fprintf(stderr, "Can't get receive buffer size.\n");
exit(1);
}
size = sizeof(addr);
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) ||
getsockname(fd, (struct sockaddr *) &addr, &size) ||
size != sizeof(addr)) {
fprintf(stderr, "Can't bind to %s:%u .\n",
inet_ntoa(addr.sin_addr), htons(addr.sin_port));
exit(1);
}
/* Open the initial log file. */
memset(pwd, 0, sizeof(pwd));
if (chdir(log_dir) || !getcwd(pwd, sizeof(pwd) - 1)) {
fprintf(stderr, "Can't change directory to %s .\n", log_dir);
exit(1);
} else {
const time_t now = time(NULL);
struct tm *tm = localtime(&now);
printf("Started at %04u-%02u-%02u %02u:%02u:%02u at %s\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, pwd);
}
/* Successfully initialized. */
printf("Options: ip=%s port=%u dir=%s timeout=%u clients=%u wbuf=%u "
"rbuf=%u\n", inet_ntoa(addr.sin_addr), htons(addr.sin_port),
pwd, wait_timeout, max_clients, wbuf_size, rbuf_size);
return fd;
}
int main(int argc, char *argv[])
{
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
const int fd = do_init(argc, argv);
do_main(fd);
return 0;
}