]> git.meshlink.io Git - meshlink/blob - test/discovery.c
Never automatically try to bind to ports >= 32768.
[meshlink] / test / discovery.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <stdlib.h>
6 #include <assert.h>
7
8 #include "utils.h"
9 #include "../src/meshlink.h"
10
11 int main(void) {
12         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
13
14         // Open two new meshlink instance.
15
16         meshlink_handle_t *mesh_a, *mesh_b;
17         open_meshlink_pair(&mesh_a, &mesh_b, "discovery");
18
19         // Forget the canoncial address
20
21         assert(meshlink_clear_canonical_address(mesh_a, meshlink_get_node(mesh_a, "b")));
22         assert(meshlink_clear_canonical_address(mesh_b, meshlink_get_node(mesh_b, "a")));
23
24         int port_a = meshlink_get_port(mesh_a);
25         int port_b = meshlink_get_port(mesh_b);
26
27         // Swap and change ports
28
29         port_a++;
30         port_b++;
31
32         meshlink_close(mesh_a);
33         assert(meshlink_set_port(mesh_b, port_a));
34         meshlink_close(mesh_b);
35         mesh_a = meshlink_open("discovery_conf.1", "a", "discovery", DEV_CLASS_BACKBONE);
36         assert(mesh_a);
37         assert(meshlink_set_port(mesh_a, port_b));
38         mesh_b = meshlink_open("discovery_conf.2", "b", "discovery", DEV_CLASS_BACKBONE);
39         assert(mesh_b);
40
41         assert(meshlink_get_port(mesh_a) == port_b);
42         assert(meshlink_get_port(mesh_b) == port_a);
43
44         // Verify that the nodes can find each other
45
46         meshlink_enable_discovery(mesh_a, true);
47         meshlink_enable_discovery(mesh_b, true);
48
49         start_meshlink_pair(mesh_a, mesh_b);
50
51         // Clean up.
52
53         close_meshlink_pair(mesh_a, mesh_b);
54 }