Casey Dahlin | 10ad15f | 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 | |
| 17 | #include "sysdeps.h" |
| 18 | |
| 19 | #include <chrono> |
| 20 | #include <dns_sd.h> |
| 21 | #include <endian.h> |
| 22 | #include <mutex> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/properties.h> |
| 27 | |
| 28 | using namespace std::chrono_literals; |
| 29 | |
| 30 | static std::mutex& mdns_lock = *new std::mutex(); |
| 31 | static int port; |
| 32 | static DNSServiceRef mdns_ref; |
| 33 | static bool mdns_registered = false; |
| 34 | |
| 35 | static void start_mdns() { |
| 36 | if (android::base::GetProperty("init.svc.mdnsd", "") == "running") { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | android::base::SetProperty("ctl.start", "mdnsd"); |
| 41 | |
| 42 | if (! android::base::WaitForProperty("init.svc.mdnsd", "running", 5s)) { |
| 43 | LOG(ERROR) << "Could not start mdnsd."; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static void mdns_callback(DNSServiceRef /*ref*/, |
| 48 | DNSServiceFlags /*flags*/, |
| 49 | DNSServiceErrorType errorCode, |
| 50 | const char* /*name*/, |
| 51 | const char* /*regtype*/, |
| 52 | const char* /*domain*/, |
| 53 | void* /*context*/) { |
| 54 | if (errorCode != kDNSServiceErr_NoError) { |
| 55 | LOG(ERROR) << "Encountered mDNS registration error (" |
| 56 | << errorCode << ")."; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void setup_mdns_thread(void* /* unused */) { |
| 61 | start_mdns(); |
| 62 | std::lock_guard<std::mutex> lock(mdns_lock); |
| 63 | |
Casey Dahlin | 50b39b4 | 2016-05-20 16:34:51 -0700 | [diff] [blame^] | 64 | std::string hostname = "adb-"; |
| 65 | hostname += android::base::GetProperty("ro.serialno", "unidentified"); |
| 66 | |
| 67 | auto error = DNSServiceRegister(&mdns_ref, 0, 0, hostname.c_str(), |
| 68 | kADBServiceType, nullptr, nullptr, |
| 69 | htobe16((uint16_t)port), 0, nullptr, |
| 70 | mdns_callback, nullptr); |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 71 | |
| 72 | if (error != kDNSServiceErr_NoError) { |
| 73 | LOG(ERROR) << "Could not register mDNS service (" << error << ")."; |
| 74 | mdns_registered = false; |
| 75 | } |
| 76 | |
| 77 | mdns_registered = true; |
| 78 | } |
| 79 | |
| 80 | static void teardown_mdns() { |
| 81 | std::lock_guard<std::mutex> lock(mdns_lock); |
| 82 | |
| 83 | if (mdns_registered) { |
| 84 | DNSServiceRefDeallocate(mdns_ref); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void setup_mdns(int port_in) { |
| 89 | port = port_in; |
| 90 | adb_thread_create(setup_mdns_thread, nullptr, nullptr); |
| 91 | |
| 92 | // TODO: Make this more robust against a hard kill. |
| 93 | atexit(teardown_mdns); |
| 94 | } |