Browse Source

Honor xlat styles when decoding mac addresses

* defs.h (print_mac_addr): Add xlat_style support, move ...
* print_mac.c: ... here.
* tests/sockaddr_xlat.c [HAVE_BLUETOOTH_BLUETOOTH_H]: Include
<bluetooth/bluetooth.h>, <bluetooth/hci.h>, <bluetooth/l2cap.h>
<bluetooth/rfcomm.h>, and <bluetooth/sco.h>.
[HAVE_BLUETOOTH_BLUETOOTH_H] (check_sco, check_rc): New functions.
(main) [HAVE_BLUETOOTH_BLUETOOTH_H]: Call them to verify
new xlat_style support in print_mac_addr().
* tests/net-packet_mreq.c: Handle XLAT_RAW, XLAT_ABBREV,
and XLAT_VERBOSE macros.
* tests/net-packet_mreq-Xabbrev.c: New test.
* tests/net-packet_mreq-Xraw.c: Likewise.
* tests/net-packet_mreq-Xverbose.c: Likewise.
* tests/gen_tests.in (net-packet_mreq-Xabbrev, net-packet_mreq-Xraw,
net-packet_mreq-Xverbose): New tests.
* tests/pure_executables.list: Add net-packet_mreq-Xabbrev,
net-packet_mreq-Xraw, and et-packet_mreq-Xverbose.
* tests/.gitignore: Likewise.

Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
Shankara Pailoor 10 months ago
parent
commit
c256b45ddf

+ 2
- 7
defs.h View File

@@ -814,6 +814,8 @@ extern const char *sprinttime_nsec(long long sec, unsigned long long nsec);
814 814
 extern const char *sprinttime_usec(long long sec, unsigned long long usec);
815 815
 
816 816
 extern const char *sprint_mac_addr(const uint8_t addr[], size_t size);
817
+extern void print_mac_addr(const char *prefix,
818
+			   const uint8_t addr[], size_t size);
817 819
 
818 820
 extern void print_uuid(const unsigned char *uuid);
819 821
 
@@ -1168,13 +1170,6 @@ printaddr_comment(const kernel_ulong_t addr)
1168 1170
 	tprintf_comment("%#llx", (unsigned long long) addr);
1169 1171
 }
1170 1172
 
1171
-static inline void
1172
-print_mac_addr(const char *prefix, const uint8_t addr[], size_t size)
1173
-{
1174
-	tprints(prefix);
1175
-	tprints(sprint_mac_addr(addr, size));
1176
-}
1177
-
1178 1173
 # if SUPPORTED_PERSONALITIES > 1
1179 1174
 extern void set_personality(unsigned int personality);
1180 1175
 extern unsigned current_personality;

+ 13
- 0
print_mac.c View File

@@ -33,3 +33,16 @@ sprint_mac_addr(const uint8_t addr[], size_t size)
33 33
 
34 34
 	return res;
35 35
 }
36
+
37
+void
38
+print_mac_addr(const char *prefix, const uint8_t addr[], size_t size)
39
+{
40
+	tprints(prefix);
41
+	if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
42
+		print_quoted_string((const char *) addr, size,
43
+				    QUOTE_FORCE_HEX);
44
+	if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
45
+		return;
46
+	(xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
47
+		? tprints_comment : tprints)(sprint_mac_addr(addr, size));
48
+}

+ 3
- 0
tests/.gitignore View File

@@ -277,6 +277,9 @@ nanosleep
277 277
 net-accept-connect
278 278
 net-icmp_filter
279 279
 net-packet_mreq
280
+net-packet_mreq-Xabbrev
281
+net-packet_mreq-Xraw
282
+net-packet_mreq-Xverbose
280 283
 net-sockaddr
281 284
 net-tpacket_req
282 285
 net-tpacket_stats

+ 3
- 0
tests/gen_tests.in View File

@@ -235,6 +235,9 @@ munlockall	-a13
235 235
 nanosleep	-a20
236 236
 net-icmp_filter	-e trace=getsockopt,setsockopt
237 237
 net-packet_mreq -e trace=setsockopt
238
+net-packet_mreq-Xabbrev	-e trace=setsockopt -Xabbrev
239
+net-packet_mreq-Xraw	-e trace=setsockopt -Xraw
240
+net-packet_mreq-Xverbose	-e trace=setsockopt -Xverbose
238 241
 net-sockaddr	-a24 -e trace=connect
239 242
 net-tpacket_req -e trace=setsockopt
