#include <catta/alternative.h>
#include <catta/error.h>
+#if defined(__APPLE__) || defined(__unix) && !defined(__linux)
+#include <net/route.h>
+#elif defined(__linux)
+#include <asm/types.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#endif
+
#include "meshlink_internal.h"
+#include "event.h"
#include "discovery.h"
#include "sockaddr.h"
#include "logger.h"
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(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
if(!mesh->catta_group) {
discovery_create_services(mesh);
char hostname[17];
generate_rand_string(mesh, hostname, sizeof(hostname));
- pthread_mutex_lock(&mesh->mutex);
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
assert(mesh->catta_server);
assert(mesh->catta_poll);
break;
case CATTA_SERVER_REGISTERING:
- pthread_mutex_lock(&mesh->mutex);
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
/* Let's drop our registered services. When the server is back
* in CATTA_SERVER_RUNNING state we will register them
break;
case CATTA_SERVER_FAILURE:
- pthread_mutex_lock(&mesh->mutex);
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
assert(mesh->catta_server);
assert(mesh->catta_poll);
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);
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
node_name += 1;
/* 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);
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
+
catta_simple_poll_quit(mesh->catta_poll);
pthread_mutex_unlock(&mesh->mutex);
break;
case CATTA_BROWSER_NEW:
- pthread_mutex_lock(&mesh->mutex);
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
+
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);
break;
case CATTA_BROWSER_REMOVE:
- pthread_mutex_lock(&mesh->mutex);
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
+
handle_network_change(mesh, --mesh->catta_interfaces);
pthread_mutex_unlock(&mesh->mutex);
break;
meshlink_handle_t *mesh = userdata;
assert(mesh);
- pthread_mutex_lock(&mesh->discovery_mutex);
+ if(pthread_mutex_lock(&mesh->discovery_mutex) != 0) {
+ abort();
+ }
// handle catta logs
catta_set_log_function(discovery_log_cb);
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;
- mesh->catta_server = catta_server_new(catta_simple_poll_get(mesh->catta_poll), &config, discovery_server_callback, mesh, &error);
+ const CattaPoll *poller = catta_simple_poll_get(mesh->catta_poll);
+
+ if(!poller) {
+ logger(mesh, MESHLINK_ERROR, "Failed to create discovery server: %s\n", catta_strerror(error));
+ goto fail;
+ }
+
+ mesh->catta_server = catta_server_new(poller, &config, discovery_server_callback, mesh, &error);
/* Free the configuration data */
catta_server_config_free(&config);
return NULL;
}
+#if defined(__linux)
+static void netlink_io_handler(event_loop_t *loop, void *data, int flags) {
+ (void)flags;
+ static time_t prev_update;
+ meshlink_handle_t *mesh = data;
+
+ struct {
+ struct nlmsghdr nlm;
+ char data[2048];
+ } msg;
+
+ while(true) {
+ 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 Netlink socket failed: %s\n", strerror(errno));
+ io_set(loop, &mesh->pfroute_io, 0);
+ }
+
+ if((size_t)result < sizeof(msg.nlm)) {
+ logger(mesh, MESHLINK_ERROR, "Invalid Netlink message\n");
+ break;
+ }
+
+ switch(msg.nlm.nlmsg_type) {
+ case RTM_NEWLINK:
+ case RTM_DELLINK:
+ 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;
+ }
+ }
+}
+#elif defined(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");
assert(!mesh->discovery_threadstarted);
assert(!mesh->catta_servicetype);
- pthread_mutex_lock(&mesh->discovery_mutex);
+ if(pthread_mutex_lock(&mesh->discovery_mutex) != 0) {
+ abort();
+ }
// Start the discovery thread
if(pthread_create(&mesh->discovery_thread, NULL, discovery_loop, mesh) != 0) {
mesh->discovery_threadstarted = true;
+#if defined(__linux)
+ int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
+
+ if(sock != -1) {
+ struct sockaddr_nl sa;
+ memset(&sa, 0, sizeof(sa));
+ sa.nl_family = AF_NETLINK;
+ sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
+
+ if(bind(sock, (struct sockaddr *)&sa, sizeof(sa)) != -1) {
+ io_add(&mesh->loop, &mesh->pfroute_io, netlink_io_handler, mesh, sock, IO_READ);
+ } else {
+ logger(mesh, MESHLINK_WARNING, "Could not bind AF_NETLINK socket: %s", strerror(errno));
+ }
+ } else {
+ logger(mesh, MESHLINK_WARNING, "Could not open AF_NETLINK socket: %s", strerror(errno));
+ }
+
+#elif defined(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;
}
assert(mesh);
+ if(mesh->pfroute_io.cb) {
+ close(mesh->pfroute_io.fd);
+ io_del(&mesh->loop, &mesh->pfroute_io);
+ }
+
// Shut down
if(mesh->catta_poll) {
catta_simple_poll_quit(mesh->catta_poll);
// Wait for the discovery thread to finish
if(mesh->discovery_threadstarted == true) {
- pthread_join(mesh->discovery_thread, NULL);
+ if(pthread_join(mesh->discovery_thread, NULL) != 0) {
+ abort();
+ }
+
mesh->discovery_threadstarted = false;
}