am 3746be26: DHCP changes from master

* commit '3746be268c1b70ebbf0dee6ed21b7efb479414c9':
  DHCP changes from master
This commit is contained in:
Irfan Sheriff 2011-05-02 12:03:12 -07:00 committed by Android Git Automerger
commit c951695ede
1 changed files with 92 additions and 10 deletions

View File

@ -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;
}
}