X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fdiscovery.c;h=8fa3aa3164a53b1f8f5a8807f572c3d9cffb802b;hb=refs%2Fheads%2Ffix%2Fnetwork-change-detection;hp=a64e9e2ebcf5434785c4a183c59be8c180dee6e7;hpb=a5a8005b2d89712e124ab7295165a3e229abdad5;p=meshlink diff --git a/src/discovery.c b/src/discovery.c index a64e9e2e..8fa3aa31 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -9,7 +9,12 @@ #include #include +#if defined(__APPLE__) || defined(__unix) && !defined(__linux) +#include +#endif + #include "meshlink_internal.h" +#include "event.h" #include "discovery.h" #include "sockaddr.h" #include "logger.h" @@ -126,13 +131,13 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat 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); + assert(pthread_mutex_lock(&mesh->mutex) == 0); if(!mesh->catta_group) { discovery_create_services(mesh); } - pthread_mutex_unlock(&mesh->mutex); + assert(pthread_mutex_unlock(&mesh->mutex) == 0); break; @@ -141,7 +146,7 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat char hostname[17]; generate_rand_string(mesh, hostname, sizeof(hostname)); - pthread_mutex_lock(&mesh->mutex); + assert(pthread_mutex_lock(&mesh->mutex) == 0); assert(mesh->catta_server); assert(mesh->catta_poll); @@ -152,12 +157,12 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat catta_simple_poll_quit(mesh->catta_poll); } - pthread_mutex_unlock(&mesh->mutex); + assert(pthread_mutex_unlock(&mesh->mutex) == 0); } break; case CATTA_SERVER_REGISTERING: - pthread_mutex_lock(&mesh->mutex); + assert(pthread_mutex_lock(&mesh->mutex) == 0); /* Let's drop our registered services. When the server is back * in CATTA_SERVER_RUNNING state we will register them @@ -167,12 +172,12 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat mesh->catta_group = NULL; } - pthread_mutex_unlock(&mesh->mutex); + assert(pthread_mutex_unlock(&mesh->mutex) == 0); break; case CATTA_SERVER_FAILURE: - pthread_mutex_lock(&mesh->mutex); + assert(pthread_mutex_lock(&mesh->mutex) == 0); assert(mesh->catta_server); assert(mesh->catta_poll); @@ -180,7 +185,7 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat /* Terminate on failure */ catta_simple_poll_quit(mesh->catta_poll); - pthread_mutex_unlock(&mesh->mutex); + assert(pthread_mutex_unlock(&mesh->mutex) == 0); break; case CATTA_SERVER_INVALID: @@ -215,7 +220,7 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI 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); + assert(pthread_mutex_lock(&mesh->mutex) == 0); node_name += 1; @@ -248,20 +253,22 @@ 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); - node_t *n = (node_t *)node; + connection_t *c = n->connection; - if(n->connection && n->connection->outgoing) { - n->connection->outgoing->timeout = 0; + n->catta_address = naddress; + node_add_recent_address(mesh, n, &naddress); - if(n->connection->outgoing->ev.cb) { - timeout_set(&mesh->loop, &n->connection->outgoing->ev, &(struct timeval) { + 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 }); } - n->connection->last_ping_time = 0; + c->last_ping_time = -3600; } } else { @@ -271,7 +278,7 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI logger(mesh, MESHLINK_WARNING, "Node %s is not part of the mesh network.\n", node_name); } - pthread_mutex_unlock(&mesh->mutex); + assert(pthread_mutex_unlock(&mesh->mutex) == 0); } } @@ -286,22 +293,22 @@ static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfInde /* Called whenever a new services becomes available on the LAN or is removed from the LAN */ switch(event) { case CATTA_BROWSER_FAILURE: - pthread_mutex_lock(&mesh->mutex); + assert(pthread_mutex_lock(&mesh->mutex) == 0); catta_simple_poll_quit(mesh->catta_poll); - pthread_mutex_unlock(&mesh->mutex); + assert(pthread_mutex_unlock(&mesh->mutex) == 0); break; case CATTA_BROWSER_NEW: - pthread_mutex_lock(&mesh->mutex); + assert(pthread_mutex_lock(&mesh->mutex) == 0); catta_s_service_resolver_new(mesh->catta_server, interface_, protocol, name, type, domain, CATTA_PROTO_UNSPEC, 0, discovery_resolve_callback, mesh); handle_network_change(mesh, ++mesh->catta_interfaces); - pthread_mutex_unlock(&mesh->mutex); + assert(pthread_mutex_unlock(&mesh->mutex) == 0); break; case CATTA_BROWSER_REMOVE: - pthread_mutex_lock(&mesh->mutex); + assert(pthread_mutex_lock(&mesh->mutex) == 0); handle_network_change(mesh, --mesh->catta_interfaces); - pthread_mutex_unlock(&mesh->mutex); + assert(pthread_mutex_unlock(&mesh->mutex) == 0); break; case CATTA_BROWSER_ALL_FOR_NOW: @@ -341,6 +348,8 @@ static void *discovery_loop(void *userdata) { meshlink_handle_t *mesh = userdata; assert(mesh); + assert(pthread_mutex_lock(&mesh->discovery_mutex) == 0); + // handle catta logs catta_set_log_function(discovery_log_cb); @@ -388,6 +397,7 @@ static void *discovery_loop(void *userdata) { config.publish_hinfo = 0; config.publish_addresses = 1; config.publish_no_reverse = 1; + config.allow_point_to_point = 1; /* Allocate a new server */ int error; @@ -396,7 +406,7 @@ static void *discovery_loop(void *userdata) { /* Free the configuration data */ catta_server_config_free(&config); - /* Check wether creating the server object succeeded */ + /* Check whether creating the server object succeeded */ if(!mesh->catta_server) { logger(mesh, MESHLINK_ERROR, "Failed to create discovery server: %s\n", catta_strerror(error)); goto fail; @@ -412,9 +422,8 @@ static void *discovery_loop(void *userdata) { fail: - pthread_mutex_lock(&mesh->discovery_mutex); - pthread_cond_broadcast(&mesh->discovery_cond); - pthread_mutex_unlock(&mesh->discovery_mutex); + assert(pthread_cond_broadcast(&mesh->discovery_cond) == 0); + assert(pthread_mutex_unlock(&mesh->discovery_mutex) == 0); if(status) { catta_simple_poll_loop(mesh->catta_poll); @@ -449,6 +458,53 @@ fail: return NULL; } +#ifdef RTM_NEWADDR +static void pfroute_io_handler(event_loop_t *loop, void *data, int flags) { + (void)flags; + static time_t prev_update; + meshlink_handle_t *mesh = data; + + struct { + struct rt_msghdr rtm; + char data[2048]; + } msg; + + while(true) { + msg.rtm.rtm_version = 0; + ssize_t result = recv(mesh->pfroute_io.fd, &msg, sizeof(msg), MSG_DONTWAIT); + + if(result <= 0) { + if(result == 0 || errno == EAGAIN || errno == EINTR) { + break; + } + + logger(mesh, MESHLINK_ERROR, "Reading from PFROUTE socket failed: %s\n", strerror(errno)); + io_set(loop, &mesh->pfroute_io, 0); + } + + if(msg.rtm.rtm_version != RTM_VERSION) { + logger(mesh, MESHLINK_ERROR, "Invalid PFROUTE message version\n"); + break; + } + + switch(msg.rtm.rtm_type) { + case RTM_IFINFO: + case RTM_NEWADDR: + case RTM_DELADDR: + if(loop->now.tv_sec > prev_update + 5) { + prev_update = loop->now.tv_sec; + handle_network_change(mesh, 1); + } + + break; + + default: + break; + } + } +} +#endif + bool discovery_start(meshlink_handle_t *mesh) { logger(mesh, MESHLINK_DEBUG, "discovery_start called\n"); @@ -459,19 +515,32 @@ bool discovery_start(meshlink_handle_t *mesh) { assert(!mesh->discovery_threadstarted); assert(!mesh->catta_servicetype); + assert(pthread_mutex_lock(&mesh->discovery_mutex) == 0); + // Start the discovery thread if(pthread_create(&mesh->discovery_thread, NULL, discovery_loop, mesh) != 0) { + assert(pthread_mutex_unlock(&mesh->discovery_mutex) == 0); logger(mesh, MESHLINK_ERROR, "Could not start discovery thread: %s\n", strerror(errno)); memset(&mesh->discovery_thread, 0, sizeof(mesh)->discovery_thread); return false; } - pthread_mutex_lock(&mesh->discovery_mutex); - pthread_cond_wait(&mesh->discovery_cond, &mesh->discovery_mutex); - pthread_mutex_unlock(&mesh->discovery_mutex); + assert(pthread_cond_wait(&mesh->discovery_cond, &mesh->discovery_mutex) == 0); + assert(pthread_mutex_unlock(&mesh->discovery_mutex) == 0); mesh->discovery_threadstarted = true; +#ifdef RTM_NEWADDR + int sock = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC); + + if(sock != -1) { + io_add(&mesh->loop, &mesh->pfroute_io, pfroute_io_handler, mesh, sock, IO_READ); + } else { + logger(mesh, MESHLINK_WARNING, "Could not open PF_ROUTE socket: %s", strerror(errno)); + } + +#endif + return true; } @@ -480,6 +549,15 @@ void discovery_stop(meshlink_handle_t *mesh) { assert(mesh); +#ifdef RTM_NEWADDR + + if(mesh->pfroute_io.cb) { + close(mesh->pfroute_io.fd); + io_del(&mesh->loop, &mesh->pfroute_io); + } + +#endif + // Shut down if(mesh->catta_poll) { catta_simple_poll_quit(mesh->catta_poll); @@ -487,7 +565,7 @@ void discovery_stop(meshlink_handle_t *mesh) { // Wait for the discovery thread to finish if(mesh->discovery_threadstarted == true) { - pthread_join(mesh->discovery_thread, NULL); + assert(pthread_join(mesh->discovery_thread, NULL) == 0); mesh->discovery_threadstarted = false; }