diff options
author | Timo Rothenpieler <timo@rothenpieler.org> | 2020-10-26 16:22:13 +0100 |
---|---|---|
committer | Timo Rothenpieler <timo@rothenpieler.org> | 2020-10-28 14:44:43 +0100 |
commit | 4fc8a29a7e06debb65f4e2651a03804beb41f781 (patch) | |
tree | 86209a9c7d55c72cbd7d08f4e3cb874c53277ba8 /src/basic/ether-addr-util.c | |
parent | Merge pull request #17471 from keszybz/man-update-fedora-version (diff) | |
download | systemd-4fc8a29a7e06debb65f4e2651a03804beb41f781.tar.xz systemd-4fc8a29a7e06debb65f4e2651a03804beb41f781.zip |
sd-netlink: introduce netlink_message_{read,append}_hw_addr
Hardware addresses come in various shapes and sizes, these new functions
and accomapying data structures account for that instead of hard-coding
a hardware address to the 6 bytes of an ethernet MAC.
Diffstat (limited to 'src/basic/ether-addr-util.c')
-rw-r--r-- | src/basic/ether-addr-util.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/basic/ether-addr-util.c b/src/basic/ether-addr-util.c index e875696a1a..2af2ce02e5 100644 --- a/src/basic/ether-addr-util.c +++ b/src/basic/ether-addr-util.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include <errno.h> +#include <inttypes.h> #include <net/ethernet.h> #include <stdio.h> #include <sys/types.h> @@ -9,6 +10,20 @@ #include "macro.h" #include "string-util.h" +char* hw_addr_to_string(const hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]) { + assert(addr); + assert(buffer); + assert(addr->length <= HW_ADDR_MAX_SIZE); + + for (size_t i = 0; i < addr->length; i++) { + sprintf(&buffer[3*i], "%02"PRIx8, addr->addr.bytes[i]); + if (i < addr->length - 1) + buffer[3*i + 2] = ':'; + } + + return buffer; +} + char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) { assert(addr); assert(buffer); |