Revert "Remove netcfg's unused options."

This reverts commit f8e83054cb8a8b13ee84a4aeeaa0a2fd8debca81.

Change-Id: Iede772f96ff9008277df433dcbb5f6603de65283
diff --git a/adb/adb.c b/adb/adb.c
index 4258a01..c939ef0 100644
--- a/adb/adb.c
+++ b/adb/adb.c
@@ -1348,7 +1348,7 @@
     ** AID_ADB to access the USB driver
     ** AID_LOG to read system logs (adb logcat)
     ** AID_INPUT to diagnose input issues (getevent)
-    ** AID_INET to diagnose network issues (ping)
+    ** AID_INET to diagnose network issues (netcfg, ping)
     ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
     ** AID_SDCARD_R to allow reading from the SD card
     ** AID_SDCARD_RW to allow writing to the SD card
diff --git a/include/netutils/ifc.h b/include/netutils/ifc.h
index 49e77e4..3b27234 100644
--- a/include/netutils/ifc.h
+++ b/include/netutils/ifc.h
@@ -51,6 +51,7 @@
 extern int ifc_clear_addresses(const char *name);
 
 extern int ifc_create_default_route(const char *name, in_addr_t addr);
+extern int ifc_remove_default_route(const char *ifname);
 extern int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength,
                         unsigned *flags);
 
diff --git a/libnetutils/dhcpclient.c b/libnetutils/dhcpclient.c
index a05b7cb..700b02f 100644
--- a/libnetutils/dhcpclient.c
+++ b/libnetutils/dhcpclient.c
@@ -353,28 +353,28 @@
 static int is_valid_reply(dhcp_msg *msg, dhcp_msg *reply, int sz)
 {
     if (sz < DHCP_MSG_FIXED_SIZE) {
-        if (verbose) ALOGD("Wrong size %d != %d\n", sz, DHCP_MSG_FIXED_SIZE);
+        if (verbose) ALOGD("netcfg: Wrong size %d != %d\n", sz, DHCP_MSG_FIXED_SIZE);
         return 0;
     }
     if (reply->op != OP_BOOTREPLY) {
-        if (verbose) ALOGD("Wrong Op %d != %d\n", reply->op, OP_BOOTREPLY);
+        if (verbose) ALOGD("netcfg: Wrong Op %d != %d\n", reply->op, OP_BOOTREPLY);
         return 0;
     }
     if (reply->xid != msg->xid) {
-        if (verbose) ALOGD("Wrong Xid 0x%x != 0x%x\n", ntohl(reply->xid),
-                           ntohl(msg->xid));
+        if (verbose) ALOGD("netcfg: Wrong Xid 0x%x != 0x%x\n", ntohl(reply->xid),
+                          ntohl(msg->xid));
         return 0;
     }
     if (reply->htype != msg->htype) {
-        if (verbose) ALOGD("Wrong Htype %d != %d\n", reply->htype, msg->htype);
+        if (verbose) ALOGD("netcfg: Wrong Htype %d != %d\n", reply->htype, msg->htype);
         return 0;
     }
     if (reply->hlen != msg->hlen) {
-        if (verbose) ALOGD("Wrong Hlen %d != %d\n", reply->hlen, msg->hlen);
+        if (verbose) ALOGD("netcfg: Wrong Hlen %d != %d\n", reply->hlen, msg->hlen);
         return 0;
     }
     if (memcmp(msg->chaddr, reply->chaddr, msg->hlen)) {
-        if (verbose) ALOGD("Wrong chaddr %x != %x\n", *(reply->chaddr),*(msg->chaddr));
+        if (verbose) ALOGD("netcfg: Wrong chaddr %x != %x\n", *(reply->chaddr),*(msg->chaddr));
         return 0;
     }
     return 1;
diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c
index ab6c654..bfe7121 100644
--- a/libnetutils/ifc_utils.c
+++ b/libnetutils/ifc_utils.c
@@ -639,6 +639,26 @@
 #endif
 }
 