240 243
 net-tpacket_stats -e trace=getsockopt

+ 1
- 0
tests/net-packet_mreq-Xabbrev.c View File

@@ -0,0 +1 @@
1
+#include "net-packet_mreq.c"

+ 2
- 0
tests/net-packet_mreq-Xraw.c View File

@@ -0,0 +1,2 @@
1
+#define XLAT_RAW 1
2
+#include "net-packet_mreq.c"

+ 2
- 0
tests/net-packet_mreq-Xverbose.c View File

@@ -0,0 +1,2 @@
1
+#define XLAT_VERBOSE 1
2
+#include "net-packet_mreq.c"

+ 101
- 0
tests/net-packet_mreq.c View File

@@ -28,39 +28,111 @@ test_packet_mreq(const int optname, const char *const optname_str)
28 28
 
29 29
 	/* setsockopt with optname unknown */
30 30
 	packet_mreq_membership(-1, NULL, 0);
31
+#if XLAT_RAW
32
+	printf("setsockopt(-1, %#x, %#x, NULL, 0) = %s\n",
33
+	       SOL_PACKET, -1, errstr);
34
+#elif XLAT_VERBOSE
35
+	printf("setsockopt(-1, %#x /* SOL_PACKET */, %#x /* PACKET_??? */"
36
+	       ", NULL, 0) = %s\n", SOL_PACKET, -1, errstr);
37
+#else
31 38
 	printf("setsockopt(-1, SOL_PACKET, %#x /* PACKET_??? */, NULL, 0) = %s\n",
32 39
 	       -1, errstr);
40
+#endif
33 41
 
34 42
 	/* setsockopt with mr_type unknown */
35 43
 	pmreq->mr_ifindex = 0;
36 44
 	pmreq->mr_alen = ARRAY_SIZE(pmreq->mr_address);
37 45
 	packet_mreq_membership(optname, pmreq, len);
46
+#if XLAT_RAW
47
+	printf("setsockopt(-1, %#x, %#x, {mr_ifindex=%d,"
48
+	       " mr_type=%#x, mr_alen=%d, mr_address=",
49
+	       SOL_PACKET, optname, pmreq->mr_ifindex,
50
+	       pmreq->mr_type, pmreq->mr_alen);
51
+	print_quoted_hex((const void *) pmreq->mr_address,
52
+			 ARRAY_SIZE(pmreq->mr_address));
53
+	printf("}, %d) = %s\n", len, errstr);
54
+#elif XLAT_VERBOSE
55
+	printf("setsockopt(-1, %#x /* SOL_PACKET */, %#x /* %s */"
56
+	       ", {mr_ifindex=%d, mr_type=%#x /* PACKET_MR_??? */"
57
+	       ", mr_alen=%d, mr_address=",
58
+	       SOL_PACKET, optname, optname_str, pmreq->mr_ifindex,
59
+	       pmreq->mr_type, pmreq->mr_alen);
60
+	print_quoted_hex((const void *) pmreq->mr_address,
61
+			 ARRAY_SIZE(pmreq->mr_address));
62
+	printf(" /* ");
63
+	for (unsigned int i = 0; i < ARRAY_SIZE(pmreq->mr_address); i++)
64
+		printf("%s%02x", i > 0 ? ":" : "", pmreq->mr_address[i]);
65
+	printf(" */}, %d) = %s\n", len, errstr);
66
+#else
38 67
 	printf("setsockopt(-1, SOL_PACKET, %s, {mr_ifindex=%d,"
39 68
 	       " mr_type=%#x /* PACKET_MR_??? */, mr_alen=%d, mr_address=",
40 69
 	       optname_str, pmreq->mr_ifindex, pmreq->mr_type, pmreq->mr_alen);
41 70
 	for (unsigned int i = 0; i < ARRAY_SIZE(pmreq->mr_address); i++)
42 71
 		printf("%s%02x", i > 0 ? ":" : "", pmreq->mr_address[i]);
43 72
 	printf("}, %d) = %s\n", len, errstr);
73
+#endif
44 74
 
45 75
 	/* setsockopt with mr_type unknown and mr_alen > sizeof(mr_address) */
46 76
 	pmreq->mr_alen = ARRAY_SIZE(pmreq->mr_address) + 1;
47 77
 	packet_mreq_membership(optname, pmreq, len);
