6 #include "../src/meshlink-tiny.h"
7 #include "../src/devtools.h"
11 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
14 static const char *levelstr[] = {
15 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
16 [MESHLINK_INFO] = "\x1b[32mINFO",
17 [MESHLINK_WARNING] = "\x1b[33mWARNING",
18 [MESHLINK_ERROR] = "\x1b[31mERROR",
19 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
21 fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
24 static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
27 fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno));
29 fprintf(stderr, "Chat connection closed by %s\n", channel->node->name);
32 channel->node->priv = NULL;
33 meshlink_channel_close(mesh, channel);
37 // TODO: we now have TCP semantics, don't expect exactly one message per receive call.
39 fprintf(stderr, "%s says: ", channel->node->name);
40 fwrite(data, len, 1, stderr);
44 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
48 // Only accept connections to the chat port
49 if(port != CHAT_PORT) {
50 fprintf(stderr, "Rejected incoming channel from '%s' to port %u\n", channel->node->name, port);
54 fprintf(stderr, "Accepted incoming channel from '%s'\n", channel->node->name);
56 // Remember the channel
57 channel->node->priv = channel;
59 // Set the receive callback
60 meshlink_set_channel_receive_cb(mesh, channel, channel_receive);
62 // Accept this channel
66 static void channel_poll(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
69 fprintf(stderr, "Channel to '%s' connected\n", channel->node->name);
70 meshlink_set_channel_poll_cb(mesh, channel, NULL);
73 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
77 fprintf(stderr, "%s joined.\n", node->name);
79 fprintf(stderr, "%s left.\n", node->name);
83 static void parse_command(meshlink_handle_t *mesh, char *buf) {
84 char *arg = strchr(buf, ' ');
90 arg1 = strchr(arg, ' ');
98 if(!strcasecmp(buf, "canonical")) {
100 char *host = NULL, *port = NULL;
103 fprintf(stderr, "/canonical requires an argument!\n");
107 if((0 == strncasecmp(arg, "-h", 2)) && (strlen(arg) > 2)) {
109 } else if((0 == strncasecmp(arg, "-p", 2)) && (strlen(arg) > 2)) {
112 fprintf(stderr, "Unknown argument: %s!\n", arg);
117 if((0 == strncasecmp(arg1, "-h", 2)) && (strlen(arg1) > 2)) {
119 } else if((0 == strncasecmp(arg1, "-p", 2)) && (strlen(arg1) > 2)) {
122 fprintf(stderr, "Unknown argument: %s!\n", arg1);
128 fprintf(stderr, "Unable to set Canonical address because no valid arguments are found!\n");
132 set = meshlink_set_canonical_address(mesh, meshlink_get_self(mesh), host, port);
135 fprintf(stderr, "Could not set canonical address '%s:%s': %s\n", host, port, meshlink_strerror(meshlink_errno));
139 fprintf(stderr, "Canonical address set as '%s:%s'\n", host, port);
140 } else if(!strcasecmp(buf, "group")) {
142 fprintf(stderr, "/group requires an argument!\n");
146 meshlink_submesh_t *s = meshlink_submesh_open(mesh, arg);
149 fprintf(stderr, "Could not create group: %s\n", meshlink_strerror(meshlink_errno));
151 fprintf(stderr, "Group '%s' created!\n", s->name);
153 } else if(!strcasecmp(buf, "join")) {
155 fprintf(stderr, "/join requires an argument!\n");
161 if(!meshlink_join(mesh, arg)) {
162 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
164 fprintf(stderr, "Invitation accepted!\n");
167 if(!meshlink_start(mesh)) {
168 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
171 } else if(!strcasecmp(buf, "who")) {
172 meshlink_submesh_t *node_group = NULL;
176 meshlink_node_t **nodes = meshlink_get_all_nodes(mesh, NULL, &nnodes);
179 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
183 fprintf(stderr, "%lu known nodes:\n", (unsigned long)nnodes);
185 for(size_t i = 0; i < nnodes; i++) {
186 fprintf(stderr, " %lu. %s", (unsigned long)i, nodes[i]->name);
188 if((node_group = meshlink_get_node_submesh(mesh, nodes[i]))) {
189 fprintf(stderr, "\t%s", node_group->name);
192 fprintf(stderr, "\n");
195 fprintf(stderr, "\n");
199 meshlink_node_t *node = meshlink_get_node(mesh, arg);
202 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
204 fprintf(stderr, "Node %s found", arg);
206 if((node_group = meshlink_get_node_submesh(mesh, node))) {
207 fprintf(stderr, " in group %s", node_group->name);
210 fprintf(stderr, "\n");
213 } else if(!strcasecmp(buf, "listgroup")) {
215 fprintf(stderr, "/listgroup requires an argument!\n");
220 meshlink_submesh_t **submeshes = devtool_get_all_submeshes(mesh, NULL, &nmemb);
222 if(!submeshes || !nmemb) {
223 fprintf(stderr, "Group does not exist!\n");
227 meshlink_submesh_t *s = NULL;
229 for(size_t i = 0; i < nmemb; i++) {
230 if(!strcmp(arg, submeshes[i]->name)) {
239 fprintf(stderr, "Group %s does not exist!\n", arg);
244 meshlink_node_t **nodes = meshlink_get_all_nodes_by_submesh(mesh, s, NULL, &nnodes);
246 if(!nodes || !nnodes) {
247 fprintf(stderr, "Group %s does not contain any nodes!\n", arg);
251 fprintf(stderr, "%zu known nodes in group %s:", nnodes, arg);
253 for(size_t i = 0; i < nnodes; i++) {
254 fprintf(stderr, " %s", nodes[i]->name);
257 fprintf(stderr, "\n");
260 } else if(!strcasecmp(buf, "quit")) {
261 fprintf(stderr, "Bye!\n");
263 } else if(!strcasecmp(buf, "help")) {
265 "<name>: <message> Send a message to the given node.\n"
266 " Subsequent messages don't need the <name>: prefix.\n"
267 "/group <name> Create a new group"
268 "/join <invitation> Join an existing mesh using an invitation.\n"
269 "/kick <name> Blacklist the given node.\n"
270 "/who [<name>] List all nodes or show information about the given node.\n"
271 "/listgroup <name> List all nodes in a given group.\n"
272 "/canonical -h<hostname> -p<port> Set Canonical address to be present in invitation.\n"
273 " Any one of two options an be specified. At least one option must be present\n"
274 "/quit Exit this program.\n"
277 fprintf(stderr, "Unknown command '/%s'\n", buf);
281 static void parse_input(meshlink_handle_t *mesh, char *buf) {
282 static meshlink_node_t *destination;
293 if(len && buf[len - 1] == '\n') {
297 if(len && buf[len - 1] == '\r') {
301 // Ignore empty lines.
307 // Commands start with '/'
310 parse_command(mesh, buf + 1);
314 // Lines in the form "name: message..." set the destination node.
317 char *colon = strchr(buf, ':');
327 destination = meshlink_get_node(mesh, buf);
330 fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
336 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
340 // We want to have one channel per node.
341 // We keep the pointer to the meshlink_channel_t in the priv field of that node.
342 meshlink_channel_t *channel = destination->priv;
345 fprintf(stderr, "Opening chat channel to '%s'\n", destination->name);
346 channel = meshlink_channel_open(mesh, destination, CHAT_PORT, channel_receive, NULL, 0);
349 fprintf(stderr, "Could not create channel to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
353 destination->priv = channel;
354 meshlink_set_channel_poll_cb(mesh, channel, channel_poll);
357 if(!meshlink_channel_send(mesh, channel, msg, strlen(msg))) {
358 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
362 fprintf(stderr, "Message sent to '%s'.\n", destination->name);
365 int main(int argc, char *argv[]) {
366 const char *confbase = ".chat";
367 const char *nick = NULL;
378 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
380 meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
383 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
387 meshlink_set_node_status_cb(mesh, node_status);
388 meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
390 // Set the channel accept callback. This implicitly turns on channels for all nodes.
391 // This replaces the call to meshlink_set_receive_cb().
392 meshlink_set_channel_accept_cb(mesh, channel_accept);
394 if(!meshlink_start(mesh)) {
395 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
399 fprintf(stderr, "Chat started.\nType /help for a list of commands.\n");
401 while(fgets(buf, sizeof(buf), stdin)) {
402 parse_input(mesh, buf);
405 fprintf(stderr, "Chat stopping.\n");
408 meshlink_close(mesh);