Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 1 | /* |
| 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 Yang | 615036f | 2018-11-28 16:38:36 -0800 | [diff] [blame^] | 17 | #include "mdns.h" |
Casey Dahlin | 13a269e | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 18 | #include "adb_mdns.h" |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 19 | #include "sysdeps.h" |
| 20 | |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 21 | #include <dns_sd.h> |
| 22 | #include <endian.h> |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 25 | #include <chrono> |
| 26 | #include <mutex> |
| 27 | #include <thread> |
| 28 | |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 29 | #include <android-base/logging.h> |
| 30 | #include <android-base/properties.h> |
| 31 | |
| 32 | using namespace std::chrono_literals; |
| 33 | |
| 34 | static std::mutex& mdns_lock = *new std::mutex(); |
| 35 | static int port; |
Lingfeng Yang | be49d8a | 2018-11-16 22:47:31 -0800 | [diff] [blame] | 36 | static DNSServiceRef mdns_refs[kNumADBDNSServices]; |
| 37 | static bool mdns_registered[kNumADBDNSServices]; |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 38 | |
| 39 | static 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 | |
| 51 | static 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 Yang | 615036f | 2018-11-28 16:38:36 -0800 | [diff] [blame^] | 64 | static void register_mdns_service(int index, int port) { |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 65 | std::lock_guard<std::mutex> lock(mdns_lock); |
| 66 | |
Casey Dahlin | 1fe3cae | 2016-05-20 16:34:51 -0700 | [diff] [blame] | 67 | std::string hostname = "adb-"; |
| 68 | hostname += android::base::GetProperty("ro.serialno", "unidentified"); |
| 69 | |
Lingfeng Yang | 615036f | 2018-11-28 16:38:36 -0800 | [diff] [blame^] | 70 | 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 Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 73 | |
Lingfeng Yang | 615036f | 2018-11-28 16:38:36 -0800 | [diff] [blame^] | 74 | if (error != kDNSServiceErr_NoError) { |
| 75 | LOG(ERROR) << "Could not register mDNS service " << kADBDNSServices[index] << ", error (" |
| 76 | << error << ")."; |
| 77 | mdns_registered[index] = false; |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Lingfeng Yang | 615036f | 2018-11-28 16:38:36 -0800 | [diff] [blame^] | 80 | mdns_registered[index] = true; |
| 81 | |
| 82 | LOG(INFO) << "adbd mDNS service " << kADBDNSServices[index] |
| 83 | << " registered: " << mdns_registered[index]; |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Lingfeng Yang | 615036f | 2018-11-28 16:38:36 -0800 | [diff] [blame^] | 86 | static void unregister_mdns_service(int index) { |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 87 | std::lock_guard<std::mutex> lock(mdns_lock); |
| 88 | |
Lingfeng Yang | 615036f | 2018-11-28 16:38:36 -0800 | [diff] [blame^] | 89 | if (mdns_registered[index]) { |
| 90 | DNSServiceRefDeallocate(mdns_refs[index]); |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Lingfeng Yang | 615036f | 2018-11-28 16:38:36 -0800 | [diff] [blame^] | 94 | static void register_base_mdns_transport() { |
| 95 | register_mdns_service(kADBTransportServiceRefIndex, port); |
| 96 | } |
| 97 | |
| 98 | static 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. |
| 108 | static void teardown_mdns() { |
| 109 | for (int i = 0; i < kNumADBDNSServices; ++i) { |
| 110 | unregister_mdns_service(i); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Public interface///////////////////////////////////////////////////////////// |
| 115 | |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 116 | void setup_mdns(int port_in) { |
| 117 | port = port_in; |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 118 | std::thread(setup_mdns_thread).detach(); |
Casey Dahlin | 6cd5e0b | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 119 | |
| 120 | // TODO: Make this more robust against a hard kill. |
| 121 | atexit(teardown_mdns); |
| 122 | } |
Lingfeng Yang | 615036f | 2018-11-28 16:38:36 -0800 | [diff] [blame^] | 123 | |
| 124 | void register_adb_secure_pairing_service(int port) { |
| 125 | std::thread([port]() { |
| 126 | register_mdns_service(kADBSecurePairingServiceRefIndex, port); |
| 127 | }).detach(); |
| 128 | } |
| 129 | |
| 130 | void unregister_adb_secure_pairing_service() { |
| 131 | std::thread([]() { unregister_mdns_service(kADBSecurePairingServiceRefIndex); }).detach(); |
| 132 | } |
| 133 | |
| 134 | bool is_adb_secure_pairing_service_registered() { |
| 135 | std::lock_guard<std::mutex> lock(mdns_lock); |
| 136 | return mdns_registered[kADBSecurePairingServiceRefIndex]; |
| 137 | } |
| 138 | |
| 139 | void register_adb_secure_connect_service(int port) { |
| 140 | std::thread([port]() { |
| 141 | register_mdns_service(kADBSecureConnectServiceRefIndex, port); |
| 142 | }).detach(); |
| 143 | } |
| 144 | |
| 145 | void unregister_adb_secure_connect_service() { |
| 146 | std::thread([]() { unregister_mdns_service(kADBSecureConnectServiceRefIndex); }).detach(); |
| 147 | } |
| 148 | |
| 149 | bool is_adb_secure_connect_service_registered() { |
| 150 | std::lock_guard<std::mutex> lock(mdns_lock); |
| 151 | return mdns_registered[kADBSecureConnectServiceRefIndex]; |
| 152 | } |