From e31663c4a2fb2f8bf22efc237c3092242cc9bac2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 28 Jun 2010 21:33:03 +0200 Subject: [PATCH] libdns_sd: implement DNSServiceQueryRecord() Modified patch, originally from "AlfonsName". http://avahi.org/ticket/270 --- avahi-compat-libdns_sd/compat.c | 105 +++++++++++++++++++++++++++ avahi-compat-libdns_sd/unsupported.c | 17 ----- 2 files changed, 105 insertions(+), 17 deletions(-) diff --git a/avahi-compat-libdns_sd/compat.c b/avahi-compat-libdns_sd/compat.c index 360abd7..3ea359e 100644 --- a/avahi-compat-libdns_sd/compat.c +++ b/avahi-compat-libdns_sd/compat.c @@ -77,11 +77,13 @@ struct _DNSServiceRef_t { DNSServiceResolveReply service_resolver_callback; DNSServiceDomainEnumReply domain_browser_callback; DNSServiceRegisterReply service_register_callback; + DNSServiceQueryRecordReply query_resolver_callback; AvahiClient *client; AvahiServiceBrowser *service_browser; AvahiServiceResolver *service_resolver; AvahiDomainBrowser *domain_browser; + AvahiRecordBrowser *record_browser; struct type_info type_info; char *service_name, *service_name_chosen, *service_domain, *service_host; @@ -582,6 +584,8 @@ static void generic_client_callback(AvahiClient *s, AvahiClientState state, void sdref->service_resolver_callback(sdref, 0, 0, error, NULL, NULL, 0, 0, NULL, sdref->context); else if (sdref->domain_browser_callback) sdref->domain_browser_callback(sdref, 0, 0, error, NULL, sdref->context); + else if (sdref->query_resolver_callback) + sdref->query_resolver_callback(sdref, 0, 0, error, NULL, 0, 0, 0, NULL, 0, sdref->context); break; @@ -1265,3 +1269,104 @@ finish: return ret; } +static void query_resolver_callback( + AvahiRecordBrowser *r, + AvahiIfIndex interface, + AVAHI_GCC_UNUSED AvahiProtocol protocol, + AvahiBrowserEvent event, + const char *name, + uint16_t clazz, + uint16_t type, + const void* rdata, + size_t size, + AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, + void *userdata) { + + DNSServiceRef sdref = userdata; + + assert(r); + assert(sdref); + assert(sdref->n_ref >= 1); + + switch (event) { + + case AVAHI_BROWSER_NEW: + case AVAHI_BROWSER_REMOVE: { + + DNSServiceFlags qflags = 0; + if (event == AVAHI_BROWSER_NEW) + qflags |= kDNSServiceFlagsAdd; + + sdref->query_resolver_callback(sdref, qflags, interface, kDNSServiceErr_NoError, name, type, clazz, size, rdata, 0, sdref->context); + break; + } + + case AVAHI_BROWSER_ALL_FOR_NOW: + case AVAHI_BROWSER_CACHE_EXHAUSTED: + /* not implemented */ + break; + + case AVAHI_BROWSER_FAILURE: + sdref->query_resolver_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, 0, 0, 0, NULL, 0, sdref->context); + break; + } +} + +DNSServiceErrorType DNSSD_API DNSServiceQueryRecord ( + DNSServiceRef *ret_sdref, + DNSServiceFlags flags, + uint32_t interface, + const char *fullname, + uint16_t type, + uint16_t clazz, + DNSServiceQueryRecordReply callback, + void *context) { + + DNSServiceErrorType ret = kDNSServiceErr_Unknown; + int error; + DNSServiceRef sdref = NULL; + AvahiIfIndex ifindex; + + AVAHI_WARN_LINKAGE; + + if (!ret_sdref || !fullname) + return kDNSServiceErr_BadParam; + *ret_sdref = NULL; + + if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) { + AVAHI_WARN_UNSUPPORTED; + return kDNSServiceErr_Unsupported; + } + + if (!(sdref = sdref_new())) + return kDNSServiceErr_Unknown; + + sdref->context = context; + sdref->query_resolver_callback = callback; + + ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); + + if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) { + ret = map_error(error); + goto finish; + } + + ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface; + + if (!(sdref->record_browser = avahi_record_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, fullname, clazz, type, 0, query_resolver_callback, sdref))) { + ret = map_error(avahi_client_errno(sdref->client)); + goto finish; + } + + ret = kDNSServiceErr_NoError; + *ret_sdref = sdref; + +finish: + + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + + if (ret != kDNSServiceErr_NoError) + DNSServiceRefDeallocate(sdref); + + return ret; +} diff --git a/avahi-compat-libdns_sd/unsupported.c b/avahi-compat-libdns_sd/unsupported.c index fe39eb7..4e94f70 100644 --- a/avahi-compat-libdns_sd/unsupported.c +++ b/avahi-compat-libdns_sd/unsupported.c @@ -45,21 +45,6 @@ DNSServiceErrorType DNSSD_API DNSServiceRegisterRecord ( return kDNSServiceErr_Unsupported; } -DNSServiceErrorType DNSSD_API DNSServiceQueryRecord ( - AVAHI_GCC_UNUSED DNSServiceRef *sdRef, - AVAHI_GCC_UNUSED DNSServiceFlags flags, - AVAHI_GCC_UNUSED uint32_t interfaceIndex, - AVAHI_GCC_UNUSED const char *fullname, - AVAHI_GCC_UNUSED uint16_t rrtype, - AVAHI_GCC_UNUSED uint16_t rrclass, - AVAHI_GCC_UNUSED DNSServiceQueryRecordReply callBack, - AVAHI_GCC_UNUSED void *context) { - - AVAHI_WARN_UNSUPPORTED; - - return kDNSServiceErr_Unsupported; -} - DNSServiceErrorType DNSSD_API DNSServiceReconfirmRecord ( AVAHI_GCC_UNUSED DNSServiceFlags flags, AVAHI_GCC_UNUSED uint32_t interfaceIndex, @@ -103,5 +88,3 @@ DNSServiceErrorType DNSSD_API DNSServiceRemoveRecord( return kDNSServiceErr_Unsupported; } - - -- 2.39.5