9 volatile bool bar_reachable = false;
11 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
12 static struct timeval tv0;
16 gettimeofday(&tv0, NULL);
19 gettimeofday(&tv, NULL);
20 fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
23 fprintf(stderr, "(%s) ", mesh->name);
26 fprintf(stderr, "[%d] %s\n", level, text);
29 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
32 if(!strcmp(node->name, "bar")) {
33 bar_reachable = reachable;
38 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
40 // Open two new meshlink instance.
42 meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE);
45 fprintf(stderr, "Could not initialize configuration for foo: %s\n", meshlink_strerror(meshlink_errno));
49 meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar", "import-export", DEV_CLASS_BACKBONE);
52 fprintf(stderr, "Could not initialize configuration for bar\n");
56 meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
57 meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
59 // Disable local discovery
61 meshlink_enable_discovery(mesh1, false);
62 meshlink_enable_discovery(mesh2, false);
64 // Import and export both side's data
66 meshlink_add_address(mesh1, "localhost");
67 meshlink_add_address(mesh2, "localhost");
69 char *data = meshlink_export(mesh1);
72 fprintf(stderr, "Foo could not export its configuration\n");
76 fprintf(stderr, "Foo export data:\n%s\n", data);
78 if(!meshlink_import(mesh2, data)) {
79 fprintf(stderr, "Bar could not import foo's configuration\n");
85 data = meshlink_export(mesh2);
88 fprintf(stderr, "Bar could not export its configuration\n");
93 if(!meshlink_import(mesh1, data)) {
94 fprintf(stderr, "Foo could not import bar's configuration\n");
100 // Start both instances
102 meshlink_set_node_status_cb(mesh1, status_cb);
104 if(!meshlink_start(mesh1)) {
105 fprintf(stderr, "Foo could not start\n");
109 if(!meshlink_start(mesh2)) {
110 fprintf(stderr, "Bar could not start\n");
114 // Wait for the two to connect.
116 for(int i = 0; i < 20; i++) {
125 fprintf(stderr, "Bar not reachable for foo after 20 seconds\n");
129 int pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
131 for(int i = 0; i < 10 && !pmtu; i++) {
133 pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
137 fprintf(stderr, "UDP communication with bar not possible after 10 seconds\n");
143 meshlink_stop(mesh2);
144 meshlink_stop(mesh1);
145 meshlink_close(mesh2);
146 meshlink_close(mesh1);