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>
34 #include "TetherController.h"
36 TetherController::TetherController() {
37 mInterfaces = new InterfaceCollection();
38 mDnsForwarders = new NetAddressCollection();
43 TetherController::~TetherController() {
44 InterfaceCollection::iterator it;
46 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
51 mDnsForwarders->clear();
54 int TetherController::setIpFwdEnabled(bool enable) {
56 LOGD("Setting IP forward enable = %d", enable);
57 int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY);
59 LOGE("Failed to open ip_forward (%s)", strerror(errno));
63 if (write(fd, (enable ? "1" : "0"), 1) != 1) {
64 LOGE("Failed to write ip_forward (%s)", strerror(errno));
72 bool TetherController::getIpFwdEnabled() {
73 int fd = open("/proc/sys/net/ipv4/ip_forward", O_RDONLY);
76 LOGE("Failed to open ip_forward (%s)", strerror(errno));
81 if (read(fd, &enabled, 1) != 1) {
82 LOGE("Failed to read ip_forward (%s)", strerror(errno));
88 return (enabled == '1' ? true : false);
91 int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
92 if (mDaemonPid != 0) {
93 LOGE("Tethering already started");
98 LOGD("Starting tethering services");
103 if (pipe(pipefd) < 0) {
104 LOGE("pipe failed (%s)", strerror(errno));
109 * TODO: Create a monitoring thread to handle and restart
110 * the daemon if it exits prematurely
112 if ((pid = fork()) < 0) {
113 LOGE("fork failed (%s)", strerror(errno));
121 if (pipefd[0] != STDIN_FILENO) {
122 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
123 LOGE("dup2 failed (%s)", strerror(errno));
129 int num_processed_args = 4 + (num_addrs/2) + 1; // 1 null for termination
130 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
131 args[num_processed_args - 1] = NULL;
132 args[0] = (char *)"/system/bin/dnsmasq";
133 args[1] = (char *)"--no-daemon";
134 args[2] = (char *)"--no-resolv";
135 args[3] = (char *)"--no-poll";
138 for (int addrIndex=0; addrIndex < num_addrs;) {
139 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
140 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
141 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
144 if (execv(args[0], args)) {
145 LOGE("execl failed (%s)", strerror(errno));
147 LOGE("Should never get here!");
153 mDaemonFd = pipefd[1];
154 LOGD("Tethering services running");
160 int TetherController::stopTethering() {
162 if (mDaemonPid == 0) {
163 LOGE("Tethering already stopped");
167 LOGD("Stopping tethering services");
169 kill(mDaemonPid, SIGTERM);
170 waitpid(mDaemonPid, NULL, 0);
174 LOGD("Tethering services stopped");
178 bool TetherController::isTetheringStarted() {
179 return (mDaemonPid == 0 ? false : true);
182 #define MAX_CMD_SIZE 1024
184 int TetherController::setDnsForwarders(char **servers, int numServers) {
186 char daemonCmd[MAX_CMD_SIZE];
188 strcpy(daemonCmd, "update_dns");
189 int cmdLen = strlen(daemonCmd);
191 mDnsForwarders->clear();
192 for (i = 0; i < numServers; i++) {
193 LOGD("setDnsForwarders(%d = '%s')", i, servers[i]);
197 if (!inet_aton(servers[i], &a)) {
198 LOGE("Failed to parse DNS server '%s'", servers[i]);
199 mDnsForwarders->clear();
203 cmdLen += strlen(servers[i]);
204 if (cmdLen + 2 >= MAX_CMD_SIZE) {
205 LOGD("Too many DNS servers listed");
209 strcat(daemonCmd, ":");
210 strcat(daemonCmd, servers[i]);
211 mDnsForwarders->push_back(a);
214 if (mDaemonFd != -1) {
215 LOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
216 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
217 LOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
218 mDnsForwarders->clear();
225 NetAddressCollection *TetherController::getDnsForwarders() {
226 return mDnsForwarders;
229 int TetherController::tetherInterface(const char *interface) {
230 mInterfaces->push_back(strdup(interface));
234 int TetherController::untetherInterface(const char *interface) {
235 InterfaceCollection::iterator it;
237 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
238 if (!strcmp(interface, *it)) {
240 mInterfaces->erase(it);
248 InterfaceCollection *TetherController::getTetheredInterfaceList() {