15 static struct sync_flag bar_reachable;
16 static struct sync_flag pmtu_flag;
18 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
21 if(reachable && !strcmp(node->name, "bar")) {
22 set_sync_flag(&bar_reachable, true);
26 static void pmtu_cb(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t pmtu) {
29 if(pmtu && !strcmp(node->name, "bar")) {
30 set_sync_flag(&pmtu_flag, true);
35 init_sync_flag(&bar_reachable);
36 init_sync_flag(&pmtu_flag);
38 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
40 // Open two new meshlink instance.
42 assert(meshlink_destroy("import_export_conf.1"));
43 assert(meshlink_destroy("import_export_conf.2"));
45 meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE);
48 meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar", "import-export", DEV_CLASS_BACKBONE);
51 // Disable local discovery
53 meshlink_enable_discovery(mesh1, false);
54 meshlink_enable_discovery(mesh2, false);
56 // Import and export both side's data
58 assert(meshlink_set_canonical_address(mesh1, meshlink_get_self(mesh1), "localhost", NULL));
59 assert(meshlink_set_canonical_address(mesh2, meshlink_get_self(mesh2), "localhost", NULL));
61 char *data = meshlink_export(mesh1);
64 assert(meshlink_import(mesh2, data));
67 data = meshlink_export(mesh2);
70 assert(meshlink_import(mesh1, data));
72 // Check that importing twice is fine
73 assert(meshlink_import(mesh1, data));
76 // Check that importing garbage is not fine
77 assert(!meshlink_import(mesh1, "Garbage\n"));
79 // Check that foo knows bar, but that it is not reachable.
81 time_t last_reachable;
82 time_t last_unreachable;
83 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
85 assert(!meshlink_get_node_reachability(mesh1, bar, &last_reachable, &last_unreachable));
86 assert(!last_reachable);
87 assert(!last_unreachable);
89 // Start both instances
91 meshlink_set_node_status_cb(mesh1, status_cb);
92 meshlink_set_node_pmtu_cb(mesh1, pmtu_cb);
94 assert(meshlink_start(mesh1));
95 assert(meshlink_start(mesh2));
97 // Wait for the two to connect.
99 assert(wait_sync_flag(&bar_reachable, 10));
101 // Wait for UDP communication to become possible.
103 assert(wait_sync_flag(&pmtu_flag, 10));
105 // Check that we now have reachability information
107 assert(meshlink_get_node_reachability(mesh1, bar, &last_reachable, &last_unreachable));
108 assert(last_reachable);
112 meshlink_stop(mesh1);
113 meshlink_stop(mesh2);
115 // Check that bar is no longer reachable
117 assert(!meshlink_get_node_reachability(mesh1, bar, &last_reachable, &last_unreachable));
118 assert(last_reachable);
119 assert(last_unreachable);
120 assert(last_reachable <= last_unreachable);
124 meshlink_close(mesh2);
125 meshlink_close(mesh1);