]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/network-internal.c
Merge pull request #17478 from yuwata/split-network-internal
[thirdparty/systemd.git] / src / libsystemd-network / network-internal.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <arpa/inet.h>
4 #include <linux/if.h>
5 #include <netinet/ether.h>
6
7 #include "sd-ndisc.h"
8
9 #include "alloc-util.h"
10 #include "dhcp-lease-internal.h"
11 #include "extract-word.h"
12 #include "hexdecoct.h"
13 #include "log.h"
14 #include "network-internal.h"
15 #include "parse-util.h"
16
17 size_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 char sbuf[INET_ADDRSTRLEN];
32
33 if (predicate && !predicate(&addresses[i]))
34 continue;
35
36 if (*with_leading_space)
37 fputc(' ', f);
38 fputs(inet_ntop(AF_INET, &addresses[i], sbuf, sizeof(sbuf)), f);
39 count++;
40 *with_leading_space = true;
41 }
42
43 return count;
44 }
45
46 int deserialize_in_addrs(struct in_addr **ret, const char *string) {
47 _cleanup_free_ struct in_addr *addresses = NULL;
48 int size = 0;
49
50 assert(ret);
51 assert(string);
52
53 for (;;) {
54 _cleanup_free_ char *word = NULL;
55 struct in_addr *new_addresses;
56 int r;
57
58 r = extract_first_word(&string, &word, NULL, 0);
59 if (r < 0)
60 return r;
61 if (r == 0)
62 break;
63
64 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
65 if (!new_addresses)
66 return -ENOMEM;
67 else
68 addresses = new_addresses;
69
70 r = inet_pton(AF_INET, word, &(addresses[size]));
71 if (r <= 0)
72 continue;
73
74 size++;
75 }
76
77 *ret = size > 0 ? TAKE_PTR(addresses) : NULL;
78
79 return size;
80 }
81
82 void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size, bool *with_leading_space) {
83 assert(f);
84 assert(addresses);
85 assert(size);
86
87 bool _space = false;
88 if (!with_leading_space)
89 with_leading_space = &_space;
90
91 for (size_t i = 0; i < size; i++) {
92 char buffer[INET6_ADDRSTRLEN];
93
94 if (*with_leading_space)
95 fputc(' ', f);
96 fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
97 *with_leading_space = true;
98 }
99 }
100
101 int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
102 _cleanup_free_ struct in6_addr *addresses = NULL;
103 int size = 0;
104
105 assert(ret);
106 assert(string);
107
108 for (;;) {
109 _cleanup_free_ char *word = NULL;
110 struct in6_addr *new_addresses;
111 int r;
112
113 r = extract_first_word(&string, &word, NULL, 0);
114 if (r < 0)
115 return r;
116 if (r == 0)
117 break;
118
119 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
120 if (!new_addresses)
121 return -ENOMEM;
122 else
123 addresses = new_addresses;
124
125 r = inet_pton(AF_INET6, word, &(addresses[size]));
126 if (r <= 0)
127 continue;
128
129 size++;
130 }
131
132 *ret = TAKE_PTR(addresses);
133
134 return size;
135 }
136
137 void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) {
138 assert(f);
139 assert(key);
140 assert(routes);
141 assert(size);
142
143 fprintf(f, "%s=", key);
144
145 for (size_t i = 0; i < size; i++) {
146 char sbuf[INET_ADDRSTRLEN];
147 struct in_addr dest, gw;
148 uint8_t length;
149
150 assert_se(sd_dhcp_route_get_destination(routes[i], &dest) >= 0);
151 assert_se(sd_dhcp_route_get_gateway(routes[i], &gw) >= 0);
152 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &length) >= 0);
153
154 fprintf(f, "%s/%" PRIu8, inet_ntop(AF_INET, &dest, sbuf, sizeof sbuf), length);
155 fprintf(f, ",%s%s", inet_ntop(AF_INET, &gw, sbuf, sizeof sbuf), i < size - 1 ? " ": "");
156 }
157
158 fputs("\n", f);
159 }
160
161 int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string) {
162 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
163 size_t size = 0, allocated = 0;
164
165 assert(ret);
166 assert(ret_size);
167 assert(ret_allocated);
168 assert(string);
169
170 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
171 for (;;) {
172 _cleanup_free_ char *word = NULL;
173 char *tok, *tok_end;
174 unsigned n;
175 int r;
176
177 r = extract_first_word(&string, &word, NULL, 0);
178 if (r < 0)
179 return r;
180 if (r == 0)
181 break;
182
183 if (!GREEDY_REALLOC(routes, allocated, size + 1))
184 return -ENOMEM;
185
186 tok = word;
187
188 /* get the subnet */
189 tok_end = strchr(tok, '/');
190 if (!tok_end)
191 continue;
192 *tok_end = '\0';
193
194 r = inet_aton(tok, &routes[size].dst_addr);
195 if (r == 0)
196 continue;
197
198 tok = tok_end + 1;
199
200 /* get the prefixlen */
201 tok_end = strchr(tok, ',');
202 if (!tok_end)
203 continue;
204
205 *tok_end = '\0';
206
207 r = safe_atou(tok, &n);
208 if (r < 0 || n > 32)
209 continue;
210
211 routes[size].dst_prefixlen = (uint8_t) n;
212 tok = tok_end + 1;
213
214 /* get the gateway */
215 r = inet_aton(tok, &routes[size].gw_addr);
216 if (r == 0)
217 continue;
218
219 size++;
220 }
221
222 *ret_size = size;
223 *ret_allocated = allocated;
224 *ret = TAKE_PTR(routes);
225
226 return 0;
227 }
228
229 int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) {
230 _cleanup_free_ char *hex_buf = NULL;
231
232 assert(f);
233 assert(key);
234 assert(data);
235
236 hex_buf = hexmem(data, size);
237 if (!hex_buf)
238 return -ENOMEM;
239
240 fprintf(f, "%s=%s\n", key, hex_buf);
241
242 return 0;
243 }