8 volatile bool baz_reachable = false;
10 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
11 static struct timeval tv0;
15 gettimeofday(&tv0, NULL);
18 gettimeofday(&tv, NULL);
19 fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
22 fprintf(stderr, "(%s) ", mesh->name);
25 fprintf(stderr, "[%d] %s\n", level, text);
28 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
31 if(!strcmp(node->name, "baz")) {
32 baz_reachable = reachable;
37 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
39 // Open thee new meshlink instance.
41 meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
44 fprintf(stderr, "Could not initialize configuration for foo\n");
48 meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
50 meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
53 fprintf(stderr, "Could not initialize configuration for bar\n");
57 meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
59 meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
62 fprintf(stderr, "Could not initialize configuration for quux\n");
66 meshlink_set_log_cb(mesh3, MESHLINK_DEBUG, log_cb);
68 // Disable local discovery.
70 meshlink_enable_discovery(mesh1, false);
71 meshlink_enable_discovery(mesh2, false);
72 meshlink_enable_discovery(mesh3, false);
74 // Start the first instance and have it generate invitations.
76 meshlink_set_node_status_cb(mesh1, status_cb);
78 if(!meshlink_start(mesh1)) {
79 fprintf(stderr, "Foo could not start\n");
83 meshlink_add_address(mesh1, "localhost");
84 char *baz_url = meshlink_invite(mesh1, NULL, "baz");
87 fprintf(stderr, "Foo could not generate an invitation for baz\n");
91 char *quux_url = meshlink_invite(mesh1, NULL, "quux");
94 fprintf(stderr, "Foo could not generate an invitation for quux\n");
98 fprintf(stderr, "Invitation URL for baz: %s\n", baz_url);
99 fprintf(stderr, "Invitation URL for quux: %s\n", quux_url);
101 // Have the second instance join the first.
103 if(!meshlink_join(mesh2, baz_url)) {
104 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 nodes cannot join with expired invitations
144 meshlink_set_invitation_timeout(mesh1, 0);
146 if(meshlink_join(mesh3, quux_url)) {
147 fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n");
155 meshlink_close(mesh3);
156 meshlink_close(mesh2);
157 meshlink_close(mesh1);