summaryrefslogtreecommitdiffstats
path: root/libnetutils/dhcp_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'libnetutils/dhcp_utils.c')
-rw-r--r--[-rwxr-xr-x]libnetutils/dhcp_utils.c140
1 files changed, 87 insertions, 53 deletions
diff --git a/libnetutils/dhcp_utils.c b/libnetutils/dhcp_utils.c
index 1e08eb6..b940453 100755..100644
--- a/libnetutils/dhcp_utils.c
+++ b/libnetutils/dhcp_utils.c
@@ -29,12 +29,33 @@ static const char DAEMON_NAME[] = "dhcpcd";
static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd";
static const char HOSTNAME_PROP_NAME[] = "net.hostname";
static const char DHCP_PROP_NAME_PREFIX[] = "dhcp";
+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];
-/* interface suffix on dhcpcd */
-#define MAX_DAEMON_SUFFIX 25
+/* 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
+ */
+#define MAX_INTERFACE_LENGTH 25
+
+/*
+ * P2p interface names increase sequentially p2p-p2p0-1, p2p-p2p0-2.. after
+ * group formation. This does not work well with system properties which can quickly
+ * exhaust or for specifiying a dhcp start target in init which requires
+ * interface to be pre-defined in init.rc file.
+ *
+ * This function returns a common string p2p for all p2p interfaces.
+ */
+void get_p2p_interface_replacement(const char *interface, char *p2p_interface) {
+ /* Use p2p for any interface starting with p2p. */
+ if (strncmp(interface, "p2p",3) == 0) {
+ strncpy(p2p_interface, "p2p", MAX_INTERFACE_LENGTH);
+ } else {
+ strncpy(p2p_interface, interface, MAX_INTERFACE_LENGTH);
+ }
+}
/*
* Wait for a system property to be assigned a specified value.
@@ -70,18 +91,23 @@ static int fill_ip_info(const char *interface,
char *dns1,
char *dns2,
char *server,
- uint32_t *lease)
+ uint32_t *lease,
+ char *vendorInfo)
{
char 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];
- snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface);
+ get_p2p_interface_replacement(interface, p2p_interface);
+
+ snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, p2p_interface);
property_get(prop_name, ipaddr, NULL);
- snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface);
+ snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, p2p_interface);
property_get(prop_name, gateway, NULL);
- snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface);
+ snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, p2p_interface);
property_get(prop_name, server, NULL);
//TODO: Handle IPv6 when we change system property usage
@@ -90,7 +116,7 @@ static int fill_ip_info(const char *interface,
strncpy(gateway, server, PROPERTY_VALUE_MAX);
}
- snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
+ snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, p2p_interface);
if (property_get(prop_name, prop_value, NULL)) {
int p;
// this conversion is v4 only, but this dhcp client is v4 only anyway
@@ -112,16 +138,21 @@ static int fill_ip_info(const char *interface,
}
*prefixLength = p;
}
- snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
+ snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, p2p_interface);
property_get(prop_name, dns1, NULL);
- snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
+ snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, p2p_interface);
property_get(prop_name, dns2, NULL);
- snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface);
+ snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, p2p_interface);
if (property_get(prop_name, prop_value, NULL)) {
*lease = atol(prop_value);
}
+
+ snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX,
+ p2p_interface);
+ property_get(prop_name, vendorInfo, NULL);
+
return 0;
}
@@ -133,18 +164,14 @@ static const char *ipaddr_to_string(in_addr_t addr)
return inet_ntoa(in_addr);
}
-void get_daemon_suffix(const char *interface, char *daemon_suffix) {
- /* Use p2p suffix for any p2p interface. */
- if (strncmp(interface, "p2p",3) == 0) {
- sprintf(daemon_suffix, "p2p");
- } else {
- snprintf(daemon_suffix, MAX_DAEMON_SUFFIX, "%s", interface);
- }
-}
-
/*
* Start the dhcp client daemon, and wait for it to finish
* configuring the interface.
+ *
+ * The device init.rc file needs a corresponding entry for this work.
+ *
+ * Example:
+ * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf
*/
int dhcp_do_request(const char *interface,
char *ipaddr,
@@ -153,35 +180,38 @@ int dhcp_do_request(const char *interface,
char *dns1,
char *dns2,
char *server,
- uint32_t *lease)
+ uint32_t *lease,
+ char *vendorInfo)
{
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];
+ char daemon_cmd[PROPERTY_VALUE_MAX * 2 + sizeof(DHCP_CONFIG_PATH)];
const char *ctrl_prop = "ctl.start";
const char *desired_status = "running";
- char daemon_suffix[MAX_DAEMON_SUFFIX];
+ /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
+ char p2p_interface[MAX_INTERFACE_LENGTH];
- get_daemon_suffix(interface, daemon_suffix);
+ get_p2p_interface_replacement(interface, p2p_interface);
snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
DHCP_PROP_NAME_PREFIX,
- interface);
+ p2p_interface);
snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
DAEMON_PROP_NAME,
- daemon_suffix);
+ p2p_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_%s:-h %s %s", DAEMON_NAME, daemon_suffix,
- prop_value, interface);
+ snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s -h %s %s", DAEMON_NAME,
+ p2p_interface, DHCP_CONFIG_PATH, prop_value, interface);
else
- snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, daemon_suffix, interface);
+ snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s %s", DAEMON_NAME,
+ p2p_interface, DHCP_CONFIG_PATH, 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) {
@@ -202,8 +232,8 @@ int dhcp_do_request(const char *interface,
}
if (strcmp(prop_value, "ok") == 0) {
char dns_prop_name[PROPERTY_KEY_MAX];
- if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease)
- == -1) {
+ if (fill_ip_info(interface, ipaddr, gateway, prefixLength,
+ dns1, dns2, server, lease, vendorInfo) == -1) {
return -1;
}
@@ -231,19 +261,19 @@ int dhcp_stop(const char *interface)
const char *ctrl_prop = "ctl.stop";
const char *desired_status = "stopped";
- char daemon_suffix[MAX_DAEMON_SUFFIX];
+ char p2p_interface[MAX_INTERFACE_LENGTH];
- get_daemon_suffix(interface, daemon_suffix);
+ get_p2p_interface_replacement(interface, p2p_interface);
snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
DHCP_PROP_NAME_PREFIX,
- interface);
+ p2p_interface);
snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
DAEMON_PROP_NAME,
- daemon_suffix);
+ p2p_interface);
- snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
+ snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
/* Stop the daemon and wait until it's reported to be stopped */
property_set(ctrl_prop, daemon_cmd);
@@ -264,15 +294,15 @@ int dhcp_release_lease(const char *interface)
const char *ctrl_prop = "ctl.stop";
const char *desired_status = "stopped";
- char daemon_suffix[MAX_DAEMON_SUFFIX];
+ char p2p_interface[MAX_INTERFACE_LENGTH];
- get_daemon_suffix(interface, daemon_suffix);
+ get_p2p_interface_replacement(interface, p2p_interface);
snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
DAEMON_PROP_NAME,
- daemon_suffix);
+ p2p_interface);
- snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
+ snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
/* Stop the daemon and wait until it's reported to be stopped */
property_set(ctrl_prop, daemon_cmd);
@@ -287,37 +317,41 @@ char *dhcp_get_errmsg() {
}
/**
- * Run WiMAX dhcp renew service.
- * "wimax_renew" service shoud be included in init.rc.
+ * The device init.rc file needs a corresponding entry.
+ *
+ * Example:
+ * service iprenew_<interface> /system/bin/dhcpcd -n
+ *
*/
int dhcp_do_request_renew(const char *interface,
- in_addr_t *ipaddr,
- in_addr_t *gateway,
+ char *ipaddr,
+ char *gateway,
uint32_t *prefixLength,
- in_addr_t *dns1,
- in_addr_t *dns2,
- in_addr_t *server,
- uint32_t *lease)
+ char *dns1,
+ char *dns2,
+ char *server,
+ uint32_t *lease,
+ char *vendorInfo)
{
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";
- char daemon_suffix[MAX_DAEMON_SUFFIX];
+ char p2p_interface[MAX_INTERFACE_LENGTH];
- get_daemon_suffix(interface, daemon_suffix);
+ get_p2p_interface_replacement(interface, p2p_interface);
snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
DHCP_PROP_NAME_PREFIX,
- interface);
+ p2p_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,
- daemon_suffix, interface);
+ p2p_interface, interface);
memset(prop_value, '\0', PROPERTY_VALUE_MAX);
property_set(ctrl_prop, daemon_cmd);
@@ -333,8 +367,8 @@ int dhcp_do_request_renew(const char *interface,
return -1;
}
if (strcmp(prop_value, "ok") == 0) {
- fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease);
- return 0;
+ return fill_ip_info(interface, ipaddr, gateway, prefixLength,
+ dns1, dns2, server, lease, vendorInfo);
} else {
snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
return -1;