]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: Read Ethernet address from configuration
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2023 11:48:23 +0000 (11:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2023 11:48:23 +0000 (11:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/networkd/address.h [new file with mode: 0644]
src/networkd/port.c

index 505d6791d8b3f0a00e1cc8277451f7c4d3096714..a27736a714b5b251094b02822ec90a3a1790d543 100644 (file)
@@ -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 (file)
index 0000000..1d9fbfc
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef NETWORKD_ADDRESS_H
+#define NETWORKD_ADDRESS_H
+
+#include <netinet/ether.h>
+#include <string.h>
+
+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 */
index f78489b02d29ce3a950ef15dba802bbead9d6ca1..aff79a532a46bd35cdfdf16200298797539343ae 100644 (file)
 
 #include <limits.h>
 #include <net/if.h>
+#include <stdint.h>
 #include <stdlib.h>
 
+#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) {