X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fchannels.c;h=0f23de89c617af11a66b8615b9589b878586c712;hb=4b6c01b1d5383b1a7417244a31ad4652aab2d5db;hp=b9dd8a256f8214b21110184b892be0e421dd8a14;hpb=ed545c8ca7b674b5e851567f2b6630ed3e4bfac0;p=meshlink diff --git a/examples/channels.c b/examples/channels.c index b9dd8a25..0f23de89 100644 --- a/examples/channels.c +++ b/examples/channels.c @@ -7,7 +7,9 @@ #define CHAT_PORT 531 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { - const char *levelstr[] = { + (void) mesh; + + static const char *levelstr[] = { [MESHLINK_DEBUG] = "\x1b[34mDEBUG", [MESHLINK_INFO] = "\x1b[32mINFO", [MESHLINK_WARNING] = "\x1b[33mWARNING", @@ -19,10 +21,11 @@ static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, con static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { if(!len) { - if(meshlink_errno) + if(meshlink_errno) { fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno)); - else + } else { fprintf(stderr, "Chat connection closed by %s\n", channel->node->name); + } channel->node->priv = NULL; meshlink_channel_close(mesh, channel); @@ -37,6 +40,9 @@ static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel } static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { + (void)data; + (void)len; + // Only accept connections to the chat port if(port != CHAT_PORT) { fprintf(stderr, "Rejected incoming channel from '%s' to port %u\n", channel->node->name, port); @@ -55,11 +61,21 @@ static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, return true; } +static void channel_poll(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) { + (void)len; + + fprintf(stderr, "Channel to '%s' connected\n", channel->node->name); + meshlink_set_channel_poll_cb(mesh, channel, NULL); +} + static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { - if(reachable) + (void)mesh; + + if(reachable) { printf("%s joined.\n", node->name); - else + } else { printf("%s left.\n", node->name); + } } static meshlink_node_t **nodes; @@ -67,8 +83,10 @@ static size_t nnodes; static void parse_command(meshlink_handle_t *mesh, char *buf) { char *arg = strchr(buf, ' '); - if(arg) + + if(arg) { *arg++ = 0; + } if(!strcasecmp(buf, "invite")) { char *invitation; @@ -78,7 +96,8 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { return; } - invitation = meshlink_invite(mesh, arg); + invitation = meshlink_invite(mesh, NULL, arg); + if(!invitation) { fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; @@ -91,15 +110,18 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { fprintf(stderr, "/join requires an argument!\n"); return; } + meshlink_stop(mesh); - if(!meshlink_join(mesh, arg)) + + if(!meshlink_join(mesh, arg)) { fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno)); - else { + } else { fprintf(stderr, "Invitation accepted!\n"); - if(!meshlink_start(mesh)) { - fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno)); - return; - } + } + + if(!meshlink_start(mesh)) { + fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno)); + exit(1); } } else if(!strcasecmp(buf, "kick")) { if(!arg) { @@ -108,27 +130,55 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } meshlink_node_t *node = meshlink_get_node(mesh, arg); + if(!node) { fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; } - meshlink_blacklist(mesh, node); + if(!meshlink_blacklist(mesh, node)) { + fprintf(stderr, "Error blacklising '%s': %s", arg, meshlink_strerror(meshlink_errno)); + return; + } printf("Node '%s' blacklisted.\n", arg); + } else if(!strcasecmp(buf, "whitelist")) { + if(!arg) { + fprintf(stderr, "/whitelist requires an argument!\n"); + return; + } + + meshlink_node_t *node = meshlink_get_node(mesh, arg); + + if(!node) { + fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); + return; + } + + if(!meshlink_whitelist(mesh, node)) { + fprintf(stderr, "Error whitelising '%s': %s", arg, meshlink_strerror(meshlink_errno)); + return; + } + + printf("Node '%s' whitelisted.\n", arg); } else if(!strcasecmp(buf, "who")) { if(!arg) { nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes); + if(!nnodes) { fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno)); } else { printf("%zu known nodes:", nnodes); - for(int i = 0; i < nnodes; i++) + + for(size_t i = 0; i < nnodes; i++) { printf(" %s", nodes[i]->name); + } + printf("\n"); } } else { meshlink_node_t *node = meshlink_get_node(mesh, arg); + if(!node) { fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); } else { @@ -140,14 +190,14 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { fclose(stdin); } else if(!strcasecmp(buf, "help")) { printf( - ": Send a message to the given node.\n" - " Subsequent messages don't need the : prefix.\n" - "/invite Create an invitation for a new node.\n" - "/join Join an existing mesh using an invitation.\n" - "/kick Blacklist the given node.\n" - "/who [] List all nodes or show information about the given node.\n" - "/quit Exit this program.\n" - ); + ": Send a message to the given node.\n" + " Subsequent messages don't need the : prefix.\n" + "/invite Create an invitation for a new node.\n" + "/join Join an existing mesh using an invitation.\n" + "/kick Blacklist the given node.\n" + "/who [] List all nodes or show information about the given node.\n" + "/quit Exit this program.\n" + ); } else { fprintf(stderr, "Unknown command '/%s'\n", buf); } @@ -157,28 +207,34 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { static meshlink_node_t *destination; size_t len; - if(!buf) + if(!buf) { return; + } // Remove newline. len = strlen(buf); - if(len && buf[len - 1] == '\n') + if(len && buf[len - 1] == '\n') { buf[--len] = 0; + } - if(len && buf[len - 1] == '\r') + if(len && buf[len - 1] == '\r') { buf[--len] = 0; + } // Ignore empty lines. - if(!len) + if(!len) { return; + } // Commands start with '/' - if(*buf == '/') - return parse_command(mesh, buf + 1); + if(*buf == '/') { + parse_command(mesh, buf + 1); + return; + } // Lines in the form "name: message..." set the destination node. @@ -188,10 +244,13 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { if(colon) { *colon = 0; msg = colon + 1; - if(*msg == ' ') + + if(*msg == ' ') { msg++; + } destination = meshlink_get_node(mesh, buf); + if(!destination) { fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno)); return; @@ -210,11 +269,14 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { if(!channel) { fprintf(stderr, "Opening chat channel to '%s'\n", destination->name); channel = meshlink_channel_open(mesh, destination, CHAT_PORT, channel_receive, NULL, 0); + if(!channel) { fprintf(stderr, "Could not create channel to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno)); return; } + destination->priv = channel; + meshlink_set_channel_poll_cb(mesh, channel, channel_poll); } if(!meshlink_channel_send(mesh, channel, msg, strlen(msg))) { @@ -230,15 +292,18 @@ int main(int argc, char *argv[]) { const char *nick = NULL; char buf[1024]; - if(argc > 1) + if(argc > 1) { confbase = argv[1]; + } - if(argc > 2) + if(argc > 2) { nick = argv[2]; + } meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message); meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY); + if(!mesh) { fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1; @@ -258,8 +323,9 @@ int main(int argc, char *argv[]) { printf("Chat started.\nType /help for a list of commands.\n"); - while(fgets(buf, sizeof buf, stdin)) + while(fgets(buf, sizeof(buf), stdin)) { parse_input(mesh, buf); + } printf("Chat stopping.\n");