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 (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
100 if (diag_msg->idiag_inode != inode)
103 switch(diag_msg->idiag_family) {
105 addr_size = sizeof(struct in_addr);
106 text_size = INET_ADDRSTRLEN;
109 addr_size = sizeof(struct in6_addr);
110 text_size = INET6_ADDRSTRLEN;
116 char src_buf[text_size];
118 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
122 if (diag_msg->id.idiag_dport ||
123 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
124 char dst_buf[text_size];
126 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
130 tprintf("%s:[%s:%u->%s:%u]",
132 src_buf, ntohs(diag_msg->id.idiag_sport),
133 dst_buf, ntohs(diag_msg->id.idiag_dport));
135 tprintf("%s:[%s:%u]", proto_name, src_buf,
136 ntohs(diag_msg->id.idiag_sport));
143 receive_responses(const int fd, const unsigned long inode,
144 const char *proto_name,
145 bool (* parser) (const char *, const void *, int, const unsigned long))
147 static long buf[8192 / sizeof(long)];
148 struct sockaddr_nl nladdr = {
149 .nl_family = AF_NETLINK
153 .iov_len = sizeof(buf)
160 struct msghdr msg = {
161 .msg_name = (void*)&nladdr,
162 .msg_namelen = sizeof(nladdr),
167 ret = recvmsg(fd, &msg, flags);
175 for (h = (struct nlmsghdr*)buf;
177 h = NLMSG_NEXT(h, ret)) {
178 switch (h->nlmsg_type) {
183 if (parser(proto_name, NLMSG_DATA(h), h->nlmsg_len, inode))
186 flags = MSG_DONTWAIT;
191 inet_print(const int fd, const int family, const int protocol,
192 const unsigned long inode, const char *proto_name)
194 return inet_send_query(fd, family, protocol)
195 && receive_responses(fd, inode, proto_name, inet_parse_response);
199 unix_send_query(const int fd, const unsigned long inode)
201 struct sockaddr_nl nladdr = {
202 .nl_family = AF_NETLINK
206 struct unix_diag_req udr;
209 .nlmsg_len = sizeof(req),
210 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
211 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
214 .sdiag_family = AF_UNIX,
217 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
222 .iov_len = sizeof(req)
224 struct msghdr msg = {
225 .msg_name = (void*)&nladdr,
226 .msg_namelen = sizeof(nladdr),
232 if (sendmsg(fd, &msg, 0) < 0) {
242 unix_parse_response(const char *proto_name, const void *data, int data_len,
243 const unsigned long inode)
245 const struct unix_diag_msg *diag_msg = data;
247 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
250 char path[UNIX_PATH_MAX + 1];
254 if (diag_msg->udiag_ino != inode)
256 if (diag_msg->udiag_family != AF_UNIX)
259 for (attr = (struct rtattr *) (diag_msg + 1);
260 RTA_OK(attr, rta_len);
261 attr = RTA_NEXT(attr, rta_len)) {
262 switch (attr->rta_type) {
265 path_len = RTA_PAYLOAD(attr);
266 if (path_len > UNIX_PATH_MAX)
267 path_len = UNIX_PATH_MAX;
268 memcpy(path, RTA_DATA(attr), path_len);
269 path[path_len] = '\0';
273 if (RTA_PAYLOAD(attr) >= 4)
274 peer = *(uint32_t *)RTA_DATA(attr);
280 * print obtained information in the following format:
281 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
283 if (peer || path_len) {
284 tprintf("%s:[%lu", proto_name, inode);
286 tprintf("->%u", peer);
288 if (path[0] == '\0') {
290 print_quoted_string(path + 1, path_len,
294 print_quoted_string(path, path_len + 1,
306 unix_print(int fd, const unsigned long inode)
308 return unix_send_query(fd, inode)
309 && receive_responses(fd, inode, "UNIX", unix_parse_response);
312 /* Given an inode number of a socket, print out the details
313 * of the ip address and port. */
315 print_sockaddr_by_inode(const unsigned long inode, const char *proto_name)
320 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
325 if (strcmp(proto_name, "TCP") == 0)
326 r = inet_print(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
327 else if (strcmp(proto_name, "UDP") == 0)
328 r = inet_print(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
329 else if (strcmp(proto_name, "TCPv6") == 0)
330 r = inet_print(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
331 else if (strcmp(proto_name, "UDPv6") == 0)
332 r = inet_print(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
333 else if (strcmp(proto_name, "UNIX") == 0)
334 r = unix_print(fd, inode);
337 tprintf("%s:[%lu]", proto_name, inode);
346 { AF_INET, IPPROTO_TCP, "TCP" },
347 { AF_INET, IPPROTO_UDP, "UDP" },
348 { AF_INET6, IPPROTO_TCP, "TCPv6" },
349 { AF_INET6, IPPROTO_UDP, "UDPv6" }
353 for (i = 0; i < ARRAY_SIZE(protocols); ++i) {
354 if ((r = inet_print(fd, protocols[i].family,
355 protocols[i].protocol, inode,