TetherController.cpp revision 9d10b341a0ba46f108cb96e46691197d778cbc06
1/*
2 * Copyright (C) 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#include <stdlib.h>
18#include <errno.h>
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24
25#define LOG_TAG "TetherController"
26#include <cutils/log.h>
27
28
29#include "TetherController.h"
30
31TetherController::TetherController() {
32    mInterfaces = new InterfaceCollection();
33    mDnsForwarders = new NetAddressCollection();
34    mDaemonFd = -1;
35    mDaemonPid = 0;
36}
37
38TetherController::~TetherController() {
39    InterfaceCollection::iterator it;
40
41    for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
42        free(*it);
43    }
44    mInterfaces->clear();
45
46    mDnsForwarders->clear();
47}
48
49int TetherController::setIpFwdEnabled(bool enable) {
50
51    LOGD("Setting IP forward enable = %d", enable);
52    int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY);
53    if (fd < 0) {
54        LOGE("Failed to open ip_forward (%s)", strerror(errno));
55        return -1;
56    }
57
58    if (write(fd, (enable ? "1" : "0"), 1) != 1) {
59        LOGE("Failed to write ip_forward (%s)", strerror(errno));
60        return -1;
61    }
62    close(fd);
63    return 0;
64}
65
66bool TetherController::getIpFwdEnabled() {
67    int fd = open("/proc/sys/net/ipv4/ip_forward", O_RDONLY);
68
69    if (fd < 0) {
70        LOGE("Failed to open ip_forward (%s)", strerror(errno));
71        return false;
72    }
73
74    char enabled;
75    if (read(fd, &enabled, 1) != 1) {
76        LOGE("Failed to read ip_forward (%s)", strerror(errno));
77        return -1;
78    }
79
80    close(fd);
81
82    return (enabled  == '1' ? true : false);
83}
84
85int TetherController::startTethering(struct in_addr dhcpStart, struct in_addr dhcpEnd) {
86
87    if (mDaemonPid != 0) {
88        LOGE("Tethering already started");
89        errno = EBUSY;
90        return -1;
91    }
92
93    LOGD("Starting tethering services");
94
95    pid_t pid;
96    int pipefd[2];
97
98    if (pipe(pipefd) < 0) {
99        LOGE("pipe failed (%s)", strerror(errno));
100        return -1;
101    }
102
103    /*
104     * TODO: Create a monitoring thread to handle and restart
105     * the daemon if it exits prematurely
106     */
107    if ((pid = fork()) < 0) {
108        LOGE("fork failed (%s)", strerror(errno));
109        close(pipefd[0]);
110        close(pipefd[1]);
111        return -1;
112    }
113
114    if (!pid) {
115        close(pipefd[1]);
116        if (pipefd[0] != STDIN_FILENO) {
117            if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
118                LOGE("dup2 failed (%s)", strerror(errno));
119                return -1;
120            }
121            close(pipefd[0]);
122        }
123        char *start = strdup(inet_ntoa(dhcpStart));
124        char *end = strdup(inet_ntoa(dhcpEnd));
125        char *range;
126
127        asprintf(&range, "--dhcp-range=%s,%s,1h", start, end);
128
129        if (execl("/system/bin/dnsmasq", "/system/bin/dnsmasq", "--no-daemon", "--no-resolv",
130                  "--no-poll", "--no-hosts", range, (char *) NULL)) {
131            LOGE("execl failed (%s)", strerror(errno));
132        }
133        LOGE("Should never get here!");
134        return 0;
135    } else {
136        close(pipefd[0]);
137        mDaemonPid = pid;
138        mDaemonFd = pipefd[1];
139        LOGD("Tethering services running");
140    }
141
142    return 0;
143}
144
145int TetherController::stopTethering() {
146
147    if (mDaemonPid == 0) {
148        LOGE("Tethering already stopped");
149        return 0;
150    }
151
152    LOGD("Stopping tethering services");
153
154    kill(mDaemonPid, SIGTERM);
155    mDaemonPid = 0;
156    close(mDaemonFd);
157    mDaemonFd = -1;
158    return 0;
159}
160
161bool TetherController::isTetheringStarted() {
162    return (mDaemonPid == 0 ? false : true);
163}
164
165int TetherController::setDnsForwarders(char **servers, int numServers) {
166    int i;
167    char daemonCmd[1024];
168
169    strcpy(daemonCmd, "update_dns");
170
171    mDnsForwarders->clear();
172    for (i = 0; i < numServers; i++) {
173        LOGD("setDnsForwarders(%d = '%s')", i, servers[i]);
174
175        struct in_addr a;
176
177        if (!inet_aton(servers[i], &a)) {
178            LOGE("Failed to parse DNS server '%s'", servers[i]);
179            mDnsForwarders->clear();
180            return -1;
181        }
182        strcat(daemonCmd, ":");
183        strcat(daemonCmd, servers[i]);
184        mDnsForwarders->push_back(a);
185    }
186
187    if (mDaemonFd != -1) {
188        LOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
189        if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
190            LOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
191            mDnsForwarders->clear();
192            return -1;
193        }
194    }
195    return 0;
196}
197
198NetAddressCollection *TetherController::getDnsForwarders() {
199    return mDnsForwarders;
200}
201
202int TetherController::tetherInterface(const char *interface) {
203    mInterfaces->push_back(strdup(interface));
204    return 0;
205}
206
207int TetherController::untetherInterface(const char *interface) {
208    InterfaceCollection::iterator it;
209
210    for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
211        if (!strcmp(interface, *it)) {
212            free(*it);
213            mInterfaces->erase(it);
214            return 0;
215        }
216    }
217    errno = ENOENT;
218    return -1;
219}
220
221InterfaceCollection *TetherController::getTetheredInterfaceList() {
222    return mInterfaces;
223}
224
225