From: Alan T. DeKok Date: Thu, 11 Jan 2024 16:27:06 +0000 (-0500) Subject: add "-t timeout" to radsniff X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e457e70d9f4e45fb70e9dbfc41aba7e135fa33dd;p=thirdparty%2Ffreeradius-server.git add "-t timeout" to radsniff So that it will exit cleanly after a given timeout --- diff --git a/src/bin/radsniff.c b/src/bin/radsniff.c index ecca879f372..0c74793ea8e 100644 --- a/src/bin/radsniff.c +++ b/src/bin/radsniff.c @@ -2163,6 +2163,15 @@ static void _unmark_link(void *request) this->in_link_tree = false; } +/** Exit the event loop after a given timeout. + * + */ +static void timeout_event(fr_event_list_t *el, UNUSED fr_time_t now_t, UNUSED void *ctx) +{ + fr_event_loop_exit(el, 1); +} + + #ifdef HAVE_COLLECTDC_H /** Re-open the collectd socket * @@ -2279,6 +2288,7 @@ static NEVER_RETURNS void usage(int status) fprintf(output, " -R RADIUS attribute response filter.\n"); fprintf(output, " -s RADIUS secret.\n"); fprintf(output, " -S Write PCAP data to stdout.\n"); + fprintf(output, " -t Stop after seconds.\n"); fprintf(output, " -v Show program version information and exit.\n"); fprintf(output, " -w Write output packets to file.\n"); fprintf(output, " -x Print more debugging information.\n"); @@ -2311,6 +2321,8 @@ int main(int argc, char *argv[]) int port = FR_AUTH_UDP_PORT; int c; + unsigned int timeout = 0; + fr_event_timer_t const *timeout_ev = NULL; char const *raddb_dir = RADDBDIR; char const *dict_dir = DICTDIR; TALLOC_CTX *autofree; @@ -2365,7 +2377,7 @@ int main(int argc, char *argv[]) /* * Get options */ - while ((c = getopt(argc, argv, "ab:c:C:d:D:e:Ef:hi:I:l:L:mp:P:qr:R:s:Svw:xXW:T:P:N:O:Z:")) != -1) { + while ((c = getopt(argc, argv, "ab:c:C:d:D:e:Ef:hi:I:l:L:mp:P:qr:R:s:St:vw:xXW:T:P:N:O:Z:")) != -1) { switch (c) { case 'a': { @@ -2500,6 +2512,10 @@ int main(int argc, char *argv[]) conf->radius_secret = talloc_strdup(conf, optarg); break; + case 't': + timeout = atoi(optarg); + break; + case 'S': conf->to_stdout = true; break; @@ -3088,6 +3104,13 @@ int main(int argc, char *argv[]) goto finish; } } + + if (timeout) { + if (fr_event_timer_in(NULL, events, &timeout_ev, fr_time_delta_from_sec(timeout), + timeout_event, NULL) < 0) { + ERROR("Failed inserting timeout event"); + } + } } /* @@ -3095,7 +3118,6 @@ int main(int argc, char *argv[]) */ if (fr_event_list_num_fds(events) == 1) goto finish; - /* * Do this as late as possible so we can return an error code if something went wrong. */