]> git.meshlink.io Git - meshlink/blobdiff - examples/channels.c
Fix errors found by Clang's static analyzer.
[meshlink] / examples / channels.c
index 3ad2742e1fcf55649695660d92775ff422e8287d..a869f513e9bed1e46c86f192c09dff6b9bef10ff 100644 (file)
@@ -18,8 +18,6 @@ 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) {
-       const char *msg = data;
-
        if(!len) {
                if(meshlink_errno)
                        fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno));
@@ -32,12 +30,10 @@ static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel
        }
 
        // TODO: we now have TCP semantics, don't expect exactly one message per receive call.
-       if(msg[len - 1]) {
-               fprintf(stderr, "Received invalid data from %s\n", channel->node->name);
-               return;
-       }
 
-       printf("%s says: %s\n", channel->node->name, msg);
+       printf("%s says: ", channel->node->name);
+       fwrite(data, len, 1, stdout);
+       fputc('\n', stdout);
 }
 
 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
@@ -59,6 +55,11 @@ 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) {
+       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)
                printf("%s joined.\n", node->name);
@@ -98,12 +99,11 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) {
                meshlink_stop(mesh);
                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) {
@@ -123,9 +123,9 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) {
        } else if(!strcasecmp(buf, "who")) {
                if(!arg) {
                        nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
-                       if(!nnodes) {
+                       if(!nnodes)
                                fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
-                       else {
+                       else {
                                printf("%zu known nodes:", nnodes);
                                for(int i = 0; i < nnodes; i++)
                                        printf(" %s", nodes[i]->name);
@@ -133,28 +133,26 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) {
                        }
                } else {
                        meshlink_node_t *node = meshlink_get_node(mesh, arg);
-                       if(!node) {
+                       if(!node)
                                fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
-                       } else {
+                       else
                                printf("Node %s found\n", arg);
-                       }
                }
        } else if(!strcasecmp(buf, "quit")) {
                printf("Bye!\n");
                fclose(stdin);
        } else if(!strcasecmp(buf, "help")) {
                printf(
-                       "<name>: <message>     Send a message to the given node.\n"
-                       "                      Subsequent messages don't need the <name>: prefix.\n"
-                       "/invite <name>        Create an invitation for a new node.\n"
-                       "/join <invitation>    Join an existing mesh using an invitation.\n"
-                       "/kick <name>          Blacklist the given node.\n"
-                       "/who [<name>]         List all nodes or show information about the given node.\n"
-                       "/quit                 Exit this program.\n"
-                       );
-       } else {
+                       "<name>: <message>     Send a message to the given node.\n"
+                       "                      Subsequent messages don't need the <name>: prefix.\n"
+                       "/invite <name>        Create an invitation for a new node.\n"
+                       "/join <invitation>    Join an existing mesh using an invitation.\n"
+                       "/kick <name>          Blacklist the given node.\n"
+                       "/who [<name>]         List all nodes or show information about the given node.\n"
+                       "/quit                 Exit this program.\n"
+               );
+       } else
                fprintf(stderr, "Unknown command '/%s'\n", buf);
-       }
 }
 
 static void parse_input(meshlink_handle_t *mesh, char *buf) {
@@ -219,9 +217,10 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) {
                        return;
                }
                destination->priv = channel;
+               meshlink_set_channel_poll_cb(mesh, channel, channel_poll);
        }
 
-       if(!meshlink_channel_send(mesh, channel, msg, len + 1)) {
+       if(!meshlink_channel_send(mesh, channel, msg, strlen(msg))) {
                fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
                return;
        }
@@ -262,7 +261,7 @@ 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");