]> git.ipfire.org Git - thirdparty/shairport-sync.git/commitdiff
Fix for name collisions in avahi
authorMike McKnight <mike@soundwall.com>
Thu, 31 Mar 2016 02:49:24 +0000 (20:49 -0600)
committerMike McKnight <mike@soundwall.com>
Thu, 31 Mar 2016 02:49:24 +0000 (20:49 -0600)
mdns_avahi.c

index 31800b8bb1034d136f8517bcf28aeb9802935f34..c7b51c55f9d7a70fad96c8cb79f77dd1183bc1e2 100644 (file)
 static AvahiClient *client = NULL;
 static AvahiEntryGroup *group = NULL;
 static AvahiThreadedPoll *tpoll = NULL;
+static pthread_t tpoll_id;
 
 static char *name = NULL;
 static int port = 0;
 
+static void register_service( AvahiClient *c );
+
 static void egroup_callback(AvahiEntryGroup *g, AvahiEntryGroupState state,
                             AVAHI_GCC_UNUSED void *userdata) {
-  if (state == AVAHI_ENTRY_GROUP_COLLISION)
-    die("service name already exists on network!");
-  if (state == AVAHI_ENTRY_GROUP_FAILURE)
-    die("avahi entry group failure!");
+   switch ( state )
+   {
+      case AVAHI_ENTRY_GROUP_ESTABLISHED:
+         /* The entry group has been established successfully */
+         inform("Service '%s' successfully established.\n", name );
+         tpoll_id = pthread_self();
+         break;
+
+      case AVAHI_ENTRY_GROUP_COLLISION:
+      {
+         char *n;
+
+         /* A service name collision with a remote service
+          * happened. Let's pick a new name */
+         n = avahi_alternative_service_name( name );
+         avahi_free( name );
+         name = n;
+
+         warn( "Service name collision, renaming service to '%s'\n", name );
+
+         /* And recreate the services */
+         register_service( avahi_entry_group_get_client( g ) );
+         break;
+      }
+
+      case AVAHI_ENTRY_GROUP_FAILURE:
+        die( "Entry group failure: %s\n", avahi_strerror( avahi_client_errno( avahi_entry_group_get_client( g ) ) ) );
+        break;
+
+      case AVAHI_ENTRY_GROUP_UNCOMMITED:
+         debug(1, "Service '%s' group is not yet commited.\n", name );
+         break;
+
+      case AVAHI_ENTRY_GROUP_REGISTERING:
+         inform( "Service '%s' group is registering.\n", name );
+         break;
+   }
 }
 
 static void register_service(AvahiClient *c) {
@@ -98,10 +134,19 @@ static void client_callback(AvahiClient *c, AvahiClientState state,
     break;
 
   case AVAHI_CLIENT_FAILURE:
-  case AVAHI_CLIENT_S_COLLISION:
     die("avahi client failure");
+    break;
+       
+  case AVAHI_CLIENT_S_COLLISION:
+    warn( "Avahi state is AVAHI_CLIENT_S_COLLISION...would have killed the service: %s\n", name );
+    break;
 
   case AVAHI_CLIENT_CONNECTING:
+    inform( "Received AVAHI_CLIENT_CONNECTING\n" );
+    break;
+
+  default:
+    warn( "Unhandled avahi state: %d\n", state );
     break;
   }
 }