]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/ether-addr-util.c
Merge pull request #2701 from keszybz/udev-rules
[thirdparty/systemd.git] / src / basic / ether-addr-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Tom Gundersen
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <net/ethernet.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23
24 #include "ether-addr-util.h"
25 #include "macro.h"
26
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 }
45
46 bool ether_addr_is_null(const struct ether_addr *addr) {
47 assert(addr);
48
49 return addr->ether_addr_octet[0] == 0 &&
50 addr->ether_addr_octet[1] == 0 &&
51 addr->ether_addr_octet[2] == 0 &&
52 addr->ether_addr_octet[3] == 0 &&
53 addr->ether_addr_octet[4] == 0 &&
54 addr->ether_addr_octet[5] == 0;
55 }
56
57 bool ether_addr_equal(const struct ether_addr *a, const struct ether_addr *b) {
58 assert(a);
59 assert(b);
60
61 return a->ether_addr_octet[0] == b->ether_addr_octet[0] &&
62 a->ether_addr_octet[1] == b->ether_addr_octet[1] &&
63 a->ether_addr_octet[2] == b->ether_addr_octet[2] &&
64 a->ether_addr_octet[3] == b->ether_addr_octet[3] &&
65 a->ether_addr_octet[4] == b->ether_addr_octet[4] &&
66 a->ether_addr_octet[5] == b->ether_addr_octet[5];
67 }