]> git.meshlink.io Git - meshlink/blob - test/invite-join.c
b69eb3d87a267179cc560d212da2706dc6fd6ca5
[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         free(baz_url);
110
111         if(!meshlink_start(mesh2)) {
112                 fprintf(stderr, "Baz could not start\n");
113                 return 1;
114         }
115
116         // Wait for the two to connect.
117
118         for(int i = 0; i < 60; i++) {
119                 sleep(1);
120
121                 if(baz_reachable) {
122                         break;
123                 }
124         }
125
126         if(!baz_reachable) {
127                 fprintf(stderr, "Baz not reachable for foo after 20 seconds\n");
128                 return 1;
129         }
130
131         int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
132
133         for(int i = 0; i < 10 && !pmtu; i++) {
134                 sleep(1);
135                 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
136         }
137
138         if(!pmtu) {
139                 fprintf(stderr, "UDP communication with baz not possible after 10 seconds\n");
140                 return 1;
141         }
142
143         // Check that nodes cannot join with expired invitations
144
145         meshlink_set_invitation_timeout(mesh1, 0);
146
147         if(meshlink_join(mesh3, quux_url)) {
148                 fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n");
149                 return 1;
150         }
151
152         free(quux_url);
153
154         // Clean up.
155
156         meshlink_close(mesh3);
157         meshlink_close(mesh2);
158         meshlink_close(mesh1);
159
160         return 0;
161 }