blob: 030e67725c35acbe937553f7436af588a1ca0fde [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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>
Olivier Baillyb93e5812010-11-17 11:47:23 -080021#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <unistd.h>
23#include <arpa/inet.h>
24#include <netinet/in.h>
25
26#include <cutils/properties.h>
27
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -080028static const char DAEMON_NAME[] = "dhcpcd";
29static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd";
30static const char HOSTNAME_PROP_NAME[] = "net.hostname";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031static const char DHCP_PROP_NAME_PREFIX[] = "dhcp";
Irfan Sheriffb1723b62010-12-21 10:31:05 -080032static const int NAP_TIME = 200; /* wait for 200ms at a time */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033 /* 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'};
Irfan Sheriffb1723b62010-12-21 10:31:05 -080045 int maxnaps = (maxwait * 1000) / NAP_TIME;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
47 if (maxnaps < 1) {
48 maxnaps = 1;
49 }
50
51 while (maxnaps-- > 0) {
Irfan Sheriffb1723b62010-12-21 10:31:05 -080052 usleep(NAP_TIME * 1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053 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
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080063static int fill_ip_info(const char *interface,
64 char *ipaddr,
65 char *gateway,
66 uint32_t *prefixLength,
67 char *dns1,
68 char *dns2,
69 char *server,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070 uint32_t *lease)
71{
72 char prop_name[PROPERTY_KEY_MAX];
73 char prop_value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
75 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080076 property_get(prop_name, ipaddr, NULL);
77
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080079 property_get(prop_name, gateway, NULL);
80
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080082 if (property_get(prop_name, prop_value, NULL)) {
83 int p;
84 // this conversion is v4 only, but this dhcp client is v4 only anyway
85 in_addr_t mask = ntohl(inet_addr(prop_value));
86 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for
87 // non 255.255.255.255 inputs. if we get that value check if it is legit..
88 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
89 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
90 return -1;
91 }
92 for (p = 0; p < 32; p++) {
93 if (mask == 0) break;
94 // check for non-contiguous netmask, e.g., 255.254.255.0
95 if ((mask & 0x80000000) == 0) {
96 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
97 return -1;
98 }
99 mask = mask << 1;
100 }
101 *prefixLength = p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 }
103 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800104 property_get(prop_name, dns1, NULL);
105
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800107 property_get(prop_name, dns2, NULL);
108
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800110 property_get(prop_name, server, NULL);
111
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface);
113 if (property_get(prop_name, prop_value, NULL)) {
114 *lease = atol(prop_value);
115 }
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800116 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117}
118
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400119static 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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127/*
128 * Start the dhcp client daemon, and wait for it to finish
129 * configuring the interface.
130 */
131int dhcp_do_request(const char *interface,
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800132 char *ipaddr,
133 char *gateway,
134 uint32_t *prefixLength,
135 char *dns1,
136 char *dns2,
137 char *server,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 uint32_t *lease)
139{
140 char result_prop_name[PROPERTY_KEY_MAX];
141 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800142 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 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 */
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800153 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);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 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) {
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400177 char dns_prop_name[PROPERTY_KEY_MAX];
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800178 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease)
179 == -1) {
180 return -1;
181 }
182
183 /* copy dns data to system properties - TODO - remove this after we have async
184 * notification of renewal's */
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400185 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
186 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
187 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
188 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 return 0;
190 } else {
191 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
192 return -1;
193 }
194}
195
196/**
197 * Stop the DHCP client daemon.
198 */
199int dhcp_stop(const char *interface)
200{
201 char result_prop_name[PROPERTY_KEY_MAX];
202 const char *ctrl_prop = "ctl.stop";
203 const char *desired_status = "stopped";
204
205 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
206 DHCP_PROP_NAME_PREFIX,
207 interface);
208 /* Stop the daemon and wait until it's reported to be stopped */
209 property_set(ctrl_prop, DAEMON_NAME);
210 if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) {
211 return -1;
212 }
213 property_set(result_prop_name, "failed");
214 return 0;
215}
216
217/**
218 * Release the current DHCP client lease.
219 */
220int dhcp_release_lease(const char *interface)
221{
222 const char *ctrl_prop = "ctl.stop";
223 const char *desired_status = "stopped";
224
225 /* Stop the daemon and wait until it's reported to be stopped */
226 property_set(ctrl_prop, DAEMON_NAME);
227 if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) {
228 return -1;
229 }
230 return 0;
231}
232
233char *dhcp_get_errmsg() {
234 return errmsg;
235}