9 #include <linux/limits.h>
12 #include "../src/meshlink.h"
13 #include "../src/devtools.h"
15 #include <sys/types.h>
23 static meshlink_handle_t **mesh;
24 static char* namesprefix="machine1";
25 static int nodeindex = 0;
27 static meshlink_node_t **nodes;
30 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
31 const char *levelstr[] = {
32 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
33 [MESHLINK_INFO] = "\x1b[32mINFO",
34 [MESHLINK_WARNING] = "\x1b[33mWARNING",
35 [MESHLINK_ERROR] = "\x1b[31mERROR",
36 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
38 fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global",levelstr[level], text);
41 //Test mesh sending data
42 static void testmesh () {
44 for(int nindex = 0; nindex < n; nindex++) {
46 nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
48 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
50 printf("%zu known nodes:\n", nnodes);
51 for(int i = 0; i < nnodes; i++) {
52 //printf(" %s\n", nodes[i]->name);
53 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
54 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
62 // Make all nodes know about each other by importing each others public keys and addresses.
63 static void linkmesh() {
64 for(int i = 0; i < n; i++) {
65 char *datai = meshlink_export(mesh[i]);
67 for(int j = i + 1; j < n; j++) {
68 char *dataj = meshlink_export(mesh[j]);
69 meshlink_import(mesh[i], dataj);
70 meshlink_import(mesh[j], datai);
78 static bool exportmeshgraph(const char* path)
81 int psr = stat(path, &ps);
83 if(psr == 0 || errno != ENOENT)
88 { fprintf(stderr, "%s exists already\n", path); }
93 FILE* stream = fopen(path, "w");
101 if(!devtool_export_json_all_edges_state(mesh[0], stream))
104 fprintf(stderr, "could not export graph\n");
113 void exportmeshgraph_timer(int signum)
116 gettimeofday(&ts, NULL);
119 snprintf(name, sizeof(name), "%sgraph_%ld_%03ld.json", namesprefix,ts.tv_sec, ts.tv_usec/1000);
121 exportmeshgraph(name);
125 static bool exportmeshgraph_started = false;
127 static bool exportmeshgraph_end(const char* none)
129 if(!exportmeshgraph_started)
132 struct itimerval zero_timer = { 0 };
133 setitimer (ITIMER_REAL, &zero_timer, NULL);
135 exportmeshgraph_started = false;
140 static bool exportmeshgraph_begin(const char* timeout_str)
145 if(exportmeshgraph_started)
147 if(!exportmeshgraph_end(NULL))
152 int timeout = atoi(timeout_str);
157 int timeout_sec = timeout / 1000;
158 int timeout_msec = timeout % 1000;
160 /* Install timer_handler as the signal handler for SIGALRM. */
161 signal(SIGALRM, exportmeshgraph_timer);
163 /* Configure the timer to expire immediately... */
164 struct itimerval timer;
165 timer.it_value.tv_sec = 0;
166 timer.it_value.tv_usec = 1000;
168 /* ... and every X msec after that. */
169 timer.it_interval.tv_sec = timeout_sec;
170 timer.it_interval.tv_usec = timeout_msec * 1000;
172 /* Start a real timer. */
173 setitimer (ITIMER_REAL, &timer, NULL);
175 exportmeshgraph_started = true;
180 static bool exportmeshgraph_end(const char* none)
185 static bool exportmeshgraph_begin(const char* timeout_str)
191 static void parse_command(char *buf) {
192 char *arg = strchr(buf, ' ');
196 if(!strcasecmp(buf, "invite")) {
200 fprintf(stderr, "/invite requires an argument!\n");
204 invitation = meshlink_invite(mesh[nodeindex], arg);
206 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
210 printf("Invitation for %s: %s\n", arg, invitation);
212 } else if(!strcasecmp(buf, "join")) {
214 fprintf(stderr, "/join requires an argument!\n");
217 meshlink_stop(mesh[nodeindex]);
218 if(!meshlink_join(mesh[nodeindex], arg))
219 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
221 fprintf(stderr, "Invitation accepted!\n");
222 meshlink_start(mesh[nodeindex]);
224 } else if(!strcasecmp(buf, "kick")) {
226 fprintf(stderr, "/kick requires an argument!\n");
230 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
232 fprintf(stderr, "Unknown node '%s'\n", arg);
236 meshlink_blacklist(mesh[nodeindex], node);
238 printf("Node '%s' blacklisted.\n", arg);
239 } else if(!strcasecmp(buf, "who")) {
241 nodes = meshlink_get_all_nodes(mesh[nodeindex], nodes, &nnodes);
243 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
245 printf("%zu known nodes:", nnodes);
246 for(int i = 0; i < nnodes; i++)
247 printf(" %s", nodes[i]->name);
251 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
253 fprintf(stderr, "Unknown node '%s'\n", arg);
255 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[nodeindex], node));
258 } else if(!strcasecmp(buf, "link")) {
260 } else if(!strcasecmp(buf, "eg")) {
261 exportmeshgraph(arg);
262 } else if(!strcasecmp(buf, "egb")) {
263 exportmeshgraph_begin(arg);
264 } else if(!strcasecmp(buf, "ege")) {
265 exportmeshgraph_end(NULL);
266 } else if(!strcasecmp(buf, "test")) {
268 } else if(!strcasecmp(buf, "select")) {
270 fprintf(stderr, "/select requires an argument!\n");
273 nodeindex = atoi(arg);
274 printf("Index is now %d\n",nodeindex);
275 } else if(!strcasecmp(buf, "stop")) {
276 meshlink_stop(mesh[nodeindex]);
277 } else if(!strcasecmp(buf, "quit")) {
280 } else if(!strcasecmp(buf, "help")) {
282 "<name>: <message> Send a message to the given node.\n"
283 " Subsequent messages don't need the <name>: prefix.\n"
284 "/invite <name> Create an invitation for a new node.\n"
285 "/join <invitation> Join an existing mesh using an invitation.\n"
286 "/kick <name> Blacklist the given node.\n"
287 "/who [<name>] List all nodes or show information about the given node.\n"
288 "/link Link all nodes together.\n"
289 "/eg <path> Export graph as json file.\n"
290 "/test Test functionality sending some data to all nodes\n"
291 "/select <number> Select the active node running the user commands\n"
292 "/stop Call meshlink_stop, use /select first to select which node to stop\n"
293 "/quit Exit this program.\n"
296 fprintf(stderr, "Unknown command '/%s'\n", buf);
300 static void parse_input(char *buf) {
301 static meshlink_node_t *destination;
311 if(len && buf[len - 1] == '\n')
314 if(len && buf[len - 1] == '\r')
317 // Ignore empty lines.
322 // Commands start with '/'
325 return parse_command(buf + 1);
327 // Lines in the form "name: message..." set the destination node.
330 char *colon = strchr(buf, ':');
338 destination = meshlink_get_node(mesh[nodeindex], buf);
340 fprintf(stderr, "Unknown node '%s'\n", buf);
346 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
350 if(!meshlink_send(mesh[nodeindex], destination, msg, strlen(msg) + 1)) {
351 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
355 printf("Message sent to '%s'.\n", destination->name);
358 int main(int argc, char *argv[]) {
359 const char *basebase = ".manynodes";
360 const char *graphexporttimeout = NULL;
367 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]);
375 namesprefix = argv[3];
378 graphexporttimeout = argv[4];
380 mesh = calloc(n, sizeof *mesh);
382 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
384 mkdir(basebase, 0750);
389 char filename[PATH_MAX];
391 for(int i = 0; i < n; i++) {
392 snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i);
393 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
394 bool itsnew = access(filename, R_OK);
396 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE);
399 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE);
401 meshlink_set_log_cb(mesh[i], MESHLINK_DEBUG, log_message);
403 fprintf(stderr, "errno is: %d\n", meshlink_errno);
404 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
411 for(int i = 0; i < n; i++) {
412 if(!meshlink_start(mesh[i]))
413 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
419 fprintf(stderr, "Could not start any node!\n");
423 if(graphexporttimeout)
424 { exportmeshgraph_begin(graphexporttimeout); }
426 printf("%d nodes started.\nType /help for a list of commands.\n", started);
429 while(fgets(buf, sizeof buf, stdin))
432 exportmeshgraph_end(NULL);
434 printf("Nodes stopping.\n");
436 for(int i = 0; i < n; i++)
437 meshlink_close(mesh[i]);