9 volatile bool baz_reachable = false;
11 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
12 static struct timeval tv0;
16 gettimeofday(&tv0, NULL);
19 gettimeofday(&tv, NULL);
20 fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
23 fprintf(stderr, "(%s) ", mesh->name);
26 fprintf(stderr, "[%d] %s\n", level, text);
29 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
32 if(!strcmp(node->name, "baz")) {
33 baz_reachable = reachable;
38 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
40 // Open thee new meshlink instance.
42 meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
45 fprintf(stderr, "Could not initialize configuration for foo\n");
49 meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
51 meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
54 fprintf(stderr, "Could not initialize configuration for bar\n");
58 meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
60 meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
63 fprintf(stderr, "Could not initialize configuration for quux\n");
67 meshlink_set_log_cb(mesh3, MESHLINK_DEBUG, log_cb);
69 // Disable local discovery.
71 meshlink_enable_discovery(mesh1, false);
72 meshlink_enable_discovery(mesh2, false);
73 meshlink_enable_discovery(mesh3, false);
75 // Start the first instance and have it generate invitations.
77 meshlink_set_node_status_cb(mesh1, status_cb);
79 if(!meshlink_start(mesh1)) {
80 fprintf(stderr, "Foo could not start\n");
84 meshlink_add_address(mesh1, "localhost");
85 char *baz_url = meshlink_invite(mesh1, NULL, "baz");
88 fprintf(stderr, "Foo could not generate an invitation for baz\n");
92 char *quux_url = meshlink_invite(mesh1, NULL, "quux");
95 fprintf(stderr, "Foo could not generate an invitation for quux\n");
99 fprintf(stderr, "Invitation URL for baz: %s\n", baz_url);
100 fprintf(stderr, "Invitation URL for quux: %s\n", quux_url);
102 // Have the second instance join the first.
104 if(!meshlink_join(mesh2, baz_url)) {
105 fprintf(stderr, "Baz could not join foo's mesh\n");
111 if(!meshlink_start(mesh2)) {
112 fprintf(stderr, "Baz could not start\n");
116 // Wait for the two to connect.
118 for(int i = 0; i < 60; i++) {
127 fprintf(stderr, "Baz not reachable for foo after 20 seconds\n");
131 int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
133 for(int i = 0; i < 10 && !pmtu; i++) {
135 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
139 fprintf(stderr, "UDP communication with baz not possible after 10 seconds\n");
143 // Check that nodes cannot join with expired invitations
145 meshlink_set_invitation_timeout(mesh1, 0);
147 if(meshlink_join(mesh3, quux_url)) {
148 fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n");
156 meshlink_close(mesh3);
157 meshlink_close(mesh2);
158 meshlink_close(mesh1);