]> granicus.if.org Git - strace/commitdiff
Generalise hardware address printing
authorEugene Syromyatnikov <evgsyr@gmail.com>
Fri, 31 Aug 2018 04:00:29 +0000 (06:00 +0200)
committerEugene Syromyatnikov <evgsyr@gmail.com>
Thu, 18 Jul 2019 13:58:07 +0000 (15:58 +0200)
* defs.h [!MAX_ADDR_LEN] (MAX_ADDR_LEN): New macro.
(sprint_hwaddr): New declaration.
(print_hwaddr): New inline function, a wrapper for sprint_hwaddr.
* print_fields.h (PRINT_FIELD_HWADDR_SZ): New macro.
* print_mac.c: Include "xlat/arp_hardware_types.h" under
XLAT_MACROS_ONLY.
[!MAX_ADDR_LEN] (MAX_ADDR_LEN): Remove.
(sprint_hwaddr): New function.
* sock.c (print_ifreq) <SIOCSIFHWADDR, SIOCGIFHWADDR>: Print hardware
address using PRINT_FIELD_HWADDR_SZ.

defs.h
print_fields.h
print_mac.c
sock.c

diff --git a/defs.h b/defs.h
index 742c01ec2ccbff4842f584f7649395a13b18a398..1c2624b8632a49d50eb2db7627b7d5bd18246b68 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -818,10 +818,19 @@ extern const char *sprinttime(long long sec);
 extern const char *sprinttime_nsec(long long sec, unsigned long long nsec);
 extern const char *sprinttime_usec(long long sec, unsigned long long usec);
 
+#ifndef MAX_ADDR_LEN
+# define MAX_ADDR_LEN 32
+#endif
+
 extern const char *sprint_mac_addr(const uint8_t addr[], size_t size);
 extern void print_mac_addr(const char *prefix,
                           const uint8_t addr[], size_t size);
 
+extern const char *sprint_hwaddr(const uint8_t addr[], size_t size,
+                                uint32_t devtype);
+extern void print_hwaddr(const char *prefix,
+                        const uint8_t addr[], size_t size, uint32_t devtype);
+
 extern void print_uuid(const unsigned char *uuid);
 
 extern void print_symbolic_mode_t(unsigned int);
index 63968af33c2b4ec171297fa7509f5fd0cbc729b4..3dfd9317cf8496256db989d07d684f5e4a7de28e 100644 (file)
                               (size_));                                \
        } while (0)
 
