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