]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
5fde13d7 | 2 | |
f5284182 | 3 | #include <arpa/inet.h> |
5cdf13c7 | 4 | #include <stdio.h> |
07630cea | 5 | |
b5efdb8a | 6 | #include "alloc-util.h" |
e1ea665e | 7 | #include "dhcp-lease-internal.h" |
5cdf13c7 | 8 | #include "dns-resolver-internal.h" |
dc7f6c9b | 9 | #include "extract-word.h" |
cf0fbc49 | 10 | #include "hexdecoct.h" |
9bcbb614 | 11 | #include "in-addr-util.h" |
6bedfcbb LP |
12 | #include "network-internal.h" |
13 | #include "parse-util.h" | |
5cdf13c7 | 14 | #include "string-util.h" |
b0e71631 | 15 | #include "strv.h" |
f00ff0de | 16 | |
072320ea TH |
17 | size_t serialize_in_addrs(FILE *f, |
18 | const struct in_addr *addresses, | |
19 | size_t size, | |
d8bff5cc | 20 | bool *with_leading_space, |
072320ea | 21 | bool (*predicate)(const struct in_addr *addr)) { |
09bee74d | 22 | assert(f); |
09bee74d | 23 | assert(addresses); |
09bee74d | 24 | |
dddc8d1e | 25 | size_t count = 0; |
d8bff5cc ZJS |
26 | bool _space = false; |
27 | if (!with_leading_space) | |
28 | with_leading_space = &_space; | |
072320ea | 29 | |
dddc8d1e | 30 | for (size_t i = 0; i < size; i++) { |
072320ea TH |
31 | if (predicate && !predicate(&addresses[i])) |
32 | continue; | |
d8bff5cc ZJS |
33 | |
34 | if (*with_leading_space) | |
072320ea | 35 | fputc(' ', f); |
071e522e | 36 | fputs(IN4_ADDR_TO_STRING(&addresses[i]), f); |
072320ea | 37 | count++; |
d8bff5cc | 38 | *with_leading_space = true; |
072320ea TH |
39 | } |
40 | ||
41 | return count; | |
09bee74d TG |
42 | } |
43 | ||
a2ba62c7 | 44 | int deserialize_in_addrs(struct in_addr **ret, const char *string) { |
09bee74d | 45 | _cleanup_free_ struct in_addr *addresses = NULL; |
a2ba62c7 | 46 | int size = 0; |
09bee74d TG |
47 | |
48 | assert(ret); | |
09bee74d TG |
49 | assert(string); |
50 | ||
93e28226 SS |
51 | for (;;) { |
52 | _cleanup_free_ char *word = NULL; | |
223a67e5 | 53 | union in_addr_union a; |
09bee74d TG |
54 | int r; |
55 | ||
93e28226 SS |
56 | r = extract_first_word(&string, &word, NULL, 0); |
57 | if (r < 0) | |
58 | return r; | |
59 | if (r == 0) | |
60 | break; | |
61 | ||
223a67e5 | 62 | if (in_addr_from_string(AF_INET, word, &a) < 0) |
09bee74d TG |
63 | continue; |
64 | ||
223a67e5 YW |
65 | if (!GREEDY_REALLOC(addresses, size + 1)) |
66 | return -ENOMEM; | |
67 | ||
68 | addresses[size++] = a.in; | |
09bee74d TG |
69 | } |
70 | ||
c24b6821 | 71 | *ret = size > 0 ? TAKE_PTR(addresses) : NULL; |
09bee74d | 72 | |
a2ba62c7 | 73 | return size; |
09bee74d TG |
74 | } |
75 | ||
d8bff5cc | 76 | void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size, bool *with_leading_space) { |
b729fa14 PF |
77 | assert(f); |
78 | assert(addresses); | |
79 | assert(size); | |
80 | ||
d8bff5cc ZJS |
81 | bool _space = false; |
82 | if (!with_leading_space) | |
83 | with_leading_space = &_space; | |
84 | ||
dddc8d1e | 85 | for (size_t i = 0; i < size; i++) { |
d8bff5cc | 86 | if (*with_leading_space) |
1f152e4b | 87 | fputc(' ', f); |
071e522e | 88 | fputs(IN6_ADDR_TO_STRING(&addresses[i]), f); |
d8bff5cc | 89 | *with_leading_space = true; |
1f152e4b | 90 | } |
b729fa14 PF |
91 | } |
92 | ||
a2ba62c7 | 93 | int deserialize_in6_addrs(struct in6_addr **ret, const char *string) { |
09bee74d | 94 | _cleanup_free_ struct in6_addr *addresses = NULL; |
a2ba62c7 | 95 | int size = 0; |
09bee74d TG |
96 | |
97 | assert(ret); | |
09bee74d TG |
98 | assert(string); |
99 | ||
93e28226 SS |
100 | for (;;) { |
101 | _cleanup_free_ char *word = NULL; | |
223a67e5 | 102 | union in_addr_union a; |
09bee74d TG |
103 | int r; |
104 | ||
93e28226 SS |
105 | r = extract_first_word(&string, &word, NULL, 0); |
106 | if (r < 0) | |
107 | return r; | |
108 | if (r == 0) | |
109 | break; | |
110 | ||
223a67e5 | 111 | if (in_addr_from_string(AF_INET6, word, &a) < 0) |
09bee74d TG |
112 | continue; |
113 | ||
223a67e5 YW |
114 | if (!GREEDY_REALLOC(addresses, size + 1)) |
115 | return -ENOMEM; | |
116 | ||
117 | addresses[size++] = a.in6; | |
09bee74d TG |
118 | } |
119 | ||
ae2a15bc | 120 | *ret = TAKE_PTR(addresses); |
09bee74d | 121 | |
a2ba62c7 | 122 | return size; |
09bee74d | 123 | } |
e1ea665e | 124 | |
b0e71631 RP |
125 | int 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 | ||
143 | static 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. */ | |
164 | int 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 | ||
f8693fc7 | 218 | void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) { |
e1ea665e EY |
219 | assert(f); |
220 | assert(key); | |
221 | assert(routes); | |
222 | assert(size); | |
223 | ||
224 | fprintf(f, "%s=", key); | |
225 | ||
dddc8d1e | 226 | for (size_t i = 0; i < size; i++) { |
f8693fc7 BG |
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 | ||
071e522e ZJS |
234 | fprintf(f, "%s,%s%s", |
235 | IN4_ADDR_PREFIX_TO_STRING(&dest, length), | |
236 | IN4_ADDR_TO_STRING(&gw), | |
237 | i < size - 1 ? " ": ""); | |
fbf7dcb5 | 238 | } |
e1ea665e EY |
239 | |
240 | fputs("\n", f); | |
241 | } | |
242 | ||
319a4f4b | 243 | int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, const char *string) { |
e1ea665e | 244 | _cleanup_free_ struct sd_dhcp_route *routes = NULL; |
319a4f4b | 245 | size_t size = 0; |
e1ea665e EY |
246 | |
247 | assert(ret); | |
248 | assert(ret_size); | |
e1ea665e EY |
249 | assert(string); |
250 | ||
93e28226 SS |
251 | /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */ |
252 | for (;;) { | |
253 | _cleanup_free_ char *word = NULL; | |
e1ea665e EY |
254 | char *tok, *tok_end; |
255 | unsigned n; | |
256 | int r; | |
257 | ||
93e28226 SS |
258 | r = extract_first_word(&string, &word, NULL, 0); |
259 | if (r < 0) | |
260 | return r; | |
261 | if (r == 0) | |
262 | break; | |
e1ea665e | 263 | |
319a4f4b | 264 | if (!GREEDY_REALLOC(routes, size + 1)) |
31db0120 | 265 | return -ENOMEM; |
e1ea665e | 266 | |
93e28226 | 267 | tok = word; |
e1ea665e EY |
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; | |
ae2a15bc | 304 | *ret = TAKE_PTR(routes); |
e1ea665e EY |
305 | |
306 | return 0; | |
307 | } | |
a073309f | 308 | |
e4735228 | 309 | int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) { |
a073309f AC |
310 | _cleanup_free_ char *hex_buf = NULL; |
311 | ||
312 | assert(f); | |
313 | assert(key); | |
314 | assert(data); | |
315 | ||
316 | hex_buf = hexmem(data, size); | |
4e361acc | 317 | if (!hex_buf) |
a073309f AC |
318 | return -ENOMEM; |
319 | ||
320 | fprintf(f, "%s=%s\n", key, hex_buf); | |
321 | ||
322 | return 0; | |
323 | } |