From 252586941934d23073a8d167ec240b221062505f Mon Sep 17 00:00:00 2001 From: David 'Digit' Turner Date: Thu, 21 Mar 2013 21:07:42 +0100 Subject: adb: implement "adb reverse " This implements the logical opposite of 'adb forward', i.e. the ability to reverse network connections from the device to the host. This feature is very useful for testing various programs running on an Android device without root or poking at the host's routing table. Options and parameters are exactly the same as those for 'adb forward', except that the direction is reversed. Examples: adb reverse tcp:5000 tcp:6000 connections to localhost:5000 on the device will be forwarded to localhost:6000 on the host. adb reverse --no-rebind tcp:5000 tcp:6000 same as above, but fails if the socket is already bound through a previous 'adb reverse tcp:5000 ...' command. adb reverse --list list all active reversed connections for the target device. Note: there is no command to list all reversed connections for all devices at once. adb reverse --remove tcp:5000 remove any reversed connection on the device from localhost:5000 adb reverse --remove-all remove all reversed connections form the current device. Reversed connections are tied to a transport, in other words, they disappear as soon as a device is disconnected. Simple testing protocol: adb forward tcp:5000 tcp:6000 adb reverse tcp:6000 tcp:7000 nc -l localhost 7000 in another terminal: echo "Hello" | nc localhost 5000 Will print "Hello" on the first terminal. Change-Id: I761af790cdb06829b68430afa4145a919fa0e6d5 --- adb/commandline.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'adb/commandline.c') diff --git a/adb/commandline.c b/adb/commandline.c index 241cefc5b..3970ab155 100644 --- a/adb/commandline.c +++ b/adb/commandline.c @@ -136,6 +136,19 @@ void help() " if is already forwarded\n" " adb forward --remove - remove a specific forward socket connection\n" " adb forward --remove-all - remove all forward socket connections\n" + " adb reverse --list - list all reverse socket connections from device\n" + " adb reverse - reverse socket connections\n" + " reverse specs are one of:\n" + " tcp:\n" + " localabstract:\n" + " localreserved:\n" + " localfilesystem:\n" + " adb reverse --norebind \n" + " - same as 'adb reverse ' but fails\n" + " if is already reversed.\n" + " adb reverse --remove \n" + " - remove a specific reversed socket connection\n" + " adb reverse --remove-all - remove all reversed socket connections from device\n" " adb jdwp - list PIDs of processes hosting a JDWP transport\n" " adb install [-l] [-r] [-s] [--algo --key --iv ] \n" " - push this package file to the device and install it\n" @@ -1299,8 +1312,11 @@ top: return 0; } - if(!strcmp(argv[0], "forward")) { + if(!strcmp(argv[0], "forward") || + !strcmp(argv[0], "reverse")) + { char host_prefix[64]; + char reverse = (char) !strcmp(argv[0], "reverse"); char remove = 0; char remove_all = 0; char list = 0; @@ -1329,15 +1345,19 @@ top: } // Determine the for this command. - if (serial) { - snprintf(host_prefix, sizeof host_prefix, "host-serial:%s", - serial); - } else if (ttype == kTransportUsb) { - snprintf(host_prefix, sizeof host_prefix, "host-usb"); - } else if (ttype == kTransportLocal) { - snprintf(host_prefix, sizeof host_prefix, "host-local"); + if (reverse) { + snprintf(host_prefix, sizeof host_prefix, "reverse"); } else { - snprintf(host_prefix, sizeof host_prefix, "host"); + if (serial) { + snprintf(host_prefix, sizeof host_prefix, "host-serial:%s", + serial); + } else if (ttype == kTransportUsb) { + snprintf(host_prefix, sizeof host_prefix, "host-usb"); + } else if (ttype == kTransportLocal) { + snprintf(host_prefix, sizeof host_prefix, "host-local"); + } else { + snprintf(host_prefix, sizeof host_prefix, "host"); + } } // Implement forward --list -- cgit v1.2.3