PppController.cpp revision d5573d34c8fac49e16b20cf144486125bf940086
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 "PppController"
26#include <cutils/log.h>
27
28#include "PppController.h"
29
30extern "C" int logwrap(int argc, const char **argv, int background);
31
32static char IPTABLES_PATH[] = "/system/bin/iptables";
33
34PppController::PppController() {
35    mTtys = new TtyCollection();
36    mPid = 0;
37}
38
39PppController::~PppController() {
40    TtyCollection::iterator it;
41
42    for (it = mTtys->begin(); it != mTtys->end(); ++it) {
43        free(*it);
44    }
45    mTtys->clear();
46}
47
48int PppController::attachPppd(const char *tty, struct in_addr local,
49                              struct in_addr remote) {
50    pid_t pid;
51
52    if (mPid) {
53        LOGE("Multiple PPPD instances not currently supported");
54        errno = EBUSY;
55        return -1;
56    }
57
58    if ((pid = fork()) < 0) {
59        LOGE("fork failed (%s)", strerror(errno));
60        return -1;
61    }
62
63    if (!pid) {
64        char *l = strdup(inet_ntoa(local));
65        char *r = strdup(inet_ntoa(remote));
66        char dev[32];
67        char *lr;
68
69        asprintf(&lr, "%s:%s", l, r);
70
71        snprintf(dev, sizeof(dev), "/dev/%s", tty); // TODO: STOPSHIP Validate this
72
73        // TODO: Deal with pppd bailing out after 99999 seconds of being started
74        // but not getting a connection
75        if (execl("/system/bin/pppd", "/system/bin/pppd", "-detach", dev,
76                  "115200", lr, "debug", "lcp-max-configure", "99999", (char *) NULL)) {
77            LOGE("execl failed (%s)", strerror(errno));
78        }
79        LOGE("Should never get here!");
80        return 0;
81    } else {
82        mPid = pid;
83    }
84    return 0;
85}
86
87int PppController::detachPppd(const char *tty) {
88
89    if (mPid == 0) {
90        LOGE("PPPD already stopped");
91        return 0;
92    }
93
94    LOGD("Stopping PPPD services on port %s", tty);
95
96    kill(mPid, SIGTERM);
97    mPid = 0;
98    return 0;
99}
100
101TtyCollection *PppController::getTtyList() {
102    return mTtys;
103}
104
105