15 static struct sync_flag baz_reachable;
17 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
20 if(reachable && !strcmp(node->name, "baz")) {
21 set_sync_flag(&baz_reachable, true);
26 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
28 assert(meshlink_destroy("invite_join_conf.1"));
29 assert(meshlink_destroy("invite_join_conf.2"));
30 assert(meshlink_destroy("invite_join_conf.3"));
32 // Open thee new meshlink instance.
34 meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
37 meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
40 meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
43 // Disable local discovery.
45 meshlink_enable_discovery(mesh1, false);
46 meshlink_enable_discovery(mesh2, false);
47 meshlink_enable_discovery(mesh3, false);
49 // Have the first instance generate invitations.
51 meshlink_set_node_status_cb(mesh1, status_cb);
53 assert(meshlink_add_address(mesh1, "localhost"));
54 char *baz_url = meshlink_invite(mesh1, NULL, "baz");
57 char *quux_url = meshlink_invite(mesh1, NULL, "quux");
60 // Have the second instance join the first.
62 assert(meshlink_start(mesh1));
64 assert(meshlink_join(mesh2, baz_url));
65 assert(meshlink_start(mesh2));
67 // Wait for the two to connect.
69 assert(wait_sync_flag(&baz_reachable, 20));
71 // Wait for UDP communication to become possible.
73 int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
75 for(int i = 0; i < 10 && !pmtu; i++) {
77 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
82 // Check that an invitation cannot be used twice
84 assert(!meshlink_join(mesh3, baz_url));
87 // Check that nodes cannot join with expired invitations
89 meshlink_set_invitation_timeout(mesh1, 0);
91 assert(!meshlink_join(mesh3, quux_url));
94 // Check that existing nodes cannot join another mesh
96 char *corge_url = meshlink_invite(mesh3, NULL, "corge");
99 assert(meshlink_start(mesh3));
101 meshlink_stop(mesh2);
103 assert(!meshlink_join(mesh2, corge_url));
106 // Check that invitations work correctly after changing ports
108 meshlink_set_invitation_timeout(mesh1, 86400);
109 meshlink_stop(mesh1);
110 meshlink_stop(mesh3);
112 int oldport = meshlink_get_port(mesh1);
113 bool success = false;
115 for(int i = 0; !success && i < 100; i++) {
116 success = meshlink_set_port(mesh1, 0x9000 + rand() % 0x1000);
120 int newport = meshlink_get_port(mesh1);
121 assert(oldport != newport);
123 assert(meshlink_start(mesh1));
124 quux_url = meshlink_invite(mesh1, NULL, "quux");
127 // The old port should not be in the invitation URL
130 snprintf(portstr, sizeof(portstr), ":%d", oldport);
131 assert(!strstr(quux_url, portstr));
133 // The new port should be in the invitation URL
135 snprintf(portstr, sizeof(portstr), ":%d", newport);
136 assert(strstr(quux_url, portstr));
138 // The invitation should work
140 assert(meshlink_join(mesh3, quux_url));
145 meshlink_close(mesh3);
146 meshlink_close(mesh2);
147 meshlink_close(mesh1);