]> git.ipfire.org Git - network.git/commitdiff
networkd: Add scaffolding for zones
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 30 Jan 2023 03:42:57 +0000 (03:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 30 Jan 2023 03:44:51 +0000 (03:44 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/networkd/config.h
src/networkd/string.h
src/networkd/zone.c [new file with mode: 0644]
src/networkd/zone.h [new file with mode: 0644]

index 3cc7ce940ab3a723b9370d4fef7841f68cd48b0c..5bb9c4d00d690bddf0d4b347b8ba5c33239bfe76 100644 (file)
@@ -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) \
index 90e73f4b9ceb2333cc123451091e91288bb008d0..3474ae13802b365a162e10327fe54ac502c9d6e9 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef NETWORKD_CONFIG_H
 #define NETWORKD_CONFIG_H
 
+#include <stdio.h>
+
 #define NETWORK_CONFIG_KEY_MAX_LENGTH          128
 #define NETWORK_CONFIG_VALUE_MAX_LENGTH                2048
 
index 3ac4846dfb86f77b8104ef686f72c2871cf3d695..52b5add16c605b6dd6ce519bf3dc0227dbc1bed3 100644 (file)
 #ifndef NETWORKD_STRING_H
 #define NETWORKD_STRING_H
 
+#include <errno.h>
 #include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
 
 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 (file)
index 0000000..5066c4b
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <stdlib.h>
+
+#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 (file)
index 0000000..78287ab
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#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 */