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