7 #include <linux/limits.h>
9 #include "../src/meshlink.h"
10 #include "../src/devtools.h"
12 #include <sys/types.h>
17 static meshlink_handle_t **mesh;
19 static meshlink_node_t **nodes;
22 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
23 const char *levelstr[] = {
24 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
25 [MESHLINK_INFO] = "\x1b[32mINFO",
26 [MESHLINK_WARNING] = "\x1b[33mWARNING",
27 [MESHLINK_ERROR] = "\x1b[31mERROR",
28 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
30 fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global",levelstr[level], text);
33 //Test mesh sending data
34 static void testmesh () {
36 for(int nindex = 0; nindex < n; nindex++) {
38 nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
40 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
42 printf("%zu known nodes:\n", nnodes);
43 for(int i = 0; i < nnodes; i++) {
44 //printf(" %s\n", nodes[i]->name);
45 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
46 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
54 // Make all nodes know about each other by importing each others public keys and addresses.
55 static void linkmesh() {
56 for(int i = 0; i < n; i++) {
57 char *datai = meshlink_export(mesh[i]);
59 for(int j = i + 1; j < n; j++) {
60 char *dataj = meshlink_export(mesh[j]);
61 meshlink_import(mesh[i], dataj);
62 meshlink_import(mesh[j], datai);
70 static bool exportmeshgraph(const char* path)
73 int psr = stat(path, &ps);
75 if(psr == 0 || errno != ENOENT)
80 { fprintf(stderr, "%s exists already\n", path); }
85 FILE* stream = fopen(path, "w");
93 if(!devtool_export_json_all_edges_state(mesh[0], stream))
96 fprintf(stderr, "could not export graph\n");
104 static void parse_command(char *buf) {
105 char *arg = strchr(buf, ' ');
109 if(!strcasecmp(buf, "invite")) {
113 fprintf(stderr, "/invite requires an argument!\n");
117 invitation = meshlink_invite(mesh[0], arg);
119 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
123 printf("Invitation for %s: %s\n", arg, invitation);
125 } else if(!strcasecmp(buf, "join")) {
127 fprintf(stderr, "/join requires an argument!\n");
130 meshlink_stop(mesh[0]);
131 if(!meshlink_join(mesh[0], arg))
132 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
134 fprintf(stderr, "Invitation accepted!\n");
135 meshlink_start(mesh[0]);
137 } else if(!strcasecmp(buf, "kick")) {
139 fprintf(stderr, "/kick requires an argument!\n");
143 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
145 fprintf(stderr, "Unknown node '%s'\n", arg);
149 meshlink_blacklist(mesh[0], node);
151 printf("Node '%s' blacklisted.\n", arg);
152 } else if(!strcasecmp(buf, "who")) {
154 nodes = meshlink_get_all_nodes(mesh[0], nodes, &nnodes);
156 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
158 printf("%zu known nodes:", nnodes);
159 for(int i = 0; i < nnodes; i++)
160 printf(" %s", nodes[i]->name);
164 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
166 fprintf(stderr, "Unknown node '%s'\n", arg);
168 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[0], node));
171 } else if(!strcasecmp(buf, "link")) {
173 } else if(!strcasecmp(buf, "eg")) {
174 exportmeshgraph(arg);
175 } else if(!strcasecmp(buf, "test")) {
177 } else if(!strcasecmp(buf, "quit")) {
180 } else if(!strcasecmp(buf, "help")) {
182 "<name>: <message> Send a message to the given node.\n"
183 " Subsequent messages don't need the <name>: prefix.\n"
184 "/invite <name> Create an invitation for a new node.\n"
185 "/join <invitation> Join an existing mesh using an invitation.\n"
186 "/kick <name> Blacklist the given node.\n"
187 "/who [<name>] List all nodes or show information about the given node.\n"
188 "/link Link all nodes together.\n"
189 "/eg <path> Export graph as json file.\n"
190 "/test Test functionality sending some data to all nodes\n"
191 "/quit Exit this program.\n"
194 fprintf(stderr, "Unknown command '/%s'\n", buf);
198 static void parse_input(char *buf) {
199 static meshlink_node_t *destination;
209 if(len && buf[len - 1] == '\n')
212 if(len && buf[len - 1] == '\r')
215 // Ignore empty lines.
220 // Commands start with '/'
223 return parse_command(buf + 1);
225 // Lines in the form "name: message..." set the destination node.
228 char *colon = strchr(buf, ':');
236 destination = meshlink_get_node(mesh[0], buf);
238 fprintf(stderr, "Unknown node '%s'\n", buf);
244 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
248 if(!meshlink_send(mesh[0], destination, msg, strlen(msg) + 1)) {
249 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
253 printf("Message sent to '%s'.\n", destination->name);
256 int main(int argc, char *argv[]) {
257 const char *basebase = ".manynodes";
258 const char *namesprefix = "machine1";
265 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames]\n", argv[0]);
273 namesprefix = argv[3];
275 mesh = calloc(n, sizeof *mesh);
277 meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
278 mkdir(basebase, 0750);
280 char filename[PATH_MAX];
282 for(int i = 0; i < n; i++) {
283 snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i);
284 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
285 bool itsnew = access(filename, R_OK);
286 mesh[i] = meshlink_open(filename, nodename, "manynodes", i%_DEV_CLASS_MAX);
287 meshlink_set_log_cb(mesh[i], MESHLINK_INFO, log_message);
289 meshlink_add_address(mesh[i], "localhost");
291 fprintf(stderr, "errno is: %d\n", meshlink_errno);
292 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
299 for(int i = 0; i < n; i++) {
300 if(!meshlink_start(mesh[i]))
301 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
307 fprintf(stderr, "Could not start any node!\n");
311 printf("%d nodes started.\nType /help for a list of commands.\n", started);
313 while(fgets(buf, sizeof buf, stdin))
316 printf("Nodes stopping.\n");
318 for(int i = 0; i < n; i++)
319 meshlink_close(mesh[i]);