+/*
+ * Removes the default route for the named interface.
+ */
+int ifc_remove_default_route(const char *ifname)
+{
+    struct rtentry rt;
+    int result;
+
+    ifc_init();
+    memset(&rt, 0, sizeof(rt));
+    rt.rt_dev = (void *)ifname;
+    rt.rt_flags = RTF_UP|RTF_GATEWAY;
+    init_sockaddr_in(&rt.rt_dst, 0);
+    if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
+        ALOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
+    }
+    ifc_close();
+    return result;
+}
+
 int
 ifc_configure(const char *ifname,
         in_addr_t address,
diff --git a/netcfg/netcfg.c b/netcfg/netcfg.c
index eec1b2f..204bf1d 100644
--- a/netcfg/netcfg.c
+++ b/netcfg/netcfg.c
@@ -24,7 +24,13 @@
 #include <stdlib.h>
 #include <string.h>
 
-static const char *ipaddr(in_addr_t addr)
+void die(const char *reason)
+{
+    perror(reason);
+    exit(1);
+}
+
+const char *ipaddr(in_addr_t addr)
 {
     struct in_addr in_addr;
 
@@ -32,13 +38,13 @@
     return inet_ntoa(in_addr);
 }
 
-static void usage(void)
+void usage(void)
 {
-    fprintf(stderr,"usage: netcfg [<interface> dhcp]\n");
+    fprintf(stderr,"usage: netcfg [<interface> {dhcp|up|down}]\n");
     exit(1);
 }
 
-static int dump_interface(const char *name)
+int dump_interface(const char *name)
 {
     unsigned addr, flags;
     unsigned char hwbuf[ETH_ALEN];
@@ -63,7 +69,7 @@
     return 0;
 }
 
-static int dump_interfaces(void)
+int dump_interfaces(void)
 {
     DIR *d;
     struct dirent *de;
@@ -79,11 +85,56 @@
     return 0;
 }
 
+int set_hwaddr(const char *name, const char *asc) {
+    struct ether_addr *addr = ether_aton(asc);
+    if (!addr) {
+        printf("Failed to parse '%s'\n", asc);
+        return -1;
+    }
+    return ifc_set_hwaddr(name, addr->ether_addr_octet);
+}
+
+struct 
+{
+    const char *name;
+    int nargs;
+    void *func;
+} CMDS[] = {
+    { "dhcp",   1, do_dhcp },
+    { "up",     1, ifc_up },
+    { "down",   1, ifc_down },
+    { "deldefault", 1, ifc_remove_default_route },
+    { "hwaddr", 2, set_hwaddr },
+    { 0, 0, 0 },
+};
+
+static int call_func(void *_func, unsigned nargs, char **args)
+{
+    switch(nargs){
+    case 1: {
+        int (*func)(char *a0) = _func;
+        return func(args[0]);
+    }
+    case 2: {
+        int (*func)(char *a0, char *a1) = _func;
+        return func(args[0], args[1]);
+    }
+    case 3: {
+        int (*func)(char *a0, char *a1, char *a2) = _func;
+        return func(args[0], args[1], args[2]);
+    }
+    default:
+        return -1;
+    }
+}
+
 int main(int argc, char **argv)
 {
+    char *iname;
+    int n;
+    
     if(ifc_init()) {
-        perror("Cannot perform requested operation");
-        exit(1);
+        die("Cannot perform requested operation");
     }
 
     if(argc == 1) {
@@ -92,23 +143,41 @@
         return result;
     }
 
-    if(argc != 3) usage();
+    if(argc < 3) usage();
 
-    char* iname = argv[1];
-    char* action = argv[2];
+    iname = argv[1];
     if(strlen(iname) > 16) usage();
 
-    if (!strcmp(action, "dhcp")) {
-        if (do_dhcp(iname)) {
-            fprintf(stderr, "dhcp failed: %s\n", strerror(errno));
-            ifc_close();
-            exit(1);
+    argc -= 2;
+    argv += 2;
+    while(argc > 0) {
+        for(n = 0; CMDS[n].name; n++){
+            if(!strcmp(argv[0], CMDS[n].name)) {
+                char *cmdname = argv[0];
+                int nargs = CMDS[n].nargs;
+                
+                argv[0] = iname;
+                if(argc < nargs) {
+                    fprintf(stderr, "not enough arguments for '%s'\n", cmdname);
+                    ifc_close();
+                    exit(1);
+                }
+                if(call_func(CMDS[n].func, nargs, argv)) {
+                    fprintf(stderr, "action '%s' failed (%s)\n", cmdname, strerror(errno));
+                    ifc_close();
+                    exit(1);
+                }
+                argc -= nargs;
+                argv += nargs;
+                goto done;
+            }
         }
-    } else {
-        fprintf(stderr,"no such action '%s'\n", action);
+        fprintf(stderr,"no such action '%s'\n", argv[0]);
         usage();
+    done:
+        ;
     }
-
     ifc_close();
+
     return 0;
 }