adb: Add a way to distinguish between connection failures and successes
This change adds a callback that is invoked exactly once, either when
the connection is fully established (i.e. CNXN packets have been sent
and received) or the atransport object is deleted before that (because
the connection failed).
This helps in distinguishing between successful and failing connections
for TCP. Especially when there is some kind of port
forwarding/multiplexing in between (like an SSH tunnel or SSLH proxy).
Bug: 74411879
Test: adb connect chromebook:22 (which runs an sslh tunnel to adbd).
either succeeds or fails, but not fake-succeeds.
Change-Id: I7e826c6f5d4c30338a03b2d376a857ac5d05672a
diff --git a/adb/test_adb.py b/adb/test_adb.py
index 363002f..32bf029 100644
--- a/adb/test_adb.py
+++ b/adb/test_adb.py
@@ -49,8 +49,16 @@
# A pipe that is used to signal the thread that it should terminate.
readpipe, writepipe = os.pipe()
+ def _adb_packet(command, arg0, arg1, data):
+ bin_command = struct.unpack('I', command)[0]
+ buf = struct.pack('IIIIII', bin_command, arg0, arg1, len(data), 0,
+ bin_command ^ 0xffffffff)
+ buf += data
+ return buf
+
def _handle():
rlist = [readpipe, serversock]
+ cnxn_sent = {}
while True:
ready, _, _ = select.select(rlist, [], [])
for r in ready:
@@ -68,7 +76,15 @@
# Client socket
data = r.recv(1024)
if not data:
+ if r in cnxn_sent:
+ del cnxn_sent[r]
rlist.remove(r)
+ continue
+ if r in cnxn_sent:
+ continue
+ cnxn_sent[r] = True
+ r.sendall(_adb_packet('CNXN', 0x01000001, 1024 * 1024,
+ 'device::ro.product.name=fakeadb'))
port = serversock.getsockname()[1]
server_thread = threading.Thread(target=_handle)