]> git.ipfire.org Git - people/ms/network.git/blob - src/networkd/zone-bus.c
Makefile: Fix typo in localstatedir
[people/ms/network.git] / src / networkd / zone-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 <errno.h>
22
23 #include "bus.h"
24 #include "daemon.h"
25 #include "logging.h"
26 #include "zone.h"
27 #include "zone-bus.h"
28 #include "zones.h"
29
30 static int nw_zone_node_enumerator(sd_bus* bus, const char* path, void* data,
31 char*** nodes, sd_bus_error* error) {
32 int r;
33
34 DEBUG("Enumerating zones...\n");
35
36 // Fetch a reference to the daemon
37 nw_daemon* daemon = (nw_daemon*)data;
38
39 // Fetch zones
40 nw_zones* zones = nw_daemon_zones(daemon);
41
42 // Make bus paths for all zones
43 r = nw_zones_bus_paths(zones, nodes);
44 if (r)
45 goto ERROR;
46
47 ERROR:
48 nw_zones_unref(zones);
49
50 return r;
51 }
52
53 static int nw_zone_object_find(sd_bus* bus, const char* path, const char* interface,
54 void* data, void** found, sd_bus_error* error) {
55 char* name = NULL;
56 int r;
57
58 // Fetch a reference to the daemon
59 nw_daemon* daemon = (nw_daemon*)data;
60
61 // Decode the path of the requested object
62 r = sd_bus_path_decode(path, "/org/ipfire/network1/zone", &name);
63 if (r <= 0)
64 return 0;
65
66 // Find the zone
67 nw_zone* zone = nw_daemon_get_zone_by_name(daemon, name);
68 if (!zone)
69 return 0;
70
71 // Match!
72 *found = zone;
73
74 nw_zone_unref(zone);
75
76 return 1;
77 }
78
79 /*
80 MTU
81 */
82 static int nw_zone_bus_get_mtu(sd_bus* bus, const char *path, const char *interface,
83 const char* property, sd_bus_message* reply, void* data, sd_bus_error *error) {
84 nw_zone* zone = (nw_zone*)data;
85
86 return sd_bus_message_append(reply, "u", nw_zone_mtu(zone));
87 }
88
89 static int nw_zone_bus_set_mtu(sd_bus* bus, const char* path, const char* interface,
90 const char* property, sd_bus_message* value, void* data, sd_bus_error* error) {
91 unsigned int mtu = 0;
92 int r;
93
94 nw_zone* zone = (nw_zone*)data;
95
96 // Parse the value
97 r = sd_bus_message_read(value, "u", &mtu);
98 if (r < 0)
99 return r;
100
101 if (!nw_zone_set_mtu(zone, mtu))
102 return -errno;
103
104 return 0;
105 }
106
107 static const sd_bus_vtable zone_vtable[] = {
108 SD_BUS_VTABLE_START(0),
109
110 // MTU
111 SD_BUS_WRITABLE_PROPERTY("MTU", "u", nw_zone_bus_get_mtu, nw_zone_bus_set_mtu,
112 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
113
114 SD_BUS_VTABLE_END
115 };
116
117 const nw_bus_implementation zone_bus_impl = {
118 "/org/ipfire/network1/zone",
119 "org.ipfire.network1.Zone",
120 .fallback_vtables = BUS_FALLBACK_VTABLES({zone_vtable, nw_zone_object_find}),
121 .node_enumerator = nw_zone_node_enumerator,
122 };