]> git.meshlink.io Git - meshlink/commitdiff
Merge branch 'partition_healing' into everbase
authorGuus Sliepen <guus@sliepen.org>
Tue, 29 Jul 2014 15:02:47 +0000 (17:02 +0200)
committerGuus Sliepen <guus@sliepen.org>
Tue, 29 Jul 2014 15:02:47 +0000 (17:02 +0200)
examples/chat.c
examples/chatpp.cc
src/conf.c
src/meshlink++.h
src/meshlink.c
src/meshlink.h

index 70d17d7d917bf6515aaf98d1fb97bea2f969df22..1c07e422d07a2fa7efb20c015d94db81246be21f 100644 (file)
@@ -42,7 +42,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) {
 
                invitation = meshlink_invite(mesh, arg);
                if(!invitation) {
-                       fprintf(stderr, "Could not invite '%s': %s\n", arg, mesh->errstr);
+                       fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
                        return;
                }
 
@@ -55,7 +55,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) {
                }
 
                if(!meshlink_join(mesh, arg))
-                       fprintf(stderr, "Could not join using invitation: %s\n", mesh->errstr);
+                       fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
                else
                        fprintf(stderr, "Invitation accepted!\n");
        } else if(!strcasecmp(buf, "kick")) {
@@ -164,7 +164,7 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) {
        }
 
        if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) {
-               fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, mesh->errstr);
+               fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
                return;
        }
 
@@ -193,7 +193,7 @@ int main(int argc, char *argv[]) {
        meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
 
        if(!meshlink_start(mesh)) {
-               fprintf(stderr, "Could not start MeshLink: %s\n", mesh->errstr);
+               fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
                return 1;
        }
 
index 864a3e809b7258d20f8dcc7dcf561d3bb4c1d4aa..73ced3c6a2c0f67c7ffbc1a2ff0c054306b12541 100644 (file)
@@ -42,7 +42,7 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
 
                invitation = mesh->invite(arg);
                if(!invitation) {
-                       fprintf(stderr, "Could not invite '%s': %s\n", arg, mesh->errstr);
+                       fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
                        return;
                }
 
@@ -55,7 +55,7 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
                }
 
                if(!mesh->join(arg))
-                       fprintf(stderr, "Could not join using invitation: %s\n", mesh->errstr);
+                       fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
                else
                        fprintf(stderr, "Invitation accepted!\n");
        } else if(!strcasecmp(buf, "kick")) {
@@ -81,7 +81,7 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
                                fprintf(stderr, "No nodes known!\n");
                        } else {
                                printf("Known nodes:");
-                               for(int i = 0; i < n && i < 100; i++)
+                               for(size_t i = 0; i < n && i < 100; i++)
                                        printf(" %s", nodes[i]->name);
                                if(n > 100)
                                        printf(" (and %zu more)", n - 100);
@@ -164,7 +164,7 @@ static void parse_input(meshlink::mesh *mesh, char *buf) {
        }
 
        if(!mesh->send(destination, msg, strlen(msg) + 1)) {
-               fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, mesh->errstr);
+               fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror());
                return;
        }
 
@@ -193,7 +193,7 @@ int main(int argc, char *argv[]) {
        mesh->set_log_cb(MESHLINK_INFO, log_message);
 
        if(!mesh->start()) {
-               fprintf(stderr, "Could not start MeshLink: %s\n", mesh->errstr);
+               fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
                return 1;
        }
 
index aa22c47ff9a67807e2721ba7d7671a3bcbfe7a0e..36c10acb45d3d025458de83fdc89bd52f8824d43 100644 (file)
@@ -323,6 +323,7 @@ bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *k
        if(!fp) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", filename, strerror(errno));
        } else {
+               fprintf(fp, "%s = %s\n", key, value);
                fclose(fp);
        }
 
