| Index: src/pkg/mdns/mdns_extension_macos.cc
|
| diff --git a/src/pkg/mdns/mdns_extension_macos.cc b/src/pkg/mdns/mdns_extension_macos.cc
|
| index 586f366ee073f60bac2a17eed6256de2a95e9f8a..ba3d7e967f72a855e8705a256796cd5f18239b58 100644
|
| --- a/src/pkg/mdns/mdns_extension_macos.cc
|
| +++ b/src/pkg/mdns/mdns_extension_macos.cc
|
| @@ -10,6 +10,7 @@
|
| #include <string.h>
|
| #include <arpa/inet.h>
|
| #include <pthread.h>
|
| +#include <sys/time.h>
|
|
|
| #include <dns_sd.h>
|
|
|
| @@ -18,8 +19,15 @@
|
|
|
| #include "mdns_extension.h"
|
|
|
| +// Constants.
|
| +static uint32_t kUsecsInSecs = 1000000;
|
| +static uint32_t kMillisecsInUsecs = 1000;
|
| +
|
| // Context passed to the callback receiving the mDNS information.
|
| struct Context {
|
| + DNSServiceRef ref;
|
| + // The timeout waiting for results.
|
| + int timeout;
|
| // The Dart port where the result should be delivered.
|
| Dart_Port port;
|
| };
|
| @@ -30,25 +38,65 @@ static void Fatal(const char *message) {
|
| exit(-1);
|
| }
|
|
|
| +int64_t timeval_to_usec(const struct timeval* tv) {
|
| + return( (int64_t)tv->tv_sec * kUsecsInSecs + tv->tv_usec ) ;
|
| +}
|
| +
|
| +struct timeval* usec_to_timeval(int64_t usec, struct timeval* tv) {
|
| + tv->tv_sec = usec / kUsecsInSecs;
|
| + tv->tv_usec = usec % kUsecsInSecs;
|
| + return tv;
|
| +}
|
| +
|
| +// Free allocated resources associated with a context.
|
| +static void FreeContext(Context* ctx) {
|
| + DNSServiceRefDeallocate(ctx->ref);
|
| + free(ctx);
|
| +}
|
| +
|
| // Code running in the threads started for pumping the results.
|
| static void* ThreadFunction(void* data) {
|
| - DNSServiceRef ref = reinterpret_cast<DNSServiceRef>(data);
|
| -
|
| - // Preocess results until an error occurs.
|
| - DNSServiceErrorType result = kDNSServiceErr_NoError;
|
| - while (result == kDNSServiceErr_NoError) {
|
| - result = DNSServiceProcessResult(ref);
|
| + Context* ctx = reinterpret_cast<Context*>(data);
|
| +
|
| + // Determint the end-time for responses to this request. Timeout from Dart
|
| + // is in milliseconds.
|
| + struct timeval time;
|
| + if (gettimeofday(&time, NULL) == -1) {
|
| + FreeContext(ctx);
|
| + return NULL;
|
| }
|
| - // The expected error from deallocation the ref.
|
| - if (result != kDNSServiceErr_BadReference) {
|
| - fprintf(stderr, "Error from DNSServiceProcessResult: %d\n", result);
|
| + int64_t end_time = timeval_to_usec(&time) + ctx->timeout * kMillisecsInUsecs;
|
| +
|
| + // Setup single file descriptor for select.
|
| + int fd = DNSServiceRefSockFD(ctx->ref);
|
| + fd_set readfds;
|
| + FD_ZERO(&readfds);
|
| + FD_SET(fd, &readfds);
|
| + while (true) {
|
| + struct timeval timeout;
|
| + int64_t timeout_usec = end_time - timeval_to_usec(&time);
|
| + if (timeout_usec <= 0) break;
|
| + usec_to_timeval(timeout_usec, &timeout);
|
| + int rc = select(fd + 1, &readfds, NULL, NULL, &timeout);
|
| + if (rc == -1 && errno == EINTR) continue;
|
| +
|
| + // Terminate the loop if timeout or error.
|
| + if (rc <= 0) break;
|
| +
|
| + // Process the result which is ready.
|
| + DNSServiceErrorType result = DNSServiceProcessResult(ctx->ref);
|
| + if (result != kDNSServiceErr_NoError) break;
|
| +
|
| + // Prepare new timeout.
|
| + if (gettimeofday(&time, NULL) == -1) break;
|
| }
|
|
|
| + FreeContext(ctx);
|
| return NULL;
|
| }
|
|
|
| // Start a thread for receiving results from a specific DNSServiceRef.
|
| -static int StartThread(DNSServiceRef ref) {
|
| +static int StartThread(Context* ctx) {
|
| pthread_attr_t attr;
|
| int result = pthread_attr_init(&attr);
|
| if (result != 0) Fatal("Error from pthread_attr_init");
|
| @@ -57,7 +105,7 @@ static int StartThread(DNSServiceRef ref) {
|
| if (result != 0) Fatal("Error from pthread_attr_setdetachstate");
|
|
|
| pthread_t tid;
|
| - result = pthread_create(&tid, &attr, ThreadFunction, ref);
|
| + result = pthread_create(&tid, &attr, ThreadFunction, ctx);
|
| if (result != 0) Fatal("Error from pthread_create");
|
|
|
| result = pthread_attr_destroy(&attr);
|
| @@ -66,65 +114,77 @@ static int StartThread(DNSServiceRef ref) {
|
| return 0;
|
| }
|
|
|
| -// Callback for results initiated by calling DNSServiceGetAddrInfo.
|
| -static void GetAddrInfoCallback(DNSServiceRef ref,
|
| +
|
| +// Callback for results initiated by calling DNSServiceQueryRecord.
|
| +static void QueryRecordCallback(DNSServiceRef ref,
|
| DNSServiceFlags flags,
|
| uint32_t interfaceIndex,
|
| DNSServiceErrorType errorCode,
|
| - const char *hostname,
|
| - const struct sockaddr *address,
|
| + const char *fullname,
|
| + uint16_t rrtype,
|
| + uint16_t rrclass,
|
| + uint16_t rdlen,
|
| + const void* rdata,
|
| uint32_t ttl,
|
| void *context) {
|
| - if (address->sa_family == AF_INET) {
|
| - const struct sockaddr_in* address_in = (const struct sockaddr_in*)address;
|
| - const uint8_t* addr =
|
| - reinterpret_cast<const uint8_t*>(&address_in->sin_addr.s_addr);
|
| -
|
| - struct Context* ctx = reinterpret_cast<struct Context*>(context);
|
| -
|
| - // Build the response message.
|
| - Dart_CObject cobject_hostname;
|
| - cobject_hostname.type = Dart_CObject_kString;
|
| - cobject_hostname.value.as_string = const_cast<char*>(hostname);
|
| - Dart_CObject cobject_address;
|
| - cobject_address.type = Dart_CObject_kTypedData;
|
| - cobject_address.value.as_typed_data.length = 4;
|
| - cobject_address.value.as_typed_data.type = Dart_TypedData_kUint8;
|
| - cobject_address.value.as_typed_data.values = const_cast<uint8_t*>(addr);
|
| - Dart_CObject cobject_result;
|
| - cobject_result.type = Dart_CObject_kArray;
|
| - Dart_CObject* result_array[] = {&cobject_hostname, &cobject_address};
|
| - cobject_result.value.as_array.length = 2;
|
| - cobject_result.value.as_array.values = result_array;
|
| - Dart_PostCObject(ctx->port, &cobject_result);
|
| -
|
| - // Result received, free allocated data and stop lookup.
|
| - free(ctx);
|
| - DNSServiceRefDeallocate(ref);
|
| + if (rrclass != kDNSServiceClass_IN) return;
|
| +
|
| + struct Context* ctx = reinterpret_cast<struct Context*>(context);
|
| +
|
| + if (rrtype != kDNSServiceType_A &&
|
| + rrtype != kDNSServiceType_SRV &&
|
| + rrtype != kDNSServiceType_PTR) {
|
| + // Ignore unsupported types.
|
| + return;
|
| }
|
| +
|
| + // Build the response message.
|
| + Dart_CObject cobject_fullname;
|
| + cobject_fullname.type = Dart_CObject_kString;
|
| + cobject_fullname.value.as_string = const_cast<char*>(fullname);
|
| + Dart_CObject cobject_type;
|
| + cobject_type.type = Dart_CObject_kInt32;
|
| + cobject_type.value.as_int32 = rrtype;
|
| + Dart_CObject cobject_ttl;
|
| + cobject_ttl.type = Dart_CObject_kInt32;
|
| + cobject_ttl.value.as_int32 = ttl;
|
| + Dart_CObject cobject_data;
|
| + cobject_data.type = Dart_CObject_kTypedData;
|
| + cobject_data.value.as_typed_data.length = rdlen;
|
| + cobject_data.value.as_typed_data.type = Dart_TypedData_kUint8;
|
| + cobject_data.value.as_typed_data.values =
|
| + const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(rdata));
|
| + Dart_CObject cobject_result;
|
| + cobject_result.type = Dart_CObject_kArray;
|
| + Dart_CObject* result_array[] =
|
| + {&cobject_fullname, &cobject_type, &cobject_ttl, &cobject_data};
|
| + cobject_result.value.as_array.length = 4;
|
| + cobject_result.value.as_array.values = result_array;
|
| + Dart_PostCObject(ctx->port, &cobject_result);
|
| }
|
|
|
| // Lookup request from Dart.
|
| -void HandleLookup(Dart_Port port_id, char* hostname) {
|
| +void HandleLookup(Dart_Port port_id, int type, char* fullname, int timeout) {
|
| DNSServiceRef ref;
|
| DNSServiceErrorType result;
|
| struct Context* context =
|
| reinterpret_cast<struct Context*>(malloc(sizeof(struct Context)));
|
| - context->port = port_id;
|
| - result = DNSServiceGetAddrInfo(&ref,
|
| + result = DNSServiceQueryRecord(&ref,
|
| 0,
|
| 0,
|
| - kDNSServiceProtocol_IPv4,
|
| - hostname,
|
| - &GetAddrInfoCallback,
|
| + fullname,
|
| + type,
|
| + kDNSServiceClass_IN,
|
| + &QueryRecordCallback,
|
| context);
|
| if (result != kDNSServiceErr_NoError) {
|
| - fprintf(stderr, "Error from DNSServiceProcessResult: %d\n", result);
|
| + fprintf(stderr, "Error from DNSServiceQueryRecord: %d\n", result);
|
| } else {
|
| - // Start a thread for retreiving the results.
|
| - // TODO(sgjesse): Add a timeout for killing the thread if there
|
| - // are no responses.
|
| - StartThread(ref);
|
| + // Start a thread for pumping the results.
|
| + context->ref = ref;
|
| + context->timeout = timeout;
|
| + context->port = port_id;
|
| + StartThread(context);
|
| }
|
| }
|
|
|
|
|