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