+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+}
+
+static void discovery_resolve_callback(AvahiSServiceResolver *resolver, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void* userdata)
+{
+ meshlink_handle_t *mesh = userdata;
+
+ // asserts
+ assert(resolver != NULL);
+ assert(mesh != NULL);
+ assert(mesh->avahi_server != NULL);
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ /* Called whenever a service has been resolved successfully or timed out */
+ switch(event)
+ {
+ case AVAHI_RESOLVER_FAILURE:
+ {
+ // asserts
+ assert(name != NULL);
+ assert(type != NULL);
+ assert(domain != NULL);
+
+ logger(mesh, MESHLINK_WARNING, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_server_errno(mesh->avahi_server)));
+ }
+ break;
+
+ case AVAHI_RESOLVER_FOUND:
+ {
+ // asserts
+ assert(name != NULL);
+ assert(type != NULL);
+ assert(domain != NULL);
+ assert(host_name != NULL);
+ assert(address != NULL);
+ assert(txt != NULL);
+
+ char straddr[AVAHI_ADDRESS_STR_MAX], *strtxt;
+
+ logger(mesh, MESHLINK_DEBUG, "(Resolver) Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
+
+ avahi_address_snprint(straddr, sizeof(straddr), address);
+ strtxt = avahi_string_list_to_string(txt);
+ logger(mesh, MESHLINK_DEBUG,
+ "\t%s:%u (%s)\n"
+ "\tTXT=%s\n"
+ "\tcookie is %u\n"
+ "\tis_local: %i\n"
+ "\twide_area: %i\n"
+ "\tmulticast: %i\n"
+ "\tcached: %i\n",
+ host_name, port, straddr,
+ strtxt,
+ avahi_string_list_get_service_cookie(txt),
+ !!(flags & AVAHI_LOOKUP_RESULT_LOCAL),
+ !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
+ !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
+ !!(flags & AVAHI_LOOKUP_RESULT_CACHED));
+ avahi_free(strtxt);
+
+ // retrieve fingerprint
+ 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);
+
+ if(node_name_li != NULL && node_fp_li != NULL)
+ {
+ 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);
+
+ if(node_name[0] == '=' && node_fp[0] == '=')
+ {
+ node_name += 1;
+ node_fp += 1;
+
+ meshlink_node_t *node = meshlink_get_node(mesh, node_name);
+
+ if(node != NULL)
+ {
+ logger(mesh, MESHLINK_INFO, "Node %s is part of the mesh network.\n", node->name);
+
+ sockaddr_t naddress;
+ memset(&naddress, 0, sizeof(naddress));
+
+ switch(address->proto)
+ {
+ case AVAHI_PROTO_INET:
+ {
+ naddress.in.sin_family = AF_INET;
+ naddress.in.sin_port = htons(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 = htons(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;
+ }
+
+ if(naddress.unknown.family != AF_UNKNOWN)
+ {
+ meshlink_hint_address(mesh, (meshlink_node_t *)node, (struct sockaddr*)&naddress);
+ }
+ else
+ {
+ logger(mesh, MESHLINK_WARNING, "Could not resolve node %s to a known address family type.\n", node->name);
+ }
+ }
+ else
+ {
+ logger(mesh, MESHLINK_WARNING, "Node %s is not part of the mesh network.\n", node_name);
+ }
+ }
+ else
+ {
+ logger(mesh, MESHLINK_WARNING, "TXT records invalid.\n");
+ }
+ }
+ else
+ {
+ logger(mesh, MESHLINK_WARNING, "TXT records missing.\n");
+ }
+ }
+ break;
+ }
+
+ avahi_s_service_resolver_free(resolver);
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));