]> git.meshlink.io Git - meshlink/blob - examples/manynodes.c
"last successfull connect" introduced
[meshlink] / examples / manynodes.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <linux/limits.h>
8
9 #include "../src/meshlink.h"
10
11 static int n = 10;
12 static meshlink_handle_t **mesh;
13
14 static meshlink_node_t **nodes;
15 static size_t nnodes;
16
17 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
18         const char *levelstr[] = {
19                 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
20                 [MESHLINK_INFO] = "\x1b[32mINFO",
21                 [MESHLINK_WARNING] = "\x1b[33mWARNING",
22                 [MESHLINK_ERROR] = "\x1b[31mERROR",
23                 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
24         };
25         fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global",levelstr[level], text);
26 }
27
28 //Test mesh sending data
29 static void testmesh () {
30
31         for(int nindex = 0; nindex < n; nindex++) {
32
33                         nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
34                         if(!nodes) {
35                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
36                         } else {
37                                 printf("%zu known nodes:\n", nnodes);
38                                 for(int i = 0; i < nnodes; i++) {
39                                         //printf(" %s\n", nodes[i]->name);
40                                                 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
41                 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
42                                                 }
43                                 }
44
45                         }
46
47         }
48 }
49 // Make all nodes know about each other by importing each others public keys and addresses.
50 static void linkmesh() {
51         for(int i = 0; i < n; i++) {
52                 char *datai = meshlink_export(mesh[i]);
53
54                 for(int j = i + 1; j < n; j++) {
55                         char *dataj = meshlink_export(mesh[j]);
56                         meshlink_import(mesh[i], dataj);
57                         meshlink_import(mesh[j], datai);
58                         free(dataj);
59                 }
60
61                 free(datai);
62         }
63 }
64
65 static void parse_command(char *buf) {
66         char *arg = strchr(buf, ' ');
67         if(arg)
68                 *arg++ = 0;
69
70         if(!strcasecmp(buf, "invite")) {
71                 char *invitation;
72
73                 if(!arg) {
74                         fprintf(stderr, "/invite requires an argument!\n");
75                         return;
76                 }
77
78                 invitation = meshlink_invite(mesh[0], arg);
79                 if(!invitation) {
80                         fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
81                         return;
82                 }
83
84                 printf("Invitation for %s: %s\n", arg, invitation);
85                 free(invitation);
86         } else if(!strcasecmp(buf, "join")) {
87                 if(!arg) {
88                         fprintf(stderr, "/join requires an argument!\n");
89                         return;
90                 }
91                 meshlink_stop(mesh[0]);
92                 if(!meshlink_join(mesh[0], arg))
93                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
94                 else {
95                         fprintf(stderr, "Invitation accepted!\n");
96                         meshlink_start(mesh[0]);
97                 }
98         } else if(!strcasecmp(buf, "kick")) {
99                 if(!arg) {
100                         fprintf(stderr, "/kick requires an argument!\n");
101                         return;
102                 }
103
104                 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
105                 if(!node) {
106                         fprintf(stderr, "Unknown node '%s'\n", arg);
107                         return;
108                 }
109
110                 meshlink_blacklist(mesh[0], node);
111
112                 printf("Node '%s' blacklisted.\n", arg);
113         } else if(!strcasecmp(buf, "who")) {
114                 if(!arg) {
115                         nodes = meshlink_get_all_nodes(mesh[0], nodes, &nnodes);
116                         if(!nodes) {
117                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
118                         } else {
119                                 printf("%zu known nodes:", nnodes);
120                                 for(int i = 0; i < nnodes; i++)
121                                         printf(" %s", nodes[i]->name);
122                                 printf("\n");
123                         }
124                 } else {
125                         meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
126                         if(!node) {
127                                 fprintf(stderr, "Unknown node '%s'\n", arg);
128                         } else {
129                                 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[0], node));
130                         }
131                 }
132         } else if(!strcasecmp(buf, "link")) {
133                 linkmesh();
134         } else if(!strcasecmp(buf, "test")) {
135                 testmesh();
136         } else if(!strcasecmp(buf, "quit")) {
137                 printf("Bye!\n");
138                 fclose(stdin);
139         } else if(!strcasecmp(buf, "help")) {
140                 printf(
141                         "<name>: <message>     Send a message to the given node.\n"
142                         "                      Subsequent messages don't need the <name>: prefix.\n"
143                         "/invite <name>        Create an invitation for a new node.\n"
144                         "/join <invitation>    Join an existing mesh using an invitation.\n"
145                         "/kick <name>          Blacklist the given node.\n"
146                         "/who [<name>]         List all nodes or show information about the given node.\n"
147                         "/link                 Link all nodes together.\n"
148                         "/test                 Test functionality sending some data to all nodes\n"
149                         "/quit                 Exit this program.\n"
150                         );
151         } else {
152                 fprintf(stderr, "Unknown command '/%s'\n", buf);
153         }
154 }
155
156 static void parse_input(char *buf) {
157         static meshlink_node_t *destination;
158         size_t len;
159
160         if(!buf)
161                 return;
162
163         // Remove newline.
164
165         len = strlen(buf);
166
167         if(len && buf[len - 1] == '\n')
168                 buf[--len] = 0;
169
170         if(len && buf[len - 1] == '\r')
171                 buf[--len] = 0;
172
173         // Ignore empty lines.
174
175         if(!len)
176                 return;
177
178         // Commands start with '/'
179
180         if(*buf == '/')
181                 return parse_command(buf + 1);
182
183         // Lines in the form "name: message..." set the destination node.
184
185         char *msg = buf;
186         char *colon = strchr(buf, ':');
187
188         if(colon) {
189                 *colon = 0;
190                 msg = colon + 1;
191                 if(*msg == ' ')
192                         msg++;
193
194                 destination = meshlink_get_node(mesh[0], buf);
195                 if(!destination) {
196                         fprintf(stderr, "Unknown node '%s'\n", buf);
197                         return;
198                 }
199         }
200
201         if(!destination) {
202                 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
203                 return;
204         }
205
206         if(!meshlink_send(mesh[0], destination, msg, strlen(msg) + 1)) {
207                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
208                 return;
209         }
210
211         printf("Message sent to '%s'.\n", destination->name);
212 }
213
214 int main(int argc, char *argv[]) {
215         const char *basebase = ".manynodes";
216         const char *namesprefix = "machine1";
217         char buf[1024];
218
219         if(argc > 1)
220                 n = atoi(argv[1]);
221
222         if(n < 1) {
223                 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames]\n", argv[0]);
224                 return 1;
225         }
226
227         if(argc > 2)
228                 basebase = argv[2];
229
230         if(argc > 3)
231                 namesprefix = argv[3];
232
233         mesh = calloc(n, sizeof *mesh);
234
235         meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
236         mkdir(basebase, 0750);
237
238         char filename[PATH_MAX];
239         char nodename[100];
240         for(int i = 0; i < n; i++) {
241                 snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i);
242                 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
243                 bool itsnew = access(filename, R_OK);
244                 mesh[i] = meshlink_open(filename, nodename, "manynodes", i%_DEV_CLASS_MAX);
245                 meshlink_set_log_cb(mesh[i], MESHLINK_INFO, log_message);
246                 if(itsnew)
247                         meshlink_add_address(mesh[i], "localhost");
248                 if(!mesh[i]) {
249                         fprintf(stderr, "errno is: %d\n", meshlink_errno);
250                         fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
251                         return 1;
252                 }
253         }
254
255         int started = 0;
256
257         for(int i = 0; i < n; i++) {
258                 if(!meshlink_start(mesh[i]))
259                         fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
260                 else
261                         started++;
262         }
263
264         if(!started) {
265                 fprintf(stderr, "Could not start any node!\n");
266                 return 1;
267         }
268
269         printf("%d nodes started.\nType /help for a list of commands.\n", started);
270
271         while(fgets(buf, sizeof buf, stdin))
272                 parse_input(buf);
273
274         printf("Nodes stopping.\n");
275
276         for(int i = 0; i < n; i++)
277                 meshlink_close(mesh[i]);
278
279         return 0;
280 }