From 066e69986b96bf83dfa2ec09f6b03b6a7ad6bbd6 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 21 Aug 2024 14:19:58 +1000 Subject: util: Helper for formatting MAC addresses There are a couple of places where we somewhat messily open code formatting an Ethernet like MAC address for display. Add an eth_ntop() helper for this. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- util.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index 0b41404..7cb2c10 100644 --- a/util.c +++ b/util.c @@ -676,6 +676,25 @@ const char *sockaddr_ntop(const void *sa, char *dst, socklen_t size) return dst; } +/** eth_ntop() - Convert an Ethernet MAC address to text format + * @mac: MAC address + * @dst: Output buffer, minimum ETH_ADDRSTRLEN bytes + * @size: Size of buffer at @dst + * + * Return: On success, a non-null pointer to @dst, NULL on failure + */ +const char *eth_ntop(const unsigned char *mac, char *dst, size_t size) +{ + int len; + + len = snprintf(dst, size, "%02x:%02x:%02x:%02x:%02x:%02x", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + if (len < 0 || (size_t)len >= size) + return NULL; + + return dst; +} + /** str_ee_origin() - Convert socket extended error origin to a string * @ee: Socket extended error structure * -- cgit v1.2.3