7 #include <linux/limits.h>
9 #include "../src/meshlink.h"
10 #include "../src/devtools.h"
12 #include <sys/types.h>
20 static meshlink_handle_t **mesh;
22 static int nodeindex = 0;
24 static meshlink_node_t **nodes;
27 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
28 const char *levelstr[] = {
29 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
30 [MESHLINK_INFO] = "\x1b[32mINFO",
31 [MESHLINK_WARNING] = "\x1b[33mWARNING",
32 [MESHLINK_ERROR] = "\x1b[31mERROR",
33 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
35 fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global",levelstr[level], text);
38 //Test mesh sending data
39 static void testmesh () {
41 for(int nindex = 0; nindex < n; nindex++) {
43 nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
45 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
47 printf("%zu known nodes:\n", nnodes);
48 for(int i = 0; i < nnodes; i++) {
49 //printf(" %s\n", nodes[i]->name);
50 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
51 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
59 // Make all nodes know about each other by importing each others public keys and addresses.
60 static void linkmesh() {
61 for(int i = 0; i < n; i++) {
62 char *datai = meshlink_export(mesh[i]);
64 for(int j = i + 1; j < n; j++) {
65 char *dataj = meshlink_export(mesh[j]);
66 meshlink_import(mesh[i], dataj);
67 meshlink_import(mesh[j], datai);
75 static bool exportmeshgraph(const char* path)
78 int psr = stat(path, &ps);
80 if(psr == 0 || errno != ENOENT)
85 { fprintf(stderr, "%s exists already\n", path); }
90 FILE* stream = fopen(path, "w");
98 if(!devtool_export_json_all_edges_state(mesh[nodeindex], stream))
101 fprintf(stderr, "could not export graph\n");
110 void exportmeshgraph_timer(int signum)
113 gettimeofday(&ts, NULL);
116 snprintf(name, sizeof(name), "graph_%ld_%03ld.json", ts.tv_sec, ts.tv_usec/1000);
118 exportmeshgraph(name);
121 static bool exportmeshgraph_started = false;
123 static bool exportmeshgraph_end(const char* none)
125 if(!exportmeshgraph_started)
128 struct itimerval zero_timer = { 0 };
129 setitimer (ITIMER_REAL, &zero_timer, NULL);
131 exportmeshgraph_started = false;
136 static bool exportmeshgraph_begin(const char* timeout_str)
141 if(exportmeshgraph_started)
143 if(!exportmeshgraph_end(NULL))
148 int timeout = atoi(timeout_str);
153 int timeout_sec = timeout / 1000;
154 int timeout_msec = timeout % 1000;
156 /* Install timer_handler as the signal handler for SIGALRM. */
157 signal(SIGALRM, exportmeshgraph_timer);
159 /* Configure the timer to expire immediately... */
160 struct itimerval timer;
161 timer.it_value.tv_sec = 0;
162 timer.it_value.tv_usec = 1000;
164 /* ... and every X msec after that. */
165 timer.it_interval.tv_sec = timeout_sec;
166 timer.it_interval.tv_usec = timeout_msec * 1000;
168 /* Start a real timer. */
169 setitimer (ITIMER_REAL, &timer, NULL);
171 exportmeshgraph_started = true;
176 static void parse_command(char *buf) {
177 char *arg = strchr(buf, ' ');
181 if(!strcasecmp(buf, "invite")) {
185 fprintf(stderr, "/invite requires an argument!\n");
189 invitation = meshlink_invite(mesh[nodeindex], arg);
191 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
195 printf("Invitation for %s: %s\n", arg, invitation);
197 } else if(!strcasecmp(buf, "join")) {
199 fprintf(stderr, "/join requires an argument!\n");
202 meshlink_stop(mesh[nodeindex]);
203 if(!meshlink_join(mesh[nodeindex], arg))
204 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
206 fprintf(stderr, "Invitation accepted!\n");
207 meshlink_start(mesh[nodeindex]);
209 } else if(!strcasecmp(buf, "kick")) {
211 fprintf(stderr, "/kick requires an argument!\n");
215 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
217 fprintf(stderr, "Unknown node '%s'\n", arg);
221 meshlink_blacklist(mesh[nodeindex], node);
223 printf("Node '%s' blacklisted.\n", arg);
224 } else if(!strcasecmp(buf, "who")) {
226 nodes = meshlink_get_all_nodes(mesh[nodeindex], nodes, &nnodes);
228 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
230 printf("%zu known nodes:", nnodes);
231 for(int i = 0; i < nnodes; i++)
232 printf(" %s", nodes[i]->name);
236 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
238 fprintf(stderr, "Unknown node '%s'\n", arg);
240 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[nodeindex], node));
243 } else if(!strcasecmp(buf, "link")) {
245 } else if(!strcasecmp(buf, "eg")) {
246 exportmeshgraph(arg);
247 } else if(!strcasecmp(buf, "egb")) {
248 exportmeshgraph_begin(arg);
249 } else if(!strcasecmp(buf, "ege")) {
250 exportmeshgraph_end(NULL);
251 } else if(!strcasecmp(buf, "test")) {
253 } else if(!strcasecmp(buf, "select")) {
255 fprintf(stderr, "/select requires an argument!\n");
258 nodeindex = atoi(arg);
259 printf("Index is now %d\n",nodeindex);
261 } else if(!strcasecmp(buf, "quit")) {
264 } else if(!strcasecmp(buf, "help")) {
266 "<name>: <message> Send a message to the given node.\n"
267 " Subsequent messages don't need the <name>: prefix.\n"
268 "/invite <name> Create an invitation for a new node.\n"
269 "/join <invitation> Join an existing mesh using an invitation.\n"
270 "/kick <name> Blacklist the given node.\n"
271 "/who [<name>] List all nodes or show information about the given node.\n"
272 "/link Link all nodes together.\n"
273 "/eg <path> Export graph as json file.\n"
274 "/test Test functionality sending some data to all nodes\n"
275 "/select <number> Select the active node running the user commands\n"
276 "/quit Exit this program.\n"
279 fprintf(stderr, "Unknown command '/%s'\n", buf);
283 static void parse_input(char *buf) {
284 static meshlink_node_t *destination;
294 if(len && buf[len - 1] == '\n')
297 if(len && buf[len - 1] == '\r')
300 // Ignore empty lines.
305 // Commands start with '/'
308 return parse_command(buf + 1);
310 // Lines in the form "name: message..." set the destination node.
313 char *colon = strchr(buf, ':');
321 destination = meshlink_get_node(mesh[nodeindex], buf);
323 fprintf(stderr, "Unknown node '%s'\n", buf);
329 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
333 if(!meshlink_send(mesh[nodeindex], destination, msg, strlen(msg) + 1)) {
334 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
338 printf("Message sent to '%s'.\n", destination->name);
341 int main(int argc, char *argv[]) {
342 const char *basebase = ".manynodes";
343 const char *namesprefix = "machine1";
344 const char *graphexporttimeout = NULL;
351 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]);
359 namesprefix = argv[3];
362 graphexporttimeout = argv[4];
364 mesh = calloc(n, sizeof *mesh);
366 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_message);
367 mkdir(basebase, 0750);
369 char filename[PATH_MAX];
371 for(int i = 0; i < n; i++) {
372 snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i);
373 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
374 bool itsnew = access(filename, R_OK);
375 mesh[i] = meshlink_open(filename, nodename, "manynodes", i%_DEV_CLASS_MAX);
376 meshlink_set_log_cb(mesh[i], MESHLINK_WARNING, log_message);
378 fprintf(stderr, "errno is: %d\n", meshlink_errno);
379 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
386 for(int i = 0; i < n; i++) {
387 if(!meshlink_start(mesh[i]))
388 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
394 fprintf(stderr, "Could not start any node!\n");
398 if(graphexporttimeout)
399 { exportmeshgraph_begin(graphexporttimeout); }
401 printf("%d nodes started.\nType /help for a list of commands.\n", started);
404 while(fgets(buf, sizeof buf, stdin))
407 exportmeshgraph_end(NULL);
409 printf("Nodes stopping.\n");
411 for(int i = 0; i < n; i++)
412 meshlink_close(mesh[i]);