blob: 35e3d907ee70959999d528f2f40565953241889e [file] [log] [blame]
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -07001/*
2 * Copyright (C) 2016 The 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
Lingfeng Yang615036f2018-11-28 16:38:36 -080017#include "mdns.h"
Casey Dahlin13a269e2016-06-23 14:19:37 -070018#include "adb_mdns.h"
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070019#include "sysdeps.h"
20
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070021#include <dns_sd.h>
22#include <endian.h>
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070023#include <unistd.h>
24
Josh Gaoe1dacfc2017-04-12 17:00:49 -070025#include <chrono>
26#include <mutex>
27#include <thread>
28
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070029#include <android-base/logging.h>
30#include <android-base/properties.h>
31
32using namespace std::chrono_literals;
33
34static std::mutex& mdns_lock = *new std::mutex();
35static int port;
Lingfeng Yangbe49d8a2018-11-16 22:47:31 -080036static DNSServiceRef mdns_refs[kNumADBDNSServices];
37static bool mdns_registered[kNumADBDNSServices];
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070038
39static void start_mdns() {
40 if (android::base::GetProperty("init.svc.mdnsd", "") == "running") {
41 return;
42 }
43
44 android::base::SetProperty("ctl.start", "mdnsd");
45
46 if (! android::base::WaitForProperty("init.svc.mdnsd", "running", 5s)) {
47 LOG(ERROR) << "Could not start mdnsd.";
48 }
49}
50
51static void mdns_callback(DNSServiceRef /*ref*/,
52 DNSServiceFlags /*flags*/,
53 DNSServiceErrorType errorCode,
54 const char* /*name*/,
55 const char* /*regtype*/,
56 const char* /*domain*/,
57 void* /*context*/) {
58 if (errorCode != kDNSServiceErr_NoError) {
59 LOG(ERROR) << "Encountered mDNS registration error ("
60 << errorCode << ").";
61 }
62}
63
Lingfeng Yang615036f2018-11-28 16:38:36 -080064static void register_mdns_service(int index, int port) {
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070065 std::lock_guard<std::mutex> lock(mdns_lock);
66
Casey Dahlin1fe3cae2016-05-20 16:34:51 -070067 std::string hostname = "adb-";
68 hostname += android::base::GetProperty("ro.serialno", "unidentified");
69
Lingfeng Yang615036f2018-11-28 16:38:36 -080070 auto error = DNSServiceRegister(&mdns_refs[index], 0, 0, hostname.c_str(),
71 kADBDNSServices[index], nullptr, nullptr,
72 htobe16((uint16_t)port), 0, nullptr, mdns_callback, nullptr);
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070073
Lingfeng Yang615036f2018-11-28 16:38:36 -080074 if (error != kDNSServiceErr_NoError) {
75 LOG(ERROR) << "Could not register mDNS service " << kADBDNSServices[index] << ", error ("
76 << error << ").";
77 mdns_registered[index] = false;
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070078 }
79
Lingfeng Yang615036f2018-11-28 16:38:36 -080080 mdns_registered[index] = true;
81
82 LOG(INFO) << "adbd mDNS service " << kADBDNSServices[index]
83 << " registered: " << mdns_registered[index];
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070084}
85
Lingfeng Yang615036f2018-11-28 16:38:36 -080086static void unregister_mdns_service(int index) {
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070087 std::lock_guard<std::mutex> lock(mdns_lock);
88
Lingfeng Yang615036f2018-11-28 16:38:36 -080089 if (mdns_registered[index]) {
90 DNSServiceRefDeallocate(mdns_refs[index]);
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070091 }
92}
93
Lingfeng Yang615036f2018-11-28 16:38:36 -080094static void register_base_mdns_transport() {
95 register_mdns_service(kADBTransportServiceRefIndex, port);
96}
97
98static void setup_mdns_thread() {
99 start_mdns();
100
101 // We will now only set up the normal transport mDNS service
102 // instead of registering all the adb secure mDNS services
103 // in the beginning. This is to provide more privacy/security.
104 register_base_mdns_transport();
105}
106
107// This also tears down any adb secure mDNS services, if they exist.
108static void teardown_mdns() {
109 for (int i = 0; i < kNumADBDNSServices; ++i) {
110 unregister_mdns_service(i);
111 }
112}
113
114// Public interface/////////////////////////////////////////////////////////////
115
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700116void setup_mdns(int port_in) {
117 port = port_in;
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700118 std::thread(setup_mdns_thread).detach();
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700119
120 // TODO: Make this more robust against a hard kill.
121 atexit(teardown_mdns);
122}
Lingfeng Yang615036f2018-11-28 16:38:36 -0800123
124void register_adb_secure_pairing_service(int port) {
125 std::thread([port]() {
126 register_mdns_service(kADBSecurePairingServiceRefIndex, port);
127 }).detach();
128}
129
130void unregister_adb_secure_pairing_service() {
131 std::thread([]() { unregister_mdns_service(kADBSecurePairingServiceRefIndex); }).detach();
132}
133
134bool is_adb_secure_pairing_service_registered() {
135 std::lock_guard<std::mutex> lock(mdns_lock);
136 return mdns_registered[kADBSecurePairingServiceRefIndex];
137}
138
139void register_adb_secure_connect_service(int port) {
140 std::thread([port]() {
141 register_mdns_service(kADBSecureConnectServiceRefIndex, port);
142 }).detach();
143}
144
145void unregister_adb_secure_connect_service() {
146 std::thread([]() { unregister_mdns_service(kADBSecureConnectServiceRefIndex); }).detach();
147}
148
149bool is_adb_secure_connect_service_registered() {
150 std::lock_guard<std::mutex> lock(mdns_lock);
151 return mdns_registered[kADBSecureConnectServiceRefIndex];
152}