1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define TRACE_TAG TRANSPORT
#include "transport.h"
#ifdef _WIN32
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include <thread>
#include <android-base/stringprintf.h>
#include <dns_sd.h>
#include "adb_mdns.h"
#include "adb_trace.h"
#include "fdevent.h"
#include "sysdeps.h"
static DNSServiceRef service_ref;
static fdevent service_ref_fde;
// Use adb_DNSServiceRefSockFD() instead of calling DNSServiceRefSockFD()
// directly so that the socket is put through the appropriate compatibility
// layers to work with the rest of ADB's internal APIs.
static inline int adb_DNSServiceRefSockFD(DNSServiceRef ref) {
return adb_register_socket(DNSServiceRefSockFD(ref));
}
#define DNSServiceRefSockFD ___xxx_DNSServiceRefSockFD
static void DNSSD_API register_service_ip(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* hostname,
const sockaddr* address,
uint32_t ttl,
void* context);
static void pump_service_ref(int /*fd*/, unsigned ev, void* data) {
DNSServiceRef* ref = reinterpret_cast<DNSServiceRef*>(data);
if (ev & FDE_READ)
DNSServiceProcessResult(*ref);
}
class AsyncServiceRef {
public:
bool Initialized() {
return initialized_;
}
virtual ~AsyncServiceRef() {
if (! initialized_) {
return;
}
DNSServiceRefDeallocate(sdRef_);
fdevent_remove(&fde_);
}
protected:
DNSServiceRef sdRef_;
void Initialize() {
fdevent_install(&fde_, adb_DNSServiceRefSockFD(sdRef_),
pump_service_ref, &sdRef_);
fdevent_set(&fde_, FDE_READ);
initialized_ = true;
}
private:
bool initialized_;
fdevent fde_;
};
class ResolvedService : public AsyncServiceRef {
public:
virtual ~ResolvedService() = default;
ResolvedService(std::string name, uint32_t interfaceIndex,
const char* hosttarget, uint16_t port) :
name_(name),
port_(port) {
/* TODO: We should be able to get IPv6 support by adding
* kDNSServiceProtocol_IPv6 to the flags below. However, when we do
* this, we get served link-local addresses that are usually useless to
* connect to. What's more, we seem to /only/ get those and nothing else.
* If we want IPv6 in the future we'll have to figure out why.
*/
DNSServiceErrorType ret =
DNSServiceGetAddrInfo(
&sdRef_, 0, interfaceIndex,
kDNSServiceProtocol_IPv4, hosttarget,
register_service_ip, reinterpret_cast<void*>(this));
if (ret != kDNSServiceErr_NoError) {
D("Got %d from DNSServiceGetAddrInfo.", ret);
} else {
Initialize();
}
}
void Connect(const sockaddr* address) {
char ip_addr[INET6_ADDRSTRLEN];
const void* ip_addr_data;
const char* addr_format;
if (address->sa_family == AF_INET) {
ip_addr_data =
&reinterpret_cast<const sockaddr_in*>(address)->sin_addr;
addr_format = "%s:%hu";
} else if (address->sa_family == AF_INET6) {
ip_addr_data =
&reinterpret_cast<const sockaddr_in6*>(address)->sin6_addr;
addr_format = "[%s]:%hu";
} else { // Should be impossible
D("mDNS resolved non-IP address.");
return;
}
// Winsock version requires the const cast Because Microsoft.
if (!inet_ntop(address->sa_family, const_cast<void*>(ip_addr_data),
ip_addr, INET6_ADDRSTRLEN)) {
D("Could not convert IP address to string.");
return;
}
std::string response;
connect_device(android::base::StringPrintf(addr_format, ip_addr, port_),
&response);
D("Connect to %s (%s:%hu) : %s", name_.c_str(), ip_addr, port_,
response.c_str());
}
private:
std::string name_;
const uint16_t port_;
};
static void DNSSD_API register_service_ip(DNSServiceRef /*sdRef*/,
DNSServiceFlags /*flags*/,
uint32_t /*interfaceIndex*/,
DNSServiceErrorType /*errorCode*/,
const char* /*hostname*/,
const sockaddr* address,
uint32_t /*ttl*/,
void* context) {
D("Got IP for service.");
std::unique_ptr<ResolvedService> data(
reinterpret_cast<ResolvedService*>(context));
data->Connect(address);
}
static void DNSSD_API register_resolved_mdns_service(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* fullname,
const char* hosttarget,
uint16_t port,
uint16_t txtLen,
const unsigned char* txtRecord,
void* context);
class DiscoveredService : public AsyncServiceRef {
public:
DiscoveredService(uint32_t interfaceIndex, const char* serviceName,
const char* regtype, const char* domain)
: serviceName_(serviceName) {
DNSServiceErrorType ret =
DNSServiceResolve(&sdRef_, 0, interfaceIndex, serviceName, regtype,
domain, register_resolved_mdns_service,
reinterpret_cast<void*>(this));
if (ret != kDNSServiceErr_NoError) {
D("Got %d from DNSServiceResolve.", ret);
} else {
Initialize();
}
}
const char* ServiceName() {
return serviceName_.c_str();
}
private:
std::string serviceName_;
};
static void DNSSD_API register_resolved_mdns_service(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* fullname,
const char* hosttarget,
uint16_t port,
uint16_t /*txtLen*/,
const unsigned char* /*txtRecord*/,
void* context) {
D("Resolved a service.");
std::unique_ptr<DiscoveredService> discovered(
reinterpret_cast<DiscoveredService*>(context));
if (errorCode != kDNSServiceErr_NoError) {
D("Got error %d resolving service.", errorCode);
return;
}
auto resolved =
new ResolvedService(discovered->ServiceName(),
interfaceIndex, hosttarget, ntohs(port));
if (! resolved->Initialized()) {
delete resolved;
}
if (flags) { /* Only ever equals MoreComing or 0 */
discovered.release();
}
}
static void DNSSD_API register_mdns_transport(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* serviceName,
const char* regtype,
const char* domain,
void* /*context*/) {
D("Registering a transport.");
if (errorCode != kDNSServiceErr_NoError) {
D("Got error %d during mDNS browse.", errorCode);
DNSServiceRefDeallocate(sdRef);
fdevent_remove(&service_ref_fde);
return;
}
auto discovered = new DiscoveredService(interfaceIndex, serviceName,
regtype, domain);
if (! discovered->Initialized()) {
delete discovered;
}
}
void init_mdns_transport_discovery_thread(void) {
DNSServiceErrorType errorCode = DNSServiceBrowse(&service_ref, 0, 0, kADBServiceType, nullptr,
register_mdns_transport, nullptr);
if (errorCode != kDNSServiceErr_NoError) {
D("Got %d initiating mDNS browse.", errorCode);
return;
}
fdevent_run_on_main_thread([]() {
fdevent_install(&service_ref_fde, adb_DNSServiceRefSockFD(service_ref), pump_service_ref,
&service_ref);
fdevent_set(&service_ref_fde, FDE_READ);
});
}
void init_mdns_transport_discovery(void) {
std::thread(init_mdns_transport_discovery_thread).detach();
}
|