]> git.ipfire.org Git - people/ms/network.git/blob - src/networkd/daemon-bus.c
Makefile: Fix typo in localstatedir
[people/ms/network.git] / src / networkd / daemon-bus.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 <stdlib.h>
22
23 #include <systemd/sd-bus.h>
24
25 #include "bus.h"
26 #include "daemon.h"
27 #include "logging.h"
28 #include "port-bus.h"
29 #include "zone-bus.h"
30 #include "zones.h"
31
32 static int nw_daemon_bus_reload(sd_bus_message* m, void* data, sd_bus_error* error) {
33 nw_daemon* daemon = (nw_daemon*)data;
34
35 // Reload the daemon
36 nw_daemon_reload(daemon);
37
38 // Respond with an empty message
39 return sd_bus_reply_method_return(m, NULL);
40 }
41
42 static int __nw_daemon_bus_list_ports(nw_daemon* daemon, nw_port* port, void* data) {
43 sd_bus_message* reply = (sd_bus_message*)data;
44 int r;
45
46 // Fetch port name
47 const char* name = nw_port_name(port);
48
49 // Fetch bus path
50 char* path = nw_port_bus_path(port);
51
52 // Append the port to the message
53 r = sd_bus_message_append(reply, "(so)", name, path);
54 if (r < 0)
55 goto ERROR;
56
57 // Success
58 r = 0;
59
60 ERROR:
61 if (path)
62 free(path);
63
64 return r;
65 }
66
67 static int nw_daemon_bus_list_ports(sd_bus_message* m, void* data, sd_bus_error* error) {
68 nw_daemon* daemon = (nw_daemon*)data;
69 sd_bus_message* reply = NULL;
70 int r;
71
72 // Form a reply message
73 r = sd_bus_message_new_method_return(m, &reply);
74 if (r < 0)
75 goto ERROR;
76
77 r = sd_bus_message_open_container(reply, 'a', "(so)");
78 if (r < 0)
79 goto ERROR;
80
81 r = nw_daemon_ports_walk(daemon, __nw_daemon_bus_list_ports, reply);
82 if (r < 0)
83 goto ERROR;
84
85 r = sd_bus_message_close_container(reply);
86 if (r < 0)
87 goto ERROR;
88
89 // Send the reply
90 r = sd_bus_send(NULL, reply, NULL);
91
92 ERROR:
93 if (reply)
94 sd_bus_message_unref(reply);
95
96 return r;
97 }
98
99 static int __nw_daemon_bus_list_zones(nw_daemon* daemon, nw_zone* zone, void* data) {
100 sd_bus_message* reply = (sd_bus_message*)data;
101 int r;
102
103 // Fetch zone name
104 const char* name = nw_zone_name(zone);
105
106 // Fetch bus path
107 char* path = nw_zone_bus_path(zone);
108
109 // Append the zone to the message
110 r = sd_bus_message_append(reply, "(so)", name, path);
111 if (r < 0)
112 goto ERROR;
113
114 // Success
115 r = 0;
116
117 ERROR:
118 if (path)
119 free(path);
120
121 return r;
122 }
123
124 static int nw_daemon_bus_list_zones(sd_bus_message* m, void* data, sd_bus_error* error) {
125 nw_daemon* daemon = (nw_daemon*)data;
126 sd_bus_message* reply = NULL;
127 int r;
128
129 // Form a reply message
130 r = sd_bus_message_new_method_return(m, &reply);
131 if (r < 0)
132 goto ERROR;
133
134 r = sd_bus_message_open_container(reply, 'a', "(so)");
135 if (r < 0)
136 goto ERROR;
137
138 r = nw_daemon_zones_walk(daemon, __nw_daemon_bus_list_zones, reply);
139 if (r < 0)
140 goto ERROR;
141
142 r = sd_bus_message_close_container(reply);
143 if (r < 0)
144 goto ERROR;
145
146 // Send the reply
147 r = sd_bus_send(NULL, reply, NULL);
148
149 ERROR:
150 if (reply)
151 sd_bus_message_unref(reply);
152
153 return r;
154 }
155
156 static const sd_bus_vtable daemon_vtable[] = {
157 SD_BUS_VTABLE_START(0),
158 SD_BUS_METHOD_WITH_ARGS("ListPorts", SD_BUS_NO_ARGS, SD_BUS_RESULT("a(so)", links),
159 nw_daemon_bus_list_ports, SD_BUS_VTABLE_UNPRIVILEGED),
160 SD_BUS_METHOD_WITH_ARGS("ListZones", SD_BUS_NO_ARGS, SD_BUS_RESULT("a(so)", links),
161 nw_daemon_bus_list_zones, SD_BUS_VTABLE_UNPRIVILEGED),
162 SD_BUS_METHOD("Reload", SD_BUS_NO_ARGS, SD_BUS_NO_RESULT,
163 nw_daemon_bus_reload, SD_BUS_VTABLE_UNPRIVILEGED),
164 SD_BUS_VTABLE_END,
165 };
166
167 const nw_bus_implementation daemon_bus_impl = {
168 .path = "/org/ipfire/network1",
169 .interface = "org.ipfire.network1",
170 .vtables = BUS_VTABLES(daemon_vtable),
171 .children = BUS_IMPLEMENTATIONS(&port_bus_impl, &zone_bus_impl),
172 };