index c8bdc2f9aa5cfd4ff2ce3b87236fda0dd8eac444..2729130cc64872f686a1fe6a4ce9bda60a564c9f 100644 (file)
@@ -182,9 +182,10 @@ namespace meshlink {
                 *  @param nodes        A pointer to an array of pointers to meshlink::node, which should be allocated by the application.
                 *  @param nmemb        The maximum number of pointers that can be stored in the nodes array.
                 *
-                *  @return             The number of known nodes. This can be larger than nmemb, in which case not all nodes were stored in the nodes array.
+                *  @return             The number of known nodes, or -1 in case of an error.
+                *                      This can be larger than nmemb, in which case not all nodes were stored in the nodes array.
                 */
-               size_t get_all_nodes(node **nodes, size_t nmemb) {
+               ssize_t get_all_nodes(node **nodes, size_t nmemb) {
                        return meshlink_get_all_nodes(this, (meshlink_node_t **)nodes, nmemb);
                }
 
@@ -387,6 +388,10 @@ namespace meshlink {
        static void close(mesh *mesh) {
                meshlink_close(mesh);
        }
+
+       static const char *strerror(errno_t err = meshlink_errno) {
+               return meshlink_strerror(err);
+       }
 };
 
 #endif // MESHLINKPP_H
index 27ba877a25f2358fcc9a3a2efd1e14b37160831e..3651988fa5c9c66c337c2df5482a1a1df7350ae4 100644 (file)
@@ -44,6 +44,8 @@ typedef struct {
 #define MSG_NOSIGNAL 0
 #endif
 
+__thread meshlink_errno_t meshlink_errno;
+
 //TODO: this can go away completely
 const var_t variables[] = {
        /* Server configuration */
@@ -192,7 +194,7 @@ static char *get_my_hostname(meshlink_handle_t* mesh) {
        fprintf(stderr, "Trying to discover externally visible hostname...\n");
        struct addrinfo *ai = str2addrinfo("meshlink.io", "80", SOCK_STREAM);
        struct addrinfo *aip = ai;
-       static const char request[] = "GET http://meshlink.io/host.cgi HTTP/1.0\r\n\r\n";
+       static const char request[] = "GET http://www.meshlink.io/host.cgi HTTP/1.0\r\n\r\n";
 
        while(aip) {
                int s = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol);
@@ -327,15 +329,20 @@ static bool try_bind(int port) {
 
        while(ai) {
                int fd = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
-               if(!fd)
+               if(!fd) {
+                       freeaddrinfo(ai);
                        return false;
+               }
                int result = bind(fd, ai->ai_addr, ai->ai_addrlen);
                closesocket(fd);
-               if(result)
+               if(result) {
+                       freeaddrinfo(ai);
                        return false;
+               }
                ai = ai->ai_next;
        }
 
+       freeaddrinfo(ai);
        return true;
 }
 
@@ -508,7 +515,7 @@ static bool finalize_join(meshlink_handle_t *mesh) {
        free(mesh->self->name);
        free(mesh->self->connection->name);
        mesh->self->name = xstrdup(name);
-       mesh->self->connection->name = xstrdup(name);
+       mesh->self->connection->name = name;
 
        fprintf(stderr, "Configuration stored in: %s\n", mesh->confbase);
 
@@ -782,7 +789,7 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
        return mesh;
 }
 
-void *meshlink_main_loop(void *arg) {
+static void *meshlink_main_loop(void *arg) {
        meshlink_handle_t *mesh = arg;
 
        try_outgoing_connections(mesh);
@@ -793,6 +800,9 @@ void *meshlink_main_loop(void *arg) {
 }
 
 bool meshlink_start(meshlink_handle_t *mesh) {
+       if(!mesh)
+               return false;
+
        // TODO: open listening sockets first
 
        //Check that a valid name is set
@@ -815,6 +825,9 @@ bool meshlink_start(meshlink_handle_t *mesh) {
 }
 
 void meshlink_stop(meshlink_handle_t *mesh) {
+       if(!mesh)
+               return;
+
        // Shut down the listening sockets to signal the main thread to shut down
 
        for(int i = 0; i < mesh->listen_sockets; i++) {
@@ -828,6 +841,9 @@ void meshlink_stop(meshlink_handle_t *mesh) {
 }
 
 void meshlink_close(meshlink_handle_t *mesh) {
+       if(!mesh)
+               return;
+
        // Close and free all resources used.
 
        close_network_connections(mesh);
@@ -837,27 +853,47 @@ void meshlink_close(meshlink_handle_t *mesh) {
        exit_configuration(&mesh->config);
        event_loop_exit(&mesh->loop);
 
-       free(mesh);
-
 #ifdef HAVE_MINGW
-       WSACleanup();
+       if(mesh->confbase)
+               WSACleanup();
 #endif
+
+       ecdsa_free(mesh->invitation_key);
+
+       free(mesh->name);
+       free(mesh->confbase);
+
+       free(mesh);
+
+       memset(mesh, 0, sizeof *mesh);
 }
 
 void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb) {
+       if(!mesh)
+               return;
        mesh->receive_cb = cb;
 }
 
 void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
+       if(!mesh)
+               return;
        mesh->node_status_cb = cb;
 }
 
 void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb) {
+       if(!mesh)
+               return;
        mesh->log_cb = cb;
        mesh->log_level = level;
 }
 
 bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, unsigned int len) {
+       if(!mesh || !destination)
+               return false;
+       if(!len)
+               return true;
+       if(!data)
+               return false;
 
        /* If there is no outgoing list yet, create one. */
 
@@ -907,10 +943,15 @@ void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh) {
 }
 
 meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) {
+       if(!mesh || !name)
+               return NULL;
        return (meshlink_node_t *)lookup_node(mesh, (char *)name); // TODO: make lookup_node() use const
 }
 
-size_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t nmemb) {
+ssize_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t nmemb) {
+       if(!mesh || (nmemb && !nodes))
+               return -1;
+
        size_t i = 0;
 
        //lock mesh->nodes
@@ -928,6 +969,8 @@ size_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes,
 }
 
 bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen) {
+       if(!mesh || !data || !len || !signature || !siglen)
+               return false;
        if(*siglen < MESHLINK_SIGLEN)
                return false;
        if(!ecdsa_sign(mesh->self->connection->ecdsa, data, len, signature))
@@ -937,6 +980,8 @@ bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *
 }
 
 bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len, const void *signature, size_t siglen) {
+       if(!mesh || !data || !len || !signature)
+               return false;
        if(siglen != MESHLINK_SIGLEN)
                return false;
        struct node_t *n = (struct node_t *)source;
@@ -1038,6 +1083,9 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) {
 }
 
 bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) {
+       if(!mesh || !address)
+               return false;
+
        for(const char *p = address; *p; p++) {
                if(isalnum(*p) || *p == '-' || *p == '.' || *p == ':')
                        continue;
@@ -1049,6 +1097,9 @@ bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) {
 }
 
 char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
+       if(!mesh)
+               return false;
+
        // Check validity of the new node's name
        if(!check_id(name)) {
                fprintf(stderr, "Invalid name for node.\n");
@@ -1100,6 +1151,8 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
 
        b64encode_urlsafe(cookie, cookie, 18);
 
+       free(fingerprint);
+
        // Create a file containing the details of the invitation.
        snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "%s", mesh->confbase, cookiehash);
        int ifd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
@@ -1147,11 +1200,15 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
        // Create an URL from the local address, key hash and cookie
        char *url;
        xasprintf(&url, "%s/%s%s", address, hash, cookie);
+       free(address);
 
        return url;
 }
 
 bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
+       if(!mesh || !invitation)
+               return false;
+
        //TODO: think of a better name for this variable, or of a different way to tokenize the invitation URL.
        char copy[strlen(invitation) + 1];
        strcpy(copy, invitation);
@@ -1209,15 +1266,19 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
        mesh->sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
        if(mesh->sock <= 0) {
                fprintf(stderr, "Could not open socket: %s\n", strerror(errno));
+               freeaddrinfo(ai);
                return false;
        }
 
        if(connect(mesh->sock, ai->ai_addr, ai->ai_addrlen)) {
                fprintf(stderr, "Could not connect to %s port %s: %s\n", address, port, strerror(errno));
                closesocket(mesh->sock);
+               freeaddrinfo(ai);
                return false;
        }
 
+       freeaddrinfo(ai);
+
        fprintf(stderr, "Connected to %s port %s...\n", address, port);
 
        // Tell him we have an invitation, and give him our throw-away key.
@@ -1230,6 +1291,8 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
                return false;
        }
 
+       free(b64key);
+
        char hisname[4096] = "";
        int code, hismajor, hisminor = 0;
 
@@ -1296,6 +1359,9 @@ invalid:
 }
 
 char *meshlink_export(meshlink_handle_t *mesh) {
+       if(!mesh)
+               return NULL;
+
        char filename[PATH_MAX];
        snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name);
        FILE *f = fopen(filename, "r");