+#define PRINT_FIELD_HWADDR_SZ(prefix_, where_, field_, size_, hwtype_) \
+       do {                                                            \
+               static_assert(sizeof(((where_).field_)[0]) == 1,        \
+                             "hwaddress is not a byte array"); \
+               STRACE_PRINTF("%s%s=", (prefix_), #field_);             \
+               print_hwaddr("", (const uint8_t *) ((where_).field_),   \
+                              (size_), (hwtype_));                     \
+       } while (0)
+
 #endif /* !STRACE_PRINT_FIELDS_H */
index 8753954e92a11ea01dfc1df2b6845870ff7016e1..e851f3073fc58499c42af117e69503faf98243c8 100644 (file)
 #include "error_prints.h"
 #include "xstring.h"
 
-#ifndef MAX_ADDR_LEN
-# define MAX_ADDR_LEN 32
-#endif
+#define XLAT_MACROS_ONLY
+# include "xlat/arp_hardware_types.h"
+#undef XLAT_MACROS_ONLY
+
+DIAG_PUSH_IGNORE_OVERRIDE_INIT
+
+static uint8_t hwaddr_sizes[] = {
+       [0 ... ARPHRD_IEEE802_TR] = 255,
+
+       [ARPHRD_NETROM]     =  7 /* AX25_ADDR_LEN */,
+       [ARPHRD_ETHER]      =  6 /* ETH_ALEN */,
+       /* ARPHRD_EETHER - no actual devices in Linux */
+       [ARPHRD_AX25]       =  7 /* AX25_ADDR_LEN */,
+       /* ARPHRD_PRONET - no actual devices in Linux */
+       /* ARPHRD_CHAOS - no actual devices in Linux */
+       [ARPHRD_IEEE802]    =  6 /* FC_ALEN */,
+       [ARPHRD_ARCNET]     =  1 /* ARCNET_ALEN */,
+       /* ARPHRD_APPLETLK - no actual devices in Linux */
+       [ARPHRD_DLCI]       = sizeof(short),
+       /* ARPHRD_ATM - no explicit setting */
+       /* ARPHRD_METRICOM - no actual devices in Linux */
+       [ARPHRD_IEEE1394]   = 16 /* FWNET_ALEN */,
+       [ARPHRD_EUI64]      =  8 /* EUI64_ADDR_LEN */,
+       [ARPHRD_INFINIBAND] = 20 /* INFINIBAND_ALEN */,
+       [ARPHRD_SLIP]       =  0,
+       /* ARPHRD_CSLIP - no actual devices in Linux */
+       /* ARPHRD_SLIP6 - no actual devices in Linux */
+       /* ARPHRD_CSLIP6 - no actual devices in Linux */
+       /* ARPHRD_RSRVD - no actual devices in Linux */
+       /* ARPHRD_ADAPT - no actual devices in Linux */
+       [ARPHRD_ROSE]       =  5 /* ROSE_ADDR_LEN */,
+       [ARPHRD_X25]        =  0,
+       /* ARPHRD_HWX25 - no actual devices in Linux */
+       [ARPHRD_CAN]        =  0,
+       [ARPHRD_PPP]        =  0,
+       /* ARPHRD_CISCO - no actual devices in Linux */
+       /* ARPHRD_LAPB - no actual devices in Linux */
+       /* ARPHRD_DDCMP - no actual devices in Linux */
+       [ARPHRD_RAWHDLC]    =  0,
+       [ARPHRD_RAWIP]      =  0,
+       [ARPHRD_TUNNEL]     =  4 /* IPIP */,
+       [ARPHRD_TUNNEL6]    = 16 /* sizeof(struct in6_addr) */,
+       /* ARPHRD_FRAD - no actual devices in Linux */
+       /* ARPHRD_SKIP - no actual devices in Linux */
+       [ARPHRD_LOOPBACK]   =  6 /* ETH_ALEN */,
+       [ARPHRD_LOCALTLK]   =  1 /* LTALK_ALEN */,
+       [ARPHRD_FDDI]       =  6 /* FDDI_K_ALEN */,
+       /* ARPHRD_BIF - no actual devices in Linux */
+       [ARPHRD_SIT]        =  4,
+       [ARPHRD_IPDDP]      =  0,
+       [ARPHRD_IPGRE]      =  4,
+       [ARPHRD_PIMREG]     =  0,
+       [ARPHRD_HIPPI]      =  6 /* HIPPI_ALEN */,
+       /* ARPHRD_ASH - no actual devices in Linux */
+       /* ARPHRD_ECONET - no actual devices in Linux */
+       [ARPHRD_IRDA]       =  4 /* LAP_ALEN */,
+       /* ARPHRD_FCPP - no actual devices in Linux */
+       /* ARPHRD_FCAL - no actual devices in Linux */
+       /* ARPHRD_FCPL - no actual devices in Linux */
+       /* ARPHRD_FCFABRIC - no actual devices in Linux */
+       /* ARPHRD_IEEE802_TR - no actual devices in Linux */
+       [ARPHRD_IEEE80211]  =  6 /* ETH_ALEN */,
+       [ARPHRD_IEEE80211_PRISM] = 6 /* ETH_ALEN */,
+       [ARPHRD_IEEE80211_RADIOTAP] = 6 /* ETH_ALEN */,
+       [ARPHRD_IEEE802154]
+               = 8 /* IEEE802154_EXTENDED_ADDR_LEN */,
+       [ARPHRD_IEEE802154_MONITOR]
+               = 8 /* IEEE802154_EXTENDED_ADDR_LEN */,
+       [ARPHRD_PHONET]     =  1,
+       [ARPHRD_PHONET_PIPE] = 1,
+       [ARPHRD_CAIF]       =  0,
+       [ARPHRD_IP6GRE]     = 16 /* sizeof(struct in6_addr) */,
+       [ARPHRD_NETLINK]    =  0,
+       [ARPHRD_6LOWPAN]    =  8 /* EUI64_ADDR_LEN */
+               /* ^ or ETH_ALEN, depending on lltype */,
+       [ARPHRD_VSOCKMON]   =  0,
+};
+
+DIAG_POP_IGNORE_OVERRIDE_INIT
 
 const char *
 sprint_mac_addr(const uint8_t addr[], size_t size)
@@ -46,3 +122,27 @@ print_mac_addr(const char *prefix, const uint8_t addr[], size_t size)
        (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
                ? tprints_comment : tprints)(sprint_mac_addr(addr, size));
 }
