]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: simplify how initial space is handled 15909/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 26 May 2020 08:19:31 +0000 (10:19 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 26 May 2020 08:19:31 +0000 (10:19 +0200)
src/libsystemd-network/network-internal.c
src/libsystemd-network/network-internal.h
src/network/networkd-link.c

index c23e439250ad6c61a020a2e88327a19bc337bdd6..3bdabd99fbd875a9a06548c58723cf5a63f8b386 100644 (file)
@@ -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;
         }
 }
 
index 16ff173ac6647cd1c491c0233f3423722520554e..c413afc7d5d4fd32c203b937668418fbbb33e3cc 100644 (file)
@@ -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 */
index 926bde7d481ee5644d65e45fd3a64d139fd18568..482cc2a44c7df228b6385721cdbfb6494d83fa64 100644 (file)
@@ -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);
                 }