]> git.meshlink.io Git - meshlink/blob - test/import-export.c
Fix compiler warnings in the test suites.
[meshlink] / test / import-export.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/time.h>
6
7 #include "meshlink.h"
8
9 volatile bool bar_reachable = false;
10
11 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
12         static struct timeval tv0;
13         struct timeval tv;
14
15         if(tv0.tv_sec == 0) {
16                 gettimeofday(&tv0, NULL);
17         }
18
19         gettimeofday(&tv, NULL);
20         fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
21
22         if(mesh) {
23                 fprintf(stderr, "(%s) ", mesh->name);
24         }
25
26         fprintf(stderr, "[%d] %s\n", level, text);
27 }
28
29 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
30         (void)mesh;
31
32         if(!strcmp(node->name, "bar")) {
33                 bar_reachable = reachable;
34         }
35 }
36
37 int main() {
38         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
39
40         // Open two new meshlink instance.
41
42         meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE);
43
44         if(!mesh1) {
45                 fprintf(stderr, "Could not initialize configuration for foo: %s\n", meshlink_strerror(meshlink_errno));
46                 return 1;
47         }
48
49         meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar", "import-export", DEV_CLASS_BACKBONE);
50
51         if(!mesh2) {
52                 fprintf(stderr, "Could not initialize configuration for bar\n");
53                 return 1;
54         }
55
56         meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
57         meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
58
59         // Disable local discovery
60
61         meshlink_enable_discovery(mesh1, false);
62         meshlink_enable_discovery(mesh2, false);
63
64         // Import and export both side's data
65
66         meshlink_add_address(mesh1, "localhost");
67         meshlink_add_address(mesh2, "localhost");
68
69         char *data = meshlink_export(mesh1);
70
71         if(!data) {
72                 fprintf(stderr, "Foo could not export its configuration\n");
73                 return 1;
74         }
75
76         fprintf(stderr, "Foo export data:\n%s\n", data);
77
78         if(!meshlink_import(mesh2, data)) {
79                 fprintf(stderr, "Bar could not import foo's configuration\n");
80                 return 1;
81         }
82
83         free(data);
84
85         data = meshlink_export(mesh2);
86
87         if(!data) {
88                 fprintf(stderr, "Bar could not export its configuration\n");
89                 return 1;
90         }
91
92
93         if(!meshlink_import(mesh1, data)) {
94                 fprintf(stderr, "Foo could not import bar's configuration\n");
95                 return 1;
96         }
97
98         free(data);
99
100         // Start both instances
101
102         meshlink_set_node_status_cb(mesh1, status_cb);
103
104         if(!meshlink_start(mesh1)) {
105                 fprintf(stderr, "Foo could not start\n");
106                 return 1;
107         }
108
109         if(!meshlink_start(mesh2)) {
110                 fprintf(stderr, "Bar could not start\n");
111                 return 1;
112         }
113
114         // Wait for the two to connect.
115
116         for(int i = 0; i < 20; i++) {
117                 sleep(1);
118
119                 if(bar_reachable) {
120                         break;
121                 }
122         }
123
124         if(!bar_reachable) {
125                 fprintf(stderr, "Bar not reachable for foo after 20 seconds\n");
126                 return 1;
127         }
128
129         int pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
130
131         for(int i = 0; i < 10 && !pmtu; i++) {
132                 sleep(1);
133                 pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
134         }
135
136         if(!pmtu) {
137                 fprintf(stderr, "UDP communication with bar not possible after 10 seconds\n");
138                 return 1;
139         }
140
141         // Clean up.
142
143         meshlink_stop(mesh2);
144         meshlink_stop(mesh1);
145         meshlink_close(mesh2);
146         meshlink_close(mesh1);
147
148         return 0;
149 }