7 #include <linux/limits.h>
9 #include "../src/meshlink.h"
12 static meshlink_handle_t **mesh;
14 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
15 const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"};
16 fprintf(stderr, "%s: %s\n", levelstr[level], text);
19 static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
20 const char *msg = data;
22 if(!len || msg[len - 1]) {
23 fprintf(stderr, "Received invalid data from %s\n", source->name);
27 printf("%s says: %s\n", source->name, msg);
30 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
32 printf("%s joined.\n", node->name);
34 printf("%s left.\n", node->name);
37 // Make all nodes know about each other by importing each others public keys and addresses.
38 static void linkmesh() {
39 for(int i = 0; i < n; i++) {
40 char *datai = meshlink_export(mesh[i]);
42 for(int j = i + 1; j < n; j++) {
43 char *dataj = meshlink_export(mesh[j]);
44 meshlink_import(mesh[i], dataj);
45 meshlink_import(mesh[j], datai);
53 static void parse_command(char *buf) {
54 char *arg = strchr(buf, ' ');
58 if(!strcasecmp(buf, "invite")) {
62 fprintf(stderr, "/invite requires an argument!\n");
66 invitation = meshlink_invite(mesh[0], arg);
68 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
72 printf("Invitation for %s: %s\n", arg, invitation);
74 } else if(!strcasecmp(buf, "join")) {
76 fprintf(stderr, "/join requires an argument!\n");
80 if(!meshlink_join(mesh[0], arg))
81 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
83 fprintf(stderr, "Invitation accepted!\n");
84 } else if(!strcasecmp(buf, "kick")) {
86 fprintf(stderr, "/kick requires an argument!\n");
90 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
92 fprintf(stderr, "Unknown node '%s'\n", arg);
96 meshlink_blacklist(mesh[0], node);
98 printf("Node '%s' blacklisted.\n", arg);
99 } else if(!strcasecmp(buf, "who")) {
101 meshlink_node_t *nodes[100];
102 size_t n = meshlink_get_all_nodes(mesh[0], nodes, 100);
104 fprintf(stderr, "No nodes known!\n");
106 printf("Known nodes:");
107 for(int i = 0; i < n && i < 100; i++)
108 printf(" %s", nodes[i]->name);
110 printf(" (and %zu more)", n - 100);
114 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
116 fprintf(stderr, "Unknown node '%s'\n", arg);
118 printf("Node %s found\n", arg);
121 } else if(!strcasecmp(buf, "link")) {
123 } else if(!strcasecmp(buf, "quit")) {
126 } else if(!strcasecmp(buf, "help")) {
128 "<name>: <message> Send a message to the given node.\n"
129 " Subsequent messages don't need the <name>: prefix.\n"
130 "/invite <name> Create an invitation for a new node.\n"
131 "/join <invitation> Join an existing mesh using an invitation.\n"
132 "/kick <name> Blacklist the given node.\n"
133 "/who [<name>] List all nodes or show information about the given node.\n"
134 "/link Link all nodes together.\n"
135 "/quit Exit this program.\n"
138 fprintf(stderr, "Unknown command '/%s'\n", buf);
142 static void parse_input(char *buf) {
143 static meshlink_node_t *destination;
153 if(len && buf[len - 1] == '\n')
156 if(len && buf[len - 1] == '\r')
159 // Ignore empty lines.
164 // Commands start with '/'
167 return parse_command(buf + 1);
169 // Lines in the form "name: message..." set the destination node.
172 char *colon = strchr(buf, ':');
180 destination = meshlink_get_node(mesh[0], buf);
182 fprintf(stderr, "Unknown node '%s'\n", buf);
188 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
192 if(!meshlink_send(mesh[0], destination, msg, strlen(msg) + 1)) {
193 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
197 printf("Message sent to '%s'.\n", destination->name);
200 int main(int argc, char *argv[]) {
201 const char *basebase = ".manynodes";
208 fprintf(stderr, "Usage: %s [number of local nodes] [confbase]\n", argv[0]);
215 mesh = calloc(n, sizeof *mesh);
217 mkdir(basebase, 0750);
219 char filename[PATH_MAX];
221 for(int i = 0; i < n; i++) {
222 snprintf(nodename, sizeof nodename, "node%d", i);
223 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
224 bool itsnew = access(filename, R_OK);
225 mesh[i] = meshlink_open(filename, nodename, "manynodes");
227 meshlink_add_address(mesh[i], "localhost");
229 fprintf(stderr, "errno is: %d\n", meshlink_errno);
230 fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
237 for(int i = 0; i < n; i++) {
238 if(!meshlink_start(mesh[i]))
239 fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
245 fprintf(stderr, "Could not start any node!\n");
249 printf("%d nodes started.\nType /help for a list of commands.\n", started);
251 while(fgets(buf, sizeof buf, stdin))
254 printf("Nodes stopping.\n");
256 for(int i = 0; i < n; i++)
257 meshlink_close(mesh[i]);