2 * Copyright (c) 2014 Zubin Mithra <zubin.mithra@gmail.com>
3 * Copyright (c) 2014-2016 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)
53 #define CACHE_SIZE 1024U
54 static cache_entry cache[CACHE_SIZE];
55 #define CACHE_MASK (CACHE_SIZE - 1)
58 cache_and_print_inode_details(const unsigned long inode, char *const details)
60 cache_entry *e = &cache[inode & CACHE_MASK];
70 print_sockaddr_by_inode_cached(const unsigned long inode)
72 const cache_entry *const e = &cache[inode & CACHE_MASK];
73 if (e && inode == e->inode) {
81 inet_send_query(const int fd, const int family, const int proto)
83 struct sockaddr_nl nladdr = {
84 .nl_family = AF_NETLINK
87 const struct nlmsghdr nlh;
88 const struct inet_diag_req_v2 idr;
91 .nlmsg_len = sizeof(req),
92 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
93 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
96 .sdiag_family = family,
97 .sdiag_protocol = proto,
103 .iov_len = sizeof(req)
105 const struct msghdr msg = {
107 .msg_namelen = sizeof(nladdr),
113 if (sendmsg(fd, &msg, 0) < 0) {
123 inet_parse_response(const char *const proto_name, const void *const data,
124 const int data_len, const unsigned long inode)
126 const struct inet_diag_msg *const diag_msg = data;
127 static const char zero_addr[sizeof(struct in6_addr)];
128 socklen_t addr_size, text_size;
130 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
132 if (diag_msg->idiag_inode != inode)
135 switch(diag_msg->idiag_family) {
137 addr_size = sizeof(struct in_addr);
138 text_size = INET_ADDRSTRLEN;
141 addr_size = sizeof(struct in6_addr);
142 text_size = INET6_ADDRSTRLEN;
148 char src_buf[text_size];
151 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
155 if (diag_msg->id.idiag_dport ||
156 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
157 char dst_buf[text_size];
159 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
163 if (asprintf(&details, "%s:[%s:%u->%s:%u]", proto_name,
164 src_buf, ntohs(diag_msg->id.idiag_sport),
165 dst_buf, ntohs(diag_msg->id.idiag_dport)) < 0)
168 if (asprintf(&details, "%s:[%s:%u]", proto_name, src_buf,
169 ntohs(diag_msg->id.idiag_sport)) < 0)
173 return cache_and_print_inode_details(inode, details);
177 receive_responses(const int fd, const unsigned long inode,
178 const char *proto_name,
179 int (* parser) (const char *, const void *,
182 static long buf[8192 / sizeof(long)];
183 struct sockaddr_nl nladdr = {
184 .nl_family = AF_NETLINK
188 .iov_len = sizeof(buf)
193 struct msghdr msg = {
195 .msg_namelen = sizeof(nladdr),
200 ssize_t ret = recvmsg(fd, &msg, flags);
207 const struct nlmsghdr *h = (struct nlmsghdr *) buf;
208 if (!NLMSG_OK(h, ret))
210 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
211 if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
213 const int rc = parser(proto_name, NLMSG_DATA(h),
214 h->nlmsg_len, inode);
220 flags = MSG_DONTWAIT;
225 inet_print(const int fd, const int family, const int protocol,
226 const unsigned long inode, const char *proto_name)
228 return inet_send_query(fd, family, protocol)
229 && receive_responses(fd, inode, proto_name, inet_parse_response);
233 unix_send_query(const int fd, const unsigned long inode)
235 struct sockaddr_nl nladdr = {
236 .nl_family = AF_NETLINK
239 const struct nlmsghdr nlh;
240 const struct unix_diag_req udr;
243 .nlmsg_len = sizeof(req),
244 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
245 .nlmsg_flags = NLM_F_REQUEST
248 .sdiag_family = AF_UNIX,
251 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER,
252 .udiag_cookie = { ~0U, ~0U }
257 .iov_len = sizeof(req)
259 const struct msghdr msg = {
261 .msg_namelen = sizeof(nladdr),
267 if (sendmsg(fd, &msg, 0) < 0) {
277 unix_parse_response(const char *proto_name, const void *data,
278 const int data_len, const unsigned long inode)
280 const struct unix_diag_msg *diag_msg = data;
282 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
285 char path[UNIX_PATH_MAX + 1];
289 if (diag_msg->udiag_ino != inode)
291 if (diag_msg->udiag_family != AF_UNIX)
294 for (attr = (struct rtattr *) (diag_msg + 1);
295 RTA_OK(attr, rta_len);
296 attr = RTA_NEXT(attr, rta_len)) {
297 switch (attr->rta_type) {
300 path_len = RTA_PAYLOAD(attr);
301 if (path_len > UNIX_PATH_MAX)
302 path_len = UNIX_PATH_MAX;
303 memcpy(path, RTA_DATA(attr), path_len);
304 path[path_len] = '\0';
308 if (RTA_PAYLOAD(attr) >= 4)
309 peer = *(uint32_t *) RTA_DATA(attr);
315 * print obtained information in the following format:
316 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
318 if (!peer && !path_len)
321 char peer_str[3 + sizeof(peer) * 3];
323 snprintf(peer_str, sizeof(peer_str), "->%u", peer);
327 const char *path_str;
329 char *outstr = alloca(4 * path_len + 4);
332 if (path[0] == '\0') {
334 string_quote(path + 1, outstr + 2,
335 path_len - 1, QUOTE_0_TERMINATED);
337 string_quote(path, outstr + 1,
338 path_len, QUOTE_0_TERMINATED);
346 if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
347 peer_str, path_str) < 0)
350 return cache_and_print_inode_details(inode, details);
354 unix_print(const int fd, const unsigned long inode)
356 return unix_send_query(fd, inode)
357 && receive_responses(fd, inode, "UNIX", unix_parse_response);
361 tcp_v4_print(const int fd, const unsigned long inode)
363 return inet_print(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
367 udp_v4_print(const int fd, const unsigned long inode)
369 return inet_print(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
373 tcp_v6_print(const int fd, const unsigned long inode)
375 return inet_print(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
379 udp_v6_print(const int fd, const unsigned long inode)
381 return inet_print(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
384 /* Given an inode number of a socket, print out the details
385 * of the ip address and port. */
387 print_sockaddr_by_inode(const unsigned long inode, const char *const proto_name)
389 static const struct {
390 const char *const name;
391 bool (*const print)(int, unsigned long);
393 { "TCP", tcp_v4_print },
394 { "UDP", udp_v4_print },
395 { "TCPv6", tcp_v6_print },
396 { "UDPv6", udp_v6_print },
397 { "UNIX", unix_print }
400 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
407 for (i = 0; i < ARRAY_SIZE(protocols); ++i) {
408 if (strcmp(proto_name, protocols[i].name) == 0) {
409 r = protocols[i].print(fd, inode);
415 tprintf("%s:[%lu]", proto_name, inode);
419 for (i = 0; i < ARRAY_SIZE(protocols); ++i) {
420 if ((r = protocols[i].print(fd, inode)))