]> granicus.if.org Git - strace/blob - tests/sockaddr_xlat.c
Update copyright headers
[strace] / tests / sockaddr_xlat.c
1 /*
2  * Check decoding of sockaddr fields under xlat styles.
3  *
4  * Copyright (c) 2015-2019 The strace developers.
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9 #include "tests.h"
10 #include <stdio.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
13 #include <netinet/in.h>
14 #include <linux/ax25.h>
15 #include <linux/if_arp.h>
16 #include <linux/if_ether.h>
17 #include <linux/if_packet.h>
18 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
19 # include <bluetooth/bluetooth.h>
20 # include <bluetooth/hci.h>
21 # include <bluetooth/l2cap.h>
22 # include <bluetooth/rfcomm.h>
23 # include <bluetooth/sco.h>
24 #endif
25
26 static void
27 check_ll(void)
28 {
29         struct sockaddr_ll c_ll = {
30                 .sll_family = AF_PACKET,
31                 .sll_protocol = htons(ETH_P_ALL),
32                 .sll_ifindex = 0xfacefeed,
33                 .sll_hatype = ARPHRD_ETHER,
34                 .sll_pkttype = PACKET_HOST,
35                 .sll_halen = sizeof(c_ll.sll_addr),
36                 .sll_addr = "abcdefgh"
37         };
38         unsigned int len = sizeof(c_ll);
39         int rc = connect(-1, (void *) &c_ll, len);
40         const char *errstr = sprintrc(rc);
41
42 #if XLAT_RAW
43         printf("connect(-1, {sa_family=%#x, sll_protocol=", AF_PACKET);
44         print_quoted_hex(&c_ll.sll_protocol, sizeof(c_ll.sll_protocol));
45         printf(", sll_ifindex=%u, sll_hatype=%#x"
46                ", sll_pkttype=%u, sll_halen=%u, sll_addr="
47                "[%#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x]"
48                "}, %u) = %s\n",
49                c_ll.sll_ifindex, ARPHRD_ETHER,
50                PACKET_HOST, c_ll.sll_halen,
51                c_ll.sll_addr[0], c_ll.sll_addr[1],
52                c_ll.sll_addr[2], c_ll.sll_addr[3],
53                c_ll.sll_addr[4], c_ll.sll_addr[5],
54                c_ll.sll_addr[6], c_ll.sll_addr[7],
55                len, errstr);
56 #elif XLAT_VERBOSE
57         printf("connect(-1, {sa_family=%#x /* AF_PACKET */"
58                ", sll_protocol=", AF_PACKET);
59         print_quoted_hex(&c_ll.sll_protocol, sizeof(c_ll.sll_protocol));
60         printf(" /* htons(ETH_P_ALL) */"
61                ", sll_ifindex=%u, sll_hatype=%#x /* ARPHRD_ETHER */"
62                ", sll_pkttype=%u /* PACKET_HOST */, sll_halen=%u, sll_addr="
63                "[%#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x]"
64                "}, %u) = %s\n",
65                c_ll.sll_ifindex, ARPHRD_ETHER,
66                PACKET_HOST, c_ll.sll_halen,
67                c_ll.sll_addr[0], c_ll.sll_addr[1],
68                c_ll.sll_addr[2], c_ll.sll_addr[3],
69                c_ll.sll_addr[4], c_ll.sll_addr[5],
70                c_ll.sll_addr[6], c_ll.sll_addr[7],
71                len, errstr);
72
73 #else /* XLAT_ABBREV */
74         printf("connect(-1, {sa_family=AF_PACKET"
75                ", sll_protocol=htons(ETH_P_ALL)"
76                ", sll_ifindex=%u, sll_hatype=ARPHRD_ETHER"
77                ", sll_pkttype=PACKET_HOST, sll_halen=%u, sll_addr="
78                "[%#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x]"
79                "}, %u) = %s\n",
80                c_ll.sll_ifindex, c_ll.sll_halen,
81                c_ll.sll_addr[0], c_ll.sll_addr[1],
82                c_ll.sll_addr[2], c_ll.sll_addr[3],
83                c_ll.sll_addr[4], c_ll.sll_addr[5],
84                c_ll.sll_addr[6], c_ll.sll_addr[7],
85                len, errstr);
86 #endif
87 }
88
89 static void
90 check_in(void)
91 {
92         const unsigned short h_port = 12345;
93         static const char h_addr[] = "127.0.0.1";
94         struct sockaddr_in in = {
95                 .sin_family = AF_INET,
96                 .sin_port = htons(h_port),
97                 .sin_addr.s_addr = inet_addr(h_addr)
98         };
99         unsigned int len = sizeof(in);
100         int rc = connect(-1, (void *) &in, len);
101         const char * errstr = sprintrc(rc);
102 #if XLAT_RAW
103         printf("connect(-1, {sa_family=%#x, sin_port=", AF_INET);
104         print_quoted_hex((const void *) &in.sin_port, sizeof(in.sin_port));
105         printf(", sin_addr=");
106         print_quoted_hex((const void *) &in.sin_addr.s_addr,
107                         sizeof(in.sin_addr.s_addr));
108         printf("}, %u) = %s\n", len, errstr);
109 #elif XLAT_VERBOSE
110         printf("connect(-1, {sa_family=%#x /* AF_INET */, sin_port=", AF_INET);
111         print_quoted_hex((const void *) &in.sin_port, sizeof(in.sin_port));
112         printf(" /* htons(%hu) */, sin_addr=", h_port);
113         print_quoted_hex((const void *) &in.sin_addr.s_addr,
114                                 sizeof(in.sin_addr.s_addr));
115         printf(" /* inet_addr(\"%s\") */}, %u) = %s\n",
116                         h_addr, len, errstr);
117 #else /* XLAT_ABBREV */
118         printf("connect(-1, {sa_family=AF_INET, sin_port=htons(%hu)"
119                ", sin_addr=inet_addr(\"%s\")}, %u) = %s\n",
120                h_port, h_addr, len, errstr);
121 #endif
122 }
123
124 static void
125 validate_in6(struct sockaddr_in6 *const in6, const char *const h_addr)
126 {
127         inet_pton(AF_INET6, h_addr, &in6->sin6_addr);
128
129         unsigned int len = sizeof(*in6);
130         int rc = connect(-1, (void *) in6, len);
131         const char *errstr = sprintrc(rc);
132 #if XLAT_RAW
133         printf("connect(-1, {sa_family=%#x, sin6_port=", AF_INET6);
134         print_quoted_hex(&in6->sin6_port, sizeof(in6->sin6_port));
135         printf(", sin6_flowinfo=");
136         print_quoted_hex(&in6->sin6_flowinfo, sizeof(in6->sin6_flowinfo));
137         printf(", sin6_addr=");
138         print_quoted_hex(&in6->sin6_addr, sizeof(struct in6_addr));
139         printf(", sin6_scope_id=%u}, %u) = %s\n",
140                in6->sin6_scope_id, len, errstr);
141 #elif XLAT_VERBOSE
142         printf("connect(-1, {sa_family=%#x /* AF_INET6 */", AF_INET6);
143         printf(", sin6_port=");
144         print_quoted_hex(&in6->sin6_port, sizeof(in6->sin6_port));
145         printf(" /* htons(%hu) */", ntohs(in6->sin6_port));
146         printf(", sin6_flowinfo=");
147         print_quoted_hex(&in6->sin6_flowinfo, sizeof(in6->sin6_flowinfo));
148         printf(" /* htonl(%u) */", ntohl(in6->sin6_flowinfo));
149         printf(", sin6_addr=");
150         print_quoted_hex(&in6->sin6_addr, sizeof(struct in6_addr));
151         printf(" /* inet_pton(AF_INET6, \"%s\") */", h_addr);
152         printf(", sin6_scope_id=%u}, %u) = %s\n",
153                in6->sin6_scope_id, len, errstr);
154 #else
155         printf("connect(-1, {sa_family=AF_INET6, sin6_port=htons(%hu)"
156                ", sin6_flowinfo=htonl(%u)"
157                ", inet_pton(AF_INET6, \"%s\", &sin6_addr)"
158                ", sin6_scope_id=%u}, %u)"
159                " = %s\n",
160                ntohs(in6->sin6_port), ntohl(in6->sin6_flowinfo),
161                h_addr, in6->sin6_scope_id, len, errstr);
162 #endif
163 }
164
165 static void
166 check_in6(void)
167 {
168         struct sockaddr_in6 in6 = {
169                 .sin6_family = AF_INET6,
170                 .sin6_port = htons(12345),
171                 .sin6_flowinfo = htonl(123456890),
172                 .sin6_scope_id = 0xfacefeed
173         };
174
175         validate_in6(&in6, "12:34:56:78:90:ab:cd:ef");
176         validate_in6(&in6, "::");
177         validate_in6(&in6, "::1");
178 }
179
180 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
181 static void
182 check_sco(void)
183 {
184         const struct sockaddr_sco c_sco = {
185                 .sco_family = AF_BLUETOOTH,
186                 .sco_bdaddr.b = "abcdef"
187         };
188         void *sco = tail_memdup(&c_sco, sizeof(c_sco));
189         unsigned int len = sizeof(c_sco);
190         int ret = connect(-1, sco, len);
191         const char *errstr = sprintrc(ret);
192 # if XLAT_RAW
193         printf("connect(-1, {sa_family=%#x, sco_bdaddr=", AF_BLUETOOTH);
194         print_quoted_hex((const void *) &c_sco.sco_bdaddr,
195                          sizeof(c_sco.sco_bdaddr));
196         printf("}, %u) = %s\n", len, errstr);
197 # elif XLAT_VERBOSE
198         printf("connect(-1, {sa_family=%#x /* AF_BLUETOOTH */"
199                ", sco_bdaddr=", AF_BLUETOOTH);
200         print_quoted_hex((const void *) &c_sco.sco_bdaddr,
201                          sizeof(c_sco.sco_bdaddr));
202         printf(" /* %02x:%02x:%02x:%02x:%02x:%02x */"
203                "}, %u) = %s\n",
204                c_sco.sco_bdaddr.b[0], c_sco.sco_bdaddr.b[1],
205                c_sco.sco_bdaddr.b[2], c_sco.sco_bdaddr.b[3],
206                c_sco.sco_bdaddr.b[4], c_sco.sco_bdaddr.b[5],
207                len, errstr);
208 # else
209         printf("connect(-1, {sa_family=AF_BLUETOOTH"
210                ", sco_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
211                "}, %u) = %s\n",
212                c_sco.sco_bdaddr.b[0], c_sco.sco_bdaddr.b[1],
213                c_sco.sco_bdaddr.b[2], c_sco.sco_bdaddr.b[3],
214                c_sco.sco_bdaddr.b[4], c_sco.sco_bdaddr.b[5],
215                len, errstr);
216 # endif
217 }
218
219 static void
220 check_rc(void)
221 {
222         const struct sockaddr_rc c_rc = {
223                 .rc_family = AF_BLUETOOTH,
224                 .rc_bdaddr.b = "abcdef",
225                 .rc_channel = 42
226         };
227         void *rc = tail_memdup(&c_rc, sizeof(c_rc));
228         unsigned int len = sizeof(c_rc);
229         int ret = connect(-1, rc, len);
230         const char *errstr = sprintrc(ret);
231 # if XLAT_RAW
232         printf("connect(-1, {sa_family=%#x, rc_bdaddr=", AF_BLUETOOTH);
233         print_quoted_hex((const void *) &c_rc.rc_bdaddr,
234                          sizeof(c_rc.rc_bdaddr));
235         printf(", rc_channel=%u}, %u) = %s\n", c_rc.rc_channel, len, errstr);
236 # elif XLAT_VERBOSE
237         printf("connect(-1, {sa_family=%#x /* AF_BLUETOOTH */"
238                ", rc_bdaddr=", AF_BLUETOOTH);
239         print_quoted_hex((const void *) &c_rc.rc_bdaddr,
240                          sizeof(c_rc.rc_bdaddr));
241         printf(" /* %02x:%02x:%02x:%02x:%02x:%02x */"
242                ", rc_channel=%u}, %u) = %s\n",
243                c_rc.rc_bdaddr.b[0], c_rc.rc_bdaddr.b[1],
244                c_rc.rc_bdaddr.b[2], c_rc.rc_bdaddr.b[3],
245                c_rc.rc_bdaddr.b[4], c_rc.rc_bdaddr.b[5],
246                c_rc.rc_channel, len, errstr);
247 # else
248         printf("connect(-1, {sa_family=AF_BLUETOOTH"
249                ", rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
250                ", rc_channel=%u}, %u) = %s\n",
251                c_rc.rc_bdaddr.b[0], c_rc.rc_bdaddr.b[1],
252                c_rc.rc_bdaddr.b[2], c_rc.rc_bdaddr.b[3],
253                c_rc.rc_bdaddr.b[4], c_rc.rc_bdaddr.b[5],
254                c_rc.rc_channel, len, errstr);
255 # endif
256 }
257 #endif /* HAVE_BLUETOOTH_BLUETOOTH_H */
258
259 int
260 main(void)
261 {
262         check_ll();
263         check_in();
264         check_in6();
265 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
266         check_sco();
267         check_rc();
268 #endif
269         puts("+++ exited with 0 +++");
270         return 0;
271 }