]> git.meshlink.io Git - meshlink/blobdiff - src/discovery.c
Add a function to destroy a MeshLink instance.
[meshlink] / src / discovery.c
index 046bcc3e9a0dec431280d692a62667b823e81ccf..b6ed37c5ab8a1040fc616b72501181455661289b 100644 (file)
@@ -1,11 +1,3 @@
-
-#include "meshlink_internal.h"
-#include "discovery.h"
-#include "sockaddr.h"
-#include "logger.h"
-
-#include <pthread.h>
-
 #include <catta/core.h>
 #include <catta/lookup.h>
 #include <catta/publish.h>
 #include <catta/alternative.h>
 #include <catta/error.h>
 
-#include <netinet/in.h>
+#include "meshlink_internal.h"
+#include "discovery.h"
+#include "sockaddr.h"
+#include "logger.h"
 
-#include <uuid/uuid.h>
+#include <pthread.h>
+
+#include <netinet/in.h>
 
 #define MESHLINK_MDNS_SERVICE_TYPE "_%s._tcp"
 #define MESHLINK_MDNS_NAME_KEY "name"
 #define MESHLINK_MDNS_FINGERPRINT_KEY "fingerprint"
 
+static void generate_rand_string(char* buffer, size_t size)
+{
+    for(size_t i = 0; i < (size - 1); ++i)
+    {
+        buffer[i] = 'a' + (rand() % ('z' - 'a' + 1));
+    }
+
+    buffer[size-1] = '\0';
+}
+
 static void discovery_entry_group_callback(CattaServer *server, CattaSEntryGroup *group, CattaEntryGroupState state, void *userdata)
 {
     meshlink_handle_t *mesh = userdata;
@@ -160,14 +167,11 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat
                 assert(mesh->catta_poll != NULL);
 
                 /* A host name collision happened. Let's pick a new name for the server */
-                uuid_t hostname;
-                uuid_generate(hostname);
-
-                char hostnamestr[36+1];
-                uuid_unparse_lower(hostname, hostnamestr);
+                char hostname[17];
+                generate_rand_string(hostname, sizeof(hostname));
 
-                logger(mesh, MESHLINK_WARNING, "Catta host name collision, retrying with '%s'\n", hostnamestr);
-                int result = catta_server_set_host_name(mesh->catta_server, hostnamestr);
+                logger(mesh, MESHLINK_WARNING, "Catta host name collision, retrying with '%s'\n", hostname);
+                int result = catta_server_set_host_name(mesh->catta_server, hostname);
 
                 if(result < 0)
                 {
@@ -209,7 +213,7 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat
     pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
-static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfIndex interface, CattaProtocol protocol, CattaResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const CattaAddress *address, uint16_t port, CattaStringList *txt, CattaLookupResultFlags flags, void* userdata)
+static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfIndex interface_, CattaProtocol protocol, CattaResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const CattaAddress *address, uint16_t port, CattaStringList *txt, CattaLookupResultFlags flags, void *userdata)
 {
     meshlink_handle_t *mesh = userdata;
 
@@ -345,7 +349,7 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI
     pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
-static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfIndex interface, CattaProtocol protocol, CattaBrowserEvent event, const char *name, const char *type, const char *domain, CattaLookupResultFlags flags, void* userdata)
+static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfIndex interface_, CattaProtocol protocol, CattaBrowserEvent event, const char *name, const char *type, const char *domain, CattaLookupResultFlags flags, void* userdata)
 {
        meshlink_handle_t *mesh = userdata;
 
@@ -378,7 +382,7 @@ static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfInde
                    function we free it. Ifthe server is terminated before
                    the callback function is called the server will free
                    the resolver for us. */
-                if(!(catta_s_service_resolver_new(mesh->catta_server, interface, protocol, name, type, domain, CATTA_PROTO_UNSPEC, 0, discovery_resolve_callback, mesh)))
+                if(!(catta_s_service_resolver_new(mesh->catta_server, interface_, protocol, name, type, domain, CATTA_PROTO_UNSPEC, 0, discovery_resolve_callback, mesh)))
                 {
                     logger(mesh, MESHLINK_DEBUG, "Failed to resolve service '%s': %s\n", name, catta_strerror(catta_server_errno(mesh->catta_server)));
                 }
@@ -461,7 +465,7 @@ bool discovery_start(meshlink_handle_t *mesh)
 
     // handle catta logs
     catta_set_log_function(discovery_log_cb);
-    
+
     // create service type string
     size_t servicetype_strlen = sizeof(MESHLINK_MDNS_SERVICE_TYPE) + strlen(mesh->appname) + 1;
     mesh->catta_servicetype = malloc(servicetype_strlen);
@@ -482,16 +486,13 @@ bool discovery_start(meshlink_handle_t *mesh)
     }
 
     // generate some unique host name (we actually do not care about it)
-    uuid_t hostname;
-    uuid_generate(hostname);
-
-    char hostnamestr[36+1];
-    uuid_unparse_lower(hostname, hostnamestr);
+    char hostname[17];
+    generate_rand_string(hostname, sizeof(hostname));
 
     // Let's set the host name for this server.
     CattaServerConfig config;
     catta_server_config_init(&config);
-    config.host_name = catta_strdup(hostnamestr);
+    config.host_name = catta_strdup(hostname);
     config.publish_workstation = 0;
     config.disallow_other_stacks = 0;
     config.publish_hinfo = 0;