Refactor dhcp_utils for more flexible uses.

Separate out fetching DHCP results from starting the daemon
(for either discover or renew).

Renamed dhcp_do_request() to dhcp_start().
Renamed dhcp_do_request_renew() to dhcp_start_renew().
Added new dhcp_get_results().

Callers of the prior versions of dhcp_do_request* now separately
call dhcp_get_results().

Bug: 19422416
Change-Id: I05447853d444015e6014661dda1bb177b58fe463
This commit is contained in:
Erik Kline 2015-02-19 19:15:34 +09:00
parent 317b4024a2
commit c2291a7ed3
2 changed files with 58 additions and 69 deletions

View File

@ -23,26 +23,18 @@
__BEGIN_DECLS
extern int do_dhcp(char *iname);
extern int dhcp_do_request(const char *ifname,
char *ipaddr,
char *gateway,
uint32_t *prefixLength,
char *dns[],
char *server,
uint32_t *lease,
char *vendorInfo,
char *domain,
char *mtu);
extern int dhcp_do_request_renew(const char *ifname,
char *ipaddr,
char *gateway,
uint32_t *prefixLength,
char *dns[],
char *server,
uint32_t *lease,
char *vendorInfo,
char *domain,
char *mtu);
extern int dhcp_start(const char *ifname);
extern int dhcp_start_renew(const char *ifname);
extern int dhcp_get_results(const char *ifname,
char *ipaddr,
char *gateway,
uint32_t *prefixLength,
char *dns[],
char *server,
uint32_t *lease,
char *vendorInfo,
char *domain,
char *mtu);
extern int dhcp_stop(const char *ifname);
extern int dhcp_release_lease(const char *ifname);
extern char *dhcp_get_errmsg();

View File

@ -33,7 +33,7 @@ static const char DHCP_CONFIG_PATH[] = "/system/etc/dhcpcd/dhcpcd.conf";
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];
static char errmsg[100] = "";
/* interface length for dhcpcd daemon start (dhcpcd_<interface> as defined in init.rc file)
* or for filling up system properties dhcpcd.<interface>.ipaddress, dhcpcd.<interface>.dns1
* and other properties on a successful bind
@ -174,6 +174,47 @@ static const char *ipaddr_to_string(in_addr_t addr)
return inet_ntoa(in_addr);
}
/*
* Get any available DHCP results.
*/
int dhcp_get_results(const char *interface,
char *ipaddr,
char *gateway,
uint32_t *prefixLength,
char *dns[],
char *server,
uint32_t *lease,
char *vendorInfo,
char *domain,
char *mtu)
{
char result_prop_name[PROPERTY_KEY_MAX];
char prop_value[PROPERTY_VALUE_MAX];
/* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
char p2p_interface[MAX_INTERFACE_LENGTH];
get_p2p_interface_replacement(interface, p2p_interface);
snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
DHCP_PROP_NAME_PREFIX,
p2p_interface);
memset(prop_value, '\0', PROPERTY_VALUE_MAX);
if (!property_get(result_prop_name, prop_value, NULL)) {
snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
return -1;
}
if (strcmp(prop_value, "ok") == 0) {
if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
server, lease, vendorInfo, domain, mtu) == -1) {
return -1;
}
return 0;
} else {
snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
return -1;
}
}
/*
* Start the dhcp client daemon, and wait for it to finish
* configuring the interface.
@ -183,16 +224,7 @@ static const char *ipaddr_to_string(in_addr_t addr)
* Example:
* service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf
*/
int dhcp_do_request(const char *interface,
char *ipaddr,
char *gateway,
uint32_t *prefixLength,
char *dns[],
char *server,
uint32_t *lease,
char *vendorInfo,
char *domain,
char *mtu)
int dhcp_start(const char *interface)
{
char result_prop_name[PROPERTY_KEY_MAX];
char daemon_prop_name[PROPERTY_KEY_MAX];
@ -236,22 +268,7 @@ int dhcp_do_request(const char *interface,
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 result property was not set");
return -1;
}
if (strcmp(prop_value, "ok") == 0) {
char dns_prop_name[PROPERTY_KEY_MAX];
if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
server, lease, vendorInfo, domain, mtu) == -1) {
return -1;
}
return 0;
} else {
snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
return -1;
}
return 0;
}
/**
@ -327,16 +344,7 @@ char *dhcp_get_errmsg() {
* service iprenew_<interface> /system/bin/dhcpcd -n
*
*/
int dhcp_do_request_renew(const char *interface,
char *ipaddr,
char *gateway,
uint32_t *prefixLength,
char *dns[],
char *server,
uint32_t *lease,
char *vendorInfo,
char *domain,
char *mtu)
int dhcp_start_renew(const char *interface)
{
char result_prop_name[PROPERTY_KEY_MAX];
char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
@ -366,16 +374,5 @@ int dhcp_do_request_renew(const char *interface,
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) {
return fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
server, lease, vendorInfo, domain, mtu);
} else {
snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
return -1;
}
return 0;
}