]> git.meshlink.io Git - meshlink/blobdiff - src/discovery.c
Merge branch 'discovery' into everbase
[meshlink] / src / discovery.c
index c004ce5849f839eb0bcde9d45e0d9cb26af792e8..4d1209b856ab9cef92d4bacab5af9fbc2a361d61 100644 (file)
@@ -67,6 +67,7 @@ static void discovery_create_services(meshlink_handle_t *mesh)
     assert(mesh->avahi_server != NULL);
     assert(mesh->avahi_poll != NULL);
     assert(mesh->avahi_servicetype != NULL);
+    assert(mesh->self != NULL);
 
     fprintf(stderr, "Adding service\n");
 
@@ -95,16 +96,9 @@ static void discovery_create_services(meshlink_handle_t *mesh)
     char txt_fingerprint[sizeof(MESHLINK_MDNS_FINGERPRINT_KEY) + 1 + MESHLINK_FINGERPRINTLEN + 1];
     snprintf(txt_fingerprint, sizeof(txt_fingerprint), "%s=%s", MESHLINK_MDNS_FINGERPRINT_KEY, meshlink_get_fingerprint(mesh, (meshlink_node_t *)mesh->self));
 
-    // Generate a name for the service (actually we do not care)
-    uuid_t srvname;
-    uuid_generate(srvname);
-
-    char srvnamestr[36+1];
-    uuid_unparse_lower(srvname, srvnamestr);
-
     /* Add the service */
     int ret = 0;
-    if((ret = avahi_server_add_service(mesh->avahi_server, mesh->avahi_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, srvnamestr, mesh->avahi_servicetype, NULL, NULL, atoi(mesh->myport), txt_name, txt_fingerprint, NULL)) < 0)
+    if((ret = avahi_server_add_service(mesh->avahi_server, mesh->avahi_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, meshlink_get_fingerprint(mesh, (meshlink_node_t *)mesh->self), mesh->avahi_servicetype, NULL, NULL, atoi(mesh->myport), txt_name, txt_fingerprint, NULL)) < 0)
     {
         fprintf(stderr, "Failed to add service: %s\n", avahi_strerror(ret));
         goto fail;
@@ -506,28 +500,42 @@ void discovery_stop(meshlink_handle_t *mesh)
 {
     // asserts
     assert(mesh != NULL);
-    assert(mesh->avahi_poll != NULL);
-    assert(mesh->avahi_server != NULL);
-    assert(mesh->avahi_browser != NULL);
-    assert(mesh->discovery_threadstarted == true);
-    assert(mesh->avahi_servicetype != NULL);
 
-       // Shut down 
-       avahi_simple_poll_quit(mesh->avahi_poll);
+       // Shut down
+    if(mesh->avahi_poll)
+    {
+        avahi_simple_poll_quit(mesh->avahi_poll);
+    }
 
        // Wait for the discovery thread to finish
-       pthread_join(mesh->discovery_thread, NULL);
+    if(mesh->discovery_threadstarted == true)
+    {
+        pthread_join(mesh->discovery_thread, NULL);
+        mesh->discovery_threadstarted = false;
+    }
 
        // Clean up resources
-    avahi_s_service_browser_free(mesh->avahi_browser);
-    mesh->avahi_browser = NULL;
+    if(mesh->avahi_browser != NULL)
+    {
+        avahi_s_service_browser_free(mesh->avahi_browser);
+        mesh->avahi_browser = NULL;
+    }
 
-    avahi_server_free(mesh->avahi_server);
-    mesh->avahi_server = NULL;
+    if(mesh->avahi_server != NULL)
+    {
+        avahi_server_free(mesh->avahi_server);
+        mesh->avahi_server = NULL;
+    }
 
-    avahi_simple_poll_free(mesh->avahi_poll);
-    mesh->avahi_poll = NULL;
+    if(mesh->avahi_poll != NULL)
+    {
+        avahi_simple_poll_free(mesh->avahi_poll);
+        mesh->avahi_poll = NULL;
+    }
 
-    free(mesh->avahi_servicetype);
-    mesh->avahi_servicetype = NULL;
+    if(mesh->avahi_servicetype != NULL)
+    {
+        free(mesh->avahi_servicetype);
+        mesh->avahi_servicetype = NULL;
+    }
 }