- switch (state)
- {
- case AVAHI_SERVER_RUNNING:
- /* The serve has startup successfully and registered its host
- * name on the network, so it's time to create our services */
- if (!mesh->avahi_group)
- discovery_create_services(mesh);
- break;
-
- case AVAHI_SERVER_COLLISION:
- /* A host name collision happened. Let's do nothing */
- break;
-
- case AVAHI_SERVER_REGISTERING:
- /* Let's drop our registered services. When the server is back
- * in AVAHI_SERVER_RUNNING state we will register them
- * again with the new host name. */
- //if (mesh->avahi_group)
- // avahi_s_entry_group_reset(mesh->avahi_group);
- break;
-
- case AVAHI_SERVER_FAILURE:
- /* Terminate on failure */
- fprintf(stderr, "Server failure: %s\n", avahi_strerror(avahi_server_errno(mesh->avahi_server)));
- avahi_simple_poll_quit(mesh->avahi_poll);
- break;
-
- case AVAHI_SERVER_INVALID:
- break;
- }
+ assert(mesh);
+
+ switch(state) {
+ case CATTA_SERVER_RUNNING:
+ /* The serve has startup successfully and registered its host
+ * name on the network, so it's time to create our services */
+ pthread_mutex_lock(&mesh->mutex);
+
+ if(!mesh->catta_group) {
+ discovery_create_services(mesh);
+ }
+
+ pthread_mutex_unlock(&mesh->mutex);
+
+ break;
+
+ case CATTA_SERVER_COLLISION: {
+ /* A host name collision happened. Let's pick a new name for the server */
+ char hostname[17];
+ generate_rand_string(mesh, hostname, sizeof(hostname));
+
+ pthread_mutex_lock(&mesh->mutex);
+
+ assert(mesh->catta_server);
+ assert(mesh->catta_poll);
+
+ int result = catta_server_set_host_name(mesh->catta_server, hostname);
+
+ if(result < 0) {
+ catta_simple_poll_quit(mesh->catta_poll);
+ }
+
+ pthread_mutex_unlock(&mesh->mutex);
+ }
+ break;
+
+ case CATTA_SERVER_REGISTERING:
+ pthread_mutex_lock(&mesh->mutex);
+
+ /* Let's drop our registered services. When the server is back
+ * in CATTA_SERVER_RUNNING state we will register them
+ * again with the new host name. */
+ if(mesh->catta_group) {
+ catta_s_entry_group_reset(mesh->catta_group);
+ mesh->catta_group = NULL;
+ }
+
+ pthread_mutex_unlock(&mesh->mutex);
+
+ break;
+
+ case CATTA_SERVER_FAILURE:
+ pthread_mutex_lock(&mesh->mutex);
+
+ assert(mesh->catta_server);
+ assert(mesh->catta_poll);
+
+ /* Terminate on failure */
+ catta_simple_poll_quit(mesh->catta_poll);
+
+ pthread_mutex_unlock(&mesh->mutex);
+ break;
+
+ case CATTA_SERVER_INVALID:
+ break;
+ }
+}
+
+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) {
+ (void)interface_;
+ (void)protocol;
+ (void)flags;
+ (void)name;
+ (void)type;
+ (void)domain;
+ (void)host_name;
+
+ meshlink_handle_t *mesh = userdata;
+
+ assert(mesh);
+
+ if(event != CATTA_RESOLVER_FOUND) {
+ catta_s_service_resolver_free(resolver);
+ return;
+ }
+
+ // retrieve fingerprint
+ CattaStringList *node_name_li = catta_string_list_find(txt, MESHLINK_MDNS_NAME_KEY);
+ CattaStringList *node_fp_li = catta_string_list_find(txt, MESHLINK_MDNS_FINGERPRINT_KEY);
+
+ if(node_name_li && node_fp_li) {
+ char *node_name = (char *)catta_string_list_get_text(node_name_li) + strlen(MESHLINK_MDNS_NAME_KEY);
+ char *node_fp = (char *)catta_string_list_get_text(node_fp_li) + strlen(MESHLINK_MDNS_FINGERPRINT_KEY);
+
+ if(node_name[0] == '=' && node_fp[0] == '=') {
+ pthread_mutex_lock(&mesh->mutex);
+
+ node_name += 1;
+
+ meshlink_node_t *node = meshlink_get_node(mesh, node_name);
+
+ if(node) {
+ 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 CATTA_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 CATTA_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) {
+ node_t *n = (node_t *)node;
+ connection_t *c = n->connection;
+
+ n->catta_address = naddress;
+ node_add_recent_address(mesh, n, &naddress);
+
+ if(c && c->outgoing && !c->status.active) {
+ c->outgoing->timeout = 0;
+
+ if(c->outgoing->ev.cb) {
+ timeout_set(&mesh->loop, &c->outgoing->ev, &(struct timespec) {
+ 0, 0
+ });
+ }
+
+ c->last_ping_time = 0;
+ }
+
+ } 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);
+ }
+
+ pthread_mutex_unlock(&mesh->mutex);
+ }
+ }
+
+ catta_s_service_resolver_free(resolver);