Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Zubin Mithra <zubin.mithra@gmail.com> |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 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. |
| 16 | * |
| 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. |
| 27 | */ |
| 28 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 29 | #include "defs.h" |
| 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> |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 36 | #include <linux/unix_diag.h> |
| 37 | #include <linux/rtnetlink.h> |
| 38 | |
Dmitry V. Levin | d9f7e7a | 2015-01-09 03:03:39 +0000 | [diff] [blame] | 39 | #if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG |
| 40 | # define NETLINK_SOCK_DIAG NETLINK_INET_DIAG |
| 41 | #endif |
| 42 | |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 43 | #include <sys/un.h> |
| 44 | #ifndef UNIX_PATH_MAX |
| 45 | # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path) |
| 46 | #endif |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 47 | |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 48 | typedef struct { |
| 49 | unsigned long inode; |
| 50 | char *details; |
| 51 | } cache_entry; |
| 52 | |
| 53 | #define CACHE_SIZE 1024U |
| 54 | static cache_entry cache[CACHE_SIZE]; |
| 55 | #define CACHE_MASK (CACHE_SIZE - 1) |
| 56 | |
| 57 | static int |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 58 | cache_and_print_inode_details(const unsigned long inode, char *const details) |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 59 | { |
| 60 | cache_entry *e = &cache[inode & CACHE_MASK]; |
| 61 | free(e->details); |
| 62 | e->inode = inode; |
| 63 | e->details = details; |
| 64 | |
| 65 | tprints(details); |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | bool |
| 70 | print_sockaddr_by_inode_cached(const unsigned long inode) |
| 71 | { |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 72 | const cache_entry *const e = &cache[inode & CACHE_MASK]; |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 73 | if (e && inode == e->inode) { |
| 74 | tprints(e->details); |
| 75 | return true; |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 80 | static bool |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 81 | send_query(const int fd, void *req, size_t req_size) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 82 | { |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 83 | struct sockaddr_nl nladdr = { |
| 84 | .nl_family = AF_NETLINK |
| 85 | }; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 86 | struct iovec iov = { |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 87 | .iov_base = req, |
| 88 | .iov_len = req_size |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 89 | }; |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 90 | const struct msghdr msg = { |
| 91 | .msg_name = &nladdr, |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 92 | .msg_namelen = sizeof(nladdr), |
| 93 | .msg_iov = &iov, |
| 94 | .msg_iovlen = 1 |
| 95 | }; |
| 96 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 97 | for (;;) { |
| 98 | if (sendmsg(fd, &msg, 0) < 0) { |
| 99 | if (errno == EINTR) |
| 100 | continue; |
| 101 | return false; |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 107 | static bool |
| 108 | inet_send_query(const int fd, const int family, const int proto) |
| 109 | { |
| 110 | struct { |
| 111 | const struct nlmsghdr nlh; |
| 112 | const struct inet_diag_req_v2 idr; |
| 113 | } req = { |
| 114 | .nlh = { |
| 115 | .nlmsg_len = sizeof(req), |
| 116 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, |
| 117 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST |
| 118 | }, |
| 119 | .idr = { |
| 120 | .sdiag_family = family, |
| 121 | .sdiag_protocol = proto, |
| 122 | .idiag_states = -1 |
| 123 | } |
| 124 | }; |
| 125 | return send_query(fd, &req, sizeof(req)); |
| 126 | } |
| 127 | |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 128 | static int |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 129 | inet_parse_response(const char *const proto_name, const void *const data, |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 130 | const int data_len, const unsigned long inode) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 131 | { |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 132 | const struct inet_diag_msg *const diag_msg = data; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 133 | static const char zero_addr[sizeof(struct in6_addr)]; |
| 134 | socklen_t addr_size, text_size; |
| 135 | |
Dmitry V. Levin | cc09ba1 | 2016-01-28 23:46:56 +0000 | [diff] [blame] | 136 | if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg))) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 137 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 138 | if (diag_msg->idiag_inode != inode) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 139 | return 0; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 140 | |
| 141 | switch(diag_msg->idiag_family) { |
| 142 | case AF_INET: |
| 143 | addr_size = sizeof(struct in_addr); |
| 144 | text_size = INET_ADDRSTRLEN; |
| 145 | break; |
| 146 | case AF_INET6: |
| 147 | addr_size = sizeof(struct in6_addr); |
| 148 | text_size = INET6_ADDRSTRLEN; |
| 149 | break; |
| 150 | default: |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 151 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | char src_buf[text_size]; |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 155 | char *details; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 156 | |
| 157 | if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src, |
| 158 | src_buf, text_size)) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 159 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 160 | |
| 161 | if (diag_msg->id.idiag_dport || |
| 162 | memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) { |
| 163 | char dst_buf[text_size]; |
| 164 | |
| 165 | if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst, |
| 166 | dst_buf, text_size)) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 167 | return -1; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 168 | |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 169 | if (asprintf(&details, "%s:[%s:%u->%s:%u]", proto_name, |
| 170 | src_buf, ntohs(diag_msg->id.idiag_sport), |
| 171 | dst_buf, ntohs(diag_msg->id.idiag_dport)) < 0) |
| 172 | return false; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 173 | } else { |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 174 | if (asprintf(&details, "%s:[%s:%u]", proto_name, src_buf, |
| 175 | ntohs(diag_msg->id.idiag_sport)) < 0) |
| 176 | return false; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 179 | return cache_and_print_inode_details(inode, details); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static bool |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 183 | receive_responses(const int fd, const unsigned long inode, |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 184 | const char *proto_name, |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 185 | int (* parser) (const char *, const void *, |
| 186 | int, unsigned long)) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 187 | { |
Dmitry V. Levin | 301c65c | 2015-03-02 23:39:41 +0000 | [diff] [blame] | 188 | static long buf[8192 / sizeof(long)]; |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 189 | struct sockaddr_nl nladdr = { |
| 190 | .nl_family = AF_NETLINK |
| 191 | }; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 192 | struct iovec iov = { |
| 193 | .iov_base = buf, |
| 194 | .iov_len = sizeof(buf) |
| 195 | }; |
Dmitry V. Levin | 2215c3e | 2016-01-27 21:35:50 +0000 | [diff] [blame] | 196 | int flags = 0; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 197 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 198 | for (;;) { |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 199 | struct msghdr msg = { |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 200 | .msg_name = &nladdr, |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 201 | .msg_namelen = sizeof(nladdr), |
| 202 | .msg_iov = &iov, |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 203 | .msg_iovlen = 1 |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 206 | ssize_t ret = recvmsg(fd, &msg, flags); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 207 | if (ret < 0) { |
| 208 | if (errno == EINTR) |
| 209 | continue; |
| 210 | return false; |
| 211 | } |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 212 | |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 213 | const struct nlmsghdr *h = (struct nlmsghdr *) buf; |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 214 | if (!NLMSG_OK(h, ret)) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 215 | return false; |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 216 | for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) { |
| 217 | if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY) |
| 218 | return false; |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 219 | const int rc = parser(proto_name, NLMSG_DATA(h), |
| 220 | h->nlmsg_len, inode); |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 221 | if (rc > 0) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 222 | return true; |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 223 | if (rc < 0) |
| 224 | return false; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 225 | } |
Dmitry V. Levin | 2215c3e | 2016-01-27 21:35:50 +0000 | [diff] [blame] | 226 | flags = MSG_DONTWAIT; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 230 | static bool |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 231 | inet_print(const int fd, const int family, const int protocol, |
| 232 | const unsigned long inode, const char *proto_name) |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 233 | { |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 234 | return inet_send_query(fd, family, protocol) |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 235 | && receive_responses(fd, inode, proto_name, inet_parse_response); |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static bool |
| 239 | unix_send_query(const int fd, const unsigned long inode) |
| 240 | { |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 241 | struct { |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 242 | const struct nlmsghdr nlh; |
| 243 | const struct unix_diag_req udr; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 244 | } req = { |
| 245 | .nlh = { |
| 246 | .nlmsg_len = sizeof(req), |
| 247 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, |
Dmitry V. Levin | 10c61e3 | 2016-02-19 01:30:34 +0000 | [diff] [blame] | 248 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 249 | }, |
| 250 | .udr = { |
| 251 | .sdiag_family = AF_UNIX, |
| 252 | .udiag_ino = inode, |
| 253 | .udiag_states = -1, |
Dmitry V. Levin | 10c61e3 | 2016-02-19 01:30:34 +0000 | [diff] [blame] | 254 | .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 255 | } |
| 256 | }; |
Fabien Siron | 071b927 | 2016-05-08 10:45:52 +0000 | [diff] [blame] | 257 | return send_query(fd, &req, sizeof(req)); |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 258 | } |
| 259 | |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 260 | static int |
| 261 | unix_parse_response(const char *proto_name, const void *data, |
| 262 | const int data_len, const unsigned long inode) |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 263 | { |
| 264 | const struct unix_diag_msg *diag_msg = data; |
| 265 | struct rtattr *attr; |
| 266 | int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg)); |
| 267 | uint32_t peer = 0; |
| 268 | size_t path_len = 0; |
| 269 | char path[UNIX_PATH_MAX + 1]; |
| 270 | |
Dmitry V. Levin | 3d0f55e | 2016-01-24 01:46:40 +0300 | [diff] [blame] | 271 | if (rta_len < 0) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 272 | return -1; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 273 | if (diag_msg->udiag_ino != inode) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 274 | return 0; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 275 | if (diag_msg->udiag_family != AF_UNIX) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 276 | return -1; |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 277 | |
| 278 | for (attr = (struct rtattr *) (diag_msg + 1); |
| 279 | RTA_OK(attr, rta_len); |
| 280 | attr = RTA_NEXT(attr, rta_len)) { |
| 281 | switch (attr->rta_type) { |
| 282 | case UNIX_DIAG_NAME: |
| 283 | if (!path_len) { |
| 284 | path_len = RTA_PAYLOAD(attr); |
| 285 | if (path_len > UNIX_PATH_MAX) |
| 286 | path_len = UNIX_PATH_MAX; |
| 287 | memcpy(path, RTA_DATA(attr), path_len); |
| 288 | path[path_len] = '\0'; |
| 289 | } |
| 290 | break; |
| 291 | case UNIX_DIAG_PEER: |
| 292 | if (RTA_PAYLOAD(attr) >= 4) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 293 | peer = *(uint32_t *) RTA_DATA(attr); |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * print obtained information in the following format: |
| 300 | * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]" |
| 301 | */ |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 302 | if (!peer && !path_len) |
Dmitry V. Levin | 3c86e0e | 2016-01-30 23:50:54 +0000 | [diff] [blame] | 303 | return -1; |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 304 | |
| 305 | char peer_str[3 + sizeof(peer) * 3]; |
| 306 | if (peer) |
| 307 | snprintf(peer_str, sizeof(peer_str), "->%u", peer); |
| 308 | else |
| 309 | peer_str[0] = '\0'; |
| 310 | |
| 311 | const char *path_str; |
| 312 | if (path_len) { |
| 313 | char *outstr = alloca(4 * path_len + 4); |
| 314 | |
| 315 | outstr[0] = ','; |
| 316 | if (path[0] == '\0') { |
| 317 | outstr[1] = '@'; |
| 318 | string_quote(path + 1, outstr + 2, |
| 319 | path_len - 1, QUOTE_0_TERMINATED); |
| 320 | } else { |
| 321 | string_quote(path, outstr + 1, |
| 322 | path_len, QUOTE_0_TERMINATED); |
| 323 | } |
| 324 | path_str = outstr; |
| 325 | } else { |
| 326 | path_str = ""; |
| 327 | } |
| 328 | |
| 329 | char *details; |
| 330 | if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode, |
| 331 | peer_str, path_str) < 0) |
| 332 | return -1; |
| 333 | |
| 334 | return cache_and_print_inode_details(inode, details); |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static bool |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 338 | unix_print(const int fd, const unsigned long inode) |
Masatake YAMATO | 120e5db | 2014-12-24 20:59:31 +0900 | [diff] [blame] | 339 | { |
| 340 | return unix_send_query(fd, inode) |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 341 | && receive_responses(fd, inode, "UNIX", unix_parse_response); |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 342 | } |
| 343 | |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 344 | static bool |
| 345 | tcp_v4_print(const int fd, const unsigned long inode) |
| 346 | { |
| 347 | return inet_print(fd, AF_INET, IPPROTO_TCP, inode, "TCP"); |
| 348 | } |
| 349 | |
| 350 | static bool |
| 351 | udp_v4_print(const int fd, const unsigned long inode) |
| 352 | { |
| 353 | return inet_print(fd, AF_INET, IPPROTO_UDP, inode, "UDP"); |
| 354 | } |
| 355 | |
| 356 | static bool |
| 357 | tcp_v6_print(const int fd, const unsigned long inode) |
| 358 | { |
| 359 | return inet_print(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6"); |
| 360 | } |
| 361 | |
| 362 | static bool |
| 363 | udp_v6_print(const int fd, const unsigned long inode) |
| 364 | { |
| 365 | return inet_print(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6"); |
| 366 | } |
| 367 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 368 | /* Given an inode number of a socket, print out the details |
| 369 | * of the ip address and port. */ |
| 370 | bool |
Dmitry V. Levin | ea21823 | 2016-02-10 17:43:09 +0000 | [diff] [blame] | 371 | print_sockaddr_by_inode(const unsigned long inode, const char *const proto_name) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 372 | { |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 373 | static const struct { |
| 374 | const char *const name; |
| 375 | bool (*const print)(int, unsigned long); |
| 376 | } protocols[] = { |
| 377 | { "TCP", tcp_v4_print }, |
| 378 | { "UDP", udp_v4_print }, |
| 379 | { "TCPv6", tcp_v6_print }, |
| 380 | { "UDPv6", udp_v6_print }, |
| 381 | { "UNIX", unix_print } |
| 382 | }; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 383 | |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 384 | const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 385 | if (fd < 0) |
| 386 | return false; |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 387 | bool r = false; |
| 388 | unsigned int i; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 389 | |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 390 | if (proto_name) { |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 391 | for (i = 0; i < ARRAY_SIZE(protocols); ++i) { |
| 392 | if (strcmp(proto_name, protocols[i].name) == 0) { |
| 393 | r = protocols[i].print(fd, inode); |
| 394 | break; |
| 395 | } |
| 396 | } |
Dmitry V. Levin | ea8b8e3 | 2016-01-23 16:35:02 +0000 | [diff] [blame] | 397 | |
| 398 | if (!r) { |
| 399 | tprintf("%s:[%lu]", proto_name, inode); |
| 400 | r = true; |
| 401 | } |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 402 | } else { |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 403 | for (i = 0; i < ARRAY_SIZE(protocols); ++i) { |
Dmitry V. Levin | bcf5975 | 2016-02-10 17:39:57 +0000 | [diff] [blame] | 404 | if ((r = protocols[i].print(fd, inode))) |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 405 | break; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
Dmitry V. Levin | 959205c | 2014-12-26 23:29:26 +0000 | [diff] [blame] | 408 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 409 | close(fd); |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame] | 410 | return r; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 411 | } |