]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/ether-addr-util.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / ether-addr-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
81a56d6f
LP
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
11c3a366 21#include <net/ethernet.h>
81a56d6f 22#include <stdio.h>
11c3a366 23#include <sys/types.h>
81a56d6f
LP
24
25#include "ether-addr-util.h"
26#include "macro.h"
dd4d201a 27#include "string-util.h"
81a56d6f
LP
28
29char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) {
30 assert(addr);
31 assert(buffer);
32
33 /* Like ether_ntoa() but uses %02x instead of %x to print
34 * ethernet addresses, which makes them look less funny. Also,
35 * doesn't use a static buffer. */
36
37 sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x",
38 addr->ether_addr_octet[0],
39 addr->ether_addr_octet[1],
40 addr->ether_addr_octet[2],
41 addr->ether_addr_octet[3],
42 addr->ether_addr_octet[4],
43 addr->ether_addr_octet[5]);
44
45 return buffer;
46}
b553a6b1 47
b553a6b1
LP
48bool ether_addr_equal(const struct ether_addr *a, const struct ether_addr *b) {
49 assert(a);
50 assert(b);
51
52 return a->ether_addr_octet[0] == b->ether_addr_octet[0] &&
53 a->ether_addr_octet[1] == b->ether_addr_octet[1] &&
54 a->ether_addr_octet[2] == b->ether_addr_octet[2] &&
55 a->ether_addr_octet[3] == b->ether_addr_octet[3] &&
56 a->ether_addr_octet[4] == b->ether_addr_octet[4] &&
57 a->ether_addr_octet[5] == b->ether_addr_octet[5];
58}
dd4d201a
DKG
59
60int ether_addr_from_string(const char *s, struct ether_addr *ret, size_t *offset) {
61 size_t pos = 0, n, field;
62 char sep = '\0';
63 const char *hex = HEXDIGITS, *hexoff;
64 size_t x;
65 bool touched;
66
67#define parse_fields(v) \
68 for (field = 0; field < ELEMENTSOF(v); field++) { \
69 touched = false; \
70 for (n = 0; n < (2 * sizeof(v[0])); n++) { \
71 if (s[pos] == '\0') \
72 break; \
73 hexoff = strchr(hex, s[pos]); \
74 if (hexoff == NULL) \
75 break; \
76 assert(hexoff >= hex); \
77 x = hexoff - hex; \
78 if (x >= 16) \
79 x -= 6; /* A-F */ \
80 assert(x < 16); \
81 touched = true; \
82 v[field] <<= 4; \
83 v[field] += x; \
84 pos++; \
85 } \
86 if (!touched) \
87 return -EINVAL; \
88 if (field < (ELEMENTSOF(v)-1)) { \
89 if (s[pos] != sep) \
90 return -EINVAL; \
91 else \
92 pos++; \
93 } \
94 }
95
96 assert(s);
97 assert(ret);
98
99 sep = s[strspn(s, hex)];
100 if (sep == '\n')
101 return -EINVAL;
102 if (strchr(":.-", sep) == NULL)
103 return -EINVAL;
104
105 if (sep == '.') {
106 uint16_t shorts[3] = { 0 };
107
108 parse_fields(shorts);
109
110 for (n = 0; n < ELEMENTSOF(shorts); n++) {
111 ret->ether_addr_octet[2*n] = ((shorts[n] & (uint16_t)0xff00) >> 8);
112 ret->ether_addr_octet[2*n + 1] = (shorts[n] & (uint16_t)0x00ff);
113 }
114 } else {
115 struct ether_addr out = { .ether_addr_octet = { 0 } };
116
117 parse_fields(out.ether_addr_octet);
118
119 for (n = 0; n < ELEMENTSOF(out.ether_addr_octet); n++)
120 ret->ether_addr_octet[n] = out.ether_addr_octet[n];
121 }
122
123 if (offset)
124 *offset = pos;
125 return 0;
126}