]> granicus.if.org Git - strace/blob - tests/sockaddr_xlat.c
Honor xlat styles when decoding sockaddr_in, sockaddr_in6, and sockaddr_ll
[strace] / tests / sockaddr_xlat.c
1 /*
2  * Check decoding of sockaddr fields under xlat styles.
3  *
4  * Copyright (c) 2015-2018 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
19 static void
20 check_ll(void)
21 {
22         struct sockaddr_ll c_ll = {
23                 .sll_family = AF_PACKET,
24                 .sll_protocol = htons(ETH_P_ALL),
25                 .sll_ifindex = 0xfacefeed,
26                 .sll_hatype = ARPHRD_ETHER,
27                 .sll_pkttype = PACKET_HOST,
28                 .sll_halen = sizeof(c_ll.sll_addr),
29                 .sll_addr = "abcdefgh"
30         };
31         unsigned int len = sizeof(c_ll);
32         int rc = connect(-1, (void *) &c_ll, len);
33         const char *errstr = sprintrc(rc);
34
35 #if XLAT_RAW
36         printf("connect(-1, {sa_family=%#x, sll_protocol=", AF_PACKET);
37         print_quoted_hex(&c_ll.sll_protocol, sizeof(c_ll.sll_protocol));
38         printf(", sll_ifindex=%u, sll_hatype=%#x"
39                ", sll_pkttype=%u, sll_halen=%u, sll_addr="
40                "[%#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x]"
41                "}, %u) = %s\n",
42                c_ll.sll_ifindex, ARPHRD_ETHER,
43                PACKET_HOST, c_ll.sll_halen,
44                c_ll.sll_addr[0], c_ll.sll_addr[1],
45                c_ll.sll_addr[2], c_ll.sll_addr[3],
46                c_ll.sll_addr[4], c_ll.sll_addr[5],
47                c_ll.sll_addr[6], c_ll.sll_addr[7],
48                len, errstr);
49 #elif XLAT_VERBOSE
50         printf("connect(-1, {sa_family=%#x /* AF_PACKET */"
51                ", sll_protocol=", AF_PACKET);
52         print_quoted_hex(&c_ll.sll_protocol, sizeof(c_ll.sll_protocol));
53         printf(" /* htons(ETH_P_ALL) */"
54                ", sll_ifindex=%u, sll_hatype=%#x /* ARPHRD_ETHER */"
55                ", sll_pkttype=%u /* PACKET_HOST */, sll_halen=%u, sll_addr="
56                "[%#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x]"
57                "}, %u) = %s\n",
58                c_ll.sll_ifindex, ARPHRD_ETHER,
59                PACKET_HOST, c_ll.sll_halen,
60                c_ll.sll_addr[0], c_ll.sll_addr[1],
61                c_ll.sll_addr[2], c_ll.sll_addr[3],
62                c_ll.sll_addr[4], c_ll.sll_addr[5],
63                c_ll.sll_addr[6], c_ll.sll_addr[7],
64                len, errstr);
65
66 #else /* XLAT_ABBREV */
67         printf("connect(-1, {sa_family=AF_PACKET"
68                ", sll_protocol=htons(ETH_P_ALL)"
69                ", sll_ifindex=%u, sll_hatype=ARPHRD_ETHER"
70                ", sll_pkttype=PACKET_HOST, sll_halen=%u, sll_addr="
71                "[%#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x]"
72                "}, %u) = %s\n",
73                c_ll.sll_ifindex, c_ll.sll_halen,
74                c_ll.sll_addr[0], c_ll.sll_addr[1],
75                c_ll.sll_addr[2], c_ll.sll_addr[3],
76                c_ll.sll_addr[4], c_ll.sll_addr[5],
77                c_ll.sll_addr[6], c_ll.sll_addr[7],
78                len, errstr);
79 #endif
80 }
81
82 static void
83 check_in(void)
84 {
85         const unsigned short h_port = 12345;
86         static const char h_addr[] = "127.0.0.1";
87         struct sockaddr_in in = {
88                 .sin_family = AF_INET,
89                 .sin_port = htons(h_port),
90                 .sin_addr.s_addr = inet_addr(h_addr)
91         };
92         unsigned int len = sizeof(in);
93         int rc = connect(-1, (void *) &in, len);
94         const char * errstr = sprintrc(rc);
95 #if XLAT_RAW
96         printf("connect(-1, {sa_family=%#x, sin_port=", AF_INET);
97         print_quoted_hex((const void *) &in.sin_port, sizeof(in.sin_port));
98         printf(", sin_addr=");
99         print_quoted_hex((const void *) &in.sin_addr.s_addr,
100                         sizeof(in.sin_addr.s_addr));
101         printf("}, %u) = %s\n", len, errstr);
102 #elif XLAT_VERBOSE
103         printf("connect(-1, {sa_family=%#x /* AF_INET */, sin_port=", AF_INET);
104         print_quoted_hex((const void *) &in.sin_port, sizeof(in.sin_port));
105         printf(" /* htons(%hu) */, sin_addr=", h_port);
106         print_quoted_hex((const void *) &in.sin_addr.s_addr,
107                                 sizeof(in.sin_addr.s_addr));
108         printf(" /* inet_addr(\"%s\") */}, %u) = %s\n",
109                         h_addr, len, errstr);
110 #else /* XLAT_ABBREV */
111         printf("connect(-1, {sa_family=AF_INET, sin_port=htons(%hu)"
112                ", sin_addr=inet_addr(\"%s\")}, %u) = %s\n",
113                h_port, h_addr, len, errstr);
114 #endif
115 }
116
117 static void
118 validate_in6(struct sockaddr_in6 *const in6, const char *const h_addr)
119 {
120         inet_pton(AF_INET6, h_addr, &in6->sin6_addr);
121
122         unsigned int len = sizeof(*in6);
123         int rc = connect(-1, (void *) in6, len);
124         const char *errstr = sprintrc(rc);
125 #if XLAT_RAW
126         printf("connect(-1, {sa_family=%#x, sin6_port=", AF_INET6);
127         print_quoted_hex(&in6->sin6_port, sizeof(in6->sin6_port));
128         printf(", sin6_addr=");
129         print_quoted_hex(&in6->sin6_addr, sizeof(struct in6_addr));
130         printf(", sin6_flowinfo=");
131         print_quoted_hex(&in6->sin6_flowinfo, sizeof(in6->sin6_flowinfo));
132         printf(", sin6_scope_id=%u}, %u)"
133                " = %s\n", in6->sin6_scope_id, len, errstr);
134 #elif XLAT_VERBOSE
135         printf("connect(-1, {sa_family=%#x /* AF_INET6 */", AF_INET6);
136         printf(", sin6_port=");
137         print_quoted_hex(&in6->sin6_port, sizeof(in6->sin6_port));
138         printf(" /* htons(%hu) */", ntohs(in6->sin6_port));
139         printf(", sin6_addr=");
140         print_quoted_hex(&in6->sin6_addr, sizeof(struct in6_addr));
141         printf(" /* inet_pton(AF_INET6, \"%s\") */", h_addr);
142         printf(", sin6_flowinfo=");
143         print_quoted_hex(&in6->sin6_flowinfo, sizeof(in6->sin6_flowinfo));
144         printf(" /* htonl(%u) */"
145                ", sin6_scope_id=%u}, %u)"
146                " = %s\n",
147                ntohl(in6->sin6_flowinfo), in6->sin6_scope_id,
148                      len, errstr);
149 #else
150         printf("connect(-1, {sa_family=AF_INET6, sin6_port=htons(%hu)"
151                ", inet_pton(AF_INET6, \"%s\", &sin6_addr)"
152                ", sin6_flowinfo=htonl(%u)"
153                ", sin6_scope_id=%u}, %u)"
154                " = %s\n",
155                ntohs(in6->sin6_port), h_addr,
156                ntohl(in6->sin6_flowinfo), in6->sin6_scope_id,
157                      len, errstr);
158 #endif
159 }
160
161 static void
162 check_in6(void)
163 {
164         struct sockaddr_in6 in6 = {
165                 .sin6_family = AF_INET6,
166                 .sin6_port = htons(12345),
167                 .sin6_flowinfo = htonl(123456890),
168                 .sin6_scope_id = 0xfacefeed
169         };
170
171         validate_in6(&in6, "12:34:56:78:90:ab:cd:ef");
172         validate_in6(&in6, "::");
173         validate_in6(&in6, "::1");
174 }
175
176 int
177 main(void)
178 {
179         check_ll();
180         check_in();
181         check_in6();
182         puts("+++ exited with 0 +++");
183         return 0;
184 }