]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Fix compiling with -Wall -W.
[meshlink] / src / meshlink.c
index 6e03e63bbe7e1b3848ccb8678c2a8b9fd4207d22..094d4983fb5318f86e79c68eba884579f55d7a22 100644 (file)
@@ -310,7 +310,7 @@ static char *get_line(const char **data) {
 
        static char line[1024];
        const char *end = strchr(*data, '\n');
-       size_t len = end ? end - *data : strlen(*data);
+       size_t len = end ? (size_t)(end - *data) : strlen(*data);
        if(len >= sizeof(line)) {
                logger(NULL, MESHLINK_ERROR, "Maximum line length exceeded!\n");
                return NULL;
@@ -553,6 +553,7 @@ static bool finalize_join(meshlink_handle_t *mesh) {
 }
 
 static bool invitation_send(void *handle, uint8_t type, const void *data, size_t len) {
+       (void)type;
        meshlink_handle_t *mesh = handle;
        while(len) {
                int result = send(mesh->sock, data, len, 0);
@@ -611,7 +612,7 @@ static bool recvline(meshlink_handle_t *mesh, size_t len) {
                mesh->blen += result;
        }
 
-       if(newline - mesh->buffer >= len)
+       if((size_t)(newline - mesh->buffer) >= len)
                return false;
 
        len = newline - mesh->buffer;
@@ -633,7 +634,7 @@ static bool sendline(int fd, char *format, ...) {
        blen = vsnprintf(buffer, sizeof(buffer), format, ap);
        va_end(ap);
 
-       if(blen < 1 || blen >= sizeof(buffer))
+       if(blen < 1 || (size_t)blen >= sizeof(buffer))
                return false;
 
        buffer[blen] = '\n';
@@ -666,7 +667,7 @@ static const char *errstr[] = {
 };
 
 const char *meshlink_strerror(meshlink_errno_t err) {
-       if(err < 0 || err >= sizeof(errstr) / sizeof(*errstr))
+       if((int)err < 0 || err >= sizeof(errstr) / sizeof(*errstr))
                return "Invalid error code";
        return errstr[err];
 }
@@ -726,6 +727,7 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) {
 }
 
 static struct timeval idle(event_loop_t *loop, void *data) {
+       (void)loop;
        meshlink_handle_t *mesh = data;
        struct timeval t, tmin = {3600, 0};
        for splay_each(node_t, n, mesh->nodes) {
@@ -872,7 +874,7 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const c
                        usingname = true;
        }
 
-       if(devclass < 0 || devclass > _DEV_CLASS_MAX) {
+       if((int)devclass < 0 || devclass > _DEV_CLASS_MAX) {
                logger(NULL, MESHLINK_ERROR, "Invalid devclass given!\n");
                meshlink_errno = MESHLINK_EINVAL;
                return NULL;
@@ -1208,8 +1210,8 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const
        memset(hdr, 0, sizeof(*hdr));
        // leave the last byte as 0 to make sure strings are always
        // null-terminated if they are longer than the buffer
-       strncpy(hdr->destination, destination->name, (sizeof(hdr)->destination) - 1);
-       strncpy(hdr->source, mesh->self->name, (sizeof(hdr)->source) - 1);
+       strncpy((char *)hdr->destination, destination->name, (sizeof(hdr)->destination) - 1);
+       strncpy((char *)hdr->source, mesh->self->name, (sizeof(hdr)->source) - 1);
 
        memcpy(packet->data + sizeof(*hdr), data, len);
 
@@ -1227,6 +1229,7 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const
 }
 
 void meshlink_send_from_queue(event_loop_t *loop, meshlink_handle_t *mesh) {
+       (void)loop;
        vpn_packet_t *packet = meshlink_queue_pop(&mesh->outpacketqueue);
        if(!packet)
                return;
@@ -1946,6 +1949,7 @@ char *meshlink_export(meshlink_handle_t *mesh) {
        if(fread(buf + len - fsize - 1, fsize, 1, f) != 1) {
                logger(mesh, MESHLINK_DEBUG, "Error reading from %s: %s\n", filename, strerror(errno));
                fclose(f);
+               free(buf);
                meshlink_errno = MESHLINK_ESTORAGE;
                pthread_mutex_unlock(&(mesh->mesh_mutex));
                return NULL;
@@ -2085,7 +2089,7 @@ void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const
        if(host && port) {
                xasprintf(&str, "%s %s", host, port);
                if((strncmp("fe80", host, 4) != 0) && (strncmp("127.", host, 4) != 0) && (strcmp("localhost", host) != 0))
-                       append_config_file(mesh, node->name, "Address", str);
+                       modify_config_file(mesh, node->name, "Address", str, 5);
                else
                        logger(mesh, MESHLINK_DEBUG, "Not adding Link Local IPv6 Address to config\n");
        }
@@ -2098,68 +2102,8 @@ void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const
        // @TODO do we want to fire off a connection attempt right away?
 }
 
-/* Return an array of edges in the current network graph.
- * Data captures the current state and will not be updated.
- * Caller must deallocate data when done.
- */
-meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink_edge_t **edges, size_t *nmemb) {
-       if(!mesh || !nmemb || (*nmemb && !edges)) {
-               meshlink_errno = MESHLINK_EINVAL;
-               return NULL;
-       }
-
-       pthread_mutex_lock(&(mesh->mesh_mutex));
-
-       meshlink_edge_t **result = NULL;
-       meshlink_edge_t *copy = NULL;
-       int result_size = 0;
-
-       result_size = mesh->edges->count;
-
-       // if result is smaller than edges, we have to dealloc all the excess meshlink_edge_t
-       if(result_size > *nmemb)
-               result = realloc(edges, result_size * sizeof(meshlink_edge_t *));
-       else
-               result = edges;
-
-       if(result) {
-               meshlink_edge_t **p = result;
-               int n = 0;
-               for splay_each(edge_t, e, mesh->edges) {
-                       // skip edges that do not represent a two-directional connection
-                       if((!e->reverse) || (e->reverse->to != e->from)) {
-                               result_size--;
-                               continue;
-                       }
-                       n++;
-                       // the first *nmemb members of result can be re-used
-                       if(n > *nmemb)
-                               copy = xzalloc(sizeof(*copy));
-                       else
-                               copy = *p;
-                       copy->from = (meshlink_node_t *)e->from;
-                       copy->to = (meshlink_node_t *)e->to;
-                       copy->address = e->address.storage;
-                       copy->options = e->options;
-                       copy->weight = e->weight;
-                       *p++ = copy;
-               }
-               // shrink result to the actual amount of memory used
-               for(int i = *nmemb; i > result_size; i--)
-                       free(result[i - 1]);
-               result = realloc(result, result_size * sizeof(meshlink_edge_t *));
-               *nmemb = result_size;
-       } else {
-               *nmemb = 0;
-               meshlink_errno = MESHLINK_ENOMEM;
-       }
-
-       pthread_mutex_unlock(&(mesh->mesh_mutex));
-
-       return result;
-}
-
 static bool channel_pre_accept(struct utcp *utcp, uint16_t port) {
+       (void)port;
        node_t *n = utcp->priv;
        meshlink_handle_t *mesh = n->mesh;
        return mesh->channel_accept_cb;
@@ -2197,7 +2141,7 @@ static void channel_accept(struct utcp_connection *utcp_connection, uint16_t por
 static ssize_t channel_send(struct utcp *utcp, const void *data, size_t len) {
        node_t *n = utcp->priv;
        meshlink_handle_t *mesh = n->mesh;
-       return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? len : -1;
+       return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? (ssize_t)len : -1;
 }
 
 void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb) {
@@ -2210,6 +2154,7 @@ void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t
 }
 
 static void channel_receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
+       (void)mesh;
        node_t *n = (node_t *)source;
        if(!n->utcp)
                abort();
@@ -2227,6 +2172,7 @@ static void channel_poll(struct utcp_connection *connection, size_t len) {
 }
 
 void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_poll_cb_t cb) {
+       (void)mesh;
        channel->poll_cb = cb;
        utcp_set_poll_cb(channel->c, cb ? channel_poll : NULL);
 }
@@ -2248,6 +2194,9 @@ void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_ac
 }
 
 meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len, uint32_t flags) {
+       if(data || len)
+               abort(); // TODO: handle non-NULL data
+
        if(!mesh || !node) {
                meshlink_errno = MESHLINK_EINVAL;
                return NULL;