• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 25770446501

12 May 2026 11:15PM UTC coverage: 72.513% (-0.1%) from 72.65%
25770446501

push

github

web-flow
dhcp: random trivial cleanups (#42061)

8 of 25 new or added lines in 4 files covered. (32.0%)

4141 existing lines in 87 files now uncovered.

327776 of 452023 relevant lines covered (72.51%)

1437215.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.94
/src/basic/ether-addr-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <net/ethernet.h>
4
#include <net/if_arp.h>
5
#include <stdio.h>
6

7
#include "ether-addr-util.h"
8
#include "hash-funcs.h"
9
#include "hexdecoct.h"
10
#include "in-addr-util.h"
11
#include "memory-util.h"
12
#include "siphash24.h"
13
#include "string-util.h"
14

15
char* hw_addr_to_string_full(
5,637✔
16
                const struct hw_addr_data *addr,
17
                HardwareAddressToStringFlags flags,
18
                char buffer[static HW_ADDR_TO_STRING_MAX]) {
19

20
        assert(addr);
5,637✔
21
        assert(buffer);
5,637✔
22
        assert(addr->length <= HW_ADDR_MAX_SIZE);
5,637✔
23

24
        for (size_t i = 0, j = 0; i < addr->length; i++) {
43,623✔
25
                buffer[j++] = hexchar(addr->bytes[i] >> 4);
37,986✔
26
                buffer[j++] = hexchar(addr->bytes[i] & 0x0f);
37,986✔
27
                if (!FLAGS_SET(flags, HW_ADDR_TO_STRING_NO_COLON))
37,986✔
28
                        buffer[j++] = ':';
37,956✔
29
        }
30

31
        buffer[addr->length == 0 || FLAGS_SET(flags, HW_ADDR_TO_STRING_NO_COLON) ?
5,637✔
32
               addr->length * 2 :
5,642✔
33
               addr->length * 3 - 1] = '\0';
5,632✔
34
        return buffer;
5,637✔
35
}
36

37
struct hw_addr_data *hw_addr_set(struct hw_addr_data *addr, const uint8_t *bytes, size_t length) {
303✔
38
        assert(addr);
303✔
39
        assert(length <= HW_ADDR_MAX_SIZE);
303✔
40

41
        addr->length = length;
303✔
42
        memcpy_safe(addr->bytes, bytes, length);
303✔
43
        return addr;
303✔
44
}
45

46
int hw_addr_compare(const struct hw_addr_data *a, const struct hw_addr_data *b) {
13,725✔
47
        int r;
13,725✔
48

49
        assert(a);
13,725✔
50
        assert(b);
13,725✔
51

52
        r = CMP(a->length, b->length);
13,725✔
53
        if (r != 0)
10,403✔
54
                return r;
55

56
        return memcmp(a->bytes, b->bytes, a->length);
9,440✔
57
}
58

59
void hw_addr_hash_func(const struct hw_addr_data *p, struct siphash *state) {
2,352✔
60
        assert(p);
2,352✔
61
        assert(state);
2,352✔
62

63
        siphash24_compress_typesafe(p->length, state);
2,352✔
64
        siphash24_compress_safe(p->bytes, p->length, state);
2,352✔
65
}
2,352✔
66

67
bool hw_addr_is_null(const struct hw_addr_data *addr) {
10,862✔
68
        assert(addr);
10,862✔
69
        return addr->length == 0 || memeqzero(addr->bytes, addr->length);
10,862✔
70
}
71

72
bool hw_addr_is_valid(const struct hw_addr_data *addr, uint16_t iftype) {
174✔
73
        assert(addr);
174✔
74

75
        switch (iftype) {
174✔
76
        case ARPHRD_ETHER:
174✔
77
                /* Refuse all zero and all 0xFF. */
78
                if (addr->length != ETH_ALEN)
174✔
79
                        return false;
80

81
                return !ether_addr_is_null(&addr->ether) && !ether_addr_is_broadcast(&addr->ether);
348✔
82

83
        case ARPHRD_INFINIBAND:
×
84
                /* The last 8 bytes cannot be zero. */
85
                if (addr->length != INFINIBAND_ALEN)
×
86
                        return false;
87

88
                return !memeqzero(addr->bytes + INFINIBAND_ALEN - 8, 8);
×
89

90
        default:
91
                return false;
92
        }
93
}
94

95
DEFINE_HASH_OPS(hw_addr_hash_ops, struct hw_addr_data, hw_addr_hash_func, hw_addr_compare);
96
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(hw_addr_hash_ops_free, struct hw_addr_data, hw_addr_hash_func, hw_addr_compare, free);
17✔
97

98
char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) {
38✔
99
        assert(addr);
38✔
100
        assert(buffer);
38✔
101

102
        /* Like ether_ntoa() but uses %02x instead of %x to print
103
         * ethernet addresses, which makes them look less funny. Also,
104
         * doesn't use a static buffer. */
105

106
        sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x",
38✔
107
                addr->ether_addr_octet[0],
38✔
108
                addr->ether_addr_octet[1],
38✔
109
                addr->ether_addr_octet[2],
38✔
110
                addr->ether_addr_octet[3],
38✔
111
                addr->ether_addr_octet[4],
38✔
112
                addr->ether_addr_octet[5]);
