Debian Codegraph @

src/fstrm_capture.c

raw
1 /*
2 * Copyright (c) 2014-2016, 2018 by Farsight Security, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
27 #include <sys/stat.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
30 #include <assert.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 #include <event2/buffer.h>
43 #include <event2/bufferevent.h>
44 #include <event2/event.h>
45 #include <event2/listener.h>
46
47 #include <fstrm.h>
48
49 #include "libmy/argv.h"
50 #include "libmy/my_alloc.h"
51 #include "libmy/print_string.h"
52
53 #if HAVE_DECL_FFLUSH_UNLOCKED
54 # define fflush fflush_unlocked
55 #endif
56
57 #if HAVE_DECL_FREAD_UNLOCKED
58 # define fread fread_unlocked
59 #endif
60
61 #if HAVE_DECL_FWRITE_UNLOCKED
62 # define fwrite fwrite_unlocked
63 #endif
64
65 struct capture;
66 struct capture_args;
67 struct conn;
68
69 typedef enum {
70 CONN_STATE_READING_CONTROL_READY,
71 CONN_STATE_READING_CONTROL_START,
72 CONN_STATE_READING_DATA,
73 CONN_STATE_STOPPED,
74 } conn_state;
75
76 typedef enum conn_verbosity {
77 CONN_CRITICAL = 0,
78 CONN_ERROR = 1,
79 CONN_WARNING = 2,
80 CONN_INFO = 3,
81 CONN_DEBUG = 4,
82 CONN_TRACE = 5,
83 } conn_verbosity;
84
85 struct conn {
86 struct capture *ctx;
87 conn_state state;
88 uint32_t len_frame_payload;
89 uint32_t len_frame_total;
90 size_t len_buf;
91 size_t bytes_read;
92 size_t bytes_skip;
93 size_t count_read;
94 struct bufferevent *bev;
95 struct evbuffer *ev_input;
96 struct evbuffer *ev_output;
97 struct fstrm_control *control;
98 };
99
100 struct capture {
101 struct capture_args *args;
102
103 struct sockaddr_storage ss;
104 socklen_t ss_len;
105 evutil_socket_t listen_fd;
106 struct event_base *ev_base;
107 struct evconnlistener *ev_connlistener;
108 struct event *ev_sighup;
109 struct event *ev_sigusr1;
110
111 FILE *output_file;
112 char *output_fname;
113 time_t output_open_timestamp;
114
115 size_t bytes_written;
116 size_t count_written;
117 size_t capture_highwater;
118 int remaining_connections;
119
120 struct tm *(*calendar_fn)(const time_t *, struct tm *);
121 };
122
123 struct capture_args {
124 bool help;
125 int debug;
126 bool localtime;
127 bool gmtime;
128 char *str_content_type;
129 char *str_read_unix;
130 char *str_read_tcp_address;
131 char *str_read_tcp_port;
132 char *str_write_fname;
133 int split_seconds;
134 int buffer_size;
135 int count_connections;
136 };
137
138 static struct capture g_program_ctx;
139 static struct capture_args g_program_args;
140
141 static argv_t g_args[] = {
142 { 'h', "help",
143 ARGV_BOOL,
144 &g_program_args.help,
145 NULL,
146 "display this help text and exit" },
147
148 { 'd', "debug",
149 ARGV_INCR,
150 &g_program_args.debug,
151 NULL,
152 "increment debugging level" },
153
154 { 't', "type",
155 ARGV_CHAR_P,
156 &g_program_args.str_content_type,
157 "<STRING>",
158 "Frame Streams content type" },
159
160 { 'u', "unix",
161 ARGV_CHAR_P,
162 &g_program_args.str_read_unix,
163 "<FILENAME>",
164 "Unix socket path to read from" },
165
166 { 'a', "tcp",
167 ARGV_CHAR_P,
168 &g_program_args.str_read_tcp_address,
169 "<ADDRESS>",
170 "TCP socket address to read from" },
171
172 { 'p', "port",
173 ARGV_CHAR_P,
174 &g_program_args.str_read_tcp_port,
175 "<PORT>",
176 "TCP socket port to read from" },
177
178 { 'b', "buffersize",
179 ARGV_INT,
180 &g_program_args.buffer_size,
181 "<SIZE>",
182 "read buffer size, in bytes (default 262144)" },
183
184 { 'c', "maxconns",
185 ARGV_INT,
186 &g_program_args.count_connections,
187 "<COUNT>",
188 "maximum concurrent connections allowed" },
189
190 { 'w', "write",
191 ARGV_CHAR_P,
192 &g_program_args.str_write_fname,
193 "<FILENAME>",
194 "file path to write Frame Streams data to" },
195
196 { 's', "split",
197 ARGV_INT,
198 &g_program_args.split_seconds,
199 "<SECONDS>",
200 "seconds before rotating output file" },
201
202 { '\0', "localtime",
203 ARGV_BOOL,
204 &g_program_args.localtime,
205 NULL,
206 "filter -w path with strftime (local time)" },
207
208 { '\0', "gmtime",
209 ARGV_BOOL,
210 &g_program_args.gmtime,
211 NULL,
212 "filter -w path with strftime (UTC)" },
213
214 { ARGV_LAST, 0, 0, 0, 0, 0 },
215 };
216
217 static struct conn *
218 conn_init(struct capture *ctx)
219 {
220 struct conn *conn;
221 conn = my_calloc(1, sizeof(*conn));
222 conn->ctx = ctx;
223 conn->state = CONN_STATE_READING_CONTROL_READY;
224 conn->control = fstrm_control_init();
225 return conn;
226 }
227
228 static void
229 conn_destroy(struct conn **conn)
230 {
231 if (*conn != NULL) {
232 fstrm_control_destroy(&(*conn)->control);
233 my_free(*conn);
234 }
235 }
236
237 static void
238 conn_log(int level, struct conn *conn, const char *format, ...)
239 {
240 if (level > conn->ctx->args->debug)
241 return;
242 int fd = -1;
243
244 if (conn->bev != NULL)
245 fd = (int) bufferevent_getfd(conn->bev);
246
247 fprintf(stderr, "%s: connection fd %d: ", argv_program, fd);
248
249 va_list args;
250 va_start(args, format);
251 vfprintf(stderr, format, args);
252 va_end(args);
253
254 fputc('\n', stderr);
255 }
256
257 static void
258 conn_log_data(int level, struct conn *conn, const void *data, size_t len, const char *format, ...)
259 {
260 if (level > conn->ctx->args->debug)
261 return;
262 fprintf(stderr, "%s: connection fd %d: ", argv_program,
263 (int) bufferevent_getfd(conn->bev));
264
265 va_list args;
266 va_start(args, format);
267 vfprintf(stderr, format, args);
268 va_end(args);
269
270 print_string(data, len, stderr);
271 fputc('\n', stderr);
272 }
273
274 static void
275 cb_close_conn(struct bufferevent *bev, short error, void *arg)
276 {
277 struct conn *conn = (struct conn *) arg;
278 struct capture *ctx = conn->ctx;
279
280 if (error & BEV_EVENT_ERROR)
281 conn_log(CONN_CRITICAL, conn, "libevent error: %s (%d)",
282 strerror(errno), errno);
283
284 conn_log(CONN_INFO, conn, "closing (read %zd frames, %zd bytes)",
285 conn->count_read, conn->bytes_read);
286
287 /*
288 * The BEV_OPT_CLOSE_ON_FREE flag is set on our bufferevent's, so the
289 * following call to bufferevent_free() will close the underlying
290 * socket transport.
291 */
292 bufferevent_free(bev);
293 conn_destroy(&conn);
294
295 ctx->remaining_connections++;
296 if (ctx->remaining_connections == 1)
297 evconnlistener_enable(ctx->ev_connlistener);
298 }
299
300 static bool
301 usage(const char *msg)
302 {
303 if (msg)
304 fprintf(stderr, "%s: Usage error: %s\n", argv_program, msg);
305 argv_usage(g_args, ARGV_USAGE_DEFAULT);
306 argv_cleanup(g_args);
307 exit(EXIT_FAILURE);
308 }
309
310 static bool
311 parse_args(const int argc, char **argv, struct capture *ctx)
312 {
313 argv_version_string = PACKAGE_VERSION;
314
315 if (argv_process(g_args, argc, argv) != 0)
316 return false;
317
318 /* Validate args. */
319 if (g_program_args.help)
320 return false;
321 if (g_program_args.str_content_type == NULL)
322 usage("Frame Streams content type (--type) is not set");
323 if (g_program_args.str_read_unix == NULL &&
324 g_program_args.str_read_tcp_address == NULL)
325 usage("One of --unix or --tcp must be set");
326 if (g_program_args.str_read_tcp_address != NULL &&
327 g_program_args.str_read_tcp_port == NULL)
328 usage("If --tcp is set, --port must also be set");
329 g_program_ctx.capture_highwater = 262144;
330 if (g_program_args.buffer_size > 0)
331 g_program_ctx.capture_highwater = (size_t)g_program_args.buffer_size;
332 g_program_ctx.remaining_connections = -1; /* unlimited connections. */
333 if (g_program_args.count_connections > 0)
334 g_program_ctx.remaining_connections = (unsigned)g_program_args.count_connections;
335 if (g_program_args.str_write_fname == NULL)
336 usage("File path to write Frame Streams data to (--write) is not set");
337 if (strcmp(g_program_args.str_write_fname, "-") == 0) {
338 if (isatty(STDOUT_FILENO) == 1)
339 usage("Refusing to write binary output to a terminal");
340 if (g_program_args.split_seconds != 0)
341 usage("Cannot use output splitting when writing to stdout");
342 }
343 if (g_program_args.localtime && g_program_args.gmtime)
344 usage("--localtime and --gmtime are mutually exclusive");
345 if (g_program_args.split_seconds && !g_program_args.localtime && !g_program_args.gmtime)
346 usage("--split requires either --localtime or --gmtime");
347
348 /* Set calendar function, if needed. */
349 if (g_program_args.localtime)
350 ctx->calendar_fn = localtime_r;
351 else if (g_program_args.gmtime)
352 ctx->calendar_fn = gmtime_r;
353
354 return true;
355 }
356
357 static bool
358 open_read_unix(struct capture *ctx)
359 {
360 int ret;
361 struct sockaddr_un *sa = (struct sockaddr_un *) &ctx->ss;
362
363 if (ctx->args->str_read_tcp_port != NULL)
364 fputs("Warning: Ignoring --port with --unix\n", stderr);
365
366 /* Construct sockaddr_un structure. */
367 if (strlen(ctx->args->str_read_unix) + 1 >
368 sizeof(sa->sun_path))
369 {
370 usage("Unix socket path is too long");
371 return false;
372 }
373 sa->sun_family = AF_UNIX;
374 strncpy(sa->sun_path,
375 ctx->args->str_read_unix,
376 sizeof(sa->sun_path) - 1);
377 ctx->ss_len = SUN_LEN(sa);
378
379 /* Remove a previously bound socket existing on the filesystem. */
380 ret = remove(sa->sun_path);
381 if (ret != 0 && errno != ENOENT) {
382 fprintf(stderr, "%s: failed to remove existing socket path %s\n",
383 argv_program, sa->sun_path);
384 return false;
385 }
386
387 /* Success. */
388 fprintf(stderr, "%s: opening Unix socket path %s\n",
389 argv_program, sa->sun_path);
390 return true;
391 }
392
393 static bool
394 open_read_tcp(struct capture *ctx)
395 {
396 struct sockaddr_in *sai = (struct sockaddr_in *) &ctx->ss;
397 struct sockaddr_in6 *sai6 = (struct sockaddr_in6 *) &ctx->ss;
398 uint64_t port = 0;
399 char *endptr = NULL;
400
401 /* Parse TCP listen port. */
402 port = strtoul(ctx->args->str_read_tcp_port, &endptr, 0);
403 if (*endptr != '\0' || port > UINT16_MAX) {
404 usage("Failed to parse TCP listen port");
405 return false;
406 }
407
408 if (inet_pton(AF_INET, ctx->args->str_read_tcp_address, &sai->sin_addr) == 1) {
409 sai->sin_family = AF_INET;
410 sai->sin_port = htons(port);
411 ctx->ss_len = sizeof(*sai);
412 } else if (inet_pton(AF_INET6, ctx->args->str_read_tcp_address, &sai6->sin6_addr) == 1) {
413 sai6->sin6_family = AF_INET6;
414 sai6->sin6_port = htons(port);
415 ctx->ss_len = sizeof(*sai6);
416 } else {
417 usage("Failed to parse TCP listen address");
418 return false;
419 }
420
421 /* Success. */
422 fprintf(stderr, "%s: opening TCP socket [%s]:%s\n",
423 argv_program, ctx->args->str_read_tcp_address, ctx->args->str_read_tcp_port);
424 return true;
425 }
426
427 static bool
428 close_write_stop(struct capture *ctx)
429 {
430 fstrm_res res;
431 uint8_t control_frame[FSTRM_CONTROL_FRAME_LENGTH_MAX];
432 size_t len_control_frame = sizeof(control_frame);
433 struct fstrm_control *c = NULL;
434
435 /* Initialize the STOP control frame. */
436 c = fstrm_control_init();
437 res = fstrm_control_set_type(c, FSTRM_CONTROL_STOP);
438 if (res != fstrm_res_success)
439 goto fail;
440
441 /* Encode the STOP frame. */
442 res = fstrm_control_encode(c, control_frame, &len_control_frame,
443 FSTRM_CONTROL_FLAG_WITH_HEADER);
444 if (res != fstrm_res_success)
445 goto fail;
446
447 /* Write the STOP frame. */
448 size_t n_written;
449 n_written = fwrite(control_frame, len_control_frame, 1, ctx->output_file);
450 if (n_written != 1)
451 goto fail;
452
453 /* Success. */
454 ctx->bytes_written += len_control_frame;
455 ctx->count_written += 1;
456 fstrm_control_destroy(&c);
457 return true;
458
459 fail:
460 fstrm_control_destroy(&c);
461 return false;
462 }
463
464 static bool
465 open_write_start(struct capture *ctx)
466 {
467 fstrm_res res;
468 uint8_t control_frame[FSTRM_CONTROL_FRAME_LENGTH_MAX];
469 size_t len_control_frame = sizeof(control_frame);
470 struct fstrm_control *c = NULL;
471
472 /* Initialize the START control frame. */
473 c = fstrm_control_init();
474 res = fstrm_control_set_type(c, FSTRM_CONTROL_START);
475 if (res != fstrm_res_success)
476 goto fail;
477
478 /* Set the "Content Type". */
479 res = fstrm_control_add_field_content_type(c,
480 (const uint8_t *) ctx->args->str_content_type,
481 strlen(ctx->args->str_content_type));
482 if (res != fstrm_res_success)
483 goto fail;
484
485 /* Encode the START frame. */
486 res = fstrm_control_encode(c, control_frame, &len_control_frame,
487 FSTRM_CONTROL_FLAG_WITH_HEADER);
488 if (res != fstrm_res_success)
489 goto fail;
490
491 /* Write the START frame. */
492 size_t n_written;
493 n_written = fwrite(control_frame, len_control_frame, 1, ctx->output_file);
494 if (n_written != 1)
495 goto fail;
496 (void) fflush(ctx->output_file);
497
498 /* Success. */
499 ctx->bytes_written += len_control_frame;
500 ctx->count_written += 1;
501 fstrm_control_destroy(&c);
502 return true;
503
504 fail:
505 fstrm_control_destroy(&c);
506 return false;
507 }
508
509 static const char *
510 update_output_fname(struct capture *ctx)
511 {
512 time_t time_now = {0};
513 struct tm tm_now = {0};
514
515 /* Get current broken-down time representation. */
516 tzset();
517 time_now = time(NULL);
518 ctx->calendar_fn(&time_now, &tm_now);
519
520 /* Save current time. */
521 ctx->output_open_timestamp = time_now;
522
523 /*
524 * Filter ctx->args->str_write_fname with strftime(), store output in
525 * ctx->output_fname. Assume strftime() lengthens the string by no more
526 * than 256 bytes.
527 */
528 if (ctx->output_fname != NULL)
529 my_free(ctx->output_fname);
530 const size_t len_output_fname = strlen(ctx->args->str_write_fname) + 256;
531 ctx->output_fname = my_calloc(1, len_output_fname);
532
533 if (strftime(ctx->output_fname, len_output_fname,
534 ctx->args->str_write_fname, &tm_now) <= 0)
535 {
536 my_free(ctx->output_fname);
537 fprintf(stderr, "%s: strftime() failed on format string \"%s\"\n",
538 argv_program, ctx->args->str_write_fname);
539 return NULL;
540 }
541
542 return ctx->output_fname;
543 }
544
545 static bool
546 open_write_file(struct capture *ctx)
547 {
548 const char *fname = ctx->args->str_write_fname;
549
550 if (strcmp(fname, "-") == 0) {
551 /* Use already opened FILE* for stdout. */
552 ctx->output_file = stdout;
553 } else {
554 mode_t open_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
555 int open_flags = O_CREAT | O_WRONLY | O_TRUNC;
556 #if defined(O_CLOEXEC)
557 open_flags |= O_CLOEXEC;
558 #endif
559
560 /* Rewrite the output filename if needed. */
561 if (ctx->calendar_fn) {
562 fname = update_output_fname(ctx);
563 if (fname == NULL)
564 return false;
565 }
566
567 /* Open the file descriptor. */
568 int fd = open(fname, open_flags, open_mode);
569 if (fd == -1) {
570 fprintf(stderr, "%s: failed to open output file %s\n",
571 argv_program, fname);
572 return false;
573 }
574
575 /* Open the FILE object. */
576 ctx->output_file = fdopen(fd, "w");
577 if (!ctx->output_file) {
578 close(fd);
579 fprintf(stderr, "%s: failed to fdopen output file %s\n",
580 argv_program, fname);
581 return false;
582 }
583 }
584
585 /* Reset output statistics. */
586 ctx->count_written = 0;
587 ctx->bytes_written = 0;
588
589 /* Write the START frame. */
590 if (!open_write_start(ctx)) {
591 fclose(ctx->output_file);
592 ctx->output_file = NULL;
593 fprintf(stderr, "%s: failed to write output file %s\n",
594 argv_program, fname);
595 return false;
596 }
597
598 /* Success. */
599 fprintf(stderr, "%s: opened output file %s\n", argv_program, fname);
600 return true;
601 }
602
603 static bool
604 close_write_file(struct capture *ctx)
605 {
606 if (ctx->output_file != NULL) {
607 /* Write the STOP frame. */
608 if (!close_write_stop(ctx))
609 return false;
610
611 /* Close the FILE object. */
612 fclose(ctx->output_file);
613 ctx->output_file = NULL;
614 }
615
616 /* Success. */
617 fprintf(stderr, "%s: closed output file %s (wrote %zd frames, %zd bytes)\n",
618 argv_program,
619 ctx->output_fname ? ctx->output_fname : ctx->args->str_write_fname,
620 ctx->count_written, ctx->bytes_written);
621 return true;
622 }
623
624 static void
625 process_data_frame(struct conn *conn)
626 {
627 conn_log(CONN_TRACE, conn, "processing data frame (%u bytes)",
628 conn->len_frame_total);
629
630 /*
631 * Peek at 'conn->len_frame_total' bytes of data from the evbuffer, and
632 * write them to the output file.
633 */
634
635 /* Determine how many iovec's we need to read. */
636 const int n_vecs = evbuffer_peek(conn->ev_input, conn->len_frame_total, NULL, NULL, 0);
637
638 /* Allocate space for the iovec's. */
639 struct evbuffer_iovec vecs[n_vecs];
640
641 /* Retrieve the iovec's. */
642 const int n = evbuffer_peek(conn->ev_input, conn->len_frame_total, NULL, vecs, n_vecs);
643 assert(n == n_vecs);
644
645 /* Write each iovec to the output file. */
646 size_t bytes_read = 0;
647 for (int i = 0; i < n_vecs; i++) {
648 size_t len = vecs[i].iov_len;
649
650 /* Only read up to 'conn->len_frame_total' bytes. */
651 if (bytes_read + len > conn->len_frame_total)
652 len = conn->len_frame_total - bytes_read;
653
654 /* Do the fwrite(). Fail hard if it fails. */
655 size_t res = fwrite(vecs[i].iov_base, len, 1, conn->ctx->output_file);
656 if (res != 1) {
657 fprintf(stderr, "%s: fwrite() failed: %s\n",
658 argv_program, strerror(errno));
659 exit(EXIT_FAILURE);
660 }
661 bytes_read += len;
662 }
663
664 /* Check that exactly the right number of bytes were written. */
665 assert(bytes_read == conn->len_frame_total);
666
667 /* Delete the data frame from the input buffer. */
668 evbuffer_drain(conn->ev_input, conn->len_frame_total);
669
670 /* Accounting. */
671 conn->count_read += 1;
672 conn->bytes_read += bytes_read;
673 conn->ctx->count_written += 1;
674 conn->ctx->bytes_written += bytes_read;
675 }
676
677 static void
678 rotate_output(struct capture *ctx)
679 {
680 /* Don't rotate output file if we're writing to stdout. */
681 if (ctx->output_file == stdout) {
682 fprintf(stderr, "%s: %s: not rotating stdout\n",
683 argv_program, __func__);
684 return;
685 }
686
687 /* Rotate output file, fail hard if unsuccessful. */
688 if (!close_write_file(ctx)) {
689 fprintf(stderr, "%s: %s: close_write_file() failed\n",
690 argv_program, __func__);
691 exit(EXIT_FAILURE);
692 }
693 if (!open_write_file(ctx)) {
694 fprintf(stderr, "%s: %s: open_write_file() failed\n",
695 argv_program, __func__);
696 exit(EXIT_FAILURE);
697 }
698 }
699
700 static void
701 maybe_rotate_output(struct conn *conn)
702 {
703 /* Output file rotation requested? */
704 if (conn->ctx->args->split_seconds > 0) {
705 time_t t_now = time(NULL);
706
707 /* Is it time to rotate? */
708 if (t_now >= conn->ctx->output_open_timestamp + conn->ctx->args->split_seconds) {
709 rotate_output(conn->ctx);
710 }
711 }
712 }
713
714 static bool
715 send_frame(struct conn *conn, const void *data, size_t size)
716 {
717 conn_log_data(CONN_TRACE, conn, data, size, "writing frame (%zd) bytes: ", size);
718
719 if (bufferevent_write(conn->bev, data, size) != 0) {
720 conn_log(CONN_WARNING, conn, "bufferevent_write() failed");
721 return false;
722 }
723
724 return true;
725 }
726
727 static bool
728 match_content_type(struct conn *conn)
729 {
730 fstrm_res res;
731
732 /* Match the "Content Type" against ours. */
733 res = fstrm_control_match_field_content_type(conn->control,
734 (const uint8_t *) conn->ctx->args->str_content_type,
735 strlen(conn->ctx->args->str_content_type));
736 if (res != fstrm_res_success) {
737 conn_log(CONN_WARNING, conn, "no CONTENT_TYPE matching: \"%s\"",
738 conn->ctx->args->str_content_type);
739 return false;
740 }
741
742 /* Success. */
743 return true;
744 }
745
746 static bool
747 write_control_frame(struct conn *conn)
748 {
749 fstrm_res res;
750 uint8_t control_frame[FSTRM_CONTROL_FRAME_LENGTH_MAX];
751 size_t len_control_frame = sizeof(control_frame);
752
753 /* Encode the control frame. */
754 res = fstrm_control_encode(conn->control,
755 control_frame, &len_control_frame,
756 FSTRM_CONTROL_FLAG_WITH_HEADER);
757 if (res != fstrm_res_success)
758 return false;
759
760 /* Send the control frame. */
761 fstrm_control_type type = 0;
762 (void)fstrm_control_get_type(conn->control, &type);
763 conn_log(CONN_DEBUG, conn, "sending %s (%d)",
764 fstrm_control_type_to_str(type), type);
765 if (!send_frame(conn, control_frame, len_control_frame))
766 return false;
767
768 /* Success. */
769 return true;
770 }
771
772 static bool
773 process_control_frame_ready(struct conn *conn)
774 {
775 fstrm_res res;
776
777 const uint8_t *content_type = NULL;
778 size_t len_content_type = 0;
779 size_t n_content_type = 0;
780
781 /* Retrieve the number of "Content Type" fields. */
782 res = fstrm_control_get_num_field_content_type(conn->control, &n_content_type);
783 if (res != fstrm_res_success)
784 return false;
785
786 for (size_t i = 0; i < n_content_type; i++) {
787 res = fstrm_control_get_field_content_type(conn->control, i,
788 &content_type,
789 &len_content_type);
790 if (res != fstrm_res_success)
791 return false;
792 conn_log_data(CONN_TRACE, conn,
793 content_type, len_content_type,
794 "CONTENT_TYPE [%zd/%zd] (%zd bytes): ",
795 i + 1, n_content_type, len_content_type);
796 }
797
798 /* Match the "Content Type" against ours. */
799 if (!match_content_type(conn))
800 return false;
801
802 /* Setup the ACCEPT frame. */
803 fstrm_control_reset(conn->control);
804 res = fstrm_control_set_type(conn->control, FSTRM_CONTROL_ACCEPT);
805 if (res != fstrm_res_success)
806 return false;
807 res = fstrm_control_add_field_content_type(conn->control,
808 (const uint8_t *) conn->ctx->args->str_content_type,
809 strlen(conn->ctx->args->str_content_type));
810 if (res != fstrm_res_success)
811 return false;
812
813 /* Send the ACCEPT frame. */
814 if (!write_control_frame(conn))
815 return false;
816
817 /* Success. */
818 conn->state = CONN_STATE_READING_CONTROL_START;
819 return true;
820 }
821
822 static bool
823 process_control_frame_start(struct conn *conn)
824 {
825 /* Match the "Content Type" against ours. */
826 if (!match_content_type(conn))
827 return false;
828
829 /* Success. */
830 conn->state = CONN_STATE_READING_DATA;
831 return true;
832 }
833
834 static bool
835 process_control_frame_stop(struct conn *conn)
836 {
837 fstrm_res res;
838
839 /* Setup the FINISH frame. */
840 fstrm_control_reset(conn->control);
841 res = fstrm_control_set_type(conn->control, FSTRM_CONTROL_FINISH);
842 if (res != fstrm_res_success)
843 return false;
844
845 /* Send the FINISH frame. */
846 if (!write_control_frame(conn))
847 return false;
848
849 conn->state = CONN_STATE_STOPPED;
850
851 /* We return true here, which prevents the caller from closing
852 * the connection directly (with the FINISH frame still in our
853 * write buffer). The connection will be closed after the FINISH
854 * frame is written and the write callback (cb_write) is called
855 * to refill the write buffer.
856 */
857 return true;
858 }
859
860 static bool
861 process_control_frame(struct conn *conn)
862 {
863 fstrm_res res;
864 fstrm_control_type type;
865
866 /* Get the control frame type. */
867 res = fstrm_control_get_type(conn->control, &type);
868 if (res != fstrm_res_success)
869 return false;
870 conn_log(CONN_DEBUG, conn, "received %s (%u)",
871 fstrm_control_type_to_str(type), type);
872
873 switch (conn->state) {
874 case CONN_STATE_READING_CONTROL_READY: {
875 if (type != FSTRM_CONTROL_READY)
876 return false;
877 return process_control_frame_ready(conn);
878 }
879 case CONN_STATE_READING_CONTROL_START: {
880 if (type != FSTRM_CONTROL_START)
881 return false;
882 return process_control_frame_start(conn);
883 }
884 case CONN_STATE_READING_DATA: {
885 if (type != FSTRM_CONTROL_STOP)
886 return false;
887 return process_control_frame_stop(conn);
888 }
889 default:
890 return false;
891 }
892
893 /* Success. */
894 return true;
895 }
896
897 static bool
898 load_control_frame(struct conn *conn)
899 {
900 fstrm_res res;
901 uint8_t *control_frame = NULL;
902
903 /* Check if the frame is too big. */
904 if (conn->len_frame_total >= FSTRM_CONTROL_FRAME_LENGTH_MAX) {
905 /* Malformed. */
906 return false;
907 }
908
909 /* Get a pointer to the full, linearized control frame. */
910 control_frame = evbuffer_pullup(conn->ev_input, conn->len_frame_total);
911 if (!control_frame) {
912 /* Malformed. */
913 return false;
914 }
915 conn_log_data(CONN_TRACE, conn, control_frame, conn->len_frame_total,
916 "reading control frame (%u bytes): ", conn->len_frame_total);
917
918 /* Decode the control frame. */
919 res = fstrm_control_decode(conn->control,
920 control_frame,
921 conn->len_frame_total,
922 FSTRM_CONTROL_FLAG_WITH_HEADER);
923 if (res != fstrm_res_success) {
924 /* Malformed. */
925 return false;
926 }
927
928 /* Drain the data read. */
929 evbuffer_drain(conn->ev_input, conn->len_frame_total);
930
931 /* Success. */
932 return true;
933 }
934
935 static bool
936 can_read_full_frame(struct conn *conn)
937 {
938 uint32_t tmp[2] = {0};
939
940 /*
941 * This tracks the total number of bytes that must be removed from the
942 * input buffer to read the entire frame. */
943 conn->len_frame_total = 0;
944
945 /* Check if the frame length field has fully arrived. */
946 if (conn->len_buf < sizeof(uint32_t))
947 return false;
948
949 /* Read the frame length field. */
950 evbuffer_copyout(conn->ev_input, &tmp[0], sizeof(uint32_t));
951 conn->len_frame_payload = ntohl(tmp[0]);
952
953 /* Account for the frame length field. */
954 conn->len_frame_total += sizeof(uint32_t);
955
956 /* Account for the length of the frame payload. */
957 conn->len_frame_total += conn->len_frame_payload;
958
959 /* Check if this is a control frame. */
960 if (conn->len_frame_payload == 0) {
961 uint32_t len_control_frame = 0;
962
963 /*
964 * Check if the control frame length field has fully arrived.
965 * Note that the input buffer hasn't been drained, so we also
966 * need to account for the initial frame length field. That is,
967 * there must be at least 8 bytes available in the buffer.
968 */
969 if (conn->len_buf < 2*sizeof(uint32_t))
970 return false;
971
972 /* Read the control frame length. */
973 evbuffer_copyout(conn->ev_input, &tmp[0], 2*sizeof(uint32_t));
974 len_control_frame = ntohl(tmp[1]);
975
976 /* Account for the length of the control frame length field. */
977 conn->len_frame_total += sizeof(uint32_t);
978
979 /* Enforce minimum and maximum control frame size. */
980 if (len_control_frame < sizeof(uint32_t) ||
981 len_control_frame > FSTRM_CONTROL_FRAME_LENGTH_MAX)
982 {
983 cb_close_conn(conn->bev, 0, conn);
984 return false;
985 }
986
987 /* Account for the control frame length. */
988 conn->len_frame_total += len_control_frame;
989 }
990
991 /*
992 * Check if the frame has fully arrived. 'len_buf' must have at least
993 * the number of bytes needed in order to read the full frame, which is
994 * exactly 'len_frame_total'.
995 */
996 if (conn->len_buf < conn->len_frame_total) {
997 conn_log(CONN_TRACE, conn, "incomplete message (have %zd bytes, want %u)",
998 conn->len_buf, conn->len_frame_total);
999 if (conn->len_frame_total > conn->ctx->capture_highwater) {
1000 conn_log(CONN_WARNING, conn,
1001 "Skipping %u byte message (%zd buffer)",
1002 conn->len_frame_total,
1003 conn->ctx->capture_highwater);
1004 conn->bytes_skip = conn->len_frame_total;
1005 }
1006 return false;
1007 }
1008
1009 /* Success. The entire frame can now be read from the buffer. */
1010 return true;
1011 }
1012
1013 static void
1014 cb_write(struct bufferevent *bev, void *arg)
1015 {
1016 struct conn *conn = (struct conn *) arg;
1017
1018 if (conn->state != CONN_STATE_STOPPED)
1019 return;
1020
1021 cb_close_conn(bev, 0, arg);
1022 }
1023
1024 static void
1025 cb_read(struct bufferevent *bev, void *arg)
1026 {
1027 struct conn *conn = (struct conn *) arg;
1028 conn->bev = bev;
1029 conn->ev_input = bufferevent_get_input(conn->bev);
1030 conn->ev_output = bufferevent_get_output(conn->bev);
1031
1032 for (;;) {
1033 /* Get the number of bytes available in the buffer. */
1034 conn->len_buf = evbuffer_get_length(conn->ev_input);
1035
1036 /* Check if there is any data available in the buffer. */
1037 if (conn->len_buf <= 0)
1038 return;
1039
1040 /* Check if the full frame has arrived. */
1041 if ((conn->bytes_skip == 0) && !can_read_full_frame(conn))
1042 return;
1043
1044 /* Skip bytes of oversized frames. */
1045 if (conn->bytes_skip > 0) {
1046 size_t skip = conn->bytes_skip;
1047
1048 if (skip > conn->len_buf)
1049 skip = conn->len_buf;
1050
1051 conn_log(CONN_TRACE, conn, "Skipping %zd bytes", skip);
1052 evbuffer_drain(conn->ev_input, skip);
1053 conn->bytes_skip -= skip;
1054 continue;
1055 }
1056
1057 /* Process the frame. */
1058 if (conn->len_frame_payload > 0) {
1059 /* This is a data frame. */
1060 process_data_frame(conn);
1061
1062 /* Check if it's time to rotate the output file. */
1063 maybe_rotate_output(conn);
1064 } else {
1065 /* This is a control frame. */
1066 if (!load_control_frame(conn)) {
1067 /* Malformed control frame, shut down the connection. */
1068 cb_close_conn(conn->bev, 0, conn);
1069 return;
1070 }
1071
1072 if (!process_control_frame(conn)) {
1073 /*
1074 * Invalid control state requested, or the
1075 * end-of-stream has been reached. Shut down
1076 * the connection.
1077 */
1078 cb_close_conn(conn->bev, 0, conn);
1079 return;
1080 }
1081 }
1082 }
1083 }
1084
1085 static void
1086 cb_accept_conn(struct evconnlistener *listener, evutil_socket_t fd,
1087 __attribute__((unused)) struct sockaddr *sa,
1088 __attribute__((unused)) int socklen, void *arg)
1089 {
1090 struct capture *ctx = (struct capture *) arg;
1091 struct event_base *base = evconnlistener_get_base(listener);
1092
1093 /* Set up a bufferevent and per-connection context for the new connection. */
1094 struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
1095 if (!bev) {
1096 if (ctx->args->debug >= CONN_ERROR)
1097 fprintf(stderr, "%s: bufferevent_socket_new() failed\n",
1098 argv_program);
1099 evutil_closesocket(fd);
1100 return;
1101 }
1102 struct conn *conn = conn_init(ctx);
1103 bufferevent_setcb(bev, cb_read, cb_write, cb_close_conn, (void *) conn);
1104 bufferevent_setwatermark(bev, EV_READ, 0, ctx->capture_highwater);
1105 bufferevent_enable(bev, EV_READ | EV_WRITE);
1106
1107 if (ctx->args->debug >= CONN_INFO)
1108 fprintf(stderr, "%s: accepted new connection fd %d\n", argv_program, fd);
1109
1110 ctx->remaining_connections--;
1111 if (ctx->remaining_connections == 0)
1112 evconnlistener_disable(listener);
1113 }
1114
1115 static void
1116 cb_accept_error(__attribute__((unused)) struct evconnlistener *listener,
1117 __attribute__((unused)) void *arg)
1118 {
1119 const int err = EVUTIL_SOCKET_ERROR();
1120 fprintf(stderr, "%s: accept() failed: %s\n", argv_program,
1121 evutil_socket_error_to_string(err));
1122 }
1123
1124 static void
1125 do_sigusr1(__attribute__((unused)) evutil_socket_t sig,
1126 __attribute__((unused)) short events, void *user_data)
1127 {
1128 struct capture *ctx = user_data;
1129 if (ctx->output_file) {
1130 fprintf(stderr, "%s: received SIGUSR1, rotating output file\n", argv_program);
1131 rotate_output(ctx);
1132 }
1133 }
1134
1135 static void
1136 do_sighup(__attribute__((unused)) evutil_socket_t sig,
1137 __attribute__((unused)) short events, void *user_data)
1138 {
1139 struct capture *ctx = user_data;
1140 if (ctx->output_file) {
1141 fflush(ctx->output_file);
1142 fprintf(stderr, "%s: received SIGHUP, flushing output\n", argv_program);
1143 }
1144 }
1145
1146 static bool
1147 setup_event_loop(struct capture *ctx)
1148 {
1149 /* Create the event base. */
1150 ctx->ev_base = event_base_new();
1151 if (!ctx->ev_base)
1152 return false;
1153
1154 /* Create the evconnlistener. */
1155 unsigned flags = 0;
1156 flags |= LEV_OPT_CLOSE_ON_FREE; /* Closes underlying sockets. */
1157 flags |= LEV_OPT_CLOSE_ON_EXEC; /* Sets FD_CLOEXEC on underlying fd's. */
1158 flags |= LEV_OPT_REUSEABLE; /* Sets SO_REUSEADDR on listener. */
1159
1160 ctx->ev_connlistener = evconnlistener_new_bind(ctx->ev_base,
1161 cb_accept_conn, (void *) ctx, flags, -1,
1162 (struct sockaddr *) &ctx->ss, ctx->ss_len);
1163 if (!ctx->ev_connlistener) {
1164 event_base_free(ctx->ev_base);
1165 ctx->ev_base = NULL;
1166 return false;
1167 }
1168 evconnlistener_set_error_cb(ctx->ev_connlistener, cb_accept_error);
1169
1170 /* Register our SIGHUP handler. */
1171 ctx->ev_sighup = evsignal_new(ctx->ev_base, SIGHUP, &do_sighup,
1172 &g_program_ctx);
1173 evsignal_add(ctx->ev_sighup, NULL);
1174
1175 /* Register our SIGUSR1 handler. */
1176 ctx->ev_sigusr1 = evsignal_new(ctx->ev_base, SIGUSR1, &do_sigusr1,
1177 &g_program_ctx);
1178 evsignal_add(ctx->ev_sigusr1, NULL);
1179
1180 /* Success. */
1181 return true;
1182 }
1183
1184 static void
1185 shutdown_handler(int signum __attribute__((unused)))
1186 {
1187 event_base_loopexit(g_program_ctx.ev_base, NULL);
1188 }
1189
1190 static bool
1191 setup_signals(void)
1192 {
1193 struct sigaction sa = {
1194 .sa_handler = shutdown_handler,
1195 };
1196
1197 if (sigemptyset(&sa.sa_mask) != 0)
1198 return false;
1199 if (sigaction(SIGTERM, &sa, NULL) != 0)
1200 return false;
1201 if (sigaction(SIGINT, &sa, NULL) != 0)
1202 return false;
1203
1204 sa.sa_handler = SIG_IGN;
1205 if (sigaction(SIGPIPE, &sa, NULL) != 0)
1206 return false;
1207
1208 /* Success. */
1209 return true;
1210 }
1211
1212 static void
1213 cleanup(struct capture *ctx)
1214 {
1215 argv_cleanup(g_args);
1216 if (ctx->ev_sighup != NULL)
1217 event_free(ctx->ev_sighup);
1218 if (ctx->ev_sigusr1 != NULL)
1219 event_free(ctx->ev_sigusr1);
1220 if (ctx->ev_connlistener != NULL)
1221 evconnlistener_free(ctx->ev_connlistener);
1222 if (ctx->ev_base != NULL)
1223 event_base_free(ctx->ev_base);
1224 my_free(ctx->output_fname);
1225 }
1226
1227 int
1228 main(int argc, char **argv)
1229 {
1230 /* Parse arguments. */
1231 if (!parse_args(argc, argv, &g_program_ctx)) {
1232 usage(NULL);
1233 return EXIT_FAILURE;
1234 }
1235 g_program_ctx.args = &g_program_args;
1236
1237 /* Open the Unix socket input, if specified. */
1238 if (g_program_ctx.args->str_read_unix != NULL) {
1239 if (!open_read_unix(&g_program_ctx))
1240 return EXIT_FAILURE;
1241 } else if (g_program_ctx.args->str_read_tcp_address != NULL &&
1242 g_program_ctx.args->str_read_tcp_port != NULL) {
1243 /* Otherwise, open the TCP socket input. */
1244 if (!open_read_tcp(&g_program_ctx))
1245 return EXIT_FAILURE;
1246 } else {
1247 fprintf(stderr, "%s: failed to setup a listening socket\n", argv_program);
1248 return EXIT_FAILURE;
1249 }
1250
1251 /* Open the file output. */
1252 if (!open_write_file(&g_program_ctx))
1253 return EXIT_FAILURE;
1254
1255 /* Setup the event loop. */
1256 if (!setup_event_loop(&g_program_ctx)) {
1257 fprintf(stderr, "%s: failed to setup event loop\n", argv_program);
1258 return EXIT_FAILURE;
1259 }
1260
1261 /* Setup signals. */
1262 if (!setup_signals()) {
1263 fprintf(stderr, "%s: failed to setup signals\n", argv_program);
1264 return EXIT_FAILURE;
1265 }
1266
1267 /* Run the event loop. */
1268 if (event_base_dispatch(g_program_ctx.ev_base) != 0) {
1269 fprintf(stderr, "%s: failed to start event loop\n", argv_program);
1270 return EXIT_FAILURE;
1271 }
1272
1273 fprintf(stderr, "%s: shutting down\n", argv_program);
1274
1275 /* Shut down. */
1276 if (!close_write_file(&g_program_ctx))
1277 return EXIT_FAILURE;
1278 cleanup(&g_program_ctx);
1279
1280 /* Success. */
1281 return EXIT_SUCCESS;
1282 }