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