]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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 | |
4fc8a29a TR |
13 | char* hw_addr_to_string(const hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]) { |
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++) { | |
19 | sprintf(&buffer[3*i], "%02"PRIx8, addr->addr.bytes[i]); | |
20 | if (i < addr->length - 1) | |
21 | buffer[3*i + 2] = ':'; | |
22 | } | |
23 | ||
24 | return buffer; | |
25 | } | |
26 | ||
81a56d6f LP |
27 | char* 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 | |
7a08d314 | 46 | int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b) { |
583706ab | 47 | return memcmp(a, b, ETH_ALEN); |
b553a6b1 | 48 | } |
dd4d201a | 49 | |
7a08d314 | 50 | static void ether_addr_hash_func(const struct ether_addr *p, struct siphash *state) { |
583706ab YW |
51 | siphash24_compress(p, sizeof(struct ether_addr), state); |
52 | } | |
53 | ||
7a08d314 | 54 | DEFINE_HASH_OPS(ether_addr_hash_ops, struct ether_addr, ether_addr_hash_func, ether_addr_compare); |
583706ab | 55 | |
e5c1be89 | 56 | int ether_addr_from_string(const char *s, struct ether_addr *ret) { |
dd4d201a DKG |
57 | size_t pos = 0, n, field; |
58 | char sep = '\0'; | |
59 | const char *hex = HEXDIGITS, *hexoff; | |
60 | size_t x; | |
61 | bool touched; | |
62 | ||
63 | #define parse_fields(v) \ | |
64 | for (field = 0; field < ELEMENTSOF(v); field++) { \ | |
65 | touched = false; \ | |
66 | for (n = 0; n < (2 * sizeof(v[0])); n++) { \ | |
67 | if (s[pos] == '\0') \ | |
68 | break; \ | |
69 | hexoff = strchr(hex, s[pos]); \ | |
234519ae | 70 | if (!hexoff) \ |
dd4d201a DKG |
71 | break; \ |
72 | assert(hexoff >= hex); \ | |
73 | x = hexoff - hex; \ | |
74 | if (x >= 16) \ | |
75 | x -= 6; /* A-F */ \ | |
76 | assert(x < 16); \ | |
77 | touched = true; \ | |
78 | v[field] <<= 4; \ | |
79 | v[field] += x; \ | |
80 | pos++; \ | |
81 | } \ | |
82 | if (!touched) \ | |
83 | return -EINVAL; \ | |
84 | if (field < (ELEMENTSOF(v)-1)) { \ | |
85 | if (s[pos] != sep) \ | |
86 | return -EINVAL; \ | |
87 | else \ | |
88 | pos++; \ | |
89 | } \ | |
90 | } | |
91 | ||
92 | assert(s); | |
93 | assert(ret); | |
94 | ||
e5c1be89 | 95 | s += strspn(s, WHITESPACE); |
dd4d201a | 96 | sep = s[strspn(s, hex)]; |
dd4d201a DKG |
97 | |
98 | if (sep == '.') { | |
99 | uint16_t shorts[3] = { 0 }; | |
100 | ||
101 | parse_fields(shorts); | |
102 | ||
e5c1be89 YW |
103 | if (s[pos] != '\0') |
104 | return -EINVAL; | |
105 | ||
dd4d201a DKG |
106 | for (n = 0; n < ELEMENTSOF(shorts); n++) { |
107 | ret->ether_addr_octet[2*n] = ((shorts[n] & (uint16_t)0xff00) >> 8); | |
108 | ret->ether_addr_octet[2*n + 1] = (shorts[n] & (uint16_t)0x00ff); | |
109 | } | |
e5c1be89 YW |
110 | |
111 | } else if (IN_SET(sep, ':', '-')) { | |
112 | struct ether_addr out = ETHER_ADDR_NULL; | |
dd4d201a DKG |
113 | |
114 | parse_fields(out.ether_addr_octet); | |
115 | ||
e5c1be89 YW |
116 | if (s[pos] != '\0') |
117 | return -EINVAL; | |
118 | ||
dd4d201a DKG |
119 | for (n = 0; n < ELEMENTSOF(out.ether_addr_octet); n++) |
120 | ret->ether_addr_octet[n] = out.ether_addr_octet[n]; | |
dd4d201a | 121 | |
e5c1be89 YW |
122 | } else |
123 | return -EINVAL; | |
124 | ||
dd4d201a DKG |
125 | return 0; |
126 | } |