From 3746be268c1b70ebbf0dee6ed21b7efb479414c9 Mon Sep 17 00:00:00 2001 From: Irfan Sheriff Date: Sat, 30 Apr 2011 12:59:51 -0700 Subject: [PATCH] DHCP changes from master Pick dhcp changes from master for the renewal fixes Bug: 3344732 Change-Id: Ie49cd11f74cf89e9edf2d1c788a699ce0996190d --- libnetutils/dhcp_utils.c | 102 +++++++++++++++++++++++++++++++++++---- 1 file changed, 92 insertions(+), 10 deletions(-) diff --git a/libnetutils/dhcp_utils.c b/libnetutils/dhcp_utils.c index 030e67725..84967255f 100644 --- a/libnetutils/dhcp_utils.c +++ b/libnetutils/dhcp_utils.c @@ -31,6 +31,7 @@ static const char HOSTNAME_PROP_NAME[] = "net.hostname"; static const char DHCP_PROP_NAME_PREFIX[] = "dhcp"; static const int NAP_TIME = 200; /* wait for 200ms at a time */ /* when polling for property values */ +static const char DAEMON_NAME_RENEW[] = "iprenew"; static char errmsg[100]; /* @@ -78,6 +79,15 @@ static int fill_ip_info(const char *interface, snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface); property_get(prop_name, gateway, NULL); + snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface); + property_get(prop_name, server, NULL); + + //TODO: Handle IPv6 when we change system property usage + if (strcmp(gateway, "0.0.0.0") == 0) { + //DHCP server is our best bet as gateway + strncpy(gateway, server, PROPERTY_VALUE_MAX); + } + snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface); if (property_get(prop_name, prop_value, NULL)) { int p; @@ -106,9 +116,6 @@ static int fill_ip_info(const char *interface, snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface); property_get(prop_name, dns2, NULL); - snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface); - 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); @@ -138,6 +145,7 @@ int dhcp_do_request(const char *interface, uint32_t *lease) { char result_prop_name[PROPERTY_KEY_MAX]; + char daemon_prop_name[PROPERTY_KEY_MAX]; char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; char daemon_cmd[PROPERTY_VALUE_MAX * 2]; const char *ctrl_prop = "ctl.start"; @@ -146,18 +154,23 @@ int dhcp_do_request(const char *interface, snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", DHCP_PROP_NAME_PREFIX, interface); + + snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", + DAEMON_PROP_NAME, + interface); + /* Erase any previous setting of the dhcp result property */ property_set(result_prop_name, ""); /* Start the daemon and wait until it's ready */ if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0')) - snprintf(daemon_cmd, sizeof(daemon_cmd), "%s:-h %s %s", DAEMON_NAME, + snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, interface, prop_value, interface); else - snprintf(daemon_cmd, sizeof(daemon_cmd), "%s:%s", DAEMON_NAME, interface); + snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, interface, interface); memset(prop_value, '\0', PROPERTY_VALUE_MAX); property_set(ctrl_prop, daemon_cmd); - if (wait_for_property(DAEMON_PROP_NAME, desired_status, 10) < 0) { + if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) { snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start"); return -1; } @@ -199,15 +212,24 @@ int dhcp_do_request(const char *interface, int dhcp_stop(const char *interface) { char result_prop_name[PROPERTY_KEY_MAX]; + char daemon_prop_name[PROPERTY_KEY_MAX]; + char daemon_cmd[PROPERTY_VALUE_MAX * 2]; const char *ctrl_prop = "ctl.stop"; const char *desired_status = "stopped"; snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", DHCP_PROP_NAME_PREFIX, interface); + + snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", + DAEMON_PROP_NAME, + interface); + + snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface); + /* Stop the daemon and wait until it's reported to be stopped */ - property_set(ctrl_prop, DAEMON_NAME); - if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) { + property_set(ctrl_prop, daemon_cmd); + if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) { return -1; } property_set(result_prop_name, "failed"); @@ -219,12 +241,20 @@ int dhcp_stop(const char *interface) */ int dhcp_release_lease(const char *interface) { + char daemon_prop_name[PROPERTY_KEY_MAX]; + char daemon_cmd[PROPERTY_VALUE_MAX * 2]; const char *ctrl_prop = "ctl.stop"; const char *desired_status = "stopped"; + snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", + DAEMON_PROP_NAME, + interface); + + snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface); + /* Stop the daemon and wait until it's reported to be stopped */ - property_set(ctrl_prop, DAEMON_NAME); - if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) { + property_set(ctrl_prop, daemon_cmd); + if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) { return -1; } return 0; @@ -233,3 +263,55 @@ int dhcp_release_lease(const char *interface) char *dhcp_get_errmsg() { return errmsg; } + +/** + * DHCP renewal request + */ +int dhcp_do_request_renew(const char *interface, + char *ipaddr, + char *gateway, + uint32_t *prefixLength, + char *dns1, + char *dns2, + char *server, + uint32_t *lease) +{ + char result_prop_name[PROPERTY_KEY_MAX]; + char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; + char daemon_cmd[PROPERTY_VALUE_MAX * 2]; + const char *ctrl_prop = "ctl.start"; + + snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", + DHCP_PROP_NAME_PREFIX, + interface); + + /* Erase any previous setting of the dhcp result property */ + property_set(result_prop_name, ""); + + /* Start the renew daemon and wait until it's ready */ + snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW, interface, interface); + memset(prop_value, '\0', PROPERTY_VALUE_MAX); + property_set(ctrl_prop, daemon_cmd); + + /* Wait for the daemon to return a result */ + if (wait_for_property(result_prop_name, NULL, 30) < 0) { + snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish"); + return -1; + } + + if (!property_get(result_prop_name, prop_value, NULL)) { + /* shouldn't ever happen, given the success of wait_for_property() */ + snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set"); + return -1; + } + if (strcmp(prop_value, "ok") == 0) { + if(fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease) + == -1) { + return -1; + } + return 0; + } else { + snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value); + return -1; + } +}