]> git.meshlink.io Git - catta/blobdiff - avahi-compat-libdns_sd/compat.c
* split client.h into client.h, lookup.h and publish.h just like we did on the server...
[catta] / avahi-compat-libdns_sd / compat.c
index fe481b0cff90f0413080654d0294ef91d6c1168e..984e288b27c2776bb922a95f9a16bdf5ee2a104f 100644 (file)
 #include <avahi-common/error.h>
 #include <avahi-common/domain.h>
 #include <avahi-common/alternative.h>
+
 #include <avahi-client/client.h>
+#include <avahi-client/publish.h>
+#include <avahi-client/lookup.h>
 
 #include "warn.h"
 #include "dns_sd.h"
@@ -45,7 +48,8 @@
 enum {
     COMMAND_POLL = 'p',
     COMMAND_QUIT = 'q',
-    COMMAND_POLL_DONE = 'P'
+    COMMAND_POLL_DONE = 'P',
+    COMMAND_POLL_FAILED = 'F'
 };
 
 struct _DNSServiceRef_t {
@@ -222,22 +226,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;
@@ -274,7 +288,7 @@ static DNSServiceRef sdref_new(void) {
 
     ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
     pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
-    ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, NULL));
+    ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, &mutex_attr));
 
     sdref->thread_running = 0;
 
@@ -540,8 +554,7 @@ finish:
         DNSServiceRefDeallocate(sdref);
 
     return ret;
-xb
-    }
+}
 
 static void service_resolver_callback(
     AvahiServiceResolver *r,
@@ -559,22 +572,22 @@ static void service_resolver_callback(
     void *userdata) {
 
     DNSServiceRef sdref = userdata;
-    char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
 
     assert(r);
     assert(sdref);
     assert(sdref->n_ref >= 1);
 
-    host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
-
     switch (event) {
         case AVAHI_RESOLVER_FOUND: {
 
+            char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
             char full_name[AVAHI_DOMAIN_NAME_MAX];
             int ret;
             char *p = NULL;
             size_t l = 0;
 
+            host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
+
             if ((p = avahi_new0(char, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
                 avahi_string_list_serialize(txt, p, l);
 
@@ -591,7 +604,7 @@ static void service_resolver_callback(
 
         case AVAHI_RESOLVER_FAILURE:
             sdref->service_resolver_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, 0, 0, NULL, sdref->context);
-            
+            break;
     }
 }
 
@@ -846,11 +859,10 @@ static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* us
         return;
     
     switch (state) {
-        case AVAHI_CLIENT_DISCONNECTED: {
+        case AVAHI_CLIENT_DISCONNECTED: 
 
             reg_report_error(sdref, kDNSServiceErr_NoError);
             break;
-        }
         
         case AVAHI_CLIENT_S_RUNNING: {
             int ret;
@@ -860,6 +872,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)));
@@ -983,7 +996,9 @@ 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 = txtRecord && txtLen > 0 ? avahi_string_list_parse(txtRecord, txtLen) : NULL;
+
+    /* Some OOM checking would be cool here */
     
     ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));