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.
This commit is contained in:
Timo Rothenpieler
2020-10-28 14:44:43 +01:00
parent 48625dc437
commit 4fc8a29a7e
4 changed files with 85 additions and 0 deletions
+15
View File
@@ -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);
+24
View File
@@ -1,11 +1,35 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
#include <linux/if_infiniband.h>
#include <net/ethernet.h>
#include <stdbool.h>
#include "hash-funcs.h"
/* This is MAX_ADDR_LEN as defined in linux/netdevice.h, but net/if_arp.h
* defines a macro of the same name with a much lower size. */
#define HW_ADDR_MAX_SIZE 32
union hw_addr_union {
struct ether_addr ether;
uint8_t infiniband[INFINIBAND_ALEN];
uint8_t bytes[HW_ADDR_MAX_SIZE];
};
typedef struct hw_addr_data {
union hw_addr_union addr;
size_t length;
} hw_addr_data;
#define HW_ADDR_TO_STRING_MAX (3*HW_ADDR_MAX_SIZE)
char* hw_addr_to_string(const hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]);
/* Use only as function argument, never stand-alone! */
#define HW_ADDR_TO_STR(hw_addr) hw_addr_to_string((hw_addr), (char[HW_ADDR_TO_STRING_MAX]){})
#define HW_ADDR_NULL ((const hw_addr_data){})
#define ETHER_ADDR_FORMAT_STR "%02X%02X%02X%02X%02X%02X"
#define ETHER_ADDR_FORMAT_VAL(x) (x).ether_addr_octet[0], (x).ether_addr_octet[1], (x).ether_addr_octet[2], (x).ether_addr_octet[3], (x).ether_addr_octet[4], (x).ether_addr_octet[5]