]> git.meshlink.io Git - meshlink/blob - test/invite-join.c
Prevent meshlink_join() when the joining node is already part of a mesh.
[meshlink] / test / invite-join.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/time.h>
6
7 #include "meshlink.h"
8
9 volatile bool baz_reachable = false;
10
11 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
12         static struct timeval tv0;
13         struct timeval tv;
14
15         if(tv0.tv_sec == 0) {
16                 gettimeofday(&tv0, NULL);
17         }
18
19         gettimeofday(&tv, NULL);
20         fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
21
22         if(mesh) {
23                 fprintf(stderr, "(%s) ", mesh->name);
24         }
25
26         fprintf(stderr, "[%d] %s\n", level, text);
27 }
28
29 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
30         (void)mesh;
31
32         if(!strcmp(node->name, "baz")) {
33                 baz_reachable = reachable;
34         }
35 }
36
37 int main() {
38         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
39
40         // Open thee new meshlink instance.
41
42         meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
43
44         if(!mesh1) {
45                 fprintf(stderr, "Could not initialize configuration for foo\n");
46                 return 1;
47         }
48
49         meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
50
51         meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
52
53         if(!mesh2) {
54                 fprintf(stderr, "Could not initialize configuration for bar\n");
55                 return 1;
56         }
57
58         meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
59
60         meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
61
62         if(!mesh3) {
63                 fprintf(stderr, "Could not initialize configuration for quux\n");
64                 return 1;
65         }
66
67         meshlink_set_log_cb(mesh3, MESHLINK_DEBUG, log_cb);
68
69         // Disable local discovery.
70
71         meshlink_enable_discovery(mesh1, false);
72         meshlink_enable_discovery(mesh2, false);
73         meshlink_enable_discovery(mesh3, false);
74
75         // Start the first instance and have it generate invitations.
76
77         meshlink_set_node_status_cb(mesh1, status_cb);
78
79         if(!meshlink_start(mesh1)) {
80                 fprintf(stderr, "Foo could not start\n");
81                 return 1;
82         }
83
84         meshlink_add_address(mesh1, "localhost");
85         char *baz_url = meshlink_invite(mesh1, NULL, "baz");
86
87         if(!baz_url) {
88                 fprintf(stderr, "Foo could not generate an invitation for baz\n");
89                 return 1;
90         }
91
92         char *quux_url = meshlink_invite(mesh1, NULL, "quux");
93
94         if(!quux_url) {
95                 fprintf(stderr, "Foo could not generate an invitation for quux\n");
96                 return 1;
97         }
98
99         fprintf(stderr, "Invitation URL for baz:  %s\n", baz_url);
100         fprintf(stderr, "Invitation URL for quux: %s\n", quux_url);
101
102         // Have the second instance join the first.
103
104         if(!meshlink_join(mesh2, baz_url)) {
105                 fprintf(stderr, "Baz could not join foo's mesh\n");
106                 return 1;
107         }
108
109         if(!meshlink_start(mesh2)) {
110                 fprintf(stderr, "Baz could not start\n");
111                 return 1;
112         }
113
114         // Wait for the two to connect.
115
116         for(int i = 0; i < 60; i++) {
117                 sleep(1);
118
119                 if(baz_reachable) {
120                         break;
121                 }
122         }
123
124         if(!baz_reachable) {
125                 fprintf(stderr, "Baz not reachable for foo after 20 seconds\n");
126                 return 1;
127         }
128
129         int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
130
131         for(int i = 0; i < 10 && !pmtu; i++) {
132                 sleep(1);
133                 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
134         }
135
136         if(!pmtu) {
137                 fprintf(stderr, "UDP communication with baz not possible after 10 seconds\n");
138                 return 1;
139         }
140
141         // Check that an invitation cannot be used twice
142
143         if(meshlink_join(mesh3, baz_url)) {
144                 fprintf(stderr, "Quux could join foo's mesh using an already used invitation\n");
145                 return 1;
146         }
147
148         free(baz_url);
149
150         // Check that nodes cannot join with expired invitations
151
152         meshlink_set_invitation_timeout(mesh1, 0);
153
154         if(meshlink_join(mesh3, quux_url)) {
155                 fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n");
156                 return 1;
157         }
158
159         free(quux_url);
160
161         // Check that existing nodes cannot join another mesh
162
163         char *corge_url = meshlink_invite(mesh3, NULL, "corge");
164
165         if(!corge_url) {
166                 fprintf(stderr, "Quux could not generate an invitation for corge\n");
167                 return 1;
168         }
169
170         fprintf(stderr, "Invitation URL for corge: %s\n", corge_url);
171
172         if(!meshlink_start(mesh3)) {
173                 fprintf(stderr, "Quux could not start\n");
174                 return 1;
175         }
176
177         meshlink_stop(mesh2);
178
179         if(meshlink_join(mesh2, corge_url)) {
180                 fprintf(stderr, "Bar could join twice\n");
181                 return 1;
182         }
183
184         free(corge_url);
185
186         // Clean up.
187
188         meshlink_close(mesh3);
189         meshlink_close(mesh2);
190         meshlink_close(mesh1);
191
192         return 0;
193 }