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