38✔
113

114
        return buffer;
38✔
115
}
116

117
int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b) {
14,905✔
118
        assert(a);
14,905✔
119
        assert(b);
14,905✔
120

121
        return memcmp(a, b, ETH_ALEN);
14,905✔
122
}
123

124
static void ether_addr_hash_func(const struct ether_addr *p, struct siphash *state) {
37✔
125
        assert(p);
37✔
126

127
        siphash24_compress_typesafe(*p, state);
37✔
128
}
37✔
129

130
bool ether_addr_is_broadcast(const struct ether_addr *addr) {
1,248✔
131
        assert(addr);
1,248✔
132
        return memeqbyte(0xff, addr->ether_addr_octet, ETH_ALEN);
1,248✔
133
}
134

135
DEFINE_HASH_OPS(ether_addr_hash_ops, struct ether_addr, ether_addr_hash_func, ether_addr_compare);
136
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(ether_addr_hash_ops_free, struct ether_addr, ether_addr_hash_func, ether_addr_compare, free);
3✔
137

138
static int parse_hw_addr_one_field(const char **s, char sep, size_t len, uint8_t *buf) {
4,308✔
139
        const char *hex = HEXDIGITS, *p;
4,308✔
140
        uint16_t data = 0;
4,308✔
141
        bool cont;
4,308✔
142

143
        assert(s);
4,308✔
144
        assert(*s);
4,308✔
145
        assert(IN_SET(len, 1, 2));
4,308✔
146
        assert(buf);
4,308✔
147

148
        p = *s;
149

150
        for (size_t i = 0; i < len * 2; i++) {
12,982✔
151
                const char *hexoff;
8,750✔
152
                size_t x;
8,750✔
153

154
                if (*p == '\0' || *p == sep) {
8,750✔
155
                        if (i == 0)
69✔
156
                                return -EINVAL;
157
                        break;
158
                }
159

160
                hexoff = strchr(hex, *p);
8,681✔
161
                if (!hexoff)
8,681✔
162
                        return -EINVAL;
163

164
                assert(hexoff >= hex);
8,674✔
165
                x = hexoff - hex;
8,674✔
166
                if (x >= 16)
8,674✔
167
                        x -= 6; /* A-F */
70✔
168

169
                assert(x < 16);
70✔
170
                data <<= 4;
8,674✔
171
                data += x;
8,674✔
172

173
                p++;
8,674✔
174
        }
175

176
        if (*p != '\0' && *p != sep)
4,292✔
177
                return -EINVAL;
178

179
        switch (len) {
4,276✔
180
        case 1:
4,183✔
181
                buf[0] = data;
4,183✔
182
                break;
4,183✔
183
        case 2:
93✔
184
                buf[0] = (data & 0xff00) >> 8;
93✔
185
                buf[1] = data & 0xff;
93✔
186
                break;
93✔
187
        default:
×
188
                assert_not_reached();
×
189
        }
190

191
        cont = *p == sep;
4,276✔
192
        *s = p + cont;
4,276✔
193
        return cont;
4,276✔
194
}
195

