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>
25 static meshlink_handle_t **mesh;
26 static char* namesprefix="machine1";
27 static int nodeindex = 0;
29 static meshlink_node_t **nodes;
32 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
33 const char *levelstr[] = {
34 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
35 [MESHLINK_INFO] = "\x1b[32mINFO",
36 [MESHLINK_WARNING] = "\x1b[33mWARNING",
37 [MESHLINK_ERROR] = "\x1b[31mERROR",
38 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
40 fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global",levelstr[level], text);
43 //Test mesh sending data
44 static void testmesh () {
46 for(int nindex = 0; nindex < n; nindex++) {
48 nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
50 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
52 printf("%zu known nodes:\n", nnodes);
53 for(int i = 0; i < nnodes; i++) {
54 //printf(" %s\n", nodes[i]->name);
55 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
56 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
64 // Make all nodes know about each other by importing each others public keys and addresses.
65 static void linkmesh() {
66 for(int i = 0; i < n; i++) {
67 char *datai = meshlink_export(mesh[i]);
69 for(int j = i + 1; j < n; j++) {
70 char *dataj = meshlink_export(mesh[j]);
71 meshlink_import(mesh[i], dataj);
72 meshlink_import(mesh[j], datai);
80 static bool exportmeshgraph(const char* path)
83 int psr = stat(path, &ps);
85 if(psr == 0 || errno != ENOENT)
90 { fprintf(stderr, "%s exists already\n", path); }
95 FILE* stream = fopen(path, "w");
103 if(!devtool_export_json_all_edges_state(mesh[0], stream))
106 fprintf(stderr, "could not export graph\n");
115 void exportmeshgraph_timer(int signum)
118 gettimeofday(&ts, NULL);
121 snprintf(name, sizeof(name), "%sgraph_%ld_%03ld.json", namesprefix,ts.tv_sec, ts.tv_usec/1000);
123 exportmeshgraph(name);
127 static bool exportmeshgraph_started = false;
129 static bool exportmeshgraph_end(const char* none)
131 if(!exportmeshgraph_started)
134 struct itimerval zero_timer = { 0 };
135 setitimer (ITIMER_REAL, &zero_timer, NULL);
137 exportmeshgraph_started = false;
142 static bool exportmeshgraph_begin(const char* timeout_str)
147 if(exportmeshgraph_started)
149 if(!exportmeshgraph_end(NULL))
154 int timeout = atoi(timeout_str);
159 int timeout_sec = timeout / 1000;
160 int timeout_msec = timeout % 1000;
162 /* Install timer_handler as the signal handler for SIGALRM. */
163 signal(SIGALRM, exportmeshgraph_timer);
165 /* Configure the timer to expire immediately... */
166 struct itimerval timer;
167 timer.it_value.tv_sec = 0;
168 timer.it_value.tv_usec = 1000;
170 /* ... and every X msec after that. */
171 timer.it_interval.tv_sec = timeout_sec;
172 timer.it_interval.tv_usec = timeout_msec * 1000;
174 /* Start a real timer. */
175 setitimer (ITIMER_REAL, &timer, NULL);
177 exportmeshgraph_started = true;
182 static bool exportmeshgraph_end(const char* none)
187 static bool exportmeshgraph_begin(const char* timeout_str)
193 static void parse_command(char *buf) {
194 char *arg = strchr(buf, ' ');
198 if(!strcasecmp(buf, "invite")) {
202 fprintf(stderr, "/invite requires an argument!\n");
206 invitation = meshlink_invite(mesh[nodeindex], arg);
208 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
212 printf("Invitation for %s: %s\n", arg, invitation);
214 } else if(!strcasecmp(buf, "join")) {
216 fprintf(stderr, "/join requires an argument!\n");
219 meshlink_stop(mesh[nodeindex]);
220 if(!meshlink_join(mesh[nodeindex], arg))
221 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
223 fprintf(stderr, "Invitation accepted!\n");
224 meshlink_start(mesh[nodeindex]);
226 } else if(!strcasecmp(buf, "kick")) {
228 fprintf(stderr, "/kick requires an argument!\n");
232 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
234 fprintf(stderr, "Unknown node '%s'\n", arg);
238 meshlink_blacklist(mesh[nodeindex], node);
240 printf("Node '%s' blacklisted.\n", arg);
241 } else if(!strcasecmp(buf, "who")) {
243 nodes = meshlink_get_all_nodes(mesh[nodeindex], nodes, &nnodes);
245 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
247 printf("%zu known nodes:", nnodes);
248 for(int i = 0; i < nnodes; i++)
249 printf(" %s", nodes[i]->name);
253 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
255 fprintf(stderr, "Unknown node '%s'\n", arg);
257 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[nodeindex], node));
260 } else if(!strcasecmp(buf, "link")) {
262 } else if(!strcasecmp(buf, "eg")) {
263 exportmeshgraph(arg);
264 } else if(!strcasecmp(buf, "egb")) {
265 exportmeshgraph_begin(arg);
266 } else if(!strcasecmp(buf, "ege")) {
267 exportmeshgraph_end(NULL);
268 } else if(!strcasecmp(buf, "test")) {
270 } else if(!strcasecmp(buf, "select")) {
272 fprintf(stderr, "/select requires an argument!\n");
275 nodeindex = atoi(arg);
276 printf("Index is now %d\n",nodeindex);
277 } else if(!strcasecmp(buf, "stop")) {
278 meshlink_stop(mesh[nodeindex]);
279 } else if(!strcasecmp(buf, "quit")) {
282 } else if(!strcasecmp(buf, "help")) {
284 "<name>: <message> Send a message to the given node.\n"
285 " Subsequent messages don't need the <name>: prefix.\n"
286 "/invite <name> Create an invitation for a new node.\n"
287 "/join <invitation> Join an existing mesh using an invitation.\n"
288 "/kick <name> Blacklist the given node.\n"
289 "/who [<name>] List all nodes or show information about the given node.\n"
290 "/link Link all nodes together.\n"
291 "/eg <path> Export graph as json file.\n"
292 "/test Test functionality sending some data to all nodes\n"
293 "/select <number> Select the active node running the user commands\n"
294 "/stop Call meshlink_stop, use /select first to select which node to stop\n"
295 "/quit Exit this program.\n"
298 fprintf(stderr, "Unknown command '/%s'\n", buf);
302 static void parse_input(char *buf) {
303 static meshlink_node_t *destination;
313 if(len && buf[len - 1] == '\n')
316 if(len && buf[len - 1] == '\r')
319 // Ignore empty lines.
324 // Commands start with '/'
327 return parse_command(buf + 1);
329 // Lines in the form "name: message..." set the destination node.
332 char *colon = strchr(buf, ':');
340 destination = meshlink_get_node(mesh[nodeindex], buf);
342 fprintf(stderr, "Unknown node '%s'\n", buf);
348 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
352 if(!meshlink_send(mesh[nodeindex], destination, msg, strlen(msg) + 1)) {
353 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
357 printf("Message sent to '%s'.\n", destination->name);
360 int main(int argc, char *argv[]) {
361 const char *basebase = ".manynodes";
362 const char *graphexporttimeout = NULL;
369 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]);
377 namesprefix = argv[3];
380 graphexporttimeout = argv[4];
382 mesh = calloc(n, sizeof *mesh);
384 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
386 mkdir(basebase, 0750);
391 char filename[PATH_MAX];
393 for(int i = 0; i < n; i++) {
394 snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i);
395 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
396 bool itsnew = access(filename, R_OK);
398 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE);
401 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE);
403 meshlink_set_log_cb(mesh[i], MESHLINK_DEBUG, log_message);
405 fprintf(stderr, "errno is: %d\n", meshlink_errno);
406 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
413 for(int i = 0; i < n; i++) {
414 if(!meshlink_start(mesh[i]))
415 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
421 fprintf(stderr, "Could not start any node!\n");
425 if(graphexporttimeout)
426 { exportmeshgraph_begin(graphexporttimeout); }
428 printf("%d nodes started.\nType /help for a list of commands.\n", started);
431 while(fgets(buf, sizeof buf, stdin))
434 exportmeshgraph_end(NULL);
436 printf("Nodes stopping.\n");
438 for(int i = 0; i < n; i++)
439 meshlink_close(mesh[i]);