1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
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. #
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. #
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/>. #
19 #############################################################################*/
23 #include <systemd/sd-bus.h>
32 static int nw_daemon_bus_reload(sd_bus_message
* m
, void* data
, sd_bus_error
* error
) {
33 nw_daemon
* daemon
= (nw_daemon
*)data
;
36 nw_daemon_reload(daemon
);
38 // Respond with an empty message
39 return sd_bus_reply_method_return(m
, NULL
);
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
;
47 const char* name
= nw_port_name(port
);
50 char* path
= nw_port_bus_path(port
);
52 // Append the port to the message
53 r
= sd_bus_message_append(reply
, "(so)", name
, path
);
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
;
72 // Form a reply message
73 r
= sd_bus_message_new_method_return(m
, &reply
);
77 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
81 r
= nw_daemon_ports_walk(daemon
, __nw_daemon_bus_list_ports
, reply
);
85 r
= sd_bus_message_close_container(reply
);
90 r
= sd_bus_send(NULL
, reply
, NULL
);
94 sd_bus_message_unref(reply
);
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
;
104 const char* name
= nw_zone_name(zone
);
107 char* path
= nw_zone_bus_path(zone
);
109 // Append the zone to the message
110 r
= sd_bus_message_append(reply
, "(so)", name
, path
);
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
;
129 // Form a reply message
130 r
= sd_bus_message_new_method_return(m
, &reply
);
134 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
138 r
= nw_daemon_zones_walk(daemon
, __nw_daemon_bus_list_zones
, reply
);
142 r
= sd_bus_message_close_container(reply
);
147 r
= sd_bus_send(NULL
, reply
, NULL
);
151 sd_bus_message_unref(reply
);
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
),
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
),