From 3469f56012c5311187dbded8d102d1a11ec4cf3c Mon Sep 17 00:00:00 2001 From: Kevin Cernekee Date: Tue, 9 Jun 2015 15:09:44 -0700 Subject: [PATCH] Add builtin handler for Android Debug Bridge (ADB) protocol This allows Android devices to run multiple services on one port. A common use case involves muxing SSH for SCP / SFTP, and ADB for sideloading packages or running CTS. Signed-off-by: Kevin Cernekee --- probe.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/probe.c b/probe.c index 8cff937..a2b2218 100644 --- a/probe.c +++ b/probe.c @@ -33,6 +33,7 @@ static int is_tinc_protocol(const char *p, int len, struct proto*); static int is_xmpp_protocol(const char *p, int len, struct proto*); static int is_http_protocol(const char *p, int len, struct proto*); static int is_tls_protocol(const char *p, int len, struct proto*); +static int is_adb_protocol(const char *p, int len, struct proto*); static int is_true(const char *p, int len, struct proto* proto) { return 1; } /* Table of protocols that have a built-in probe @@ -46,6 +47,7 @@ static struct proto builtins[] = { { "http", NULL, NULL, is_http_protocol }, { "ssl", NULL, NULL, is_tls_protocol }, { "tls", NULL, NULL, is_tls_protocol }, + { "adb", NULL, NULL, is_adb_protocol }, { "anyprot", NULL, NULL, is_true } }; @@ -224,6 +226,22 @@ static int is_tls_protocol(const char *p, int len, struct proto *proto) return p[0] == 0x16 && p[1] == 0x03 && ( p[2] >= 0 && p[2] <= 0x03); } +static int is_adb_protocol(const char *p, int len, struct proto *proto) +{ + if (len < 30) + return PROBE_AGAIN; + + /* The initial ADB host->device packet has a command type of CNXN, and a + * data payload starting with "host:". Note that current versions of the + * client hardcode "host::" (with empty serialno and banner fields) but + * other clients may populate those fields. + * + * We aren't checking amessage.data_length, under the assumption that + * a packet >= 30 bytes long will have "something" in the payload field. + */ + return !memcmp(&p[0], "CNXN", 4) && !memcmp(&p[24], "host:", 5); +} + static int regex_probe(const char *p, int len, struct proto *proto) { regex_t **probe = proto->data;