]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Merge remote-tracking branch 'origin/everbase' into discovery
[meshlink] / src / meshlink.c
index d33c0d172914631b6946b8f8b1dfc33aa9147068..f9e7e9054d291c308b6987a6d4460794e6c030f7 100644 (file)
@@ -626,8 +626,15 @@ static bool sendline(int fd, char *format, ...) {
 
 static const char *errstr[] = {
        [MESHLINK_OK] = "No error",
+       [MESHLINK_EINVAL] = "Invalid argument",
        [MESHLINK_ENOMEM] = "Out of memory",
        [MESHLINK_ENOENT] = "No such node",
+       [MESHLINK_EEXIST] = "Node already exists",
+       [MESHLINK_EINTERNAL] = "Internal error",
+       [MESHLINK_ERESOLV] = "Could not resolve hostname",
+       [MESHLINK_ESTORAGE] = "Storage error",
+       [MESHLINK_ENETWORK] = "Network error",
+       [MESHLINK_EPEER] = "Error communicating with peer",
 };
 
 const char *meshlink_strerror(meshlink_errno_t err) {
@@ -645,6 +652,7 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) {
 
        if(!(key = ecdsa_generate())) {
                fprintf(stderr, "Error during key generation!\n");
+               meshlink_errno = MESHLINK_EINTERNAL;
                return false;
        } else
                fprintf(stderr, "Done.\n");
@@ -652,8 +660,10 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) {
        snprintf(privname, sizeof privname, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
        f = fopen(privname, "w");
 
-       if(!f)
+       if(!f) {
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
+       }
 
 #ifdef HAVE_FCHMOD
        fchmod(fileno(f), 0600);
@@ -663,17 +673,19 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) {
                fprintf(stderr, "Error writing private key!\n");
                ecdsa_free(key);
                fclose(f);
+               meshlink_errno = MESHLINK_EINTERNAL;
                return false;
        }
 
        fclose(f);
 
-
        snprintf(pubname, sizeof pubname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->name);
        f = fopen(pubname, "a");
 
-       if(!f)
+       if(!f) {
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
+       }
 
        char *pubkey = ecdsa_get_base64_public_key(key);
        fprintf(f, "ECDSAPublicKey = %s\n", pubkey);
@@ -688,6 +700,7 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) {
 static bool meshlink_setup(meshlink_handle_t *mesh) {
        if(mkdir(mesh->confbase, 0777) && errno != EEXIST) {
                fprintf(stderr, "Could not create directory %s: %s\n", mesh->confbase, strerror(errno));
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
 
@@ -696,6 +709,7 @@ static bool meshlink_setup(meshlink_handle_t *mesh) {
 
        if(mkdir(filename, 0777) && errno != EEXIST) {
                fprintf(stderr, "Could not create directory %s: %s\n", filename, strerror(errno));
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
 
@@ -703,20 +717,24 @@ static bool meshlink_setup(meshlink_handle_t *mesh) {
 
        if(!access(filename, F_OK)) {
                fprintf(stderr, "Configuration file %s already exists!\n", filename);
+               meshlink_errno = MESHLINK_EEXIST;
                return false;
        }
 
        FILE *f = fopen(filename, "w");
        if(!f) {
                fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
-               return 1;
+               meshlink_errno = MESHLINK_ESTORAGE;
+               return false;
        }
 
        fprintf(f, "Name = %s\n", mesh->name);
        fclose(f);
 
-       if(!ecdsa_keygen(mesh))
+       if(!ecdsa_keygen(mesh)) {
+               meshlink_errno = MESHLINK_EINTERNAL;
                return false;
+       }
 
        check_port(mesh);
 
@@ -729,6 +747,7 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
 
        if(!confbase || !*confbase) {
                fprintf(stderr, "No confbase given!\n");
+               meshlink_errno = MESHLINK_EINVAL;
                return NULL;
        }
 
@@ -740,6 +759,7 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
 
                if(!check_id(name)) {
                        fprintf(stderr, "Invalid name given!\n");
+                       meshlink_errno = MESHLINK_EINVAL;
                        return NULL;
                } else { usingname = true;}
        }
@@ -764,10 +784,15 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
        if(access(filename, R_OK)) {
                if(errno == ENOENT) {
                        // If not, create it
-                       meshlink_setup(mesh);
+                       if(!meshlink_setup(mesh)) {
+                               // meshlink_errno is set by meshlink_setup()
+                               return NULL;
+                       }
                } else {
                        fprintf(stderr, "Cannot not read from %s: %s\n", filename, strerror(errno));
-                       return meshlink_close(mesh), NULL;
+                       meshlink_close(mesh);
+                       meshlink_errno = MESHLINK_ESTORAGE;
+                       return NULL;
                }
        }
 
@@ -775,8 +800,11 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
 
        init_configuration(&mesh->config);
 
-       if(!read_server_config(mesh))
-               return meshlink_close(mesh), NULL;
+       if(!read_server_config(mesh)) {
+               meshlink_close(mesh);
+               meshlink_errno = MESHLINK_ESTORAGE;
+               return NULL;
+       };
 
 #ifdef HAVE_MINGW
        struct WSAData wsa_state;
@@ -786,8 +814,11 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
        // Setup up everything
        // TODO: we should not open listening sockets yet
 
-       if(!setup_network(mesh))
-               return meshlink_close(mesh), NULL;
+       if(!setup_network(mesh)) {
+               meshlink_close(mesh);
+               meshlink_errno = MESHLINK_ENETWORK;
+               return NULL;
+       }
 
        return mesh;
 }
@@ -803,14 +834,17 @@ static void *meshlink_main_loop(void *arg) {
 }
 
 bool meshlink_start(meshlink_handle_t *mesh) {
-       if(!mesh)
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
+       }
 
        // TODO: open listening sockets first
 
        //Check that a valid name is set
        if(!mesh->name ) {
                fprintf(stderr, "No name given!\n");
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
        }
 
@@ -819,6 +853,7 @@ bool meshlink_start(meshlink_handle_t *mesh) {
        if(pthread_create(&mesh->thread, NULL, meshlink_main_loop, mesh) != 0) {
                fprintf(stderr, "Could not start thread: %s\n", strerror(errno));
                memset(&mesh->thread, 0, sizeof mesh->thread);
+               meshlink_errno = MESHLINK_EINTERNAL;
                return false;
        }
 
@@ -832,8 +867,10 @@ bool meshlink_start(meshlink_handle_t *mesh) {
 }
 
 void meshlink_stop(meshlink_handle_t *mesh) {
-       if(!mesh)
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
                return;
+       }
 
        // Stop discovery
        discovery_stop(mesh);
@@ -851,8 +888,10 @@ void meshlink_stop(meshlink_handle_t *mesh) {
 }
 
 void meshlink_close(meshlink_handle_t *mesh) {
-       if(!mesh || !mesh->confbase)
+       if(!mesh || !mesh->confbase) {
+               meshlink_errno = MESHLINK_EINVAL;
                return;
+       }
 
        // Close and free all resources used.
 
@@ -879,31 +918,46 @@ void meshlink_close(meshlink_handle_t *mesh) {
 }
 
 void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb) {
-       if(!mesh)
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
                return;
+       }
+
        mesh->receive_cb = cb;
 }
 
 void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
-       if(!mesh)
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
                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)
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
                return;
+       }
+
        mesh->log_cb = cb;
        mesh->log_level = level;
 }
 
 bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len) {
-       if(!mesh || !destination)
+       if(!mesh || !destination) {
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
+       }
+
        if(!len)
                return true;
-       if(!data)
+
+       if(!data) {
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
+       }
 
        /* If there is no outgoing list yet, create one. */
 
@@ -953,8 +1007,10 @@ void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh) {
 }
 
 ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination) {
-       if(!mesh || !destination)
+       if(!mesh || !destination) {
+               meshlink_errno = MESHLINK_EINVAL;
                return -1;
+       }
 
        node_t *n = (node_t *)destination;
        if(!n->status.reachable)
@@ -965,17 +1021,43 @@ ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination)
                return MTU;
 }
 