@@ -1323,6 +1389,9 @@ char *meshlink_export(meshlink_handle_t *mesh) {
 }
 
 bool meshlink_import(meshlink_handle_t *mesh, const char *data) {
+       if(!mesh || !data)
+               return false;
+
        if(strncmp(data, "Name = ", 7)) {
                fprintf(stderr, "Invalid data\n");
                return false;
@@ -1370,15 +1439,18 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) {
 }
 
 void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) {
-    node_t *n;
-    n = (node_t*)node;
-    n->status.blacklisted=true;
+       if(!mesh || !node)
+               return;
+
+       node_t *n;
+       n = (node_t*)node;
+       n->status.blacklisted=true;
        fprintf(stderr, "Blacklisted %s.\n",node->name);
 
        //Make blacklisting persistent in the config file
        append_config_file(mesh, n->name, "blacklisted", "yes");
-    return;
 
+       return;
 }
 
 static void __attribute__((constructor)) meshlink_init(void) {
index b54e961e5c9adab0bf6b144e2a527ad34f558511..a21af570d54c75c81a2b61425acce7524a424af6 100644 (file)
@@ -48,11 +48,12 @@ typedef enum {
        MESHLINK_ENOENT, ///< Node is not known
 } meshlink_errno_t;
 
+/// A variable holding the last encountered error from MeshLink.
+extern __thread meshlink_errno_t meshlink_errno;
+
 #ifndef MESHLINK_INTERNAL_H
 
 struct meshlink_handle {
-       meshlink_errno_t meshlink_errno; ///< Code of the last encountered error.
-       const char *errstr;              ///< Textual representation of most recent error encountered.
 };
 
 struct meshlink_node {
@@ -74,20 +75,29 @@ struct meshlink_channel {
  */
 extern const char *meshlink_strerror(meshlink_errno_t errno);
 
-/// Initialize MeshLink's configuration directory.
-/** This function causes MeshLink to initialize its configuration directory,
- *  if it hasn't already been initialized.
- *  It only has to be run the first time the application starts,
- *  but it is not a problem if it is run more than once, as long as
- *  the arguments given are the same.
+/// Open or create a MeshLink instance.
+/** This function opens or creates a MeshLink instance.
+ *  The state is stored in the configuration directory passed in the variable @a confbase @a.
+ *  If the configuration directory does not exist yet, for example when it is the first time
+ *  this instance is opened, the configuration directory will be automatically created and initialized.
+ *  However, the parent directory should already exist, otherwise an error will be returned.
+ *
+ *  The name given should be a unique identifier for this instance.
+ *
+ *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
+ *  When the application does no longer need to use this handle, it must call meshlink_close() to
+ *  free its resources.
  *
  *  This function does not start any network I/O yet. The application should
  *  first set callbacks, and then call meshlink_start().
  *
  *  @param confbase The directory in which MeshLink will store its configuration files.
+ *                  After the function returns, the application is free to overwrite or free @a confbase @a.
  *  @param name     The name which this instance of the application will use in the mesh.
+ *                  After the function returns, the application is free to overwrite or free @a name @a.
  *
- *  @return         This function will return true if MeshLink has succesfully set up its configuration files, false otherwise.
+ *  @return         A pointer to a meshlink_handle_t which represents this instance of MeshLink.
+ *                  The pointer is valid until meshlink_close() is called.
  */
 extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name);
 
@@ -111,8 +121,8 @@ extern void meshlink_stop(meshlink_handle_t *mesh);
 
 /// Close the MeshLink handle.
 /** This function calls meshlink_stop() if necessary,
- *  and frees all memory allocated by MeshLink.
- *  Afterwards, the handle and any pointers to a struct meshlink_node are invalid.
+ *  and frees the struct meshlink_handle and all associacted memory allocated by MeshLink.
+ *  Afterwards, the handle and any pointers to a struct meshlink_node or struct meshlink_channel are invalid.
  *
  *  @param mesh     A handle which represents an instance of MeshLink.
  */
@@ -122,6 +132,8 @@ extern void meshlink_close(meshlink_handle_t *mesh);
 /** @param mesh      A handle which represents an instance of MeshLink.
  *  @param source    A pointer to a meshlink_node_t describing the source of the data.
  *  @param data      A pointer to a buffer containing the data sent by the source.
+ *                   The pointer is only valid during the lifetime of the callback.
+ *                   The callback should mempcy() the data if it needs to be available outside the callback.
  *  @param len       The length of the received data.
  */
 typedef void (*meshlink_receive_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len);
@@ -135,6 +147,7 @@ typedef void (*meshlink_receive_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *
  *
  *  @param mesh      A handle which represents an instance of MeshLink.
  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
+ *                   If a NULL pointer is given, the callback will be disabled.
  */
 extern void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb);
 
@@ -154,6 +167,7 @@ typedef void (*meshlink_node_status_cb_t)(meshlink_handle_t *mesh, meshlink_node
  *
  *  @param mesh      A handle which represents an instance of MeshLink.
  *  @param cb        A pointer to the function which will be called when another node's status changes.
+ *                   If a NULL pointer is given, the callback will be disabled.
  */
 extern void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb);
 
@@ -170,6 +184,8 @@ typedef enum {
 /** @param mesh      A handle which represents an instance of MeshLink.
  *  @param level     An enum describing the severity level of the message.
  *  @param text      A pointer to a string containing the textual log message.
+ *                   This pointer is only valid for the duration of the callback.
+ *                   The application should strdup() the text if it has to be available outside the callback.
  */
 typedef void (*meshlink_log_cb_t)(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text);
 
@@ -183,6 +199,7 @@ typedef void (*meshlink_log_cb_t)(meshlink_handle_t *mesh, meshlink_log_level_t
  *  @param mesh      A handle which represents an instance of MeshLink.
  *  @param level     An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
+ *                   If a NULL pointer is given, the callback will be disabled.
  */
 extern void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb);
 
@@ -196,6 +213,7 @@ extern void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t le
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param destination  A pointer to a meshlink_node_t describing the destination for the data.
  *  @param data         A pointer to a buffer containing the data to be sent to the source.
+ *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
  *  @param len          The length of the data.
  *  @return             This function will return true if MeshLink has queued the message for transmission, and false otherwise.
  *                      A return value of true does not guarantee that the message will actually arrive at the destination.
@@ -207,9 +225,11 @@ extern bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination,
  *
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param name         The name of the node for which a handle is requested.
+ *                      After this function returns, the application is free to overwrite or free @a name @a.
  *
  *  @return             A pointer to a meshlink_node_t which represents the requested node,
  *                      or NULL if the requested node does not exist.
+ *                      The pointer is guaranteed to be valid until meshlink_close() is called.
  */
 extern meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name);
 
@@ -218,11 +238,13 @@ extern meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *n
  *
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param nodes        A pointer to an array of pointers to meshlink_node_t, which should be allocated by the application.
+ *                      The pointers are valid until meshlink_close() is called.
  *  @param nmemb        The maximum number of pointers that can be stored in the nodes array.
  *
- *  @return             The number of known nodes. This can be larger than nmemb, in which case not all nodes were stored in the nodes array.
+ *  @return             The number of known nodes, or -1 in case of an error.
+ *                      The returned number of nodes can be larger than nmemb, in which case not all nodes were stored in the nodes array.
  */
-extern size_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t nmemb);
+extern ssize_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t nmemb);
 
 /// Sign data using the local node's MeshLink key.
 /** This function signs data using the local node's MeshLink key.
@@ -232,6 +254,7 @@ extern size_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **
  *  @param data         A pointer to a buffer containing the data to be signed.
  *  @param len          The length of the data to be signed.
  *  @param signature    A pointer to a buffer where the signature will be stored.
+ *                      The buffer must be allocated by the application, and should be at least MESHLINK_SIGLEN bytes big.
  *  @param siglen       The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
  *
  *  @return             This function returns true if the signature was correctly generated, false otherwise.
@@ -245,8 +268,9 @@ extern bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len,
  *  @param source       A pointer to a meshlink_node_t describing the source of the signature.
  *  @param data         A pointer to a buffer containing the data to be verified.
  *  @param len          The length of the data to be verified.
- *  @param signature    A pointer to a string containing the signature.
- *  @param siglen       The size of the signature.
+ *  @param signature    A pointer to a buffer where the signature will be stored.
+ *  @param siglen       A pointer to a variable holding the size of the signature buffer.
+ *                      The contents of the variable will be changed by meshlink_sign() to reflect the actual size of the signature.
  *
  *  @return             This function returns true if the signature is valid, false otherwise.
  */
