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");
109 if(!meshlink_start(mesh2)) {
110 fprintf(stderr, "Baz could not start\n");
114 // Wait for the two to connect.
116 for(int i = 0; i < 60; i++) {
125 fprintf(stderr, "Baz not reachable for foo after 20 seconds\n");
129 int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
131 for(int i = 0; i < 10 && !pmtu; i++) {
133 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
137 fprintf(stderr, "UDP communication with baz not possible after 10 seconds\n");
141 // Check that an invitation cannot be used twice
143 if(meshlink_join(mesh3, baz_url)) {
144 fprintf(stderr, "Quux could join foo's mesh using an already used invitation\n");
150 // Check that nodes cannot join with expired invitations
152 meshlink_set_invitation_timeout(mesh1, 0);
154 if(meshlink_join(mesh3, quux_url)) {
155 fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n");
161 // Check that existing nodes cannot join another mesh
163 char *corge_url = meshlink_invite(mesh3, NULL, "corge");
166 fprintf(stderr, "Quux could not generate an invitation for corge\n");
170 fprintf(stderr, "Invitation URL for corge: %s\n", corge_url);
172 if(!meshlink_start(mesh3)) {
173 fprintf(stderr, "Quux could not start\n");
177 meshlink_stop(mesh2);
179 if(meshlink_join(mesh2, corge_url)) {
180 fprintf(stderr, "Bar could join twice\n");
188 meshlink_close(mesh3);
189 meshlink_close(mesh2);
190 meshlink_close(mesh1);