/** * 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, 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] = {'\0'}; char daemon_cmd[PROPERTY_VALUE_MAX * 2]; const char *ctrl_prop = "ctl.start"; 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); /* 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, p2p_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) { 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; } }
/* * 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 #ifdef SAMSUNG_WIFI_ROAMING ,uint32_t *roaming #endif ) { 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]; #ifdef SAMSUNG_OXYGEN_NETWORK char result_interface[MAX_INTERFACE_LENGTH]; #endif get_p2p_interface_replacement(interface, p2p_interface); #ifdef SAMSUNG_OXYGEN_NETWORK if (get_ibss_interface_replacement(interface, p2p_interface) > 0) { strncpy(result_interface, p2p_interface, MAX_INTERFACE_LENGTH); } else { strncpy(result_interface, interface, MAX_INTERFACE_LENGTH); } #endif 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( #ifdef SAMSUNG_OXYGEN_NETWORK result_interface, #else interface, #endif ipaddr, gateway, prefixLength, dns, server, lease, vendorInfo, domain, mtu #ifdef SAMSUNG_WIFI_ROAMING , roaming #endif ) == -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. */ int dhcp_do_request(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 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"; const char *desired_status = "running"; char daemon_suffix[MAX_DAEMON_SUFFIX]; get_daemon_suffix(interface, daemon_suffix); 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, daemon_suffix); /* 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); else snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, daemon_suffix, 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) { snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start"); return -1; } /* 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 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 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, 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); property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : ""); 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. * * 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, 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 daemon_prop_name[PROPERTY_KEY_MAX]; char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; char daemon_cmd[PROPERTY_VALUE_MAX * 2 + sizeof(DHCP_CONFIG_PATH)]; const char *ctrl_prop = "ctl.start"; const char *desired_status = "running"; /* 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); snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", DAEMON_PROP_NAME, p2p_interface); /* Erase any previous setting of the dhcp result property */ property_set(result_prop_name, ""); /* HACK: Sometimes the lease file appears to be corrupted, which can cause * unknown errors while obtaining an address. We just remove the corresponding * lease file prior to starting the daemon... */ { char lease_file_path[PROPERTY_VALUE_MAX]; snprintf(lease_file_path, sizeof(lease_file_path), "/data/misc/dhcp/dhcpcd-%s.lease", p2p_interface); unlink(lease_file_path); } /* 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:-f %s -h %s %s", DAEMON_NAME, p2p_interface, DHCP_CONFIG_PATH, prop_value, interface); else 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) { snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start"); return -1; } /* 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 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 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; } }