]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/libsystemd-network/network-internal.c
ssh-generator: generate /etc/issue.d/ with VSOCK ssh info data (#37819)
[thirdparty/systemd.git] / src / libsystemd-network / network-internal.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3#include <arpa/inet.h>
4#include <stdio.h>
5
6#include "alloc-util.h"
7#include "dhcp-lease-internal.h"
8#include "dns-resolver-internal.h"
9#include "extract-word.h"
10#include "hexdecoct.h"
11#include "in-addr-util.h"
12#include "network-internal.h"
13#include "parse-util.h"
14#include "string-util.h"
15#include "strv.h"
16
17size_t serialize_in_addrs(FILE *f,
18 const struct in_addr *addresses,
19 size_t size,
20 bool *with_leading_space,
21 bool (*predicate)(const struct in_addr *addr)) {
22 assert(f);
23 assert(addresses);
24
25 size_t count = 0;
26 bool _space = false;
27 if (!with_leading_space)
28 with_leading_space = &_space;
29
30 for (size_t i = 0; i < size; i++) {
31 if (predicate && !predicate(&addresses[i]))
32 continue;
33
34 if (*with_leading_space)
35 fputc(' ', f);
36 fputs(IN4_ADDR_TO_STRING(&addresses[i]), f);
37 count++;
38 *with_leading_space = true;
39 }
40
41 return count;
42}
43
44int deserialize_in_addrs(struct in_addr **ret, const char *string) {
45 _cleanup_free_ struct in_addr *addresses = NULL;
46 int size = 0;
47
48 assert(ret);
49 assert(string);
50
51 for (;;) {
52 _cleanup_free_ char *word = NULL;
53 union in_addr_union a;
54 int r;
55
56 r = extract_first_word(&string, &word, NULL, 0);
57 if (r < 0)
58 return r;
59 if (r == 0)
60 break;
61
62 if (in_addr_from_string(AF_INET, word, &a) < 0)
63 continue;
64
65 if (!GREEDY_REALLOC(addresses, size + 1))
66 return -ENOMEM;
67
68 addresses[size++] = a.in;
69 }
70
71 *ret = size > 0 ? TAKE_PTR(addresses) : NULL;
72
73 return size;
74}
75
76void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size, bool *with_leading_space) {
77 assert(f);
78 assert(addresses);
79 assert(size);
80
81 bool _space = false;
82 if (!with_leading_space)
83 with_leading_space = &_space;
84
85 for (size_t i = 0; i < size; i++) {
86 if (*with_leading_space)
87 fputc(' ', f);
88 fputs(IN6_ADDR_TO_STRING(&addresses[i]), f);
89 *with_leading_space = true;
90 }
91}
92
93int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
94 _cleanup_free_ struct in6_addr *addresses = NULL;
95 int size = 0;
96
97 assert(ret);
98 assert(string);
99
100 for (;;) {
101 _cleanup_free_ char *word = NULL;
102 union in_addr_union a;
103 int r;
104
105 r = extract_first_word(&string, &word, NULL, 0);
106 if (r < 0)
107 return r;
108 if (r == 0)
109 break;
110
111 if (in_addr_from_string(AF_INET6, word, &a) < 0)
112 continue;
113
114 if (!GREEDY_REALLOC(addresses, size + 1))
115 return -ENOMEM;
116
117 addresses[size++] = a.in6;
118 }
119
120 *ret = TAKE_PTR(addresses);
121
122 return size;
123}
124
125int serialize_dnr(FILE *f, const sd_dns_resolver *dnr, size_t n_dnr, bool *with_leading_space) {
126 int r;
127
128 bool _space = false;
129 if (!with_leading_space)
130 with_leading_space = &_space;
131
132 int n = 0;
133 _cleanup_strv_free_ char **names = NULL;
134 r = dns_resolvers_to_dot_strv(dnr, n_dnr, &names);
135 if (r < 0)
136 return r;
137 if (r > 0)
138 fputstrv(f, names, NULL, with_leading_space);
139 n += r;
140 return n;
141}
142
143static int coalesce_dnr(sd_dns_resolver *dnr, size_t n_dnr, int family, const char *auth_name,
144 union in_addr_union *addr) {
145 assert(dnr || n_dnr == 0);
146 assert(auth_name);
147 assert(addr);
148
149 /* Look through list of DNR for matching resolvers to add our addr to. Since DoT is assumed, no need
150 * to compare transports/dohpath/etc. */
151 FOREACH_ARRAY(res, dnr, n_dnr) {
152 if (family == res->family && streq(auth_name, res->auth_name)) {
153 if (!GREEDY_REALLOC(res->addrs, res->n_addrs + 1))
154 return -ENOMEM;
155 res->addrs[res->n_addrs++] = *addr;
156 return true;
157 }
158 }
159
160 return false;
161}
162
163/* Deserialized resolvers are assumed to offer DoT service. */
164int deserialize_dnr(sd_dns_resolver **ret, const char *string) {
165 int r;
166
167 assert(ret);
168 assert(string);
169
170 sd_dns_resolver *dnr = NULL;
171 size_t n = 0;
172 CLEANUP_ARRAY(dnr, n, dns_resolver_done_many);
173 int priority = 0;
174
175 for (;;) {
176 _cleanup_free_ char *word = NULL;
177
178 r = extract_first_word(&string, &word, NULL, 0);
179 if (r < 0)
180 return r;
181 if (r == 0)
182 break;
183
184 uint16_t port;
185 int family;
186 _cleanup_free_ union in_addr_union *addr = new(union in_addr_union, 1);
187 _cleanup_free_ char *auth_name = NULL;
188
189 r = in_addr_port_ifindex_name_from_string_auto(word, &family, addr, &port, NULL, &auth_name);
190 if (r < 0)
191 return r;
192
193 r = coalesce_dnr(dnr, n, family, auth_name, addr);
194 if (r < 0)
195 return r;
196 if (r > 0)
197 continue;
198
199 if (!GREEDY_REALLOC(dnr, n+1))
200 return -ENOMEM;
201
202 priority = n+1;
203 dnr[n++] = (sd_dns_resolver) {
204 .priority = priority, /* not serialized, but this will preserve the order */
205 .auth_name = TAKE_PTR(auth_name),
206 .family = family,
207 .addrs = TAKE_PTR(addr),
208 .n_addrs = 1,
209 .transports = SD_DNS_ALPN_DOT,
210 .port = port,
211 };
212 }
213
214 *ret = TAKE_PTR(dnr);
215 return n;
216}
217
218void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) {
219 assert(f);
220 assert(key);
221 assert(routes);
222 assert(size);
223
224 fprintf(f, "%s=", key);
225
226 for (size_t i = 0; i < size; i++) {
227 struct in_addr dest, gw;
228 uint8_t length;
229
230 assert_se(sd_dhcp_route_get_destination(routes[i], &dest) >= 0);
231 assert_se(sd_dhcp_route_get_gateway(routes[i], &gw) >= 0);
232 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &length) >= 0);
233
234 fprintf(f, "%s,%s%s",
235 IN4_ADDR_PREFIX_TO_STRING(&dest, length),
236 IN4_ADDR_TO_STRING(&gw),
237 i < size - 1 ? " ": "");
238 }
239
240 fputs("\n", f);
241}
242
243int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, const char *string) {
244 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
245 size_t size = 0;
246
247 assert(ret);
248 assert(ret_size);
249 assert(string);
250
251 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
252 for (;;) {
253 _cleanup_free_ char *word = NULL;
254 char *tok, *tok_end;
255 unsigned n;
256 int r;
257
258 r = extract_first_word(&string, &word, NULL, 0);
259 if (r < 0)
260 return r;
261 if (r == 0)
262 break;
263
264 if (!GREEDY_REALLOC(routes, size + 1))
265 return -ENOMEM;
266
267 tok = word;
268
269 /* get the subnet */
270 tok_end = strchr(tok, '/');
271 if (!tok_end)
272 continue;
273 *tok_end = '\0';
274
275 r = inet_aton(tok, &routes[size].dst_addr);
276 if (r == 0)
277 continue;
278
279 tok = tok_end + 1;
280
281 /* get the prefixlen */
282 tok_end = strchr(tok, ',');
283 if (!tok_end)
284 continue;
285
286 *tok_end = '\0';
287
288 r = safe_atou(tok, &n);
289 if (r < 0 || n > 32)
290 continue;
291
292 routes[size].dst_prefixlen = (uint8_t) n;
293 tok = tok_end + 1;
294
295 /* get the gateway */
296 r = inet_aton(tok, &routes[size].gw_addr);
297 if (r == 0)
298 continue;
299
300 size++;
301 }
302
303 *ret_size = size;
304 *ret = TAKE_PTR(routes);
305
306 return 0;
307}
308
309int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) {
310 _cleanup_free_ char *hex_buf = NULL;
311
312 assert(f);
313 assert(key);
314 assert(data);
315
316 hex_buf = hexmem(data, size);
317 if (!hex_buf)
318 return -ENOMEM;
319
320 fprintf(f, "%s=%s\n", key, hex_buf);
321
322 return 0;
323}