Support adb client connect to remote server

ADB client: allow user to specify hostname and port number of remote
adb server.
ADB server: bind server to all network interfaces instead of just
localhost when user gives -a flag.

Primary use-case for this change is to support remote testing of USB
devices. HostA is running some test automation software which invokes adb
client. HostB has USB-only device attached and is running adb server. adb
client on HostA makes connection to adb server on HostB to talk to the
USB device.

Change-Id: I845cc8c00350b400317f8c18f813e6fd79bd5470
Signed-off-by: Dean Kwon <daex.i.kwon@intel.com>
Signed-off-by: Jim Bride <jim.bride@intel.com>
Signed-off-by: Matt Gumbel <matthew.k.gumbel@intel.com>
diff --git a/adb_client.c b/adb_client.c
index 9a812f0..8340738 100644
--- a/adb_client.c
+++ b/adb_client.c
@@ -17,6 +17,7 @@
 static const char* __adb_serial = NULL;
 
 static int __adb_server_port = DEFAULT_ADB_PORT;
+static const char* __adb_server_name = NULL;
 
 void adb_set_transport(transport_type type, const char* serial)
 {
@@ -29,6 +30,11 @@
     __adb_server_port = server_port;
 }
 
+void adb_set_tcp_name(const char* hostname)
+{
+    __adb_server_name = hostname;
+}
+
 int  adb_get_emulator_console_port(void)
 {
     const char*   serial = __adb_serial;
@@ -181,7 +187,11 @@
     }
     snprintf(tmp, sizeof tmp, "%04x", len);
 
-    fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);
+    if (__adb_server_name)
+        fd = socket_network_client(__adb_server_name, __adb_server_port, SOCK_STREAM);
+    else
+        fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);
+
     if(fd < 0) {
         strcpy(__adb_error, "cannot connect to daemon");
         return -2;
@@ -212,7 +222,10 @@
     int fd = _adb_connect("host:version");
 
     D("adb_connect: service %s\n", service);
-    if(fd == -2) {
+    if(fd == -2 && __adb_server_name) {
+        fprintf(stderr,"** Cannot start server on remote host\n");
+        return fd;
+    } else if(fd == -2) {
         fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
                 __adb_server_port);
     start_server:
@@ -266,7 +279,7 @@
 
     fd = _adb_connect(service);
     if(fd == -2) {
-        fprintf(stderr,"** daemon still not running");
+        fprintf(stderr,"** daemon still not running\n");
     }
     D("adb_connect: return fd %d\n", fd);