Loading libnetutils/dhcp_utils.c +92 −10 Original line number Diff line number Diff line Loading @@ -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]; /* Loading Loading @@ -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; Loading Loading @@ -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); Loading Loading @@ -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"; Loading @@ -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; } Loading Loading @@ -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"); Loading @@ -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; Loading @@ -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; } } Loading
libnetutils/dhcp_utils.c +92 −10 Original line number Diff line number Diff line Loading @@ -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]; /* Loading Loading @@ -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; Loading Loading @@ -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); Loading Loading @@ -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"; Loading @@ -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; } Loading Loading @@ -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"); Loading @@ -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; Loading @@ -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; } }