dhcp_utils.c revision c88e09cb98fb3690c3cf49f5a825532e0d9bf300
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 <unistd.h> 22#include <arpa/inet.h> 23#include <netinet/in.h> 24 25#include <cutils/properties.h> 26 27static const char DAEMON_NAME[] = "dhcpcd"; 28static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd"; 29static const char DHCP_PROP_NAME_PREFIX[] = "dhcp"; 30static const int NAP_TIME = 1; /* wait for 1 second at a time */ 31 /* when polling for property values */ 32static char errmsg[100]; 33 34/* 35 * Wait for a system property to be assigned a specified value. 36 * If desired_value is NULL, then just wait for the property to 37 * be created with any value. maxwait is the maximum amount of 38 * time in seconds to wait before giving up. 39 */ 40static int wait_for_property(const char *name, const char *desired_value, int maxwait) 41{ 42 char value[PROPERTY_VALUE_MAX] = {'\0'}; 43 int maxnaps = maxwait / NAP_TIME; 44 45 if (maxnaps < 1) { 46 maxnaps = 1; 47 } 48 49 while (maxnaps-- > 0) { 50 usleep(1000000); 51 if (property_get(name, value, NULL)) { 52 if (desired_value == NULL || 53 strcmp(value, desired_value) == 0) { 54 return 0; 55 } 56 } 57 } 58 return -1; /* failure */ 59} 60 61static void fill_ip_info(const char *interface, 62 in_addr_t *ipaddr, 63 in_addr_t *gateway, 64 in_addr_t *mask, 65 in_addr_t *dns1, 66 in_addr_t *dns2, 67 in_addr_t *server, 68 uint32_t *lease) 69{ 70 char prop_name[PROPERTY_KEY_MAX]; 71 char prop_value[PROPERTY_VALUE_MAX]; 72 struct in_addr addr; 73 in_addr_t iaddr; 74 75 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface); 76 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 77 *ipaddr = addr.s_addr; 78 } else { 79 *ipaddr = 0; 80 } 81 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface); 82 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 83 *gateway = addr.s_addr; 84 } else { 85 *gateway = 0; 86 } 87 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface); 88 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 89 *mask = addr.s_addr; 90 } else { 91 *mask = 0; 92 } 93 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface); 94 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 95 *dns1 = addr.s_addr; 96 } else { 97 *dns1 = 0; 98 } 99 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface); 100 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 101 *dns2 = addr.s_addr; 102 } else { 103 *dns2 = 0; 104 } 105 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface); 106 if (property_get(prop_name, prop_value, NULL) && inet_aton(prop_value, &addr)) { 107 *server = addr.s_addr; 108 } else { 109 *server = 0; 110 } 111 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface); 112 if (property_get(prop_name, prop_value, NULL)) { 113 *lease = atol(prop_value); 114 } 115} 116 117static const char *ipaddr_to_string(in_addr_t addr) 118{ 119 struct in_addr in_addr; 120 121 in_addr.s_addr = addr; 122 return inet_ntoa(in_addr); 123} 124 125/* 126 * Start the dhcp client daemon, and wait for it to finish 127 * configuring the interface. 128 */ 129int dhcp_do_request(const char *interface, 130 in_addr_t *ipaddr, 131 in_addr_t *gateway, 132 in_addr_t *mask, 133 in_addr_t *dns1, 134 in_addr_t *dns2, 135 in_addr_t *server, 136 uint32_t *lease) 137{ 138 char result_prop_name[PROPERTY_KEY_MAX]; 139 char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; 140 const char *ctrl_prop = "ctl.start"; 141 const char *desired_status = "running"; 142 143 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", 144 DHCP_PROP_NAME_PREFIX, 145 interface); 146 /* Erase any previous setting of the dhcp result property */ 147 property_set(result_prop_name, ""); 148 149 /* Start the daemon and wait until it's ready */ 150 property_set(ctrl_prop, DAEMON_NAME); 151 if (wait_for_property(DAEMON_PROP_NAME, desired_status, 10) < 0) { 152 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start"); 153 return -1; 154 } 155 156 /* Wait for the daemon to return a result */ 157 if (wait_for_property(result_prop_name, NULL, 30) < 0) { 158 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish"); 159 return -1; 160 } 161 162 if (!property_get(result_prop_name, prop_value, NULL)) { 163 /* shouldn't ever happen, given the success of wait_for_property() */ 164 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set"); 165 return -1; 166 } 167 if (strcmp(prop_value, "ok") == 0) { 168 char dns_prop_name[PROPERTY_KEY_MAX]; 169 fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease); 170 /* copy the dhcp.XXX.dns properties to net.XXX.dns */ 171 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface); 172 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : ""); 173 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface); 174 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : ""); 175 return 0; 176 } else { 177 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value); 178 return -1; 179 } 180} 181 182/** 183 * Stop the DHCP client daemon. 184 */ 185int dhcp_stop(const char *interface) 186{ 187 char result_prop_name[PROPERTY_KEY_MAX]; 188 const char *ctrl_prop = "ctl.stop"; 189 const char *desired_status = "stopped"; 190 191 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", 192 DHCP_PROP_NAME_PREFIX, 193 interface); 194 /* Stop the daemon and wait until it's reported to be stopped */ 195 property_set(ctrl_prop, DAEMON_NAME); 196 if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) { 197 return -1; 198 } 199 property_set(result_prop_name, "failed"); 200 return 0; 201} 202 203/** 204 * Release the current DHCP client lease. 205 */ 206int dhcp_release_lease(const char *interface) 207{ 208 const char *ctrl_prop = "ctl.stop"; 209 const char *desired_status = "stopped"; 210 211 /* Stop the daemon and wait until it's reported to be stopped */ 212 property_set(ctrl_prop, DAEMON_NAME); 213 if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) { 214 return -1; 215 } 216 return 0; 217} 218 219char *dhcp_get_errmsg() { 220 return errmsg; 221} 222