2 test_step.c -- Handlers for executing test steps during node simulation
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "../../../src/meshlink.h"
25 #include "test_step.h"
26 #include "common_handlers.h"
28 /* Modify this to change the logging level of Meshlink */
29 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
31 meshlink_handle_t *mesh_handle = NULL;
32 bool mesh_started = false;
33 char *eth_if_name = NULL;
35 meshlink_handle_t *execute_open(char *node_name, char *dev_class) {
36 /* Set up logging for Meshlink */
37 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
39 /* Create meshlink instance */
40 mesh_handle = meshlink_open("testconf", node_name, "node_sim", atoi(dev_class));
41 fprintf(stderr, "meshlink_open status: %s\n", meshlink_strerror(meshlink_errno));
42 meshlink_enable_discovery(mesh_handle, false);
43 PRINT_TEST_CASE_MSG("meshlink_open status: %s\n", meshlink_strerror(meshlink_errno));
46 /* Set up logging for Meshlink with the newly acquired Mesh Handle */
47 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
48 /* Set up callback for node status (reachable / unreachable) */
49 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
54 char *execute_invite(char *invitee, meshlink_submesh_t *submesh) {
55 char *invite_url = meshlink_invite_ex(mesh_handle, submesh, invitee, MESHLINK_INVITE_LOCAL | MESHLINK_INVITE_NUMERIC);
57 PRINT_TEST_CASE_MSG("meshlink_invite status: %s\n", meshlink_strerror(meshlink_errno));
63 void execute_join(char *invite_url) {
66 join_status = meshlink_join(mesh_handle, invite_url);
70 void execute_start(void) {
71 bool start_init_status = meshlink_start(mesh_handle);
73 PRINT_TEST_CASE_MSG("meshlink_start status: %s\n", meshlink_strerror(meshlink_errno));
74 assert(start_init_status);
78 void execute_stop(void) {
80 meshlink_stop(mesh_handle);
84 void execute_close(void) {
86 meshlink_close(mesh_handle);
89 void execute_change_ip(void) {
92 char new_ip[20] = "", gateway_ip[20] = "";
96 /* Get existing IP Address of Ethernet Bridge Interface */
97 assert((eth_if_ip = get_ip(eth_if_name)));
99 /* Set new IP Address by replacing the last byte with last byte + 1 */
100 strncpy(new_ip, eth_if_ip, sizeof(new_ip) - 1);
101 assert((last_dot_in_ip = strrchr(new_ip, '.')));
102 last_byte = atoi(last_dot_in_ip + 1);
103 assert(snprintf(last_dot_in_ip + 1, 4, "%d", (last_byte > 253) ? 2 : (last_byte + 1)) >= 0);
105 /* TO DO: Check for IP conflicts with other interfaces and existing Containers */
106 /* Bring the network interface down before making changes */
107 stop_nw_intf(eth_if_name);
108 /* Save the netmask first, then restore it after setting the new IP Address */
109 assert((eth_if_netmask = get_netmask(eth_if_name)));
110 set_ip(eth_if_name, new_ip);
111 set_netmask(eth_if_name, eth_if_netmask);
112 /* Bring the network interface back up again to apply changes */
113 start_nw_intf(eth_if_name);
115 /* Get Gateway's IP Address, by replacing the last byte with 1 in the current IP Address */
116 /* TO DO: Obtain the actual Gateway IP Address */
117 strncpy(gateway_ip, eth_if_ip, sizeof(gateway_ip) - 1);
118 assert((last_dot_in_ip = strrchr(gateway_ip, '.')));
119 assert(snprintf(last_dot_in_ip + 1, 4, "%d", 1) >= 0);
121 /* Add the default route back again, which would have been deleted when the
122 network interface was brought down */
123 /* TO DO: Perform this action using ioctl with SIOCADDRT */
124 /*assert(snprintf(route_chg_command, sizeof(route_chg_command), "route add default gw %s",
126 route_chg_status = system(route_chg_command);
127 PRINT_TEST_CASE_MSG("Default Route Add status = %d\n", route_chg_status);
128 assert(route_chg_status == 0); */
129 // Not necessary for ubuntu versions of 16.04 and 18.04
131 PRINT_TEST_CASE_MSG("Node '%s' IP Address changed to %s\n", NUT_NODE_NAME, new_ip);
134 free(eth_if_netmask);