X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fdiscovery.c;h=7e3fca5b49b920c8ba2da3bc709edb7ad9bc05e4;hb=2e606e5823d06aece9a0d4719e07ce3b28ebeadc;hp=f344b18d07f56960375561ffadcbb931fbc1a545;hpb=1c31a5dc4b17c274aac48444a3fe0da35a2b409c;p=meshlink diff --git a/src/discovery.c b/src/discovery.c index f344b18d..7e3fca5b 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -17,7 +17,8 @@ #include -#define MESHLINK_MDNS_SERVICE_TYPE "_meshlink._tcp" +#define MESHLINK_MDNS_SERVICE_TYPE "_%s._tcp" +#define MESHLINK_MDNS_NAME_KEY "name" #define MESHLINK_MDNS_FINGERPRINT_KEY "fingerprint" static void discovery_entry_group_callback(AvahiServer *server, AvahiSEntryGroup *group, AvahiEntryGroupState state, void *userdata) @@ -26,7 +27,6 @@ static void discovery_entry_group_callback(AvahiServer *server, AvahiSEntryGroup // asserts assert(mesh != NULL); - assert(mesh->name != NULL); assert(mesh->avahi_server != NULL); assert(mesh->avahi_poll != NULL); @@ -35,11 +35,12 @@ static void discovery_entry_group_callback(AvahiServer *server, AvahiSEntryGroup { case AVAHI_ENTRY_GROUP_ESTABLISHED: /* The entry group has been established successfully */ - fprintf(stderr, "Service '%s' successfully established.\n", mesh->name); + fprintf(stderr, "Service successfully established.\n"); break; case AVAHI_ENTRY_GROUP_COLLISION: - fprintf(stderr, "Service name collision '%s'\n", mesh->name); + fprintf(stderr, "Service collision\n"); + // @TODO can we just set a new name and retry? break; case AVAHI_ENTRY_GROUP_FAILURE : @@ -57,14 +58,18 @@ static void discovery_entry_group_callback(AvahiServer *server, AvahiSEntryGroup static void discovery_create_services(meshlink_handle_t *mesh) { + char *txt_name = NULL; + // asserts assert(mesh != NULL); assert(mesh->name != NULL); assert(mesh->myport != NULL); assert(mesh->avahi_server != NULL); assert(mesh->avahi_poll != NULL); + assert(mesh->avahi_servicetype != NULL); + assert(mesh->self != NULL); - fprintf(stderr, "Adding service '%s'\n", mesh->name); + fprintf(stderr, "Adding service\n"); /* Ifthis is the first time we're called, let's create a new entry group */ if(!mesh->avahi_group) @@ -76,13 +81,24 @@ static void discovery_create_services(meshlink_handle_t *mesh) } } - /* Create txt record */ - char txt_fingerprint[MESHLINK_FINGERPRINTLEN + sizeof(MESHLINK_MDNS_FINGERPRINT_KEY) + 2]; - snprintf(txt_fingerprint, sizeof(txt_fingerprint), "%s=%s", MESHLINK_MDNS_FINGERPRINT_KEY, meshlink_get_fingerprint(mesh, meshlink_get_node(mesh, mesh->name))); + /* 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) + { + fprintf(stderr, "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)); /* 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, mesh->name, MESHLINK_MDNS_SERVICE_TYPE, NULL, NULL, atoi(mesh->myport), 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; @@ -95,10 +111,14 @@ static void discovery_create_services(meshlink_handle_t *mesh) goto fail; } - return; + goto done; fail: avahi_simple_poll_quit(mesh->avahi_poll); + +done: + if(txt_name) + { free(txt_name); } } static void discovery_server_callback(AvahiServer *server, AvahiServerState state, void * userdata) @@ -127,14 +147,15 @@ static void discovery_server_callback(AvahiServer *server, AvahiServerState stat assert(mesh->avahi_server != NULL); assert(mesh->avahi_poll != NULL); - fprintf(stderr, "Host name collision with '%s'\n", avahi_server_get_host_name(mesh->avahi_server)); - /* A host name collision happened. Let's pick a new name for the server */ - /* - char *new_name = avahi_alternative_host_name(avahi_server_get_host_name(mesh->avahi_server)); - fprintf(stderr, "Host name collision, retrying with '%s'\n", new_name); - int result = avahi_server_set_host_name(mesh->avahi_server, new_name); - avahi_free(new_name); + uuid_t hostname; + uuid_generate(hostname); + + char hostnamestr[36+1]; + uuid_unparse_lower(hostname, hostnamestr); + + fprintf(stderr, "Host name collision, retrying with '%s'\n", hostnamestr); + int result = avahi_server_set_host_name(mesh->avahi_server, hostnamestr); if(result < 0) { @@ -142,7 +163,6 @@ static void discovery_server_callback(AvahiServer *server, AvahiServerState stat avahi_simple_poll_quit(mesh->avahi_poll); return; } - */ } break; @@ -233,53 +253,73 @@ static void discovery_resolve_callback(AvahiSServiceResolver *resolver, AvahiIfI avahi_free(strtxt); // retrieve fingerprint - AvahiStringList *fgli = avahi_string_list_find(txt, MESHLINK_MDNS_FINGERPRINT_KEY); - meshlink_node_t *node = meshlink_get_node(mesh, name); + AvahiStringList *node_name_li = avahi_string_list_find(txt, MESHLINK_MDNS_NAME_KEY); + AvahiStringList *node_fp_li = avahi_string_list_find(txt, MESHLINK_MDNS_FINGERPRINT_KEY); - fprintf(stderr, "%p, %p, %s, %s\n", fgli, node, avahi_string_list_get_text(fgli), meshlink_get_fingerprint(mesh, node)); - - if(node && fgli && strcmp(avahi_string_list_get_text(fgli) + strlen(MESHLINK_MDNS_FINGERPRINT_KEY) + 1, meshlink_get_fingerprint(mesh, node)) == 0 ) + if(node_name_li != NULL && node_fp_li != NULL) { - fprintf(stderr, "Node %s is part of the mesh network.\n", node->name); - - sockaddr_t naddress; - memset(&naddress, 0, sizeof(naddress)); + char *node_name = (char*)avahi_string_list_get_text(node_name_li) + strlen(MESHLINK_MDNS_NAME_KEY); + char *node_fp = (char*)avahi_string_list_get_text(node_fp_li) + strlen(MESHLINK_MDNS_FINGERPRINT_KEY); - switch(address->proto) + if(node_name[0] == '=' && node_fp[0] == '=') { - case AVAHI_PROTO_INET: + node_name += 1; + node_fp += 1; + + meshlink_node_t *node = meshlink_get_node(mesh, node_name); + + if(node != NULL) + { + fprintf(stderr, "Node %s is part of the mesh network.\n", node->name); + + sockaddr_t naddress; + memset(&naddress, 0, sizeof(naddress)); + + switch(address->proto) { - naddress.in.sin_family = AF_INET; - naddress.in.sin_port = port; - naddress.in.sin_addr.s_addr = address->data.ipv4.address; + case AVAHI_PROTO_INET: + { + naddress.in.sin_family = AF_INET; + naddress.in.sin_port = port; + naddress.in.sin_addr.s_addr = address->data.ipv4.address; + } + break; + + case AVAHI_PROTO_INET6: + { + naddress.in6.sin6_family = AF_INET6; + naddress.in6.sin6_port = port; + memcpy(naddress.in6.sin6_addr.s6_addr, address->data.ipv6.address, sizeof(naddress.in6.sin6_addr.s6_addr)); + } + break; + + default: + naddress.unknown.family = AF_UNKNOWN; + break; } - break; - case AVAHI_PROTO_INET6: + if(naddress.unknown.family != AF_UNKNOWN) { - naddress.in6.sin6_family = AF_INET6; - naddress.in6.sin6_port = port; - memcpy(naddress.in6.sin6_addr.s6_addr, address->data.ipv6.address, sizeof(naddress.in6.sin6_addr.s6_addr)); + meshlink_hint_address(mesh, (meshlink_node_t *)node, (struct sockaddr*)&naddress); } - break; - - default: - naddress.unknown.family = AF_UNKNOWN; - break; - } - - if(naddress.unknown.family != AF_UNKNOWN) - { - meshlink_hint_address(mesh, node, (struct sockaddr*)&naddress); + else + { + fprintf(stderr, "Could not resolve node %s to a known address family type.\n", node->name); + } + } + else + { + fprintf(stderr, "Node %s is not part of the mesh network.\n", node_name); + } } else { - fprintf(stderr, "Could not resolve node %s to a known address family type.\n", node->name); + fprintf(stderr, "TXT records invalid.\n"); } } else { - fprintf(stderr, "Node %s is not part of the mesh network.\n", node ? node->name : "n/a"); + fprintf(stderr, "TXT records missing.\n"); } } break; @@ -304,8 +344,8 @@ static void discovery_browse_callback(AvahiSServiceBrowser *browser, AvahiIfInde { fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_server_errno(mesh->avahi_server))); avahi_simple_poll_quit(mesh->avahi_poll); - return; } + return; case AVAHI_BROWSER_NEW: { @@ -361,12 +401,27 @@ static void *discovery_loop(void *userdata) bool discovery_start(meshlink_handle_t *mesh) { + fprintf(stderr, "discovery_start called\n"); + // asserts assert(mesh != NULL); assert(mesh->avahi_poll == NULL); assert(mesh->avahi_server == NULL); assert(mesh->avahi_browser == NULL); assert(mesh->discovery_threadstarted == false); + assert(mesh->avahi_servicetype == NULL); + + // create service type string + size_t servicetype_strlen = sizeof(MESHLINK_MDNS_SERVICE_TYPE) + strlen(mesh->appname) + 1; + mesh->avahi_servicetype = malloc(servicetype_strlen); + + if(mesh->avahi_servicetype == NULL) + { + fprintf(stderr, "Failed to allocate memory for service type string.\n"); + goto fail; + } + + snprintf(mesh->avahi_servicetype, servicetype_strlen, MESHLINK_MDNS_SERVICE_TYPE, mesh->appname); // Allocate discovery loop object if(!(mesh->avahi_poll = avahi_simple_poll_new())) @@ -375,7 +430,7 @@ bool discovery_start(meshlink_handle_t *mesh) goto fail; } - // generate random host name + // generate some unique host name (we actually do not care about it) uuid_t hostname; uuid_generate(hostname); @@ -389,6 +444,8 @@ bool discovery_start(meshlink_handle_t *mesh) config.publish_workstation = 0; config.disallow_other_stacks = 0; config.publish_hinfo = 0; + config.publish_addresses = 1; + config.publish_no_reverse = 1; /* Allocate a new server */ int error; @@ -405,7 +462,7 @@ bool discovery_start(meshlink_handle_t *mesh) } // Create the service browser - if(!(mesh->avahi_browser = avahi_s_service_browser_new(mesh->avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, MESHLINK_MDNS_SERVICE_TYPE, NULL, 0, discovery_browse_callback, mesh))) + if(!(mesh->avahi_browser = avahi_s_service_browser_new(mesh->avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, mesh->avahi_servicetype, NULL, 0, discovery_browse_callback, mesh))) { fprintf(stderr, "Failed to create discovery service browser: %s\n", avahi_strerror(avahi_server_errno(mesh->avahi_server))); goto fail; @@ -424,49 +481,75 @@ bool discovery_start(meshlink_handle_t *mesh) return true; fail: - if(mesh->avahi_browser) + if(mesh->avahi_browser != NULL) { avahi_s_service_browser_free(mesh->avahi_browser); mesh->avahi_browser = NULL; } - if(mesh->avahi_server) + if(mesh->avahi_server != NULL) { avahi_server_free(mesh->avahi_server); mesh->avahi_server = NULL; } - if(mesh->avahi_poll) + if(mesh->avahi_poll != NULL) { avahi_simple_poll_free(mesh->avahi_poll); mesh->avahi_poll = NULL; } + if(mesh->avahi_servicetype != NULL) + { + free(mesh->avahi_servicetype); + mesh->avahi_servicetype = NULL; + } + return false; } void discovery_stop(meshlink_handle_t *mesh) { + fprintf(stderr, "discovery_stop called\n"); + // asserts assert(mesh != NULL); - assert(mesh->avahi_poll != NULL); - assert(mesh->avahi_server != NULL); - assert(mesh->avahi_browser != NULL); - assert(mesh->discovery_threadstarted == true); - // 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; + } + + if(mesh->avahi_server != NULL) + { + avahi_server_free(mesh->avahi_server); + mesh->avahi_server = NULL; + } - avahi_server_free(mesh->avahi_server); - mesh->avahi_server = NULL; + if(mesh->avahi_poll != NULL) + { + avahi_simple_poll_free(mesh->avahi_poll); + mesh->avahi_poll = NULL; + } - avahi_simple_poll_free(mesh->avahi_poll); - mesh->avahi_poll = NULL; + if(mesh->avahi_servicetype != NULL) + { + free(mesh->avahi_servicetype); + mesh->avahi_servicetype = NULL; + } }