11 static struct sync_flag baz_reachable;
13 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
16 if(reachable && !strcmp(node->name, "baz")) {
17 set_sync_flag(&baz_reachable, true);
22 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
24 assert(meshlink_destroy("invite_join_conf.1"));
25 assert(meshlink_destroy("invite_join_conf.2"));
26 assert(meshlink_destroy("invite_join_conf.3"));
28 // Open thee new meshlink instance.
30 meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
33 meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
36 meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
39 // Disable local discovery.
41 meshlink_enable_discovery(mesh1, false);
42 meshlink_enable_discovery(mesh2, false);
43 meshlink_enable_discovery(mesh3, false);
45 // Have the first instance generate invitations.
47 meshlink_set_node_status_cb(mesh1, status_cb);
49 meshlink_add_address(mesh1, "localhost");
50 char *baz_url = meshlink_invite(mesh1, NULL, "baz");
53 char *quux_url = meshlink_invite(mesh1, NULL, "quux");
56 // Have the second instance join the first.
58 assert(meshlink_start(mesh1));
60 assert(meshlink_join(mesh2, baz_url));
61 assert(meshlink_start(mesh2));
63 // Wait for the two to connect.
65 assert(wait_sync_flag(&baz_reachable, 20));
67 // Wait for UDP communication to become possible.
69 int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
71 for(int i = 0; i < 10 && !pmtu; i++) {
73 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
78 // Check that an invitation cannot be used twice
80 assert(!meshlink_join(mesh3, baz_url));
83 // Check that nodes cannot join with expired invitations
85 meshlink_set_invitation_timeout(mesh1, 0);
87 assert(!meshlink_join(mesh3, quux_url));
90 // Check that existing nodes cannot join another mesh
92 char *corge_url = meshlink_invite(mesh3, NULL, "corge");
95 assert(meshlink_start(mesh3));
99 assert(!meshlink_join(mesh2, corge_url));
102 // Check that invitations work correctly after changing ports
104 meshlink_set_invitation_timeout(mesh1, 86400);
105 meshlink_stop(mesh1);
106 meshlink_stop(mesh3);
108 int oldport = meshlink_get_port(mesh1);
109 bool success = false;
111 for(int i = 0; !success && i < 100; i++) {
112 success = meshlink_set_port(mesh1, 0x9000 + rand() % 0x1000);
116 int newport = meshlink_get_port(mesh1);
117 assert(oldport != newport);
119 assert(meshlink_start(mesh1));
120 quux_url = meshlink_invite(mesh1, NULL, "quux");
123 // The old port should not be in the invitation URL
126 snprintf(portstr, sizeof(portstr), ":%d", oldport);
127 assert(!strstr(quux_url, portstr));
129 // The new port should be in the invitation URL
131 snprintf(portstr, sizeof(portstr), ":%d", newport);
132 assert(strstr(quux_url, portstr));
134 // The invitation should work
136 assert(meshlink_join(mesh3, quux_url));
141 meshlink_close(mesh3);
142 meshlink_close(mesh2);
143 meshlink_close(mesh1);