From 2e7f4705a61e8435e22d49fcff49ecf8dde389ef Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 11 Feb 2023 11:48:23 +0000 Subject: [PATCH] networkd: Read Ethernet address from configuration Signed-off-by: Michael Tremer --- Makefile.am | 1 + src/networkd/address.h | 47 ++++++++++++++++++++++++++++++++++++++++++ src/networkd/port.c | 36 +++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/networkd/address.h diff --git a/Makefile.am b/Makefile.am index 505d6791..a27736a7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -308,6 +308,7 @@ sbin_PROGRAMS += \ networkd dist_networkd_SOURCES = \ + src/networkd/address.h \ src/networkd/bus.c \ src/networkd/bus.h \ src/networkd/config.c \ diff --git a/src/networkd/address.h b/src/networkd/address.h new file mode 100644 index 00000000..1d9fbfc9 --- /dev/null +++ b/src/networkd/address.h @@ -0,0 +1,47 @@ +/*############################################################################# +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2023 IPFire Network Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef NETWORKD_ADDRESS_H +#define NETWORKD_ADDRESS_H + +#include +#include + +typedef struct ether_addr nw_address_t; + +static inline int nw_address_from_string(nw_address_t* addr, const char* s) { + struct ether_addr* p = ether_aton_r(s, addr); + if (!p) + return 1; + + return 0; +} + +static inline char* nw_address_to_string(const nw_address_t* addr) { + char buffer[ETH_ALEN]; + + char* p = ether_ntoa_r(addr, buffer); + if (!p) + return NULL; + + return strdup(buffer); +} + +#endif /* NETWORKD_ADDRESS_H */ diff --git a/src/networkd/port.c b/src/networkd/port.c index f78489b0..aff79a53 100644 --- a/src/networkd/port.c +++ b/src/networkd/port.c @@ -20,8 +20,10 @@ #include #include +#include #include +#include "address.h" #include "config.h" #include "logging.h" #include "string.h" @@ -35,6 +37,9 @@ struct nw_port { // Configuration struct nw_config *config; + + // Common attributes + nw_address_t address; }; static const struct nw_port_type_map { @@ -63,8 +68,37 @@ static void nw_port_free(struct nw_port* port) { free(port); } +static int nw_port_setup_address(struct nw_port* port) { + int r; + + // Read ADDRESS from configuration + const char* s = nw_config_get(port->config, "ADDRESS"); + if (!s) { + ERROR("Port %s: Address isn't set\n", port->name); + return 1; + } + + // Parse the address + r = nw_address_from_string(&port->address, s); + if (r) { + ERROR("Port %s: Could not parse address: %m\n", port->name); + return r; + } + + // XXX Do we need to check for multicast here? + + return 0; +} + static int nw_port_setup_common(struct nw_port* port) { - return 0; // XXX TODO + int r; + + // Address + r = nw_port_setup_address(port); + if (r) + return r; + + return 0; } static nw_port_type_t nw_port_setup_type(struct nw_port* port) { -- 2.47.2