chiark / gitweb /
sd-dhcp-client: refactor DNS support
authorTom Gundersen <teg@jklm.no>
Thu, 16 Jan 2014 18:32:22 +0000 (19:32 +0100)
committerTom Gundersen <teg@jklm.no>
Thu, 16 Jan 2014 19:32:08 +0000 (20:32 +0100)
Rather than keeping an array of pointers to addresses, just keep an array of addresses.

src/libsystemd/sd-dhcp-client.c
src/network/networkd-link.c
src/network/networkd-manager.c
src/systemd/sd-dhcp-client.h

index c5d8371..c5bc115 100644 (file)
@@ -41,7 +41,8 @@ struct DHCPLease {
         be32_t server_address;
         be32_t subnet_mask;
         be32_t router;
-        struct in_addr **dns;
+        struct in_addr *dns;
+        size_t dns_size;
         uint16_t mtu;
         char *hostname;
 };
@@ -215,10 +216,11 @@ int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu)
         return 0;
 }
 
-int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr ***addr)
+int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size)
 {
         assert_return(client, -EINVAL);
         assert_return(addr, -EINVAL);
+        assert_return(addr_size, -EINVAL);
 
         switch (client->state) {
         case DHCP_STATE_INIT:
@@ -231,9 +233,10 @@ int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr ***addr)
         case DHCP_STATE_BOUND:
         case DHCP_STATE_RENEWING:
         case DHCP_STATE_REBINDING:
-                if (client->lease->dns)
+                if (client->lease->dns_size) {
+                        *addr_size = client->lease->dns_size;
                         *addr = client->lease->dns;
-                else
+                else
                         return -ENOENT;
 
                 break;
@@ -341,18 +344,6 @@ static int client_notify(sd_dhcp_client *client, int event)
         return 0;
 }
 
-static void in_addrs_free(struct in_addr **addrs) {
-        unsigned i;
-
-        if (!addrs)
-                return;
-
-        for (i = 0; addrs[i]; i++)
-                free(addrs[i]);
-
-        free(addrs);
-}
-
 static int client_stop(sd_dhcp_client *client, int error)
 {
         assert_return(client, -EINVAL);
@@ -394,7 +385,7 @@ static int client_stop(sd_dhcp_client *client, int error)
         }
 
         if (client->lease) {
-                in_addrs_free(client->lease->dns);
+                free(client->lease->dns);
                 free(client->lease);
                 client->lease = NULL;
         }
@@ -828,18 +819,16 @@ static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
                 if (len >= 4) {
                         unsigned i;
 
-                        in_addrs_free(lease->dns);
+                        lease->dns_size = len / 4;
 
-                        lease->dns = new0(struct in_addr*, len / 4 + 1);
+                        free(lease->dns);
+                        lease->dns = new0(struct in_addr, lease->dns_size);
                         if (!lease->dns)
                                 return -ENOMEM;
 
-                        for (i = 0; i < len / 4; i++) {
-                                lease->dns[i] = new0(struct in_addr, 1);
-                                memcpy(&lease->dns[i]->s_addr, option + 4 * i, 4);
+                        for (i = 0; i < lease->dns_size; i++) {
+                                memcpy(&lease->dns[i].s_addr, option + 4 * i, 4);
                         }
-
-                        lease->dns[len / 4] = NULL;
                 }
 
                 break;
index 444af8f..7f692d1 100644 (file)
@@ -491,7 +491,8 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
         if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_IP_ACQUIRE) {
                 _cleanup_address_free_ Address *addr = NULL;
                 _cleanup_route_free_ Route *rt = NULL;
-                struct in_addr **nameservers;
+                struct in_addr *nameservers;
+                size_t nameservers_size;
 
                 log_struct_link(LOG_INFO, link,
                                 "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
@@ -535,7 +536,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
                 rt = NULL;
 
                 if (link->network->dhcp_dns) {
-                        r = sd_dhcp_client_get_dns(client, &nameservers);
+                        r = sd_dhcp_client_get_dns(client, &nameservers, &nameservers_size);
                         if (r >= 0) {
                                 r = manager_update_resolv_conf(link->manager);
                                 if (r < 0)
index ca781e2..3b54214 100644 (file)
@@ -338,14 +338,15 @@ int manager_update_resolv_conf(Manager *m) {
 
         HASHMAP_FOREACH(link, m->links, i) {
                 if (link->dhcp) {
-                        struct in_addr **nameservers;
+                        struct in_addr *nameservers;
+                        size_t nameservers_size;
 
-                        r = sd_dhcp_client_get_dns(link->dhcp, &nameservers);
+                        r = sd_dhcp_client_get_dns(link->dhcp, &nameservers, &nameservers_size);
                         if (r >= 0) {
                                 unsigned j;
 
-                                for (j = 0; nameservers[j]; j++)
-                                        append_dns(f, nameservers[j], AF_INET, &count);
+                                for (j = 0; j < nameservers_size; j++)
+                                        append_dns(f, &nameservers[j], AF_INET, &count);
                         }
                 }
         }
index e387d54..beb8642 100644 (file)
@@ -54,7 +54,7 @@ int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr);
 int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr);
 int sd_dhcp_client_prefixlen(const struct in_addr *addr);
 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr);
-int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr ***addr);
+int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size);
 int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu);
 int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname);