+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return;
+}
+
+void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) {
+ if(!mesh || !node) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ node_t *n = (node_t *)node;
+ n->status.blacklisted = false;
+
+ //TODO: remove blacklisted = yes from the config file
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return;
+}
+
+void meshlink_set_default_blacklist(meshlink_handle_t *mesh, bool blacklist) {
+ mesh->default_blacklist = blacklist;
+}
+
+/* Hint that a hostname may be found at an address
+ * See header file for detailed comment.
+ */
+void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const struct sockaddr *addr) {
+ if(!mesh || !node || !addr) {
+ return;
+ }
+
+ // Ignore hints about ourself.
+ if((node_t *)node == mesh->self) {
+ return;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ char *host = NULL, *port = NULL, *str = NULL;
+ sockaddr2str((const sockaddr_t *)addr, &host, &port);
+
+ if(host && port) {
+ xasprintf(&str, "%s %s", host, port);
+
+ if((strncmp("fe80", host, 4) != 0) && (strncmp("127.", host, 4) != 0) && (strcmp("localhost", host) != 0)) {
+ modify_config_file(mesh, node->name, "Address", str, 5);
+ } else {
+ logger(mesh, MESHLINK_DEBUG, "Not adding Link Local IPv6 Address to config\n");
+ }
+ }
+
+ free(str);
+ free(host);
+ free(port);
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ // @TODO do we want to fire off a connection attempt right away?
+}
+
+static bool channel_pre_accept(struct utcp *utcp, uint16_t port) {
+ (void)port;
+ node_t *n = utcp->priv;
+ meshlink_handle_t *mesh = n->mesh;
+ return mesh->channel_accept_cb;
+}
+
+static ssize_t channel_recv(struct utcp_connection *connection, const void *data, size_t len) {
+ meshlink_channel_t *channel = connection->priv;
+
+ if(!channel) {
+ abort();
+ }
+
+ node_t *n = channel->node;
+ meshlink_handle_t *mesh = n->mesh;
+
+ if(n->status.destroyed) {
+ meshlink_channel_close(mesh, channel);
+ } else if(channel->receive_cb) {
+ channel->receive_cb(mesh, channel, data, len);
+ }
+
+ return len;
+}
+
+static void channel_accept(struct utcp_connection *utcp_connection, uint16_t port) {
+ node_t *n = utcp_connection->utcp->priv;
+
+ if(!n) {
+ abort();
+ }
+
+ meshlink_handle_t *mesh = n->mesh;
+
+ if(!mesh->channel_accept_cb) {
+ return;
+ }
+
+ meshlink_channel_t *channel = xzalloc(sizeof(*channel));
+ channel->node = n;
+ channel->c = utcp_connection;
+
+ if(mesh->channel_accept_cb(mesh, channel, port, NULL, 0)) {
+ utcp_accept(utcp_connection, channel_recv, channel);
+ } else {
+ free(channel);
+ }
+}
+
+static ssize_t channel_send(struct utcp *utcp, const void *data, size_t len) {
+ node_t *n = utcp->priv;
+
+ if(n->status.destroyed) {
+ return -1;
+ }
+
+ meshlink_handle_t *mesh = n->mesh;
+ return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? (ssize_t)len : -1;
+}
+
+void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb) {
+ if(!mesh || !channel) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ channel->receive_cb = cb;
+}
+
+static void channel_receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
+ (void)mesh;
+ node_t *n = (node_t *)source;
+
+ if(!n->utcp) {
+ abort();
+ }
+
+ utcp_recv(n->utcp, data, len);
+}
+
+static void channel_poll(struct utcp_connection *connection, size_t len) {
+ meshlink_channel_t *channel = connection->priv;
+
+ if(!channel) {
+ abort();
+ }
+
+ node_t *n = channel->node;
+ meshlink_handle_t *mesh = n->mesh;
+
+ if(channel->poll_cb) {
+ channel->poll_cb(mesh, channel, len);
+ }
+}
+
+void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_poll_cb_t cb) {
+ (void)mesh;
+ channel->poll_cb = cb;
+ utcp_set_poll_cb(channel->c, cb ? channel_poll : NULL);
+}
+
+void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&mesh->mesh_mutex);
+ mesh->channel_accept_cb = cb;
+ mesh->receive_cb = channel_receive;
+
+ for splay_each(node_t, n, mesh->nodes) {
+ if(!n->utcp && n != mesh->self) {
+ n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
+ }
+ }
+
+ pthread_mutex_unlock(&mesh->mesh_mutex);
+}
+
+meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len, uint32_t flags) {
+ if(data || len) {
+ abort(); // TODO: handle non-NULL data
+ }
+
+ if(!mesh || !node) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ node_t *n = (node_t *)node;
+
+ if(!n->utcp) {
+ n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
+ mesh->receive_cb = channel_receive;
+
+ if(!n->utcp) {
+ meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL;
+ return NULL;
+ }
+ }
+
+ meshlink_channel_t *channel = xzalloc(sizeof(*channel));
+ channel->node = n;
+ channel->receive_cb = cb;
+ channel->c = utcp_connect_ex(n->utcp, port, channel_recv, channel, flags);
+
+ if(!channel->c) {
+ meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL;
+ free(channel);
+ return NULL;
+ }
+
+ return channel;
+}
+
+meshlink_channel_t *meshlink_channel_open(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len) {
+ return meshlink_channel_open_ex(mesh, node, port, cb, data, len, MESHLINK_CHANNEL_TCP);
+}
+
+void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *channel, int direction) {
+ if(!mesh || !channel) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ utcp_shutdown(channel->c, direction);
+}
+
+void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel) {
+ if(!mesh || !channel) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ utcp_close(channel->c);
+ free(channel);
+}
+
+ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
+ if(!mesh || !channel) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return -1;
+ }
+
+ if(!len) {
+ return 0;
+ }
+
+ if(!data) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return -1;
+ }
+
+ // TODO: more finegrained locking.
+ // Ideally we want to put the data into the UTCP connection's send buffer.
+ // Then, preferrably only if there is room in the receiver window,
+ // kick the meshlink thread to go send packets.
+
+ pthread_mutex_lock(&mesh->mesh_mutex);
+ ssize_t retval = utcp_send(channel->c, data, len);
+ pthread_mutex_unlock(&mesh->mesh_mutex);
+
+ if(retval < 0) {
+ meshlink_errno = MESHLINK_ENETWORK;
+ }
+
+ return retval;
+}
+
+uint32_t meshlink_channel_get_flags(meshlink_handle_t *mesh, meshlink_channel_t *channel) {
+ if(!mesh || !channel) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return -1;
+ }
+
+ return channel->c->flags;
+}
+
+void update_node_status(meshlink_handle_t *mesh, node_t *n) {
+ if(n->status.reachable && mesh->channel_accept_cb && !n->utcp) {
+ n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
+ }
+
+ if(mesh->node_status_cb) {
+ mesh->node_status_cb(mesh, (meshlink_node_t *)n, n->status.reachable);
+ }
+}
+
+void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&mesh->mesh_mutex);
+
+ if(mesh->discovery == enable) {
+ goto end;
+ }
+
+ if(mesh->threadstarted) {
+ if(enable) {
+ discovery_start(mesh);
+ } else {
+ discovery_stop(mesh);
+ }
+ }
+
+ mesh->discovery = enable;
+
+end:
+ pthread_mutex_unlock(&mesh->mesh_mutex);