+char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node) {
+       if(!mesh || !node) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return NULL;
+       }
+
+       node_t *n = (node_t *)node;
+
+       if(!node_read_ecdsa_public_key(mesh, n) || !n->ecdsa) {
+               meshlink_errno = MESHLINK_EINTERNAL;
+               return false;
+       }
+
+       char *fingerprint = ecdsa_get_base64_public_key(n->ecdsa);
+
+       if(!fingerprint)
+               meshlink_errno = MESHLINK_EINTERNAL;
+
+       return fingerprint;
+}
+
 meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) {
-       if(!mesh || !name)
+       if(!mesh || !name) {
+               meshlink_errno = MESHLINK_EINVAL;
                return NULL;
+       }
+
        return (meshlink_node_t *)lookup_node(mesh, (char *)name); // TODO: make lookup_node() use const
 }
 
 meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t *nmemb) {
-       if(!mesh || (nmemb && !nodes))
+       if(!mesh || !nmemb || (*nmemb && !nodes)) {
+               meshlink_errno = MESHLINK_EINVAL;
                return NULL;
+       }
 
-       meshlink_node_t **result, **p;
+       meshlink_node_t **result;
 
        //lock mesh->nodes
        pthread_mutex_lock(&(mesh->nodes_mutex));
@@ -984,6 +1066,7 @@ meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_
        result = realloc(nodes, *nmemb * sizeof *nodes);
 
        if(result) {
+               meshlink_node_t **p = result;
                for splay_each(node_t, n, mesh->nodes)
                        *p++ = (meshlink_node_t *)n;
        } else {
@@ -998,25 +1081,43 @@ meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_
 }
 
 bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen) {
-       if(!mesh || !data || !len || !signature || !siglen)
+       if(!mesh || !data || !len || !signature || !siglen) {
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
-       if(*siglen < MESHLINK_SIGLEN)
+       }
+
+       if(*siglen < MESHLINK_SIGLEN) {
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
-       if(!ecdsa_sign(mesh->self->connection->ecdsa, data, len, signature))
+       }
+
+       if(!ecdsa_sign(mesh->self->connection->ecdsa, data, len, signature)) {
+               meshlink_errno = MESHLINK_EINTERNAL;
                return false;
+       }
+
        *siglen = MESHLINK_SIGLEN;
        return true;
 }
 
 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)
