chiark / gitweb /
networkd: merge DNS and NTP entries when exporting
authorTom Gundersen <teg@jklm.no>
Wed, 23 Jul 2014 11:48:18 +0000 (13:48 +0200)
committerTom Gundersen <teg@jklm.no>
Wed, 23 Jul 2014 21:54:51 +0000 (23:54 +0200)
In the state files, do not distinguish where the various entries came from
(static or DHCP), but include them all in the same list.

src/libsystemd-network/network-internal.c
src/libsystemd-network/network-internal.h
src/libsystemd-network/sd-dhcp-lease.c
src/network/networkd-address.c
src/network/networkd-link.c
src/network/networkd-network-gperf.gperf
src/network/networkd-network.c
src/network/networkd.h
src/resolve/resolved-link.c

index 3407537..603ee6d 100644 (file)
@@ -339,21 +339,16 @@ int net_parse_inaddr(const char *address, int *family, void *dst) {
         return 0;
 }
 
-void serialize_in_addrs(FILE *f, const char *key, const struct in_addr *addresses, size_t size) {
+void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size) {
         unsigned i;
 
         assert(f);
-        assert(key);
         assert(addresses);
         assert(size);
 
-        fprintf(f, "%s=", key);
-
         for (i = 0; i < size; i++)
                 fprintf(f, "%s%s", inet_ntoa(addresses[i]),
                         (i < (size - 1)) ? " ": "");
-
-        fputs("\n", f);
 }
 
 int deserialize_in_addrs(struct in_addr **ret, const char *string) {
index 765dff4..6dd518b 100644 (file)
@@ -67,7 +67,7 @@ int net_parse_inaddr(const char *address, int *family, void *dst);
 int net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]);
 const char *net_get_name(struct udev_device *device);
 
-void serialize_in_addrs(FILE *f, const char *key, const struct in_addr *addresses, size_t size);
+void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size);
 int deserialize_in_addrs(struct in_addr **addresses, const char *string);
 int deserialize_in6_addrs(struct in6_addr **addresses, const char *string);
 
index 4ed30f5..bfd1b0c 100644 (file)
@@ -629,13 +629,17 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
         if (r >= 0)
                 fprintf(f, "MTU=%" PRIu16 "\n", mtu);
 
+        fputs("DNS=", f);
         r = sd_dhcp_lease_get_dns(lease, &addresses);
         if (r >= 0)
-                serialize_in_addrs(f, "DNS", addresses, r);
+                serialize_in_addrs(f, addresses, r);
+        fputs("\n", f);
 
+        fputs("NTP=", f);
         r = sd_dhcp_lease_get_ntp(lease, &addresses);
         if (r >= 0)
-                serialize_in_addrs(f, "NTP", addresses, r);
+                serialize_in_addrs(f, addresses, r);
+        fputs("\n", f);
 
         r = sd_dhcp_lease_get_domainname(lease, &string);
         if (r >= 0)
index 8c4ef99..551c1ea 100644 (file)
@@ -394,55 +394,6 @@ int address_configure(Address *address, Link *link,
         return 0;
 }
 
