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