+       if(!mesh || !data || !len || !signature) {
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
-       if(siglen != MESHLINK_SIGLEN)
+       }
+
+       if(siglen != MESHLINK_SIGLEN) {
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
+       }
+
        struct node_t *n = (struct node_t *)source;
        node_read_ecdsa_public_key(mesh, n);
-       if(!n->ecdsa)
+       if(!n->ecdsa) {
+               meshlink_errno = MESHLINK_EINTERNAL;
                return false;
+       }
+
        return ecdsa_verify(((struct node_t *)source)->ecdsa, data, len, signature);
 }
 
@@ -1026,6 +1127,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) {
        snprintf(filename, sizeof filename, "%s" SLASH "invitations", mesh->confbase);
        if(mkdir(filename, 0700) && errno != EEXIST) {
                fprintf(stderr, "Could not create directory %s: %s\n", filename, strerror(errno));
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
 
@@ -1033,6 +1135,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) {
        DIR *dir = opendir(filename);
        if(!dir) {
                fprintf(stderr, "Could not read directory %s: %s\n", filename, strerror(errno));
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
 
@@ -1061,6 +1164,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) {
        if(errno) {
                fprintf(stderr, "Error while reading directory %s: %s\n", filename, strerror(errno));
                closedir(dir);
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
 
@@ -1085,17 +1189,20 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) {
        if(!f) {
                if(errno != ENOENT) {
                        fprintf(stderr, "Could not read %s: %s\n", filename, strerror(errno));
+                       meshlink_errno = MESHLINK_ESTORAGE;
                        return false;
                }
 
                mesh->invitation_key = ecdsa_generate();
                if(!mesh->invitation_key) {
                        fprintf(stderr, "Could not generate a new key!\n");
+                       meshlink_errno = MESHLINK_EINTERNAL;
                        return false;
                }
                f = fopen(filename, "w");
                if(!f) {
                        fprintf(stderr, "Could not write %s: %s\n", filename, strerror(errno));
+                       meshlink_errno = MESHLINK_ESTORAGE;
                        return false;
                }
                chmod(filename, 0600);
@@ -1104,21 +1211,26 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) {
        } else {
                mesh->invitation_key = ecdsa_read_pem_private_key(f);
                fclose(f);
-               if(!mesh->invitation_key)
+               if(!mesh->invitation_key) {
                        fprintf(stderr, "Could not read private key from %s\n", filename);
+                       meshlink_errno = MESHLINK_ESTORAGE;
+               }
        }
 
        return mesh->invitation_key;
 }
 
 bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) {
-       if(!mesh || !address)
+       if(!mesh || !address) {
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
+       }
 
        for(const char *p = address; *p; p++) {
                if(isalnum(*p) || *p == '-' || *p == '.' || *p == ':')
                        continue;
                fprintf(stderr, "Invalid character in address: %s\n", address);
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
        }
 
@@ -1126,12 +1238,15 @@ 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;
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return NULL;
+       }
 
        // Check validity of the new node's name
        if(!check_id(name)) {
                fprintf(stderr, "Invalid name for node.\n");
+               meshlink_errno = MESHLINK_EINVAL;
                return NULL;
        }
 
@@ -1140,12 +1255,14 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
        snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
        if(!access(filename, F_OK)) {
                fprintf(stderr, "A host config file for %s already exists!\n", name);
+               meshlink_errno = MESHLINK_EEXIST;
                return NULL;
        }
 
        // Ensure no other nodes know about this name
        if(meshlink_get_node(mesh, name)) {
                fprintf(stderr, "A node with name %s is already known!\n", name);
+               meshlink_errno = MESHLINK_EEXIST;
                return NULL;
        }
 
@@ -1153,11 +1270,14 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
        char *address = get_my_hostname(mesh);
        if(!address) {
                fprintf(stderr, "No Address known for ourselves!\n");
+               meshlink_errno = MESHLINK_ERESOLV;
                return NULL;
        }
 
-       if(!refresh_invitation_key(mesh))
+       if(!refresh_invitation_key(mesh)) {
+               meshlink_errno = MESHLINK_EINTERNAL;
                return NULL;
+       }
 
        char hash[64];
 
@@ -1187,6 +1307,7 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
        int ifd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
        if(!ifd) {
                fprintf(stderr, "Could not create invitation file %s: %s\n", filename, strerror(errno));
+               meshlink_errno = MESHLINK_ESTORAGE;
                return NULL;
        }
        FILE *f = fdopen(ifd, "w");
@@ -1216,6 +1337,7 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
                fclose(tc);
        } else {
                fprintf(stderr, "Could not create %s: %s\n", filename, strerror(errno));
+               meshlink_errno = MESHLINK_ESTORAGE;
                return NULL;
        }
 
@@ -1235,8 +1357,10 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
 }
 
 bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
