10 volatile bool baz_reachable = false;
12 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
13 static struct timeval tv0;
17 gettimeofday(&tv0, NULL);
20 gettimeofday(&tv, NULL);
21 fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
24 fprintf(stderr, "(%s) ", mesh->name);
27 fprintf(stderr, "[%d] %s\n", level, text);
30 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
33 if(!strcmp(node->name, "baz")) {
34 baz_reachable = reachable;
39 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
41 // Open thee new meshlink instance.
43 meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
46 fprintf(stderr, "Could not initialize configuration for foo\n");
50 meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
52 meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
55 fprintf(stderr, "Could not initialize configuration for bar\n");
59 meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
61 meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
64 fprintf(stderr, "Could not initialize configuration for quux\n");
68 meshlink_set_log_cb(mesh3, MESHLINK_DEBUG, log_cb);
70 // Disable local discovery.
72 meshlink_enable_discovery(mesh1, false);
73 meshlink_enable_discovery(mesh2, false);
74 meshlink_enable_discovery(mesh3, false);
76 // Start the first instance and have it generate invitations.
78 meshlink_set_node_status_cb(mesh1, status_cb);
80 if(!meshlink_start(mesh1)) {
81 fprintf(stderr, "Foo could not start\n");
85 meshlink_add_address(mesh1, "localhost");
86 char *baz_url = meshlink_invite(mesh1, NULL, "baz");
89 fprintf(stderr, "Foo could not generate an invitation for baz\n");
93 char *quux_url = meshlink_invite(mesh1, NULL, "quux");
96 fprintf(stderr, "Foo could not generate an invitation for quux\n");
100 fprintf(stderr, "Invitation URL for baz: %s\n", baz_url);
101 fprintf(stderr, "Invitation URL for quux: %s\n", quux_url);
103 // Have the second instance join the first.
105 if(!meshlink_join(mesh2, baz_url)) {
106 fprintf(stderr, "Baz could not join foo's mesh\n");
110 if(!meshlink_start(mesh2)) {
111 fprintf(stderr, "Baz could not start\n");
115 // Wait for the two to connect.
117 for(int i = 0; i < 60; i++) {
126 fprintf(stderr, "Baz not reachable for foo after 20 seconds\n");
130 int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
132 for(int i = 0; i < 10 && !pmtu; i++) {
134 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
138 fprintf(stderr, "UDP communication with baz not possible after 10 seconds\n");
142 // Check that an invitation cannot be used twice
144 if(meshlink_join(mesh3, baz_url)) {
145 fprintf(stderr, "Quux could join foo's mesh using an already used invitation\n");
151 // Check that nodes cannot join with expired invitations
153 meshlink_set_invitation_timeout(mesh1, 0);
155 if(meshlink_join(mesh3, quux_url)) {
156 fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n");
162 // Check that existing nodes cannot join another mesh
164 char *corge_url = meshlink_invite(mesh3, NULL, "corge");
167 fprintf(stderr, "Quux could not generate an invitation for corge\n");
171 fprintf(stderr, "Invitation URL for corge: %s\n", corge_url);
173 if(!meshlink_start(mesh3)) {
174 fprintf(stderr, "Quux could not start\n");
178 meshlink_stop(mesh2);
180 if(meshlink_join(mesh2, corge_url)) {
181 fprintf(stderr, "Bar could join twice\n");
187 // Check that invitations work correctly after changing ports
189 meshlink_set_invitation_timeout(mesh1, 86400);
190 meshlink_stop(mesh1);
191 meshlink_stop(mesh3);
193 int oldport = meshlink_get_port(mesh1);
196 for(int i = 0; i < 100; i++) {
197 success = meshlink_set_port(mesh1, 0x9000 + rand() % 0x1000);
201 int newport = meshlink_get_port(mesh1);
202 assert(oldport != newport);
204 assert(meshlink_start(mesh1));
205 quux_url = meshlink_invite(mesh1, NULL, "quux");
206 fprintf(stderr, "Invitation URL for quux: %s\n", quux_url);
208 // The old port should not be in the invitation URL
211 snprintf(portstr, sizeof(portstr), ":%d", oldport);
212 assert(!strstr(quux_url, portstr));
214 // The new port should be in the invitation URL
216 snprintf(portstr, sizeof(portstr), ":%d", newport);
217 assert(strstr(quux_url, portstr));
219 // The invitation should work
221 assert(meshlink_join(mesh3, quux_url));
225 meshlink_close(mesh3);
226 meshlink_close(mesh2);
227 meshlink_close(mesh1);