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