blob: b80e1a672d1f2acc06978735a03d50cba80e86fe [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
San Mehatd5573d32010-01-19 17:19:41 -080017#include <errno.h>
San Mehat18817622010-01-21 09:23:06 -080018#include <fcntl.h>
Tom Cherry20a60132020-04-13 23:52:06 +000019#include <stdlib.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080020#include <string.h>
Tom Cherry20a60132020-04-13 23:52:06 +000021#include <unistd.h>
San Mehat18817622010-01-21 09:23:06 -080022
San Mehatd5573d32010-01-19 17:19:41 -080023#include <sys/socket.h>
24#include <sys/stat.h>
San Mehat18817622010-01-21 09:23:06 -080025#include <sys/types.h>
26#include <sys/wait.h>
27
28#include <dirent.h>
29
San Mehatd5573d32010-01-19 17:19:41 -080030#include <netinet/in.h>
31#include <arpa/inet.h>
32
33#define LOG_TAG "PppController"
Logan Chien3f461482018-04-23 14:31:32 +080034#include <log/log.h>
San Mehatd5573d32010-01-19 17:19:41 -080035
36#include "PppController.h"
37
San Mehatd5573d32010-01-19 17:19:41 -080038PppController::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,
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080053 struct in_addr remote, struct in_addr dns1,
54 struct in_addr dns2) {
San Mehatd5573d32010-01-19 17:19:41 -080055 pid_t pid;
56
57 if (mPid) {
Steve Block5ea0c052012-01-06 19:18:11 +000058 ALOGE("Multiple PPPD instances not currently supported");
San Mehatd5573d32010-01-19 17:19:41 -080059 errno = EBUSY;
60 return -1;
61 }
62
San Mehat18817622010-01-21 09:23:06 -080063 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()) {
Steve Block5ea0c052012-01-06 19:18:11 +000070 ALOGE("Invalid tty '%s' specified", tty);
San Mehat18817622010-01-21 09:23:06 -080071 errno = -EINVAL;
72 return -1;
73 }
74
San Mehatd5573d32010-01-19 17:19:41 -080075 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000076 ALOGE("fork failed (%s)", strerror(errno));
San Mehatd5573d32010-01-19 17:19:41 -080077 return -1;
78 }
79
80 if (!pid) {
81 char *l = strdup(inet_ntoa(local));
82 char *r = strdup(inet_ntoa(remote));
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080083 char *d1 = strdup(inet_ntoa(dns1));
84 char *d2 = strdup(inet_ntoa(dns2));
San Mehatd5573d32010-01-19 17:19:41 -080085 char dev[32];
86 char *lr;
87
88 asprintf(&lr, "%s:%s", l, r);
Jesper Hanssona9d791f2012-04-27 13:54:27 +020089 free(l);
90 free(r);
San Mehatd5573d32010-01-19 17:19:41 -080091
San Mehat18817622010-01-21 09:23:06 -080092 snprintf(dev, sizeof(dev), "/dev/%s", tty);
San Mehatd5573d32010-01-19 17:19:41 -080093
94 // TODO: Deal with pppd bailing out after 99999 seconds of being started
95 // but not getting a connection
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080096 if (execl("/system/bin/pppd", "/system/bin/pppd", "-detach", dev, "115200",
Yi Kongbdfd57e2018-07-25 13:26:10 -070097 lr, "ms-dns", d1, "ms-dns", d2, "lcp-max-configure", "99999", (char *) nullptr)) {
Steve Block5ea0c052012-01-06 19:18:11 +000098 ALOGE("execl failed (%s)", strerror(errno));
San Mehatd5573d32010-01-19 17:19:41 -080099 }
Jesper Hanssona9d791f2012-04-27 13:54:27 +0200100 free(lr);
101 free(d1);
102 free(d2);
Steve Block5ea0c052012-01-06 19:18:11 +0000103 ALOGE("Should never get here!");
San Mehatd5573d32010-01-19 17:19:41 -0800104 return 0;
105 } else {
106 mPid = pid;
107 }
108 return 0;
109}
110
111int PppController::detachPppd(const char *tty) {
112
113 if (mPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000114 ALOGE("PPPD already stopped");
San Mehatd5573d32010-01-19 17:19:41 -0800115 return 0;
116 }
117
Steve Block7b984e32011-12-20 16:22:42 +0000118 ALOGD("Stopping PPPD services on port %s", tty);
San Mehatd5573d32010-01-19 17:19:41 -0800119 kill(mPid, SIGTERM);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700120 waitpid(mPid, nullptr, 0);
San Mehatd5573d32010-01-19 17:19:41 -0800121 mPid = 0;
Steve Block7b984e32011-12-20 16:22:42 +0000122 ALOGD("PPPD services on port %s stopped", tty);
San Mehatd5573d32010-01-19 17:19:41 -0800123 return 0;
124}
125
126TtyCollection *PppController::getTtyList() {
San Mehat18817622010-01-21 09:23:06 -0800127 updateTtyList();
San Mehatd5573d32010-01-19 17:19:41 -0800128 return mTtys;
129}
130
San Mehat18817622010-01-21 09:23:06 -0800131int 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) {
Steve Block5ea0c052012-01-06 19:18:11 +0000141 ALOGE("Error opening /sys/class/tty (%s)", strerror(errno));
San Mehat18817622010-01-21 09:23:06 -0800142 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}