2 * Copyright (c) 2014 Zubin Mithra <zubin.mithra@gmail.com>
3 * Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <linux/netlink.h>
34 #include <linux/sock_diag.h>
35 #include <linux/inet_diag.h>
36 #include <linux/unix_diag.h>
37 #include <linux/rtnetlink.h>
39 #if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
40 # define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
45 # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path)
49 inet_send_query(const int fd, const int family, const int proto)
51 struct sockaddr_nl nladdr = {
52 .nl_family = AF_NETLINK
56 struct inet_diag_req_v2 idr;
59 .nlmsg_len = sizeof(req),
60 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
61 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
64 .sdiag_family = family,
65 .sdiag_protocol = proto,
71 .iov_len = sizeof(req)
74 .msg_name = (void*)&nladdr,
75 .msg_namelen = sizeof(nladdr),
81 if (sendmsg(fd, &msg, 0) < 0) {
91 inet_parse_response(const char *proto_name, const void *data, int data_len,
92 const unsigned long inode)
94 const struct inet_diag_msg *diag_msg = data;
95 static const char zero_addr[sizeof(struct in6_addr)];
96 socklen_t addr_size, text_size;
98 if (diag_msg->idiag_inode != inode)
101 switch(diag_msg->idiag_family) {
103 addr_size = sizeof(struct in_addr);
104 text_size = INET_ADDRSTRLEN;
107 addr_size = sizeof(struct in6_addr);
108 text_size = INET6_ADDRSTRLEN;
114 char src_buf[text_size];
116 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
120 if (diag_msg->id.idiag_dport ||
121 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
122 char dst_buf[text_size];
124 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
128 tprintf("%s:[%s:%u->%s:%u]",
130 src_buf, ntohs(diag_msg->id.idiag_sport),
131 dst_buf, ntohs(diag_msg->id.idiag_dport));
133 tprintf("%s:[%s:%u]", proto_name, src_buf,
134 ntohs(diag_msg->id.idiag_sport));
141 receive_responses(const int fd, const unsigned long inode,
142 const char *proto_name,
143 bool (* parser) (const char *, const void *, int, const unsigned long))
145 static long buf[8192 / sizeof(long)];
146 struct sockaddr_nl nladdr = {
147 .nl_family = AF_NETLINK
151 .iov_len = sizeof(buf)
157 struct msghdr msg = {
158 .msg_name = (void*)&nladdr,
159 .msg_namelen = sizeof(nladdr),
164 ret = recvmsg(fd, &msg, 0);
172 for (h = (struct nlmsghdr*)buf;
174 h = NLMSG_NEXT(h, ret)) {
175 switch (h->nlmsg_type) {
180 if (parser(proto_name, NLMSG_DATA(h), h->nlmsg_len, inode))
187 inet_print(const int fd, const int family, const int protocol,
188 const unsigned long inode, const char *proto_name)
190 return inet_send_query(fd, family, protocol)
191 && receive_responses(fd, inode, proto_name, inet_parse_response);
195 unix_send_query(const int fd, const unsigned long inode)
197 struct sockaddr_nl nladdr = {
198 .nl_family = AF_NETLINK
202 struct unix_diag_req udr;
205 .nlmsg_len = sizeof(req),
206 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
207 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
210 .sdiag_family = AF_UNIX,
213 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
218 .iov_len = sizeof(req)
220 struct msghdr msg = {
221 .msg_name = (void*)&nladdr,
222 .msg_namelen = sizeof(nladdr),
228 if (sendmsg(fd, &msg, 0) < 0) {
238 unix_parse_response(const char *proto_name, const void *data, int data_len,
239 const unsigned long inode)
241 const struct unix_diag_msg *diag_msg = data;
243 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
246 char path[UNIX_PATH_MAX + 1];
250 if (diag_msg->udiag_ino != inode)
252 if (diag_msg->udiag_family != AF_UNIX)
255 for (attr = (struct rtattr *) (diag_msg + 1);
256 RTA_OK(attr, rta_len);
257 attr = RTA_NEXT(attr, rta_len)) {
258 switch (attr->rta_type) {
261 path_len = RTA_PAYLOAD(attr);
262 if (path_len > UNIX_PATH_MAX)
263 path_len = UNIX_PATH_MAX;
264 memcpy(path, RTA_DATA(attr), path_len);
265 path[path_len] = '\0';
269 if (RTA_PAYLOAD(attr) >= 4)
270 peer = *(uint32_t *)RTA_DATA(attr);
276 * print obtained information in the following format:
277 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
279 if (peer || path_len) {
280 tprintf("%s:[%lu", proto_name, inode);
282 tprintf("->%u", peer);
284 if (path[0] == '\0') {
286 print_quoted_string(path + 1, path_len,
290 print_quoted_string(path, path_len + 1,
302 unix_print(int fd, const unsigned long inode)
304 return unix_send_query(fd, inode)
305 && receive_responses(fd, inode, "UNIX", unix_parse_response);
308 /* Given an inode number of a socket, print out the details
309 * of the ip address and port. */
311 print_sockaddr_by_inode(const unsigned long inode, const char *proto_name)
316 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
321 if (strcmp(proto_name, "TCP") == 0)
322 r = inet_print(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
323 else if (strcmp(proto_name, "UDP") == 0)
324 r = inet_print(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
325 else if (strcmp(proto_name, "TCPv6") == 0)
326 r = inet_print(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
327 else if (strcmp(proto_name, "UDPv6") == 0)
328 r = inet_print(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
329 else if (strcmp(proto_name, "UNIX") == 0)
330 r = unix_print(fd, inode);
333 tprintf("%s:[%lu]", proto_name, inode);
342 { AF_INET, IPPROTO_TCP, "TCP" },
343 { AF_INET, IPPROTO_UDP, "UDP" },
344 { AF_INET6, IPPROTO_TCP, "TCPv6" },
345 { AF_INET6, IPPROTO_UDP, "UDPv6" }
349 for (i = 0; i < ARRAY_SIZE(protocols); ++i) {
350 if ((r = inet_print(fd, protocols[i].family,
351 protocols[i].protocol, inode,