The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 21 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <arpa/inet.h> |
| 24 | #include <netinet/in.h> |
| 25 | |
| 26 | #include <cutils/properties.h> |
| 27 | |
Dmitry Shmidt | 9363b7d | 2009-12-14 15:41:54 -0800 | [diff] [blame] | 28 | static const char DAEMON_NAME[] = "dhcpcd"; |
| 29 | static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd"; |
| 30 | static const char HOSTNAME_PROP_NAME[] = "net.hostname"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | static const char DHCP_PROP_NAME_PREFIX[] = "dhcp"; |
Dmitry Shmidt | 62d6f74 | 2012-07-23 17:39:30 -0700 | [diff] [blame] | 32 | static const char DHCP_CONFIG_PATH[] = "/system/etc/dhcpcd/dhcpcd.conf"; |
Irfan Sheriff | b1723b6 | 2010-12-21 10:31:05 -0800 | [diff] [blame] | 33 | static const int NAP_TIME = 200; /* wait for 200ms at a time */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | /* when polling for property values */ |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 35 | static const char DAEMON_NAME_RENEW[] = "iprenew"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | static char errmsg[100]; |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 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 |
| 39 | * and other properties on a successful bind |
| 40 | */ |
| 41 | #define MAX_INTERFACE_LENGTH 25 |
| 42 | |
| 43 | /* |
| 44 | * P2p interface names increase sequentially p2p-p2p0-1, p2p-p2p0-2.. after |
| 45 | * group formation. This does not work well with system properties which can quickly |
| 46 | * exhaust or for specifiying a dhcp start target in init which requires |
| 47 | * interface to be pre-defined in init.rc file. |
| 48 | * |
| 49 | * This function returns a common string p2p for all p2p interfaces. |
| 50 | */ |
| 51 | void get_p2p_interface_replacement(const char *interface, char *p2p_interface) { |
| 52 | /* Use p2p for any interface starting with p2p. */ |
| 53 | if (strncmp(interface, "p2p",3) == 0) { |
| 54 | strncpy(p2p_interface, "p2p", MAX_INTERFACE_LENGTH); |
| 55 | } else { |
| 56 | strncpy(p2p_interface, interface, MAX_INTERFACE_LENGTH); |
| 57 | } |
| 58 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Wait for a system property to be assigned a specified value. |
| 62 | * If desired_value is NULL, then just wait for the property to |
| 63 | * be created with any value. maxwait is the maximum amount of |
| 64 | * time in seconds to wait before giving up. |
| 65 | */ |
| 66 | static int wait_for_property(const char *name, const char *desired_value, int maxwait) |
| 67 | { |
| 68 | char value[PROPERTY_VALUE_MAX] = {'\0'}; |
Irfan Sheriff | b1723b6 | 2010-12-21 10:31:05 -0800 | [diff] [blame] | 69 | int maxnaps = (maxwait * 1000) / NAP_TIME; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | |
| 71 | if (maxnaps < 1) { |
| 72 | maxnaps = 1; |
| 73 | } |
| 74 | |
daisuke niwa | c855bdd | 2015-02-27 09:49:39 +0100 | [diff] [blame] | 75 | while (maxnaps-- >= 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | if (property_get(name, value, NULL)) { |
| 77 | if (desired_value == NULL || |
| 78 | strcmp(value, desired_value) == 0) { |
| 79 | return 0; |
| 80 | } |
| 81 | } |
daisuke niwa | c855bdd | 2015-02-27 09:49:39 +0100 | [diff] [blame] | 82 | if (maxnaps >= 0) { |
| 83 | usleep(NAP_TIME * 1000); |
| 84 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | } |
| 86 | return -1; /* failure */ |
| 87 | } |
| 88 | |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 89 | static int fill_ip_info(const char *interface, |
| 90 | char *ipaddr, |
| 91 | char *gateway, |
| 92 | uint32_t *prefixLength, |
Robert Greenwalt | fdd5731 | 2013-01-09 16:22:12 -0800 | [diff] [blame] | 93 | char *dns[], |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 94 | char *server, |
Jeff Sharkey | 0fb8ec8 | 2012-04-18 21:54:55 -0700 | [diff] [blame] | 95 | uint32_t *lease, |
Robert Greenwalt | 6ecbdca | 2012-11-13 10:56:01 -0800 | [diff] [blame] | 96 | char *vendorInfo, |
Dmitry Shmidt | be06210 | 2013-07-24 17:37:05 -0700 | [diff] [blame] | 97 | char *domain, |
| 98 | char *mtu) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | { |
| 100 | char prop_name[PROPERTY_KEY_MAX]; |
| 101 | char prop_value[PROPERTY_VALUE_MAX]; |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 102 | /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */ |
| 103 | char p2p_interface[MAX_INTERFACE_LENGTH]; |
Robert Greenwalt | fdd5731 | 2013-01-09 16:22:12 -0800 | [diff] [blame] | 104 | int x; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 106 | get_p2p_interface_replacement(interface, p2p_interface); |
| 107 | |
| 108 | snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, p2p_interface); |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 109 | property_get(prop_name, ipaddr, NULL); |
| 110 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 111 | snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, p2p_interface); |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 112 | property_get(prop_name, gateway, NULL); |
| 113 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 114 | snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, p2p_interface); |
Irfan Sheriff | bdaaec1 | 2011-04-15 16:04:24 -0700 | [diff] [blame] | 115 | property_get(prop_name, server, NULL); |
| 116 | |
| 117 | //TODO: Handle IPv6 when we change system property usage |
Irfan Sheriff | 94cecfc | 2012-12-07 10:36:29 -0800 | [diff] [blame] | 118 | if (gateway[0] == '\0' || strncmp(gateway, "0.0.0.0", 7) == 0) { |
Irfan Sheriff | bdaaec1 | 2011-04-15 16:04:24 -0700 | [diff] [blame] | 119 | //DHCP server is our best bet as gateway |
| 120 | strncpy(gateway, server, PROPERTY_VALUE_MAX); |
| 121 | } |
| 122 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 123 | snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, p2p_interface); |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 124 | if (property_get(prop_name, prop_value, NULL)) { |
| 125 | int p; |
| 126 | // this conversion is v4 only, but this dhcp client is v4 only anyway |
| 127 | in_addr_t mask = ntohl(inet_addr(prop_value)); |
| 128 | // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for |
| 129 | // non 255.255.255.255 inputs. if we get that value check if it is legit.. |
| 130 | if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) { |
| 131 | snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value); |
| 132 | return -1; |
| 133 | } |
| 134 | for (p = 0; p < 32; p++) { |
| 135 | if (mask == 0) break; |
| 136 | // check for non-contiguous netmask, e.g., 255.254.255.0 |
| 137 | if ((mask & 0x80000000) == 0) { |
| 138 | snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value); |
| 139 | return -1; |
| 140 | } |
| 141 | mask = mask << 1; |
| 142 | } |
| 143 | *prefixLength = p; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 144 | } |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 145 | |
Robert Greenwalt | fdd5731 | 2013-01-09 16:22:12 -0800 | [diff] [blame] | 146 | for (x=0; dns[x] != NULL; x++) { |
| 147 | snprintf(prop_name, sizeof(prop_name), "%s.%s.dns%d", DHCP_PROP_NAME_PREFIX, p2p_interface, x+1); |
| 148 | property_get(prop_name, dns[x], NULL); |
| 149 | } |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 150 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 151 | snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, p2p_interface); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | if (property_get(prop_name, prop_value, NULL)) { |
| 153 | *lease = atol(prop_value); |
| 154 | } |
Jeff Sharkey | 0fb8ec8 | 2012-04-18 21:54:55 -0700 | [diff] [blame] | 155 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 156 | snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX, |
| 157 | p2p_interface); |
Jeff Sharkey | 0fb8ec8 | 2012-04-18 21:54:55 -0700 | [diff] [blame] | 158 | property_get(prop_name, vendorInfo, NULL); |
| 159 | |
Robert Greenwalt | 6ecbdca | 2012-11-13 10:56:01 -0800 | [diff] [blame] | 160 | snprintf(prop_name, sizeof(prop_name), "%s.%s.domain", DHCP_PROP_NAME_PREFIX, |
| 161 | p2p_interface); |
| 162 | property_get(prop_name, domain, NULL); |
| 163 | |
Dmitry Shmidt | be06210 | 2013-07-24 17:37:05 -0700 | [diff] [blame] | 164 | snprintf(prop_name, sizeof(prop_name), "%s.%s.mtu", DHCP_PROP_NAME_PREFIX, |
| 165 | p2p_interface); |
| 166 | property_get(prop_name, mtu, NULL); |
| 167 | |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 168 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Start the dhcp client daemon, and wait for it to finish |
| 173 | * configuring the interface. |
Irfan Sheriff | 35c2860 | 2011-11-09 11:10:50 -0800 | [diff] [blame] | 174 | * |
| 175 | * The device init.rc file needs a corresponding entry for this work. |
| 176 | * |
| 177 | * Example: |
Dmitry Shmidt | 62d6f74 | 2012-07-23 17:39:30 -0700 | [diff] [blame] | 178 | * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 | */ |
| 180 | int dhcp_do_request(const char *interface, |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 181 | char *ipaddr, |
| 182 | char *gateway, |
| 183 | uint32_t *prefixLength, |
Robert Greenwalt | fdd5731 | 2013-01-09 16:22:12 -0800 | [diff] [blame] | 184 | char *dns[], |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 185 | char *server, |
Jeff Sharkey | 0fb8ec8 | 2012-04-18 21:54:55 -0700 | [diff] [blame] | 186 | uint32_t *lease, |
Robert Greenwalt | 6ecbdca | 2012-11-13 10:56:01 -0800 | [diff] [blame] | 187 | char *vendorInfo, |
Dmitry Shmidt | be06210 | 2013-07-24 17:37:05 -0700 | [diff] [blame] | 188 | char *domain, |
| 189 | char *mtu) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 190 | { |
| 191 | char result_prop_name[PROPERTY_KEY_MAX]; |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 192 | char daemon_prop_name[PROPERTY_KEY_MAX]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; |
Dmitry Shmidt | 62d6f74 | 2012-07-23 17:39:30 -0700 | [diff] [blame] | 194 | char daemon_cmd[PROPERTY_VALUE_MAX * 2 + sizeof(DHCP_CONFIG_PATH)]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | const char *ctrl_prop = "ctl.start"; |
| 196 | const char *desired_status = "running"; |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 197 | /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */ |
| 198 | char p2p_interface[MAX_INTERFACE_LENGTH]; |
repo sync | a329b42 | 2011-07-29 12:07:34 -0700 | [diff] [blame] | 199 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 200 | get_p2p_interface_replacement(interface, p2p_interface); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 201 | |
| 202 | snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", |
| 203 | DHCP_PROP_NAME_PREFIX, |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 204 | p2p_interface); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 205 | |
| 206 | snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", |
| 207 | DAEMON_PROP_NAME, |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 208 | p2p_interface); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 209 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | /* Erase any previous setting of the dhcp result property */ |
| 211 | property_set(result_prop_name, ""); |
| 212 | |
| 213 | /* Start the daemon and wait until it's ready */ |
Dmitry Shmidt | 9363b7d | 2009-12-14 15:41:54 -0800 | [diff] [blame] | 214 | if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0')) |
Dmitry Shmidt | 62d6f74 | 2012-07-23 17:39:30 -0700 | [diff] [blame] | 215 | snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s -h %s %s", DAEMON_NAME, |
| 216 | p2p_interface, DHCP_CONFIG_PATH, prop_value, interface); |
Dmitry Shmidt | 9363b7d | 2009-12-14 15:41:54 -0800 | [diff] [blame] | 217 | else |
Dmitry Shmidt | 62d6f74 | 2012-07-23 17:39:30 -0700 | [diff] [blame] | 218 | snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s %s", DAEMON_NAME, |
Matt Gumbel | ba2ba5c | 2013-01-04 09:53:42 -0800 | [diff] [blame] | 219 | p2p_interface, DHCP_CONFIG_PATH, interface); |
Dmitry Shmidt | 9363b7d | 2009-12-14 15:41:54 -0800 | [diff] [blame] | 220 | memset(prop_value, '\0', PROPERTY_VALUE_MAX); |
| 221 | property_set(ctrl_prop, daemon_cmd); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 222 | if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start"); |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | /* Wait for the daemon to return a result */ |
| 228 | if (wait_for_property(result_prop_name, NULL, 30) < 0) { |
| 229 | snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish"); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | if (!property_get(result_prop_name, prop_value, NULL)) { |
| 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) { |
Robert Greenwalt | fdd5731 | 2013-01-09 16:22:12 -0800 | [diff] [blame] | 239 | if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns, |
Dmitry Shmidt | be06210 | 2013-07-24 17:37:05 -0700 | [diff] [blame] | 240 | server, lease, vendorInfo, domain, mtu) == -1) { |
Robert Greenwalt | faab26d | 2011-01-14 14:35:42 -0800 | [diff] [blame] | 241 | return -1; |
| 242 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | return 0; |
| 244 | } else { |
| 245 | snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value); |
| 246 | return -1; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Stop the DHCP client daemon. |
| 252 | */ |
| 253 | int dhcp_stop(const char *interface) |
| 254 | { |
| 255 | char result_prop_name[PROPERTY_KEY_MAX]; |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 256 | char daemon_prop_name[PROPERTY_KEY_MAX]; |
| 257 | char daemon_cmd[PROPERTY_VALUE_MAX * 2]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | const char *ctrl_prop = "ctl.stop"; |
| 259 | const char *desired_status = "stopped"; |
| 260 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 261 | char p2p_interface[MAX_INTERFACE_LENGTH]; |
repo sync | a329b42 | 2011-07-29 12:07:34 -0700 | [diff] [blame] | 262 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 263 | get_p2p_interface_replacement(interface, p2p_interface); |
repo sync | a329b42 | 2011-07-29 12:07:34 -0700 | [diff] [blame] | 264 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 265 | snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", |
| 266 | DHCP_PROP_NAME_PREFIX, |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 267 | p2p_interface); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 268 | |
| 269 | snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", |
| 270 | DAEMON_PROP_NAME, |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 271 | p2p_interface); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 272 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 273 | snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 274 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 275 | /* Stop the daemon and wait until it's reported to be stopped */ |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 276 | property_set(ctrl_prop, daemon_cmd); |
| 277 | if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 278 | return -1; |
| 279 | } |
| 280 | property_set(result_prop_name, "failed"); |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Release the current DHCP client lease. |
| 286 | */ |
| 287 | int dhcp_release_lease(const char *interface) |
| 288 | { |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 289 | char daemon_prop_name[PROPERTY_KEY_MAX]; |
| 290 | char daemon_cmd[PROPERTY_VALUE_MAX * 2]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 291 | const char *ctrl_prop = "ctl.stop"; |
| 292 | const char *desired_status = "stopped"; |
| 293 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 294 | char p2p_interface[MAX_INTERFACE_LENGTH]; |
repo sync | a329b42 | 2011-07-29 12:07:34 -0700 | [diff] [blame] | 295 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 296 | get_p2p_interface_replacement(interface, p2p_interface); |
repo sync | a329b42 | 2011-07-29 12:07:34 -0700 | [diff] [blame] | 297 | |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 298 | snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", |
| 299 | DAEMON_PROP_NAME, |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 300 | p2p_interface); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 301 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 302 | snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 303 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 304 | /* Stop the daemon and wait until it's reported to be stopped */ |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 305 | property_set(ctrl_prop, daemon_cmd); |
| 306 | if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | return -1; |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | char *dhcp_get_errmsg() { |
| 313 | return errmsg; |
| 314 | } |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 315 | |
| 316 | /** |
Irfan Sheriff | 35c2860 | 2011-11-09 11:10:50 -0800 | [diff] [blame] | 317 | * The device init.rc file needs a corresponding entry. |
| 318 | * |
| 319 | * Example: |
| 320 | * service iprenew_<interface> /system/bin/dhcpcd -n |
| 321 | * |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 322 | */ |
| 323 | int dhcp_do_request_renew(const char *interface, |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 324 | char *ipaddr, |
| 325 | char *gateway, |
tk.mun | ed21633 | 2011-10-13 22:58:55 +0900 | [diff] [blame] | 326 | uint32_t *prefixLength, |
Robert Greenwalt | fdd5731 | 2013-01-09 16:22:12 -0800 | [diff] [blame] | 327 | char *dns[], |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 328 | char *server, |
Jeff Sharkey | 0fb8ec8 | 2012-04-18 21:54:55 -0700 | [diff] [blame] | 329 | uint32_t *lease, |
Robert Greenwalt | 6ecbdca | 2012-11-13 10:56:01 -0800 | [diff] [blame] | 330 | char *vendorInfo, |
Dmitry Shmidt | be06210 | 2013-07-24 17:37:05 -0700 | [diff] [blame] | 331 | char *domain, |
| 332 | char *mtu) |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 333 | { |
| 334 | char result_prop_name[PROPERTY_KEY_MAX]; |
| 335 | char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; |
| 336 | char daemon_cmd[PROPERTY_VALUE_MAX * 2]; |
| 337 | const char *ctrl_prop = "ctl.start"; |
| 338 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 339 | char p2p_interface[MAX_INTERFACE_LENGTH]; |
repo sync | a329b42 | 2011-07-29 12:07:34 -0700 | [diff] [blame] | 340 | |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 341 | get_p2p_interface_replacement(interface, p2p_interface); |
repo sync | a329b42 | 2011-07-29 12:07:34 -0700 | [diff] [blame] | 342 | |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 343 | snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", |
| 344 | DHCP_PROP_NAME_PREFIX, |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 345 | p2p_interface); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 346 | |
| 347 | /* Erase any previous setting of the dhcp result property */ |
| 348 | property_set(result_prop_name, ""); |
| 349 | |
| 350 | /* Start the renew daemon and wait until it's ready */ |
repo sync | a329b42 | 2011-07-29 12:07:34 -0700 | [diff] [blame] | 351 | snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW, |
Irfan Sheriff | 89f58cf | 2012-05-23 10:30:05 -0700 | [diff] [blame] | 352 | p2p_interface, interface); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 353 | memset(prop_value, '\0', PROPERTY_VALUE_MAX); |
| 354 | property_set(ctrl_prop, daemon_cmd); |
| 355 | |
| 356 | /* Wait for the daemon to return a result */ |
| 357 | if (wait_for_property(result_prop_name, NULL, 30) < 0) { |
| 358 | snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish"); |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | if (!property_get(result_prop_name, prop_value, NULL)) { |
| 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) { |
Colin Cross | 0a0a876 | 2013-06-10 15:06:43 -0700 | [diff] [blame] | 368 | return fill_ip_info(interface, ipaddr, gateway, prefixLength, dns, |
Dmitry Shmidt | be06210 | 2013-07-24 17:37:05 -0700 | [diff] [blame] | 369 | server, lease, vendorInfo, domain, mtu); |
TK MUN | 9d15787 | 2011-02-23 18:58:36 +0900 | [diff] [blame] | 370 | } else { |
| 371 | snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value); |
| 372 | return -1; |
| 373 | } |
| 374 | } |