Loading adb/client/commandline.cpp +1 −0 Original line number Original line Diff line number Diff line Loading @@ -240,6 +240,7 @@ static void help() { " $ANDROID_SERIAL serial number to connect to (see -s)\n" " $ANDROID_SERIAL serial number to connect to (see -s)\n" " $ANDROID_LOG_TAGS tags to be used by logcat (see logcat --help)\n" " $ANDROID_LOG_TAGS tags to be used by logcat (see logcat --help)\n" " $ADB_LOCAL_TRANSPORT_MAX_PORT max emulator scan port (default 5585, 16 emus)\n" " $ADB_LOCAL_TRANSPORT_MAX_PORT max emulator scan port (default 5585, 16 emus)\n" " $ADB_MDNS_AUTO_CONNECT comma-separated list of mdns services to allow auto-connect (default adb-tls-connect)\n" ); ); // clang-format on // clang-format on } } Loading adb/client/transport_mdns.cpp +69 −13 Original line number Original line Diff line number Diff line Loading @@ -26,6 +26,7 @@ #include <memory> #include <memory> #include <thread> #include <thread> #include <unordered_set> #include <vector> #include <vector> #include <android-base/stringprintf.h> #include <android-base/stringprintf.h> Loading @@ -42,27 +43,75 @@ static DNSServiceRef service_refs[kNumADBDNSServices]; static DNSServiceRef service_refs[kNumADBDNSServices]; static fdevent* service_ref_fdes[kNumADBDNSServices]; static fdevent* service_ref_fdes[kNumADBDNSServices]; static auto& g_autoconn_whitelist = *new std::unordered_set<int>(); static int adb_DNSServiceIndexByName(const char* regType) { static int adb_DNSServiceIndexByName(std::string_view regType) { for (int i = 0; i < kNumADBDNSServices; ++i) { for (int i = 0; i < kNumADBDNSServices; ++i) { if (!strncmp(regType, kADBDNSServices[i], strlen(kADBDNSServices[i]))) { if (!strncmp(regType.data(), kADBDNSServices[i], strlen(kADBDNSServices[i]))) { return i; return i; } } } } return -1; return -1; } } static bool adb_DNSServiceShouldConnect(const char* regType, const char* serviceName) { static void config_auto_connect_services() { // ADB_MDNS_AUTO_CONNECT is a comma-delimited list of mdns services // that are allowed to auto-connect. By default, only allow "adb-tls-connect" // to auto-connect, since this is filtered down to auto-connect only to paired // devices. g_autoconn_whitelist.insert(kADBSecureConnectServiceRefIndex); const char* srvs = getenv("ADB_MDNS_AUTO_CONNECT"); if (!srvs) { return; } if (strcmp(srvs, "0") == 0) { D("Disabling all auto-connecting"); g_autoconn_whitelist.clear(); return; } if (strcmp(srvs, "1") == 0) { D("Allow all auto-connecting"); g_autoconn_whitelist.insert(kADBTransportServiceRefIndex); return; } // Selectively choose which services to allow auto-connect. // E.g. ADB_MDNS_AUTO_CONNECT=adb,adb-tls-connect would allow // _adb._tcp and _adb-tls-connnect._tcp services to auto-connect. auto srvs_list = android::base::Split(srvs, ","); std::unordered_set<int> new_whitelist; for (const auto& item : srvs_list) { auto full_srv = android::base::StringPrintf("_%s._tcp", item.data()); int idx = adb_DNSServiceIndexByName(full_srv); if (idx >= 0) { new_whitelist.insert(idx); } } if (!new_whitelist.empty()) { g_autoconn_whitelist = std::move(new_whitelist); } } static bool adb_DNSServiceShouldAutoConnect(const char* regType, const char* serviceName) { // Try to auto-connect to any "_adb" or "_adb-tls-connect" services excluding emulator services. int index = adb_DNSServiceIndexByName(regType); int index = adb_DNSServiceIndexByName(regType); if (index == kADBTransportServiceRefIndex) { if (index != kADBTransportServiceRefIndex && index != kADBSecureConnectServiceRefIndex) { return false; } if (g_autoconn_whitelist.find(index) == g_autoconn_whitelist.end()) { D("Auto-connect for regType '%s' disabled", regType); return false; } // Ignore adb-EMULATOR* service names, as it interferes with the // Ignore adb-EMULATOR* service names, as it interferes with the // emulator ports that are already connected. // emulator ports that are already connected. if (android::base::StartsWith(serviceName, "adb-EMULATOR")) { if (android::base::StartsWith(serviceName, "adb-EMULATOR")) { LOG(INFO) << "Ignoring emulator transport service [" << serviceName << "]"; LOG(INFO) << "Ignoring emulator transport service [" << serviceName << "]"; return false; return false; } } } return true; return (index == kADBTransportServiceRefIndex || index == kADBSecureConnectServiceRefIndex); } } // Use adb_DNSServiceRefSockFD() instead of calling DNSServiceRefSockFD() // Use adb_DNSServiceRefSockFD() instead of calling DNSServiceRefSockFD() Loading Loading @@ -196,7 +245,7 @@ class ResolvedService : public AsyncServiceRef { // adb secure service needs to do something different from just // adb secure service needs to do something different from just // connecting here. // connecting here. if (adb_DNSServiceShouldConnect(regType_.c_str(), serviceName_.c_str())) { if (adb_DNSServiceShouldAutoConnect(regType_.c_str(), serviceName_.c_str())) { std::string response; std::string response; D("Attempting to serviceName=[%s], regtype=[%s] ipaddr=(%s:%hu)", serviceName_.c_str(), D("Attempting to serviceName=[%s], regtype=[%s] ipaddr=(%s:%hu)", serviceName_.c_str(), regType_.c_str(), ip_addr_, port_); regType_.c_str(), ip_addr_, port_); Loading Loading @@ -539,8 +588,15 @@ static void DNSSD_API on_service_browsed(DNSServiceRef sdRef, DNSServiceFlags fl } } void init_mdns_transport_discovery_thread(void) { void init_mdns_transport_discovery_thread(void) { int errorCodes[kNumADBDNSServices]; config_auto_connect_services(); std::string res; std::for_each(g_autoconn_whitelist.begin(), g_autoconn_whitelist.end(), [&](const int& i) { res += kADBDNSServices[i]; res += ","; }); D("mdns auto-connect whitelist: [%s]", res.data()); int errorCodes[kNumADBDNSServices]; for (int i = 0; i < kNumADBDNSServices; ++i) { for (int i = 0; i < kNumADBDNSServices; ++i) { errorCodes[i] = DNSServiceBrowse(&service_refs[i], 0, 0, kADBDNSServices[i], nullptr, errorCodes[i] = DNSServiceBrowse(&service_refs[i], 0, 0, kADBDNSServices[i], nullptr, on_service_browsed, nullptr); on_service_browsed, nullptr); Loading Loading
adb/client/commandline.cpp +1 −0 Original line number Original line Diff line number Diff line Loading @@ -240,6 +240,7 @@ static void help() { " $ANDROID_SERIAL serial number to connect to (see -s)\n" " $ANDROID_SERIAL serial number to connect to (see -s)\n" " $ANDROID_LOG_TAGS tags to be used by logcat (see logcat --help)\n" " $ANDROID_LOG_TAGS tags to be used by logcat (see logcat --help)\n" " $ADB_LOCAL_TRANSPORT_MAX_PORT max emulator scan port (default 5585, 16 emus)\n" " $ADB_LOCAL_TRANSPORT_MAX_PORT max emulator scan port (default 5585, 16 emus)\n" " $ADB_MDNS_AUTO_CONNECT comma-separated list of mdns services to allow auto-connect (default adb-tls-connect)\n" ); ); // clang-format on // clang-format on } } Loading
adb/client/transport_mdns.cpp +69 −13 Original line number Original line Diff line number Diff line Loading @@ -26,6 +26,7 @@ #include <memory> #include <memory> #include <thread> #include <thread> #include <unordered_set> #include <vector> #include <vector> #include <android-base/stringprintf.h> #include <android-base/stringprintf.h> Loading @@ -42,27 +43,75 @@ static DNSServiceRef service_refs[kNumADBDNSServices]; static DNSServiceRef service_refs[kNumADBDNSServices]; static fdevent* service_ref_fdes[kNumADBDNSServices]; static fdevent* service_ref_fdes[kNumADBDNSServices]; static auto& g_autoconn_whitelist = *new std::unordered_set<int>(); static int adb_DNSServiceIndexByName(const char* regType) { static int adb_DNSServiceIndexByName(std::string_view regType) { for (int i = 0; i < kNumADBDNSServices; ++i) { for (int i = 0; i < kNumADBDNSServices; ++i) { if (!strncmp(regType, kADBDNSServices[i], strlen(kADBDNSServices[i]))) { if (!strncmp(regType.data(), kADBDNSServices[i], strlen(kADBDNSServices[i]))) { return i; return i; } } } } return -1; return -1; } } static bool adb_DNSServiceShouldConnect(const char* regType, const char* serviceName) { static void config_auto_connect_services() { // ADB_MDNS_AUTO_CONNECT is a comma-delimited list of mdns services // that are allowed to auto-connect. By default, only allow "adb-tls-connect" // to auto-connect, since this is filtered down to auto-connect only to paired // devices. g_autoconn_whitelist.insert(kADBSecureConnectServiceRefIndex); const char* srvs = getenv("ADB_MDNS_AUTO_CONNECT"); if (!srvs) { return; } if (strcmp(srvs, "0") == 0) { D("Disabling all auto-connecting"); g_autoconn_whitelist.clear(); return; } if (strcmp(srvs, "1") == 0) { D("Allow all auto-connecting"); g_autoconn_whitelist.insert(kADBTransportServiceRefIndex); return; } // Selectively choose which services to allow auto-connect. // E.g. ADB_MDNS_AUTO_CONNECT=adb,adb-tls-connect would allow // _adb._tcp and _adb-tls-connnect._tcp services to auto-connect. auto srvs_list = android::base::Split(srvs, ","); std::unordered_set<int> new_whitelist; for (const auto& item : srvs_list) { auto full_srv = android::base::StringPrintf("_%s._tcp", item.data()); int idx = adb_DNSServiceIndexByName(full_srv); if (idx >= 0) { new_whitelist.insert(idx); } } if (!new_whitelist.empty()) { g_autoconn_whitelist = std::move(new_whitelist); } } static bool adb_DNSServiceShouldAutoConnect(const char* regType, const char* serviceName) { // Try to auto-connect to any "_adb" or "_adb-tls-connect" services excluding emulator services. int index = adb_DNSServiceIndexByName(regType); int index = adb_DNSServiceIndexByName(regType); if (index == kADBTransportServiceRefIndex) { if (index != kADBTransportServiceRefIndex && index != kADBSecureConnectServiceRefIndex) { return false; } if (g_autoconn_whitelist.find(index) == g_autoconn_whitelist.end()) { D("Auto-connect for regType '%s' disabled", regType); return false; } // Ignore adb-EMULATOR* service names, as it interferes with the // Ignore adb-EMULATOR* service names, as it interferes with the // emulator ports that are already connected. // emulator ports that are already connected. if (android::base::StartsWith(serviceName, "adb-EMULATOR")) { if (android::base::StartsWith(serviceName, "adb-EMULATOR")) { LOG(INFO) << "Ignoring emulator transport service [" << serviceName << "]"; LOG(INFO) << "Ignoring emulator transport service [" << serviceName << "]"; return false; return false; } } } return true; return (index == kADBTransportServiceRefIndex || index == kADBSecureConnectServiceRefIndex); } } // Use adb_DNSServiceRefSockFD() instead of calling DNSServiceRefSockFD() // Use adb_DNSServiceRefSockFD() instead of calling DNSServiceRefSockFD() Loading Loading @@ -196,7 +245,7 @@ class ResolvedService : public AsyncServiceRef { // adb secure service needs to do something different from just // adb secure service needs to do something different from just // connecting here. // connecting here. if (adb_DNSServiceShouldConnect(regType_.c_str(), serviceName_.c_str())) { if (adb_DNSServiceShouldAutoConnect(regType_.c_str(), serviceName_.c_str())) { std::string response; std::string response; D("Attempting to serviceName=[%s], regtype=[%s] ipaddr=(%s:%hu)", serviceName_.c_str(), D("Attempting to serviceName=[%s], regtype=[%s] ipaddr=(%s:%hu)", serviceName_.c_str(), regType_.c_str(), ip_addr_, port_); regType_.c_str(), ip_addr_, port_); Loading Loading @@ -539,8 +588,15 @@ static void DNSSD_API on_service_browsed(DNSServiceRef sdRef, DNSServiceFlags fl } } void init_mdns_transport_discovery_thread(void) { void init_mdns_transport_discovery_thread(void) { int errorCodes[kNumADBDNSServices]; config_auto_connect_services(); std::string res; std::for_each(g_autoconn_whitelist.begin(), g_autoconn_whitelist.end(), [&](const int& i) { res += kADBDNSServices[i]; res += ","; }); D("mdns auto-connect whitelist: [%s]", res.data()); int errorCodes[kNumADBDNSServices]; for (int i = 0; i < kNumADBDNSServices; ++i) { for (int i = 0; i < kNumADBDNSServices; ++i) { errorCodes[i] = DNSServiceBrowse(&service_refs[i], 0, 0, kADBDNSServices[i], nullptr, errorCodes[i] = DNSServiceBrowse(&service_refs[i], 0, 0, kADBDNSServices[i], nullptr, on_service_browsed, nullptr); on_service_browsed, nullptr); Loading