From c90e2998f6ddb225efba3538f3e06b8a79f2c023 Mon Sep 17 00:00:00 2001 From: netblue30 Date: Tue, 25 Oct 2022 14:34:17 -0400 Subject: nettrace: various fixes --- src/fnettrace-icmp/main.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'src/fnettrace-icmp/main.c') diff --git a/src/fnettrace-icmp/main.c b/src/fnettrace-icmp/main.c index 47d61a326..e1e5daa48 100644 --- a/src/fnettrace-icmp/main.c +++ b/src/fnettrace-icmp/main.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #define MAX_BUF_SIZE (64 * 1024) char *type_description[19] = { @@ -139,6 +141,19 @@ static void custom_bpf(int sock) { } } +static void print_date(void) { + static int day = -1; + time_t now = time(NULL); + struct tm *t = localtime(&now); + + if (day != t->tm_yday) { + printf("\nICMP trace for %s", ctime(&now)); + day = t->tm_yday; + } + + fflush(0); +} + static void run_trace(void) { // grab all Ethernet packets and use a custom BPF filter to get TLS/SNI packets int s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); @@ -146,19 +161,24 @@ static void run_trace(void) { errExit("socket"); custom_bpf(s); + struct timeval tv; + tv.tv_sec = 10; + tv.tv_usec = 0; unsigned char buf[MAX_BUF_SIZE]; while (1) { fd_set rfds; FD_ZERO(&rfds); FD_SET(s, &rfds); - struct timeval tv; - tv.tv_sec = 1; - tv.tv_usec = 0; int rv = select(s + 1, &rfds, NULL, NULL, &tv); if (rv < 0) errExit("select"); - else if (rv == 0) + else if (rv == 0) { + print_date(); + tv.tv_sec = 10; + tv.tv_usec = 0; continue; + } + unsigned bytes = recvfrom(s, buf, MAX_BUF_SIZE, 0, NULL, NULL); if (bytes >= (14 + 20 + 2)) { // size of MAC + IP + ICMP code and type fields @@ -180,7 +200,6 @@ static void run_trace(void) { close(s); } - static void usage(void) { printf("Usage: fnettrace-icmp [OPTIONS]\n"); printf("Options:\n"); @@ -207,8 +226,10 @@ int main(int argc, char **argv) { return 1; } - time_t now = time(NULL); - printf("ICMP trace for %s\n", ctime(&now)); + // kill the process if the parent died + prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); + + print_date(); run_trace(); return 0; -- cgit v1.2.3-70-g09d2