Skip to content

Commit dc7901f

Browse files
esyrldv-alt
andcommitted
Add a common MAC address printing routine
* print_mac.c: New file. * Makefile.am (strace_SOURCES): Add it. * defs.h (sprint_mac_addr): New declaration. (print_mac_addr): New function, a thin wrapper around sprint_mac_addr. * print_fields.h (PRINT_FIELD_MAC, PRINT_FIELD_MAC_SZ): New macros. * rtnl_link.c (decode_ifla_bridge_id): Use PRINT_FIELD_MAC for bridge address printing. * sock.c (print_ifreq) <case SIOCGIFHWADDR>: Use print_mac_addr for ifr_hwaddr printing. * sockaddr.c (print_sockaddr_data_bt): Use print_mac_addr for {sco,rc,l2}_bdaddr field printing. Co-Authored-by: Dmitry V. Levin <[email protected]>
1 parent 2d82c2c commit dc7901f

File tree

7 files changed

+91
-23
lines changed

7 files changed

+91
-23
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ strace_SOURCES = \
242242
print_group_req.c \
243243
print_fields.h \
244244
print_ifindex.c \
245+
print_mac.c \
245246
print_mq_attr.c \
246247
print_msgbuf.c \
247248
print_sg_req_info.c \

defs.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,9 @@ extern const char *sprintflags_ex(const char *prefix, const struct xlat *xlat,
687687
extern const char *sprinttime(long long sec);
688688
extern const char *sprinttime_nsec(long long sec, unsigned long long nsec);
689689
extern const char *sprinttime_usec(long long sec, unsigned long long usec);
690+
691+
extern const char *sprint_mac_addr(const uint8_t addr[], size_t size);
692+
690693
extern void print_symbolic_mode_t(unsigned int);
691694
extern void print_numeric_umode_t(unsigned short);
692695
extern void print_numeric_long_umask(unsigned long);
@@ -969,6 +972,13 @@ extern void tprints(const char *str);
969972
extern void tprintf_comment(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
970973
extern void tprints_comment(const char *str);
971974

975+
static inline void
976+
print_mac_addr(const char *prefix, const uint8_t addr[], size_t size)
977+
{
978+
tprints(prefix);
979+
tprints(sprint_mac_addr(addr, size));
980+
}
981+
972982
#if SUPPORTED_PERSONALITIES > 1
973983
extern void set_personality(unsigned int personality);
974984
extern unsigned current_personality;

print_fields.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,17 @@
220220
printpath((tcp_), (where_).field_); \
221221
} while (0)
222222

223+
#define PRINT_FIELD_MAC(prefix_, where_, field_) \
224+
PRINT_FIELD_MAC_SZ((prefix_), (where_), field_, \
225+
ARRAY_SIZE((where_).field_))
226+
227+
#define PRINT_FIELD_MAC_SZ(prefix_, where_, field_, size_) \
228+
do { \
229+
static_assert(sizeof(((where_).field_)[0]) == 1, \
230+
"MAC address is not a byte array"); \
231+
STRACE_PRINTF("%s%s=", (prefix_), #field_); \
232+
print_mac_addr("", (const uint8_t *) ((where_).field_), \
233+
(size_)); \
234+
} while (0)
235+
223236
#endif /* !STRACE_PRINT_FIELDS_H */

print_mac.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2018 The strace developers.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
* 3. The name of the author may not be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#include "defs.h"
29+
30+
#include "error_prints.h"
31+
#include "xstring.h"
32+
33+
#ifndef MAX_ADDR_LEN
34+
# define MAX_ADDR_LEN 32
35+
#endif
36+
37+
const char *
38+
sprint_mac_addr(const uint8_t addr[], size_t size)
39+
{
40+
static char res[MAX_ADDR_LEN * 3];
41+
42+
if (size > MAX_ADDR_LEN) {
43+
error_func_msg("Address size (%zu) is more than maximum "
44+
"supported (%u)", size, MAX_ADDR_LEN);
45+
46+
return NULL;
47+
}
48+
49+
char *ptr = res;
50+
51+
for (size_t i = 0; i < size; i++)
52+
ptr = xappendstr(res, ptr, "%s%02x", i ? ":" : "", addr[i]);
53+
54+
return res;
55+
}

rtnl_link.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ decode_ifla_bridge_id(struct tcb *const tcp,
113113
if (len < sizeof(id))
114114
return false;
115115
else if (!umove_or_printaddr(tcp, addr, &id)) {
116-
tprintf("{prio=[%u, %u], addr=%02x:%02x:%02x:%02x:%02x:%02x}",
117-
id.prio[0], id.prio[1],
118-
id.addr[0], id.addr[1], id.addr[2],
119-
id.addr[3], id.addr[4], id.addr[5]);
116+
tprintf("{prio=[%u, %u]", id.prio[0], id.prio[1]);
117+
PRINT_FIELD_MAC(", ", id, addr);
118+
tprints("}");
120119
}
121120

122121
return true;

sock.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,8 @@ print_ifreq(struct tcb *const tcp, const unsigned int code,
7878
case SIOCGIFHWADDR: {
7979
/* XXX Are there other hardware addresses
8080
than 6-byte MACs? */
81-
const unsigned char *bytes =
82-
(unsigned char *) &ifr->ifr_hwaddr.sa_data;
83-
tprintf("ifr_hwaddr=%02x:%02x:%02x:%02x:%02x:%02x",
84-
bytes[0], bytes[1], bytes[2],
85-
bytes[3], bytes[4], bytes[5]);
81+
print_mac_addr("ifr_hwaddr=",
82+
(const uint8_t *) &ifr->ifr_hwaddr.sa_data, 6);
8683
break;
8784
}
8885
case SIOCSIFFLAGS:

sockaddr.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -384,29 +384,22 @@ print_sockaddr_data_bt(const void *const buf, const int addrlen)
384384
}
385385
case sizeof(struct sockaddr_sco): {
386386
const struct sockaddr_sco *const sco = buf;
387-
tprintf("sco_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x",
388-
sco->sco_bdaddr.b[0], sco->sco_bdaddr.b[1],
389-
sco->sco_bdaddr.b[2], sco->sco_bdaddr.b[3],
390-
sco->sco_bdaddr.b[4], sco->sco_bdaddr.b[5]);
387+
print_mac_addr("sco_bdaddr=", sco->sco_bdaddr.b,
388+
sizeof(sco->sco_bdaddr.b));
391389
break;
392390
}
393391
case sizeof(struct sockaddr_rc): {
394392
const struct sockaddr_rc *const rc = buf;
395-
tprintf("rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
396-
", rc_channel=%u",
397-
rc->rc_bdaddr.b[0], rc->rc_bdaddr.b[1],
398-
rc->rc_bdaddr.b[2], rc->rc_bdaddr.b[3],
399-
rc->rc_bdaddr.b[4], rc->rc_bdaddr.b[5],
400-
rc->rc_channel);
393+
print_mac_addr("rc_bdaddr=", rc->rc_bdaddr.b,
394+
sizeof(rc->rc_bdaddr.b));
395+
tprintf(", rc_channel=%u", rc->rc_channel);
401396
break;
402397
}
403398
case sizeof(struct sockaddr_l2): {
404399
const struct sockaddr_l2 *const l2 = buf;
405400
print_bluetooth_l2_psm("l2_psm=", l2->l2_psm);
406-
tprintf(", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x",
407-
l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1],
408-
l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3],
409-
l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5]);
401+
print_mac_addr(", l2_bdaddr=", l2->l2_bdaddr.b,
402+
sizeof(l2->l2_bdaddr.b));
410403
print_bluetooth_l2_cid(", l2_cid=", l2->l2_cid);
411404
tprints(", l2_bdaddr_type=");
412405
printxval_index(bdaddr_types, l2->l2_bdaddr_type,

0 commit comments

Comments
 (0)