-       if(!mesh || !invitation)
+       if(!mesh || !invitation) {
+               meshlink_errno = MESHLINK_EINVAL;
                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];
@@ -1277,8 +1401,10 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
 
        // Generate a throw-away key for the invitation.
        ecdsa_t *key = ecdsa_generate();
-       if(!key)
+       if(!key) {
+               meshlink_errno = MESHLINK_EINTERNAL;
                return false;
+       }
 
        char *b64key = ecdsa_get_base64_public_key(key);
 
@@ -1289,13 +1415,16 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
 
        // Connect to the meshlink daemon mentioned in the URL.
        struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
-       if(!ai)
+       if(!ai) {
+               meshlink_errno = MESHLINK_ERESOLV;
                return false;
+       }
 
        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);
+               meshlink_errno = MESHLINK_ENETWORK;
                return false;
        }
 
@@ -1303,6 +1432,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
                fprintf(stderr, "Could not connect to %s port %s: %s\n", address, port, strerror(errno));
                closesocket(mesh->sock);
                freeaddrinfo(ai);
+               meshlink_errno = MESHLINK_ENETWORK;
                return false;
        }
 
@@ -1317,6 +1447,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
        if(!sendline(mesh->sock, "0 ?%s %d.%d", b64key, PROT_MAJOR, 1)) {
                fprintf(stderr, "Error sending request to %s port %s: %s\n", address, port, strerror(errno));
                closesocket(mesh->sock);
+               meshlink_errno = MESHLINK_ENETWORK;
                return false;
        }
 
@@ -1328,6 +1459,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
        if(!recvline(mesh, sizeof mesh->line) || sscanf(mesh->line, "%d %s %d.%d", &code, hisname, &hismajor, &hisminor) < 3 || code != 0 || hismajor != PROT_MAJOR || !check_id(hisname) || !recvline(mesh, sizeof mesh->line) || !rstrip(mesh->line) || sscanf(mesh->line, "%d ", &code) != 1 || code != ACK || strlen(mesh->line) < 3) {
                fprintf(stderr, "Cannot read greeting from peer\n");
                closesocket(mesh->sock);
+               meshlink_errno = MESHLINK_ENETWORK;
                return false;
        }
 
@@ -1336,25 +1468,33 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
        char hishash[64];
        if(sha512(fingerprint, strlen(fingerprint), hishash)) {
                fprintf(stderr, "Could not create hash\n%s\n", mesh->line + 2);
+               meshlink_errno = MESHLINK_EINTERNAL;
                return false;
        }
        if(memcmp(hishash, mesh->hash, 18)) {
                fprintf(stderr, "Peer has an invalid key!\n%s\n", mesh->line + 2);
+               meshlink_errno = MESHLINK_EPEER;
                return false;
 
        }
 
        ecdsa_t *hiskey = ecdsa_set_base64_public_key(fingerprint);
-       if(!hiskey)
+       if(!hiskey) {
+               meshlink_errno = MESHLINK_EINTERNAL;
                return false;
+       }
 
        // Start an SPTPS session
