8 #if !defined(_WIN32) && !defined(__APPLE__)
9 #include <linux/limits.h>
10 #elif defined(__APPLE__)
11 #include <sys/syslimits.h>
14 #include "../src/meshlink-tiny.h"
15 #include "../src/devtools.h"
17 #include <sys/types.h>
26 static meshlink_handle_t **meshes;
27 static const 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(void) {
46 for(int nindex = 0; nindex < n; nindex++) {
48 nodes = meshlink_get_all_nodes(meshes[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);
55 for(size_t i = 0; i < nnodes; i++) {
56 //printf(" %s\n", nodes[i]->name);
57 if(!meshlink_send(meshes[nindex], nodes[i], "magic", strlen("magic") + 1)) {
58 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
66 // Make all nodes know about each other by importing each others public keys and addresses.
67 static void linkmesh(void) {
68 for(int i = 0; i < n; i++) {
69 char *datai = meshlink_export(meshes[i]);
71 for(int j = i + 1; j < n; j++) {
72 char *dataj = meshlink_export(meshes[j]);
74 if(!meshlink_import(meshes[i], dataj) || !meshlink_import(meshes[j], datai)) {
75 fprintf(stderr, "Could not exchange keys between %s and %s: %s\n", meshes[i]->name, meshes[j]->name, meshlink_strerror(meshlink_errno));
85 static bool exportmeshgraph(const char *path) {
89 int psr = stat(path, &ps);
91 if(psr == 0 || errno != ENOENT) {
95 fprintf(stderr, "%s exists already\n", path);
101 FILE *stream = fopen(path, "w");
108 if(!devtool_export_json_all_edges_state(meshes[0], stream)) {
110 fprintf(stderr, "could not export graph\n");
119 static void exportmeshgraph_timer(int signum) {
123 gettimeofday(&ts, NULL);
126 snprintf(name, sizeof(name), "%sgraph_%ld_%03ld.json", namesprefix, ts.tv_sec, ts.tv_usec / 1000L);
128 exportmeshgraph(name);
132 static bool exportmeshgraph_started = false;
134 static bool exportmeshgraph_end(void) {
135 if(!exportmeshgraph_started) {
139 struct itimerval zero_timer;
141 setitimer(ITIMER_REAL, &zero_timer, NULL);
143 exportmeshgraph_started = false;
148 static bool exportmeshgraph_begin(const char *timeout_str) {
153 if(exportmeshgraph_started) {
154 if(!exportmeshgraph_end()) {
160 int timeout = atoi(timeout_str);
166 int timeout_sec = timeout / 1000;
167 int timeout_msec = timeout % 1000;
169 /* Install timer_handler as the signal handler for SIGALRM. */
170 signal(SIGALRM, exportmeshgraph_timer);
172 /* Configure the timer to expire immediately... */
173 struct itimerval timer;
174 timer.it_value.tv_sec = 0;
175 timer.it_value.tv_usec = 1000;
177 /* ... and every X msec after that. */
178 timer.it_interval.tv_sec = timeout_sec;
179 timer.it_interval.tv_usec = timeout_msec * 1000;
181 /* Start a real timer. */
182 setitimer(ITIMER_REAL, &timer, NULL);
184 exportmeshgraph_started = true;
189 static bool exportmeshgraph_end(void) {
193 static bool exportmeshgraph_begin(const char *timeout_str) {
198 static void parse_command(char *buf) {
199 char *arg = strchr(buf, ' ');
205 if(!strcasecmp(buf, "join")) {
207 fprintf(stderr, "/join requires an argument!\n");
211 meshlink_stop(meshes[nodeindex]);
213 if(!meshlink_join(meshes[nodeindex], arg)) {
214 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
216 fprintf(stderr, "Invitation accepted!\n");
219 if(!meshlink_start(meshes[nodeindex])) {
220 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
223 } else if(!strcasecmp(buf, "who")) {
225 nodes = meshlink_get_all_nodes(meshes[nodeindex], nodes, &nnodes);
228 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
230 printf("%zu known nodes:", nnodes);
232 for(size_t i = 0; i < nnodes; i++) {
233 printf(" %s", nodes[i]->name);
239 meshlink_node_t *node = meshlink_get_node(meshes[nodeindex], arg);
242 fprintf(stderr, "Unknown node '%s'\n", arg);
244 printf("Node %s found, pmtu %ld\n", arg, (long int)meshlink_get_pmtu(meshes[nodeindex], node));
247 } else if(!strcasecmp(buf, "link")) {
249 } else if(!strcasecmp(buf, "eg")) {
250 exportmeshgraph(arg);
251 } else if(!strcasecmp(buf, "egb")) {
252 exportmeshgraph_begin(arg);
253 } else if(!strcasecmp(buf, "ege")) {
254 exportmeshgraph_end();
255 } else if(!strcasecmp(buf, "test")) {
257 } else if(!strcasecmp(buf, "select")) {
259 fprintf(stderr, "/select requires an argument!\n");
263 nodeindex = atoi(arg);
264 printf("Index is now %d\n", nodeindex);
265 } else if(!strcasecmp(buf, "stop")) {
266 meshlink_stop(meshes[nodeindex]);
267 } else if(!strcasecmp(buf, "quit")) {
270 } else if(!strcasecmp(buf, "help")) {
272 "<name>: <message> Send a message to the given node.\n"
273 " Subsequent messages don't need the <name>: prefix.\n"
274 "/join <invitation> Join an existing mesh using an invitation.\n"
275 "/kick <name> Blacklist the given node.\n"
276 "/who [<name>] List all nodes or show information about the given node.\n"
277 "/link Link all nodes together.\n"
278 "/eg <path> Export graph as json file.\n"
279 "/test Test functionality sending some data to all nodes\n"
280 "/select <number> Select the active node running the user commands\n"
281 "/stop Call meshlink_stop, use /select first to select which node to stop\n"
282 "/quit Exit this program.\n"
285 fprintf(stderr, "Unknown command '/%s'\n", buf);
289 static void parse_input(char *buf) {
290 static meshlink_node_t *destination;
301 if(len && buf[len - 1] == '\n') {
305 if(len && buf[len - 1] == '\r') {
309 // Ignore empty lines.
315 // Commands start with '/'
318 parse_command(buf + 1);
322 // Lines in the form "name: message..." set the destination node.
325 char *colon = strchr(buf, ':');
335 destination = meshlink_get_node(meshes[nodeindex], buf);
338 fprintf(stderr, "Unknown node '%s'\n", buf);
344 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
348 if(!meshlink_send(meshes[nodeindex], destination, msg, strlen(msg) + 1)) {
349 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
353 printf("Message sent to '%s'.\n", destination->name);
356 int main(int argc, char *argv[]) {
357 const char *basebase = ".manynodes";
358 const char *graphexporttimeout = NULL;
366 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]);
375 namesprefix = argv[3];
379 graphexporttimeout = argv[4];
382 meshes = calloc(n, sizeof(*meshes));
384 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
386 mkdir(basebase, 0750);
391 char filename[PATH_MAX];
394 for(int i = 0; i < n; i++) {
395 snprintf(nodename, sizeof(nodename), "%snode%d", namesprefix, i);
396 snprintf(filename, sizeof(filename), "%s/%s", basebase, nodename);
398 if(n / (i + 1) > n / 4) {
399 meshes[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE);
401 meshes[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE);
404 meshlink_set_log_cb(meshes[i], MESHLINK_DEBUG, log_message);
407 fprintf(stderr, "errno is: %d\n", meshlink_errno);
408 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
415 for(int i = 0; i < n; i++) {
416 if(!meshlink_start(meshes[i])) {
417 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
424 fprintf(stderr, "Could not start any node!\n");
428 if(graphexporttimeout) {
429 exportmeshgraph_begin(graphexporttimeout);
432 printf("%d nodes started.\nType /help for a list of commands.\n", started);
435 while(fgets(buf, sizeof(buf), stdin)) {
439 exportmeshgraph_end();
441 printf("Nodes stopping.\n");
443 for(int i = 0; i < n; i++) {
444 meshlink_close(meshes[i]);