]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/ether-addr-util.c
ether-addr-util: drop redundant "addr" from struct hw_addr_data
[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
81a56d6f
LP
27char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) {
28 assert(addr);
29 assert(buffer);
30
31 /* Like ether_ntoa() but uses %02x instead of %x to print
32 * ethernet addresses, which makes them look less funny. Also,
33 * doesn't use a static buffer. */
34
35 sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x",
36 addr->ether_addr_octet[0],
37 addr->ether_addr_octet[1],
38 addr->ether_addr_octet[2],
39 addr->ether_addr_octet[3],
40 addr->ether_addr_octet[4],
41 addr->ether_addr_octet[5]);
42
43 return buffer;
44}
b553a6b1 45
ae8e3c2b
YW
46int ether_addr_to_string_alloc(const struct ether_addr *addr, char **ret) {
47 char *buf;
48
49 assert(addr);
50 assert(ret);
51
52 buf = new(char, ETHER_ADDR_TO_STRING_MAX);
53 if (!buf)
54 return -ENOMEM;
55
56 ether_addr_to_string(addr, buf);
57
58 *ret = buf;
59 return 0;
60}
61
7a08d314 62int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b) {
583706ab 63 return memcmp(a, b, ETH_ALEN);
b553a6b1 64}
dd4d201a 65
7a08d314 66static void ether_addr_hash_func(const struct ether_addr *p, struct siphash *state) {
583706ab
YW
67 siphash24_compress(p, sizeof(struct ether_addr), state);
68}
69
7a08d314 70DEFINE_HASH_OPS(ether_addr_hash_ops, struct ether_addr, ether_addr_hash_func, ether_addr_compare);
583706ab 71
e5c1be89 72int ether_addr_from_string(const char *s, struct ether_addr *ret) {
dd4d201a
DKG
73 size_t pos = 0, n, field;
74 char sep = '\0';
75 const char *hex = HEXDIGITS, *hexoff;
76 size_t x;
77 bool touched;
78
79#define parse_fields(v) \
80 for (field = 0; field < ELEMENTSOF(v); field++) { \
81 touched = false; \
82 for (n = 0; n < (2 * sizeof(v[0])); n++) { \
83 if (s[pos] == '\0') \
84 break; \
85 hexoff = strchr(hex, s[pos]); \
234519ae 86 if (!hexoff) \
dd4d201a
DKG
87 break; \
88 assert(hexoff >= hex); \
89 x = hexoff - hex; \
90 if (x >= 16) \
91 x -= 6; /* A-F */ \
92 assert(x < 16); \
93 touched = true; \
94 v[field] <<= 4; \
95 v[field] += x; \
96 pos++; \
97 } \
98 if (!touched) \
99 return -EINVAL; \
100 if (field < (ELEMENTSOF(v)-1)) { \
101 if (s[pos] != sep) \
102 return -EINVAL; \
103 else \
104 pos++; \
105 } \
106 }
107
108 assert(s);
109 assert(ret);
110
e5c1be89 111 s += strspn(s, WHITESPACE);
dd4d201a 112 sep = s[strspn(s, hex)];
dd4d201a
DKG
113
114 if (sep == '.') {
115 uint16_t shorts[3] = { 0 };
116
117 parse_fields(shorts);
118
e5c1be89
YW
119 if (s[pos] != '\0')
120 return -EINVAL;
121
dd4d201a
DKG
122 for (n = 0; n < ELEMENTSOF(shorts); n++) {
123 ret->ether_addr_octet[2*n] = ((shorts[n] & (uint16_t)0xff00) >> 8);
124 ret->ether_addr_octet[2*n + 1] = (shorts[n] & (uint16_t)0x00ff);
125 }
e5c1be89
YW
126
127 } else if (IN_SET(sep, ':', '-')) {
128 struct ether_addr out = ETHER_ADDR_NULL;
dd4d201a
DKG
129
130 parse_fields(out.ether_addr_octet);
131
e5c1be89
YW
132 if (s[pos] != '\0')
133 return -EINVAL;
134
dd4d201a
DKG
135 for (n = 0; n < ELEMENTSOF(out.ether_addr_octet); n++)
136 ret->ether_addr_octet[n] = out.ether_addr_octet[n];
dd4d201a 137
e5c1be89
YW
138 } else
139 return -EINVAL;
140
dd4d201a
DKG
141 return 0;
142}