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));
63 // Make all nodes know about each other by importing each others public keys and addresses.
64 static void linkmesh() {
65 for(int i = 0; i < n; i++) {
66 char *datai = meshlink_export(mesh[i]);
68 for(int j = i + 1; j < n; j++) {
69 char *dataj = meshlink_export(mesh[j]);
70 meshlink_import(mesh[i], dataj);
71 meshlink_import(mesh[j], datai);
79 static bool exportmeshgraph(const char *path) {
81 int psr = stat(path, &ps);
83 if(psr == 0 || errno != ENOENT) {
87 fprintf(stderr, "%s exists already\n", path);
92 FILE *stream = fopen(path, "w");
99 if(!devtool_export_json_all_edges_state(mesh[0], stream)) {
101 fprintf(stderr, "could not export graph\n");
110 void exportmeshgraph_timer(int signum) {
112 gettimeofday(&ts, NULL);
115 snprintf(name, sizeof(name), "%sgraph_%ld_%03ld.json", namesprefix, ts.tv_sec, ts.tv_usec / 1000);
117 exportmeshgraph(name);
121 static bool exportmeshgraph_started = false;
123 static bool exportmeshgraph_end(const char *none) {
124 if(!exportmeshgraph_started)
127 struct itimerval zero_timer = { 0 };
128 setitimer(ITIMER_REAL, &zero_timer, NULL);
130 exportmeshgraph_started = false;
135 static bool exportmeshgraph_begin(const char *timeout_str) {
139 if(exportmeshgraph_started) {
140 if(!exportmeshgraph_end(NULL))
145 int timeout = atoi(timeout_str);
150 int timeout_sec = timeout / 1000;
151 int timeout_msec = timeout % 1000;
153 /* Install timer_handler as the signal handler for SIGALRM. */
154 signal(SIGALRM, exportmeshgraph_timer);
156 /* Configure the timer to expire immediately... */
157 struct itimerval timer;
158 timer.it_value.tv_sec = 0;
159 timer.it_value.tv_usec = 1000;
161 /* ... and every X msec after that. */
162 timer.it_interval.tv_sec = timeout_sec;
163 timer.it_interval.tv_usec = timeout_msec * 1000;
165 /* Start a real timer. */
166 setitimer(ITIMER_REAL, &timer, NULL);
168 exportmeshgraph_started = true;
173 static bool exportmeshgraph_end(const char *none) {
177 static bool exportmeshgraph_begin(const char *timeout_str) {
182 static void parse_command(char *buf) {
183 char *arg = strchr(buf, ' ');
187 if(!strcasecmp(buf, "invite")) {
191 fprintf(stderr, "/invite requires an argument!\n");
195 invitation = meshlink_invite(mesh[nodeindex], arg);
197 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
201 printf("Invitation for %s: %s\n", arg, invitation);
203 } else if(!strcasecmp(buf, "join")) {
205 fprintf(stderr, "/join requires an argument!\n");
208 meshlink_stop(mesh[nodeindex]);
209 if(!meshlink_join(mesh[nodeindex], arg))
210 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
212 fprintf(stderr, "Invitation accepted!\n");
213 if(!meshlink_start(mesh[nodeindex])) {
214 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
217 } else if(!strcasecmp(buf, "kick")) {
219 fprintf(stderr, "/kick requires an argument!\n");
223 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
225 fprintf(stderr, "Unknown node '%s'\n", arg);
229 meshlink_blacklist(mesh[nodeindex], node);
231 printf("Node '%s' blacklisted.\n", arg);
232 } else if(!strcasecmp(buf, "who")) {
234 nodes = meshlink_get_all_nodes(mesh[nodeindex], nodes, &nnodes);
236 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
238 printf("%zu known nodes:", nnodes);
239 for(int i = 0; i < nnodes; i++)
240 printf(" %s", nodes[i]->name);
244 meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
246 fprintf(stderr, "Unknown node '%s'\n", arg);
248 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[nodeindex], node));
250 } else if(!strcasecmp(buf, "link"))
252 else if(!strcasecmp(buf, "eg"))
253 exportmeshgraph(arg);
254 else if(!strcasecmp(buf, "egb"))
255 exportmeshgraph_begin(arg);
256 else if(!strcasecmp(buf, "ege"))
257 exportmeshgraph_end(NULL);
258 else if(!strcasecmp(buf, "test"))
260 else if(!strcasecmp(buf, "select")) {
262 fprintf(stderr, "/select requires an argument!\n");
265 nodeindex = atoi(arg);
266 printf("Index is now %d\n", nodeindex);
267 } else if(!strcasecmp(buf, "stop"))
268 meshlink_stop(mesh[nodeindex]);
269 else if(!strcasecmp(buf, "quit")) {
272 } else if(!strcasecmp(buf, "help")) {
274 "<name>: <message> Send a message to the given node.\n"
275 " Subsequent messages don't need the <name>: prefix.\n"
276 "/invite <name> Create an invitation for a new node.\n"
277 "/join <invitation> Join an existing mesh using an invitation.\n"
278 "/kick <name> Blacklist the given node.\n"
279 "/who [<name>] List all nodes or show information about the given node.\n"
280 "/link Link all nodes together.\n"
281 "/eg <path> Export graph as json file.\n"
282 "/test Test functionality sending some data to all nodes\n"
283 "/select <number> Select the active node running the user commands\n"
284 "/stop Call meshlink_stop, use /select first to select which node to stop\n"
285 "/quit Exit this program.\n"
288 fprintf(stderr, "Unknown command '/%s'\n", buf);
291 static void parse_input(char *buf) {
292 static meshlink_node_t *destination;
302 if(len && buf[len - 1] == '\n')
305 if(len && buf[len - 1] == '\r')
308 // Ignore empty lines.
313 // Commands start with '/'
316 return parse_command(buf + 1);
318 // Lines in the form "name: message..." set the destination node.
321 char *colon = strchr(buf, ':');
329 destination = meshlink_get_node(mesh[nodeindex], buf);
331 fprintf(stderr, "Unknown node '%s'\n", buf);
337 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
341 if(!meshlink_send(mesh[nodeindex], destination, msg, strlen(msg) + 1)) {
342 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
346 printf("Message sent to '%s'.\n", destination->name);
349 int main(int argc, char *argv[]) {
350 const char *basebase = ".manynodes";
351 const char *graphexporttimeout = NULL;
358 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]);
366 namesprefix = argv[3];
369 graphexporttimeout = argv[4];
371 mesh = calloc(n, sizeof(*mesh));
373 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
375 mkdir(basebase, 0750);
380 char filename[PATH_MAX];
382 for(int i = 0; i < n; i++) {
383 snprintf(nodename, sizeof(nodename), "%snode%d", namesprefix, i);
384 snprintf(filename, sizeof(filename), "%s/%s", basebase, nodename);
385 bool itsnew = access(filename, R_OK);
386 if(n / (i + 1) > n / 4)
387 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE);
389 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE);
390 meshlink_set_log_cb(mesh[i], MESHLINK_DEBUG, log_message);
392 fprintf(stderr, "errno is: %d\n", meshlink_errno);
393 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
400 for(int i = 0; i < n; i++) {
401 if(!meshlink_start(mesh[i]))
402 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
408 fprintf(stderr, "Could not start any node!\n");
412 if(graphexporttimeout)
413 exportmeshgraph_begin(graphexporttimeout);
415 printf("%d nodes started.\nType /help for a list of commands.\n", started);
418 while(fgets(buf, sizeof(buf), stdin))
421 exportmeshgraph_end(NULL);
423 printf("Nodes stopping.\n");
425 for(int i = 0; i < n; i++)
426 meshlink_close(mesh[i]);