summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-01-16 19:32:22 +0100
committerTom Gundersen <teg@jklm.no>2014-01-16 20:32:08 +0100
commit7ae4ef6d23bc884d0e0ee2ec9cf87ec99d0686bb (patch)
treee042cd409684df14801911af4ddfa239b6060cf8
parent1346b1f0388f4100bb3c2a2bb23bc881769c020c (diff)
sd-dhcp-client: refactor DNS support
Rather than keeping an array of pointers to addresses, just keep an array of addresses.
-rw-r--r--src/libsystemd/sd-dhcp-client.c37
-rw-r--r--src/network/networkd-link.c5
-rw-r--r--src/network/networkd-manager.c9
-rw-r--r--src/systemd/sd-dhcp-client.h2
4 files changed, 22 insertions, 31 deletions
diff --git a/src/libsystemd/sd-dhcp-client.c b/src/libsystemd/sd-dhcp-client.c
index c5d8371a98..c5bc115f42 100644
--- a/src/libsystemd/sd-dhcp-client.c
+++ b/src/libsystemd/sd-dhcp-client.c
@@ -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;
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 444af8f6db..7f692d1f9b 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -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)
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index ca781e2bb4..3b542149c0 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -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);
}
}
}
diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h
index e387d54046..beb8642a89 100644
--- a/src/systemd/sd-dhcp-client.h
+++ b/src/systemd/sd-dhcp-client.h
@@ -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);