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);
51 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
53 printf("%zu known nodes:\n", nnodes);
54 for(int i = 0; i < nnodes; i++) {
55 //printf(" %s\n", nodes[i]->name);
56 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1))
57 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) {
84 int psr = stat(path, &ps);
86 if(psr == 0 || errno != ENOENT) {
90 fprintf(stderr, "%s exists already\n", path);
95 FILE *stream = fopen(path, "w");
102 if(!devtool_export_json_all_edges_state(mesh[0], stream)) {
104 fprintf(stderr, "could not export graph\n");
113 void exportmeshgraph_timer(int signum) {
115 gettimeofday(&ts, NULL);
118 snprintf(name, sizeof(name), "%sgraph_%ld_%03ld.json", namesprefix, ts.tv_sec, ts.tv_usec / 1000);
120 exportmeshgraph(name);
124 static bool exportmeshgraph_started = false;
126 static bool exportmeshgraph_end(const char *none) {
127 if(!exportmeshgraph_started)
130 struct itimerval zero_timer = { 0 };
131 setitimer(ITIMER_REAL, &zero_timer, NULL);
133 exportmeshgraph_started = false;
138 static bool exportmeshgraph_begin(const char *timeout_str) {
142 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 bool exportmeshgraph_end(const char *none) {
180 static bool exportmeshgraph_begin(const char *timeout_str) {
185 static void parse_command(char *buf) {
186 char *arg = strchr(buf, ' ');
190 if(!strcasecmp(buf, "invite")) {
194 fprintf(stderr, "/invite requires an argument!\n");
198 invitation = meshlink_invite(mesh[nodeindex], arg);
200 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
204 printf("Invitation for %s: %s\n", arg, invitation);
206 } else if(!strcasecmp(buf, "join")) {
208 fprintf(stderr, "/join requires an argument!\n");
211 meshlink_stop(mesh[nodeindex]);
212 if(!meshlink_join(mesh[nodeindex], arg))
213 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
215 fprintf(stderr, "Invitation accepted!\n");
216 if(!meshlink_start(mesh[nodeindex])) {
217 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
220 } else if(!strcasecmp(buf, "kick")) {
222 fprintf(stderr, "/kick requires an argument!\n");
226 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
228 fprintf(stderr, "Unknown node '%s'\n", arg);
232 meshlink_blacklist(mesh[nodeindex], node);
234 printf("Node '%s' blacklisted.\n", arg);
235 } else if(!strcasecmp(buf, "who")) {
237 nodes = meshlink_get_all_nodes(mesh[nodeindex], nodes, &nnodes);
239 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
241 printf("%zu known nodes:", nnodes);
242 for(int i = 0; i < nnodes; i++)
243 printf(" %s", nodes[i]->name);
247 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
249 fprintf(stderr, "Unknown node '%s'\n", arg);
251 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[nodeindex], node));
253 } else if(!strcasecmp(buf, "link"))
255 else if(!strcasecmp(buf, "eg"))
256 exportmeshgraph(arg);
257 else if(!strcasecmp(buf, "egb"))
258 exportmeshgraph_begin(arg);
259 else if(!strcasecmp(buf, "ege"))
260 exportmeshgraph_end(NULL);
261 else if(!strcasecmp(buf, "test"))
263 else if(!strcasecmp(buf, "select")) {
265 fprintf(stderr, "/select requires an argument!\n");
268 nodeindex = atoi(arg);
269 printf("Index is now %d\n", nodeindex);
270 } else if(!strcasecmp(buf, "stop"))
271 meshlink_stop(mesh[nodeindex]);
272 else if(!strcasecmp(buf, "quit")) {
275 } else if(!strcasecmp(buf, "help")) {
277 "<name>: <message> Send a message to the given node.\n"
278 " Subsequent messages don't need the <name>: prefix.\n"
279 "/invite <name> Create an invitation for a new node.\n"
280 "/join <invitation> Join an existing mesh using an invitation.\n"
281 "/kick <name> Blacklist the given node.\n"
282 "/who [<name>] List all nodes or show information about the given node.\n"
283 "/link Link all nodes together.\n"
284 "/eg <path> Export graph as json file.\n"
285 "/test Test functionality sending some data to all nodes\n"
286 "/select <number> Select the active node running the user commands\n"
287 "/stop Call meshlink_stop, use /select first to select which node to stop\n"
288 "/quit Exit this program.\n"
291 fprintf(stderr, "Unknown command '/%s'\n", buf);
294 static void parse_input(char *buf) {
295 static meshlink_node_t *destination;
305 if(len && buf[len - 1] == '\n')
308 if(len && buf[len - 1] == '\r')
311 // Ignore empty lines.
316 // Commands start with '/'
319 return parse_command(buf + 1);
321 // Lines in the form "name: message..." set the destination node.
324 char *colon = strchr(buf, ':');
332 destination = meshlink_get_node(mesh[nodeindex], buf);
334 fprintf(stderr, "Unknown node '%s'\n", buf);
340 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
344 if(!meshlink_send(mesh[nodeindex], destination, msg, strlen(msg) + 1)) {
345 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
349 printf("Message sent to '%s'.\n", destination->name);
352 int main(int argc, char *argv[]) {
353 const char *basebase = ".manynodes";
354 const char *graphexporttimeout = NULL;
361 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]);
369 namesprefix = argv[3];
372 graphexporttimeout = argv[4];
374 mesh = calloc(n, sizeof(*mesh));
376 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
378 mkdir(basebase, 0750);
383 char filename[PATH_MAX];
385 for(int i = 0; i < n; i++) {
386 snprintf(nodename, sizeof(nodename), "%snode%d", namesprefix, i);
387 snprintf(filename, sizeof(filename), "%s/%s", basebase, nodename);
388 if(n / (i + 1) > n / 4)
389 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE);
391 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE);
392 meshlink_set_log_cb(mesh[i], MESHLINK_DEBUG, log_message);
394 fprintf(stderr, "errno is: %d\n", meshlink_errno);
395 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
402 for(int i = 0; i < n; i++) {
403 if(!meshlink_start(mesh[i]))
404 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
410 fprintf(stderr, "Could not start any node!\n");
414 if(graphexporttimeout)
415 exportmeshgraph_begin(graphexporttimeout);
417 printf("%d nodes started.\nType /help for a list of commands.\n", started);
420 while(fgets(buf, sizeof(buf), stdin))
423 exportmeshgraph_end(NULL);
425 printf("Nodes stopping.\n");
427 for(int i = 0; i < n; i++)
428 meshlink_close(mesh[i]);