blob: 849378fef978e9a0b29daaa1f0074b07941afbc0 [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
Casey Dahlin13a269e2016-06-23 14:19:37 -070017#include "adb_mdns.h"
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070018#include "sysdeps.h"
19
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070020#include <dns_sd.h>
21#include <endian.h>
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070022#include <unistd.h>
23
Josh Gaoe1dacfc2017-04-12 17:00:49 -070024#include <chrono>
25#include <mutex>
26#include <thread>
27
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070028#include <android-base/logging.h>
29#include <android-base/properties.h>
30
31using namespace std::chrono_literals;
32
33static std::mutex& mdns_lock = *new std::mutex();
34static int port;
35static DNSServiceRef mdns_ref;
36static bool mdns_registered = false;
37
38static void start_mdns() {
39 if (android::base::GetProperty("init.svc.mdnsd", "") == "running") {
40 return;
41 }
42
43 android::base::SetProperty("ctl.start", "mdnsd");
44
45 if (! android::base::WaitForProperty("init.svc.mdnsd", "running", 5s)) {
46 LOG(ERROR) << "Could not start mdnsd.";
47 }
48}
49
50static void mdns_callback(DNSServiceRef /*ref*/,
51 DNSServiceFlags /*flags*/,
52 DNSServiceErrorType errorCode,
53 const char* /*name*/,
54 const char* /*regtype*/,
55 const char* /*domain*/,
56 void* /*context*/) {
57 if (errorCode != kDNSServiceErr_NoError) {
58 LOG(ERROR) << "Encountered mDNS registration error ("
59 << errorCode << ").";
60 }
61}
62
Josh Gaoe1dacfc2017-04-12 17:00:49 -070063static void setup_mdns_thread() {
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070064 start_mdns();
65 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
70 auto error = DNSServiceRegister(&mdns_ref, 0, 0, hostname.c_str(),
71 kADBServiceType, nullptr, nullptr,
72 htobe16((uint16_t)port), 0, nullptr,
73 mdns_callback, nullptr);
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070074
75 if (error != kDNSServiceErr_NoError) {
76 LOG(ERROR) << "Could not register mDNS service (" << error << ").";
77 mdns_registered = false;
78 }
79
80 mdns_registered = true;
81}
82
83static void teardown_mdns() {
84 std::lock_guard<std::mutex> lock(mdns_lock);
85
86 if (mdns_registered) {
87 DNSServiceRefDeallocate(mdns_ref);
88 }
89}
90
91void setup_mdns(int port_in) {
92 port = port_in;
Josh Gaoe1dacfc2017-04-12 17:00:49 -070093 std::thread(setup_mdns_thread).detach();
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070094
95 // TODO: Make this more robust against a hard kill.
96 atexit(teardown_mdns);
97}