From e4eebc6e6a0091729831fd1361d0a9fd0d205114 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 30 Jan 2023 03:42:57 +0000 Subject: [PATCH] networkd: Add scaffolding for zones Signed-off-by: Michael Tremer --- Makefile.am | 4 ++- src/networkd/config.h | 2 ++ src/networkd/string.h | 3 ++ src/networkd/zone.c | 83 +++++++++++++++++++++++++++++++++++++++++++ src/networkd/zone.h | 35 ++++++++++++++++++ 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 src/networkd/zone.c create mode 100644 src/networkd/zone.h diff --git a/Makefile.am b/Makefile.am index 3cc7ce94..5bb9c4d0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -312,7 +312,9 @@ dist_networkd_SOURCES = \ src/networkd/daemon.h \ src/networkd/logging.h \ src/networkd/main.c \ - src/networkd/string.h + src/networkd/string.h \ + src/networkd/zone.c \ + src/networkd/zone.h networkd_CPPFLAGS = \ $(AM_CPPFLAGS) \ diff --git a/src/networkd/config.h b/src/networkd/config.h index 90e73f4b..3474ae13 100644 --- a/src/networkd/config.h +++ b/src/networkd/config.h @@ -21,6 +21,8 @@ #ifndef NETWORKD_CONFIG_H #define NETWORKD_CONFIG_H +#include + #define NETWORK_CONFIG_KEY_MAX_LENGTH 128 #define NETWORK_CONFIG_VALUE_MAX_LENGTH 2048 diff --git a/src/networkd/string.h b/src/networkd/string.h index 3ac4846d..52b5add1 100644 --- a/src/networkd/string.h +++ b/src/networkd/string.h @@ -21,7 +21,10 @@ #ifndef NETWORKD_STRING_H #define NETWORKD_STRING_H +#include #include +#include +#include static inline int __nw_string_vformat(char* s, const size_t length, const char* format, va_list args) { diff --git a/src/networkd/zone.c b/src/networkd/zone.c new file mode 100644 index 00000000..5066c4b2 --- /dev/null +++ b/src/networkd/zone.c @@ -0,0 +1,83 @@ +/*############################################################################# +# # +# 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 . # +# # +#############################################################################*/ + +#include + +#include "config.h" +#include "string.h" +#include "zone.h" + +struct nw_zone { + int nrefs; + + char name[NETWORK_ZONE_NAME_MAX_LENGTH]; + + // Configuration + struct nw_config *config; +}; + +static void nw_zone_free(struct nw_zone* zone) { + if (zone->config) + nw_config_unref(zone->config); + + free(zone); +} + +int nw_zone_create(struct nw_zone** zone, const char* name) { + int r; + + // Allocate a new object + struct nw_zone* z = calloc(1, sizeof(*z)); + if (!z) + return 1; + + // Initialize reference counter + z->nrefs = 1; + + // Store the name + r = nw_string_set(z->name, name); + if (r) + goto ERROR; + + *zone = z; + return 0; + +ERROR: + nw_zone_free(z); + return r; +} + +struct nw_zone* nw_zone_ref(struct nw_zone* zone) { + zone->nrefs++; + + return zone; +} + +struct nw_zone* nw_zone_unref(struct nw_zone* zone) { + if (--zone->nrefs > 0) + return zone; + + nw_zone_free(zone); + return NULL; +} + +const char* nw_zone_name(struct nw_zone* zone) { + return zone->name; +} diff --git a/src/networkd/zone.h b/src/networkd/zone.h new file mode 100644 index 00000000..78287ab3 --- /dev/null +++ b/src/networkd/zone.h @@ -0,0 +1,35 @@ +/*############################################################################# +# # +# 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_ZONE_H +#define NETWORKD_ZONE_H + +#define NETWORK_ZONE_NAME_MAX_LENGTH 16 + +struct nw_zone; + +int nw_zone_create(struct nw_zone** zone, const char* name); + +struct nw_zone* nw_zone_ref(struct nw_zone* zone); +struct nw_zone* nw_zone_unref(struct nw_zone* zone); + +const char* nw_zone_name(struct nw_zone* zone); + +#endif /* NETWORKD_ZONE_H */ -- 2.47.3