]> git.ipfire.org Git - people/ms/network.git/blame - src/networkd/zone.c
networkd: Save configuration when the daemon exits
[people/ms/network.git] / src / networkd / zone.c
CommitLineData
e4eebc6e
MT
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
00b5de56 21#include <limits.h>
e4eebc6e
MT
22#include <stdlib.h>
23
ebc65f19
MT
24#include <systemd/sd-bus.h>
25
e4eebc6e 26#include "config.h"
dd84704e 27#include "daemon.h"
9368a163 28#include "logging.h"
e4eebc6e
MT
29#include "string.h"
30#include "zone.h"
31
32struct nw_zone {
dd84704e 33 nw_daemon* daemon;
e4eebc6e
MT
34 int nrefs;
35
36 char name[NETWORK_ZONE_NAME_MAX_LENGTH];
37
38 // Configuration
2361667e 39 nw_config *config;
e4eebc6e
MT
40};
41
00b5de56
MT
42#define nw_zone_path(zone, path, format, ...) \
43 __nw_zone_path(zone, path, sizeof(path), format, __VA_ARGS__)
44
2361667e 45static int __nw_zone_path(nw_zone* zone, char* p, const size_t length,
00b5de56
MT
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
2361667e 68static void nw_zone_free(nw_zone* zone) {
e4eebc6e
MT
69 if (zone->config)
70 nw_config_unref(zone->config);
dd84704e
MT
71 if (zone->daemon)
72 nw_daemon_unref(zone->daemon);
e4eebc6e
MT
73
74 free(zone);
75}
76
2361667e 77static int nw_zone_setup(nw_zone* zone) {
00b5de56
MT
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
dd84704e 94int nw_zone_create(nw_zone** zone, nw_daemon* daemon, const char* name) {
e4eebc6e
MT
95 int r;
96
97 // Allocate a new object
2361667e 98 nw_zone* z = calloc(1, sizeof(*z));
e4eebc6e
MT
99 if (!z)
100 return 1;
101
dd84704e
MT
102 // Store a reference to the daemon
103 z->daemon = nw_daemon_ref(daemon);
104
e4eebc6e
MT
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
00b5de56
MT
113 // Setup the zone
114 r = nw_zone_setup(z);
115 if (r)
116 goto ERROR;
117
e4eebc6e
MT
118 *zone = z;
119 return 0;
120
121ERROR:
122 nw_zone_free(z);
123 return r;
124}
125
2361667e 126nw_zone* nw_zone_ref(nw_zone* zone) {
e4eebc6e
MT
127 zone->nrefs++;
128
129 return zone;
130}
131
2361667e 132nw_zone* nw_zone_unref(nw_zone* zone) {
e4eebc6e
MT
133 if (--zone->nrefs > 0)
134 return zone;
135
136 nw_zone_free(zone);
137 return NULL;
138}
139
605e975f
MT
140int 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
2361667e 150const char* nw_zone_name(nw_zone* zone) {
e4eebc6e
MT
151 return zone->name;
152}
ebc65f19 153
2361667e 154char* nw_zone_bus_path(nw_zone* zone) {
ebc65f19
MT
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}
9368a163
MT
165
166/*
167 MTU
168*/
2361667e 169unsigned int nw_zone_mtu(nw_zone* zone) {
9368a163
MT
170 return nw_config_get_int(zone->config, "MTU", NETWORK_ZONE_DEFAULT_MTU);
171}
172
2361667e 173int nw_zone_set_mtu(nw_zone* zone, unsigned int mtu) {
9368a163
MT
174 DEBUG("Change MTU of %s to %u\n", zone->name, mtu);
175
176 return nw_config_set_int(zone->config, "MTU", mtu);
177}