]> git.meshlink.io Git - meshlink/blob - test/invite-join.c
Merge branch 'discovery' into everbase
[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         if(!strcmp(node->name, "baz"))
12                 baz_reachable = reachable;
13 }
14
15 int main(int argc, char *argv[]) {
16         // Open two new meshlink instance.
17
18         meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo");
19         if(!mesh1) {
20                 fprintf(stderr, "Could not initialize configuration for foo\n");
21                 return 1;
22         }
23
24         meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar");
25         if(!mesh2) {
26                 fprintf(stderr, "Could not initialize configuration for bar\n");
27                 return 1;
28         }
29
30         // Start the first instance and have it generate an invitation.
31
32         meshlink_set_node_status_cb(mesh1, status_cb);
33         
34         if(!meshlink_start(mesh1)) {
35                 fprintf(stderr, "Foo could not start\n");
36                 return 1;
37         }
38
39         meshlink_add_address(mesh1, "localhost");
40         char *url = meshlink_invite(mesh1, "baz");
41         if(!url) {
42                 fprintf(stderr, "Foo could not generate an invitation for baz\n");
43                 return 1;
44         }
45
46         // Have the second instance join the first.
47
48         if(!meshlink_join(mesh2, url)) {
49                 fprintf(stderr, "Baz could not join foo's mesh\n");
50                 return 1;
51         }
52
53         free(url);
54
55         if(!meshlink_start(mesh2)) {
56                 fprintf(stderr, "Baz could not start\n");
57                 return 1;
58         }
59
60         // Wait for the two to connect.
61
62         for(int i = 0; i < 60; i++) {
63                 sleep(1);
64                 if(baz_reachable)
65                         break;
66         }
67
68         if(!baz_reachable) {
69                 fprintf(stderr, "Baz not reachable for foo after 20 seconds\n");
70                 return 1;
71         }
72
73         int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
74         for(int i = 0; i < 10 && !pmtu; i++) {
75                 sleep(1);
76                 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
77         }
78
79         if(!pmtu) {
80                 fprintf(stderr, "UDP communication with baz not possible after 10 seconds\n");
81                 return 1;
82         }
83
84         // Clean up.
85
86         meshlink_stop(mesh2);
87         meshlink_stop(mesh1);
88         meshlink_close(mesh2);
89         meshlink_close(mesh1);
90
91         return 0;
92 }