]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/ether-addr-util.c
sd-ipv4ll: introduce sd_ipv4ll_set_check_mac_callback()
[thirdparty/systemd.git] / src / basic / ether-addr-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
81a56d6f 2
dccca82b 3#include <errno.h>
4fc8a29a 4#include <inttypes.h>
11c3a366 5#include <net/ethernet.h>
81a56d6f 6#include <stdio.h>
11c3a366 7#include <sys/types.h>
81a56d6f
LP
8
9#include "ether-addr-util.h"
10#include "macro.h"
dd4d201a 11#include "string-util.h"
81a56d6f 12
ca2b7cd8 13char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]) {
4fc8a29a
TR
14 assert(addr);
15 assert(buffer);
16 assert(addr->length <= HW_ADDR_MAX_SIZE);
17
18 for (size_t i = 0; i < addr->length; i++) {
ca2b7cd8 19 sprintf(&buffer[3*i], "%02"PRIx8, addr->bytes[i]);
4fc8a29a
TR
20 if (i < addr->length - 1)
21 buffer[3*i + 2] = ':';
22 }
23
24 return buffer;
25}
26
30b97725
YW
27int hw_addr_compare(const struct hw_addr_data *a, const struct hw_addr_data *b) {
28 int r;
29
30 assert(a);
31 assert(b);
32
33 r = CMP(a->length, b->length);
34 if (r != 0)
35 return r;
36
37 return memcmp(a->bytes, b->bytes, a->length);
38}
39
81a56d6f
LP
40char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) {
41 assert(addr);
42 assert(buffer);
43
44 /* Like ether_ntoa() but uses %02x instead of %x to print
45 * ethernet addresses, which makes them look less funny. Also,
46 * doesn't use a static buffer. */
47
48 sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x",
49 addr->ether_addr_octet[0],
50 addr->ether_addr_octet[1],
51 addr->ether_addr_octet[2],
52 addr->ether_addr_octet[3],
53 addr->ether_addr_octet[4],
54 addr->ether_addr_octet[5]);
55
56 return buffer;
57}
b553a6b1 58
ae8e3c2b
YW
59int ether_addr_to_string_alloc(const struct ether_addr *addr, char **ret) {
60 char *buf;
61
62 assert(addr);
63 assert(ret);
64
65 buf = new(char, ETHER_ADDR_TO_STRING_MAX);
66 if (!buf)
67 return -ENOMEM;
68
69 ether_addr_to_string(addr, buf);
70
71 *ret = buf;
72 return 0;
73}
74
7a08d314 75int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b) {
583706ab 76 return memcmp(a, b, ETH_ALEN);
b553a6b1 77}
dd4d201a 78
7a08d314 79static void ether_addr_hash_func(const struct ether_addr *p, struct siphash *state) {
583706ab
YW
80 siphash24_compress(p, sizeof(struct ether_addr), state);
81}
82
7a08d314 83DEFINE_HASH_OPS(ether_addr_hash_ops, struct ether_addr, ether_addr_hash_func, ether_addr_compare);
583706ab 84
e5c1be89 85int ether_addr_from_string(const char *s, struct ether_addr *ret) {
dd4d201a
DKG
86 size_t pos = 0, n, field;
87 char sep = '\0';
88 const char *hex = HEXDIGITS, *hexoff;
89 size_t x;
90 bool touched;
91
92#define parse_fields(v) \
93 for (field = 0; field < ELEMENTSOF(v); field++) { \
94 touched = false; \
95 for (n = 0; n < (2 * sizeof(v[0])); n++) { \
96 if (s[pos] == '\0') \
97 break; \
98 hexoff = strchr(hex, s[pos]); \
234519ae 99 if (!hexoff) \
dd4d201a
DKG
100 break; \
101 assert(hexoff >= hex); \
102 x = hexoff - hex; \
103 if (x >= 16) \
104 x -= 6; /* A-F */ \
105 assert(x < 16); \
106 touched = true; \
107 v[field] <<= 4; \
108 v[field] += x; \
109 pos++; \
110 } \
111 if (!touched) \
112 return -EINVAL; \
113 if (field < (ELEMENTSOF(v)-1)) { \
114 if (s[pos] != sep) \
115 return -EINVAL; \
116 else \
117 pos++; \
118 } \
119 }
120
121 assert(s);
122 assert(ret);
123
e5c1be89 124 s += strspn(s, WHITESPACE);
dd4d201a 125 sep = s[strspn(s, hex)];
dd4d201a
DKG
126
127 if (sep == '.') {
128 uint16_t shorts[3] = { 0 };
129
130 parse_fields(shorts);
131
e5c1be89
YW
132 if (s[pos] != '\0')
133 return -EINVAL;
134
dd4d201a
DKG
135 for (n = 0; n < ELEMENTSOF(shorts); n++) {
136 ret->ether_addr_octet[2*n] = ((shorts[n] & (uint16_t)0xff00) >> 8);
137 ret->ether_addr_octet[2*n + 1] = (shorts[n] & (uint16_t)0x00ff);
138 }
e5c1be89
YW
139
140 } else if (IN_SET(sep, ':', '-')) {
141 struct ether_addr out = ETHER_ADDR_NULL;
dd4d201a
DKG
142
143 parse_fields(out.ether_addr_octet);
144
e5c1be89
YW
145 if (s[pos] != '\0')
146 return -EINVAL;
147
dd4d201a
DKG
148 for (n = 0; n < ELEMENTSOF(out.ether_addr_octet); n++)
149 ret->ether_addr_octet[n] = out.ether_addr_octet[n];
dd4d201a 150
e5c1be89
YW
151 } else
152 return -EINVAL;
153
dd4d201a
DKG
154 return 0;
155}