blob: 80a36e953033ddd520b1eddff7fc6c281ebbc8ed [file] [log] [blame]
San Mehatd5573d32010-01-19 17:19:41 -08001/*
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>
San Mehat18817622010-01-21 09:23:06 -080019#include <fcntl.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080020#include <string.h>
San Mehat18817622010-01-21 09:23:06 -080021
San Mehatd5573d32010-01-19 17:19:41 -080022#include <sys/socket.h>
23#include <sys/stat.h>
San Mehat18817622010-01-21 09:23:06 -080024#include <sys/types.h>
25#include <sys/wait.h>
26
27#include <dirent.h>
28
San Mehatd5573d32010-01-19 17:19:41 -080029#include <netinet/in.h>
30#include <arpa/inet.h>
31
32#define LOG_TAG "PppController"
Logan Chien3f461482018-04-23 14:31:32 +080033#include <log/log.h>
San Mehatd5573d32010-01-19 17:19:41 -080034
35#include "PppController.h"
36
San Mehatd5573d32010-01-19 17:19:41 -080037PppController::PppController() {
38 mTtys = new TtyCollection();
39 mPid = 0;
40}
41
42PppController::~PppController() {
43 TtyCollection::iterator it;
44
45 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
46 free(*it);
47 }
48 mTtys->clear();
49}
50
51int PppController::attachPppd(const char *tty, struct in_addr local,
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080052 struct in_addr remote, struct in_addr dns1,
53 struct in_addr dns2) {
San Mehatd5573d32010-01-19 17:19:41 -080054 pid_t pid;
55
56 if (mPid) {
Steve Block5ea0c052012-01-06 19:18:11 +000057 ALOGE("Multiple PPPD instances not currently supported");
San Mehatd5573d32010-01-19 17:19:41 -080058 errno = EBUSY;
59 return -1;
60 }
61
San Mehat18817622010-01-21 09:23:06 -080062 TtyCollection::iterator it;
63 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
64 if (!strcmp(tty, *it)) {
65 break;
66 }
67 }
68 if (it == mTtys->end()) {
Steve Block5ea0c052012-01-06 19:18:11 +000069 ALOGE("Invalid tty '%s' specified", tty);
San Mehat18817622010-01-21 09:23:06 -080070 errno = -EINVAL;
71 return -1;
72 }
73
San Mehatd5573d32010-01-19 17:19:41 -080074 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000075 ALOGE("fork failed (%s)", strerror(errno));
San Mehatd5573d32010-01-19 17:19:41 -080076 return -1;
77 }
78
79 if (!pid) {
80 char *l = strdup(inet_ntoa(local));
81 char *r = strdup(inet_ntoa(remote));
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080082 char *d1 = strdup(inet_ntoa(dns1));
83 char *d2 = strdup(inet_ntoa(dns2));
San Mehatd5573d32010-01-19 17:19:41 -080084 char dev[32];
85 char *lr;
86
87 asprintf(&lr, "%s:%s", l, r);
Jesper Hanssona9d791f2012-04-27 13:54:27 +020088 free(l);
89 free(r);
San Mehatd5573d32010-01-19 17:19:41 -080090
San Mehat18817622010-01-21 09:23:06 -080091 snprintf(dev, sizeof(dev), "/dev/%s", tty);
San Mehatd5573d32010-01-19 17:19:41 -080092
93 // TODO: Deal with pppd bailing out after 99999 seconds of being started
94 // but not getting a connection
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080095 if (execl("/system/bin/pppd", "/system/bin/pppd", "-detach", dev, "115200",
Yi Kongbdfd57e2018-07-25 13:26:10 -070096 lr, "ms-dns", d1, "ms-dns", d2, "lcp-max-configure", "99999", (char *) nullptr)) {
Steve Block5ea0c052012-01-06 19:18:11 +000097 ALOGE("execl failed (%s)", strerror(errno));
San Mehatd5573d32010-01-19 17:19:41 -080098 }
Jesper Hanssona9d791f2012-04-27 13:54:27 +020099 free(lr);
100 free(d1);
101 free(d2);
Steve Block5ea0c052012-01-06 19:18:11 +0000102 ALOGE("Should never get here!");
San Mehatd5573d32010-01-19 17:19:41 -0800103 return 0;
104 } else {
105 mPid = pid;
106 }
107 return 0;
108}
109
110int PppController::detachPppd(const char *tty) {
111
112 if (mPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000113 ALOGE("PPPD already stopped");
San Mehatd5573d32010-01-19 17:19:41 -0800114 return 0;
115 }
116
Steve Block7b984e32011-12-20 16:22:42 +0000117 ALOGD("Stopping PPPD services on port %s", tty);
San Mehatd5573d32010-01-19 17:19:41 -0800118 kill(mPid, SIGTERM);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700119 waitpid(mPid, nullptr, 0);
San Mehatd5573d32010-01-19 17:19:41 -0800120 mPid = 0;
Steve Block7b984e32011-12-20 16:22:42 +0000121 ALOGD("PPPD services on port %s stopped", tty);
San Mehatd5573d32010-01-19 17:19:41 -0800122 return 0;
123}
124
125TtyCollection *PppController::getTtyList() {
San Mehat18817622010-01-21 09:23:06 -0800126 updateTtyList();
San Mehatd5573d32010-01-19 17:19:41 -0800127 return mTtys;
128}
129
San Mehat18817622010-01-21 09:23:06 -0800130int PppController::updateTtyList() {
131 TtyCollection::iterator it;
132
133 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
134 free(*it);
135 }
136 mTtys->clear();
137
138 DIR *d = opendir("/sys/class/tty");
139 if (!d) {
Steve Block5ea0c052012-01-06 19:18:11 +0000140 ALOGE("Error opening /sys/class/tty (%s)", strerror(errno));
San Mehat18817622010-01-21 09:23:06 -0800141 return -1;
142 }
143
144 struct dirent *de;
145 while ((de = readdir(d))) {
146 if (de->d_name[0] == '.')
147 continue;
148 if ((!strncmp(de->d_name, "tty", 3)) && (strlen(de->d_name) > 3)) {
149 mTtys->push_back(strdup(de->d_name));
150 }
151 }
152 closedir(d);
153 return 0;
154}