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