blob: 087a9ab9bb0fc0144ab01a9192dfd46482c4747c [file] [log] [blame]
Joshua Duongf4ba8d72021-01-13 12:18:15 -08001/*
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 <set>
22
23#include <android-base/stringprintf.h>
24#include <android-base/strings.h>
25
26#include "adb_trace.h"
27
28#define ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ver) ("v=" #ver)
29
30const char* kADBSecurePairingServiceTxtRecord =
31 ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ADB_SECURE_SERVICE_VERSION);
32const char* kADBSecureConnectServiceTxtRecord =
33 ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ADB_SECURE_SERVICE_VERSION);
34
35#define ADB_FULL_MDNS_SERVICE_TYPE(atype) ("_" atype "._tcp")
36const char* kADBDNSServices[] = {ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_SERVICE_TYPE),
37 ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_PAIRING_TYPE),
38 ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_CONNECT_TYPE)};
39
40const char* kADBDNSServiceTxtRecords[] = {
41 nullptr,
42 kADBSecurePairingServiceTxtRecord,
43 kADBSecureConnectServiceTxtRecord,
44};
45
46#if ADB_HOST
47namespace {
48
49std::atomic<bool> g_allowedlist_configured{false};
50[[clang::no_destroy]] std::set<int> g_autoconn_allowedlist;
51
52void config_auto_connect_services() {
53 bool expected = false;
54 if (!g_allowedlist_configured.compare_exchange_strong(expected, true)) {
55 return;
56 }
57
58 // ADB_MDNS_AUTO_CONNECT is a comma-delimited list of mdns services
59 // that are allowed to auto-connect. By default, only allow "adb-tls-connect"
60 // to auto-connect, since this is filtered down to auto-connect only to paired
61 // devices.
62 g_autoconn_allowedlist.insert(kADBSecureConnectServiceRefIndex);
63 const char* srvs = getenv("ADB_MDNS_AUTO_CONNECT");
64 if (!srvs) {
65 return;
66 }
67
68 if (strcmp(srvs, "0") == 0) {
69 D("Disabling all auto-connecting");
70 g_autoconn_allowedlist.clear();
71 return;
72 }
73
74 if (strcmp(srvs, "all") == 0) {
75 D("Allow all auto-connecting");
76 g_autoconn_allowedlist.insert(kADBTransportServiceRefIndex);
77 return;
78 }
79
80 // Selectively choose which services to allow auto-connect.
81 // E.g. ADB_MDNS_AUTO_CONNECT=adb,adb-tls-connect would allow
82 // _adb._tcp and _adb-tls-connnect._tcp services to auto-connect.
83 auto srvs_list = android::base::Split(srvs, ",");
84 std::set<int> new_allowedlist;
85 for (const auto& item : srvs_list) {
86 auto full_srv = android::base::StringPrintf("_%s._tcp", item.data());
87 std::optional<int> idx = adb_DNSServiceIndexByName(full_srv);
88 if (idx.has_value()) {
89 new_allowedlist.insert(*idx);
90 }
91 }
92
93 if (!new_allowedlist.empty()) {
94 g_autoconn_allowedlist = std::move(new_allowedlist);
95 }
96
97 std::string res;
98 std::for_each(g_autoconn_allowedlist.begin(), g_autoconn_allowedlist.end(), [&](const int& i) {
99 res += kADBDNSServices[i];
100 res += ",";
101 });
102 D("mdns auto-connect allowedlist: [%s]", res.data());
103}
104
105} // namespace
106
107std::optional<int> adb_DNSServiceIndexByName(std::string_view reg_type) {
108 for (int i = 0; i < kNumADBDNSServices; ++i) {
109 if (!strncmp(reg_type.data(), kADBDNSServices[i], strlen(kADBDNSServices[i]))) {
110 return i;
111 }
112 }
113 return std::nullopt;
114}
115
116bool adb_DNSServiceShouldAutoConnect(std::string_view reg_type, std::string_view service_name) {
117 config_auto_connect_services();
118
119 // Try to auto-connect to any "_adb" or "_adb-tls-connect" services excluding emulator services.
120 std::optional<int> index = adb_DNSServiceIndexByName(reg_type);
121 if (!index ||
122 (index != kADBTransportServiceRefIndex && index != kADBSecureConnectServiceRefIndex)) {
123 return false;
124 }
125 if (g_autoconn_allowedlist.find(*index) == g_autoconn_allowedlist.end()) {
126 D("Auto-connect for reg_type '%s' disabled", reg_type.data());
127 return false;
128 }
129 // Ignore adb-EMULATOR* service names, as it interferes with the
130 // emulator ports that are already connected.
131 if (android::base::StartsWith(service_name, "adb-EMULATOR")) {
132 LOG(INFO) << "Ignoring emulator transport service [" << service_name << "]";
133 return false;
134 }
135 return true;
136}
137
138#endif // ADB_HOST