netd: Add support for attaching/detaching PPP daemons

Signed-off-by: San Mehat <san@google.com>
diff --git a/Android.mk b/Android.mk
index 910c819..678a5b9 100644
--- a/Android.mk
+++ b/Android.mk
@@ -17,7 +17,8 @@
                   NetlinkHandler.cpp                   \
                   logwrapper.c                         \
                   TetherController.cpp                 \
-                  NatController.cpp
+                  NatController.cpp                    \
+                  PppController.cpp
 
 LOCAL_MODULE:= netd
 
diff --git a/CommandListener.cpp b/CommandListener.cpp
index 4bfb356..774d018 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -32,6 +32,7 @@
 
 TetherController *CommandListener::sTetherCtrl = NULL;
 NatController *CommandListener::sNatCtrl = NULL;
+PppController *CommandListener::sPppCtrl = NULL;
 
 CommandListener::CommandListener() :
                  FrameworkListener("netd") {
@@ -39,11 +40,15 @@
     registerCmd(new IpFwdCmd());
     registerCmd(new TetherCmd());
     registerCmd(new NatCmd());
+    registerCmd(new ListTtysCmd());
+    registerCmd(new PppdCmd());
 
     if (!sTetherCtrl)
         sTetherCtrl = new TetherController();
     if (!sNatCtrl)
         sNatCtrl = new NatController();
+    if (!sPppCtrl)
+        sPppCtrl = new PppController();
 }
 
 CommandListener::ListInterfacesCmd::ListInterfacesCmd() :
@@ -57,6 +62,23 @@
     return 0;
 }
 
+CommandListener::ListTtysCmd::ListTtysCmd() :
+                 NetdCommand("list_ttys") {
+}
+
+int CommandListener::ListTtysCmd::runCommand(SocketClient *cli,
+                                             int argc, char **argv) {
+    TtyCollection *tlist = sPppCtrl->getTtyList();
+    TtyCollection::iterator it;
+
+    for (it = tlist->begin(); it != tlist->end(); ++it) {
+        cli->sendMsg(ResponseCode::TtyListResult, *it, false);
+    }
+
+    cli->sendMsg(ResponseCode::CommandOkay, "Ttys listed.", false);
+    return 0;
+}
+
 CommandListener::IpFwdCmd::IpFwdCmd() :
                  NetdCommand("ipfwd") {
 }
@@ -217,3 +239,43 @@
     return 0;
 }
 
+CommandListener::PppdCmd::PppdCmd() :
+                 NetdCommand("pppd") {
+}
+
+int CommandListener::PppdCmd::runCommand(SocketClient *cli,
+                                                      int argc, char **argv) {
+    int rc = 0;
+
+    if (argc < 3) {
+        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
+        return 0;
+    }
+
+    if (!strcmp(argv[1], "attach")) {
+        struct in_addr l, r;
+
+        if (!inet_aton(argv[3], &l)) {
+            cli->sendMsg(ResponseCode::CommandParameterError, "Invalid local address", false);
+            return 0;
+        }
+        if (!inet_aton(argv[4], &r)) {
+            cli->sendMsg(ResponseCode::CommandParameterError, "Invalid remote address", false);
+            return 0;
+        }
+        rc = sPppCtrl->attachPppd(argv[2], l, r);
+    } else if (!strcmp(argv[1], "detach")) {
+        rc = sPppCtrl->detachPppd(argv[2]);
+    } else {
+        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown pppd cmd", false);
+        return 0;
+    }
+
+    if (!rc) {
+        cli->sendMsg(ResponseCode::CommandOkay, "Pppd operation succeeded", false);
+    } else {
+        cli->sendMsg(ResponseCode::OperationFailed, "Pppd operation failed", true);
+    }
+
+    return 0;
+}
diff --git a/CommandListener.h b/CommandListener.h
index 3d69eba..55a039d 100644
--- a/CommandListener.h
+++ b/CommandListener.h
@@ -22,10 +22,12 @@
 #include "NetdCommand.h"
 #include "TetherController.h"
 #include "NatController.h"
+#include "PppController.h"
 
 class CommandListener : public FrameworkListener {
     static TetherController *sTetherCtrl;
     static NatController *sNatCtrl;
+    static PppController *sPppCtrl;
 
 public:
     CommandListener();
@@ -60,6 +62,20 @@
         virtual ~NatCmd() {}
         int runCommand(SocketClient *c, int argc, char ** argv);
     };
