]> git.meshlink.io Git - catta/blobdiff - avahi-compat-libdns_sd/compat.c
* add new entry group state AVAHI_ENTRY_GROUP_FAILURE
[catta] / avahi-compat-libdns_sd / compat.c
index 984e288b27c2776bb922a95f9a16bdf5ee2a104f..a06ca6826c627da13d9fdba991366e97076aee7d 100644 (file)
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <signal.h>
 #include <netinet/in.h>
+#include <fcntl.h>
 
 #include <avahi-common/simple-watch.h>
 #include <avahi-common/malloc.h>
@@ -63,7 +64,7 @@ struct _DNSServiceRef_t {
     int thread_running;
 
     pthread_mutex_t mutex;
-
+    
     void *context;
     DNSServiceBrowseReply service_browser_callback;
     DNSServiceResolveReply service_resolver_callback;
@@ -107,7 +108,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:
@@ -335,7 +336,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);
 
@@ -387,9 +387,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)
@@ -414,20 +414,18 @@ 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(
@@ -471,23 +469,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_S_FAILURE:
+
+            error = map_error(avahi_client_errno(s));
+
+            /* Fall through */
+            
+        case AVAHI_CLIENT_DISCONNECTED:
 
             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:
@@ -859,9 +863,12 @@ 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_Unknown);
+            break;
 
-            reg_report_error(sdref, kDNSServiceErr_NoError);
+        case AVAHI_CLIENT_S_FAILURE:
+            reg_report_error(sdref, map_error(avahi_client_errno(s)));
             break;
         
         case AVAHI_CLIENT_S_RUNNING: {
@@ -952,6 +959,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;
+            
     }
 }