8 #if !defined(_WIN32) && !defined(__APPLE__)
9 #include <linux/limits.h>
10 #elif defined(__APPLE__)
11 #include <sys/syslimits.h>
14 #include "../src/meshlink.h"
15 #include "../src/devtools.h"
17 #include <sys/types.h>
26 static meshlink_handle_t **meshes;
27 static const char *namesprefix = "machine1";
28 static int nodeindex = 0;
30 static meshlink_node_t **nodes;
33 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
34 const char *levelstr[] = {
35 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
36 [MESHLINK_INFO] = "\x1b[32mINFO",
37 [MESHLINK_WARNING] = "\x1b[33mWARNING",
38 [MESHLINK_ERROR] = "\x1b[31mERROR",
39 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
41 fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global", levelstr[level], text);
44 //Test mesh sending data
45 static void testmesh(void) {
46 for(int nindex = 0; nindex < n; nindex++) {
48 nodes = meshlink_get_all_nodes(meshes[nindex], nodes, &nnodes);
51 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
53 printf("%zu known nodes:\n", nnodes);
55 for(size_t i = 0; i < nnodes; i++) {
56 //printf(" %s\n", nodes[i]->name);
57 if(!meshlink_send(meshes[nindex], nodes[i], "magic", strlen("magic") + 1)) {
58 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
66 // Make all nodes know about each other by importing each others public keys and addresses.
67 static void linkmesh(void) {
68 for(int i = 0; i < n; i++) {
69 char *datai = meshlink_export(meshes[i]);
71 for(int j = i + 1; j < n; j++) {
72 char *dataj = meshlink_export(meshes[j]);
74 if(!meshlink_import(meshes[i], dataj) || !meshlink_import(meshes[j], datai)) {
75 fprintf(stderr, "Could not exchange keys between %s and %s: %s\n", meshes[i]->name, meshes[j]->name, meshlink_strerror(meshlink_errno));
85 static bool exportmeshgraph(const char *path) {
89 int psr = stat(path, &ps);
91 if(psr == 0 || errno != ENOENT) {
95 fprintf(stderr, "%s exists already\n", path);
101 FILE *stream = fopen(path, "w");
108 if(!devtool_export_json_all_edges_state(meshes[0], stream)) {
110 fprintf(stderr, "could not export graph\n");
119 static void exportmeshgraph_timer(int signum) {
123 gettimeofday(&ts, NULL);
126 snprintf(name, sizeof(name), "%sgraph_%ld_%03ld.json", namesprefix, ts.tv_sec, ts.tv_usec / 1000L);
128 exportmeshgraph(name);
132 static bool exportmeshgraph_started = false;
134 static bool exportmeshgraph_end(void) {
135 if(!exportmeshgraph_started) {
139 struct itimerval zero_timer;
141 setitimer(ITIMER_REAL, &zero_timer, NULL);
143 exportmeshgraph_started = false;
148 static bool exportmeshgraph_begin(const char *timeout_str) {
153 if(exportmeshgraph_started) {
154 if(!exportmeshgraph_end()) {
160 int timeout = atoi(timeout_str);
166 int timeout_sec = timeout / 1000;
167 int timeout_msec = timeout % 1000;
169 /* Install timer_handler as the signal handler for SIGALRM. */
170 signal(SIGALRM, exportmeshgraph_timer);
172 /* Configure the timer to expire immediately... */
173 struct itimerval timer;
174 timer.it_value.tv_sec = 0;
175 timer.it_value.tv_usec = 1000;
177 /* ... and every X msec after that. */
178 timer.it_interval.tv_sec = timeout_sec;
179 timer.it_interval.tv_usec = timeout_msec * 1000;
181 /* Start a real timer. */
182 setitimer(ITIMER_REAL, &timer, NULL);
184 exportmeshgraph_started = true;
189 static bool exportmeshgraph_end(void) {
193 static bool exportmeshgraph_begin(const char *timeout_str) {
198 static void parse_command(char *buf) {
199 char *arg = strchr(buf, ' ');
205 if(!strcasecmp(buf, "invite")) {
209 fprintf(stderr, "/invite requires an argument!\n");
213 invitation = meshlink_invite(meshes[nodeindex], NULL, arg);
216 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
220 printf("Invitation for %s: %s\n", arg, invitation);
222 } else if(!strcasecmp(buf, "join")) {
224 fprintf(stderr, "/join requires an argument!\n");
228 meshlink_stop(meshes[nodeindex]);
230 if(!meshlink_join(meshes[nodeindex], arg)) {
231 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
233 fprintf(stderr, "Invitation accepted!\n");
236 if(!meshlink_start(meshes[nodeindex])) {
237 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
240 } else if(!strcasecmp(buf, "kick")) {
242 fprintf(stderr, "/kick requires an argument!\n");
246 meshlink_node_t *node = meshlink_get_node(meshes[nodeindex], arg);
249 fprintf(stderr, "Unknown node '%s'\n", arg);
253 if(!meshlink_blacklist(meshes[nodeindex], node)) {
254 fprintf(stderr, "Error blacklising '%s': %s", arg, meshlink_strerror(meshlink_errno));
258 printf("Node '%s' blacklisted.\n", arg);
259 } else if(!strcasecmp(buf, "whitelist")) {
261 fprintf(stderr, "/whitelist requires an argument!\n");
265 meshlink_node_t *node = meshlink_get_node(meshes[nodeindex], arg);
268 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
272 if(!meshlink_whitelist(meshes[nodeindex], node)) {
273 fprintf(stderr, "Error whitelising '%s': %s", arg, meshlink_strerror(meshlink_errno));
277 printf("Node '%s' whitelisted.\n", arg);
278 } else if(!strcasecmp(buf, "who")) {
280 nodes = meshlink_get_all_nodes(meshes[nodeindex], nodes, &nnodes);
283 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
285 printf("%zu known nodes:", nnodes);
287 for(size_t i = 0; i < nnodes; i++) {
288 printf(" %s", nodes[i]->name);
294 meshlink_node_t *node = meshlink_get_node(meshes[nodeindex], arg);
297 fprintf(stderr, "Unknown node '%s'\n", arg);
299 printf("Node %s found, pmtu %ld\n", arg, (long int)meshlink_get_pmtu(meshes[nodeindex], node));
302 } else if(!strcasecmp(buf, "link")) {
304 } else if(!strcasecmp(buf, "eg")) {
305 exportmeshgraph(arg);
306 } else if(!strcasecmp(buf, "egb")) {
307 exportmeshgraph_begin(arg);
308 } else if(!strcasecmp(buf, "ege")) {
309 exportmeshgraph_end();
310 } else if(!strcasecmp(buf, "test")) {
312 } else if(!strcasecmp(buf, "select")) {
314 fprintf(stderr, "/select requires an argument!\n");
318 nodeindex = atoi(arg);
319 printf("Index is now %d\n", nodeindex);
320 } else if(!strcasecmp(buf, "stop")) {
321 meshlink_stop(meshes[nodeindex]);
322 } else if(!strcasecmp(buf, "quit")) {
325 } else if(!strcasecmp(buf, "help")) {
327 "<name>: <message> Send a message to the given node.\n"
328 " Subsequent messages don't need the <name>: prefix.\n"
329 "/invite <name> Create an invitation for a new node.\n"
330 "/join <invitation> Join an existing mesh using an invitation.\n"
331 "/kick <name> Blacklist the given node.\n"
332 "/who [<name>] List all nodes or show information about the given node.\n"
333 "/link Link all nodes together.\n"
334 "/eg <path> Export graph as json file.\n"
335 "/test Test functionality sending some data to all nodes\n"
336 "/select <number> Select the active node running the user commands\n"
337 "/stop Call meshlink_stop, use /select first to select which node to stop\n"
338 "/quit Exit this program.\n"
341 fprintf(stderr, "Unknown command '/%s'\n", buf);
345 static void parse_input(char *buf) {
346 static meshlink_node_t *destination;
357 if(len && buf[len - 1] == '\n') {
361 if(len && buf[len - 1] == '\r') {
365 // Ignore empty lines.
371 // Commands start with '/'
374 parse_command(buf + 1);
378 // Lines in the form "name: message..." set the destination node.
381 char *colon = strchr(buf, ':');
391 destination = meshlink_get_node(meshes[nodeindex], buf);
394 fprintf(stderr, "Unknown node '%s'\n", buf);
400 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
404 if(!meshlink_send(meshes[nodeindex], destination, msg, strlen(msg) + 1)) {
405 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
409 printf("Message sent to '%s'.\n", destination->name);
412 int main(int argc, char *argv[]) {
413 const char *basebase = ".manynodes";
414 const char *graphexporttimeout = NULL;
422 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]);
431 namesprefix = argv[3];
435 graphexporttimeout = argv[4];
438 meshes = calloc(n, sizeof(*meshes));
440 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
442 mkdir(basebase, 0750);
447 char filename[PATH_MAX];
450 for(int i = 0; i < n; i++) {
451 snprintf(nodename, sizeof(nodename), "%snode%d", namesprefix, i);
452 snprintf(filename, sizeof(filename), "%s/%s", basebase, nodename);
454 if(n / (i + 1) > n / 4) {
455 meshes[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE);
457 meshes[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE);
460 meshlink_set_log_cb(meshes[i], MESHLINK_DEBUG, log_message);
463 fprintf(stderr, "errno is: %d\n", meshlink_errno);
464 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
471 for(int i = 0; i < n; i++) {
472 if(!meshlink_start(meshes[i])) {
473 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
480 fprintf(stderr, "Could not start any node!\n");
484 if(graphexporttimeout) {
485 exportmeshgraph_begin(graphexporttimeout);
488 printf("%d nodes started.\nType /help for a list of commands.\n", started);
491 while(fgets(buf, sizeof(buf), stdin)) {
495 exportmeshgraph_end();
497 printf("Nodes stopping.\n");
499 for(int i = 0; i < n; i++) {
500 meshlink_close(meshes[i]);