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 meshlink_node_t **nodes;
25 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
26 const char *levelstr[] = {
27 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
28 [MESHLINK_INFO] = "\x1b[32mINFO",
29 [MESHLINK_WARNING] = "\x1b[33mWARNING",
30 [MESHLINK_ERROR] = "\x1b[31mERROR",
31 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
33 fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global",levelstr[level], text);
36 //Test mesh sending data
37 static void testmesh () {
39 for(int nindex = 0; nindex < n; nindex++) {
41 nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
43 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
45 printf("%zu known nodes:\n", nnodes);
46 for(int i = 0; i < nnodes; i++) {
47 //printf(" %s\n", nodes[i]->name);
48 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
49 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
57 // Make all nodes know about each other by importing each others public keys and addresses.
58 static void linkmesh() {
59 for(int i = 0; i < n; i++) {
60 char *datai = meshlink_export(mesh[i]);
62 for(int j = i + 1; j < n; j++) {
63 char *dataj = meshlink_export(mesh[j]);
64 meshlink_import(mesh[i], dataj);
65 meshlink_import(mesh[j], datai);
73 static bool exportmeshgraph(const char* path)
76 int psr = stat(path, &ps);
78 if(psr == 0 || errno != ENOENT)
83 { fprintf(stderr, "%s exists already\n", path); }
88 FILE* stream = fopen(path, "w");
96 if(!devtool_export_json_all_edges_state(mesh[0], stream))
99 fprintf(stderr, "could not export graph\n");
108 void exportmeshgraph_timer(int signum)
111 gettimeofday(&ts, NULL);
114 snprintf(name, sizeof(name), "graph_%ld_%03ld.json", ts.tv_sec, ts.tv_usec/1000);
116 exportmeshgraph(name);
119 static bool exportmeshgraph_started = false;
121 static bool exportmeshgraph_end(const char* none)
123 if(!exportmeshgraph_started)
126 struct itimerval zero_timer = { 0 };
127 setitimer (ITIMER_REAL, &zero_timer, NULL);
129 exportmeshgraph_started = false;
134 static bool exportmeshgraph_begin(const char* timeout_str)
139 if(exportmeshgraph_started)
141 if(!exportmeshgraph_end(NULL))
146 int timeout = atoi(timeout_str);
151 int timeout_sec = timeout / 1000;
152 int timeout_msec = timeout % 1000;
154 /* Install timer_handler as the signal handler for SIGALRM. */
155 signal(SIGALRM, exportmeshgraph_timer);
157 /* Configure the timer to expire immediately... */
158 struct itimerval timer;
159 timer.it_value.tv_sec = 0;
160 timer.it_value.tv_usec = 1000;
162 /* ... and every X msec after that. */
163 timer.it_interval.tv_sec = timeout_sec;
164 timer.it_interval.tv_usec = timeout_msec * 1000;
166 /* Start a real timer. */
167 setitimer (ITIMER_REAL, &timer, NULL);
169 exportmeshgraph_started = true;
174 static void parse_command(char *buf) {
175 char *arg = strchr(buf, ' ');
179 if(!strcasecmp(buf, "invite")) {
183 fprintf(stderr, "/invite requires an argument!\n");
187 invitation = meshlink_invite(mesh[0], arg);
189 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
193 printf("Invitation for %s: %s\n", arg, invitation);
195 } else if(!strcasecmp(buf, "join")) {
197 fprintf(stderr, "/join requires an argument!\n");
200 meshlink_stop(mesh[0]);
201 if(!meshlink_join(mesh[0], arg))
202 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
204 fprintf(stderr, "Invitation accepted!\n");
205 meshlink_start(mesh[0]);
207 } else if(!strcasecmp(buf, "kick")) {
209 fprintf(stderr, "/kick requires an argument!\n");
213 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
215 fprintf(stderr, "Unknown node '%s'\n", arg);
219 meshlink_blacklist(mesh[0], node);
221 printf("Node '%s' blacklisted.\n", arg);
222 } else if(!strcasecmp(buf, "who")) {
224 nodes = meshlink_get_all_nodes(mesh[0], nodes, &nnodes);
226 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
228 printf("%zu known nodes:", nnodes);
229 for(int i = 0; i < nnodes; i++)
230 printf(" %s", nodes[i]->name);
234 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
236 fprintf(stderr, "Unknown node '%s'\n", arg);
238 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[0], node));
241 } else if(!strcasecmp(buf, "link")) {
243 } else if(!strcasecmp(buf, "eg")) {
244 exportmeshgraph(arg);
245 } else if(!strcasecmp(buf, "egb")) {
246 exportmeshgraph_begin(arg);
247 } else if(!strcasecmp(buf, "ege")) {
248 exportmeshgraph_end(NULL);
249 } else if(!strcasecmp(buf, "test")) {
251 } else if(!strcasecmp(buf, "quit")) {
254 } else if(!strcasecmp(buf, "help")) {
256 "<name>: <message> Send a message to the given node.\n"
257 " Subsequent messages don't need the <name>: prefix.\n"
258 "/invite <name> Create an invitation for a new node.\n"
259 "/join <invitation> Join an existing mesh using an invitation.\n"
260 "/kick <name> Blacklist the given node.\n"
261 "/who [<name>] List all nodes or show information about the given node.\n"
262 "/link Link all nodes together.\n"
263 "/eg <path> Export graph as json file.\n"
264 "/test Test functionality sending some data to all nodes\n"
265 "/quit Exit this program.\n"
268 fprintf(stderr, "Unknown command '/%s'\n", buf);
272 static void parse_input(char *buf) {
273 static meshlink_node_t *destination;
283 if(len && buf[len - 1] == '\n')
286 if(len && buf[len - 1] == '\r')
289 // Ignore empty lines.
294 // Commands start with '/'
297 return parse_command(buf + 1);
299 // Lines in the form "name: message..." set the destination node.
302 char *colon = strchr(buf, ':');
310 destination = meshlink_get_node(mesh[0], buf);
312 fprintf(stderr, "Unknown node '%s'\n", buf);
318 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
322 if(!meshlink_send(mesh[0], destination, msg, strlen(msg) + 1)) {
323 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
327 printf("Message sent to '%s'.\n", destination->name);
330 int main(int argc, char *argv[]) {
331 const char *basebase = ".manynodes";
332 const char *namesprefix = "machine1";
333 const char *graphexporttimeout = NULL;
340 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]);
348 namesprefix = argv[3];
351 graphexporttimeout = argv[4];
353 mesh = calloc(n, sizeof *mesh);
355 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_message);
356 mkdir(basebase, 0750);
358 char filename[PATH_MAX];
360 for(int i = 0; i < n; i++) {
361 snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i);
362 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
363 bool itsnew = access(filename, R_OK);
364 mesh[i] = meshlink_open(filename, nodename, "manynodes", i%_DEV_CLASS_MAX);
365 meshlink_set_log_cb(mesh[i], MESHLINK_WARNING, log_message);
367 meshlink_add_address(mesh[i], "localhost");
369 fprintf(stderr, "errno is: %d\n", meshlink_errno);
370 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
377 for(int i = 0; i < n; i++) {
378 if(!meshlink_start(mesh[i]))
379 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
385 fprintf(stderr, "Could not start any node!\n");
389 if(graphexporttimeout)
390 { exportmeshgraph_begin(graphexporttimeout); }
392 printf("%d nodes started.\nType /help for a list of commands.\n", started);
395 while(fgets(buf, sizeof buf, stdin))
398 exportmeshgraph_end(NULL);
400 printf("Nodes stopping.\n");
402 for(int i = 0; i < n; i++)
403 meshlink_close(mesh[i]);