From 0a619dc9efa358c03372e08192bda075ecbeb0a6 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 6 Feb 2022 15:11:35 +0100 Subject: [PATCH] Allow selecting a device from IP without port Since the previous commit, if a serial is given via -s/--serial (either a real USB serial or an IP:port), a device is selected if its serial matches exactly. In addition, if the user pass an IP without a port, then select any device with this IP, regardless of the port (so that "192.168.1.1" matches any "192.168.1.1:port"). This is also the default behavior of adb. PR #3005 --- app/src/adb/adb.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/src/adb/adb.c b/app/src/adb/adb.c index ef316884..baa0e406 100644 --- a/app/src/adb/adb.c +++ b/app/src/adb/adb.c @@ -421,6 +421,24 @@ sc_adb_accept_device(const struct sc_adb_device *device, const char *serial) { return true; } + char *device_serial_colon = strchr(device->serial, ':'); + if (device_serial_colon) { + // The device serial is an IP:port... + char *serial_colon = strchr(serial, ':'); + if (!serial_colon) { + // But the requested serial has no ':', so only consider the IP part + // of the device serial. This allows to use "192.168.1.1" to match + // any "192.168.1.1:port". + size_t serial_len = strlen(serial); + size_t device_ip_len = device_serial_colon - device->serial; + if (serial_len != device_ip_len) { + // They are not equal, they don't even have the same length + return false; + } + return !strncmp(serial, device->serial, device_ip_len); + } + } + return !strcmp(serial, device->serial); }