15 struct sync_flag bar_reachable;
17 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
20 if(reachable && !strcmp(node->name, "bar")) {
21 set_sync_flag(&bar_reachable, true);
26 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
28 // Open two new meshlink instance.
30 assert(meshlink_destroy("import_export_conf.1"));
31 assert(meshlink_destroy("import_export_conf.2"));
33 meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE);
36 meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar", "import-export", DEV_CLASS_BACKBONE);
39 // Disable local discovery
41 meshlink_enable_discovery(mesh1, false);
42 meshlink_enable_discovery(mesh2, false);
44 // Import and export both side's data
46 assert(meshlink_set_canonical_address(mesh1, meshlink_get_self(mesh1), "localhost", NULL));
47 assert(meshlink_set_canonical_address(mesh2, meshlink_get_self(mesh2), "localhost", NULL));
49 char *data = meshlink_export(mesh1);
52 assert(meshlink_import(mesh2, data));
55 data = meshlink_export(mesh2);
58 assert(meshlink_import(mesh1, data));
60 // Check that importing twice is fine
61 assert(meshlink_import(mesh1, data));
64 // Check that importing garbage is not fine
65 assert(!meshlink_import(mesh1, "Garbage\n"));
67 // Check that foo knows bar, but that it is not reachable.
69 time_t last_reachable;
70 time_t last_unreachable;
71 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
73 assert(!meshlink_get_node_reachability(mesh1, bar, &last_reachable, &last_unreachable));
74 assert(!last_reachable);
75 assert(!last_unreachable);
77 // Start both instances
79 meshlink_set_node_status_cb(mesh1, status_cb);
81 assert(meshlink_start(mesh1));
82 assert(meshlink_start(mesh2));
84 // Wait for the two to connect.
86 assert(wait_sync_flag(&bar_reachable, 20));
88 // Wait for UDP communication to become possible.
90 int pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
92 for(int i = 0; i < 10 && !pmtu; i++) {
94 pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
99 // Check that we now have reachability information
101 assert(meshlink_get_node_reachability(mesh1, bar, &last_reachable, &last_unreachable));
102 assert(last_reachable);
103 assert(!last_unreachable);
107 meshlink_stop(mesh1);
108 meshlink_stop(mesh2);
110 // Check that bar is no longer reachable
112 assert(!meshlink_get_node_reachability(mesh1, bar, &last_reachable, &last_unreachable));
113 assert(last_reachable);
114 assert(last_unreachable);
115 assert(last_reachable <= last_unreachable);
119 meshlink_close(mesh2);
120 meshlink_close(mesh1);