OSDN Git Service

Merge "softap: Add hostapd support if BOARD_HOSTAPD_DRIVER is set"
[android-x86/system-netd.git] / PppController.cpp
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 <fcntl.h>
20 #include <string.h>
21
22 #include <sys/socket.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26
27 #include <dirent.h>
28
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31
32 #define LOG_TAG "PppController"
33 #include <cutils/log.h>
34
35 #include "PppController.h"
36
37 extern "C" int logwrap(int argc, const char **argv, int background);
38
39 PppController::PppController() {
40     mTtys = new TtyCollection();
41     mPid = 0;
42 }
43
44 PppController::~PppController() {
45     TtyCollection::iterator it;
46
47     for (it = mTtys->begin(); it != mTtys->end(); ++it) {
48         free(*it);
49     }
50     mTtys->clear();
51 }
52
53 int PppController::attachPppd(const char *tty, struct in_addr local,
54                               struct in_addr remote, struct in_addr dns1,
55                               struct in_addr dns2) {
56     pid_t pid;
57
58     if (mPid) {
59         LOGE("Multiple PPPD instances not currently supported");
60         errno = EBUSY;
61         return -1;
62     }
63
64     TtyCollection::iterator it;
65     for (it = mTtys->begin(); it != mTtys->end(); ++it) {
66         if (!strcmp(tty, *it)) {
67             break;
68         }
69     }
70     if (it == mTtys->end()) {
71         LOGE("Invalid tty '%s' specified", tty);
72         errno = -EINVAL;
73         return -1;
74     }
75
76     if ((pid = fork()) < 0) {
77         LOGE("fork failed (%s)", strerror(errno));
78         return -1;
79     }
80
81     if (!pid) {
82         char *l = strdup(inet_ntoa(local));
83         char *r = strdup(inet_ntoa(remote));
84         char *d1 = strdup(inet_ntoa(dns1));
85         char *d2 = strdup(inet_ntoa(dns2));
86         char dev[32];
87         char *lr;
88
89         asprintf(&lr, "%s:%s", l, r);
90
91         snprintf(dev, sizeof(dev), "/dev/%s", tty);
92
93         // TODO: Deal with pppd bailing out after 99999 seconds of being started
94         // but not getting a connection
95         if (execl("/system/bin/pppd", "/system/bin/pppd", "-detach", dev, "115200",
96                   lr, "ms-dns", d1, "ms-dns", d2, "lcp-max-configure", "99999", (char *) NULL)) {
97             LOGE("execl failed (%s)", strerror(errno));
98         }
99         LOGE("Should never get here!");
100         return 0;
101     } else {
102         mPid = pid;
103     }
104     return 0;
105 }
106
107 int PppController::detachPppd(const char *tty) {
108
109     if (mPid == 0) {
110         LOGE("PPPD already stopped");
111         return 0;
112     }
113
114     LOGD("Stopping PPPD services on port %s", tty);
115     kill(mPid, SIGTERM);
116     waitpid(mPid, NULL, 0);
117     mPid = 0;
118     LOGD("PPPD services on port %s stopped", tty);
119     return 0;
120 }
121
122 TtyCollection *PppController::getTtyList() {
123     updateTtyList();
124     return mTtys;
125 }
126
127 int PppController::updateTtyList() {
128     TtyCollection::iterator it;
129
130     for (it = mTtys->begin(); it != mTtys->end(); ++it) {
131         free(*it);
132     }
133     mTtys->clear();
134
135     DIR *d = opendir("/sys/class/tty");
136     if (!d) {
137         LOGE("Error opening /sys/class/tty (%s)", strerror(errno));
138         return -1;
139     }
140
141     struct dirent *de;
142     while ((de = readdir(d))) {
143         if (de->d_name[0] == '.')
144             continue;
145         if ((!strncmp(de->d_name, "tty", 3)) && (strlen(de->d_name) > 3)) {
146             mTtys->push_back(strdup(de->d_name));
147         }
148     }
149     closedir(d);
150     return 0;
151 }