]> git.ipfire.org Git - people/ms/network.git/blob - src/networkctl/zone.c
Makefile: Fix typo in localstatedir
[people/ms/network.git] / src / networkctl / zone.c
1 /*#############################################################################
2 # #
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #include <systemd/sd-bus.h>
22
23 #include "command.h"
24 #include "zone.h"
25
26 typedef int (*networkctl_zone_walk_callback)
27 (sd_bus* bus, const char* path, const char* name, void* data);
28
29 static int networkctl_zone_walk(sd_bus* bus,
30 networkctl_zone_walk_callback callback, void* data) {
31 sd_bus_message* reply = NULL;
32 sd_bus_error error = SD_BUS_ERROR_NULL;
33 int r;
34
35 // Call ListZones
36 r = sd_bus_call_method(bus, "org.ipfire.network1", "/org/ipfire/network1",
37 "org.ipfire.network1", "ListZones", &error, &reply, "");
38 if (r < 0) {
39 fprintf(stderr, "ListZones call failed: %m\n");
40 goto ERROR;
41 }
42
43 const char* name = NULL;
44 const char* path = NULL;
45
46 // Open the container
47 r = sd_bus_message_enter_container(reply, 'a', "(so)");
48 if (r < 0) {
49 fprintf(stderr, "Could not open container: %m\n");
50 goto ERROR;
51 }
52
53 // Iterate over all zones
54 for (;;) {
55 r = sd_bus_message_read(reply, "(so)", &name, &path);
56 if (r < 0)
57 goto ERROR;
58
59 // Break if we reached the end of the container
60 if (r == 0)
61 break;
62
63 // Call the callback
64 r = callback(bus, path, name, data);
65 if (r)
66 goto ERROR;
67 }
68
69 // Close the container
70 sd_bus_message_exit_container(reply);
71
72 ERROR:
73 if (reply)
74 sd_bus_message_unref(reply);
75 sd_bus_error_free(&error);
76
77 return r;
78 }
79
80 static int __networkctl_zone_list(sd_bus* bus, const char* path, const char* name, void* data) {
81 printf("%s\n", name);
82
83 return 0;
84 }
85
86 static int networkctl_zone_list(sd_bus* bus, int argc, char* argv[]) {
87 return networkctl_zone_walk(bus, __networkctl_zone_list, NULL);
88 }
89
90 int networkctl_zone(sd_bus* bus, int argc, char* argv[]) {
91 static const struct command commands[] = {
92 { "list", 0, networkctl_zone_list },
93 { NULL },
94 };
95
96 return command_dispatch(bus, commands, argc, argv);
97 }