Change our dhcp interface to be IPv6 compat.
Doesn't do IPv6, but removes v4-specific structures from the framework
(netmask, ints for addrs)
bug:2542681
Change-Id: I1110e6f32d42a472b104835a4298e18b391db0a7
diff --git a/include/netutils/dhcp.h b/include/netutils/dhcp.h
index 96798c5..bd16240 100644
--- a/include/netutils/dhcp.h
+++ b/include/netutils/dhcp.h
@@ -24,12 +24,12 @@
extern int do_dhcp(char *iname);
extern int dhcp_do_request(const char *ifname,
- in_addr_t *ipaddr,
- in_addr_t *gateway,
- in_addr_t *mask,
- in_addr_t *dns1,
- in_addr_t *dns2,
- in_addr_t *server,
+ char *ipaddr,
+ char *gateway,
+ uint32_t *prefixLength,
+ char *dns1,
+ char *dns2,
+ char *server,
uint32_t *lease);
extern int dhcp_stop(const char *ifname);
extern int dhcp_release_lease(const char *ifname);
diff --git a/libnetutils/dhcp_utils.c b/libnetutils/dhcp_utils.c
index 1802688..030e677 100644
--- a/libnetutils/dhcp_utils.c
+++ b/libnetutils/dhcp_utils.c
@@ -60,60 +60,60 @@
return -1; /* failure */
}
-static void fill_ip_info(const char *interface,
- in_addr_t *ipaddr,
- in_addr_t *gateway,
- in_addr_t *mask,
- in_addr_t *dns1,
- in_addr_t *dns2,
- in_addr_t *server,
+static int fill_ip_info(const char *interface,
+ char *ipaddr,
+ char *gateway,
+ uint32_t *prefixLength,
+ char *dns1,
+ char *dns2,
+ char *server,
uint32_t *lease)
{
char prop_name[PROPERTY_KEY_MAX];
char prop_value[PROPERTY_VALUE_MAX];
- struct in_addr addr;
- in_addr_t iaddr;
snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface);
- if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) {
- *ipaddr = addr.s_addr;
- } else {
- *ipaddr = 0;
- }
+ property_get(prop_name, ipaddr, NULL);
+
snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface);
- if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) {
- *gateway = addr.s_addr;
- } else {
- *gateway = 0;
- }
+ property_get(prop_name, gateway, NULL);
+
snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
- if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) {
- *mask = addr.s_addr;
- } else {
- *mask = 0;
+ if (property_get(prop_name, prop_value, NULL)) {
+ int p;
+ // this conversion is v4 only, but this dhcp client is v4 only anyway
+ in_addr_t mask = ntohl(inet_addr(prop_value));
+ // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for
+ // non 255.255.255.255 inputs. if we get that value check if it is legit..
+ if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
+ snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
+ return -1;
+ }
+ for (p = 0; p < 32; p++) {
+ if (mask == 0) break;
+ // check for non-contiguous netmask, e.g., 255.254.255.0
+ if ((mask & 0x80000000) == 0) {
+ snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
+ return -1;
+ }
+ mask = mask << 1;
+ }
+ *prefixLength = p;
}
snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
- if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) {
- *dns1 = addr.s_addr;
- } else {
- *dns1 = 0;
- }
+ property_get(prop_name, dns1, NULL);
+
snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
- if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) {
- *dns2 = addr.s_addr;
- } else {
- *dns2 = 0;
- }
+ property_get(prop_name, dns2, NULL);
+
snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface);
- if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) {
- *server = addr.s_addr;
- } else {
- *server = 0;
- }
+ property_get(prop_name, server, NULL);
+
snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface);
if (property_get(prop_name, prop_value, NULL)) {
*lease = atol(prop_value);
}
+ return 0;
}
static const char *ipaddr_to_string(in_addr_t addr)
@@ -129,12 +129,12 @@
* configuring the interface.
*/
int dhcp_do_request(const char *interface,
- in_addr_t *ipaddr,
- in_addr_t *gateway,
- in_addr_t *mask,
- in_addr_t *dns1,
- in_addr_t *dns2,
- in_addr_t *server,
+ char *ipaddr,
+ char *gateway,
+ uint32_t *prefixLength,
+ char *dns1,
+ char *dns2,
+ char *server,
uint32_t *lease)
{
char result_prop_name[PROPERTY_KEY_MAX];
@@ -175,8 +175,13 @@
}
if (strcmp(prop_value, "ok") == 0) {
char dns_prop_name[PROPERTY_KEY_MAX];
- fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease);
- /* copy the dhcp.XXX.dns properties to net.XXX.dns */
+ if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease)
+ == -1) {
+ return -1;
+ }
+
+ /* copy dns data to system properties - TODO - remove this after we have async
+ * notification of renewal's */
snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);