mausezahn: Don't use ternary operator to decide which function to call
[netsniff-ng.git] / proto_nlmsg.c
blob0098d2772693121542934398b716c8f2e0cd35a8
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2014 Tobias Klauser.
4 * Subject to the GPL, version 2.
5 */
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <libnl3/netlink/msg.h>
10 #include <libgen.h>
12 #include "pkt_buff.h"
13 #include "proto.h"
15 static void nlmsg(struct pkt_buff *pkt)
17 struct nlmsghdr *hdr = (struct nlmsghdr *) pkt_pull(pkt, sizeof(*hdr));
18 char type[32];
19 char flags[128];
20 char procname[1024];
22 if (hdr == NULL)
23 return;
25 /* Look up the process name if message is not coming from the kernel */
26 if (hdr->nlmsg_pid != 0) {
27 char path[1024];
28 int ret;
30 snprintf(path, sizeof(path), "/proc/%u/exe", hdr->nlmsg_pid);
31 ret = readlink(path, procname, sizeof(procname) - 1);
32 if (ret < 0)
33 procname[0] = '\0';
34 } else
35 snprintf(procname, sizeof(procname), "kernel");
37 tprintf(" [ NLMSG ");
38 tprintf("Len %u, ", hdr->nlmsg_len);
39 tprintf("Type 0x%.4x (%s%s%s), ", hdr->nlmsg_type,
40 colorize_start(bold),
41 nl_nlmsgtype2str(hdr->nlmsg_type, type, sizeof(type)),
42 colorize_end());
43 tprintf("Flags 0x%.4x (%s%s%s), ", hdr->nlmsg_flags,
44 colorize_start(bold),
45 nl_nlmsg_flags2str(hdr->nlmsg_flags, flags, sizeof(flags)),
46 colorize_end());
47 tprintf("Seq-Nr %u, ", hdr->nlmsg_seq);
48 tprintf("PID %u", hdr->nlmsg_pid);
49 if (procname[0])
50 tprintf(" (%s%s%s)", colorize_start(bold), basename(procname),
51 colorize_end());
52 tprintf(" ]\n");
55 static void nlmsg_less(struct pkt_buff *pkt)
57 struct nlmsghdr *hdr = (struct nlmsghdr *) pkt_pull(pkt, sizeof(*hdr));
58 char type[32];
60 if (hdr == NULL)
61 return;
63 tprintf(" NLMSG %u (%s%s%s)", hdr->nlmsg_type, colorize_start(bold),
64 nl_nlmsgtype2str(hdr->nlmsg_type, type, sizeof(type)),
65 colorize_end());
68 struct protocol nlmsg_ops = {
69 .print_full = nlmsg,
70 .print_less = nlmsg_less,