summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'libnetutils')
-rw-r--r--libnetutils/dhcp_utils.c112
1 files changed, 55 insertions, 57 deletions
diff --git a/libnetutils/dhcp_utils.c b/libnetutils/dhcp_utils.c
index 70e37c676..c6b9fe491 100644
--- a/libnetutils/dhcp_utils.c
+++ b/libnetutils/dhcp_utils.c
@@ -1,16 +1,16 @@
1/* 1/*
2 * Copyright 2008, The Android Open Source Project 2 * Copyright 2008, The Android Open Source Project
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with 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 6 * You may obtain a copy of the License at
7 * 7 *
8 * http://www.apache.org/licenses/LICENSE-2.0 8 * http://www.apache.org/licenses/LICENSE-2.0
9 * 9 *
10 * Unless required by applicable law or agreed to in writing, software 10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, 11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
@@ -33,7 +33,7 @@ static const char DHCP_CONFIG_PATH[] = "/system/etc/dhcpcd/dhcpcd.conf";
33static const int NAP_TIME = 200; /* wait for 200ms at a time */ 33static const int NAP_TIME = 200; /* wait for 200ms at a time */
34 /* when polling for property values */ 34 /* when polling for property values */
35static const char DAEMON_NAME_RENEW[] = "iprenew"; 35static const char DAEMON_NAME_RENEW[] = "iprenew";
36static char errmsg[100]; 36static char errmsg[100] = "\0";
37/* interface length for dhcpcd daemon start (dhcpcd_<interface> as defined in init.rc file) 37/* interface length for dhcpcd daemon start (dhcpcd_<interface> as defined in init.rc file)
38 * or for filling up system properties dhcpcd.<interface>.ipaddress, dhcpcd.<interface>.dns1 38 * or for filling up system properties dhcpcd.<interface>.ipaddress, dhcpcd.<interface>.dns1
39 * and other properties on a successful bind 39 * and other properties on a successful bind
@@ -74,7 +74,7 @@ static int wait_for_property(const char *name, const char *desired_value, int ma
74 74
75 while (maxnaps-- >= 0) { 75 while (maxnaps-- >= 0) {
76 if (property_get(name, value, NULL)) { 76 if (property_get(name, value, NULL)) {
77 if (desired_value == NULL || 77 if (desired_value == NULL ||
78 strcmp(value, desired_value) == 0) { 78 strcmp(value, desired_value) == 0) {
79 return 0; 79 return 0;
80 } 80 }
@@ -169,6 +169,47 @@ static int fill_ip_info(const char *interface,
169} 169}
170 170
171/* 171/*
172 * Get any available DHCP results.
173 */
174int dhcp_get_results(const char *interface,
175 char *ipaddr,
176 char *gateway,
177 uint32_t *prefixLength,
178 char *dns[],
179 char *server,
180 uint32_t *lease,
181 char *vendorInfo,
182 char *domain,
183 char *mtu)
184{
185 char result_prop_name[PROPERTY_KEY_MAX];
186 char prop_value[PROPERTY_VALUE_MAX];
187
188 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
189 char p2p_interface[MAX_INTERFACE_LENGTH];
190 get_p2p_interface_replacement(interface, p2p_interface);
191 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
192 DHCP_PROP_NAME_PREFIX,
193 p2p_interface);
194
195 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
196 if (!property_get(result_prop_name, prop_value, NULL)) {
197 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
198 return -1;
199 }
200 if (strcmp(prop_value, "ok") == 0) {
201 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
202 server, lease, vendorInfo, domain, mtu) == -1) {
203 return -1;
204 }
205 return 0;
206 } else {
207 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
208 return -1;
209 }
210}
211
212/*
172 * Start the dhcp client daemon, and wait for it to finish 213 * Start the dhcp client daemon, and wait for it to finish
173 * configuring the interface. 214 * configuring the interface.
174 * 215 *
@@ -177,16 +218,7 @@ static int fill_ip_info(const char *interface,
177 * Example: 218 * Example:
178 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf 219 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf
179 */ 220 */
180int dhcp_do_request(const char *interface, 221int dhcp_start(const char *interface)
181 char *ipaddr,
182 char *gateway,
183 uint32_t *prefixLength,
184 char *dns[],
185 char *server,
186 uint32_t *lease,
187 char *vendorInfo,
188 char *domain,
189 char *mtu)
190{ 222{
191 char result_prop_name[PROPERTY_KEY_MAX]; 223 char result_prop_name[PROPERTY_KEY_MAX];
192 char daemon_prop_name[PROPERTY_KEY_MAX]; 224 char daemon_prop_name[PROPERTY_KEY_MAX];
@@ -230,21 +262,7 @@ int dhcp_do_request(const char *interface,
230 return -1; 262 return -1;
231 } 263 }
232 264
233 if (!property_get(result_prop_name, prop_value, NULL)) { 265 return 0;
234 /* shouldn't ever happen, given the success of wait_for_property() */
235 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
236 return -1;
237 }
238 if (strcmp(prop_value, "ok") == 0) {
239 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
240 server, lease, vendorInfo, domain, mtu) == -1) {
241 return -1;
242 }
243 return 0;
244 } else {
245 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
246 return -1;
247 }
248} 266}
249 267
250/** 268/**
@@ -320,16 +338,7 @@ char *dhcp_get_errmsg() {
320 * service iprenew_<interface> /system/bin/dhcpcd -n 338 * service iprenew_<interface> /system/bin/dhcpcd -n
321 * 339 *
322 */ 340 */
323int dhcp_do_request_renew(const char *interface, 341int dhcp_start_renew(const char *interface)
324 char *ipaddr,
325 char *gateway,
326 uint32_t *prefixLength,
327 char *dns[],
328 char *server,
329 uint32_t *lease,
330 char *vendorInfo,
331 char *domain,
332 char *mtu)
333{ 342{
334 char result_prop_name[PROPERTY_KEY_MAX]; 343 char result_prop_name[PROPERTY_KEY_MAX];
335 char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; 344 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
@@ -359,16 +368,5 @@ int dhcp_do_request_renew(const char *interface,
359 return -1; 368 return -1;
360 } 369 }
361 370
362 if (!property_get(result_prop_name, prop_value, NULL)) { 371 return 0;
363 /* shouldn't ever happen, given the success of wait_for_property() */
364 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
365 return -1;
366 }
367 if (strcmp(prop_value, "ok") == 0) {
368 return fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
369 server, lease, vendorInfo, domain, mtu);
370 } else {
371 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
372 return -1;
373 }
374} 372}