Merge changes from topic 'adb-mdnsd'

am: f4f95496ce

Change-Id: I1749f913b8c5cca64880d3eec3009f850bc9141e
This commit is contained in:
Casey Dahlin 2017-03-10 01:16:46 +00:00 committed by android-build-merger
commit 1b35c3f471
4 changed files with 92 additions and 65 deletions

View File

@ -143,22 +143,19 @@ LOCAL_CFLAGS_linux := $(LIBADB_linux_CFLAGS)
LOCAL_CFLAGS_darwin := $(LIBADB_darwin_CFLAGS)
LOCAL_SRC_FILES := \
$(LIBADB_SRC_FILES) \
adb_auth_host.cpp
adb_auth_host.cpp \
transport_mdns.cpp \
LOCAL_SRC_FILES_darwin := $(LIBADB_darwin_SRC_FILES)
LOCAL_SRC_FILES_linux := $(LIBADB_linux_SRC_FILES)
LOCAL_SRC_FILES_windows := $(LIBADB_windows_SRC_FILES)
LOCAL_SRC_FILES_linux += transport_mdns.cpp
LOCAL_SRC_FILES_darwin += transport_mdns.cpp
LOCAL_SRC_FILES_windows += transport_mdns_unsupported.cpp
LOCAL_SANITIZE := $(adb_host_sanitize)
# Even though we're building a static library (and thus there's no link step for
# this to take effect), this adds the includes to our path.
LOCAL_STATIC_LIBRARIES := libcrypto_utils libcrypto libbase
LOCAL_STATIC_LIBRARIES_linux := libusb libmdnssd
LOCAL_STATIC_LIBRARIES := libcrypto_utils libcrypto libbase libmdnssd
LOCAL_STATIC_LIBRARIES_linux := libusb
LOCAL_STATIC_LIBRARIES_darwin := libusb
LOCAL_C_INCLUDES_windows := development/host/windows/usb/api/
@ -179,7 +176,7 @@ LOCAL_SRC_FILES := \
shell_service_test.cpp \
LOCAL_SANITIZE := $(adb_target_sanitize)
LOCAL_STATIC_LIBRARIES := libadbd libcrypto_utils libcrypto libusb
LOCAL_STATIC_LIBRARIES := libadbd libcrypto_utils libcrypto libusb libmdnssd
LOCAL_SHARED_LIBRARIES := liblog libbase libcutils
include $(BUILD_NATIVE_TEST)
@ -227,9 +224,10 @@ LOCAL_STATIC_LIBRARIES := \
libcrypto \
libcutils \
libdiagnose_usb \
libmdnssd \
libgmock_host
LOCAL_STATIC_LIBRARIES_linux := libusb libmdnssd
LOCAL_STATIC_LIBRARIES_linux := libusb
LOCAL_STATIC_LIBRARIES_darwin := libusb
# Set entrypoint to wmain from sysdeps_win32.cpp instead of main
@ -294,11 +292,12 @@ LOCAL_STATIC_LIBRARIES := \
libcrypto_utils \
libcrypto \
libdiagnose_usb \
liblog
liblog \
libmdnssd
# Don't use libcutils on Windows.
LOCAL_STATIC_LIBRARIES_darwin := libcutils
LOCAL_STATIC_LIBRARIES_linux := libcutils libmdnssd
LOCAL_STATIC_LIBRARIES_linux := libcutils
LOCAL_STATIC_LIBRARIES_darwin += libusb
LOCAL_STATIC_LIBRARIES_linux += libusb

View File