+
+const char *
+sprint_hwaddr(const uint8_t hwaddr[], size_t size, uint32_t devtype)
+{
+       uint8_t sz = (devtype < ARRAY_SIZE(hwaddr_sizes))
+                       ? hwaddr_sizes[devtype] : 255;
+
+       return sprint_mac_addr(hwaddr, MIN(size, sz));
+}
+
+void
+print_hwaddr(const char *prefix, const uint8_t addr[], size_t size,
+            uint32_t devtype)
+{
+       tprints(prefix);
+       if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
+               print_quoted_string((const char *) addr, size,
+                                   QUOTE_FORCE_HEX);
+       if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
+               return;
+       (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
+               ? tprints_comment : tprints)(sprint_hwaddr(addr, size,
+                                                          devtype));
+}
diff --git a/sock.c b/sock.c
index a0152bddd4f5191a80c6b7054f7a86b9f1c0725a..e24898557896bd353ebcc0259a864c7cc4335e16 100644 (file)
--- a/sock.c
+++ b/sock.c
@@ -37,8 +37,6 @@ print_ifname(const char *ifname)
        print_quoted_string(ifname, IFNAMSIZ + 1, QUOTE_0_TERMINATED);
 }
 
-DIAG_PUSH_IGNORE_OVERRIDE_INIT
-
 static void
 print_ifreq(struct tcb *const tcp, const unsigned int code,
            const kernel_ulong_t arg, const struct_ifreq *const ifr)
