2 * Check decoding of sockaddr fields under xlat styles.
4 * Copyright (c) 2015-2018 The strace developers.
7 * SPDX-License-Identifier: GPL-2.0-or-later
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>
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"
31 unsigned int len = sizeof(c_ll);
32 int rc = connect(-1, (void *) &c_ll, len);
33 const char *errstr = sprintrc(rc);
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]"
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],
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]"
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],
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]"
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],
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)
92 unsigned int len = sizeof(in);
93 int rc = connect(-1, (void *) &in, len);
94 const char * errstr = sprintrc(rc);
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);
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);
118 validate_in6(struct sockaddr_in6 *const in6, const char *const h_addr)
120 inet_pton(AF_INET6, h_addr, &in6->sin6_addr);
122 unsigned int len = sizeof(*in6);
123 int rc = connect(-1, (void *) in6, len);
124 const char *errstr = sprintrc(rc);
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);
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)"
147 ntohl(in6->sin6_flowinfo), in6->sin6_scope_id,
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)"
155 ntohs(in6->sin6_port), h_addr,
156 ntohl(in6->sin6_flowinfo), in6->sin6_scope_id,
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
171 validate_in6(&in6, "12:34:56:78:90:ab:cd:ef");
172 validate_in6(&in6, "::");
173 validate_in6(&in6, "::1");
182 puts("+++ exited with 0 +++");