]> git.meshlink.io Git - meshlink/blob - test/invite-join.c
Add meshlink_set_invitation_timeout().
[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         (void)mesh;
12
13         if(!strcmp(node->name, "baz")) {
14                 baz_reachable = reachable;
15         }
16 }
17
18 int main() {
19         // Open two new meshlink instance.
20
21         meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
22
23         if(!mesh1) {
24                 fprintf(stderr, "Could not initialize configuration for foo\n");
25                 return 1;
26         }
27
28         meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
29
30         if(!mesh2) {
31                 fprintf(stderr, "Could not initialize configuration for bar\n");
32                 return 1;
33         }
34
35         meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
36
37         if(!mesh3) {
38                 fprintf(stderr, "Could not initialize configuration for quux\n");
39                 return 1;
40         }
41
42         // Disable local discovery.
43
44         meshlink_enable_discovery(mesh1, false);
45         meshlink_enable_discovery(mesh2, false);
46         meshlink_enable_discovery(mesh3, false);
47
48         // Start the first instance and have it generate invitations.
49
50         meshlink_set_node_status_cb(mesh1, status_cb);
51
52         if(!meshlink_start(mesh1)) {
53                 fprintf(stderr, "Foo could not start\n");
54                 return 1;
55         }
56
57         meshlink_add_address(mesh1, "localhost");
58         char *baz_url = meshlink_invite(mesh1, "baz");
59
60         if(!baz_url) {
61                 fprintf(stderr, "Foo could not generate an invitation for baz\n");
62                 return 1;
63         }
64
65         char *quux_url = meshlink_invite(mesh1, "quux");
66
67         if(!quux_url) {
68                 fprintf(stderr, "Foo could not generate an invitation for quux\n");
69                 return 1;
70         }
71
72         // Have the second instance join the first.
73
74         if(!meshlink_join(mesh2, baz_url)) {
75                 fprintf(stderr, "Baz could not join foo's mesh\n");
76                 return 1;
77         }
78
79         free(baz_url);
80
81         if(!meshlink_start(mesh2)) {
82                 fprintf(stderr, "Baz could not start\n");
83                 return 1;
84         }
85
86         // Wait for the two to connect.
87
88         for(int i = 0; i < 60; i++) {
89                 sleep(1);
90
91                 if(baz_reachable) {
92                         break;
93                 }
94         }
95
96         if(!baz_reachable) {
97                 fprintf(stderr, "Baz not reachable for foo after 20 seconds\n");
98                 return 1;
99         }
100
101         int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
102
103         for(int i = 0; i < 10 && !pmtu; i++) {
104                 sleep(1);
105                 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
106         }
107
108         if(!pmtu) {
109                 fprintf(stderr, "UDP communication with baz not possible after 10 seconds\n");
110                 return 1;
111         }
112
113         // Check that nodes cannot join with expired invitations
114
115         meshlink_set_invitation_timeout(mesh1, 0);
116
117         if(meshlink_join(mesh3, quux_url)) {
118                 fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n");
119                 return 1;
120         }
121
122         free(quux_url);
123
124         // Clean up.
125
126         meshlink_stop(mesh2);
127         meshlink_stop(mesh1);
128         meshlink_close(mesh2);
129         meshlink_close(mesh1);
130
131         return 0;
132 }