X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fdiscovery.c;h=95e5ed65d86da5e75297fd0fa94a9dd8eb9e762f;hb=230b5907e242aa48bb8c8d09450fbd3767e9c24a;hp=e7ac94ee64cec97eb596acb4efa0cf6fc5326579;hpb=92fcf64776730c1788717ac0d5405f0948892cb9;p=meshlink diff --git a/src/discovery.c b/src/discovery.c index e7ac94ee..95e5ed65 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -1,3 +1,5 @@ +#include "system.h" + #include #include #include @@ -11,10 +13,9 @@ #include "discovery.h" #include "sockaddr.h" #include "logger.h" - -#include - -#include +#include "node.h" +#include "connection.h" +#include "xalloc.h" #define MESHLINK_MDNS_SERVICE_TYPE "_%s._tcp" #define MESHLINK_MDNS_NAME_KEY "name" @@ -60,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)); @@ -68,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); @@ -92,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; } @@ -125,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)); } @@ -266,6 +259,23 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI if(naddress.unknown.family != AF_UNKNOWN) { meshlink_hint_address(mesh, (meshlink_node_t *)node, (struct sockaddr *)&naddress); + pthread_mutex_lock(&(mesh->mesh_mutex)); + + node_t *n = (node_t *)node; + + if(n->connection && n->connection->outgoing) { + n->connection->outgoing->timeout = 0; + + if(n->connection->outgoing->ev.cb) { + timeout_set(&mesh->loop, &n->connection->outgoing->ev, &(struct timeval) { + 0, 0 + }); + } + + n->connection->last_ping_time = 0; + } + + pthread_mutex_unlock(&(mesh->mesh_mutex)); } else { logger(mesh, MESHLINK_WARNING, "Could not resolve node %s to a known address family type.\n", node->name); } @@ -301,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; @@ -491,4 +509,6 @@ void discovery_stop(meshlink_handle_t *mesh) { free(mesh->catta_servicetype); mesh->catta_servicetype = NULL; } + + mesh->catta_interfaces = 0; }