Merge changes from topic 'adb-mdnsd' am: f4f95496ce

am: 1b35c3f471

Change-Id: Ie93a0a2d4361bcc8d18d3433c0cc0369a864c815
This commit is contained in:
Casey Dahlin 2017-03-10 01:21:24 +00:00 committed by android-build-merger
commit b3b862ed2e
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_CFLAGS_darwin := $(LIBADB_darwin_CFLAGS)
LOCAL_SRC_FILES := \ LOCAL_SRC_FILES := \
$(LIBADB_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_darwin := $(LIBADB_darwin_SRC_FILES)
LOCAL_SRC_FILES_linux := $(LIBADB_linux_SRC_FILES) LOCAL_SRC_FILES_linux := $(LIBADB_linux_SRC_FILES)
LOCAL_SRC_FILES_windows := $(LIBADB_windows_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) LOCAL_SANITIZE := $(adb_host_sanitize)
# Even though we're building a static library (and thus there's no link step for # 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. # this to take effect), this adds the includes to our path.
LOCAL_STATIC_LIBRARIES := libcrypto_utils libcrypto libbase LOCAL_STATIC_LIBRARIES := libcrypto_utils libcrypto libbase libmdnssd
LOCAL_STATIC_LIBRARIES_linux := libusb libmdnssd LOCAL_STATIC_LIBRARIES_linux := libusb
LOCAL_STATIC_LIBRARIES_darwin := libusb LOCAL_STATIC_LIBRARIES_darwin := libusb
LOCAL_C_INCLUDES_windows := development/host/windows/usb/api/ LOCAL_C_INCLUDES_windows := development/host/windows/usb/api/
@ -179,7 +176,7 @@ LOCAL_SRC_FILES := \
shell_service_test.cpp \ shell_service_test.cpp \
LOCAL_SANITIZE := $(adb_target_sanitize) 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 LOCAL_SHARED_LIBRARIES := liblog libbase libcutils
include $(BUILD_NATIVE_TEST) include $(BUILD_NATIVE_TEST)
@ -227,9 +224,10 @@ LOCAL_STATIC_LIBRARIES := \
libcrypto \ libcrypto \
libcutils \ libcutils \
libdiagnose_usb \ libdiagnose_usb \
libmdnssd \
libgmock_host libgmock_host
LOCAL_STATIC_LIBRARIES_linux := libusb libmdnssd LOCAL_STATIC_LIBRARIES_linux := libusb
LOCAL_STATIC_LIBRARIES_darwin := libusb LOCAL_STATIC_LIBRARIES_darwin := libusb
# Set entrypoint to wmain from sysdeps_win32.cpp instead of main # Set entrypoint to wmain from sysdeps_win32.cpp instead of main
@ -294,11 +292,12 @@ LOCAL_STATIC_LIBRARIES := \
libcrypto_utils \ libcrypto_utils \
libcrypto \ libcrypto \
libdiagnose_usb \ libdiagnose_usb \
liblog liblog \
libmdnssd
# Don't use libcutils on Windows. # Don't use libcutils on Windows.
LOCAL_STATIC_LIBRARIES_darwin := libcutils LOCAL_STATIC_LIBRARIES_darwin := libcutils
LOCAL_STATIC_LIBRARIES_linux := libcutils libmdnssd LOCAL_STATIC_LIBRARIES_linux := libcutils
LOCAL_STATIC_LIBRARIES_darwin += libusb LOCAL_STATIC_LIBRARIES_darwin += libusb
LOCAL_STATIC_LIBRARIES_linux += 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_lseek(int fd, int pos, int where);
extern int adb_shutdown(int fd); extern int adb_shutdown(int fd);
extern int adb_close(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(). // See the comments for the !defined(_WIN32) version of unix_close().
static __inline__ int unix_close(int fd) static __inline__ int unix_close(int fd)
@ -523,6 +524,12 @@ __inline__ int adb_close(int fd) {
#undef close #undef close
#define close ____xxx_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) static __inline__ int adb_read(int fd, void* buf, size_t len)
{ {

View file

@ -126,10 +126,7 @@ typedef struct FHRec_
SOCKET socket; SOCKET socket;
} u; } u;
int mask;
char name[32]; char name[32];
} FHRec; } FHRec;
#define fh_handle u.handle #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) { static void _fh_socket_init(FH f) {
f->fh_socket = INVALID_SOCKET; f->fh_socket = INVALID_SOCKET;
f->mask = 0;
} }
static int _fh_socket_close( FH f ) { static int _fh_socket_close( FH f ) {
@ -598,7 +594,6 @@ static int _fh_socket_close( FH f ) {
} }
f->fh_socket = INVALID_SOCKET; f->fh_socket = INVALID_SOCKET;
} }
f->mask = 0;
return 0; return 0;
} }
@ -913,6 +908,12 @@ int network_connect(const std::string& host, int port, int type, int timeout, st
return fd; 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 #undef accept
int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen) 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) { if (!fh || !fh->used) {
errno = EBADF; errno = EBADF;
D("Setting nonblocking on bad file descriptor %d", fd);
return false; return false;
} }
if (fh->clazz == &_fh_socket_class) { if (fh->clazz == &_fh_socket_class) {
u_long x = !block; u_long x = !block;
if (ioctlsocket(fh->u.socket, FIONBIO, &x) != 0) { 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 false;
} }
return true; return true;
} else { } else {
errno = ENOTSOCK; errno = ENOTSOCK;
D("Setting nonblocking on non-socket %d", fd);
return false; return false;
} }
} }