78
+#if XLAT_RAW
79
+	printf("setsockopt(-1, %#x, %#x, {mr_ifindex=%d,"
80
+	       " mr_type=%#x, mr_alen=%d, mr_address=",
81
+	       SOL_PACKET, optname, pmreq->mr_ifindex,
82
+	       pmreq->mr_type, pmreq->mr_alen);
83
+	print_quoted_hex((const void *) pmreq->mr_address,
84
+			 ARRAY_SIZE(pmreq->mr_address));
85
+	printf("}, %d) = %s\n", len, errstr);
86
+#elif XLAT_VERBOSE
87
+	printf("setsockopt(-1, %#x /* SOL_PACKET */, %#x /* %s */"
88
+	       ", {mr_ifindex=%d, mr_type=%#x /* PACKET_MR_??? */"
89
+	       ", mr_alen=%d, mr_address=",
90
+	       SOL_PACKET, optname, optname_str, pmreq->mr_ifindex,
91
+	       pmreq->mr_type, pmreq->mr_alen);
92
+	print_quoted_hex((const void *) pmreq->mr_address,
93
+			 ARRAY_SIZE(pmreq->mr_address));
94
+	printf(" /* ");
95
+	for (unsigned int i = 0; i < ARRAY_SIZE(pmreq->mr_address); i++)
96
+		printf("%s%02x", i > 0 ? ":" : "", pmreq->mr_address[i]);
97
+	printf(" */}, %d) = %s\n", len, errstr);
98
+#else
48 99
 	printf("setsockopt(-1, SOL_PACKET, %s, {mr_ifindex=%d,"
49 100
 	       " mr_type=%#x /* PACKET_MR_??? */, mr_alen=%d, mr_address=",
50 101
 	       optname_str, pmreq->mr_ifindex, pmreq->mr_type, pmreq->mr_alen);
51 102
 	for (unsigned int i = 0; i < ARRAY_SIZE(pmreq->mr_address); i++)
52 103
 		printf("%s%02x", i > 0 ? ":" : "", pmreq->mr_address[i]);
53 104
 	printf("}, %d) = %s\n", len, errstr);
105
+#endif
54 106
 
55 107
 	/* setsockopt with mr_type unknown and mr_alen < sizeof(mr_address) */
56 108
 	pmreq->mr_alen = ARRAY_SIZE(pmreq->mr_address) - 1;
57 109
 	packet_mreq_membership(optname, pmreq, len);
110
+#if XLAT_RAW
111
+	printf("setsockopt(-1, %#x, %#x, {mr_ifindex=%d,"
112
+	       " mr_type=%#x, mr_alen=%d, mr_address=",
113
+	       SOL_PACKET, optname, pmreq->mr_ifindex,
114
+	       pmreq->mr_type, pmreq->mr_alen);
115
+	print_quoted_hex((const void *) pmreq->mr_address, pmreq->mr_alen);
116
+	printf("}, %d) = %s\n", len, errstr);
117
+#elif XLAT_VERBOSE
118
+	printf("setsockopt(-1, %#x /* SOL_PACKET */, %#x /* %s */"
119
+	       ", {mr_ifindex=%d, mr_type=%#x /* PACKET_MR_??? */"
120
+	       ", mr_alen=%d, mr_address=",
121
+	       SOL_PACKET, optname, optname_str, pmreq->mr_ifindex,
122
+	       pmreq->mr_type, pmreq->mr_alen);
123
+	print_quoted_hex((const void *) pmreq->mr_address, pmreq->mr_alen);
124
+	printf(" /* ");
125
+	for (unsigned int i = 0; i < pmreq->mr_alen; i++)
126
+		printf("%s%02x", i > 0 ? ":" : "", pmreq->mr_address[i]);
127
+	printf(" */}, %d) = %s\n", len, errstr);
128
+#else
58 129
 	printf("setsockopt(-1, SOL_PACKET, %s, {mr_ifindex=%d,"
59 130
 	       " mr_type=%#x /* PACKET_MR_??? */, mr_alen=%d, mr_address=",
60 131
 	       optname_str, pmreq->mr_ifindex, pmreq->mr_type, pmreq->mr_alen);
61 132
 	for (unsigned int i = 0; i < pmreq->mr_alen; i++)
62 133
 		printf("%s%02x", i > 0 ? ":" : "", pmreq->mr_address[i]);
63 134
 	printf("}, %d) = %s\n", len, errstr);
135
+#endif
64 136
 
65 137
 	/* setsockopt with valid mr_type */
66 138
 	pmreq->mr_alen = ARRAY_SIZE(pmreq->mr_address);
