X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fdiscovery.c;h=95e5ed65d86da5e75297fd0fa94a9dd8eb9e762f;hb=230b5907e242aa48bb8c8d09450fbd3767e9c24a;hp=4aa34d434ef0d5a687857a17786c9c27f438ba89;hpb=48053fbb098f832784d2e775822e712fac84fe3c;p=meshlink diff --git a/src/discovery.c b/src/discovery.c index 4aa34d43..95e5ed65 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -1,3 +1,5 @@ +#include "system.h" + #include #include #include @@ -13,10 +15,7 @@ #include "logger.h" #include "node.h" #include "connection.h" - -#include - -#include +#include "xalloc.h" #define MESHLINK_MDNS_SERVICE_TYPE "_%s._tcp" #define MESHLINK_MDNS_NAME_KEY "name" @@ -62,7 +61,7 @@ static void discovery_entry_group_callback(CattaServer *server, CattaSEntryGroup case CATTA_ENTRY_GROUP_UNCOMMITED: case CATTA_ENTRY_GROUP_REGISTERING: - ; + break; } pthread_mutex_unlock(&(mesh->mesh_mutex)); @@ -70,7 +69,9 @@ static void discovery_entry_group_callback(CattaServer *server, CattaSEntryGroup static void discovery_create_services(meshlink_handle_t *mesh) { + char *fingerprint = NULL; char *txt_name = NULL; + char *txt_fingerprint = NULL; // asserts assert(mesh != NULL); @@ -94,23 +95,14 @@ static void discovery_create_services(meshlink_handle_t *mesh) { } /* Create txt records */ - size_t txt_name_len = sizeof(MESHLINK_MDNS_NAME_KEY) + 1 + strlen(mesh->name) + 1; - txt_name = malloc(txt_name_len); - - if(txt_name == NULL) { - logger(mesh, MESHLINK_ERROR, "Could not allocate memory for TXT record\n"); - goto fail; - } - - snprintf(txt_name, txt_name_len, "%s=%s", MESHLINK_MDNS_NAME_KEY, mesh->name); - - 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)); + fingerprint = meshlink_get_fingerprint(mesh, (meshlink_node_t *)mesh->self); + xasprintf(&txt_name, "%s=%s", MESHLINK_MDNS_NAME_KEY, mesh->name); + xasprintf(&txt_fingerprint, "%s=%s", MESHLINK_MDNS_FINGERPRINT_KEY, fingerprint); /* Add the service */ int ret = 0; - if((ret = catta_server_add_service(mesh->catta_server, mesh->catta_group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, meshlink_get_fingerprint(mesh, (meshlink_node_t *)mesh->self), mesh->catta_servicetype, NULL, NULL, atoi(mesh->myport), txt_name, txt_fingerprint, NULL)) < 0) { + if((ret = catta_server_add_service(mesh->catta_server, mesh->catta_group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, fingerprint, mesh->catta_servicetype, NULL, NULL, atoi(mesh->myport), txt_name, txt_fingerprint, NULL)) < 0) { logger(mesh, MESHLINK_ERROR, "Failed to add service: %s\n", catta_strerror(ret)); goto fail; } @@ -127,10 +119,9 @@ fail: catta_simple_poll_quit(mesh->catta_poll); done: - - if(txt_name) { - free(txt_name); - } + free(fingerprint); + free(txt_name); + free(txt_fingerprint); pthread_mutex_unlock(&(mesh->mesh_mutex)); } @@ -320,9 +311,17 @@ static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfInde case CATTA_BROWSER_NEW: catta_s_service_resolver_new(mesh->catta_server, interface_, protocol, name, type, domain, CATTA_PROTO_UNSPEC, 0, discovery_resolve_callback, mesh); + pthread_mutex_lock(&mesh->mesh_mutex); + handle_network_change(mesh, ++mesh->catta_interfaces); + pthread_mutex_unlock(&mesh->mesh_mutex); break; case CATTA_BROWSER_REMOVE: + pthread_mutex_lock(&mesh->mesh_mutex); + handle_network_change(mesh, --mesh->catta_interfaces); + pthread_mutex_unlock(&mesh->mesh_mutex); + break; + case CATTA_BROWSER_ALL_FOR_NOW: case CATTA_BROWSER_CACHE_EXHAUSTED: break; @@ -510,4 +509,6 @@ void discovery_stop(meshlink_handle_t *mesh) { free(mesh->catta_servicetype); mesh->catta_servicetype = NULL; } + + mesh->catta_interfaces = 0; }