1
1
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:
Zbigniew Jędrzejewski-Szmek 2020-05-26 10:19:31 +02:00
parent d5e172d2fb
commit d8bff5cc37
3 changed files with 24 additions and 26 deletions

View File

@ -656,24 +656,27 @@ int config_parse_bridge_port_priority(
size_t serialize_in_addrs(FILE *f, size_t serialize_in_addrs(FILE *f,
const struct in_addr *addresses, const struct in_addr *addresses,
size_t size, size_t size,
bool with_leading_space, bool *with_leading_space,
bool (*predicate)(const struct in_addr *addr)) { bool (*predicate)(const struct in_addr *addr)) {
assert(f); assert(f);
assert(addresses); assert(addresses);
size_t count = 0; size_t count = 0;
bool _space = false;
if (!with_leading_space)
with_leading_space = &_space;
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
char sbuf[INET_ADDRSTRLEN]; char sbuf[INET_ADDRSTRLEN];
if (predicate && !predicate(&addresses[i])) if (predicate && !predicate(&addresses[i]))
continue; continue;
if (with_leading_space)
if (*with_leading_space)
fputc(' ', f); fputc(' ', f);
else
with_leading_space = true;
fputs(inet_ntop(AF_INET, &addresses[i], sbuf, sizeof(sbuf)), f); fputs(inet_ntop(AF_INET, &addresses[i], sbuf, sizeof(sbuf)), f);
count++; count++;
*with_leading_space = true;
} }
return count; return count;
@ -715,18 +718,22 @@ int deserialize_in_addrs(struct in_addr **ret, const char *string) {
return size; 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(f);
assert(addresses); assert(addresses);
assert(size); assert(size);
bool _space = false;
if (!with_leading_space)
with_leading_space = &_space;
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
char buffer[INET6_ADDRSTRLEN]; char buffer[INET6_ADDRSTRLEN];
fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f); if (*with_leading_space)
if (i < size - 1)
fputc(' ', f); fputc(' ', f);
fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
*with_leading_space = true;
} }
} }

View File

@ -50,11 +50,12 @@ const char *net_get_name_persistent(sd_device *device);
size_t serialize_in_addrs(FILE *f, size_t serialize_in_addrs(FILE *f,
const struct in_addr *addresses, const struct in_addr *addresses,
size_t size, size_t size,
bool with_leading_space, bool *with_leading_space,
bool (*predicate)(const struct in_addr *addr)); bool (*predicate)(const struct in_addr *addr));
int deserialize_in_addrs(struct in_addr **addresses, const char *string); int deserialize_in_addrs(struct in_addr **addresses, const char *string);
void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, 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); 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 */ /* don't include "dhcp-lease-internal.h" as it causes conflicts between netinet/ip.h and linux/ip.h */

View File

@ -4026,20 +4026,15 @@ static void serialize_addresses(
r = sd_dhcp_lease_get_servers(lease, what, &lease_addresses); r = sd_dhcp_lease_get_servers(lease, what, &lease_addresses);
if (r > 0) if (r > 0)
if (serialize_in_addrs(f, lease_addresses, r, space, in4_addr_is_non_local) > 0) serialize_in_addrs(f, lease_addresses, r, space, in4_addr_is_non_local);
*space = true;
} }
if (lease6 && conditional6 && lease6_get_addr) { if (lease6 && conditional6 && lease6_get_addr) {
const struct in6_addr *in6_addrs; const struct in6_addr *in6_addrs;
r = lease6_get_addr(lease6, &in6_addrs); r = lease6_get_addr(lease6, &in6_addrs);
if (r > 0) { if (r > 0)
if (*space) serialize_in6_addrs(f, in6_addrs, r, space);
fputc(' ', f);
serialize_in6_addrs(f, in6_addrs, r);
*space = true;
}
} }
if (lease6 && conditional6 && lease6_get_fqdn) { 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) { if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) {
NDiscRDNSS *dd; NDiscRDNSS *dd;
SET_FOREACH(dd, link->ndisc_rdnss, i) { SET_FOREACH(dd, link->ndisc_rdnss, i)
if (space) serialize_in6_addrs(f, &dd->address, 1, &space);
fputc(' ', f);
serialize_in6_addrs(f, &dd->address, 1);
space = true;
}
} }
fputc('\n', f); fputc('\n', f);
@ -4361,7 +4351,7 @@ int link_save(Link *link) {
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r >= 0) { if (r >= 0) {
fputs("DHCP4_ADDRESS=", f); fputs("DHCP4_ADDRESS=", f);
serialize_in_addrs(f, &address, 1, false, NULL); serialize_in_addrs(f, &address, 1, NULL, NULL);
fputc('\n', f); fputc('\n', f);
} }