#include <time.h>
#include "../common/common_handlers.h"
#include "../common/test_step.h"
-#include "../common/mesh_event_handler.h"
+#include "../common/network_namespace_framework.h"
#include "../../utils.h"
#include "../run_blackbox_tests/test_optimal_pmtu.h"
-#define CMD_LINE_ARG_NODENAME 1
-#define CMD_LINE_ARG_DEVCLASS 2
-#define CMD_LINE_ARG_CLIENTID 3
-#define CMD_LINE_ARG_IMPORTSTR 4
-#define CMD_LINE_ARG_INVITEURL 5
-#define CHANNEL_PORT 1234
-
-#pragma pack(1)
-
-static int client_id = -1;
+extern bool test_pmtu_nut_running;
+extern bool test_pmtu_peer_running;
+extern bool test_pmtu_relay_running;
+extern struct sync_flag test_pmtu_nut_closed;
+extern bool ping_channel_enable_07;
static struct sync_flag peer_reachable = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
static struct sync_flag channel_opened = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
bool reachable);
static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
-static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
-static pmtu_attr_t node_pmtu[2];
+pmtu_attr_t node_pmtu[2];
+static time_t node_shutdown_time = 0;
+static bool mtu_set = true;
static void print_mtu_calc(pmtu_attr_t node_pmtu) {
fprintf(stderr, "MTU size : %d\n", node_pmtu.mtu_size);
}
// Node status callback
-static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
- bool reachable) {
+static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
+ (void)mesh;
+
// Signal pthread_cond_wait if peer is reachable
if(!strcasecmp(node->name, "peer") && reachable) {
set_sync_flag(&peer_reachable, true);
}
- // Notify the JOIN or LEFT event of a node to the test driver
- mesh_event_sock_send(client_id, reachable ? NODE_JOINED : NODE_LEFT, node->name, 100);
return;
}
return;
}
-static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
- (void)dat;
- (void)len;
-
- assert(port == CHANNEL_PORT);
-
- // If the channel is from peer node set receive callback for it else reject the channel
- if(!strcmp(channel->node->name, "peer")) {
- meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
- mesh->priv = channel;
-
- return true;
- }
-
- return false;
-}
-
/* channel receive callback */
static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
if(len == 0) {
- mesh_event_sock_send(client_id, ERR_NETWORK, channel->node->name, 100);
+ fail();
return;
}
if(!strcmp(channel->node->name, "peer")) {
if(!memcmp(dat, "reply", 5)) {
set_sync_flag(&channel_opened, true);
- fprintf(stderr, "GOT REPLY FROM PEER\n");
} else if(!memcmp(dat, "test", 5)) {
assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
}
}
// Meshlink log handler
-void meshlink_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
- const char *text) {
+static void meshlink_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
+ const char *text) {
(void)mesh;
(void)level;
int probe_len;
cur_time = time(NULL);
assert(cur_time != -1);
- static time_t node_shutdown_time = 0;
- bool mtu_probe = false;
-
if(node_shutdown_time && cur_time >= node_shutdown_time) {
- test_running = false;
+ test_pmtu_nut_running = false;
}
- static const char *levelstr[] = {
- [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
- [MESHLINK_INFO] = "\x1b[32mINFO",
- [MESHLINK_WARNING] = "\x1b[33mWARNING",
- [MESHLINK_ERROR] = "\x1b[31mERROR",
- [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
- };
-
- fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
+ if(level == MESHLINK_INFO) {
+ fprintf(stderr, "\x1b[32m nut:\x1b[0m %s\n", text);
+ }
/* Calculate the MTU parameter values from the meshlink logs */
if(sscanf(text, "Sending MTU probe length %d to %s", &probe_len, node_name) == 2) {
node_pmtu[i].mtu_recv_probes.time = cur_time;
} else if(sscanf(text, "Fixing MTU of %s to %d after %d probes", node_name, &mtu_len, &probes) == 3) {
- static bool mtu_set = true;
- if(!node_shutdown_time && !strcasecmp("relay", node_name) && mtu_set) {
+ if(!node_shutdown_time && !strcasecmp("peer", node_name) && mtu_set) {
node_shutdown_time = cur_time + PING_TRACK_TIMEOUT;
mtu_set = false;
}
node_pmtu[i].mtu_discovery.count += 1;
node_pmtu[i].mtu_size = mtu_len;
- } else if(sscanf(text, "SPTPS key exchange with %s succesful", node_name) == 1) {
+ } else if(sscanf(text, "SPTPS key exchange with %s successful", node_name) == 1) {
find_node_index(i, node_name);
node_pmtu[i].mtu_start.time = cur_time;
node_pmtu[i].mtu_start.count += 1;
} else if(sscanf(text, "No response to MTU probes from %s", node_name) == 1) {
} else if((sscanf(text, "Connection with %s activated", node_name) == 1) || (sscanf(text, "Already connected to %s", node_name) == 1)) {
- mesh_event_sock_send(client_id, META_CONN_SUCCESSFUL, node_name, sizeof(node_name));
} else if((sscanf(text, "Connection closed by %s", node_name) == 1) || (sscanf(text, "Closing connection with %s", node_name) == 1)) {
- mesh_event_sock_send(client_id, META_CONN_CLOSED, node_name, sizeof(node_name));
-
}
}
-int main(int argc, char *argv[]) {
+void *node_sim_pmtu_nut_01(void *arg) {
+ mesh_arg_t *mesh_arg = (mesh_arg_t *)arg;
struct timeval main_loop_wait = { 5, 0 };
- int i;
-
- // Import mesh event handler
- if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
- client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
- mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
- }
-
- setup_signals();
+ set_sync_flag(&peer_reachable, false);
+ set_sync_flag(&channel_opened, false);
+ node_shutdown_time = 0;
+ mtu_set = true;
- // Execute test steps
+ // Run relay node instance
- meshlink_handle_t *mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
- "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
+ meshlink_handle_t *mesh;
+ mesh = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class);
assert(mesh);
meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_logger);
meshlink_set_node_status_cb(mesh, node_status_cb);
- meshlink_enable_discovery(mesh, false);
sleep(1);
// Join relay node and if fails to join then try few more attempts
- if(argv[CMD_LINE_ARG_INVITEURL]) {
+ if(mesh_arg->join_invitation) {
int attempts;
bool join_ret;
for(attempts = 0; attempts < 10; attempts++) {
- join_ret = meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]);
+ join_ret = meshlink_join(mesh, mesh_arg->join_invitation);
if(join_ret) {
break;
}
if(attempts == 10) {
- abort();
+ fail();
}
}
meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
assert(wait_sync_flag(&channel_opened, 30));
- assert(mesh_event_sock_send(client_id, CHANNEL_OPENED, NULL, 0));
// All test steps executed - wait for signals to stop/start or close the mesh
- while(test_running) {
+ time_t time_stamp, send_time;
+
+ time_stamp = time(NULL);
+ send_time = time_stamp + 10;
+
+ while(test_pmtu_nut_running) {
select(1, NULL, NULL, NULL, &main_loop_wait);
+
+ // Ping the channel for every 10 seconds if ping_channel_enable_07 is enabled
+
+ if(ping_channel_enable_07) {
+ time_stamp = time(NULL);
+
+ if(time_stamp >= send_time) {
+ send_time = time_stamp + 10;
+ meshlink_channel_send(mesh, channel, "ping", 5);
+ }
+ }
}
// Send MTU probe parameters data to the test driver
+ meshlink_close(mesh);
- pmtu_attr_t send_mtu_data;
- send_mtu_data = node_pmtu[NODE_PMTU_PEER];
- print_mtu_calc(send_mtu_data);
- assert(mesh_event_sock_send(client_id, OPTIMAL_PMTU_PEER, &send_mtu_data, sizeof(send_mtu_data)));
- send_mtu_data = node_pmtu[NODE_PMTU_RELAY];
- print_mtu_calc(send_mtu_data);
- assert(mesh_event_sock_send(client_id, OPTIMAL_PMTU_RELAY, &send_mtu_data, sizeof(send_mtu_data)));
+ set_sync_flag(&test_pmtu_nut_closed, true);
+ fprintf(stderr, "NODE_PMTU_PEER :\n");
+ print_mtu_calc(node_pmtu[NODE_PMTU_PEER]);
+ fprintf(stderr, "\nNODE_PMTU_RELAY :\n");
+ print_mtu_calc(node_pmtu[NODE_PMTU_RELAY]);
- meshlink_close(mesh);
+ return NULL;
}