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