+ 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);