summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Kline2015-02-19 04:15:34 -0600
committerErik Kline2015-03-01 21:26:53 -0600
commitc2291a7ed34edeae230a57607a964209f6932357 (patch)
tree3c0d422f98a6f7fde8a17b920e91f927bd8f5a8a /libnetutils
parent317b4024a2a46b8c57abfa08f2a649df13572bd3 (diff)
downloadplatform-system-core-c2291a7ed34edeae230a57607a964209f6932357.tar.gz
platform-system-core-c2291a7ed34edeae230a57607a964209f6932357.tar.xz
platform-system-core-c2291a7ed34edeae230a57607a964209f6932357.zip
Refactor dhcp_utils for more flexible uses.
Separate out fetching DHCP results from starting the daemon (for either discover or renew). Renamed dhcp_do_request() to dhcp_start(). Renamed dhcp_do_request_renew() to dhcp_start_renew(). Added new dhcp_get_results(). Callers of the prior versions of dhcp_do_request* now separately call dhcp_get_results(). Bug: 19422416 Change-Id: I05447853d444015e6014661dda1bb177b58fe463
Diffstat (limited to 'libnetutils')
-rw-r--r--libnetutils/dhcp_utils.c95
1 files changed, 46 insertions, 49 deletions
diff --git a/libnetutils/dhcp_utils.c b/libnetutils/dhcp_utils.c
index e1df87481..9b7bd8d0c 100644
--- a/libnetutils/dhcp_utils.c
+++ b/libnetutils/dhcp_utils.c
@@ -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] = "";
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
@@ -175,6 +175,47 @@ static const char *ipaddr_to_string(in_addr_t addr)
175} 175}
176 176
177/* 177/*
178 * Get any available DHCP results.
179 */
180int dhcp_get_results(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{
191 char result_prop_name[PROPERTY_KEY_MAX];
192 char prop_value[PROPERTY_VALUE_MAX];
193
194 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
195 char p2p_interface[MAX_INTERFACE_LENGTH];
196 get_p2p_interface_replacement(interface, p2p_interface);
197 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
198 DHCP_PROP_NAME_PREFIX,
199 p2p_interface);
200
201 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
202 if (!property_get(result_prop_name, prop_value, NULL)) {
203 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
204 return -1;
205 }
206 if (strcmp(prop_value, "ok") == 0) {
207 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
208 server, lease, vendorInfo, domain, mtu) == -1) {
209 return -1;
210 }
211 return 0;
212 } else {
213 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
214 return -1;
215 }
216}
217
218/*
178 * Start the dhcp client daemon, and wait for it to finish 219 * Start the dhcp client daemon, and wait for it to finish
179 * configuring the interface. 220 * configuring the interface.
180 * 221 *
@@ -183,16 +224,7 @@ static const char *ipaddr_to_string(in_addr_t addr)
183 * Example: 224 * Example:
184 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf 225 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf
185 */ 226 */
186int dhcp_do_request(const char *interface, 227int dhcp_start(const char *interface)
187 char *ipaddr,
188 char *gateway,
189 uint32_t *prefixLength,
190 char *dns[],
191 char *server,
192 uint32_t *lease,
193 char *vendorInfo,
194 char *domain,
195 char *mtu)
196{ 228{
197 char result_prop_name[PROPERTY_KEY_MAX]; 229 char result_prop_name[PROPERTY_KEY_MAX];
198 char daemon_prop_name[PROPERTY_KEY_MAX]; 230 char daemon_prop_name[PROPERTY_KEY_MAX];
@@ -236,22 +268,7 @@ int dhcp_do_request(const char *interface,
236 return -1; 268 return -1;
237 } 269 }
238 270
239 if (!property_get(result_prop_name, prop_value, NULL)) { 271 return 0;
240 /* shouldn't ever happen, given the success of wait_for_property() */
241 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
242 return -1;
243 }
244 if (strcmp(prop_value, "ok") == 0) {
245 char dns_prop_name[PROPERTY_KEY_MAX];
246 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
247 server, lease, vendorInfo, domain, mtu) == -1) {
248 return -1;
249 }
250 return 0;
251 } else {
252 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
253 return -1;
254 }
255} 272}
256 273
257/** 274/**
@@ -327,16 +344,7 @@ char *dhcp_get_errmsg() {
327 * service iprenew_<interface> /system/bin/dhcpcd -n 344 * service iprenew_<interface> /system/bin/dhcpcd -n
328 * 345 *
329 */ 346 */
330int dhcp_do_request_renew(const char *interface, 347int dhcp_start_renew(const char *interface)
331 char *ipaddr,
332 char *gateway,
333 uint32_t *prefixLength,
334 char *dns[],
335 char *server,
336 uint32_t *lease,
337 char *vendorInfo,
338 char *domain,
339 char *mtu)
340{ 348{
341 char result_prop_name[PROPERTY_KEY_MAX]; 349 char result_prop_name[PROPERTY_KEY_MAX];
342 char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; 350 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
@@ -366,16 +374,5 @@ int dhcp_do_request_renew(const char *interface,
366 return -1; 374 return -1;
367 } 375 }
368 376
369 if (!property_get(result_prop_name, prop_value, NULL)) { 377 return 0;
370 /* shouldn't ever happen, given the success of wait_for_property() */
371 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
372 return -1;
373 }
374 if (strcmp(prop_value, "ok") == 0) {
375 return fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
376 server, lease, vendorInfo, domain, mtu);
377 } else {
378 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
379 return -1;
380 }
381} 378}