From e4c4bf1e7685b4cc37b16feacbfba87298cfddb3 Mon Sep 17 00:00:00 2001 From: wubijie Date: Wed, 21 Jun 2023 16:00:43 +0800 Subject: [PATCH] Handle events and output relevant information --- observation/src/bindsnoop/bindsnoop.c | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/observation/src/bindsnoop/bindsnoop.c b/observation/src/bindsnoop/bindsnoop.c index c833692f..257c89ee 100644 --- a/observation/src/bindsnoop/bindsnoop.c +++ b/observation/src/bindsnoop/bindsnoop.c @@ -114,3 +114,41 @@ static void sig_handler(int sig) { exiting = 1; } + +static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz) +{ + struct bind_event *e = data; + char addr[48]; + char opts[] = { 'F', 'T', 'N', 'R', 'r', '\0' }; + const char *proto; + int i = 0; + + if (env.emit_timestamp) { + char ts[32]; + + strftime_now(ts, sizeof(ts), "%H:%M:%S"); + printf("%8s ", ts); + } + + if (e->proto == IPPROTO_TCP) + proto = "TCP"; + else if (e->proto == IPPROTO_UDP) + proto = "UDP"; + else + proto = "UNK"; + + while (opts[i]) { + if (!((1 << i) & e->opts)) { + opts[i] = '.'; + } + i++; + } + + if (e->ver == 4) + inet_ntop(AF_INET, &e->addr, addr, sizeof(addr)); + else + inet_ntop(AF_INET6, &e->addr, addr, sizeof(addr)); + + printf("%-7d %-16s %-3d %-5s %-5s %-4d %-5d %-48s\n", + e->pid, e->task, e->ret, proto, opts, e->bound_dev_if, e->port, addr); +} \ No newline at end of file -- Gitee