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 #############################################################################*/
24 #include <systemd/sd-bus.h>
36 char name
[NETWORK_ZONE_NAME_MAX_LENGTH
];
42 #define nw_zone_path(zone, path, format, ...) \
43 __nw_zone_path(zone, path, sizeof(path), format, __VA_ARGS__)
45 static int __nw_zone_path(nw_zone
* zone
, char* p
, const size_t length
,
46 const char* format
, ...) {
47 char prefix
[NAME_MAX
];
48 char suffix
[NAME_MAX
];
53 r
= nw_string_format(prefix
, "%s/zones/%s", CONFIG_DIR
, zone
->name
);
58 va_start(args
, format
);
59 r
= nw_string_vformat(suffix
, format
, args
);
64 // Join the two parts together
65 return __nw_path_join(p
, length
, prefix
, suffix
);
68 static void nw_zone_free(nw_zone
* zone
) {
70 nw_config_unref(zone
->config
);
72 nw_daemon_unref(zone
->daemon
);
77 static int nw_zone_setup(nw_zone
* zone
) {
81 // Compose the path to the main configuration file
82 r
= nw_zone_path(zone
, path
, "%s", "settings");
86 // Initialize the configuration
87 r
= nw_config_create(&zone
->config
, path
);
94 int nw_zone_create(nw_zone
** zone
, nw_daemon
* daemon
, const char* name
) {
97 // Allocate a new object
98 nw_zone
* z
= calloc(1, sizeof(*z
));
102 // Store a reference to the daemon
103 z
->daemon
= nw_daemon_ref(daemon
);
105 // Initialize reference counter
109 r
= nw_string_set(z
->name
, name
);
114 r
= nw_zone_setup(z
);
126 nw_zone
* nw_zone_ref(nw_zone
* zone
) {
132 nw_zone
* nw_zone_unref(nw_zone
* zone
) {
133 if (--zone
->nrefs
> 0)
140 int nw_zone_save(nw_zone
* zone
) {
143 r
= nw_config_write(zone
->config
);
150 const char* nw_zone_name(nw_zone
* zone
) {
154 char* nw_zone_bus_path(nw_zone
* zone
) {
158 // Encode the bus path
159 r
= sd_bus_path_encode("/org/ipfire/network1/zone", zone
->name
, &p
);
169 unsigned int nw_zone_mtu(nw_zone
* zone
) {
170 return nw_config_get_int(zone
->config
, "MTU", NETWORK_ZONE_DEFAULT_MTU
);
173 int nw_zone_set_mtu(nw_zone
* zone
, unsigned int mtu
) {
174 DEBUG("Change MTU of %s to %u\n", zone
->name
, mtu
);
176 return nw_config_set_int(zone
->config
, "MTU", mtu
);