7 #include <linux/limits.h>
9 #include "../src/meshlink.h"
12 static meshlink_handle_t **mesh;
14 static meshlink_node_t **nodes;
17 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
18 const char *levelstr[] = {
19 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
20 [MESHLINK_INFO] = "\x1b[32mINFO",
21 [MESHLINK_WARNING] = "\x1b[33mWARNING",
22 [MESHLINK_ERROR] = "\x1b[31mERROR",
23 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
25 fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global",levelstr[level], text);
28 //Test mesh sending data
29 static void testmesh () {
31 for(int nindex = 0; nindex < n; nindex++) {
33 nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
35 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
37 printf("%zu known nodes:\n", nnodes);
38 for(int i = 0; i < nnodes; i++) {
39 //printf(" %s\n", nodes[i]->name);
40 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
41 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
49 // Make all nodes know about each other by importing each others public keys and addresses.
50 static void linkmesh() {
51 for(int i = 0; i < n; i++) {
52 char *datai = meshlink_export(mesh[i]);
54 for(int j = i + 1; j < n; j++) {
55 char *dataj = meshlink_export(mesh[j]);
56 meshlink_import(mesh[i], dataj);
57 meshlink_import(mesh[j], datai);
65 static void parse_command(char *buf) {
66 char *arg = strchr(buf, ' ');
70 if(!strcasecmp(buf, "invite")) {
74 fprintf(stderr, "/invite requires an argument!\n");
78 invitation = meshlink_invite(mesh[0], arg);
80 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
84 printf("Invitation for %s: %s\n", arg, invitation);
86 } else if(!strcasecmp(buf, "join")) {
88 fprintf(stderr, "/join requires an argument!\n");
91 meshlink_stop(mesh[0]);
92 if(!meshlink_join(mesh[0], arg))
93 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
95 fprintf(stderr, "Invitation accepted!\n");
96 meshlink_start(mesh[0]);
98 } else if(!strcasecmp(buf, "kick")) {
100 fprintf(stderr, "/kick requires an argument!\n");
104 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
106 fprintf(stderr, "Unknown node '%s'\n", arg);
110 meshlink_blacklist(mesh[0], node);
112 printf("Node '%s' blacklisted.\n", arg);
113 } else if(!strcasecmp(buf, "who")) {
115 nodes = meshlink_get_all_nodes(mesh[0], nodes, &nnodes);
117 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
119 printf("%zu known nodes:", nnodes);
120 for(int i = 0; i < nnodes; i++)
121 printf(" %s", nodes[i]->name);
125 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
127 fprintf(stderr, "Unknown node '%s'\n", arg);
129 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[0], node));
132 } else if(!strcasecmp(buf, "link")) {
134 } else if(!strcasecmp(buf, "test")) {
136 } else if(!strcasecmp(buf, "quit")) {
139 } else if(!strcasecmp(buf, "help")) {
141 "<name>: <message> Send a message to the given node.\n"
142 " Subsequent messages don't need the <name>: prefix.\n"
143 "/invite <name> Create an invitation for a new node.\n"
144 "/join <invitation> Join an existing mesh using an invitation.\n"
145 "/kick <name> Blacklist the given node.\n"
146 "/who [<name>] List all nodes or show information about the given node.\n"
147 "/link Link all nodes together.\n"
148 "/test Test functionality sending some data to all nodes\n"
149 "/quit Exit this program.\n"
152 fprintf(stderr, "Unknown command '/%s'\n", buf);
156 static void parse_input(char *buf) {
157 static meshlink_node_t *destination;
167 if(len && buf[len - 1] == '\n')
170 if(len && buf[len - 1] == '\r')
173 // Ignore empty lines.
178 // Commands start with '/'
181 return parse_command(buf + 1);
183 // Lines in the form "name: message..." set the destination node.
186 char *colon = strchr(buf, ':');
194 destination = meshlink_get_node(mesh[0], buf);
196 fprintf(stderr, "Unknown node '%s'\n", buf);
202 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
206 if(!meshlink_send(mesh[0], destination, msg, strlen(msg) + 1)) {
207 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
211 printf("Message sent to '%s'.\n", destination->name);
214 int main(int argc, char *argv[]) {
215 const char *basebase = ".manynodes";
216 const char *namesprefix = "machine1";
223 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames]\n", argv[0]);
231 namesprefix = argv[3];
233 mesh = calloc(n, sizeof *mesh);
235 meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
236 mkdir(basebase, 0750);
238 char filename[PATH_MAX];
240 for(int i = 0; i < n; i++) {
241 snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i);
242 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
243 bool itsnew = access(filename, R_OK);
244 mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE);
245 meshlink_set_log_cb(mesh[i], MESHLINK_INFO, log_message);
247 meshlink_add_address(mesh[i], "localhost");
249 fprintf(stderr, "errno is: %d\n", meshlink_errno);
250 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
257 for(int i = 0; i < n; i++) {
258 if(!meshlink_start(mesh[i]))
259 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
265 fprintf(stderr, "Could not start any node!\n");
269 printf("%d nodes started.\nType /help for a list of commands.\n", started);
271 while(fgets(buf, sizeof buf, stdin))
274 printf("Nodes stopping.\n");
276 for(int i = 0; i < n; i++)
277 meshlink_close(mesh[i]);