]> granicus.if.org Git - strace/blob - sockaddr.c
Introduce print_inet_addr
[strace] / sockaddr.c
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-2000 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 2005-2016 Dmitry V. Levin <ldv@altlinux.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "defs.h"
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37
38 #include <linux/netlink.h>
39 #include <linux/if_packet.h>
40 #include <linux/if_arp.h>
41 #include <linux/if_ether.h>
42
43 #ifdef HAVE_NETIPX_IPX_H
44 # include <netipx/ipx.h>
45 #else
46 # include <linux/ipx.h>
47 #endif
48
49 #include "xlat/addrfams.h"
50 #include "xlat/arp_hardware_types.h"
51 #include "xlat/ethernet_protocols.h"
52 #include "xlat/af_packet_types.h"
53
54 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
55 # include <bluetooth/bluetooth.h>
56 # include <bluetooth/hci.h>
57 # include <bluetooth/l2cap.h>
58 # include <bluetooth/rfcomm.h>
59 # include <bluetooth/sco.h>
60
61 # include "xlat/hci_channels.h"
62 #endif
63
64 #define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
65
66 static void
67 print_sockaddr_data_un(const void *const buf, const int addrlen)
68 {
69         const struct sockaddr_un *const sa_un = buf;
70         const int un_len = addrlen > (int) sizeof(*sa_un)
71                            ? (int) sizeof(*sa_un) : addrlen;
72         const int path_len = un_len - SIZEOF_SA_FAMILY;
73
74         tprints("sun_path=");
75         if (sa_un->sun_path[0]) {
76                 print_quoted_string(sa_un->sun_path, path_len + 1,
77                                     QUOTE_0_TERMINATED);
78         } else {
79                 tprints("@");
80                 print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
81         }
82 }
83
84 bool
85 print_inet_addr(const int af,
86                 const void *const addr,
87                 const unsigned int len,
88                 const char *const var_name)
89 {
90         const char *af_name = NULL;
91         char buf[INET6_ADDRSTRLEN];
92
93         switch (af) {
94         case AF_INET:
95                 af_name = "AF_INET";
96                 break;
97         case AF_INET6:
98                 af_name = "AF_INET6";
99                 break;
100         }
101
102         if (af_name && inet_ntop(af, addr, buf, sizeof(buf))) {
103                 tprintf("inet_pton(%s, \"%s\", &%s)", af_name, buf, var_name);
104                 return true;
105         } else {
106                 tprintf("%s=", var_name);
107                 print_quoted_string(addr, len, 0);
108                 return false;
109         }
110 }
111
112 static void
113 print_sockaddr_data_in(const void *const buf, const int addrlen)
114 {
115         const struct sockaddr_in *const sa_in = buf;
116
117         tprintf("sin_port=htons(%u), sin_addr=inet_addr(\"%s\")",
118                 ntohs(sa_in->sin_port), inet_ntoa(sa_in->sin_addr));
119 }
120
121 #define SIN6_MIN_LEN offsetof(struct sockaddr_in6, sin6_scope_id)
122
123 static void
124 print_sockaddr_data_in6(const void *const buf, const int addrlen)
125 {
126         const struct sockaddr_in6 *const sa_in6 = buf;
127
128         tprintf("sin6_port=htons(%u), ", ntohs(sa_in6->sin6_port));
129         print_inet_addr(AF_INET6, &sa_in6->sin6_addr,
130                         sizeof(sa_in6->sin6_addr), "sin6_addr");
131         tprintf(", sin6_flowinfo=htonl(%u)", ntohl(sa_in6->sin6_flowinfo));
132
133         if (addrlen <= (int) SIN6_MIN_LEN)
134                 return;
135
136         tprints(", sin6_scope_id=");
137 #if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
138         if (IN6_IS_ADDR_LINKLOCAL(&sa_in6->sin6_addr)
139             || IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6->sin6_addr))
140                 print_ifindex(sa_in6->sin6_scope_id);
141         else
142 #endif
143                 tprintf("%u", sa_in6->sin6_scope_id);
144 }
145
146 static void
147 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
148 {
149         const struct sockaddr_ipx *const sa_ipx = buf;
150         unsigned int i;
151
152         tprintf("sipx_port=htons(%u)"
153                 ", sipx_network=htonl(%#08x)"
154                 ", sipx_node=[",
155                 ntohs(sa_ipx->sipx_port),
156                 ntohl(sa_ipx->sipx_network));
157         for (i = 0; i < IPX_NODE_LEN; ++i) {
158                 tprintf("%s%#02x", i ? ", " : "",
159                         sa_ipx->sipx_node[i]);
160         }
161         tprintf("], sipx_type=%#02x", sa_ipx->sipx_type);
162 }
163
164 static void
165 print_sockaddr_data_nl(const void *const buf, const int addrlen)
166 {
167         const struct sockaddr_nl *const sa_nl = buf;
168
169         tprintf("nl_pid=%d, nl_groups=%#08x",
170                 sa_nl->nl_pid, sa_nl->nl_groups);
171 }
172
173 static void
174 print_sockaddr_data_ll(const void *const buf, const int addrlen)
175 {
176         const struct sockaddr_ll *const sa_ll = buf;
177
178         tprints("sll_protocol=htons(");
179         printxval(ethernet_protocols, ntohs(sa_ll->sll_protocol), "ETH_P_???");
180         tprints("), sll_ifindex=");
181         print_ifindex(sa_ll->sll_ifindex);
182         tprints(", sll_hatype=");
183         printxval(arp_hardware_types, sa_ll->sll_hatype, "ARPHRD_???");
184         tprints(", sll_pkttype=");
185         printxval(af_packet_types, sa_ll->sll_pkttype, "PACKET_???");
186         tprintf(", sll_halen=%u", sa_ll->sll_halen);
187         if (sa_ll->sll_halen) {
188                 const unsigned int oob_halen =
189                         addrlen - offsetof(struct sockaddr_ll, sll_addr);
190                 unsigned int i;
191
192                 tprints(", sll_addr=[");
193                 for (i = 0; i < sa_ll->sll_halen; ++i) {
194                         if (i)
195                                 tprints(", ");
196                         if (i >= oob_halen) {
197                                 tprints("...");
198                                 break;
199                         }
200                         tprintf("%#02x", sa_ll->sll_addr[i]);
201                 }
202                 tprints("]");
203         }
204 }
205
206 static void
207 print_sockaddr_data_raw(const void *const buf, const int addrlen)
208 {
209         const char *const data = buf + SIZEOF_SA_FAMILY;
210         const int datalen = addrlen - SIZEOF_SA_FAMILY;
211
212         tprints("sa_data=");
213         print_quoted_string(data, datalen, 0);
214 }
215
216 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
217 static void
218 print_sockaddr_data_bt(const void *const buf, const int addrlen)
219 {
220         switch (addrlen) {
221                 case sizeof(struct sockaddr_hci): {
222                         const struct sockaddr_hci *const hci = buf;
223                         tprintf("hci_dev=htobs(%hu), hci_channel=",
224                                 btohs(hci->hci_dev));
225                         printxval(hci_channels, hci->hci_channel,
226                                   "HCI_CHANNEL_???");
227                         break;
228                 }
229                 case sizeof(struct sockaddr_sco): {
230                         const struct sockaddr_sco *const sco = buf;
231                         tprintf("sco_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x",
232                                 sco->sco_bdaddr.b[0], sco->sco_bdaddr.b[1],
233                                 sco->sco_bdaddr.b[2], sco->sco_bdaddr.b[3],
234                                 sco->sco_bdaddr.b[4], sco->sco_bdaddr.b[5]);
235                         break;
236                 }
237                 case sizeof(struct sockaddr_rc): {
238                         const struct sockaddr_rc *const rc = buf;
239                         tprintf("rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
240                                 ", rc_channel=%u",
241                                 rc->rc_bdaddr.b[0], rc->rc_bdaddr.b[1],
242                                 rc->rc_bdaddr.b[2], rc->rc_bdaddr.b[3],
243                                 rc->rc_bdaddr.b[4], rc->rc_bdaddr.b[5],
244                                 rc->rc_channel);
245                         break;
246                 }
247                 case sizeof(struct sockaddr_l2): {
248                         const struct sockaddr_l2 *const l2 = buf;
249                         tprintf("l2_psm=htobs(%hu)"
250                                 ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
251                                 ", l2_cid=htobs(%hu), l2_bdaddr_type=%u",
252                                 btohs(l2->l2_psm),
253                                 l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1],
254                                 l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3],
255                                 l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5],
256                                 btohs(l2->l2_cid), l2->l2_bdaddr_type);
257                         break;
258                 }
259                 default:
260                         print_sockaddr_data_raw(buf, addrlen);
261                         break;
262         }
263 }
264 #endif /* HAVE_BLUETOOTH_BLUETOOTH_H */
265
266 typedef void (* const sockaddr_printer)(const void *const, const int);
267
268 static const struct {
269         const sockaddr_printer printer;
270         const int min_len;
271 } sa_printers[] = {
272         [AF_UNIX] = { print_sockaddr_data_un, SIZEOF_SA_FAMILY + 1 },
273         [AF_INET] = { print_sockaddr_data_in, sizeof(struct sockaddr_in) },
274         [AF_IPX] = { print_sockaddr_data_ipx, sizeof(struct sockaddr_ipx) },
275         [AF_INET6] = { print_sockaddr_data_in6, SIN6_MIN_LEN },
276         [AF_NETLINK] = { print_sockaddr_data_nl, SIZEOF_SA_FAMILY + 1 },
277         [AF_PACKET] = { print_sockaddr_data_ll, sizeof(struct sockaddr_ll) },
278 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
279         [AF_BLUETOOTH] = { print_sockaddr_data_bt, SIZEOF_SA_FAMILY + 1 },
280 #endif
281 };
282
283 void
284 print_sockaddr(struct tcb *tcp, const void *const buf, const int addrlen)
285 {
286         const struct sockaddr *const sa = buf;
287
288         tprints("{sa_family=");
289         printxval(addrfams, sa->sa_family, "AF_???");
290
291         if (addrlen > (int) SIZEOF_SA_FAMILY) {
292                 tprints(", ");
293
294                 if (sa->sa_family < ARRAY_SIZE(sa_printers)
295                     && sa_printers[sa->sa_family].printer
296                     && addrlen >= sa_printers[sa->sa_family].min_len) {
297                         sa_printers[sa->sa_family].printer(buf, addrlen);
298                 } else {
299                         print_sockaddr_data_raw(buf, addrlen);
300                 }
301         }
302
303         tprints("}");
304 }
305
306 int
307 decode_sockaddr(struct tcb *const tcp, const kernel_ulong_t addr, int addrlen)
308 {
309         if (addrlen < 2) {
310                 printaddr(addr);
311                 return -1;
312         }
313
314         union {
315                 struct sockaddr sa;
316                 struct sockaddr_storage storage;
317                 char pad[sizeof(struct sockaddr_storage) + 1];
318         } addrbuf;
319
320         if ((unsigned) addrlen > sizeof(addrbuf.storage))
321                 addrlen = sizeof(addrbuf.storage);
322
323         if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
324                 return -1;
325
326         memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
327
328         print_sockaddr(tcp, &addrbuf, addrlen);
329
330         return addrbuf.sa.sa_family;
331 }