@ -197,6 +197,7 @@ extern int adb_write(int fd, const void* buf, int len);
extern int adb_lseek(int fd, int pos, int where);
extern int adb_shutdown(int fd);
extern int adb_close(int fd);
extern int adb_register_socket(SOCKET s);
// See the comments for the !defined(_WIN32) version of unix_close().
static __inline__ int unix_close(int fd)
@ -523,6 +524,12 @@ __inline__ int adb_close(int fd) {
#undef close
#define close ____xxx_close
// On Windows, ADB has an indirection layer for file descriptors. If we get a
// Win32 SOCKET object from an external library, we have to map it in to that
// indirection layer, which this does.
__inline__ int adb_register_socket(int s) {
return s;
}
static __inline__ int adb_read(int fd, void* buf, size_t len)
{

View File

@ -126,10 +126,7 @@ typedef struct FHRec_
SOCKET socket;
} u;
int mask;
char name[32];
} FHRec;
#define fh_handle u.handle
@ -577,7 +574,6 @@ extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
static void _fh_socket_init(FH f) {
f->fh_socket = INVALID_SOCKET;
f->mask = 0;
}
static int _fh_socket_close( FH f ) {
@ -598,7 +594,6 @@ static int _fh_socket_close( FH f ) {
}
f->fh_socket = INVALID_SOCKET;
}
f->mask = 0;
return 0;
}
@ -913,6 +908,12 @@ int network_connect(const std::string& host, int port, int type, int timeout, st
return fd;
}
int adb_register_socket(SOCKET s) {
FH f = _fh_alloc( &_fh_socket_class );
f->fh_socket = s;
return _fh_to_int(f);
}
#undef accept
int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
{
@ -1113,18 +1114,22 @@ bool set_file_block_mode(int fd, bool block) {
if (!fh || !fh->used) {
errno = EBADF;
D("Setting nonblocking on bad file descriptor %d", fd);
return false;
}
if (fh->clazz == &_fh_socket_class) {
u_long x = !block;
if (ioctlsocket(fh->u.socket, FIONBIO, &x) != 0) {
_socket_set_errno(WSAGetLastError());
int error = WSAGetLastError();
_socket_set_errno(error);
D("Setting %d nonblocking failed (%d)", fd, error);
return false;
}
return true;
} else {
errno = ENOTSOCK;
D("Setting nonblocking on non-socket %d", fd);
return false;
}
}

View File

@ -18,7 +18,11 @@
#include "transport.h"
#ifdef _WIN32
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include <android-base/stringprintf.h>
#include <dns_sd.h>
@ -31,14 +35,22 @@
static DNSServiceRef service_ref;
static fdevent service_ref_fde;
static void register_service_ip(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* hostname,
const sockaddr* address,
uint32_t ttl,
void* context);
// 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);
@ -66,7 +78,7 @@ class AsyncServiceRef {
DNSServiceRef sdRef_;
void Initialize() {
fdevent_install(&fde_, DNSServiceRefSockFD(sdRef_),
fdevent_install(&fde_, adb_DNSServiceRefSockFD(sdRef_),
pump_service_ref, &sdRef_);
fdevent_set(&fde_, FDE_READ);
initialized_ = true;
@ -123,8 +135,9 @@ class ResolvedService : public AsyncServiceRef {
return;
}
if (!inet_ntop(address->sa_family, ip_addr_data, ip_addr,
INET6_ADDRSTRLEN)) {
// 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;
}
@ -141,29 +154,30 @@ class ResolvedService : public AsyncServiceRef {
const uint16_t port_;
};
static void 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 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 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);
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:
@ -191,16 +205,17 @@ class DiscoveredService : public AsyncServiceRef {
std::string serviceName_;
};
static void 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) {
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));
@ -223,14 +238,15 @@ static void register_resolved_mdns_service(DNSServiceRef sdRef,
}
}
static void register_mdns_transport(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* serviceName,
const char* regtype,
const char* domain,
void* /*context*/) {
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);
@ -257,7 +273,7 @@ void init_mdns_transport_discovery(void) {
}
fdevent_install(&service_ref_fde,
DNSServiceRefSockFD(service_ref),
adb_DNSServiceRefSockFD(service_ref),
pump_service_ref,
&service_ref);
fdevent_set(&service_ref_fde, FDE_READ);