@@ -270,6 +294,7 @@ extern bool meshlink_add_address(meshlink_handle_t *mesh, const char *address);
  *
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param name         The name that the invitee will use in the mesh.
+ *                      After this function returns, the application is free to overwrite or free @a name @a.
  *
  *  @return             This function returns a string that contains the invitation URL.
  *                      The application should call free() after it has finished using the URL.
@@ -283,6 +308,7 @@ extern char *meshlink_invite(meshlink_handle_t *mesh, const char *name);
  *
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param invitation   A string containing the invitation URL.
+ *                      After this function returns, the application is free to overwrite or free @a invitation @a.
  *
  *  @return             This function returns true if the local node joined the mesh it was invited to, false otherwise.
  */
@@ -306,6 +332,7 @@ extern char *meshlink_export(meshlink_handle_t *mesh);
  *
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param data         A string containing the other node's exported key and addresses.
+ *                      After this function returns, the application is free to overwrite or free @a data @a.
  *
  *  @return             This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
  */
@@ -327,9 +354,17 @@ extern void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node);
  *
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param channel      A handle for the incoming channel.
+ *                      If the application accepts the incoming channel by returning true,
+ *                      then this handle is valid until meshlink_channel_close() is called on it.
+ *                      If the application rejects the incoming channel by returning false,
+ *                      then this handle is invalid after the callback returns
+ *                      (the callback does not need to call meshlink_channel_close() itself in this case).
  *  @param node         The node from which this channel is being initiated.
