7 #include <linux/limits.h>
9 #include "../src/meshlink.h"
12 static meshlink_handle_t **mesh;
14 static meshlink_node_t **nodes;
17 //Test mesh sending data
18 static void testmesh () {
20 for(int nindex = 0; nindex < n; nindex++) {
22 nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
24 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
26 printf("%zu known nodes:", nnodes);
27 for(int i = 0; i < nnodes; i++) {
28 printf(" %s", nodes[i]->name);
29 if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
30 fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
38 // Make all nodes know about each other by importing each others public keys and addresses.
39 static void linkmesh() {
40 for(int i = 0; i < n; i++) {
41 char *datai = meshlink_export(mesh[i]);
43 for(int j = i + 1; j < n; j++) {
44 char *dataj = meshlink_export(mesh[j]);
45 meshlink_import(mesh[i], dataj);
46 meshlink_import(mesh[j], datai);
54 static void parse_command(char *buf) {
55 char *arg = strchr(buf, ' ');
59 if(!strcasecmp(buf, "invite")) {
63 fprintf(stderr, "/invite requires an argument!\n");
67 invitation = meshlink_invite(mesh[0], arg);
69 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
73 printf("Invitation for %s: %s\n", arg, invitation);
75 } else if(!strcasecmp(buf, "join")) {
77 fprintf(stderr, "/join requires an argument!\n");
80 meshlink_stop(mesh[0]);
81 if(!meshlink_join(mesh[0], arg))
82 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
84 fprintf(stderr, "Invitation accepted!\n");
85 meshlink_start(mesh[0]);
87 } else if(!strcasecmp(buf, "kick")) {
89 fprintf(stderr, "/kick requires an argument!\n");
93 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
95 fprintf(stderr, "Unknown node '%s'\n", arg);
99 meshlink_blacklist(mesh[0], node);
101 printf("Node '%s' blacklisted.\n", arg);
102 } else if(!strcasecmp(buf, "who")) {
104 nodes = meshlink_get_all_nodes(mesh[0], nodes, &nnodes);
106 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
108 printf("%zu known nodes:", nnodes);
109 for(int i = 0; i < nnodes; i++)
110 printf(" %s", nodes[i]->name);
114 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
116 fprintf(stderr, "Unknown node '%s'\n", arg);
118 printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[0], node));
121 } else if(!strcasecmp(buf, "link")) {
123 } else if(!strcasecmp(buf, "test")) {
125 } else if(!strcasecmp(buf, "quit")) {
128 } else if(!strcasecmp(buf, "help")) {
130 "<name>: <message> Send a message to the given node.\n"
131 " Subsequent messages don't need the <name>: prefix.\n"
132 "/invite <name> Create an invitation for a new node.\n"
133 "/join <invitation> Join an existing mesh using an invitation.\n"
134 "/kick <name> Blacklist the given node.\n"
135 "/who [<name>] List all nodes or show information about the given node.\n"
136 "/link Link all nodes together.\n"
137 "/test Test functionality sending some data to all nodes\n"
138 "/quit Exit this program.\n"
141 fprintf(stderr, "Unknown command '/%s'\n", buf);
145 static void parse_input(char *buf) {
146 static meshlink_node_t *destination;
156 if(len && buf[len - 1] == '\n')
159 if(len && buf[len - 1] == '\r')
162 // Ignore empty lines.
167 // Commands start with '/'
170 return parse_command(buf + 1);
172 // Lines in the form "name: message..." set the destination node.
175 char *colon = strchr(buf, ':');
183 destination = meshlink_get_node(mesh[0], buf);
185 fprintf(stderr, "Unknown node '%s'\n", buf);
191 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
195 if(!meshlink_send(mesh[0], destination, msg, strlen(msg) + 1)) {
196 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
200 printf("Message sent to '%s'.\n", destination->name);
203 int main(int argc, char *argv[]) {
204 const char *basebase = ".manynodes";
205 const char *namesprefix = "machine1";
212 fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames]\n", argv[0]);
220 namesprefix = argv[3];
222 mesh = calloc(n, sizeof *mesh);
224 mkdir(basebase, 0750);
226 char filename[PATH_MAX];
228 for(int i = 0; i < n; i++) {
229 snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i);
230 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
231 bool itsnew = access(filename, R_OK);
232 mesh[i] = meshlink_open(filename, nodename, "manynodes");
234 meshlink_add_address(mesh[i], "localhost");
236 fprintf(stderr, "errno is: %d\n", meshlink_errno);
237 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
244 for(int i = 0; i < n; i++) {
245 if(!meshlink_start(mesh[i]))
246 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
252 fprintf(stderr, "Could not start any node!\n");
256 printf("%d nodes started.\nType /help for a list of commands.\n", started);
258 while(fgets(buf, sizeof buf, stdin))
261 printf("Nodes stopping.\n");
263 for(int i = 0; i < n; i++)
264 meshlink_close(mesh[i]);