dhcp_utils.c revision b93e5812faffd3b6c5fb349072413aace31918d8
1/* 2 * Copyright 2008, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17/* Utilities for managing the dhcpcd DHCP client daemon */ 18 19#include <stdio.h> 20#include <stdlib.h> 21#include <string.h> 22#include <unistd.h> 23#include <arpa/inet.h> 24#include <netinet/in.h> 25 26#include <cutils/properties.h> 27 28static const char DAEMON_NAME[] = "dhcpcd"; 29static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd"; 30static const char HOSTNAME_PROP_NAME[] = "net.hostname"; 31static const char DHCP_PROP_NAME_PREFIX[] = "dhcp"; 32static const int NAP_TIME = 1; /* wait for 1 second at a time */ 33 /* when polling for property values */ 34static char errmsg[100]; 35 36/* 37 * Wait for a system property to be assigned a specified value. 38 * If desired_value is NULL, then just wait for the property to 39 * be created with any value. maxwait is the maximum amount of 40 * time in seconds to wait before giving up. 41 */ 42static int wait_for_property(const char *name, const char *desired_value, int maxwait) 43{ 44 char value[PROPERTY_VALUE_MAX] = {'\0'}; 45 int maxnaps = maxwait / NAP_TIME; 46 47 if (maxnaps < 1) { 48 maxnaps = 1; 49 } 50 51 while (maxnaps-- > 0) { 52 usleep(1000000); 53 if (property_get(name, value, NULL)) { 54 if (desired_value == NULL || 55 strcmp(value, desired_value) == 0) { 56 return 0; 57 } 58 } 59 } 60 return -1; /* failure */ 61} 62 63static void fill_ip_info(const char *interface, 64 in_addr_t *ipaddr, 65 in_addr_t *gateway, 66 in_addr_t *mask, 67 in_addr_t *dns1, 68 in_addr_t *dns2, 69 in_addr_t *server, 70 uint32_t *lease) 71{ 72 char prop_name[PROPERTY_KEY_MAX]; 73 char prop_value[PROPERTY_VALUE_MAX]; 74 struct in_addr addr; 75 in_addr_t iaddr; 76 77 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface); 78 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 79 *ipaddr = addr.s_addr; 80 } else { 81 *ipaddr = 0; 82 } 83 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface); 84 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 85 *gateway = addr.s_addr; 86 } else { 87 *gateway = 0; 88 } 89 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface); 90 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 91 *mask = addr.s_addr; 92 } else { 93 *mask = 0; 94 } 95 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface); 96 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 97 *dns1 = addr.s_addr; 98 } else { 99 *dns1 = 0; 100 } 101 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface); 102 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 103 *dns2 = addr.s_addr; 104 } else { 105 *dns2 = 0; 106 } 107 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface); 108 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 109 *server = addr.s_addr; 110 } else { 111 *server = 0; 112 } 113 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface); 114 if (property_get(prop_name, prop_value, NULL)) { 115 *lease = atol(prop_value); 116 } 117} 118 119static const char *ipaddr_to_string(in_addr_t addr) 120{ 121 struct in_addr in_addr; 122 123 in_addr.s_addr = addr; 124 return inet_ntoa(in_addr); 125} 126 127/* 128 * Start the dhcp client daemon, and wait for it to finish 129 * configuring the interface. 130 */ 131int dhcp_do_request(const char *interface, 132 in_addr_t *ipaddr, 133 in_addr_t *gateway, 134 in_addr_t *mask, 135 in_addr_t *dns1, 136 in_addr_t *dns2, 137 in_addr_t *server, 138 uint32_t *lease) 139{ 140 char result_prop_name[PROPERTY_KEY_MAX]; 141 char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; 142 char daemon_cmd[PROPERTY_VALUE_MAX * 2]; 143 const char *ctrl_prop = "ctl.start"; 144 const char *desired_status = "running"; 145 146 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", 147 DHCP_PROP_NAME_PREFIX, 148 interface); 149 /* Erase any previous setting of the dhcp result property */ 150 property_set(result_prop_name, ""); 151 152 /* Start the daemon and wait until it's ready */ 153 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0')) 154 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s:-h %s %s", DAEMON_NAME, 155 prop_value, interface); 156 else 157 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s:%s", DAEMON_NAME, interface); 158 memset(prop_value, '\0', PROPERTY_VALUE_MAX); 159 property_set(ctrl_prop, daemon_cmd); 160 if (wait_for_property(DAEMON_PROP_NAME, desired_status, 10) < 0) { 161 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start"); 162 return -1; 163 } 164 165 /* Wait for the daemon to return a result */ 166 if (wait_for_property(result_prop_name, NULL, 30) < 0) { 167 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish"); 168 return -1; 169 } 170 171 if (!property_get(result_prop_name, prop_value, NULL)) { 172 /* shouldn't ever happen, given the success of wait_for_property() */ 173 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set"); 174 return -1; 175 } 176 if (strcmp(prop_value, "ok") == 0) { 177 char dns_prop_name[PROPERTY_KEY_MAX]; 178 fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease); 179 /* copy the dhcp.XXX.dns properties to net.XXX.dns */ 180 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface); 181 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : ""); 182 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface); 183 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : ""); 184 return 0; 185 } else { 186 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value); 187 return -1; 188 } 189} 190 191/** 192 * Stop the DHCP client daemon. 193 */ 194int dhcp_stop(const char *interface) 195{ 196 char result_prop_name[PROPERTY_KEY_MAX]; 197 const char *ctrl_prop = "ctl.stop"; 198 const char *desired_status = "stopped"; 199 200 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", 201 DHCP_PROP_NAME_PREFIX, 202 interface); 203 /* Stop the daemon and wait until it's reported to be stopped */ 204 property_set(ctrl_prop, DAEMON_NAME); 205 if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) { 206 return -1; 207 } 208 property_set(result_prop_name, "failed"); 209 return 0; 210} 211 212/** 213 * Release the current DHCP client lease. 214 */ 215int dhcp_release_lease(const char *interface) 216{ 217 const char *ctrl_prop = "ctl.stop"; 218 const char *desired_status = "stopped"; 219 220 /* Stop the daemon and wait until it's reported to be stopped */ 221 property_set(ctrl_prop, DAEMON_NAME); 222 if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) { 223 return -1; 224 } 225 return 0; 226} 227 228char *dhcp_get_errmsg() { 229 return errmsg; 230} 231