• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define TRACE_TAG TRANSPORT
18 
19 #include "adb_mdns.h"
20 
21 #include <algorithm>
22 #include <set>
23 
24 #include <android-base/stringprintf.h>
25 #include <android-base/strings.h>
26 
27 #include "adb_trace.h"
28 
29 #define ADB_FULL_MDNS_SERVICE_TYPE(atype) ("_" atype "._tcp")
30 const char* kADBDNSServices[] = {ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_SERVICE_TYPE),
31                                  ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_PAIRING_TYPE),
32                                  ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_CONNECT_TYPE)};
33 
34 #if ADB_HOST
35 namespace {
36 
37 std::atomic<bool> g_allowedlist_configured{false};
38 [[clang::no_destroy]] std::set<int> g_autoconn_allowedlist;
39 
config_auto_connect_services()40 void config_auto_connect_services() {
41     bool expected = false;
42     if (!g_allowedlist_configured.compare_exchange_strong(expected, true)) {
43         return;
44     }
45 
46     // ADB_MDNS_AUTO_CONNECT is a comma-delimited list of mdns services
47     // that are allowed to auto-connect. By default, only allow "adb-tls-connect"
48     // to auto-connect, since this is filtered down to auto-connect only to paired
49     // devices.
50     g_autoconn_allowedlist.insert(kADBSecureConnectServiceRefIndex);
51     const char* srvs = getenv("ADB_MDNS_AUTO_CONNECT");
52     if (!srvs) {
53         return;
54     }
55 
56     if (strcmp(srvs, "0") == 0) {
57         D("Disabling all auto-connecting");
58         g_autoconn_allowedlist.clear();
59         return;
60     }
61 
62     if (strcmp(srvs, "all") == 0) {
63         D("Allow all auto-connecting");
64         g_autoconn_allowedlist.insert(kADBTransportServiceRefIndex);
65         return;
66     }
67 
68     // Selectively choose which services to allow auto-connect.
69     // E.g. ADB_MDNS_AUTO_CONNECT=adb,adb-tls-connect would allow
70     // _adb._tcp and _adb-tls-connnect._tcp services to auto-connect.
71     auto srvs_list = android::base::Split(srvs, ",");
72     std::set<int> new_allowedlist;
73     for (const auto& item : srvs_list) {
74         auto full_srv = android::base::StringPrintf("_%s._tcp", item.data());
75         std::optional<int> idx = adb_DNSServiceIndexByName(full_srv);
76         if (idx.has_value()) {
77             new_allowedlist.insert(*idx);
78         }
79     }
80 
81     if (!new_allowedlist.empty()) {
82         g_autoconn_allowedlist = std::move(new_allowedlist);
83     }
84 
85     std::string res;
86     std::for_each(g_autoconn_allowedlist.begin(), g_autoconn_allowedlist.end(), [&](const int& i) {
87         res += kADBDNSServices[i];
88         res += ",";
89     });
90     D("mdns auto-connect allowedlist: [%s]", res.data());
91 }
92 
93 }  // namespace
94 
adb_DNSServiceIndexByName(std::string_view reg_type)95 std::optional<int> adb_DNSServiceIndexByName(std::string_view reg_type) {
96     for (int i = 0; i < kNumADBDNSServices; ++i) {
97         if (!strncmp(reg_type.data(), kADBDNSServices[i], strlen(kADBDNSServices[i]))) {
98             return i;
99         }
100     }
101     return std::nullopt;
102 }
103 
adb_DNSServiceShouldAutoConnect(std::string_view reg_type,std::string_view service_name)104 bool adb_DNSServiceShouldAutoConnect(std::string_view reg_type, std::string_view service_name) {
105     config_auto_connect_services();
106 
107     // Try to auto-connect to any "_adb" or "_adb-tls-connect" services excluding emulator services.
108     std::optional<int> index = adb_DNSServiceIndexByName(reg_type);
109     if (!index ||
110         (index != kADBTransportServiceRefIndex && index != kADBSecureConnectServiceRefIndex)) {
111         return false;
112     }
113     if (!g_autoconn_allowedlist.contains(*index)) {
114         D("Auto-connect for reg_type '%s' disabled", reg_type.data());
115         return false;
116     }
117     // Ignore adb-EMULATOR* service names, as it interferes with the
118     // emulator ports that are already connected.
119     if (android::base::StartsWith(service_name, "adb-EMULATOR")) {
120         LOG(INFO) << "Ignoring emulator transport service [" << service_name << "]";
121         return false;
122     }
123     return true;
124 }
125 
126 #endif  // ADB_HOST
127