]> git.ipfire.org Git - network.git/commitdiff
networkctl: Implement "zone list" command
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Apr 2023 11:26:43 +0000 (11:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Apr 2023 11:26:43 +0000 (11:26 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/networkctl/main.c
src/networkctl/zone.c [new file with mode: 0644]
src/networkctl/zone.h [new file with mode: 0644]

index 186af948736294e3ae349919edbc109d053b1227..80a72365ad6eeb6815e5ce434a1ed1cf06c19b06 100644 (file)
@@ -385,7 +385,9 @@ bin_PROGRAMS += \
 dist_networkctl_SOURCES = \
        src/networkctl/command.c \
        src/networkctl/command.h \
-       src/networkctl/main.c
+       src/networkctl/main.c \
+       src/networkctl/zone.c \
+       src/networkctl/zone.h
 
 networkctl_CFLAGS = \
        $(AM_CFLAGS) \
index 0cbc3e68ccb839a9c99f1d6f2243fe1cdf88fdb5..a08256cec64522bd68dda493e2fe3e465dac5a1c 100644 (file)
@@ -26,6 +26,7 @@
 #include <systemd/sd-bus.h>
 
 #include "command.h"
+#include "zone.h"
 
 static int networkctl_status(sd_bus* bus, int argc, char* argv[]) {
        printf("%s called\n", __FUNCTION__);
@@ -35,6 +36,7 @@ static int networkctl_status(sd_bus* bus, int argc, char* argv[]) {
 static int networkctl_main(sd_bus* bus, int argc, char* argv[]) {
        static const struct command commands[] = {
                { "status", 0, networkctl_status },
+               { "zone",   0, networkctl_zone },
                { NULL },
        };
 
diff --git a/src/networkctl/zone.c b/src/networkctl/zone.c
new file mode 100644 (file)
index 0000000..ee1d0a2
--- /dev/null
@@ -0,0 +1,97 @@
+/*#############################################################################
+#                                                                             #
+# 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 <systemd/sd-bus.h>
+
+#include "command.h"
+#include "zone.h"
+
+typedef int (*networkctl_zone_walk_callback)
+       (sd_bus* bus, const char* path, const char* name, void* data);
+
+static int networkctl_zone_walk(sd_bus* bus,
+               networkctl_zone_walk_callback callback, void* data) {
+       sd_bus_message* reply = NULL;
+       sd_bus_error error = SD_BUS_ERROR_NULL;
+       int r;
+
+       // Call ListZones
+       r = sd_bus_call_method(bus, "org.ipfire.network1", "/org/ipfire/network1",
+               "org.ipfire.network1", "ListZones", &error, &reply, "");
+       if (r < 0) {
+               fprintf(stderr, "ListZones call failed: %m\n");
+               goto ERROR;
+       }
+
+       const char* name = NULL;
+       const char* path = NULL;
+
+       // Open the container
+       r = sd_bus_message_enter_container(reply, 'a', "(so)");
+       if (r < 0) {
+               fprintf(stderr, "Could not open container: %m\n");
+               goto ERROR;
+       }
+
+       // Iterate over all zones
+       for (;;) {
+               r = sd_bus_message_read(reply, "(so)", &name, &path);
+               if (r < 0)
+                       goto ERROR;
+
+               // Break if we reached the end of the container
+               if (r == 0)
+                       break;
+
+               // Call the callback
+               r = callback(bus, path, name, data);
+               if (r)
+                       goto ERROR;
+       }
+
+       // Close the container
+       sd_bus_message_exit_container(reply);
+
+ERROR:
+       if (reply)
+               sd_bus_message_unref(reply);
+       sd_bus_error_free(&error);
+
+       return r;
+}
+
+static int __networkctl_zone_list(sd_bus* bus, const char* path, const char* name, void* data) {
+       printf("%s\n", name);
+
+       return 0;
+}
+
+static int networkctl_zone_list(sd_bus* bus, int argc, char* argv[]) {
+       return networkctl_zone_walk(bus, __networkctl_zone_list, NULL);
+}
+
+int networkctl_zone(sd_bus* bus, int argc, char* argv[]) {
+       static const struct command commands[] = {
+               { "list", 0, networkctl_zone_list },
+               { NULL },
+       };
+
+       return command_dispatch(bus, commands, argc, argv);
+}
diff --git a/src/networkctl/zone.h b/src/networkctl/zone.h
new file mode 100644 (file)
index 0000000..5eddd98
--- /dev/null
@@ -0,0 +1,26 @@
+/*#############################################################################
+#                                                                             #
+# 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 NETWORKCTL_ZONE_H
+#define NETWORKCTL_ZONE_H
+
+int networkctl_zone(sd_bus* bus, int argc, char* argv[]);
+
+#endif /* NETWORKCTL_ZONE_H */