]> granicus.if.org Git - strace/blob - socketutils.c
tests: extend coverage of struct sigevent
[strace] / socketutils.c
1 /*
2  * Copyright (c) 2014 Zubin Mithra <zubin.mithra@gmail.com>
3  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
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
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>
36 #include <linux/unix_diag.h>
37 #include <linux/rtnetlink.h>
38
39 #if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
40 # define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
41 #endif
42
43 #include <sys/un.h>
44 #ifndef UNIX_PATH_MAX
45 # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path)
46 #endif
47
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
58 cache_and_print_inode_details(const unsigned long inode, char *details)
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 {
72         const cache_entry *e = &cache[inode & CACHE_MASK];
73         if (e && inode == e->inode) {
74                 tprints(e->details);
75                 return true;
76         }
77         return false;
78 }
79
80 static bool
81 inet_send_query(const int fd, const int family, const int proto)
82 {
83         struct sockaddr_nl nladdr = {
84                 .nl_family = AF_NETLINK
85         };
86         struct {
87                 struct nlmsghdr nlh;
88                 struct inet_diag_req_v2 idr;
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         };
101         struct iovec iov = {
102                 .iov_base = &req,
103                 .iov_len = sizeof(req)
104         };
105         struct msghdr msg = {
106                 .msg_name = (void *) &nladdr,
107                 .msg_namelen = sizeof(nladdr),
108                 .msg_iov = &iov,
109                 .msg_iovlen = 1
110         };
111
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
122 static int
123 inet_parse_response(const char *proto_name, const void *data,
124                     const int data_len, const unsigned long inode)
125 {
126         const struct inet_diag_msg *diag_msg = data;
127         static const char zero_addr[sizeof(struct in6_addr)];
128         socklen_t addr_size, text_size;
129
130         if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
131                 return -1;
132         if (diag_msg->idiag_inode != inode)
133                 return 0;
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:
145                         return -1;
146         }
147
148         char src_buf[text_size];
149         char *details;
150
151         if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
152                        src_buf, text_size))
153                 return -1;
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))
161                         return -1;
162
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;
167         } else {
168                 if (asprintf(&details, "%s:[%s:%u]", proto_name, src_buf,
169                              ntohs(diag_msg->id.idiag_sport)) < 0)
170                         return false;
171         }
172
173         return cache_and_print_inode_details(inode, details);
174 }
175
176 static bool
177 receive_responses(const int fd, const unsigned long inode,
178                   const char *proto_name,
179                   int (* parser) (const char *, const void *,
180                                   int, unsigned long))
181 {
182         static long buf[8192 / sizeof(long)];
183         struct sockaddr_nl nladdr = {
184                 .nl_family = AF_NETLINK
185         };
186         struct iovec iov = {
187                 .iov_base = buf,
188                 .iov_len = sizeof(buf)
189         };
190         int flags = 0;
191
192         for (;;) {
193                 ssize_t ret;
194                 struct msghdr msg = {
195                         .msg_name = (void *) &nladdr,
196                         .msg_namelen = sizeof(nladdr),
197                         .msg_iov = &iov,
198                         .msg_iovlen = 1
199                 };
200
201                 ret = recvmsg(fd, &msg, flags);
202                 if (ret < 0) {
203                         if (errno == EINTR)
204                                 continue;
205                         return false;
206                 }
207
208                 struct nlmsghdr *h = (struct nlmsghdr *) buf;
209                 if (!NLMSG_OK(h, ret))
210                         return false;
211                 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
212                         if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
213                                 return false;
214                         int rc = parser(proto_name, NLMSG_DATA(h),
215                                         h->nlmsg_len, inode);
216                         if (rc > 0)
217                                 return true;
218                         if (rc < 0)
219                                 return false;
220                 }
221                 flags = MSG_DONTWAIT;
222         }
223 }
224
225 static bool
226 inet_print(const int fd, const int family, const int protocol,
227            const unsigned long inode, const char *proto_name)
228 {
229         return inet_send_query(fd, family, protocol)
230                 && receive_responses(fd, inode, proto_name, inet_parse_response);
231 }
232
233 static bool
234 unix_send_query(const int fd, const unsigned long inode)
235 {
236         struct sockaddr_nl nladdr = {
237                 .nl_family = AF_NETLINK
238         };
239         struct {
240                 struct nlmsghdr nlh;
241                 struct unix_diag_req udr;
242         } req = {
243                 .nlh = {
244                         .nlmsg_len = sizeof(req),
245                         .nlmsg_type = SOCK_DIAG_BY_FAMILY,
246                         .nlmsg_flags = NLM_F_REQUEST
247                 },
248                 .udr = {
249                         .sdiag_family = AF_UNIX,
250                         .udiag_ino = inode,
251                         .udiag_states = -1,
252                         .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER,
253                         .udiag_cookie = { ~0U, ~0U }
254                 }
255         };
256         struct iovec iov = {
257                 .iov_base = &req,
258                 .iov_len = sizeof(req)
259         };
260         struct msghdr msg = {
261                 .msg_name = (void *) &nladdr,
262                 .msg_namelen = sizeof(nladdr),
263                 .msg_iov = &iov,
264                 .msg_iovlen = 1
265         };
266
267         for (;;) {
268                 if (sendmsg(fd, &msg, 0) < 0) {
269                         if (errno == EINTR)
270                                 continue;
271                         return false;
272                 }
273                 return true;
274         }
275 }
276
277 static int
278 unix_parse_response(const char *proto_name, const void *data,
279                     const int data_len, const unsigned long inode)
280 {
281         const struct unix_diag_msg *diag_msg = data;
282         struct rtattr *attr;
283         int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
284         uint32_t peer = 0;
285         size_t path_len = 0;
286         char path[UNIX_PATH_MAX + 1];
287
288         if (rta_len < 0)
289                 return -1;
290         if (diag_msg->udiag_ino != inode)
291                 return 0;
292         if (diag_msg->udiag_family != AF_UNIX)
293                 return -1;
294
295         for (attr = (struct rtattr *) (diag_msg + 1);
296              RTA_OK(attr, rta_len);
297              attr = RTA_NEXT(attr, rta_len)) {
298                 switch (attr->rta_type) {
299                 case UNIX_DIAG_NAME:
300                         if (!path_len) {
301                                 path_len = RTA_PAYLOAD(attr);
302                                 if (path_len > UNIX_PATH_MAX)
303                                         path_len = UNIX_PATH_MAX;
304                                 memcpy(path, RTA_DATA(attr), path_len);
305                                 path[path_len] = '\0';
306                         }
307                         break;
308                 case UNIX_DIAG_PEER:
309                         if (RTA_PAYLOAD(attr) >= 4)
310                                 peer = *(uint32_t *) RTA_DATA(attr);
311                         break;
312                 }
313         }
314
315         /*
316          * print obtained information in the following format:
317          * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
318          */
319         if (!peer && !path_len)
320                 return -1;
321
322         char peer_str[3 + sizeof(peer) * 3];
323         if (peer)
324                 snprintf(peer_str, sizeof(peer_str), "->%u", peer);
325         else
326                 peer_str[0] = '\0';
327
328         const char *path_str;
329         if (path_len) {
330                 char *outstr = alloca(4 * path_len + 4);
331
332                 outstr[0] = ',';
333                 if (path[0] == '\0') {
334                         outstr[1] = '@';
335                         string_quote(path + 1, outstr + 2,
336                                      path_len - 1, QUOTE_0_TERMINATED);
337                 } else {
338                         string_quote(path, outstr + 1,
339                                      path_len, QUOTE_0_TERMINATED);
340                 }
341                 path_str = outstr;
342         } else {
343                 path_str = "";
344         }
345
346         char *details;
347         if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
348                      peer_str, path_str) < 0)
349                 return -1;
350
351         return cache_and_print_inode_details(inode, details);
352 }
353
354 static bool
355 unix_print(int fd, const unsigned long inode)
356 {
357         return unix_send_query(fd, inode)
358                 && receive_responses(fd, inode, "UNIX", unix_parse_response);
359 }
360
361 /* Given an inode number of a socket, print out the details
362  * of the ip address and port. */
363 bool
364 print_sockaddr_by_inode(const unsigned long inode, const char *proto_name)
365 {
366         int fd;
367         bool r = false;
368
369         fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
370         if (fd < 0)
371                 return false;
372
373         if (proto_name) {
374                 if (strcmp(proto_name, "TCP") == 0)
375                         r = inet_print(fd, AF_INET, IPPROTO_TCP, inode, "TCP");
376                 else if (strcmp(proto_name, "UDP") == 0)
377                         r = inet_print(fd, AF_INET, IPPROTO_UDP, inode, "UDP");
378                 else if (strcmp(proto_name, "TCPv6") == 0)
379                         r = inet_print(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
380                 else if (strcmp(proto_name, "UDPv6") == 0)
381                         r = inet_print(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
382                 else if (strcmp(proto_name, "UNIX") == 0)
383                         r = unix_print(fd, inode);
384
385                 if (!r) {
386                         tprintf("%s:[%lu]", proto_name, inode);
387                         r = true;
388                 }
389         } else {
390                 const struct {
391                         const int family;
392                         const int protocol;
393                         const char *name;
394                 } protocols[] = {
395                         { AF_INET, IPPROTO_TCP, "TCP" },
396                         { AF_INET, IPPROTO_UDP, "UDP" },
397                         { AF_INET6, IPPROTO_TCP, "TCPv6" },
398                         { AF_INET6, IPPROTO_UDP, "UDPv6" }
399                 };
400                 size_t i;
401
402                 for (i = 0; i < ARRAY_SIZE(protocols); ++i) {
403                         if ((r = inet_print(fd, protocols[i].family,
404                                             protocols[i].protocol, inode,
405                                             protocols[i].name)))
406                                 break;
407                 }
408         }
409
410         close(fd);
411         return r;
412 }