+ *                      The pointer is guaranteed to be valid until meshlink_close() is called.
  *  @param port         The port number the peer wishes to connect to.
  *  @param data         A pointer to a buffer containing data already received. (Not yet used.)
+ *                      The pointer is only valid during the lifetime of the callback.
+ *                      The callback should mempcy() the data if it needs to be available outside the callback.
  *  @param len          The length of the data. (Not yet used.)
  *
  *  @return             This function should return true if the application accepts the incoming channel, false otherwise.
@@ -344,6 +379,8 @@ typedef bool (*meshlink_channel_accept_cb_t)(meshlink_handle_t *mesh, meshlink_c
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param channel      A handle for the channel.
  *  @param data         A pointer to a buffer containing data sent by the source.
+ *                      The pointer is only valid during the lifetime of the callback.
+ *                      The callback should mempcy() the data if it needs to be available outside the callback.
  *  @param len          The length of the data.
  */
 typedef void (*meshlink_channel_receive_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
@@ -355,8 +392,11 @@ typedef void (*meshlink_channel_receive_cb_t)(meshlink_handle_t *mesh, meshlink_
  *  to hand the data over to the application's thread.
  *  The callback should also not block itself and return as quickly as possible.
  *
+ *  If no accept callback is set, incoming channels are rejected.
+ *
  *  @param mesh      A handle which represents an instance of MeshLink.
  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
+ *                   If a NULL pointer is given, the callback will be disabled.
  */
 extern void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb);
 
@@ -370,6 +410,7 @@ extern void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_cha
  *  @param mesh      A handle which represents an instance of MeshLink.
  *  @param channel   A handle for the channel.
  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
+ *                   If a NULL pointer is given, the callback will be disabled.
  */
 extern void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb);
 
@@ -377,14 +418,20 @@ extern void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_ch
 /** This function is called whenever a remote node wants to open a channel to the local node.
  *  The application then has to decide whether to accept or reject this channel.
  *
+ *  This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
+ *  When the application does no longer need to use this channel, it must call meshlink_close()
+ *  to free its resources.
+ *
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param node         The node to which this channel is being initiated.
  *  @param port         The port number the peer wishes to connect to.
  *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
  *  @param data         A pointer to a buffer containing data to already queue for sending.
+ *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
  *  @param len          The length of the data.
  *
  *  @return             A handle for the channel, or NULL in case of an error.
+ *                      The handle is valid until meshlink_channel_close() is called.
  */
 extern meshlink_channel_t *meshlink_channel_open(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len);
 
@@ -393,6 +440,8 @@ extern meshlink_channel_t *meshlink_channel_open(meshlink_handle_t *mesh, meshli
  *  It can be used to inform the remote node that the local node has finished sending all data on the channel,
  *  but still allows waiting for incoming data from the remote node.
  *
+ *  Shutting down the receive direction is also possible, and is equivalent to setting the receive callback to NULL.
+ *
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param channel      A handle for the channel.
  *  @param direction    Must be one of SHUT_RD, SHUT_WR or SHUT_RDWR.
@@ -402,6 +451,7 @@ extern void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_
 /// Close a reliable stream channel.
 /** This informs the remote node that the local node has finished sending all data on the channel.
  *  It also causes the local node to stop accepting incoming data from the remote node.
+ *  It will free the struct meshlink_channel and all associated resources.
  *  Afterwards, the channel handle is invalid and must not be used any more.
  *
  *  @param mesh         A handle which represents an instance of MeshLink.
@@ -415,6 +465,7 @@ extern void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *
  *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param channel      A handle for the channel.
  *  @param data         A pointer to a buffer containing data sent by the source.
+ *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
  *  @param len          The length of the data.
  *
  *  @return             The amount of data that was queued, which can be less than len, or a negative value in case of an error.