View file

@ -18,7 +18,11 @@
#include "transport.h" #include "transport.h"
#ifdef _WIN32
#include <winsock2.h>
#else
#include <arpa/inet.h> #include <arpa/inet.h>
#endif
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
#include <dns_sd.h> #include <dns_sd.h>
@ -31,14 +35,22 @@
static DNSServiceRef service_ref; static DNSServiceRef service_ref;
static fdevent service_ref_fde; static fdevent service_ref_fde;
static void register_service_ip(DNSServiceRef sdRef, // Use adb_DNSServiceRefSockFD() instead of calling DNSServiceRefSockFD()
DNSServiceFlags flags, // directly so that the socket is put through the appropriate compatibility
uint32_t interfaceIndex, // layers to work with the rest of ADB's internal APIs.
DNSServiceErrorType errorCode, static inline int adb_DNSServiceRefSockFD(DNSServiceRef ref) {
const char* hostname, return adb_register_socket(DNSServiceRefSockFD(ref));
const sockaddr* address, }
uint32_t ttl, #define DNSServiceRefSockFD ___xxx_DNSServiceRefSockFD
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);
static void pump_service_ref(int /*fd*/, unsigned ev, void* data) { static void pump_service_ref(int /*fd*/, unsigned ev, void* data) {
DNSServiceRef* ref = reinterpret_cast<DNSServiceRef*>(data); DNSServiceRef* ref = reinterpret_cast<DNSServiceRef*>(data);
@ -66,7 +78,7 @@ class AsyncServiceRef {
DNSServiceRef sdRef_; DNSServiceRef sdRef_;
void Initialize() { void Initialize() {
fdevent_install(&fde_, DNSServiceRefSockFD(sdRef_), fdevent_install(&fde_, adb_DNSServiceRefSockFD(sdRef_),
pump_service_ref, &sdRef_); pump_service_ref, &sdRef_);
fdevent_set(&fde_, FDE_READ); fdevent_set(&fde_, FDE_READ);
initialized_ = true; initialized_ = true;
@ -123,8 +135,9 @@ class ResolvedService : public AsyncServiceRef {
return; return;
} }
if (!inet_ntop(address->sa_family, ip_addr_data, ip_addr, // Winsock version requires the const cast Because Microsoft.
INET6_ADDRSTRLEN)) { if (!inet_ntop(address->sa_family, const_cast<void*>(ip_addr_data),
ip_addr, INET6_ADDRSTRLEN)) {
D("Could not convert IP address to string."); D("Could not convert IP address to string.");
return; return;
} }
@ -141,29 +154,30 @@ class ResolvedService : public AsyncServiceRef {
const uint16_t port_; const uint16_t port_;
}; };
static void register_service_ip(DNSServiceRef /*sdRef*/, static void DNSSD_API register_service_ip(DNSServiceRef /*sdRef*/,
DNSServiceFlags /*flags*/, DNSServiceFlags /*flags*/,
uint32_t /*interfaceIndex*/, uint32_t /*interfaceIndex*/,
DNSServiceErrorType /*errorCode*/, DNSServiceErrorType /*errorCode*/,
const char* /*hostname*/, const char* /*hostname*/,
const sockaddr* address, const sockaddr* address,
uint32_t /*ttl*/, uint32_t /*ttl*/,
void* context) { void* context) {
D("Got IP for service.");
std::unique_ptr<ResolvedService> data( std::unique_ptr<ResolvedService> data(
reinterpret_cast<ResolvedService*>(context)); reinterpret_cast<ResolvedService*>(context));
data->Connect(address); data->Connect(address);
} }
static void register_resolved_mdns_service(DNSServiceRef sdRef, static void DNSSD_API register_resolved_mdns_service(DNSServiceRef sdRef,
DNSServiceFlags flags, DNSServiceFlags flags,
uint32_t interfaceIndex, uint32_t interfaceIndex,
DNSServiceErrorType errorCode, DNSServiceErrorType errorCode,
const char* fullname, const char* fullname,
const char* hosttarget, const char* hosttarget,
uint16_t port, uint16_t port,
uint16_t txtLen, uint16_t txtLen,
const unsigned char* txtRecord, const unsigned char* txtRecord,
void* context); void* context);
class DiscoveredService : public AsyncServiceRef { class DiscoveredService : public AsyncServiceRef {
public: public:
@ -191,16 +205,17 @@ class DiscoveredService : public AsyncServiceRef {
std::string serviceName_; std::string serviceName_;
}; };
static void register_resolved_mdns_service(DNSServiceRef sdRef, static void DNSSD_API register_resolved_mdns_service(DNSServiceRef sdRef,
DNSServiceFlags flags, DNSServiceFlags flags,
uint32_t interfaceIndex, uint32_t interfaceIndex,
DNSServiceErrorType errorCode, DNSServiceErrorType errorCode,
const char* fullname, const char* fullname,
const char* hosttarget, const char* hosttarget,
uint16_t port, uint16_t port,
uint16_t /*txtLen*/, uint16_t /*txtLen*/,
const unsigned char* /*txtRecord*/, const unsigned char* /*txtRecord*/,
void* context) { void* context) {
D("Resolved a service.");
std::unique_ptr<DiscoveredService> discovered( std::unique_ptr<DiscoveredService> discovered(
reinterpret_cast<DiscoveredService*>(context)); reinterpret_cast<DiscoveredService*>(context));
@ -223,14 +238,15 @@ static void register_resolved_mdns_service(DNSServiceRef sdRef,
} }
} }
static void register_mdns_transport(DNSServiceRef sdRef, static void DNSSD_API register_mdns_transport(DNSServiceRef sdRef,
DNSServiceFlags flags, DNSServiceFlags flags,
uint32_t interfaceIndex, uint32_t interfaceIndex,
DNSServiceErrorType errorCode, DNSServiceErrorType errorCode,
const char* serviceName, const char* serviceName,
const char* regtype, const char* regtype,
const char* domain, const char* domain,
void* /*context*/) { void* /*context*/) {
D("Registering a transport.");
if (errorCode != kDNSServiceErr_NoError) { if (errorCode != kDNSServiceErr_NoError) {
D("Got error %d during mDNS browse.", errorCode); D("Got error %d during mDNS browse.", errorCode);
DNSServiceRefDeallocate(sdRef); DNSServiceRefDeallocate(sdRef);
@ -257,7 +273,7 @@ void init_mdns_transport_discovery(void) {
} }
fdevent_install(&service_ref_fde, fdevent_install(&service_ref_fde,
DNSServiceRefSockFD(service_ref), adb_DNSServiceRefSockFD(service_ref),
pump_service_ref, pump_service_ref,
&service_ref); &service_ref);
fdevent_set(&service_ref_fde, FDE_READ); fdevent_set(&service_ref_fde, FDE_READ);