2 #include <netinet/in.h>
3 #include <sys/socket.h>
5 #include <linux/netlink.h>
6 #include <linux/sock_diag.h>
7 #include <linux/inet_diag.h>
8 #include <linux/unix_diag.h>
9 #include <linux/rtnetlink.h>
11 #if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
12 # define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
17 # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path)
21 inet_send_query(const int fd, const int family, const int proto)
23 struct sockaddr_nl nladdr = {
24 .nl_family = AF_NETLINK
28 struct inet_diag_req_v2 idr;
31 .nlmsg_len = sizeof(req),
32 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
33 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
36 .sdiag_family = family,
37 .sdiag_protocol = proto,
43 .iov_len = sizeof(req)
46 .msg_name = (void*)&nladdr,
47 .msg_namelen = sizeof(nladdr),
53 if (sendmsg(fd, &msg, 0) < 0) {
63 inet_parse_response(const char *proto_name, const void *data, int data_len,
64 const unsigned long inode)
66 const struct inet_diag_msg *diag_msg = data;
67 static const char zero_addr[sizeof(struct in6_addr)];
68 socklen_t addr_size, text_size;
70 if (diag_msg->idiag_inode != inode)
73 switch(diag_msg->idiag_family) {
75 addr_size = sizeof(struct in_addr);
76 text_size = INET_ADDRSTRLEN;
79 addr_size = sizeof(struct in6_addr);
80 text_size = INET6_ADDRSTRLEN;
86 char src_buf[text_size];
88 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
92 if (diag_msg->id.idiag_dport ||
93 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
94 char dst_buf[text_size];
96 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
100 tprintf("%s:[%s:%u->%s:%u]",
102 src_buf, ntohs(diag_msg->id.idiag_sport),
103 dst_buf, ntohs(diag_msg->id.idiag_dport));
105 tprintf("%s:[%s:%u]", proto_name, src_buf,
106 ntohs(diag_msg->id.idiag_sport));
113 receive_responses(const int fd, const unsigned long inode,
114 const char *proto_name,
115 bool (* parser) (const char *, const void *, int, const unsigned long))
117 static char buf[8192];
118 struct sockaddr_nl nladdr = {
119 .nl_family = AF_NETLINK
123 .iov_len = sizeof(buf)
129 struct msghdr msg = {
130 .msg_name = (void*)&nladdr,
131 .msg_namelen = sizeof(nladdr),
136 ret = recvmsg(fd, &msg, 0);
144 for (h = (struct nlmsghdr*)buf;
146 h = NLMSG_NEXT(h, ret)) {
147 switch (h->nlmsg_type) {
152 if (parser(proto_name, NLMSG_DATA(h), h->nlmsg_len, inode))
159 inet_print(const int fd, const int family, const int protocol,
160 const unsigned long inode, const char *proto_name)
162 return inet_send_query(fd, family, protocol)
163 && receive_responses(fd, inode, proto_name, inet_parse_response);
167 unix_send_query(const int fd, const unsigned long inode)
169 struct sockaddr_nl nladdr = {
170 .nl_family = AF_NETLINK
174 struct unix_diag_req udr;
177 .nlmsg_len = sizeof(req),
178 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
179 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
182 .sdiag_family = AF_UNIX,
185 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
190 .iov_len = sizeof(req)
192 struct msghdr msg = {
193 .msg_name = (void*)&nladdr,
194 .msg_namelen = sizeof(nladdr),
200 if (sendmsg(fd, &msg, 0) < 0) {
210 unix_parse_response(const char *proto_name, const void *data, int data_len,
211 const unsigned long inode)
213 const struct unix_diag_msg *diag_msg = data;
215 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
218 char path[UNIX_PATH_MAX + 1];
220 if (diag_msg->udiag_ino != inode)
222 if (diag_msg->udiag_family != AF_UNIX)
225 for (attr = (struct rtattr *) (diag_msg + 1);
226 RTA_OK(attr, rta_len);
227 attr = RTA_NEXT(attr, rta_len)) {
228 switch (attr->rta_type) {
231 path_len = RTA_PAYLOAD(attr);
232 if (path_len > UNIX_PATH_MAX)
233 path_len = UNIX_PATH_MAX;
234 memcpy(path, RTA_DATA(attr), path_len);
235 path[path_len] = '\0';
239 if (RTA_PAYLOAD(attr) >= 4)
240 peer = *(uint32_t *)RTA_DATA(attr);
246 * print obtained information in the following format:
247 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
249 if (peer || path_len) {
250 tprintf("%s:[%lu", proto_name, inode);
252 tprintf("->%u", peer);
254 if (path[0] == '\0') {
256 print_quoted_string(path + 1, path_len,
260 print_quoted_string(path, path_len + 1,
272 unix_print(int fd, const unsigned long inode)
274 return unix_send_query(fd, inode)
275 && receive_responses(fd, inode, "UNIX", unix_parse_response);
278 /* Given an inode number of a socket, print out the details
279 * of the ip address and port. */
281 print_sockaddr_by_inode(const unsigned long inode, const char *proto_name)
286 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
291 if (strcmp(proto_name, "TCP") == 0)
292 r = inet_print(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
293 else if (strcmp(proto_name, "UDP") == 0)
294 r = inet_print(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
295 else if (strcmp(proto_name, "TCPv6") == 0)
296 r = inet_print(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
297 else if (strcmp(proto_name, "UDPv6") == 0)
298 r = inet_print(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
299 else if (strcmp(proto_name, "UNIX") == 0)
300 r = unix_print(fd, inode);
307 { AF_INET, IPPROTO_TCP, "TCP" },
308 { AF_INET, IPPROTO_UDP, "UDP" },
309 { AF_INET6, IPPROTO_TCP, "TCPv6" },
310 { AF_INET6, IPPROTO_UDP, "UDPv6" }
314 for (i = 0; i < ARRAY_SIZE(protocols); ++i) {
315 if ((r = inet_print(fd, protocols[i].family,
316 protocols[i].protocol, inode,