-       if(!sptps_start(&mesh->sptps, mesh, true, false, key, hiskey, "meshlink invitation", 15, invitation_send, invitation_receive))
+       if(!sptps_start(&mesh->sptps, mesh, true, false, key, hiskey, "meshlink invitation", 15, invitation_send, invitation_receive)) {
+               meshlink_errno = MESHLINK_EINTERNAL;
                return false;
+       }
 
        // Feed rest of input buffer to SPTPS
-       if(!sptps_receive_data(&mesh->sptps, mesh->buffer, mesh->blen))
+       if(!sptps_receive_data(&mesh->sptps, mesh->buffer, mesh->blen)) {
+               meshlink_errno = MESHLINK_EPEER;
                return false;
+       }
 
        int len;
 
@@ -1363,11 +1503,14 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
                        if(errno == EINTR)
                                continue;
                        fprintf(stderr, "Error reading data from %s port %s: %s\n", address, port, strerror(errno));
+                       meshlink_errno = MESHLINK_ENETWORK;
                        return false;
                }
 
-               if(!sptps_receive_data(&mesh->sptps, mesh->line, len))
+               if(!sptps_receive_data(&mesh->sptps, mesh->line, len)) {
+                       meshlink_errno = MESHLINK_EPEER;
                        return false;
+               }
        }
 
        sptps_stop(&mesh->sptps);
@@ -1377,6 +1520,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
 
        if(!mesh->success) {
                fprintf(stderr, "Connection closed by peer, invitation cancelled.\n");
+               meshlink_errno = MESHLINK_EPEER;
                return false;
        }
 
@@ -1384,18 +1528,22 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
 
 invalid:
        fprintf(stderr, "Invalid invitation URL or you are already connected to a Mesh ?\n");
+       meshlink_errno = MESHLINK_EINVAL;
        return false;
 }
 
 char *meshlink_export(meshlink_handle_t *mesh) {
-       if(!mesh)
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
                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");
        if(!f) {
                fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
+               meshlink_errno = MESHLINK_ESTORAGE;
                return NULL;
        }
 
@@ -1409,6 +1557,7 @@ char *meshlink_export(meshlink_handle_t *mesh) {
        if(fread(buf + len - fsize - 1, fsize, 1, f) != 1) {
                fprintf(stderr, "Error reading from %s: %s\n", filename, strerror(errno));
                fclose(f);
+               meshlink_errno = MESHLINK_ESTORAGE;
                return NULL;
        }
 
@@ -1418,17 +1567,21 @@ char *meshlink_export(meshlink_handle_t *mesh) {
 }
 
 bool meshlink_import(meshlink_handle_t *mesh, const char *data) {
-       if(!mesh || !data)
+       if(!mesh || !data) {
+               meshlink_errno = MESHLINK_EINVAL;
                return false;
+       }
 
        if(strncmp(data, "Name = ", 7)) {
                fprintf(stderr, "Invalid data\n");
+               meshlink_errno = MESHLINK_EPEER;
                return false;
        }
 
        char *end = strchr(data + 7, '\n');
        if(!end) {
                fprintf(stderr, "Invalid data\n");
+               meshlink_errno = MESHLINK_EPEER;
                return false;
        }
 
@@ -1438,6 +1591,7 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) {
        name[len] = 0;
        if(!check_id(name)) {
                fprintf(stderr, "Invalid Name\n");
+               meshlink_errno = MESHLINK_EPEER;
                return false;
        }
 
@@ -1445,17 +1599,20 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) {
        snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
        if(!access(filename, F_OK)) {
                fprintf(stderr, "File %s already exists, not importing\n", filename);
+               meshlink_errno = MESHLINK_EEXIST;
                return false;
        }
 
        if(errno != ENOENT) {
                fprintf(stderr, "Error accessing %s: %s\n", filename, strerror(errno));
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
 
        FILE *f = fopen(filename, "w");
        if(!f) {
                fprintf(stderr, "Could not create %s: %s\n", filename, strerror(errno));
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
 
@@ -1468,8 +1625,10 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) {
 }
 
 void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) {
-       if(!mesh || !node)
+       if(!mesh || !node) {
+               meshlink_errno = MESHLINK_EINVAL;
                return;
+       }
 
        node_t *n;
        n = (node_t*)node;
@@ -1483,8 +1642,10 @@ void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) {
 }
 
 void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) {
-       if(!mesh || !node)
+       if(!mesh || !node) {
+               meshlink_errno = MESHLINK_EINVAL;
                return;
+       }
 
        node_t *n = (node_t *)node;
        n->status.blacklisted = false;