OSDN Git Service

Add support to disable/enable ipv6
authorrepo sync <isheriff@google.com>
Thu, 29 Sep 2011 23:10:42 +0000 (16:10 -0700)
committerrepo sync <isheriff@google.com>
Thu, 29 Sep 2011 23:52:40 +0000 (16:52 -0700)
Bug: 5388757
Change-Id: I0506254948477cbff05603faed625cc73d94d777

CommandListener.cpp
CommandListener.h

index 9e9664a..68a1309 100644 (file)
@@ -81,6 +81,22 @@ CommandListener::InterfaceCmd::InterfaceCmd() :
                  NetdCommand("interface") {
 }
 
+int CommandListener::writeFile(const char *path, const char *value, int size) {
+    int fd = open(path, O_WRONLY);
+    if (fd < 0) {
+        LOGE("Failed to open %s: %s", path, strerror(errno));
+        return -1;
+    }
+
+    if (write(fd, value, size) != size) {
+        LOGE("Failed to write %s: %s", path, strerror(errno));
+        close(fd);
+        return -1;
+    }
+    close(fd);
+    return 0;
+}
+
 int CommandListener::InterfaceCmd::runCommand(SocketClient *cli,
                                                       int argc, char **argv) {
     if (argc < 2) {
@@ -355,28 +371,39 @@ int CommandListener::InterfaceCmd::runCommand(SocketClient *cli,
                 return 0;
             }
 
-            char *tmp = NULL;
+            char *tmp;
             asprintf(&tmp, "/proc/sys/net/ipv6/conf/%s/use_tempaddr", argv[2]);
-            int fd = open(tmp, O_WRONLY);
-            if (fd < 0) {
-                LOGE("Failed to open %s (%s)", tmp, strerror(errno));
+
+            if (writeFile(tmp, !strncmp(argv[3], "enable", 7) ? "2" : "0", 1) < 0) {
                 free(tmp);
                 cli->sendMsg(ResponseCode::OperationFailed,
                         "Failed to set ipv6 privacy extensions", true);
                 return 0;
             }
 
-            if (write(fd, (!strncmp(argv[3], "enable", 6) ? "2" : "0"), 1) != 1) {
-                LOGE("Failed to write %s (%s)", tmp, strerror(errno));
-                close(fd);
+            free(tmp);
+            cli->sendMsg(ResponseCode::CommandOkay, "IPv6 privacy extensions changed", false);
+            return 0;
+        } else if (!strcmp(argv[1], "ipv6")) {
+            if (argc != 4) {
+                cli->sendMsg(ResponseCode::CommandSyntaxError,
+                        "Usage: interface ipv6 <interface> <enable|disable>",
+                        false);
+                return 0;
+            }
+
+            char *tmp;
+            asprintf(&tmp, "/proc/sys/net/ipv6/conf/%s/disable_ipv6", argv[2]);
+
+            if (writeFile(tmp, !strncmp(argv[3], "enable", 7) ? "0" : "1", 1) < 0) {
                 free(tmp);
                 cli->sendMsg(ResponseCode::OperationFailed,
-                        "Failed to set ipv6 privacy extensions", true);
+                        "Failed to change IPv6 state", true);
                 return 0;
             }
-            close(fd);
+
             free(tmp);
-            cli->sendMsg(ResponseCode::CommandOkay, "IPv6 privacy extensions changed", false);
+            cli->sendMsg(ResponseCode::CommandOkay, "IPv6 state changed", false);
             return 0;
         } else {
             cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown interface cmd", false);
index 7c0cfd9..f0a7db7 100644 (file)
@@ -43,6 +43,8 @@ public:
 
 private:
 
+    static int writeFile(const char *path, const char *value, int size);
+
     static int readInterfaceCounters(const char *iface, unsigned long *rx, unsigned long *tx);
 
     class SoftapCmd : public NetdCommand {