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.
31 #include "../common/common_handlers.h"
32 #include "../common/test_step.h"
33 #include "../common/network_namespace_framework.h"
34 #include "../../utils.h"
36 #define CHANNEL_PORT 1234
38 bool test_channel_blacklist_disonnection_peer_01_running;
39 bool test_case_signal_peer_restart_01;
41 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
42 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
44 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
48 assert(port == CHANNEL_PORT);
50 if(!strcmp(channel->node->name, "nut")) {
51 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
58 /* channel receive callback */
59 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
62 fprintf(stderr, "Channel closure\n");
65 if(!strcmp(channel->node->name, "nut")) {
66 if(!memcmp(dat, "test", 5)) {
67 assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
74 void *test_channel_blacklist_disonnection_peer_01(void *arg) {
75 struct timeval main_loop_wait = { 2, 0 };
76 mesh_arg_t *mesh_arg = (mesh_arg_t *)arg;
77 test_channel_blacklist_disonnection_peer_01_running = true;
79 // Run relay node instance
81 meshlink_handle_t *mesh;
82 mesh = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class);
84 meshlink_set_channel_accept_cb(mesh, channel_accept);
86 // Join relay node and if fails to join then try few more attempts
88 if(mesh_arg->join_invitation) {
89 assert(meshlink_join(mesh, mesh_arg->join_invitation));
92 assert(meshlink_start(mesh));
94 // All test steps executed - wait for signals to stop/start or close the mesh
96 while(test_channel_blacklist_disonnection_peer_01_running) {
97 select(1, NULL, NULL, NULL, &main_loop_wait);
99 if(test_case_signal_peer_restart_01) {
101 assert(meshlink_start(mesh));
102 test_case_signal_peer_restart_01 = false;
106 meshlink_close(mesh);