Enable interconnection of emulators

This patch adds a -shared-net-id option to the emulator which
joins the emulator in a shared network.
If the option is given the emulator is started with an additional
network interface bound to a multicast socket. This multicast socket
emulates a network hub, interconnecting emulators.
If the -shared-net-id option is not given, nothing changes.

Change-Id: I0ea70a073cdbd34f804161300240fafca34080d0
diff --git a/android/main.c b/android/main.c
index 3aa89d0..bf3feec 100644
--- a/android/main.c
+++ b/android/main.c
@@ -1758,6 +1758,19 @@
         qemu_cpu_delay = (int) delay;
     }
 
+    if (opts->shared_net_id) {
+        char*  end;
+        long   shared_net_id = strtol(opts->shared_net_id, &end, 0);
+        if (end == NULL || *end || shared_net_id < 1 || shared_net_id > 255) {
+            fprintf(stderr, "option -shared-net-id must be an integer between 1 and 255\n");
+            exit(1);
+        }
+        char ip[11];
+        snprintf(ip, 11, "10.1.2.%ld", shared_net_id);
+        boot_property_add("net.shared_net_ip",ip);
+    }
+
+
     emulator_config_init();
     init_skinned_ui(opts->skindir, opts->skin, opts);
 
@@ -2271,6 +2284,22 @@
     args[n++] = "unix";
 #endif
 
+    /* Set up the interfaces for inter-emulator networking */
+    if (opts->shared_net_id) {
+        unsigned int shared_net_id = atoi(opts->shared_net_id);
+        char nic[37];
+        args[n++] = "-net";
+        snprintf(nic, 37, "nic,vlan=1,macaddr=52:54:00:12:34:%02x", shared_net_id);
+        args[n++] = strdup(nic);
+        args[n++] = "-net";
+        args[n++] = "socket,vlan=1,mcast=230.0.0.10:1234";
+
+        args[n++] = "-net";
+        args[n++] = "nic,vlan=0";
+        args[n++] = "-net";
+        args[n++] = "user,vlan=0";
+    }
+
     while(argc-- > 0) {
         args[n++] = *argv++;
     }