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