2 node_sim_peer.c -- Implementation of Node Simulation for Meshlink Testing
3 for channel connections with respective to blacklisting their nodes
4 Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "../common/common_handlers.h"
27 #include "../common/test_step.h"
28 #include "../common/network_namespace_framework.h"
29 #include "../../utils.h"
31 #define CHANNEL_PORT 1234
33 bool test_channel_blacklist_disonnection_peer_01_running;
34 bool test_case_signal_peer_restart_01;
36 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
37 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
39 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
43 assert(port == CHANNEL_PORT);
45 if(!strcmp(channel->node->name, "nut")) {
46 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
53 /* channel receive callback */
54 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
57 fprintf(stderr, "Channel closure\n");
60 if(!strcmp(channel->node->name, "nut")) {
61 if(!memcmp(dat, "test", 5)) {
62 assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
69 void *test_channel_blacklist_disonnection_peer_01(void *arg) {
70 struct timeval main_loop_wait = { 2, 0 };
71 mesh_arg_t *mesh_arg = (mesh_arg_t *)arg;
72 test_channel_blacklist_disonnection_peer_01_running = true;
74 // Run relay node instance
76 meshlink_handle_t *mesh;
77 mesh = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class);
79 meshlink_set_channel_accept_cb(mesh, channel_accept);
81 // Join relay node and if fails to join then try few more attempts
83 if(mesh_arg->join_invitation) {
84 assert(meshlink_join(mesh, mesh_arg->join_invitation));
87 assert(meshlink_start(mesh));
89 // All test steps executed - wait for signals to stop/start or close the mesh
91 while(test_channel_blacklist_disonnection_peer_01_running) {
92 select(1, NULL, NULL, NULL, &main_loop_wait);
94 if(test_case_signal_peer_restart_01) {
96 assert(meshlink_start(mesh));
97 test_case_signal_peer_restart_01 = false;
101 meshlink_close(mesh);