]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/ether-addr-util.h
test: add tests for parse_hw_addr()
[thirdparty/systemd.git] / src / basic / ether-addr-util.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
d8500c53
TG
2#pragma once
3
4fc8a29a 4#include <linux/if_infiniband.h>
d8500c53 5#include <net/ethernet.h>
b553a6b1 6#include <stdbool.h>
d8500c53 7
583706ab 8#include "hash-funcs.h"
76a5d3de 9#include "in-addr-util.h"
1f86a3fe
YW
10#include "macro.h"
11#include "memory-util.h"
583706ab 12
4fc8a29a
TR
13/* This is MAX_ADDR_LEN as defined in linux/netdevice.h, but net/if_arp.h
14 * defines a macro of the same name with a much lower size. */
15#define HW_ADDR_MAX_SIZE 32
16
ca2b7cd8 17struct hw_addr_data {
4fc8a29a 18 size_t length;
ca2b7cd8
YW
19 union {
20 struct ether_addr ether;
21 uint8_t infiniband[INFINIBAND_ALEN];
76a5d3de
YW
22 struct in_addr in;
23 struct in6_addr in6;
ca2b7cd8
YW
24 uint8_t bytes[HW_ADDR_MAX_SIZE];
25 };
26};
4fc8a29a 27
76a5d3de
YW
28int parse_hw_addr_full(const char *s, size_t expected_len, struct hw_addr_data *ret);
29static inline int parse_hw_addr(const char *s, struct hw_addr_data *ret) {
30 return parse_hw_addr_full(s, 0, ret);
31}
32
4fc8a29a 33#define HW_ADDR_TO_STRING_MAX (3*HW_ADDR_MAX_SIZE)
ca2b7cd8 34char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]);
4fc8a29a 35
e265fa81
ZJS
36/* Note: the lifetime of the compound literal is the immediately surrounding block,
37 * see C11 §6.5.2.5, and
38 * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
4fc8a29a
TR
39#define HW_ADDR_TO_STR(hw_addr) hw_addr_to_string((hw_addr), (char[HW_ADDR_TO_STRING_MAX]){})
40
ca2b7cd8 41#define HW_ADDR_NULL ((const struct hw_addr_data){})
4fc8a29a 42
30b97725
YW
43int hw_addr_compare(const struct hw_addr_data *a, const struct hw_addr_data *b);
44static inline bool hw_addr_equal(const struct hw_addr_data *a, const struct hw_addr_data *b) {
45 return hw_addr_compare(a, b) == 0;
46}
47static inline bool hw_addr_is_null(const struct hw_addr_data *addr) {
de0f1579
YW
48 assert(addr);
49 return addr->length == 0 || memeqzero(addr->bytes, addr->length);
30b97725
YW
50}
51
907d5ce4
YW
52extern const struct hash_ops hw_addr_hash_ops;
53
d8500c53
TG
54#define ETHER_ADDR_FORMAT_STR "%02X%02X%02X%02X%02X%02X"
55#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]
81a56d6f
LP
56
57#define ETHER_ADDR_TO_STRING_MAX (3*6)
81a56d6f 58char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]);
ae8e3c2b 59int ether_addr_to_string_alloc(const struct ether_addr *addr, char **ret);
4b574fd8
YW
60/* Use only as function argument, never stand-alone! */
61#define ETHER_ADDR_TO_STR(addr) ether_addr_to_string((addr), (char[ETHER_ADDR_TO_STRING_MAX]){})
b553a6b1 62
7a08d314 63int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b);
583706ab
YW
64static inline bool ether_addr_equal(const struct ether_addr *a, const struct ether_addr *b) {
65 return ether_addr_compare(a, b) == 0;
66}
6b0132e4
ZJS
67
68#define ETHER_ADDR_NULL ((const struct ether_addr){})
69
70static inline bool ether_addr_is_null(const struct ether_addr *addr) {
71 return ether_addr_equal(addr, &ETHER_ADDR_NULL);
72}
dd4d201a 73
1f86a3fe
YW
74static inline bool ether_addr_is_broadcast(const struct ether_addr *addr) {
75 assert(addr);
76 return memeqbyte(0xff, addr->ether_addr_octet, ETH_ALEN);
77}
78
79static inline bool ether_addr_is_multicast(const struct ether_addr *addr) {
80 assert(addr);
81 return FLAGS_SET(addr->ether_addr_octet[0], 0x01);
82}
83
84static inline bool ether_addr_is_unicast(const struct ether_addr *addr) {
85 return !ether_addr_is_multicast(addr);
86}
87
88static inline bool ether_addr_is_local(const struct ether_addr *addr) {
89 /* Determine if the Ethernet address is locally-assigned one (IEEE 802) */
90 assert(addr);
91 return !FLAGS_SET(addr->ether_addr_octet[0], 0x02);
92}
93
e5c1be89 94int ether_addr_from_string(const char *s, struct ether_addr *ret);
583706ab
YW
95
96extern const struct hash_ops ether_addr_hash_ops;