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 **mesh;
27 static 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() {
47 for(int nindex = 0; nindex < n; nindex++) {
49 nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
52 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
54 printf("%zu known nodes:\n", nnodes);
56 for(size_t i = 0; i < nnodes; i++) {
57 //printf(" %s\n", nodes[i]->name);
58 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
59 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
67 // Make all nodes know about each other by importing each others public keys and addresses.
68 static void linkmesh() {
69 for(int i = 0; i < n; i++) {
70 char *datai = meshlink_export(mesh[i]);
72 for(int j = i + 1; j < n; j++) {
73 char *dataj = meshlink_export(mesh[j]);
74 meshlink_import(mesh[i], dataj);
75 meshlink_import(mesh[j], datai);
83 static bool exportmeshgraph(const char *path) {
87 int psr = stat(path, &ps);
89 if(psr == 0 || errno != ENOENT) {
93 fprintf(stderr, "%s exists already\n", path);
99 FILE *stream = fopen(path, "w");
106 if(!devtool_export_json_all_edges_state(mesh[0], stream)) {
108 fprintf(stderr, "could not export graph\n");
117 void exportmeshgraph_timer(int signum) {
121 gettimeofday(&ts, NULL);
124 snprintf(name, sizeof(name), "%sgraph_%ld_%03ld.json", namesprefix, ts.tv_sec, ts.tv_usec / 1000);
126 exportmeshgraph(name);
130 static bool exportmeshgraph_started = false;
132 static bool exportmeshgraph_end(void) {
133 if(!exportmeshgraph_started) {
137 struct itimerval zero_timer;
139 setitimer(ITIMER_REAL, &zero_timer, NULL);
141 exportmeshgraph_started = false;
146 static bool exportmeshgraph_begin(const char *timeout_str) {
151 if(exportmeshgraph_started) {
152 if(!exportmeshgraph_end()) {
158 int timeout = atoi(timeout_str);
164 int timeout_sec = timeout / 1000;
165 int timeout_msec = timeout % 1000;
167 /* Install timer_handler as the signal handler for SIGALRM. */
168 signal(SIGALRM, exportmeshgraph_timer);
170 /* Configure the timer to expire immediately... */
171 struct itimerval timer;
172 timer.it_value.tv_sec = 0;
173 timer.it_value.tv_usec = 1000;
175 /* ... and every X msec after that. */
176 timer.it_interval.tv_sec = timeout_sec;
177 timer.it_interval.tv_usec = timeout_msec * 1000;
179 /* Start a real timer. */
180 setitimer(ITIMER_REAL, &timer, NULL);
182 exportmeshgraph_started = true;
187 static bool exportmeshgraph_end(void) {
191 static bool exportmeshgraph_begin(const char *timeout_str) {
196 static void parse_command(char *buf) {
197 char *arg = strchr(buf, ' ');
203 if(!strcasecmp(buf, "invite")) {
207 fprintf(stderr, "/invite requires an argument!\n");
211 invitation = meshlink_invite(mesh[nodeindex], arg);
214 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
218 printf("Invitation for %s: %s\n", arg, invitation);
220 } else if(!strcasecmp(buf, "join")) {
222 fprintf(stderr, "/join requires an argument!\n");
226 meshlink_stop(mesh[nodeindex]);
228 if(!meshlink_join(mesh[nodeindex], arg)) {
229 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
231 fprintf(stderr, "Invitation accepted!\n");
234 if(!meshlink_start(mesh[nodeindex])) {
235 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
238 } else if(!strcasecmp(buf, "kick")) {
240 fprintf(stderr, "/kick requires an argument!\n");
244 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
247 fprintf(stderr, "Unknown node '%s'\n", arg);
251 meshlink_blacklist(mesh[nodeindex], node);
253 printf("Node '%s' blacklisted.\n", arg);
254 } else if(!strcasecmp(buf, "who")) {
256 nodes = meshlink_get_all_nodes(mesh[nodeindex], nodes, &nnodes);
259 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
261 printf("%zu known nodes:", nnodes);
263 for(size_t i = 0; i < nnodes; i++) {
264 printf(" %s", nodes[i]->name);
270 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
273 fprintf(stderr, "Unknown node '%s'\n", arg);
275 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[nodeindex], node));
278 } else if(!strcasecmp(buf, "link")) {
280 } else if(!strcasecmp(buf, "eg")) {
281 exportmeshgraph(arg);
282 } else if(!strcasecmp(buf, "egb")) {
283 exportmeshgraph_begin(arg);
284 } else if(!strcasecmp(buf, "ege")) {
285 exportmeshgraph_end();
286 } else if(!strcasecmp(buf, "test")) {
288 } else if(!strcasecmp(buf, "select")) {
290 fprintf(stderr, "/select requires an argument!\n");
294 nodeindex = atoi(arg);
295 printf("Index is now %d\n", nodeindex);
296 } else if(!strcasecmp(buf, "stop")) {
297 meshlink_stop(mesh[nodeindex]);
298 } else if(!strcasecmp(buf, "quit")) {
301 } else if(!strcasecmp(buf, "help")) {
303 "<name>: <message> Send a message to the given node.\n"
304 " Subsequent messages don't need the <name>: prefix.\n"
305 "/invite <name> Create an invitation for a new node.\n"
306 "/join <invitation> Join an existing mesh using an invitation.\n"
307 "/kick <name> Blacklist the given node.\n"
308 "/who [<name>] List all nodes or show information about the given node.\n"
309 "/link Link all nodes together.\n"
310 "/eg <path> Export graph as json file.\n"
311 "/test Test functionality sending some data to all nodes\n"
312 "/select <number> Select the active node running the user commands\n"
313 "/stop Call meshlink_stop, use /select first to select which node to stop\n"
314 "/quit Exit this program.\n"
317 fprintf(stderr, "Unknown command '/%s'\n", buf);
321 static void parse_input(char *buf) {
322 static meshlink_node_t *destination;
333 if(len && buf[len - 1] == '\n') {
337 if(len && buf[len - 1] == '\r') {
341 // Ignore empty lines.
347 // Commands start with '/'
350 parse_command(buf + 1);
354 // Lines in the form "name: message..." set the destination node.
357 char *colon = strchr(buf, ':');
367 destination = meshlink_get_node(mesh[nodeindex], buf);
370 fprintf(stderr, "Unknown node '%s'\n", buf);
376 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
380 if(!meshlink_send(mesh[nodeindex], destination, msg, strlen(msg) + 1)) {
381 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
385 printf("Message sent to '%s'.\n", destination->name);
388 int main(int argc, char *argv[]) {
389 const char *basebase = ".manynodes";
390 const char *graphexporttimeout = NULL;
398 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]);
407 namesprefix = argv[3];
411 graphexporttimeout = argv[4];
414 mesh = calloc(n, sizeof(*mesh));
416 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
418 mkdir(basebase, 0750);
423 char filename[PATH_MAX];
426 for(int i = 0; i < n; i++) {
427 snprintf(nodename, sizeof(nodename), "%snode%d", namesprefix, i);
428 snprintf(filename, sizeof(filename), "%s/%s", basebase, nodename);
430 if(n / (i + 1) > n / 4) {
431 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE);
433 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE);
436 meshlink_set_log_cb(mesh[i], MESHLINK_DEBUG, log_message);
439 fprintf(stderr, "errno is: %d\n", meshlink_errno);
440 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
447 for(int i = 0; i < n; i++) {
448 if(!meshlink_start(mesh[i])) {
449 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
456 fprintf(stderr, "Could not start any node!\n");
460 if(graphexporttimeout) {
461 exportmeshgraph_begin(graphexporttimeout);
464 printf("%d nodes started.\nType /help for a list of commands.\n", started);
467 while(fgets(buf, sizeof(buf), stdin)) {
471 exportmeshgraph_end();
473 printf("Nodes stopping.\n");
475 for(int i = 0; i < n; i++) {
476 meshlink_close(mesh[i]);