1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
21 #ifndef NETWORKD_ADDRESS_H
22 #define NETWORKD_ADDRESS_H
25 #include <netinet/ether.h>
27 #include <sys/random.h>
31 typedef struct ether_addr nw_address_t
;
34 NW_ADDRESS_MULTICAST
= (1 << 0),
35 NW_ADDRESS_SOFTWAREASSIGNED
= (1 << 1),
38 static inline int nw_address_from_string(nw_address_t
* addr
, const char* s
) {
42 struct ether_addr
* p
= ether_aton_r(s
, addr
);
49 static inline char* nw_address_to_string(const nw_address_t
* addr
) {
52 char* p
= ether_ntoa_r(addr
, buffer
);
56 return strdup(buffer
);
59 static inline int nw_address_generate(nw_address_t
* addr
) {
60 ssize_t bytes
= getrandom(addr
, sizeof(*addr
), 0);
62 ERROR("getrandom() failed: %m\n");
66 // Check if we filled the entire buffer
67 if (bytes
< (ssize_t
)sizeof(*addr
)) {
68 ERROR("Could not gather enough randomness\n");
72 // Clear the multicast bit
73 addr
->ether_addr_octet
[0] &= ~NW_ADDRESS_MULTICAST
;
75 // Set the software-generated bit
76 addr
->ether_addr_octet
[0] |= NW_ADDRESS_SOFTWAREASSIGNED
;
81 static inline int nw_address_is_multicast(const nw_address_t
* addr
) {
82 return (addr
->ether_addr_octet
[0] & NW_ADDRESS_MULTICAST
);
85 #endif /* NETWORKD_ADDRESS_H */