]> git.ipfire.org Git - people/ms/network.git/blob - src/networkd/zone.c
networkd: Save configuration when the daemon exits
[people/ms/network.git] / src / networkd / 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 <limits.h>
22 #include <stdlib.h>
23
24 #include <systemd/sd-bus.h>
25
26 #include "config.h"
27 #include "daemon.h"
28 #include "logging.h"
29 #include "string.h"
30 #include "zone.h"
31
32 struct nw_zone {
33 nw_daemon* daemon;
34 int nrefs;
35
36 char name[NETWORK_ZONE_NAME_MAX_LENGTH];
37
38 // Configuration
39 nw_config *config;
40 };
41
42 #define nw_zone_path(zone, path, format, ...) \
43 __nw_zone_path(zone, path, sizeof(path), format, __VA_ARGS__)
44
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];
49 va_list args;
50 int r;
51
52 // Format the prefix
53 r = nw_string_format(prefix, "%s/zones/%s", CONFIG_DIR, zone->name);
54 if (r)
55 return r;
56
57 // Format the suffix
58 va_start(args, format);
59 r = nw_string_vformat(suffix, format, args);
60 va_end(args);
61 if (r)
62 return r;
63
64 // Join the two parts together
65 return __nw_path_join(p, length, prefix, suffix);
66 }
67
68 static void nw_zone_free(nw_zone* zone) {
69 if (zone->config)
70 nw_config_unref(zone->config);
71 if (zone->daemon)
72 nw_daemon_unref(zone->daemon);
73
74 free(zone);
75 }
76
77 static int nw_zone_setup(nw_zone* zone) {
78 char path[PATH_MAX];
79 int r;
80
81 // Compose the path to the main configuration file
82 r = nw_zone_path(zone, path, "%s", "settings");
83 if (r)
84 return r;
85
86 // Initialize the configuration
87 r = nw_config_create(&zone->config, path);
88 if (r)
89 return r;
90
91 return 0;
92 }
93
94 int nw_zone_create(nw_zone** zone, nw_daemon* daemon, const char* name) {
95 int r;
96
97 // Allocate a new object
98 nw_zone* z = calloc(1, sizeof(*z));
99 if (!z)
100 return 1;
101
102 // Store a reference to the daemon
103 z->daemon = nw_daemon_ref(daemon);
104
105 // Initialize reference counter
106 z->nrefs = 1;
107
108 // Store the name
109 r = nw_string_set(z->name, name);
110 if (r)
111 goto ERROR;
112
113 // Setup the zone
114 r = nw_zone_setup(z);
115 if (r)
116 goto ERROR;
117
118 *zone = z;
119 return 0;
120
121 ERROR:
122 nw_zone_free(z);
123 return r;
124 }
125
126 nw_zone* nw_zone_ref(nw_zone* zone) {
127 zone->nrefs++;
128
129 return zone;
130 }
131
132 nw_zone* nw_zone_unref(nw_zone* zone) {
133 if (--zone->nrefs > 0)
134 return zone;
135
136 nw_zone_free(zone);
137 return NULL;
138 }
139
140 int nw_zone_save(nw_zone* zone) {
141 int r;
142
143 r = nw_config_write(zone->config);
144 if (r)
145 return r;
146
147 return 0;
148 }
149
150 const char* nw_zone_name(nw_zone* zone) {
151 return zone->name;
152 }
153
154 char* nw_zone_bus_path(nw_zone* zone) {
155 char* p = NULL;
156 int r;
157
158 // Encode the bus path
159 r = sd_bus_path_encode("/org/ipfire/network1/zone", zone->name, &p);
160 if (r < 0)
161 return NULL;
162
163 return p;
164 }
165
166 /*
167 MTU
168 */
169 unsigned int nw_zone_mtu(nw_zone* zone) {
170 return nw_config_get_int(zone->config, "MTU", NETWORK_ZONE_DEFAULT_MTU);
171 }
172
173 int nw_zone_set_mtu(nw_zone* zone, unsigned int mtu) {
174 DEBUG("Change MTU of %s to %u\n", zone->name, mtu);
175
176 return nw_config_set_int(zone->config, "MTU", mtu);
177 }