build: Rename LD command variable to LDQ
[netsniff-ng.git] / proto_nlmsg.c
blob6f3d31097e278972db583a63bd78c8588cd4c809
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 <limits.h>
10 #include <libnl3/netlink/msg.h>
11 #include <libgen.h>
13 #include "pkt_buff.h"
14 #include "proto.h"
15 #include "protos.h"
17 static void nlmsg(struct pkt_buff *pkt)
19 struct nlmsghdr *hdr = (struct nlmsghdr *) pkt_pull(pkt, sizeof(*hdr));
20 char type[32];
21 char flags[128];
22 char procname[PATH_MAX];
24 if (hdr == NULL)
25 return;
27 /* Look up the process name if message is not coming from the kernel.
29 * Note that the port id is not necessarily equal to the PID of the
30 * receiving process (e.g. if the application is multithreaded or using
31 * multiple sockets). In these cases we're not able to find a matching
32 * PID and the information will not be printed.
34 if (hdr->nlmsg_pid != 0) {
35 char path[1024];
36 int ret;
38 snprintf(path, sizeof(path), "/proc/%u/exe", hdr->nlmsg_pid);
39 ret = readlink(path, procname, sizeof(procname) - 1);
40 if (ret < 0)
41 ret = 0;
42 procname[ret] = '\0';
43 } else
44 snprintf(procname, sizeof(procname), "kernel");
46 tprintf(" [ NLMSG ");
47 tprintf("Len %u, ", hdr->nlmsg_len);
48 tprintf("Type 0x%.4x (%s%s%s), ", hdr->nlmsg_type,
49 colorize_start(bold),
50 nl_nlmsgtype2str(hdr->nlmsg_type, type, sizeof(type)),
51 colorize_end());
52 tprintf("Flags 0x%.4x (%s%s%s), ", hdr->nlmsg_flags,
53 colorize_start(bold),
54 nl_nlmsg_flags2str(hdr->nlmsg_flags, flags, sizeof(flags)),
55 colorize_end());
56 tprintf("Seq-Nr %u, ", hdr->nlmsg_seq);
57 tprintf("PID %u", hdr->nlmsg_pid);
58 if (procname[0])
59 tprintf(" (%s%s%s)", colorize_start(bold), basename(procname),
60 colorize_end());
61 tprintf(" ]\n");
64 static void nlmsg_less(struct pkt_buff *pkt)
66 struct nlmsghdr *hdr = (struct nlmsghdr *) pkt_pull(pkt, sizeof(*hdr));
67 char type[32];
69 if (hdr == NULL)
70 return;
72 tprintf(" NLMSG %u (%s%s%s)", hdr->nlmsg_type, colorize_start(bold),
73 nl_nlmsgtype2str(hdr->nlmsg_type, type, sizeof(type)),
74 colorize_end());
77 struct protocol nlmsg_ops = {
78 .print_full = nlmsg,
79 .print_less = nlmsg_less,