blob: 064eb0cdc87c9da2a6740b7577c79b28faccf470 [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>
21#include <unistd.h>
22#include <arpa/inet.h>
23#include <netinet/in.h>
24
25#include <cutils/properties.h>
26
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -080027static const char DAEMON_NAME[] = "dhcpcd";
28static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd";
29static const char HOSTNAME_PROP_NAME[] = "net.hostname";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030static const char DHCP_PROP_NAME_PREFIX[] = "dhcp";
TK MUN72380722011-03-20 11:42:49 +090031static const char DAEMON_NAME_RENEW[] = "iprenew";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032static 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
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,
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];
TK MUN5b638842011-02-23 18:58:36 +0900141 char daemon_prop_name[PROPERTY_KEY_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800143 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 const char *ctrl_prop = "ctl.start";
145 const char *desired_status = "running";
146
147 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
148 DHCP_PROP_NAME_PREFIX,
149 interface);
TK MUN5b638842011-02-23 18:58:36 +0900150
151 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
152 DAEMON_PROP_NAME,
153 interface);
154
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155 /* Erase any previous setting of the dhcp result property */
156 property_set(result_prop_name, "");
157
158 /* Start the daemon and wait until it's ready */
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800159 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
TK MUN5b638842011-02-23 18:58:36 +0900160 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, interface,
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800161 prop_value, interface);
162 else
TK MUN5b638842011-02-23 18:58:36 +0900163 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, interface, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800164 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
165 property_set(ctrl_prop, daemon_cmd);
TK MUN5b638842011-02-23 18:58:36 +0900166 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
168 return -1;
169 }
170
171 /* Wait for the daemon to return a result */
172 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
173 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
174 return -1;
175 }
176
177 if (!property_get(result_prop_name, prop_value, NULL)) {
178 /* shouldn't ever happen, given the success of wait_for_property() */
179 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
180 return -1;
181 }
182 if (strcmp(prop_value, "ok") == 0) {
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400183 char dns_prop_name[PROPERTY_KEY_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease);
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400185 /* copy the dhcp.XXX.dns properties to net.XXX.dns */
186 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
187 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
188 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
189 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 return 0;
191 } else {
192 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
193 return -1;
194 }
195}
196
197/**
198 * Stop the DHCP client daemon.
199 */
200int dhcp_stop(const char *interface)
201{
202 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN5b638842011-02-23 18:58:36 +0900203 char daemon_prop_name[PROPERTY_KEY_MAX];
204 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 const char *ctrl_prop = "ctl.stop";
206 const char *desired_status = "stopped";
207
208 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
209 DHCP_PROP_NAME_PREFIX,
210 interface);
TK MUN5b638842011-02-23 18:58:36 +0900211
212 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
213 DAEMON_PROP_NAME,
214 interface);
215
216 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface);
217
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN5b638842011-02-23 18:58:36 +0900219 property_set(ctrl_prop, daemon_cmd);
220 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 return -1;
222 }
223 property_set(result_prop_name, "failed");
224 return 0;
225}
226
227/**
228 * Release the current DHCP client lease.
229 */
230int dhcp_release_lease(const char *interface)
231{
TK MUN5b638842011-02-23 18:58:36 +0900232 char daemon_prop_name[PROPERTY_KEY_MAX];
233 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 const char *ctrl_prop = "ctl.stop";
235 const char *desired_status = "stopped";
236
TK MUN5b638842011-02-23 18:58:36 +0900237 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
238 DAEMON_PROP_NAME,
239 interface);
240
241 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface);
242
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN5b638842011-02-23 18:58:36 +0900244 property_set(ctrl_prop, daemon_cmd);
245 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 return -1;
247 }
248 return 0;
249}
250
251char *dhcp_get_errmsg() {
252 return errmsg;
253}
TK MUN5b638842011-02-23 18:58:36 +0900254
255/**
256 * Run WiMAX dhcp renew service.
257 * "wimax_renew" service shoud be included in init.rc.
258 */
259int dhcp_do_request_renew(const char *interface,
260 in_addr_t *ipaddr,
261 in_addr_t *gateway,
262 in_addr_t *mask,
263 in_addr_t *dns1,
264 in_addr_t *dns2,
265 in_addr_t *server,
266 uint32_t *lease)
267{
268 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN5b638842011-02-23 18:58:36 +0900269 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
270 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
271 const char *ctrl_prop = "ctl.start";
TK MUN5b638842011-02-23 18:58:36 +0900272
273 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
TK MUN72380722011-03-20 11:42:49 +0900274 DHCP_PROP_NAME_PREFIX,
TK MUN5b638842011-02-23 18:58:36 +0900275 interface);
276
277 /* Erase any previous setting of the dhcp result property */
278 property_set(result_prop_name, "");
279
280 /* Start the renew daemon and wait until it's ready */
281 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW, interface, interface);
282 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
283 property_set(ctrl_prop, daemon_cmd);
TK MUN5b638842011-02-23 18:58:36 +0900284
285 /* Wait for the daemon to return a result */
286 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
287 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
288 return -1;
289 }
290
291 if (!property_get(result_prop_name, prop_value, NULL)) {
292 /* shouldn't ever happen, given the success of wait_for_property() */
293 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
294 return -1;
295 }
296 if (strcmp(prop_value, "ok") == 0) {
297 fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease);
298 return 0;
299 } else {
300 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
301 return -1;
302 }
TK MUN9badcf72011-03-09 19:47:47 -0800303}