X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-compat-libdns_sd%2Fcompat.c;h=b4f2fc6f3762529c5a2b18e8fea218ff42156faf;hb=d4fd0d73b1d646f3708f1fdcbca0d494dda7e475;hp=9b7ff8b3f7507df3863e2ca74b3af449fa08a9f0;hpb=45513c3b363228f92572d853e63085b5db60c166;p=catta diff --git a/avahi-compat-libdns_sd/compat.c b/avahi-compat-libdns_sd/compat.c index 9b7ff8b..b4f2fc6 100644 --- a/avahi-compat-libdns_sd/compat.c +++ b/avahi-compat-libdns_sd/compat.c @@ -31,13 +31,20 @@ #include #include #include +#include + +#include +#include #include #include #include #include #include + #include +#include +#include #include "warn.h" #include "dns_sd.h" @@ -45,7 +52,8 @@ enum { COMMAND_POLL = 'p', COMMAND_QUIT = 'q', - COMMAND_POLL_DONE = 'P' + COMMAND_POLL_DONE = 'P', + COMMAND_POLL_FAILED = 'F' }; struct _DNSServiceRef_t { @@ -59,7 +67,7 @@ struct _DNSServiceRef_t { int thread_running; pthread_mutex_t mutex; - + void *context; DNSServiceBrowseReply service_browser_callback; DNSServiceResolveReply service_resolver_callback; @@ -103,7 +111,7 @@ static DNSServiceErrorType map_error(int error) { return kDNSServiceErr_BadParam; - case AVAHI_ERR_LOCAL_COLLISION: + case AVAHI_ERR_COLLISION: return kDNSServiceErr_NameConflict; case AVAHI_ERR_TOO_MANY_CLIENTS: @@ -137,7 +145,7 @@ static DNSServiceErrorType map_error(int error) { case AVAHI_ERR_INVALID_CONFIG: case AVAHI_ERR_TIMEOUT: case AVAHI_ERR_DBUS_ERROR: - case AVAHI_ERR_NOT_CONNECTED: + case AVAHI_ERR_DISCONNECTED: case AVAHI_ERR_NO_DAEMON: break; @@ -222,22 +230,32 @@ static void * thread_func(void *data) { switch (command) { - case COMMAND_POLL: + case COMMAND_POLL: { + int ret; ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); - - if (avahi_simple_poll_run(sdref->simple_poll) < 0) { - fprintf(stderr, __FILE__": avahi_simple_poll_run() failed.\n"); - ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + + for (;;) { + errno = 0; + + if ((ret = avahi_simple_poll_run(sdref->simple_poll)) < 0) { + + if (errno == EINTR) + continue; + + fprintf(stderr, __FILE__": avahi_simple_poll_run() failed: %s\n", strerror(errno)); + } + break; } ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); - if (write_command(sdref->thread_fd, COMMAND_POLL_DONE) < 0) + if (write_command(sdref->thread_fd, ret < 0 ? COMMAND_POLL_FAILED : COMMAND_POLL_DONE) < 0) break; break; + } case COMMAND_QUIT: return NULL; @@ -321,7 +339,6 @@ static void sdref_free(DNSServiceRef sdref) { if (sdref->simple_poll) avahi_simple_poll_free(sdref->simple_poll); - if (sdref->thread_fd >= 0) close(sdref->thread_fd); @@ -357,8 +374,8 @@ static void sdref_unref(DNSServiceRef sdref) { } int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdref) { - assert(sdref); - assert(sdref->n_ref >= 1); + if (!sdref || sdref->n_ref <= 0) + return -1; AVAHI_WARN_LINKAGE; @@ -373,9 +390,9 @@ DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) { AVAHI_WARN_LINKAGE; - sdref_ref(sdref); - ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); + + sdref_ref(sdref); /* Cleanup notification socket */ if (read_command(sdref->main_fd) != COMMAND_POLL_DONE) @@ -400,31 +417,29 @@ DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) { finish: - ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); - sdref_unref(sdref); + + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); return ret; } void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdref) { - assert(sdref); - assert(sdref->n_ref >= 1); - AVAHI_WARN_LINKAGE; - sdref_unref(sdref); + if (sdref) + sdref_unref(sdref); } static void service_browser_callback( AvahiServiceBrowser *b, AvahiIfIndex interface, - AvahiProtocol protocol, + AVAHI_GCC_UNUSED AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, - AvahiLookupResultFlags flags, + AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, void *userdata) { DNSServiceRef sdref = userdata; @@ -457,28 +472,29 @@ static void service_browser_callback( static void generic_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) { DNSServiceRef sdref = userdata; - + int error = kDNSServiceErr_Unknown; + assert(s); assert(sdref); assert(sdref->n_ref >= 1); switch (state) { - case AVAHI_CLIENT_DISCONNECTED: { + + case AVAHI_CLIENT_FAILURE: if (sdref->service_browser_callback) - sdref->service_browser_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, NULL, sdref->context); + sdref->service_browser_callback(sdref, 0, 0, error, NULL, NULL, NULL, sdref->context); else if (sdref->service_resolver_callback) - sdref->service_resolver_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, 0, 0, NULL, sdref->context); + 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, kDNSServiceErr_Unknown, NULL, sdref->context); + sdref->domain_browser_callback(sdref, 0, 0, error, NULL, sdref->context); break; - } case AVAHI_CLIENT_S_RUNNING: case AVAHI_CLIENT_S_COLLISION: - case AVAHI_CLIENT_S_INVALID: case AVAHI_CLIENT_S_REGISTERING: + case AVAHI_CLIENT_CONNECTING: break; } } @@ -501,7 +517,6 @@ DNSServiceErrorType DNSSD_API DNSServiceBrowse( assert(ret_sdref); assert(regtype); - assert(domain); assert(callback); if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) { @@ -517,7 +532,7 @@ DNSServiceErrorType DNSSD_API DNSServiceBrowse( ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); - if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) { + 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; } @@ -545,16 +560,16 @@ finish: static void service_resolver_callback( AvahiServiceResolver *r, AvahiIfIndex interface, - AvahiProtocol protocol, + AVAHI_GCC_UNUSED AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, - const AvahiAddress *a, + AVAHI_GCC_UNUSED const AvahiAddress *a, uint16_t port, AvahiStringList *txt, - AvahiLookupResultFlags flags, + AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, void *userdata) { DNSServiceRef sdref = userdata; @@ -630,7 +645,7 @@ DNSServiceErrorType DNSSD_API DNSServiceResolve( ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); - if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) { + 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; } @@ -677,10 +692,10 @@ int DNSSD_API DNSServiceConstructFullName ( static void domain_browser_callback( AvahiDomainBrowser *b, AvahiIfIndex interface, - AvahiProtocol protocol, + AVAHI_GCC_UNUSED AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, - AvahiLookupResultFlags flags, + AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, void *userdata) { DNSServiceRef sdref = userdata; @@ -742,7 +757,7 @@ DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains( ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); - if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) { + 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; } @@ -775,7 +790,8 @@ static void reg_report_error(DNSServiceRef sdref, DNSServiceErrorType error) { assert(sdref); assert(sdref->n_ref >= 1); - assert(sdref->service_register_callback); + if (!sdref->service_register_callback) + return; regtype = add_trailing_dot(sdref->service_regtype, regtype_fixed, sizeof(regtype_fixed)); domain = add_trailing_dot(sdref->service_domain, domain_fixed, sizeof(domain_fixed)); @@ -845,12 +861,10 @@ static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* us return; switch (state) { - case AVAHI_CLIENT_DISCONNECTED: { - - reg_report_error(sdref, kDNSServiceErr_NoError); + case AVAHI_CLIENT_FAILURE: + reg_report_error(sdref, kDNSServiceErr_Unknown); break; - } - + case AVAHI_CLIENT_S_RUNNING: { int ret; @@ -859,6 +873,7 @@ static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* us /* If the service name is taken from the host name, copy that */ avahi_free(sdref->service_name_chosen); + sdref->service_name_chosen = NULL; if (!(n = avahi_client_get_host_name(sdref->client))) { reg_report_error(sdref, map_error(avahi_client_errno(sdref->client))); @@ -888,7 +903,7 @@ static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* us break; - case AVAHI_CLIENT_S_INVALID: + case AVAHI_CLIENT_CONNECTING: case AVAHI_CLIENT_S_REGISTERING: /* Ignore */ break; @@ -938,6 +953,12 @@ static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState st case AVAHI_ENTRY_GROUP_UNCOMMITED: /* Ignore */ break; + + case AVAHI_ENTRY_GROUP_FAILURE: + /* Inform the user */ + reg_report_error(sdref, map_error(avahi_client_errno(sdref->client))); + break; + } } @@ -958,11 +979,11 @@ DNSServiceErrorType DNSSD_API DNSServiceRegister ( DNSServiceErrorType ret = kDNSServiceErr_Unknown; int error; DNSServiceRef sdref = NULL; + AvahiStringList *txt = NULL; AVAHI_WARN_LINKAGE; assert(ret_sdref); - assert(callback); assert(regtype); if (interface == kDNSServiceInterfaceIndexLocalOnly || flags) { @@ -970,8 +991,14 @@ DNSServiceErrorType DNSSD_API DNSServiceRegister ( return kDNSServiceErr_Unsupported; } - if (!(sdref = sdref_new())) + if (txtRecord && txtLen > 0) + if (avahi_string_list_parse(txtRecord, txtLen, &txt) < 0) + return kDNSServiceErr_Invalid; + + if (!(sdref = sdref_new())) { + avahi_string_list_free(txt); return kDNSServiceErr_Unknown; + } sdref->context = context; sdref->service_register_callback = callback; @@ -982,11 +1009,13 @@ DNSServiceErrorType DNSSD_API DNSServiceRegister ( sdref->service_host = host ? avahi_normalize_name_strdup(host) : NULL; sdref->service_interface = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface; sdref->service_port = ntohs(port); - sdref->service_txt = txtRecord ? avahi_string_list_parse(txtRecord, txtLen) : NULL; + sdref->service_txt = txt; + + /* Some OOM checking would be cool here */ ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); - if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), reg_client_callback, sdref, &error))) { + if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, reg_client_callback, sdref, &error))) { ret = map_error(error); goto finish; }