@@ -62,88 +60,13 @@ print_ifreq(struct tcb *const tcp, const unsigned int code,
                break;
        case SIOCSIFHWADDR:
        case SIOCGIFHWADDR: {
-               static uint8_t hwaddr_sizes[] = {
-                       [0 ... ARPHRD_IEEE802_TR] = 255,
-
-                       [ARPHRD_NETROM]     =  7 /* AX25_ADDR_LEN */,
-                       [ARPHRD_ETHER]      =  6 /* ETH_ALEN */,
-                       /* ARPHRD_EETHER - no actual devices in Linux */
-                       [ARPHRD_AX25]       =  7 /* AX25_ADDR_LEN */,
-                       /* ARPHRD_PRONET - no actual devices in Linux */
-                       /* ARPHRD_CHAOS - no actual devices in Linux */
-                       [ARPHRD_IEEE802]    =  6 /* FC_ALEN */,
-                       [ARPHRD_ARCNET]     =  1 /* ARCNET_ALEN */,
-                       /* ARPHRD_APPLETLK - no actual devices in Linux */
-                       [ARPHRD_DLCI]       = sizeof(short),
-                       /* ARPHRD_ATM - no explicit setting */
-                       /* ARPHRD_METRICOM - no actual devices in Linux */
-                       [ARPHRD_IEEE1394]   = 16 /* FWNET_ALEN */,
-                       [ARPHRD_EUI64]      =  8 /* EUI64_ADDR_LEN */,
-                       [ARPHRD_INFINIBAND] = 20 /* INFINIBAND_ALEN */,
-                       [ARPHRD_SLIP]       =  0,
-                       /* ARPHRD_CSLIP - no actual devices in Linux */
-                       /* ARPHRD_SLIP6 - no actual devices in Linux */
-                       /* ARPHRD_CSLIP6 - no actual devices in Linux */
-                       /* ARPHRD_RSRVD - no actual devices in Linux */
-                       /* ARPHRD_ADAPT - no actual devices in Linux */
-                       [ARPHRD_ROSE]       =  5 /* ROSE_ADDR_LEN */,
-                       [ARPHRD_X25]        =  0,
-                       /* ARPHRD_HWX25 - no actual devices in Linux */
-                       [ARPHRD_CAN]        =  0,
-                       [ARPHRD_PPP]        =  0,
-                       /* ARPHRD_CISCO - no actual devices in Linux */
-                       /* ARPHRD_LAPB - no actual devices in Linux */
-                       /* ARPHRD_DDCMP - no actual devices in Linux */
-                       [ARPHRD_RAWHDLC]    =  0,
-                       [ARPHRD_RAWIP]      =  0,
-                       [ARPHRD_TUNNEL]     =  4 /* IPIP */,
-                       [ARPHRD_TUNNEL6]    = 16 /* sizeof(struct in6_addr) */,
-                       /* ARPHRD_FRAD - no actual devices in Linux */
-                       /* ARPHRD_SKIP - no actual devices in Linux */
-                       [ARPHRD_LOOPBACK]   =  6 /* ETH_ALEN */,
-                       [ARPHRD_LOCALTLK]   =  1 /* LTALK_ALEN */,
-                       [ARPHRD_FDDI]       =  6 /* FDDI_K_ALEN */,
-                       /* ARPHRD_BIF - no actual devices in Linux */
-                       [ARPHRD_SIT]        =  4,
-                       [ARPHRD_IPDDP]      =  0,
-                       [ARPHRD_IPGRE]      =  4,
-                       [ARPHRD_PIMREG]     =  0,
-                       [ARPHRD_HIPPI]      =  6 /* HIPPI_ALEN */,
-                       /* ARPHRD_ASH - no actual devices in Linux */
-                       /* ARPHRD_ECONET - no actual devices in Linux */
-                       [ARPHRD_IRDA]       =  4 /* LAP_ALEN */,
-                       /* ARPHRD_FCPP - no actual devices in Linux */
-                       /* ARPHRD_FCAL - no actual devices in Linux */
-                       /* ARPHRD_FCPL - no actual devices in Linux */
-                       /* ARPHRD_FCFABRIC - no actual devices in Linux */
-                       /* ARPHRD_IEEE802_TR - no actual devices in Linux */
-                       [ARPHRD_IEEE80211]  =  6 /* ETH_ALEN */,
-                       [ARPHRD_IEEE80211_PRISM] = 6 /* ETH_ALEN */,
-                       [ARPHRD_IEEE80211_RADIOTAP] = 6 /* ETH_ALEN */,
-                       [ARPHRD_IEEE802154]
-                               = 8 /* IEEE802154_EXTENDED_ADDR_LEN */,
-                       [ARPHRD_IEEE802154_MONITOR]
-                               = 8 /* IEEE802154_EXTENDED_ADDR_LEN */,
-                       [ARPHRD_PHONET]     =  1,
-                       [ARPHRD_PHONET_PIPE] = 1,
-                       [ARPHRD_CAIF]       =  0,
-                       [ARPHRD_IP6GRE]     = 16 /* sizeof(struct in6_addr) */,
-                       [ARPHRD_NETLINK]    =  0,
-                       [ARPHRD_6LOWPAN]    =  8 /* EUI64_ADDR_LEN */
-                               /* ^ or ETH_ALEN, depending on lltype */,
-                       [ARPHRD_VSOCKMON]   =  0,
-               };
-
-               uint16_t proto = ifr->ifr_hwaddr.sa_family;
-               uint8_t sz = (proto < ARRAY_SIZE(hwaddr_sizes))
-                               ? hwaddr_sizes[proto] : 255;
-
                PRINT_FIELD_XVAL_SORTED_SIZED("ifr_hwaddr={", ifr->ifr_hwaddr,
                                              sa_family, arp_hardware_types,
                                              arp_hardware_types_size,
                                              "ARPHRD_???");
-               PRINT_FIELD_MAC_SZ(", ", ifr->ifr_hwaddr, sa_data,
-                                  MIN(sizeof(ifr->ifr_hwaddr.sa_data), sz));
+               PRINT_FIELD_HWADDR_SZ(", ", ifr->ifr_hwaddr, sa_data,
+                                     sizeof(ifr->ifr_hwaddr.sa_data),
+                                     ifr->ifr_hwaddr.sa_family);
                tprints("}");
                break;
        }
@@ -184,8 +107,6 @@ print_ifreq(struct tcb *const tcp, const unsigned int code,
        }
 }
 
-DIAG_POP_IGNORE_OVERRIDE_INIT
-
 static unsigned int
 print_ifc_len(int len)
 {