196
int parse_hw_addr_full(const char *s, size_t expected_len, struct hw_addr_data *ret) {
795✔
197
        size_t field_size, max_len, len = 0;
795✔
198
        uint8_t bytes[HW_ADDR_MAX_SIZE];
795✔
199
        char sep;
795✔
200
        int r;
795✔
201

202
        assert(s);
795✔
203
        assert(expected_len <= HW_ADDR_MAX_SIZE || expected_len == SIZE_MAX);
795✔
204
        assert(ret);
795✔
205

206
        /* This accepts the following formats:
207
         *
208
         * Dot separated 2 bytes format: xxyy.zzaa.bbcc
209
         * Colon separated 1 bytes format: xx:yy:zz:aa:bb:cc
210
         * Hyphen separated 1 bytes format: xx-yy-zz-aa-bb-cc
211
         *
212
         * Moreover, if expected_len == 0, 4, or 16, this also accepts:
213
         *
214
         * IPv4 format: used by IPv4 tunnel, e.g. ipgre
215
         * IPv6 format: used by IPv6 tunnel, e.g. ip6gre
216
         *
217
         * The expected_len argument controls the length of acceptable addresses:
218
         *
219
         * 0: accepts 4 (AF_INET), 16 (AF_INET6), 6 (ETH_ALEN), or 20 (INFINIBAND_ALEN).
220
         * SIZE_MAX: accepts arbitrary length, but at least one separator must be included.
221
         * Otherwise: accepts addresses with matching length.
222
         */
223

224
        if (IN_SET(expected_len, 0, sizeof(struct in_addr), sizeof(struct in6_addr))) {
795✔
225
                union in_addr_union a;
103✔
226
                int family;
103✔
227

228
                if (expected_len == 0)
103✔
229
                        r = in_addr_from_string_auto(s, &family, &a);
90✔
230
                else {
231
                        family = expected_len == sizeof(struct in_addr) ? AF_INET : AF_INET6;
13✔
232
                        r = in_addr_from_string(family, s, &a);
13✔
233
                }
234
                if (r >= 0) {
103✔
235
                        ret->length = FAMILY_ADDRESS_SIZE(family);
30✔
236
                        memcpy(ret->bytes, a.bytes, ret->length);
30✔
237
                        return 0;
30✔
238
                }
239
        }
240

241
        max_len =
28✔
242
                expected_len == 0 ? INFINIBAND_ALEN :
765✔
243
                expected_len == SIZE_MAX ? HW_ADDR_MAX_SIZE : expected_len;
694✔
244
        sep = s[strspn(s, HEXDIGITS)];
765✔
245

246
        if (sep == '.')
765✔
247
                field_size = 2;
248
        else if (IN_SET(sep, ':', '-'))
727✔
249
                field_size = 1;
250
        else
251
                return -EINVAL;
252

253
        if (max_len % field_size != 0)
754✔
254
                return -EINVAL;
255

256
        for (size_t i = 0; i < max_len / field_size; i++) {
4,316✔
257
                r = parse_hw_addr_one_field(&s, sep, field_size, bytes + i * field_size);
4,308✔
258
                if (r < 0)
4,308✔
259
                        return r;
260
                if (r == 0) {
4,276✔
261
                        len = (i + 1) * field_size;
714✔
262
                        break;
714✔
263
                }
264
        }
265

266
        if (len == 0)
722✔
267
                return -EINVAL;
268

269
        if (expected_len == 0) {
714✔
270
                if (!IN_SET(len, 4, 16, ETH_ALEN, INFINIBAND_ALEN))
68✔
271
                        return -EINVAL;
272
        } else if (expected_len != SIZE_MAX) {
646✔
273
                if (len != expected_len)
633✔
274
                        return -EINVAL;
275
        }
276

277
        ret->length = len;
702✔
278
        memcpy(ret->bytes, bytes, ret->length);
702✔
279
        return 0;
702✔
280
}
281

282
int parse_ether_addr(const char *s, struct ether_addr *ret) {
130✔
283
        struct hw_addr_data a;
130✔
284
        int r;
130✔
285

286
        assert(s);
130✔
287
        assert(ret);
130✔
288

289
        r = parse_hw_addr_full(s, ETH_ALEN, &a);
130✔
290
        if (r < 0)
130✔
291
                return r;
130✔
292

293
        *ret = a.ether;
91✔
294
        return 0;
91✔
295
}
296

297
void ether_addr_mark_random(struct ether_addr *addr) {
90✔
298
        assert(addr);
90✔
299

300
        /* see eth_random_addr in the kernel */
301
        addr->ether_addr_octet[0] &= 0xfe;        /* clear multicast bit */
90✔
302
        addr->ether_addr_octet[0] |= 0x02;        /* set local assignment bit (IEEE802) */
90✔
303
}
90✔
304

305
int hw_addr_ensure_broadcast(struct hw_addr_data *bcast_addr, uint16_t arp_type) {
102✔
306
        assert(bcast_addr);
102✔
307

308
        if (!hw_addr_is_null(bcast_addr))
102✔
309
                return 0;
310

UNCOV
311
        switch (arp_type) {
×
UNCOV
312
        case ARPHRD_ETHER:
×
UNCOV
313
                *bcast_addr = (struct hw_addr_data) {
×
314
                        .length = ETH_ALEN,
315
                        .ether = {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }},
316
                };
UNCOV
317
                return 0;
×
UNCOV
318
        case ARPHRD_INFINIBAND:
×
UNCOV
319
                *bcast_addr = (struct hw_addr_data) {
×
320
                        .length = INFINIBAND_ALEN,
321
                        .infiniband = {
322
                                0x00, 0xff, 0xff, 0xff, 0xff, 0x12, 0x40, 0x1b,
323
                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
324
                                0xff, 0xff, 0xff, 0xff,
325
                        },
326
                };
UNCOV
327
                return 0;
×
328
        default:
329
                return -EAFNOSUPPORT;
330
        }
331
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc