• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <errno.h>
18 #include <fcntl.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 #include <sys/socket.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 
28 #include <dirent.h>
29 
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 
33 #define LOG_TAG "PppController"
34 #include <log/log.h>
35 
36 #include "PppController.h"
37 
PppController()38 PppController::PppController() {
39     mTtys = new TtyCollection();
40     mPid = 0;
41 }
42 
~PppController()43 PppController::~PppController() {
44     TtyCollection::iterator it;
45 
46     for (it = mTtys->begin(); it != mTtys->end(); ++it) {
47         free(*it);
48     }
49     mTtys->clear();
50 }
51 
attachPppd(const char * tty,struct in_addr local,struct in_addr remote,struct in_addr dns1,struct in_addr dns2)52 int 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         ALOGE("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         ALOGE("Invalid tty '%s' specified", tty);
71         errno = -EINVAL;
72         return -1;
73     }
74 
75     if ((pid = fork()) < 0) {
76         ALOGE("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         free(l);
90         free(r);
91 
92         snprintf(dev, sizeof(dev), "/dev/%s", tty);
93 
94         // TODO: Deal with pppd bailing out after 99999 seconds of being started
95         // but not getting a connection
96         if (execl("/system/bin/pppd", "/system/bin/pppd", "-detach", dev, "115200",
97                   lr, "ms-dns", d1, "ms-dns", d2, "lcp-max-configure", "99999", (char *) nullptr)) {
98             ALOGE("execl failed (%s)", strerror(errno));
99         }
100         free(lr);
101         free(d1);
102         free(d2);
103         ALOGE("Should never get here!");
104         return 0;
105     } else {
106         mPid = pid;
107     }
108     return 0;
109 }
110 
detachPppd(const char * tty)111 int PppController::detachPppd(const char *tty) {
112 
113     if (mPid == 0) {
114         ALOGE("PPPD already stopped");
115         return 0;
116     }
117 
118     ALOGD("Stopping PPPD services on port %s", tty);
119     kill(mPid, SIGTERM);
120     waitpid(mPid, nullptr, 0);
121     mPid = 0;
122     ALOGD("PPPD services on port %s stopped", tty);
123     return 0;
124 }
125 
getTtyList()126 TtyCollection *PppController::getTtyList() {
127     updateTtyList();
128     return mTtys;
129 }
130 
updateTtyList()131 int PppController::updateTtyList() {
132     TtyCollection::iterator it;
133 
134     for (it = mTtys->begin(); it != mTtys->end(); ++it) {
135         free(*it);
136     }
137     mTtys->clear();
138 
139     DIR *d = opendir("/sys/class/tty");
140     if (!d) {
141         ALOGE("Error opening /sys/class/tty (%s)", strerror(errno));
142         return -1;
143     }
144 
145     struct dirent *de;
146     while ((de = readdir(d))) {
147         if (de->d_name[0] == '.')
148             continue;
149         if ((!strncmp(de->d_name, "tty", 3)) && (strlen(de->d_name) > 3)) {
150             mTtys->push_back(strdup(de->d_name));
151         }
152     }
153     closedir(d);
154     return 0;
155 }
156