]> git.meshlink.io Git - meshlink/blob - test/blackbox/common/common_handlers.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / common / common_handlers.c
1 /*
2     common_handlers.c -- Implementation of common callback handling and signal handling
3                             functions for black box tests
4     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
5
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.
10
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.
15
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.
19 */
20 #include <stdio.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <sys/ioctl.h>
27 #include <net/if.h>
28 #include <arpa/inet.h>
29 #include <sys/socket.h>
30 #include <netdb.h>
31 #include <ifaddrs.h>
32 #include <ctype.h>
33 #include "test_step.h"
34 #include "common_handlers.h"
35
36 char *lxc_bridge = NULL;
37 black_box_state_t *state_ptr = NULL;
38
39 bool meta_conn_status[10];
40
41 bool test_running;
42
43 static int meshlink_get_node_in_container(const char *name) {
44         int i;
45
46         for(i = 0; i < state_ptr->num_nodes; i++) {
47                 if(!strcasecmp(state_ptr->node_names[i], name)) {
48                         return i;
49                         break;
50                 }
51         }
52
53         return -1;
54 }
55
56 void mesh_close_signal_handler(int signum) {
57         (void)signum;
58         test_running = false;
59
60         exit(EXIT_SUCCESS);
61 }
62
63 void setup_signals(void) {
64         test_running = true;
65         signal(SIGTERM, mesh_close_signal_handler);
66 }
67
68 /* Return the IP Address of the Interface 'if_name'
69     The caller is responsible for freeing the dynamically allocated string that is returned */
70 char *get_ip(const char *if_name) {
71         struct ifaddrs *ifaddr, *ifa;
72         char *ip;
73         int family;
74
75         ip = malloc(NI_MAXHOST);
76         assert(ip);
77         assert(getifaddrs(&ifaddr) != -1);
78
79         for(ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
80                 if(ifa->ifa_addr == NULL) {
81                         continue;
82                 }
83
84                 family = ifa->ifa_addr->sa_family;
85
86                 if(family == AF_INET && !strcmp(ifa->ifa_name, if_name)) {
87                         assert(!getnameinfo(ifa->ifa_addr, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST));
88                         break;
89                 }
90         }
91
92         return ip;
93 }
94
95 /* Return the IP Address of the Interface 'if_name'
96     The caller is responsible for freeing the dynamically allocated string that is returned */
97 char *get_netmask(const char *if_name) {
98         struct ifaddrs *ifaddr, *ifa;
99         char *ip;
100         int family;
101
102         ip = malloc(NI_MAXHOST);
103         assert(ip);
104         assert(getifaddrs(&ifaddr) != -1);
105
106         for(ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
107                 if(ifa->ifa_addr == NULL) {
108                         continue;
109                 }
110
111                 family = ifa->ifa_addr->sa_family;
112
113                 if(family == AF_INET && !strcmp(ifa->ifa_name, if_name)) {
114                         assert(!getnameinfo(ifa->ifa_netmask, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST));
115                         break;
116                 }
117         }
118
119         return ip;
120 }
121
122 /* Change the IP Address of an interface */
123 void set_ip(const char *if_name, const char *new_ip) {
124         char set_ip_cmd[100];
125         assert(snprintf(set_ip_cmd, sizeof(set_ip_cmd), "ifconfig %s %s", if_name, new_ip) >= 0);
126         assert(system(set_ip_cmd) == 0);
127 }
128
129 /* Change the Netmask of an interface */
130 void set_netmask(const char *if_name, const char *new_netmask) {
131         char set_mask_cmd[100];
132         assert(snprintf(set_mask_cmd, sizeof(set_mask_cmd), "ifconfig %s netmask %s", if_name, new_netmask) >= 0);
133         assert(system(set_mask_cmd) == 0);
134 }
135
136 /* Bring a network interface down (before making changes such as the IP Address) */
137 void stop_nw_intf(const char *if_name) {
138         char nw_down_cmd[100];
139         assert(snprintf(nw_down_cmd, sizeof(nw_down_cmd), "ifconfig %s down", if_name) >= 0);
140         assert(system(nw_down_cmd) == 0);
141 }
142
143 /* Bring a network interface up (after bringing it down and making changes such as
144     the IP Address) */
145 void start_nw_intf(const char *if_name) {
146         char nw_up_cmd[100];
147         assert(snprintf(nw_up_cmd, sizeof(nw_up_cmd), "ifconfig %s up", if_name) >= 0);
148         assert(system(nw_up_cmd) == 0);
149 }
150
151 void meshlink_callback_node_status(meshlink_handle_t *mesh, meshlink_node_t *node,
152                                    bool reachable) {
153         (void)mesh;
154         fprintf(stderr, "Node %s became %s\n", node->name, (reachable) ? "reachable" : "unreachable");
155 }
156
157 void meshlink_callback_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
158                               const char *text) {
159         (void)mesh;
160         (void)level;
161
162         fprintf(stderr, "meshlink>> %s\n", text);
163
164         if(state_ptr) {
165                 bool status;
166                 char name[100];
167
168                 if(sscanf(text, "Connection with %s activated", name) == 1) {
169                         status = true;
170                 } else if(sscanf(text, "Already connected to %s", name) == 1) {
171                         status = true;
172                 } else if(sscanf(text, "Connection closed by %s", name) == 1) {
173                         status = false;
174                 } else if(sscanf(text, "Closing connection with %s", name) == 1) {
175                         status = false;
176                 } else {
177                         return;
178                 }
179
180                 int i = meshlink_get_node_in_container(name);
181                 assert(i != -1);
182                 meta_conn_status[i] = status;
183         }
184 }