From: Guus Sliepen Date: Fri, 22 Aug 2014 08:58:47 +0000 (+0200) Subject: Merge branch 'channels' X-Git-Url: http://git.meshlink.io/?p=meshlink;a=commitdiff_plain;h=01eaeb3c9fa60ae9c6e5b866acd9baef79622d99;hp=0a81750b70ecca975e4b8d532111a572576abcff Merge branch 'channels' Conflicts: .gitmodules src/node.h --- diff --git a/.gitmodules b/.gitmodules index 0e6f5440..e938b091 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,3 +2,6 @@ path = avahi url = git@chicago.everbase.net:meshlink/avahi-noptr.git branch = noptr +[submodule "src/utcp"] + path = src/utcp + url = git://meshlink.io/utcp diff --git a/src/Makefile.am b/src/Makefile.am index cae2ab95..a2a9305a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -28,6 +28,9 @@ chacha_poly1305_SOURCES = \ chacha-poly1305/chacha-poly1305.c chacha-poly1305/chacha-poly1305.h \ chacha-poly1305/poly1305.c chacha-poly1305/poly1305.h +utcp_SOURCES = \ + utcp/utcp.c utcp/utcp.h + sptps_test_SOURCES = \ crypto.c crypto.h \ logger.c logger.h \ @@ -101,7 +104,8 @@ libmeshlink_la_SOURCES = \ xalloc.h \ devtools.c devtools.h \ $(ed25519_SOURCES) \ - $(chacha_poly1305_SOURCES) + $(chacha_poly1305_SOURCES) \ + $(utcp_SOURCES) libmeshlink_la_CFLAGS = -fPIC -I../avahi/ diff --git a/src/meshlink.c b/src/meshlink.c index ddaa740b..ef02580e 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1914,6 +1914,76 @@ meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink return result; } +static bool channel_pre_accept(struct utcp *utcp, uint16_t port) { + //TODO: implement + return false; +} + +static void channel_accept(struct utcp_connection *utcp_connection, uint16_t port) { + //TODO: implement +} + +static int channel_recv(struct utcp_connection *connection, const void *data, size_t len) { + meshlink_channel_t *channel = connection->priv; + node_t *n = channel->node; + meshlink_handle_t *mesh = n->mesh; + if(!channel->receive_cb) + return -1; + else { + channel->receive_cb(mesh, channel, data, len); + return 0; + } +} + +static int channel_send(struct utcp *utcp, const void *data, size_t len) { + node_t *n = utcp->priv; + meshlink_handle_t *mesh = n->mesh; + return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? len : -1; +} + +void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) { + mesh->channel_accept_cb = cb; +} + +void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb) { + channel->receive_cb = cb; +} + +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) { + node_t *n = (node_t *)node; + if(!n->utcp) { + n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); + if(!n->utcp) + return NULL; + } + meshlink_channel_t *channel = xzalloc(sizeof *channel); + channel->node = n; + channel->receive_cb = cb; + channel->c = utcp_connect(n->utcp, port, channel_recv, channel); + if(!channel->c) { + free(channel); + return NULL; + } + return channel; +} + +void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *channel, int direction) { + utcp_shutdown(channel->c, direction); +} + +void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel) { + 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) { + // TODO: 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. + return utcp_send(channel->c, data, len); +} + static void __attribute__((constructor)) meshlink_init(void) { crypto_init(); } diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 994f25bf..517dc1e0 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -74,6 +74,8 @@ struct meshlink_handle { meshlink_log_cb_t log_cb; meshlink_log_level_t log_level; + meshlink_channel_accept_cb_t channel_accept_cb; + pthread_t thread; bool threadstarted; pthread_mutex_t outpacketqueue_mutex; @@ -148,6 +150,13 @@ struct meshlink_node { void *priv; }; +/// A channel. +struct meshlink_channel { + struct utcp_connection *c; + struct node_t *node; + meshlink_channel_receive_cb_t receive_cb; +}; + /// Header for data packets routed between nodes typedef struct meshlink_packethdr { uint8_t destination[16]; diff --git a/src/node.h b/src/node.h index c86c3cee..5761e4d7 100644 --- a/src/node.h +++ b/src/node.h @@ -23,6 +23,7 @@ #include "event.h" #include "sockaddr.h" #include "sptps.h" +#include "utcp/utcp.h" typedef struct node_status_t { unsigned int unused_active:1; /* 1 if active (not used for nodes) */ @@ -40,9 +41,10 @@ typedef struct node_status_t { typedef struct node_t { char *name; /* name of this node */ + void *priv; + uint32_t options; /* options turned on for this node */ dev_class_t devclass; - void *priv; struct meshlink_handle *mesh; /* The mesh this node belongs to */ @@ -89,6 +91,8 @@ typedef struct node_t { float bandwidth; /* Last measured bandwidth */ float packetloss; /* Last measured packet loss rate */ + struct utcp *utcp; + uint64_t in_packets; uint64_t in_bytes; uint64_t out_packets; diff --git a/src/utcp b/src/utcp new file mode 160000 index 00000000..69348803 --- /dev/null +++ b/src/utcp @@ -0,0 +1 @@ +Subproject commit 6934880365c13cfebe5806be0f40c4c45a5dcf84