]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/network-internal.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <netinet/ether.h>
9 #include "alloc-util.h"
10 #include "dhcp-lease-internal.h"
11 #include "extract-word.h"
12 #include "hexdecoct.h"
14 #include "network-internal.h"
15 #include "parse-util.h"
17 size_t serialize_in_addrs(FILE *f
,
18 const struct in_addr
*addresses
,
20 bool *with_leading_space
,
21 bool (*predicate
)(const struct in_addr
*addr
)) {
27 if (!with_leading_space
)
28 with_leading_space
= &_space
;
30 for (size_t i
= 0; i
< size
; i
++) {
31 char sbuf
[INET_ADDRSTRLEN
];
33 if (predicate
&& !predicate(&addresses
[i
]))
36 if (*with_leading_space
)
38 fputs(inet_ntop(AF_INET
, &addresses
[i
], sbuf
, sizeof(sbuf
)), f
);
40 *with_leading_space
= true;
46 int deserialize_in_addrs(struct in_addr
**ret
, const char *string
) {
47 _cleanup_free_
struct in_addr
*addresses
= NULL
;
54 _cleanup_free_
char *word
= NULL
;
55 struct in_addr
*new_addresses
;
58 r
= extract_first_word(&string
, &word
, NULL
, 0);
64 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in_addr
));
68 addresses
= new_addresses
;
70 r
= inet_pton(AF_INET
, word
, &(addresses
[size
]));
77 *ret
= size
> 0 ? TAKE_PTR(addresses
) : NULL
;
82 void serialize_in6_addrs(FILE *f
, const struct in6_addr
*addresses
, size_t size
, bool *with_leading_space
) {
88 if (!with_leading_space
)
89 with_leading_space
= &_space
;
91 for (size_t i
= 0; i
< size
; i
++) {
92 char buffer
[INET6_ADDRSTRLEN
];
94 if (*with_leading_space
)
96 fputs(inet_ntop(AF_INET6
, addresses
+i
, buffer
, sizeof(buffer
)), f
);
97 *with_leading_space
= true;
101 int deserialize_in6_addrs(struct in6_addr
**ret
, const char *string
) {
102 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
109 _cleanup_free_
char *word
= NULL
;
110 struct in6_addr
*new_addresses
;
113 r
= extract_first_word(&string
, &word
, NULL
, 0);
119 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in6_addr
));
123 addresses
= new_addresses
;
125 r
= inet_pton(AF_INET6
, word
, &(addresses
[size
]));
132 *ret
= TAKE_PTR(addresses
);
137 void serialize_dhcp_routes(FILE *f
, const char *key
, sd_dhcp_route
**routes
, size_t size
) {
143 fprintf(f
, "%s=", key
);
145 for (size_t i
= 0; i
< size
; i
++) {
146 char sbuf
[INET_ADDRSTRLEN
];
147 struct in_addr dest
, gw
;
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);
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 ? " ": "");
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;
167 assert(ret_allocated
);
170 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
172 _cleanup_free_
char *word
= NULL
;
177 r
= extract_first_word(&string
, &word
, NULL
, 0);
183 if (!GREEDY_REALLOC(routes
, allocated
, size
+ 1))
189 tok_end
= strchr(tok
, '/');
194 r
= inet_aton(tok
, &routes
[size
].dst_addr
);
200 /* get the prefixlen */
201 tok_end
= strchr(tok
, ',');
207 r
= safe_atou(tok
, &n
);
211 routes
[size
].dst_prefixlen
= (uint8_t) n
;
214 /* get the gateway */
215 r
= inet_aton(tok
, &routes
[size
].gw_addr
);
223 *ret_allocated
= allocated
;
224 *ret
= TAKE_PTR(routes
);
229 int serialize_dhcp_option(FILE *f
, const char *key
, const void *data
, size_t size
) {
230 _cleanup_free_
char *hex_buf
= NULL
;
236 hex_buf
= hexmem(data
, size
);
240 fprintf(f
, "%s=%s\n", key
, hex_buf
);