2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/socket.h>
24 #include <sys/types.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
30 #define LOG_TAG "TetherController"
31 #include <cutils/log.h>
32 #include <cutils/properties.h>
34 #include "TetherController.h"
36 TetherController::TetherController() {
37 mInterfaces = new InterfaceCollection();
38 mDnsForwarders = new NetAddressCollection();
44 TetherController::~TetherController() {
45 InterfaceCollection::iterator it;
47 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
52 mDnsForwarders->clear();
55 int TetherController::setIpFwdEnabled(bool enable) {
57 ALOGD("Setting IP forward enable = %d", enable);
59 // In BP tools mode, do not disable IP forwarding
60 char bootmode[PROPERTY_VALUE_MAX] = {0};
61 property_get("ro.bootmode", bootmode, "unknown");
62 if ((enable == false) && (0 == strcmp("bp-tools", bootmode))) {
66 int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY);
68 ALOGE("Failed to open ip_forward (%s)", strerror(errno));
72 if (write(fd, (enable ? "1" : "0"), 1) != 1) {
73 ALOGE("Failed to write ip_forward (%s)", strerror(errno));
81 bool TetherController::getIpFwdEnabled() {
82 int fd = open("/proc/sys/net/ipv4/ip_forward", O_RDONLY);
85 ALOGE("Failed to open ip_forward (%s)", strerror(errno));
90 if (read(fd, &enabled, 1) != 1) {
91 ALOGE("Failed to read ip_forward (%s)", strerror(errno));
97 return (enabled == '1' ? true : false);
100 int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
101 if (mDaemonPid != 0) {
102 ALOGE("Tethering already started");
107 ALOGD("Starting tethering services");
112 if (pipe(pipefd) < 0) {
113 ALOGE("pipe failed (%s)", strerror(errno));
118 * TODO: Create a monitoring thread to handle and restart
119 * the daemon if it exits prematurely
121 if ((pid = fork()) < 0) {
122 ALOGE("fork failed (%s)", strerror(errno));
130 if (pipefd[0] != STDIN_FILENO) {
131 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
132 ALOGE("dup2 failed (%s)", strerror(errno));
138 int num_processed_args = 7 + (num_addrs/2) + 1; // 1 null for termination
139 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
140 args[num_processed_args - 1] = NULL;
141 args[0] = (char *)"/system/bin/dnsmasq";
142 args[1] = (char *)"--keep-in-foreground";
143 args[2] = (char *)"--no-resolv";
144 args[3] = (char *)"--no-poll";
145 // TODO: pipe through metered status from ConnService
146 args[4] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
147 args[5] = (char *)"--pid-file";
148 args[6] = (char *)"";
151 for (int addrIndex=0; addrIndex < num_addrs;) {
152 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
153 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
154 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
157 if (execv(args[0], args)) {
158 ALOGE("execl failed (%s)", strerror(errno));
160 ALOGE("Should never get here!");
165 mDaemonFd = pipefd[1];
166 ALOGD("Tethering services running");
172 int TetherController::stopTethering() {
174 if (mDaemonPid == 0) {
175 ALOGE("Tethering already stopped");
179 ALOGD("Stopping tethering services");
181 kill(mDaemonPid, SIGTERM);
182 waitpid(mDaemonPid, NULL, 0);
186 ALOGD("Tethering services stopped");
191 int TetherController::startReverseTethering(const char* iface) {
192 if (mDhcpcdPid != 0) {
193 ALOGE("Reverse tethering already started");
198 ALOGD("TetherController::startReverseTethering, Starting reverse tethering");
201 * TODO: Create a monitoring thread to handle and restart
202 * the daemon if it exits prematurely
204 //cleanup the dhcp result
205 char dhcp_result_name[64];
206 snprintf(dhcp_result_name, sizeof(dhcp_result_name) - 1, "dhcp.%s.result", iface);
207 property_set(dhcp_result_name, "");
210 if ((pid = fork()) < 0) {
211 ALOGE("fork failed (%s)", strerror(errno));
219 args[argc++] = "/system/bin/dhcpcd";
221 if (property_get("net.hostname", host_name, NULL) && (host_name[0] != '\0'))
224 args[argc++] = host_name;
226 args[argc++] = (char*)iface;
228 if (execv(args[0], args)) {
229 ALOGE("startReverseTethering, execv failed (%s)", strerror(errno));
231 ALOGE("startReverseTethering, Should never get here!");
232 // TODO(BT) inform parent of the failure.
233 // Parent process need wait for child to report error status
234 // before it set mDhcpcdPid and return 0.
238 ALOGD("Reverse Tethering running, pid:%d", pid);
244 int TetherController::stopReverseTethering() {
246 if (mDhcpcdPid == 0) {
247 ALOGE("Tethering already stopped");
251 ALOGD("Stopping tethering services");
253 kill(mDhcpcdPid, SIGTERM);
254 waitpid(mDhcpcdPid, NULL, 0);
256 ALOGD("Tethering services stopped");
259 bool TetherController::isTetheringStarted() {
260 return (mDaemonPid == 0 ? false : true);
263 #define MAX_CMD_SIZE 1024
265 int TetherController::setDnsForwarders(char **servers, int numServers) {
267 char daemonCmd[MAX_CMD_SIZE];
269 strcpy(daemonCmd, "update_dns");
270 int cmdLen = strlen(daemonCmd);
272 mDnsForwarders->clear();
273 for (i = 0; i < numServers; i++) {
274 ALOGD("setDnsForwarders(%d = '%s')", i, servers[i]);
278 if (!inet_aton(servers[i], &a)) {
279 ALOGE("Failed to parse DNS server '%s'", servers[i]);
280 mDnsForwarders->clear();
284 cmdLen += (strlen(servers[i]) + 1);
285 if (cmdLen + 1 >= MAX_CMD_SIZE) {
286 ALOGD("Too many DNS servers listed");
290 strcat(daemonCmd, ":");
291 strcat(daemonCmd, servers[i]);
292 mDnsForwarders->push_back(a);
295 if (mDaemonFd != -1) {
296 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
297 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
298 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
299 mDnsForwarders->clear();
306 NetAddressCollection *TetherController::getDnsForwarders() {
307 return mDnsForwarders;
310 int TetherController::tetherInterface(const char *interface) {
311 mInterfaces->push_back(strdup(interface));
315 int TetherController::untetherInterface(const char *interface) {
316 InterfaceCollection::iterator it;
318 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
319 if (!strcmp(interface, *it)) {
321 mInterfaces->erase(it);
329 InterfaceCollection *TetherController::getTetheredInterfaceList() {