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_set_canonical_address(mesh1, meshlink_get_self(mesh1), "localhost", NULL));
55 char *baz_url = meshlink_invite(mesh1, NULL, "baz");
58 char *quux_url = meshlink_invite(mesh1, NULL, "quux");
61 // Have the second instance join the first.
63 assert(meshlink_start(mesh1));
65 assert(meshlink_join(mesh2, baz_url));
66 assert(meshlink_start(mesh2));
68 // Wait for the two to connect.
70 assert(wait_sync_flag(&baz_reachable, 20));
72 // Wait for UDP communication to become possible.
74 int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
76 for(int i = 0; i < 10 && !pmtu; i++) {
78 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
83 // Check that an invitation cannot be used twice
85 assert(!meshlink_join(mesh3, baz_url));
88 // Check that nodes cannot join with expired invitations
90 meshlink_set_invitation_timeout(mesh1, 0);
92 assert(!meshlink_join(mesh3, quux_url));
95 // Check that existing nodes cannot join another mesh
97 char *corge_url = meshlink_invite(mesh3, NULL, "corge");
100 assert(meshlink_start(mesh3));
102 meshlink_stop(mesh2);
104 assert(!meshlink_join(mesh2, corge_url));
107 // Check that invitations work correctly after changing ports
109 meshlink_set_invitation_timeout(mesh1, 86400);
110 meshlink_stop(mesh1);
111 meshlink_stop(mesh3);
113 int oldport = meshlink_get_port(mesh1);
114 bool success = false;
116 for(int i = 0; !success && i < 100; i++) {
117 success = meshlink_set_port(mesh1, 0x9000 + rand() % 0x1000);
121 int newport = meshlink_get_port(mesh1);
122 assert(oldport != newport);
124 assert(meshlink_start(mesh1));
125 quux_url = meshlink_invite(mesh1, NULL, "quux");
128 // The old port should not be in the invitation URL
131 snprintf(portstr, sizeof(portstr), ":%d", oldport);
132 assert(!strstr(quux_url, portstr));
134 // The new port should be in the invitation URL
136 snprintf(portstr, sizeof(portstr), ":%d", newport);
137 assert(strstr(quux_url, portstr));
139 // The invitation should work
141 assert(meshlink_join(mesh3, quux_url));
144 // Check that adding duplicate addresses get removed correctly
146 assert(meshlink_add_invitation_address(mesh1, "localhost", portstr + 1));
147 corge_url = meshlink_invite(mesh1, NULL, "corge");
149 char *localhost = strstr(corge_url, "localhost");
151 assert(!strstr(localhost + 1, "localhost"));
154 // Check that resetting and adding multiple, different invitation address works
156 meshlink_clear_invitation_addresses(mesh1);
157 assert(meshlink_add_invitation_address(mesh1, "1.invalid.", NULL));
158 assert(meshlink_add_invitation_address(mesh1, "2.invalid.", NULL));
159 assert(meshlink_add_invitation_address(mesh1, "3.invalid.", NULL));
160 assert(meshlink_add_invitation_address(mesh1, "4.invalid.", NULL));
161 assert(meshlink_add_invitation_address(mesh1, "5.invalid.", NULL));
162 char *grault_url = meshlink_invite(mesh1, NULL, "grault");
164 fprintf(stderr, "%s\n", grault_url);
165 localhost = strstr(grault_url, "localhost");
167 char *invalid1 = strstr(grault_url, "1.invalid.");
169 char *invalid5 = strstr(grault_url, "5.invalid.");
172 // Check that explicitly added invitation addresses come before others, in the order they were specified.
174 assert(invalid1 < invalid5);
175 assert(invalid5 < localhost);
180 meshlink_close(mesh3);
181 meshlink_close(mesh2);
182 meshlink_close(mesh1);