]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/ether-addr-util.c
log: minimize includes in log.h
[thirdparty/systemd.git] / src / basic / ether-addr-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Tom Gundersen
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <net/ethernet.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25
26 #include "ether-addr-util.h"
27 #include "macro.h"
28 #include "string-util.h"
29
30 char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) {
31 assert(addr);
32 assert(buffer);
33
34 /* Like ether_ntoa() but uses %02x instead of %x to print
35 * ethernet addresses, which makes them look less funny. Also,
36 * doesn't use a static buffer. */
37
38 sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x",
39 addr->ether_addr_octet[0],
40 addr->ether_addr_octet[1],
41 addr->ether_addr_octet[2],
42 addr->ether_addr_octet[3],
43 addr->ether_addr_octet[4],
44 addr->ether_addr_octet[5]);
45
46 return buffer;
47 }
48
49 bool ether_addr_equal(const struct ether_addr *a, const struct ether_addr *b) {
50 assert(a);
51 assert(b);
52
53 return a->ether_addr_octet[0] == b->ether_addr_octet[0] &&
54 a->ether_addr_octet[1] == b->ether_addr_octet[1] &&
55 a->ether_addr_octet[2] == b->ether_addr_octet[2] &&
56 a->ether_addr_octet[3] == b->ether_addr_octet[3] &&
57 a->ether_addr_octet[4] == b->ether_addr_octet[4] &&
58 a->ether_addr_octet[5] == b->ether_addr_octet[5];
59 }
60
61 int ether_addr_from_string(const char *s, struct ether_addr *ret, size_t *offset) {
62 size_t pos = 0, n, field;
63 char sep = '\0';
64 const char *hex = HEXDIGITS, *hexoff;
65 size_t x;
66 bool touched;
67
68 #define parse_fields(v) \
69 for (field = 0; field < ELEMENTSOF(v); field++) { \
70 touched = false; \
71 for (n = 0; n < (2 * sizeof(v[0])); n++) { \
72 if (s[pos] == '\0') \
73 break; \
74 hexoff = strchr(hex, s[pos]); \
75 if (!hexoff) \
76 break; \
77 assert(hexoff >= hex); \
78 x = hexoff - hex; \
79 if (x >= 16) \
80 x -= 6; /* A-F */ \
81 assert(x < 16); \
82 touched = true; \
83 v[field] <<= 4; \
84 v[field] += x; \
85 pos++; \
86 } \
87 if (!touched) \
88 return -EINVAL; \
89 if (field < (ELEMENTSOF(v)-1)) { \
90 if (s[pos] != sep) \
91 return -EINVAL; \
92 else \
93 pos++; \
94 } \
95 }
96
97 assert(s);
98 assert(ret);
99
100 sep = s[strspn(s, hex)];
101 if (sep == '\n')
102 return -EINVAL;
103 if (!strchr(":.-", sep))
104 return -EINVAL;
105
106 if (sep == '.') {
107 uint16_t shorts[3] = { 0 };
108
109 parse_fields(shorts);
110
111 for (n = 0; n < ELEMENTSOF(shorts); n++) {
112 ret->ether_addr_octet[2*n] = ((shorts[n] & (uint16_t)0xff00) >> 8);
113 ret->ether_addr_octet[2*n + 1] = (shorts[n] & (uint16_t)0x00ff);
114 }
115 } else {
116 struct ether_addr out = { .ether_addr_octet = { 0 } };
117
118 parse_fields(out.ether_addr_octet);
119
120 for (n = 0; n < ELEMENTSOF(out.ether_addr_octet); n++)
121 ret->ether_addr_octet[n] = out.ether_addr_octet[n];
122 }
123
124 if (offset)
125 *offset = pos;
126 return 0;
127 }