-int config_parse_dns(const char *unit,
-                const char *filename,
-                unsigned line,
-                const char *section,
-                unsigned section_line,
-                const char *lvalue,
-                int ltype,
-                const char *rvalue,
-                void *data,
-                void *userdata) {
-        Network *network = userdata;
-        Address *tail;
-        _cleanup_address_free_ Address *n = NULL;
-        int r;
-
-        assert(filename);
-        assert(section);
-        assert(lvalue);
-        assert(rvalue);
-        assert(network);
-
-        r = address_new_dynamic(&n);
-        if (r < 0)
-                return r;
-
-        r = net_parse_inaddr(rvalue, &n->family, &n->in_addr);
-        if (r < 0) {
-                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
-                           "DNS address is invalid, ignoring assignment: %s", rvalue);
-                return 0;
-        }
-
-        if (streq(lvalue, "DNS")) {
-                LIST_FIND_TAIL(addresses, network->dns, tail);
-                LIST_INSERT_AFTER(addresses, network->dns, tail, n);
-        } else if (streq(lvalue, "NTP")) {
-                LIST_FIND_TAIL(addresses, network->ntp, tail);
-                LIST_INSERT_AFTER(addresses, network->ntp, tail, n);
-        } else {
-                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
-                           "Key is invalid, ignoring assignment: %s=%s", lvalue, rvalue);
-                return 0;
-        }
-
-        n = NULL;
-
-        return 0;
-}
-
 int config_parse_broadcast(const char *unit,
                 const char *filename,
                 unsigned line,
index 86d4b83..7e6bc7b 100644 (file)
@@ -2419,27 +2419,6 @@ int link_update(Link *link, sd_rtnl_message *m) {
         return link_update_flags(link, m);
 }
 
-static void serialize_addresses(FILE *f, const char *key, Address *address) {
-        Address *ad;
-
-        assert(f);
-        assert(key);
-
-        if (!address)
-                return;
-
-        fprintf(f, "%s=", key);
-
-        LIST_FOREACH(addresses, ad, address) {
-                char buf[INET6_ADDRSTRLEN];
-
-                if (inet_ntop(ad->family, &ad->in_addr, buf, INET6_ADDRSTRLEN))
-                        fprintf(f, "%s%s", buf, (ad->addresses_next) ? " ": "");
-        }
-
-        fputs("\n", f);
-}
-
 static void link_update_operstate(Link *link) {
 
         assert(link);
@@ -2510,8 +2489,47 @@ int link_save(Link *link) {
                 admin_state, oper_state);
 
         if (link->network) {
-                serialize_addresses(f, "DNS", link->network->dns);
-                serialize_addresses(f, "NTP", link->network->ntp);
+                char **address;
+
+                fputs("DNS=", f);
+
+                if (link->network->dhcp_dns &&
+                    link->dhcp_lease) {
+                        const struct in_addr *addresses;
+
+                        r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
+                        if (r > 0) {
+                                serialize_in_addrs(f, addresses, r);
+                                if (link->network->dns)
+                                        fputs(" ", f);
+                        }
+                }
+
+                STRV_FOREACH(address, link->network->dns)
+                        fprintf(f, "%s%s", *address,
+                                (address + 1 ? " " : ""));
+
+                fputs("\n", f);
+
+                fprintf(f, "NTP=");
+
+                if (link->network->dhcp_ntp &&
+                    link->dhcp_lease) {
+                        const struct in_addr *addresses;
+
+                        r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
+                        if (r > 0) {
+                                serialize_in_addrs(f, addresses, r);
+                                if (link->network->ntp)
+                                        fputs(" ", f);
+                        }
+                }
+
+                STRV_FOREACH(address, link->network->ntp)
+                        fprintf(f, "%s%s", *address,
+                                (address + 1 ? " " : ""));
+
+                fputs("\n", f);
         }
 
         if (link->dhcp_lease) {
@@ -2522,12 +2540,8 @@ int link_save(Link *link) {
                         goto finish;
 
                 fprintf(f,
-                        "DHCP_LEASE=%s\n"
-                        "DHCP_USE_DNS=%s\n"
-                        "DHCP_USE_NTP=%s\n",
-                        link->lease_file,
-                        yes_no(link->network->dhcp_dns),
-                        yes_no(link->network->dhcp_ntp));
+                        "DHCP_LEASE=%s\n",
+                        link->lease_file);
         } else
                 unlink(link->lease_file);
 
index f5156b4..2938f8a 100644 (file)
@@ -35,8 +35,8 @@ Network.DHCPServer,          config_parse_bool,                  0,
 Network.IPv4LL,              config_parse_bool,                  0,                             offsetof(Network, ipv4ll)
 Network.Address,             config_parse_address,               0,                             0
 Network.Gateway,             config_parse_gateway,               0,                             0
-Network.DNS,                 config_parse_dns,                   0,                             offsetof(Network, dns)
-Network.NTP,                 config_parse_dns,                   0,                             offsetof(Network, ntp)
+Network.DNS,                 config_parse_strv,                  0,                             offsetof(Network, dns)
+Network.NTP,                 config_parse_strv,                  0,                             offsetof(Network, ntp)
 Network.Tunnel,              config_parse_tunnel,                0,                             offsetof(Network, tunnel)
 Address.Address,             config_parse_address,               0,                             0
 Address.Peer,                config_parse_address,               0,                             0
index bc93042..24fc26b 100644 (file)
@@ -169,15 +169,8 @@ void network_free(Network *network) {
         free(network->description);
         free(network->dhcp_vendor_class_identifier);
 
-        while ((address = network->ntp)) {
-                LIST_REMOVE(addresses, network->ntp, address);
-                address_free(address);
-        }
-
-        while ((address = network->dns)) {
-                LIST_REMOVE(addresses, network->dns, address);
-                address_free(address);
-        }
+        strv_free(network->ntp);
+        strv_free(network->dns);
 
         netdev_unref(network->bridge);
 
index 7d291e5..bae2852 100644 (file)
@@ -105,8 +105,7 @@ struct Network {
         Hashmap *addresses_by_section;
         Hashmap *routes_by_section;
 
-        LIST_HEAD(Address, dns);
-        LIST_HEAD(Address, ntp);
+        char **dns, **ntp;
 
         LIST_FIELDS(Network, networks);
 };
@@ -338,10 +337,6 @@ bool address_equal(Address *a1, Address *a2);
 DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
 #define _cleanup_address_free_ _cleanup_(address_freep)
 
-int config_parse_dns(const char *unit, const char *filename, unsigned line,
-                     const char *section, unsigned section_line, const char *lvalue,
-                     int ltype, const char *rvalue, void *data, void *userdata);
-
 int config_parse_address(const char *unit, const char *filename, unsigned line,
                          const char *section, unsigned section_line, const char *lvalue,
                          int ltype, const char *rvalue, void *data, void *userdata);
index 078301a..43f60d8 100644 (file)
@@ -136,57 +136,6 @@ int link_update_rtnl(Link *l, sd_rtnl_message *m) {
         return 0;
 }
 
-static int link_update_dhcp_dns_servers(Link *l) {
-        _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
-        const struct in_addr *nameservers = NULL;
-        DnsServer *s, *nx;
-        int r, n, i;
-
-        assert(l);
-
-        r = sd_network_dhcp_use_dns(l->ifindex);
-        if (r <= 0)
-                goto clear;
-
-        r = sd_network_get_dhcp_lease(l->ifindex, &lease);
-        if (r < 0)
-                goto clear;
-
-        LIST_FOREACH(servers, s, l->dhcp_dns_servers)
-                s->marked = true;
-
-        n = sd_dhcp_lease_get_dns(lease, &nameservers);
-        if (n < 0) {
-                r = n;
-                goto clear;
-        }
-
-        for (i = 0; i < n; i++) {
-                union in_addr_union a = { .in = nameservers[i] };
-
-                s = link_find_dns_server(l, DNS_SERVER_DHCP, AF_INET, &a);
-                if (s)
-                        s->marked = false;
-                else {
-                        r = dns_server_new(l->manager, NULL, DNS_SERVER_DHCP, l, AF_INET, &a);
-                        if (r < 0)
-                                goto clear;
-                }
-        }
-
-        LIST_FOREACH_SAFE(servers, s, nx, l->dhcp_dns_servers)
-                if (s->marked)
-                        dns_server_free(s);
-
-        return 0;
-
-clear:
-        while (l->dhcp_dns_servers)
-                dns_server_free(l->dhcp_dns_servers);
-
-        return r;
-}
-
 static int link_update_link_dns_servers(Link *l) {
         _cleanup_free_ struct in_addr *nameservers = NULL;
         _cleanup_free_ struct in6_addr *nameservers6 = NULL;
@@ -252,7 +201,6 @@ clear:
 int link_update_monitor(Link *l) {
         assert(l);
 
-        link_update_dhcp_dns_servers(l);
         link_update_link_dns_servers(l);
         link_allocate_scopes(l);