From: Guus Sliepen Date: Wed, 29 Jul 2020 12:40:27 +0000 (+0200) Subject: Ensure the poll callback is called when a UDP channel has finished connecting. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=f84fc9da827afbc7bc7bd14fa94b84ee9922e575;p=meshlink Ensure the poll callback is called when a UDP channel has finished connecting. --- diff --git a/src/utcp.c b/src/utcp.c index acd8188b..7ac96c62 100644 --- a/src/utcp.c +++ b/src/utcp.c @@ -650,6 +650,7 @@ void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) { debug(c, "accepted %p %p\n", c, recv, priv); c->recv = recv; c->priv = priv; + c->do_poll = true; set_state(c, ESTABLISHED); } @@ -1718,6 +1719,7 @@ skip_ack: c->snd.last++; set_state(c, FIN_WAIT_1); } else { + c->do_poll = true; set_state(c, ESTABLISHED); } diff --git a/test/Makefile.am b/test/Makefile.am index 6dcfc610..e398eeb3 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -11,6 +11,7 @@ TESTS = \ channels-fork \ channels-no-partial \ channels-udp \ + channels-udp-cornercases \ duplicate \ encrypted \ ephemeral \ @@ -50,6 +51,7 @@ check_PROGRAMS = \ channels-fork \ channels-no-partial \ channels-udp \ + channels-udp-cornercases \ duplicate \ echo-fork \ encrypted \ @@ -106,6 +108,9 @@ channels_cornercases_LDADD = $(top_builddir)/src/libmeshlink.la channels_udp_SOURCES = channels-udp.c utils.c utils.h channels_udp_LDADD = $(top_builddir)/src/libmeshlink.la +channels_udp_cornercases_SOURCES = channels-udp-cornercases.c utils.c utils.h +channels_udp_cornercases_LDADD = $(top_builddir)/src/libmeshlink.la + duplicate_SOURCES = duplicate.c utils.c utils.h duplicate_LDADD = $(top_builddir)/src/libmeshlink.la diff --git a/test/channels-udp-cornercases.c b/test/channels-udp-cornercases.c new file mode 100644 index 00000000..d70a6cd5 --- /dev/null +++ b/test/channels-udp-cornercases.c @@ -0,0 +1,175 @@ +#define _GNU_SOURCE + +#ifdef NDEBUG +#undef NDEBUG +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "../src/meshlink.h" +#include "utils.h" + +static struct sync_flag b_responded; +static struct sync_flag b_closed; +static size_t a_poll_cb_len; + +static void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { + (void)mesh; + (void)channel; + + if(len == 5 && !memcmp(data, "Hello", 5)) { + set_sync_flag(&b_responded, true); + } else if(len == 0) { + set_sync_flag(&b_closed, true); + set_sync_flag(channel->priv, true); + } +} + +static void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { + // Send one message back, then close the channel. + if(len) { + assert(meshlink_channel_send(mesh, channel, data, len) == (ssize_t)len); + } + + meshlink_channel_close(mesh, channel); +} + +static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { + (void)mesh; + (void)channel; + (void)port; + (void)data; + (void)len; + + return false; +} + +static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { + (void)port; + + meshlink_set_channel_accept_cb(mesh, NULL); + meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb); + + if(data) { + b_receive_cb(mesh, channel, data, len); + } + + return true; +} + +static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) { + (void)len; + + meshlink_set_channel_poll_cb(mesh, channel, NULL); + set_sync_flag(channel->priv, true); +} + +static void poll_cb2(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) { + (void)mesh; + (void)channel; + + a_poll_cb_len = len; + meshlink_set_channel_poll_cb(mesh, channel, NULL); + set_sync_flag(channel->priv, true); +} + +int main(void) { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + init_sync_flag(&b_responded); + init_sync_flag(&b_closed); + + meshlink_handle_t *a, *b; + open_meshlink_pair(&a, &b, "channels-udp-cornercases"); + + // Set the callbacks. + + meshlink_set_channel_accept_cb(a, reject_cb); + meshlink_set_channel_accept_cb(b, accept_cb); + + // Open a channel from a to b before starting the mesh. + + meshlink_node_t *nb = meshlink_get_node(a, "b"); + assert(nb); + + struct sync_flag channel_opened; + init_sync_flag(&channel_opened); + + meshlink_channel_t *channel = meshlink_channel_open_ex(a, nb, 7, a_receive_cb, &channel_opened, 0, MESHLINK_CHANNEL_UDP); + assert(channel); + + meshlink_set_channel_poll_cb(a, channel, poll_cb); + + // Check that the channel isn't established yet and sending a packet at this point returns 0 + assert(meshlink_channel_send(a, channel, "test", 4) == 0); + assert(wait_sync_flag(&channel_opened, 1) == false); + + // Start MeshLink and wait for the channel to become connected. + start_meshlink_pair(a, b); + + assert(wait_sync_flag(&channel_opened, 5)); + + // Re-initialize everything + meshlink_channel_close(a, channel); + close_meshlink_pair(a, b); + reset_sync_flag(&channel_opened); + reset_sync_flag(&b_responded); + reset_sync_flag(&b_closed); + open_meshlink_pair(&a, &b, "channels-udp-cornercases"); + + meshlink_set_channel_accept_cb(a, reject_cb); + meshlink_set_channel_accept_cb(b, accept_cb); + + start_meshlink_pair(a, b); + + // Create a channel to b + nb = meshlink_get_node(a, "b"); + assert(nb); + + channel = meshlink_channel_open_ex(a, nb, 7, a_receive_cb, &channel_opened, 0, MESHLINK_CHANNEL_UDP); + assert(channel); + meshlink_set_channel_poll_cb(a, channel, poll_cb); + + assert(wait_sync_flag(&channel_opened, 5)); + + // Send a message to b + + struct sync_flag channel_closed; + init_sync_flag(&channel_closed); + channel->priv = &channel_closed; + + for(int i = 0; i < 10; i++) { + assert(meshlink_channel_send(a, channel, "Hello", 5) == 5); + + if(wait_sync_flag(&channel_closed, 1)) { + break; + } + } + + assert(wait_sync_flag(&channel_closed, 1)); + + wait_sync_flag(&b_responded, 1); + wait_sync_flag(&b_closed, 1); + + // Try to create a second channel + + struct sync_flag channel_polled; + init_sync_flag(&channel_polled); + + meshlink_channel_t *channel2 = meshlink_channel_open_ex(a, nb, 7, a_receive_cb, &channel_polled, 0, MESHLINK_CHANNEL_UDP); + assert(channel2); + meshlink_set_channel_poll_cb(a, channel2, poll_cb2); + + assert(wait_sync_flag(&channel_polled, 5)); + + assert(0 == a_poll_cb_len); + + meshlink_channel_close(a, channel); + meshlink_channel_close(a, channel2); + close_meshlink_pair(a, b); +} diff --git a/test/utils.c b/test/utils.c index f1f1c770..f3f338c5 100644 --- a/test/utils.c +++ b/test/utils.c @@ -16,6 +16,7 @@ void init_sync_flag(struct sync_flag *s) { assert(pthread_mutex_init(&s->mutex, NULL) == 0); assert(pthread_cond_init(&s->cond, NULL) == 0); + s->flag = false; } void set_sync_flag(struct sync_flag *s, bool value) {