]> git.meshlink.io Git - meshlink/blob - test/invite-join.c
Allow Catta to be disabled.
[meshlink] / test / invite-join.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "meshlink.h"
7
8 volatile bool baz_reachable = false;
9
10 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
11         if(!strcmp(node->name, "baz"))
12                 baz_reachable = reachable;
13 }
14
15 int main(int argc, char *argv[]) {
16         // Open two new meshlink instance.
17
18         meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
19         if(!mesh1) {
20                 fprintf(stderr, "Could not initialize configuration for foo\n");
21                 return 1;
22         }
23
24         meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
25         if(!mesh2) {
26                 fprintf(stderr, "Could not initialize configuration for bar\n");
27                 return 1;
28         }
29
30         // Disable local discovery.
31
32         meshlink_enable_discovery(mesh1, false);
33         meshlink_enable_discovery(mesh2, false);
34
35         // Start the first instance and have it generate an invitation.
36
37         meshlink_set_node_status_cb(mesh1, status_cb);
38
39         if(!meshlink_start(mesh1)) {
40                 fprintf(stderr, "Foo could not start\n");
41                 return 1;
42         }
43
44         meshlink_add_address(mesh1, "localhost");
45         char *url = meshlink_invite(mesh1, "baz");
46         if(!url) {
47                 fprintf(stderr, "Foo could not generate an invitation for baz\n");
48                 return 1;
49         }
50
51         // Have the second instance join the first.
52
53         if(!meshlink_join(mesh2, url)) {
54                 fprintf(stderr, "Baz could not join foo's mesh\n");
55                 return 1;
56         }
57
58         free(url);
59
60         if(!meshlink_start(mesh2)) {
61                 fprintf(stderr, "Baz could not start\n");
62                 return 1;
63         }
64
65         // Wait for the two to connect.
66
67         for(int i = 0; i < 60; i++) {
68                 sleep(1);
69                 if(baz_reachable)
70                         break;
71         }
72
73         if(!baz_reachable) {
74                 fprintf(stderr, "Baz not reachable for foo after 20 seconds\n");
75                 return 1;
76         }
77
78         int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
79         for(int i = 0; i < 10 && !pmtu; i++) {
80                 sleep(1);
81                 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
82         }
83
84         if(!pmtu) {
85                 fprintf(stderr, "UDP communication with baz not possible after 10 seconds\n");
86                 return 1;
87         }
88
89         // Clean up.
90
91         meshlink_stop(mesh2);
92         meshlink_stop(mesh1);
93         meshlink_close(mesh2);
94         meshlink_close(mesh1);
95
96         return 0;
97 }