+
+    class ListTtysCmd : public NetdCommand {
+    public:
+        ListTtysCmd();
+        virtual ~ListTtysCmd() {}
+        int runCommand(SocketClient *c, int argc, char ** argv);
+    };
+
+    class PppdCmd : public NetdCommand {
+    public:
+        PppdCmd();
+        virtual ~PppdCmd() {}
+        int runCommand(SocketClient *c, int argc, char ** argv);
+    };
 };
 
 #endif
diff --git a/PppController.cpp b/PppController.cpp
new file mode 100644
index 0000000..93789a9
--- /dev/null
+++ b/PppController.cpp
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#define LOG_TAG "PppController"
+#include <cutils/log.h>
+
+#include "PppController.h"
+
+extern "C" int logwrap(int argc, const char **argv, int background);
+
+static char IPTABLES_PATH[] = "/system/bin/iptables";
+
+PppController::PppController() {
+    mTtys = new TtyCollection();
+    mPid = 0;
+}
+
+PppController::~PppController() {
+    TtyCollection::iterator it;
+
+    for (it = mTtys->begin(); it != mTtys->end(); ++it) {
+        free(*it);
+    }
+    mTtys->clear();
+}
+
+int PppController::attachPppd(const char *tty, struct in_addr local,
+                              struct in_addr remote) {
+    pid_t pid;
+
+    if (mPid) {
+        LOGE("Multiple PPPD instances not currently supported");
+        errno = EBUSY;
+        return -1;
+    }
+
+    if ((pid = fork()) < 0) {
+        LOGE("fork failed (%s)", strerror(errno));
+        return -1;
+    }
+
+    if (!pid) {
+        char *l = strdup(inet_ntoa(local));
+        char *r = strdup(inet_ntoa(remote));
+        char dev[32];
+        char *lr;
+
+        asprintf(&lr, "%s:%s", l, r);
+
+        snprintf(dev, sizeof(dev), "/dev/%s", tty); // TODO: STOPSHIP Validate this
+
+        // TODO: Deal with pppd bailing out after 99999 seconds of being started
+        // but not getting a connection
+        if (execl("/system/bin/pppd", "/system/bin/pppd", "-detach", dev,
+                  "115200", lr, "debug", "lcp-max-configure", "99999", (char *) NULL)) {
+            LOGE("execl failed (%s)", strerror(errno));
+        }
+        LOGE("Should never get here!");
+        return 0;
+    } else {
+        mPid = pid;
+    }
+    return 0;
+}
+
+int PppController::detachPppd(const char *tty) {
+
+    if (mPid == 0) {
+        LOGE("PPPD already stopped");
+        return 0;
+    }
+
+    LOGD("Stopping PPPD services on port %s", tty);
+
+    kill(mPid, SIGTERM);
+    mPid = 0;
+    return 0;
+}
+
+TtyCollection *PppController::getTtyList() {
+    return mTtys;
+}
+
diff --git a/PppController.h b/PppController.h
new file mode 100644
index 0000000..8683de2
--- /dev/null
+++ b/PppController.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _PPP_CONTROLLER_H
+#define _PPP_CONTROLLER_H
+
+#include <linux/in.h>
+
+#include <utils/List.h>
+
+typedef android::List<char *> TtyCollection;
+
+class PppController {
+    TtyCollection *mTtys;
+    pid_t          mPid; // TODO: Add support for > 1 pppd instance
+
+public:
+    PppController();
+    virtual ~PppController();
+
+    int attachPppd(const char *tty, struct in_addr local,
+                   struct in_addr remote);
+    int detachPppd(const char *tty);
+    TtyCollection *getTtyList();
+
+private:
+};
+
+#endif
diff --git a/ResponseCode.h b/ResponseCode.h
index 37f0008..6c1c238 100644
--- a/ResponseCode.h
+++ b/ResponseCode.h
@@ -25,6 +25,7 @@
     static const int InterfaceListResult       = 110;
     static const int TetherInterfaceListResult = 111;
     static const int TetherDnsFwdTgtListResult = 112;
+    static const int TtyListResult             = 113;
 
 
     // 200 series - Requested action has been successfully completed