if(!n->status.reachable) {
update_node_udp(mesh, n, NULL);
- memset(&n->status, 0, sizeof(n)->status);
+ n->status.broadcast = false;
n->options = 0;
} else if(n->connection) {
if(n->connection->outgoing) {
*/
typedef void (*node_status_cb_t)(mesh *mesh, node *node, bool reachable);
+/// A callback reporting duplicate node detection.
+/** @param mesh A handle which represents an instance of MeshLink.
+ * @param node A pointer to a meshlink_node_t describing the node which is duplicate.
+ * This pointer is valid until meshlink_close() is called.
+ */
+typedef void (*duplicate_cb_t)(mesh *mesh, node *node);
+
/// A callback for receiving log messages generated by MeshLink.
/** @param mesh A handle which represents an instance of MeshLink.
* @param level An enum describing the severity level of the message.
(void) length;
}
- /// This functions is called whenever another node's status changed.
- virtual void node_status(node *peer, bool reachable) {
+ /// This functions is called whenever another node's status changed.
+ virtual void node_status(node *peer, bool reachable) {
/* do nothing */
(void)peer;
(void)reachable;
}
+ /// This functions is called whenever a duplicate node is detected.
+ virtual void node_duplicate(node *peer) {
+ /* do nothing */
+ (void)peer;
+ }
+
/// This functions is called whenever MeshLink has some information to log.
- virtual void log(log_level_t level, const char *message) {
+ virtual void log(log_level_t level, const char *message) {
/* do nothing */
(void)level;
(void)message;
bool start() {
meshlink_set_receive_cb(handle, &receive_trampoline);
meshlink_set_node_status_cb(handle, &node_status_trampoline);
+ meshlink_set_node_duplicate_cb(handle, &node_duplicate_trampoline);
meshlink_set_log_cb(handle, MESHLINK_DEBUG, &log_trampoline);
meshlink_set_channel_accept_cb(handle, &channel_accept_trampoline);
return meshlink_start(handle);
that->node_status(static_cast<node *>(peer), reachable);
}
+ static void node_duplicate_trampoline(meshlink_handle_t *handle, meshlink_node_t *peer) {
+ if(!(handle->priv)) {
+ return;
+ }
+
+ meshlink::mesh *that = static_cast<mesh *>(handle->priv);
+ that->node_duplicate(static_cast<node *>(peer));
+ }
+
static void log_trampoline(meshlink_handle_t *handle, log_level_t level, const char *message) {
if(!(handle->priv)) {
return;
mesh->threadstarted = true;
#if HAVE_CATTA
+
if(mesh->discovery) {
discovery_start(mesh);
}
+
#endif
pthread_mutex_unlock(&(mesh->mesh_mutex));
logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n");
#if HAVE_CATTA
+
// Stop discovery
if(mesh->discovery) {
discovery_stop(mesh);
}
+
#endif
// Shut down the main thread
pthread_mutex_unlock(&(mesh->mesh_mutex));
}
+void meshlink_set_node_duplicate_cb(meshlink_handle_t *mesh, meshlink_node_duplicate_cb_t cb) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ mesh->node_duplicate_cb = cb;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+}
+
void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb) {
if(mesh) {
pthread_mutex_lock(&(mesh->mesh_mutex));
//Make blacklisting persistent in the config file
append_config_file(mesh, n->name, "blacklisted", "yes");
+ //Immediately terminate any connections we have with the blacklisted node
+ for list_each(connection_t, c, mesh->connections) {
+ if(c->node == n) {
+ terminate_connection(mesh, c, c->status.active);
+ }
+ }
+
pthread_mutex_unlock(&(mesh->mesh_mutex));
- return;
}
void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) {
}
}
+void handle_duplicate_node(meshlink_handle_t *mesh, node_t *n) {
+ if(!mesh->node_duplicate_cb || n->status.duplicate) {
+ return;
+ }
+
+ n->status.duplicate = true;
+ mesh->node_duplicate_cb(mesh, (meshlink_node_t *)n);
+}
+
void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable) {
#if HAVE_CATTA
+
if(!mesh) {
meshlink_errno = MESHLINK_EINVAL;
return;
*/
extern void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb);
+/// A callback reporting duplicate node detection.
+/** @param mesh A handle which represents an instance of MeshLink.
+ * @param node A pointer to a meshlink_node_t describing the node which is duplicate.
+ * This pointer is valid until meshlink_close() is called.
+ */
+typedef void (*meshlink_node_duplicate_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *node);
+
+/// Set the node duplicate callback.
+/** This functions sets the callback that is called whenever a duplicate node is detected.
+ * The callback is run in MeshLink's own thread.
+ * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
+ * to hand the data over to the application's thread.
+ * The callback should also not block itself and return as quickly as possible.
+ *
+ * @param mesh A handle which represents an instance of MeshLink.
+ * @param cb A pointer to the function which will be called when a duplicate node is detected.
+ * If a NULL pointer is given, the callback will be disabled.
+ */
+extern void meshlink_set_node_duplicate_cb(meshlink_handle_t *mesh, meshlink_node_duplicate_cb_t cb);
+
/// Severity of log messages generated by MeshLink.
typedef enum {
MESHLINK_DEBUG, ///< Internal debugging messages. Only useful during application development.
meshlink_log_level_t log_level;
meshlink_channel_accept_cb_t channel_accept_cb;
+ meshlink_node_duplicate_cb_t node_duplicate_cb;
pthread_t thread;
bool threadstarted;
extern meshlink_log_level_t global_log_level;
extern meshlink_log_cb_t global_log_cb;
extern int check_port(meshlink_handle_t *mesh);
+extern void handle_duplicate_node(meshlink_handle_t *mesh, struct node_t *n);
/// Device class traits
typedef struct {
if(c->edge) {
if(report) {
- send_del_edge(mesh, mesh->everyone, c->edge);
+ send_del_edge(mesh, mesh->everyone, c->edge, 0);
}
edge_del(mesh, c->edge);
e = lookup_edge(c->node, mesh->self);
if(e) {
- send_del_edge(mesh, mesh->everyone, e);
+ send_del_edge(mesh, mesh->everyone, e, 0);
edge_del(mesh, e);
}
}
unsigned int broadcast: 1; /* 1 if the next UDP packet should be broadcast to the local network */
unsigned int blacklisted: 1; /* 1 if the node is blacklist so we never want to speak with him anymore */
unsigned int destroyed: 1; /* 1 if the node is being destroyed, deallocate channels when any callback is triggered */
- unsigned int unused: 22;
+ unsigned int duplicate: 1; /* 1 if the node is duplicate, ie. multiple nodes using the same Name are online */
+ unsigned int unused: 20;
} node_status_t;
typedef struct node_t {
extern bool send_ack(struct meshlink_handle *mesh, struct connection_t *);
extern bool send_ping(struct meshlink_handle *mesh, struct connection_t *);
extern bool send_pong(struct meshlink_handle *mesh, struct connection_t *);
-extern bool send_add_edge(struct meshlink_handle *mesh, struct connection_t *, const struct edge_t *);
-extern bool send_del_edge(struct meshlink_handle *mesh, struct connection_t *, const struct edge_t *);
+extern bool send_add_edge(struct meshlink_handle *mesh, struct connection_t *, const struct edge_t *, int contradictions);
+extern bool send_del_edge(struct meshlink_handle *mesh, struct connection_t *, const struct edge_t *, int contradictions);
extern void send_key_changed(struct meshlink_handle *mesh);
extern bool send_req_key(struct meshlink_handle *mesh, struct node_t *);
}
}
+ bool blacklisted = false;
+ get_config_bool(lookup_config(c->config_tree, "blacklisted"), &blacklisted);
+
+ if(blacklisted) {
+ logger(mesh, MESHLINK_EPEER, "Peer %s is blacklisted", c->name);
+ return false;
+ }
+
read_ecdsa_public_key(mesh, c);
if(!ecdsa_active(c->ecdsa)) {
for splay_each(node_t, n, mesh->nodes) {
for splay_each(edge_t, e, n->edge_tree) {
- send_add_edge(mesh, c, e);
+ send_add_edge(mesh, c, e, 0);
}
}
}
/* Notify everyone of the new edge */
- send_add_edge(mesh, mesh->everyone, c->edge);
+ send_add_edge(mesh, mesh->everyone, c->edge, 0);
/* Run MST and SSSP algorithms */
extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
-bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e) {
+bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
bool x;
char *address, *port;
sockaddr2str(&e->address, &address, &port);
- x = send_request(mesh, c, "%d %x %s %d %s %s %s %d %x %d", ADD_EDGE, rand(),
+ x = send_request(mesh, c, "%d %x %s %d %s %s %s %d %x %d %d", ADD_EDGE, rand(),
e->from->name, e->from->devclass, e->to->name, address, port, e->to->devclass,
- e->options, e->weight);
+ e->options, e->weight, contradictions);
free(address);
free(port);
sockaddr_t address;
uint32_t options;
int weight;
+ int contradictions = 0;
- if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %x %d",
- from_name, &from_devclass, to_name, to_address, to_port, &to_devclass, &options, &weight) != 8) {
+ if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %x %d %d",
+ from_name, &from_devclass, to_name, to_address, to_port, &to_devclass, &options, &weight, &contradictions) < 8) {
logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ADD_EDGE", c->name);
return false;
}
node_add(mesh, from);
}
+ if(contradictions > 50) {
+ handle_duplicate_node(mesh, from);
+ }
+
from->devclass = from_devclass;
node_write_devclass(mesh, from);
if(from == mesh->self) {
logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not match existing entry",
"ADD_EDGE", c->name);
- send_add_edge(mesh, c, e);
+ send_add_edge(mesh, c, e, 0);
return true;
} else {
logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not match existing entry",
e = new_edge();
e->from = from;
e->to = to;
- send_del_edge(mesh, c, e);
+ send_del_edge(mesh, c, e, mesh->contradicting_add_edge);
free_edge(e);
return true;
}
return true;
}
-bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e) {
- return send_request(mesh, c, "%d %x %s %s", DEL_EDGE, rand(),
- e->from->name, e->to->name);
+bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
+ return send_request(mesh, c, "%d %x %s %s %d", DEL_EDGE, rand(),
+ e->from->name, e->to->name, contradictions);
}
bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
char from_name[MAX_STRING_SIZE];
char to_name[MAX_STRING_SIZE];
node_t *from, *to;
+ int contradictions = 0;
- if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
+ if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" %d", from_name, to_name, &contradictions) < 2) {
logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "DEL_EDGE", c->name);
return false;
}
return true;
}
+ if(contradictions > 50) {
+ handle_duplicate_node(mesh, from);
+ }
+
/* Check if edge exists */
e = lookup_edge(from, to);
logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself",
"DEL_EDGE", c->name);
mesh->contradicting_del_edge++;
- send_add_edge(mesh, c, e); /* Send back a correction */
+ send_add_edge(mesh, c, e, mesh->contradicting_del_edge); /* Send back a correction */
return true;
}
e = lookup_edge(to, mesh->self);
if(e) {
- send_del_edge(mesh, mesh->everyone, e);
+ send_del_edge(mesh, mesh->everyone, e, 0);
edge_del(mesh, e);
}
}
TESTS = \
basic.test \
basicpp.test \
- trio.test \
channels.test \
channels-fork.test \
channels-cornercases.test \
+ duplicate.test \
import-export.test \
invite-join.test \
- sign-verify.test
+ sign-verify.test \
+ trio.test
dist_check_SCRIPTS = $(TESTS)
check_PROGRAMS = \
basic \
basicpp \
- trio \
channels \
channels-fork \
channels-cornercases \
+ duplicate \
+ echo-fork \
import-export \
invite-join \
sign-verify \
- echo-fork
+ trio
if INSTALL_TESTS
bin_PROGRAMS = $(check_PROGRAMS)
basicpp_SOURCES = basicpp.cpp
basicpp_LDADD = ../src/libmeshlink.la
-trio_SOURCES = trio.c
-trio_LDADD = ../src/libmeshlink.la
-
channels_SOURCES = channels.c
channels_LDADD = ../src/libmeshlink.la
channels_cornercases_SOURCES = channels-cornercases.c utils.c utils.h
channels_cornercases_LDADD = ../src/libmeshlink.la
+duplicate_SOURCES = duplicate.c
+duplicate_LDADD = ../src/libmeshlink.la
+
echo_fork_SOURCES = echo-fork.c
echo_fork_LDADD = ../src/libmeshlink.la
sign_verify_SOURCES = sign-verify.c
sign_verify_LDADD = ../src/libmeshlink.la
+
+trio_SOURCES = trio.c
+trio_LDADD = ../src/libmeshlink.la