@@ -79,19 +151,48 @@ test_packet_mreq(const int optname, const char *const optname_str)
79 151
 	for (unsigned int i = 0; i < ARRAY_SIZE(a); i++) {
80 152
 		pmreq->mr_type = a[i].type;
81 153
 		packet_mreq_membership(optname, pmreq, len);
154
+#if XLAT_RAW
155
+		printf("setsockopt(-1, %#x, %#x, {mr_ifindex=%d,"
156
+		       " mr_type=%#x, mr_alen=%d, mr_address=",
157
+		       SOL_PACKET, optname, pmreq->mr_ifindex,
158
+		       pmreq->mr_type, pmreq->mr_alen);
159
+		print_quoted_hex((const void *) pmreq->mr_address, pmreq->mr_alen);
160
+		printf("}, %d) = %s\n", len, errstr);
161
+#elif XLAT_VERBOSE
162
+		printf("setsockopt(-1, %#x /* SOL_PACKET */, %#x /* %s */"
163
+		       ", {mr_ifindex=%d, mr_type=%#x /* %s */"
164
+		       ", mr_alen=%d, mr_address=",
165
+		       SOL_PACKET, optname, optname_str, pmreq->mr_ifindex,
166
+		       pmreq->mr_type, a[i].type_str, pmreq->mr_alen);
167
+		print_quoted_hex((const void *) pmreq->mr_address, pmreq->mr_alen);
168
+		printf(" /* ");
169
+		for (unsigned int i = 0; i < pmreq->mr_alen; i++)
170
+			printf("%s%02x", i > 0 ? ":" : "", pmreq->mr_address[i]);
171
+		printf(" */}, %d) = %s\n", len, errstr);
172
+#else
82 173
 		printf("setsockopt(-1, SOL_PACKET, %s, {mr_ifindex=%d,"
83 174
 		       " mr_type=%s, mr_alen=%d, mr_address=",
84 175
 		       optname_str, pmreq->mr_ifindex, a[i].type_str, pmreq->mr_alen);
85 176
 		for (unsigned int i = 0; i < pmreq->mr_alen; i++)
86 177
 			printf("%s%02x", i > 0 ? ":" : "", pmreq->mr_address[i]);
87 178
 		printf("}, %d) = %s\n", len, errstr);
179
+#endif
88 180
 	}
89 181
 
90 182
 	/* setsockopt with optlen larger than usual */
91 183
 	len = len + 1;
92 184
 	packet_mreq_membership(optname, pmreq, len);
185
+#if XLAT_RAW
186
+	printf("setsockopt(-1, %#x, %#x, %p, %d) = %s\n",
187
+	       SOL_PACKET, optname, pmreq, len, errstr);
188
+#elif XLAT_VERBOSE
189
+	printf("setsockopt(-1, %#x /* SOL_PACKET */, %#x /* %s */"
190
+	       ", %p, %d) = %s\n", SOL_PACKET, optname, optname_str,
191
+	       pmreq, len, errstr);
192
+#else
93 193
 	printf("setsockopt(-1, SOL_PACKET, %s, %p,"
94 194
 	       " %d) = %s\n", optname_str, pmreq, len, errstr);
195
+#endif
95 196
 }
96 197
 
97 198
 int

+ 3
- 0
tests/pure_executables.list View File

@@ -233,6 +233,9 @@ munlockall
233 233
 nanosleep
234 234
 net-icmp_filter
235 235
 net-packet_mreq
236
+net-packet_mreq-Xabbrev
237
+net-packet_mreq-Xraw
238
+net-packet_mreq-Xverbose
236 239
 net-sockaddr
237 240
 net-tpacket_req
238 241
 net-tpacket_stats

+ 90
- 0
tests/sockaddr_xlat.c View File

@@ -15,6 +15,13 @@
15 15
 #include <linux/if_arp.h>
16 16
 #include <linux/if_ether.h>
17 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
18 25
 
19 26
 static void
20 27
 check_ll(void)
@@ -170,12 +177,95 @@ check_in6(void)
170 177
 	validate_in6(&in6, "::1");
171 178
 }
172 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
+
173 259
 int
174 260
 main(void)
175 261
 {
176 262
 	check_ll();
177 263
 	check_in();
178 264
 	check_in6();
265
+#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
266
+	check_sco();
267
+	check_rc();
268
+#endif
179 269
 	puts("+++ exited with 0 +++");
180 270
 	return 0;
181 271
 }

Loading…
Cancel
Save