mz: Allow to print packet types by '-t help'
[netsniff-ng.git] / proto_nlmsg.c
blob347109456d9727b047f5050e413d4ff336344a9e
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"
14 #include "protos.h"
16 static void nlmsg(struct pkt_buff *pkt)
18 struct nlmsghdr *hdr = (struct nlmsghdr *) pkt_pull(pkt, sizeof(*hdr));
19 char type[32];
20 char flags[128];
21 char procname[1024];
23 if (hdr == NULL)
24 return;
26 /* Look up the process name if message is not coming from the kernel.
28 * Note that the port id is not necessarily equal to the PID of the
29 * receiving process (e.g. if the application is multithreaded or using
30 * multiple sockets). In these cases we're not able to find a matching
31 * PID and the information will not be printed.
33 if (hdr->nlmsg_pid != 0) {
34 char path[1024];
35 int ret;
37 snprintf(path, sizeof(path), "/proc/%u/exe", hdr->nlmsg_pid);
38 ret = readlink(path, procname, sizeof(procname) - 1);
39 if (ret < 0)
40 procname[0] = '\0';
41 } else
42 snprintf(procname, sizeof(procname), "kernel");
44 tprintf(" [ NLMSG ");
45 tprintf("Len %u, ", hdr->nlmsg_len);
46 tprintf("Type 0x%.4x (%s%s%s), ", hdr->nlmsg_type,
47 colorize_start(bold),
48 nl_nlmsgtype2str(hdr->nlmsg_type, type, sizeof(type)),
49 colorize_end());
50 tprintf("Flags 0x%.4x (%s%s%s), ", hdr->nlmsg_flags,
51 colorize_start(bold),
52 nl_nlmsg_flags2str(hdr->nlmsg_flags, flags, sizeof(flags)),
53 colorize_end());
54 tprintf("Seq-Nr %u, ", hdr->nlmsg_seq);
55 tprintf("PID %u", hdr->nlmsg_pid);
56 if (procname[0])
57 tprintf(" (%s%s%s)", colorize_start(bold), basename(procname),
58 colorize_end());
59 tprintf(" ]\n");
62 static void nlmsg_less(struct pkt_buff *pkt)
64 struct nlmsghdr *hdr = (struct nlmsghdr *) pkt_pull(pkt, sizeof(*hdr));
65 char type[32];
67 if (hdr == NULL)
68 return;
70 tprintf(" NLMSG %u (%s%s%s)", hdr->nlmsg_type, colorize_start(bold),
71 nl_nlmsgtype2str(hdr->nlmsg_type, type, sizeof(type)),
72 colorize_end());
75 struct protocol nlmsg_ops = {
76 .print_full = nlmsg,
77 .print_less = nlmsg_less,