mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-12-22 13:33:56 +03:00
network: simplify how initial space is handled
This commit is contained in:
parent
d5e172d2fb
commit
d8bff5cc37
@ -656,24 +656,27 @@ int config_parse_bridge_port_priority(
|
||||
size_t serialize_in_addrs(FILE *f,
|
||||
const struct in_addr *addresses,
|
||||
size_t size,
|
||||
bool with_leading_space,
|
||||
bool *with_leading_space,
|
||||
bool (*predicate)(const struct in_addr *addr)) {
|
||||
assert(f);
|
||||
assert(addresses);
|
||||
|
||||
size_t count = 0;
|
||||
bool _space = false;
|
||||
if (!with_leading_space)
|
||||
with_leading_space = &_space;
|
||||
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
char sbuf[INET_ADDRSTRLEN];
|
||||
|
||||
if (predicate && !predicate(&addresses[i]))
|
||||
continue;
|
||||
if (with_leading_space)
|
||||
|
||||
if (*with_leading_space)
|
||||
fputc(' ', f);
|
||||
else
|
||||
with_leading_space = true;
|
||||
fputs(inet_ntop(AF_INET, &addresses[i], sbuf, sizeof(sbuf)), f);
|
||||
count++;
|
||||
*with_leading_space = true;
|
||||
}
|
||||
|
||||
return count;
|
||||
@ -715,18 +718,22 @@ int deserialize_in_addrs(struct in_addr **ret, const char *string) {
|
||||
return size;
|
||||
}
|
||||
|
||||
void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size) {
|
||||
void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size, bool *with_leading_space) {
|
||||
assert(f);
|
||||
assert(addresses);
|
||||
assert(size);
|
||||
|
||||
bool _space = false;
|
||||
if (!with_leading_space)
|
||||
with_leading_space = &_space;
|
||||
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
char buffer[INET6_ADDRSTRLEN];
|
||||
|
||||
fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
|
||||
|
||||
if (i < size - 1)
|
||||
if (*with_leading_space)
|
||||
fputc(' ', f);
|
||||
fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
|
||||
*with_leading_space = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,11 +50,12 @@ const char *net_get_name_persistent(sd_device *device);
|
||||
size_t serialize_in_addrs(FILE *f,
|
||||
const struct in_addr *addresses,
|
||||
size_t size,
|
||||
bool with_leading_space,
|
||||
bool *with_leading_space,
|
||||
bool (*predicate)(const struct in_addr *addr));
|
||||
int deserialize_in_addrs(struct in_addr **addresses, const char *string);
|
||||
void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses,
|
||||
size_t size);
|
||||
size_t size,
|
||||
bool *with_leading_space);
|
||||
int deserialize_in6_addrs(struct in6_addr **addresses, const char *string);
|
||||
|
||||
/* don't include "dhcp-lease-internal.h" as it causes conflicts between netinet/ip.h and linux/ip.h */
|
||||
|
@ -4026,20 +4026,15 @@ static void serialize_addresses(
|
||||
|
||||
r = sd_dhcp_lease_get_servers(lease, what, &lease_addresses);
|
||||
if (r > 0)
|
||||
if (serialize_in_addrs(f, lease_addresses, r, space, in4_addr_is_non_local) > 0)
|
||||
*space = true;
|
||||
serialize_in_addrs(f, lease_addresses, r, space, in4_addr_is_non_local);
|
||||
}
|
||||
|
||||
if (lease6 && conditional6 && lease6_get_addr) {
|
||||
const struct in6_addr *in6_addrs;
|
||||
|
||||
r = lease6_get_addr(lease6, &in6_addrs);
|
||||
if (r > 0) {
|
||||
if (*space)
|
||||
fputc(' ', f);
|
||||
serialize_in6_addrs(f, in6_addrs, r);
|
||||
*space = true;
|
||||
}
|
||||
if (r > 0)
|
||||
serialize_in6_addrs(f, in6_addrs, r, space);
|
||||
}
|
||||
|
||||
if (lease6 && conditional6 && lease6_get_fqdn) {
|
||||
@ -4149,13 +4144,8 @@ int link_save(Link *link) {
|
||||
if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) {
|
||||
NDiscRDNSS *dd;
|
||||
|
||||
SET_FOREACH(dd, link->ndisc_rdnss, i) {
|
||||
if (space)
|
||||
fputc(' ', f);
|
||||
|
||||
serialize_in6_addrs(f, &dd->address, 1);
|
||||
space = true;
|
||||
}
|
||||
SET_FOREACH(dd, link->ndisc_rdnss, i)
|
||||
serialize_in6_addrs(f, &dd->address, 1, &space);
|
||||
}
|
||||
|
||||
fputc('\n', f);
|
||||
@ -4361,7 +4351,7 @@ int link_save(Link *link) {
|
||||
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
|
||||
if (r >= 0) {
|
||||
fputs("DHCP4_ADDRESS=", f);
|
||||
serialize_in_addrs(f, &address, 1, false, NULL);
|
||||
serialize_in_addrs(f, &address, 1, NULL, NULL);
|
||||
fputc('\n', f);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user