netsniff-ng 0.5.9-rc2
[netsniff-ng.git] / proto_nlmsg.c
blob3f5ef64cd7ac6733f00290ae22d40ae1f0f80dd1
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.
27 * Note that the port id is not necessarily equal to the PID of the
28 * receiving process (e.g. if the application is multithreaded or using
29 * multiple sockets). In these cases we're not able to find a matching
30 * PID and the information will not be printed.
32 if (hdr->nlmsg_pid != 0) {
33 char path[1024];
34 int ret;
36 snprintf(path, sizeof(path), "/proc/%u/exe", hdr->nlmsg_pid);
37 ret = readlink(path, procname, sizeof(procname) - 1);
38 if (ret < 0)
39 procname[0] = '\0';
40 } else
41 snprintf(procname, sizeof(procname), "kernel");
43 tprintf(" [ NLMSG ");
44 tprintf("Len %u, ", hdr->nlmsg_len);
45 tprintf("Type 0x%.4x (%s%s%s), ", hdr->nlmsg_type,
46 colorize_start(bold),
47 nl_nlmsgtype2str(hdr->nlmsg_type, type, sizeof(type)),
48 colorize_end());
49 tprintf("Flags 0x%.4x (%s%s%s), ", hdr->nlmsg_flags,
50 colorize_start(bold),
51 nl_nlmsg_flags2str(hdr->nlmsg_flags, flags, sizeof(flags)),
52 colorize_end());
53 tprintf("Seq-Nr %u, ", hdr->nlmsg_seq);
54 tprintf("PID %u", hdr->nlmsg_pid);
55 if (procname[0])
56 tprintf(" (%s%s%s)", colorize_start(bold), basename(procname),
57 colorize_end());
58 tprintf(" ]\n");
61 static void nlmsg_less(struct pkt_buff *pkt)
63 struct nlmsghdr *hdr = (struct nlmsghdr *) pkt_pull(pkt, sizeof(*hdr));
64 char type[32];
66 if (hdr == NULL)
67 return;
69 tprintf(" NLMSG %u (%s%s%s)", hdr->nlmsg_type, colorize_start(bold),
70 nl_nlmsgtype2str(hdr->nlmsg_type, type, sizeof(type)),
71 colorize_end());
74 struct protocol nlmsg_ops = {
75 .print_full = nlmsg,
76 .print_less = nlmsg_less,