*/
typedef void (*receive_cb_t)(mesh *mesh, node *source, const void *data, size_t len);
+/// A callback reporting the meta-connection attempt made by the host node to an another node.
+/** @param mesh A handle which represents an instance of MeshLink.
+ * @param node A pointer to a meshlink_node_t describing the node to whom meta-connection is being tried.
+ * This pointer is valid until meshlink_close() is called.
+ */
+typedef void (*connection_try_cb_t)(mesh *mesh, node *node);
+
/// A callback reporting node status changes.
/** @param mesh A handle which represents an instance of MeshLink.
* @param node A pointer to a meshlink::node describing the node whose status changed.
(void)message;
}
+ /// This functions is called whenever MeshLink a meta-connection attempt is made.
+ virtual void connection_try(node *peer) {
+ /* do nothing */
+ (void)peer;
+ }
+
/// This functions is called whenever another node attempts to open a channel to the local node.
/**
* If the channel is accepted, the poll_callback will be set to channel_poll and can be
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);
+ meshlink_set_connection_try_cb(handle, &connection_try_trampoline);
return meshlink_start(handle);
}
that->log(level, message);
}
+ static void connection_try_trampoline(meshlink_handle_t *handle, meshlink_node_t *peer) {
+ if(!(handle->priv)) {
+ return;
+ }
+
+ meshlink::mesh *that = static_cast<mesh *>(handle->priv);
+ that->connection_try(static_cast<node *>(peer));
+ }
+
static bool channel_accept_trampoline(meshlink_handle_t *handle, meshlink_channel *channel, uint16_t port, const void *data, size_t len) {
if(!(handle->priv)) {
return false;
pthread_mutex_unlock(&(mesh->mesh_mutex));
}
+void meshlink_set_connection_try_cb(meshlink_handle_t *mesh, meshlink_connection_try_cb_t cb) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ mesh->connection_try_cb = cb;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+}
+
void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
if(!mesh) {
meshlink_errno = MESHLINK_EINVAL;
*/
extern void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb);
+/// A callback reporting the meta-connection attempt made by the host node to an another node.
+/** @param mesh A handle which represents an instance of MeshLink.
+ * @param node A pointer to a meshlink_node_t describing the node to whom meta-connection is being tried.
+ * This pointer is valid until meshlink_close() is called.
+ */
+typedef void (*meshlink_connection_try_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *node);
+
+/// Set the meta-connection try callback.
+/** This functions sets the callback that is called whenever a connection attempt is happened to another node.
+ * 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 host node attempts to make
+ * the connection to another node. If a NULL pointer is given, the callback will be disabled.
+ */
+extern void meshlink_set_connection_try_cb(meshlink_handle_t *mesh, meshlink_connection_try_cb_t cb);
+
/// A callback reporting node status changes.
/** @param mesh A handle which represents an instance of MeshLink.
* @param node A pointer to a meshlink_node_t describing the node whose status changed.
meshlink_channel_accept_cb_t channel_accept_cb;
meshlink_node_duplicate_cb_t node_duplicate_cb;
+ meshlink_connection_try_cb_t connection_try_cb;
pthread_t thread;
bool threadstarted;
return;
}
+ if(mesh->connection_try_cb) {
+ node_t *n = lookup_node(mesh, outgoing->name);
+ mesh->connection_try_cb(mesh, (struct meshlink_node *)n);
+ }
+
do_outgoing_connection(mesh, outgoing);
}
#include <signal.h>
#include <sys/types.h>
#include "common_handlers.h"
-#include "containers.h"
#include "tcpdump.h"
pid_t tcpdump_start(char *interface) {
test_cases_submesh02.c \
test_cases_submesh03.c \
test_cases_submesh04.c \
- test_cases_autoconnect.c
+ test_cases_autoconnect.c \
+ test_cases_set_connection_try_cb.c
run_blackbox_tests_LDADD = ../../../src/libmeshlink.la $(LXC_LIBS) $(CMOCKA_LIBS)
run_blackbox_tests_CFLAGS = -D_GNU_SOURCE $(LXC_CFLAGS) $(CMOCKA_CFLAGS)
/*
run_blackbox_tests.c -- Implementation of Black Box Test Execution for meshlink
- Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
+ Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "test_cases_submesh02.h"
#include "test_cases_submesh03.h"
#include "test_cases_submesh04.h"
-
#include "test_cases_autoconnect.h"
+#include "test_cases_set_connection_try_cb.h"
#include "../common/containers.h"
#include "../common/common_handlers.h"
int failed_tests = 0;
- /*
failed_tests += test_meta_conn();
failed_tests += test_meshlink_set_status_cb();
failed_tests += test_meshlink_join();
failed_tests += test_meshlink_channel_close();
failed_tests += test_meshlink_channel_conn();
- */
failed_tests += test_optimal_pmtu();
failed_tests += test_cases_submesh01();
failed_tests += test_cases_submesh04();
failed_tests += test_meshlink_autoconnect();
+ failed_tests += test_cases_connection_try();
failed_tests += test_optimal_pmtu();
--- /dev/null
+/*
+ test_cases_set_connection_try_cb.c -- Execution of specific meshlink black box test cases
+ Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "execute_tests.h"
+#include "test_cases_set_connection_try_cb.h"
+#include "../common/test_step.h"
+#include "../common/common_handlers.h"
+#include "../../utils.h"
+#include <assert.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <pthread.h>
+
+static void test_case_set_connection_try_cb_01(void **state);
+static bool test_set_connection_try_cb_01(void);
+
+static bool bar_reachable;
+static int connection_attempts;
+static struct sync_flag status_changed_cond = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
+static struct sync_flag connection_attempt_cond = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
+
+static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *source, bool reachable) {
+ if(!strcmp(mesh->name, "foo") && !strcmp(source->name, "bar")) {
+ bar_reachable = reachable;
+ set_sync_flag(&status_changed_cond, true);
+ }
+}
+
+/* Meta-connection try callback handler */
+static void connection_try_cb(meshlink_handle_t *mesh, meshlink_node_t *source) {
+ if(!strcmp(mesh->name, "foo")) {
+ ++connection_attempts;
+
+ if(connection_attempts > 3) {
+ set_sync_flag(&connection_attempt_cond, true);
+ }
+ }
+}
+
+/* Execute set meta connection try callback Test Case # 1 */
+static void test_case_set_connection_try_cb_01(void **state) {
+ execute_test(test_set_connection_try_cb_01, state);
+}
+
+/* Test Steps for meshlink_set_connection_try_cb Test Case # 1
+
+ Test Steps:
+ 1. Run foo and bar nodes after exporting and importing node's keys and addresses mutually.
+ 2. Close bar node. Wait for connection attempts and cleanup.
+
+ Expected Result:
+ Connection try callback should be invoked initially when foo and bar forms a meta-connection.
+ After closing bar node it should invoke 3 connection try callbacks in span of about 30 seconds.
+*/
+static bool test_set_connection_try_cb_01(void) {
+ meshlink_destroy("meshlink_conf.1");
+ meshlink_destroy("meshlink_conf.2");
+
+ // Opening foo and bar nodes
+ meshlink_handle_t *mesh1 = meshlink_open("meshlink_conf.1", "foo", "test", DEV_CLASS_STATIONARY);
+ assert(mesh1 != NULL);
+ meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, meshlink_callback_logger);
+ meshlink_enable_discovery(mesh1, false);
+ meshlink_handle_t *mesh2 = meshlink_open("meshlink_conf.2", "bar", "test", DEV_CLASS_STATIONARY);
+ assert(mesh2 != NULL);
+
+ // Set up callback for node status
+ meshlink_set_node_status_cb(mesh1, node_status_cb);
+ meshlink_set_connection_try_cb(mesh1, connection_try_cb);
+
+ // Exporting and Importing mutually
+ char *exp1 = meshlink_export(mesh1);
+ assert(exp1 != NULL);
+ char *exp2 = meshlink_export(mesh2);
+ assert(exp2 != NULL);
+ bool imp1 = meshlink_import(mesh1, exp2);
+ bool imp2 = meshlink_import(mesh2, exp1);
+ free(exp1);
+ free(exp2);
+
+ assert(meshlink_start(mesh1));
+ assert(meshlink_start(mesh2));
+
+ // Wait for foo and bar nodes to join
+ assert(wait_sync_flag(&status_changed_cond, 5));
+ assert(bar_reachable);
+
+ // Joining should in this case raise one connection try callback
+ assert_int_equal(connection_attempts, 1);
+
+ // Close the bar node
+ set_sync_flag(&status_changed_cond, false);
+ meshlink_close(mesh2);
+ assert(wait_sync_flag(&status_changed_cond, 5));
+ assert(!bar_reachable);
+
+ // Wait for additional 3 connection try callbacks
+ time_t attempt_time_start = time(NULL);
+ assert(attempt_time_start != -1);
+ assert_int_equal(wait_sync_flag(&connection_attempt_cond, 60), true);
+
+ // Close bar node and assert on number of callbacks invoked and the time taken.
+ meshlink_close(mesh1);
+ time_t attempt_time_stop = time(NULL);
+ assert(attempt_time_stop != -1);
+ assert_int_equal(connection_attempts, 4);
+ assert_in_range(attempt_time_stop - attempt_time_start, 25, 45);
+
+ // Cleanup
+ meshlink_destroy("meshlink_conf.1");
+ meshlink_destroy("meshlink_conf.2");
+
+ return true;
+}
+
+int test_cases_connection_try(void) {
+ black_box_state_t test_case_set_connection_try_cb_01_state = {
+ .test_case_name = "test_case_set_connection_try_cb_01",
+ };
+
+ const struct CMUnitTest blackbox_connection_try_tests[] = {
+ cmocka_unit_test_prestate_setup_teardown(test_case_set_connection_try_cb_01, NULL, NULL,
+ (void *)&test_case_set_connection_try_cb_01_state),
+ };
+ total_tests += sizeof(blackbox_connection_try_tests) / sizeof(blackbox_connection_try_tests[0]);
+
+ int failed = cmocka_run_group_tests(blackbox_connection_try_tests, NULL, NULL);
+
+ return failed;
+}
--- /dev/null
+#ifndef TEST_CASES_SET_CONNECTION_TRY_CB_H
+#define TEST_CASES_SET_CONNECTION_TRY_CB_H
+
+/*
+ test_cases_set_connection_try_cb.h -- Declarations for Individual Test Case implementation functions
+ Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+extern int test_cases_connection_try(void);
+extern int total_tests;
+
+#endif // TEST_CASES_SET_CONNECTION_TRY_CB_H