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.
28 #include "../../../src/meshlink.h"
29 #include "test_step.h"
30 #include "common_handlers.h"
32 /* Modify this to change the logging level of Meshlink */
33 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
35 meshlink_handle_t *mesh_handle = NULL;
36 bool mesh_started = false;
37 char *eth_if_name = NULL;
39 meshlink_handle_t *execute_open(char *node_name, char *dev_class) {
40 /* Set up logging for Meshlink */
41 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
43 /* Create meshlink instance */
44 mesh_handle = meshlink_open("testconf", node_name, "node_sim", atoi(dev_class));
45 fprintf(stderr, "meshlink_open status: %s\n", meshlink_strerror(meshlink_errno));
46 meshlink_enable_discovery(mesh_handle, false);
47 PRINT_TEST_CASE_MSG("meshlink_open status: %s\n", meshlink_strerror(meshlink_errno));
50 /* Set up logging for Meshlink with the newly acquired Mesh Handle */
51 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
52 /* Set up callback for node status (reachable / unreachable) */
53 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
58 char *execute_invite(char *invitee, meshlink_submesh_t *submesh) {
59 char *invite_url = meshlink_invite_ex(mesh_handle, submesh, invitee, MESHLINK_INVITE_LOCAL | MESHLINK_INVITE_NUMERIC);
61 PRINT_TEST_CASE_MSG("meshlink_invite status: %s\n", meshlink_strerror(meshlink_errno));
67 void execute_join(char *invite_url) {
70 join_status = meshlink_join(mesh_handle, invite_url);
74 void execute_start(void) {
75 bool start_init_status = meshlink_start(mesh_handle);
77 PRINT_TEST_CASE_MSG("meshlink_start status: %s\n", meshlink_strerror(meshlink_errno));
78 assert(start_init_status);
82 void execute_stop(void) {
84 meshlink_stop(mesh_handle);
88 void execute_close(void) {
90 meshlink_close(mesh_handle);
93 void execute_change_ip(void) {
96 char new_ip[20] = "", gateway_ip[20] = "";
100 /* Get existing IP Address of Ethernet Bridge Interface */
101 assert((eth_if_ip = get_ip(eth_if_name)));
103 /* Set new IP Address by replacing the last byte with last byte + 1 */
104 strncpy(new_ip, eth_if_ip, sizeof(new_ip) - 1);
105 assert((last_dot_in_ip = strrchr(new_ip, '.')));
106 last_byte = atoi(last_dot_in_ip + 1);
107 assert(snprintf(last_dot_in_ip + 1, 4, "%d", (last_byte > 253) ? 2 : (last_byte + 1)) >= 0);
109 /* TO DO: Check for IP conflicts with other interfaces and existing Containers */
110 /* Bring the network interface down before making changes */
111 stop_nw_intf(eth_if_name);
112 /* Save the netmask first, then restore it after setting the new IP Address */
113 assert((eth_if_netmask = get_netmask(eth_if_name)));
114 set_ip(eth_if_name, new_ip);
115 set_netmask(eth_if_name, eth_if_netmask);
116 /* Bring the network interface back up again to apply changes */
117 start_nw_intf(eth_if_name);
119 /* Get Gateway's IP Address, by replacing the last byte with 1 in the current IP Address */
120 /* TO DO: Obtain the actual Gateway IP Address */
121 strncpy(gateway_ip, eth_if_ip, sizeof(gateway_ip) - 1);
122 assert((last_dot_in_ip = strrchr(gateway_ip, '.')));
123 assert(snprintf(last_dot_in_ip + 1, 4, "%d", 1) >= 0);
125 /* Add the default route back again, which would have been deleted when the
126 network interface was brought down */
127 /* TO DO: Perform this action using ioctl with SIOCADDRT */
128 /*assert(snprintf(route_chg_command, sizeof(route_chg_command), "route add default gw %s",
130 route_chg_status = system(route_chg_command);
131 PRINT_TEST_CASE_MSG("Default Route Add status = %d\n", route_chg_status);
132 assert(route_chg_status == 0); */
133 // Not necessary for ubuntu versions of 16.04 and 18.04
135 PRINT_TEST_CASE_MSG("Node '%s' IP Address changed to %s\n", NUT_NODE_NAME, new_ip);
138 free(eth_if_netmask);