]> git.meshlink.io Git - meshlink/blob - test/invite-join.c
Add support for encrypted storage.
[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 log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
11         static struct timeval tv0;
12         struct timeval tv;
13
14         if(tv0.tv_sec == 0) {
15                 gettimeofday(&tv0, NULL);
16         }
17
18         gettimeofday(&tv, NULL);
19         fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
20
21         if(mesh) {
22                 fprintf(stderr, "(%s) ", mesh->name);
23         }
24
25         fprintf(stderr, "[%d] %s\n", level, text);
26 }
27
28 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
29         (void)mesh;
30
31         if(!strcmp(node->name, "baz")) {
32                 baz_reachable = reachable;
33         }
34 }
35
36 int main() {
37         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
38
39         // Open thee new meshlink instance.
40
41         meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
42
43         if(!mesh1) {
44                 fprintf(stderr, "Could not initialize configuration for foo\n");
45                 return 1;
46         }
47
48         meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
49
50         meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
51
52         if(!mesh2) {
53                 fprintf(stderr, "Could not initialize configuration for bar\n");
54                 return 1;
55         }
56
57         meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
58
59         meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
60
61         if(!mesh3) {
62                 fprintf(stderr, "Could not initialize configuration for quux\n");
63                 return 1;
64         }
65
66         meshlink_set_log_cb(mesh3, MESHLINK_DEBUG, log_cb);
67
68         // Disable local discovery.
69
70         meshlink_enable_discovery(mesh1, false);
71         meshlink_enable_discovery(mesh2, false);
72         meshlink_enable_discovery(mesh3, false);
73
74         // Start the first instance and have it generate invitations.
75
76         meshlink_set_node_status_cb(mesh1, status_cb);
77
78         if(!meshlink_start(mesh1)) {
79                 fprintf(stderr, "Foo could not start\n");
80                 return 1;
81         }
82
83         meshlink_add_address(mesh1, "localhost");
84         char *baz_url = meshlink_invite(mesh1, NULL, "baz");
85
86         if(!baz_url) {
87                 fprintf(stderr, "Foo could not generate an invitation for baz\n");
88                 return 1;
89         }
90
91         char *quux_url = meshlink_invite(mesh1, NULL, "quux");
92
93         if(!quux_url) {
94                 fprintf(stderr, "Foo could not generate an invitation for quux\n");
95                 return 1;
96         }
97
98         fprintf(stderr, "Invitation URL for baz:  %s\n", baz_url);
99         fprintf(stderr, "Invitation URL for quux: %s\n", quux_url);
100
101         // Have the second instance join the first.
102
103         if(!meshlink_join(mesh2, baz_url)) {
104                 fprintf(stderr, "Baz could not join foo's mesh\n");
105                 return 1;
106         }
107
108         free(baz_url);
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 nodes cannot join with expired invitations
143
144         meshlink_set_invitation_timeout(mesh1, 0);
145
146         if(meshlink_join(mesh3, quux_url)) {
147                 fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n");
148                 return 1;
149         }
150
151         free(quux_url);
152
153         // Clean up.
154
155         meshlink_close(mesh3);
156         meshlink_close(mesh2);
157         meshlink_close(mesh1);
158
159         return 0;
160 }