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);
41 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
42 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
51 // Make all nodes know about each other by importing each others public keys and addresses.
52 static void linkmesh() {
53 for(int i = 0; i < n; i++) {
54 char *datai = meshlink_export(mesh[i]);
56 for(int j = i + 1; j < n; j++) {
57 char *dataj = meshlink_export(mesh[j]);
58 meshlink_import(mesh[i], dataj);
59 meshlink_import(mesh[j], datai);
67 static void parse_command(char *buf) {
68 char *arg = strchr(buf, ' ');
72 if(!strcasecmp(buf, "invite")) {
76 fprintf(stderr, "/invite requires an argument!\n");
80 invitation = meshlink_invite(mesh[0], arg);
82 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
86 printf("Invitation for %s: %s\n", arg, invitation);
88 } else if(!strcasecmp(buf, "join")) {
90 fprintf(stderr, "/join requires an argument!\n");
93 meshlink_stop(mesh[0]);
94 if(!meshlink_join(mesh[0], arg))
95 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
97 fprintf(stderr, "Invitation accepted!\n");
98 meshlink_start(mesh[0]);
100 } else if(!strcasecmp(buf, "kick")) {
102 fprintf(stderr, "/kick requires an argument!\n");
106 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
108 fprintf(stderr, "Unknown node '%s'\n", arg);
112 meshlink_blacklist(mesh[0], node);
114 printf("Node '%s' blacklisted.\n", arg);
115 } else if(!strcasecmp(buf, "who")) {
117 nodes = meshlink_get_all_nodes(mesh[0], nodes, &nnodes);
119 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
121 printf("%zu known nodes:", nnodes);
122 for(int i = 0; i < nnodes; i++)
123 printf(" %s", nodes[i]->name);
127 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
129 fprintf(stderr, "Unknown node '%s'\n", arg);
131 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[0], node));
134 } else if(!strcasecmp(buf, "link")) {
136 } else if(!strcasecmp(buf, "test")) {
138 } else if(!strcasecmp(buf, "quit")) {
141 } else if(!strcasecmp(buf, "help")) {
143 "<name>: <message> Send a message to the given node.\n"
144 " Subsequent messages don't need the <name>: prefix.\n"
145 "/invite <name> Create an invitation for a new node.\n"
146 "/join <invitation> Join an existing mesh using an invitation.\n"
147 "/kick <name> Blacklist the given node.\n"
148 "/who [<name>] List all nodes or show information about the given node.\n"
149 "/link Link all nodes together.\n"
150 "/test Test functionality sending some data to all nodes\n"
151 "/quit Exit this program.\n"
154 fprintf(stderr, "Unknown command '/%s'\n", buf);
158 static void parse_input(char *buf) {
159 static meshlink_node_t *destination;
169 if(len && buf[len - 1] == '\n')
172 if(len && buf[len - 1] == '\r')
175 // Ignore empty lines.
180 // Commands start with '/'
183 return parse_command(buf + 1);
185 // Lines in the form "name: message..." set the destination node.
188 char *colon = strchr(buf, ':');
196 destination = meshlink_get_node(mesh[0], buf);
198 fprintf(stderr, "Unknown node '%s'\n", buf);
204 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
208 if(!meshlink_send(mesh[0], destination, msg, strlen(msg) + 1)) {
209 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
213 printf("Message sent to '%s'.\n", destination->name);
216 int main(int argc, char *argv[]) {
217 const char *basebase = ".manynodes";
218 const char *namesprefix = "machine1";
225 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames]\n", argv[0]);
233 namesprefix = argv[3];
235 mesh = calloc(n, sizeof *mesh);
237 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
238 mkdir(basebase, 0750);
240 char filename[PATH_MAX];
242 for(int i = 0; i < n; i++) {
243 snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i);
244 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
245 bool itsnew = access(filename, R_OK);
246 mesh[i] = meshlink_open(filename, nodename, "manynodes", STATIONARY);
247 meshlink_set_log_cb(mesh[i], MESHLINK_INFO, log_message);
249 meshlink_add_address(mesh[i], "localhost");
251 fprintf(stderr, "errno is: %d\n", meshlink_errno);
252 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
259 for(int i = 0; i < n; i++) {
260 if(!meshlink_start(mesh[i]))
261 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
267 fprintf(stderr, "Could not start any node!\n");
271 printf("%d nodes started.\nType /help for a list of commands.\n", started);
273 while(fgets(buf, sizeof buf, stdin))
276 printf("Nodes stopping.\n");
278 for(int i = 0; i < n; i++)
279 meshlink_close(mesh[i]);