]> git.meshlink.io Git - meshlink/commitdiff
Merge branch 'address_hint_api' into discovery
authorNiklas Hofmann <niklas.hofmann@everbase.net>
Wed, 30 Jul 2014 23:51:55 +0000 (01:51 +0200)
committerNiklas Hofmann <niklas.hofmann@everbase.net>
Wed, 30 Jul 2014 23:51:55 +0000 (01:51 +0200)
1  2 
src/meshlink.c
src/meshlink.h

diff --combined src/meshlink.c
index f9e7e9054d291c308b6987a6d4460794e6c030f7,bb17fcac85caeabd240e04441630e66a5d211453..8d120813a1fb57067ba33ca8a274d964d31fd7dd
@@@ -21,6 -21,8 +21,8 @@@
  #define VAR_MULTIPLE 4  /* Multiple statements allowed */
  #define VAR_OBSOLETE 8  /* Should not be used anymore */
  #define VAR_SAFE 16     /* Variable is safe when accepting invitations */
+ #define MAX_ADDRESS_LENGTH 45 /* Max length of an (IPv6) address */
+ #define MAX_PORT_LENGTH 5 /* 0-65535 */
  typedef struct {
        const char *name;
        int type;
@@@ -39,7 -41,6 +41,7 @@@
  #include "utils.h"
  #include "xalloc.h"
  #include "ed25519/sha512.h"
 +#include "discovery.h"
  
  #ifndef MSG_NOSIGNAL
  #define MSG_NOSIGNAL 0
@@@ -626,15 -627,8 +628,15 @@@ static bool sendline(int fd, char *form
  
  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) {
@@@ -652,7 -646,6 +654,7 @@@ static bool ecdsa_keygen(meshlink_handl
  
        if(!(key = ecdsa_generate())) {
                fprintf(stderr, "Error during key generation!\n");
 +              meshlink_errno = MESHLINK_EINTERNAL;
                return false;
        } else
                fprintf(stderr, "Done.\n");
        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);
                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);
  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;
        }
  
  
        if(mkdir(filename, 0777) && errno != EEXIST) {
                fprintf(stderr, "Could not create directory %s: %s\n", filename, strerror(errno));
 +              meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
  
  
        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);
  
@@@ -747,7 -730,6 +749,7 @@@ meshlink_handle_t *meshlink_open(const 
  
        if(!confbase || !*confbase) {
                fprintf(stderr, "No confbase given!\n");
 +              meshlink_errno = MESHLINK_EINVAL;
                return NULL;
        }
  
  
                if(!check_id(name)) {
                        fprintf(stderr, "Invalid name given!\n");
 +                      meshlink_errno = MESHLINK_EINVAL;
                        return NULL;
                } else { usingname = true;}
        }
        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;
                }
        }
  
  
        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;
        // 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;
  }
@@@ -834,17 -804,14 +836,17 @@@ static void *meshlink_main_loop(void *a
  }
  
  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;
        }
  
        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;
        }
  
        mesh->threadstarted=true;
  
 +      // Start discovery
 +      if(!discovery_start(mesh))
 +              return false;
 +
        return true;
  }
  
  void meshlink_stop(meshlink_handle_t *mesh) {
 -      if(!mesh)
 +      if(!mesh) {
 +              meshlink_errno = MESHLINK_EINVAL;
                return;
 +      }
 +
 +      // Stop discovery
 +      discovery_stop(mesh);
  
        // Shut down the listening sockets to signal the main thread to shut down
  
  }
  
  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.
  
  }
  
  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. */
  
@@@ -1007,10 -947,8 +1009,10 @@@ void meshlink_send_from_queue(event_loo
  }
  
  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)
                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));
        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 {
  }
  
  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);
  }
  
@@@ -1127,7 -1020,6 +1129,7 @@@ static bool refresh_invitation_key(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;
        }
  
        DIR *dir = opendir(filename);
        if(!dir) {
                fprintf(stderr, "Could not read directory %s: %s\n", filename, strerror(errno));
 +              meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
  
        if(errno) {
                fprintf(stderr, "Error while reading directory %s: %s\n", filename, strerror(errno));
                closedir(dir);
 +              meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
  
        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);
        } 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;
        }
  
  }
  
  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;
        }
  
        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;
        }
  
        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];
  
        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");
                fclose(tc);
        } else {
                fprintf(stderr, "Could not create %s: %s\n", filename, strerror(errno));
 +              meshlink_errno = MESHLINK_ESTORAGE;
                return NULL;
        }
  
  }
  
  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];
  
        // 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);
  
  
        // 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;
        }
  
                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;
        }
  
        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;
        }
  
        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;
        }
  
        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;
  
                        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);
  
        if(!mesh->success) {
                fprintf(stderr, "Connection closed by peer, invitation cancelled.\n");
 +              meshlink_errno = MESHLINK_EPEER;
                return false;
        }
  
  
  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;
        }
  
        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;
        }
  
  }
  
  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;
        }
  
        name[len] = 0;
        if(!check_id(name)) {
                fprintf(stderr, "Invalid Name\n");
 +              meshlink_errno = MESHLINK_EPEER;
                return false;
        }
  
        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;
        }
  
  }
  
  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;
  }
  
  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;
        return;
  }
  
+ /* Hint that a hostname may be found at an address
+  * See header file for detailed comment.
+  */
+ extern void meshlink_hint_address(meshlink_handle_t *mesh, char *hostname, struct sockaddr *addr) {
+       if(!mesh || !hostname || !addr)
+               return;
+       
+       node_t *n = NULL;
+       char *addr_str = malloc(MAX_ADDRESS_LENGTH*sizeof(char));
+       memset(addr_str, 0, MAX_ADDRESS_LENGTH*sizeof(char));
+       char *port_str = malloc(MAX_PORT_LENGTH*sizeof(char));
+       memset(port_str, 0, MAX_PORT_LENGTH*sizeof(char));
+       
+       // extra byte for a space, and one to make sure string is null-terminated
+       int full_addr_len = MAX_ADDRESS_LENGTH + MAX_PORT_LENGTH + 2;
+       char *full_addr_str = malloc(full_addr_len*sizeof(char));
+       memset(full_addr_str, 0, full_addr_len*sizeof(char));
+       
+       // check that hostname matches an existing node
+       n = lookup_node(mesh, hostname);
+       if(!n)
+               return;
+       // get address and port number
+       if(!get_ip_str(addr, addr_str, MAX_ADDRESS_LENGTH))
+               return;
+       if(!get_port_str(addr, port_str, MAX_ADDRESS_LENGTH))
+               return;
+       // append_config_file expects an address, a space, and then a port number
+       strcat(full_addr_str, addr_str);
+       strcat(full_addr_str, " ");
+       strcat(full_addr_str, port_str);
+       
+       append_config_file(mesh, n->name, "Address", full_addr_str);
+       free(addr_str);
+       free(port_str);
+       free(full_addr_str);
+       // TODO do we want to fire off a connection attempt right away?
+ }
  static void __attribute__((constructor)) meshlink_init(void) {
        crypto_init();
  }
diff --combined src/meshlink.h
index ee2c3f0ae41e556932c875259750972d9b513719,b3059c39a73c50bab102f92b57647bd37847d92b..8a101741635ba4f60f7bec8be3ce3e1e8012dbda
@@@ -44,15 -44,8 +44,15 @@@ typedef struct meshlink_channel meshlin
  /// Code of most recent error encountered.
  typedef enum {
        MESHLINK_OK,     ///< Everything is fine
 +      MESHLINK_EINVAL, ///< Invalid parameter(s) to function call
        MESHLINK_ENOMEM, ///< Out of memory
        MESHLINK_ENOENT, ///< Node is not known
 +      MESHLINK_EEXIST, ///< Node already exists
 +      MESHLINK_EINTERNAL, ///< MeshLink internal error
 +      MESHLINK_ERESOLV, ///< MeshLink could not resolve a hostname
 +      MESHLINK_ESTORAGE, ///< MeshLink coud not load or write data from/to disk
 +      MESHLINK_ENETWORK, ///< MeshLink encountered a network error
 +      MESHLINK_EPEER, ///< A peer caused an error
  } meshlink_errno_t;
  
  /// A variable holding the last encountered error from MeshLink.
@@@ -66,8 -59,6 +66,8 @@@ extern __thread meshlink_errno_t meshli
  #ifndef MESHLINK_INTERNAL_H
  
  struct meshlink_handle {
 +      const char *name;
 +      void *priv;
  };
  
  struct meshlink_node {
@@@ -277,18 -268,6 +277,18 @@@ extern ssize_t meshlink_get_pmtu(meshli
   */
  extern meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name);
  
 +/// Get the fingerprint of a node's public key.
 +/** This function returns a fingerprint of the node's public key.
 + *  It should be treated as an opaque blob.
 + *
 + *  @param mesh         A handle which represents an instance of MeshLink.
 + *  @param node         A pointer to a meshlink_node_t describing the node.
 + *
 + *  @return           A nul-terminated C string containing the fingerprint of the node's public key in a printable ASCII format.
 + *                      The application should call free() after it is done using this string.
 + */
 +extern char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node);
 +
  /// Get a list of all nodes.
  /** This function returns a list with handles for all known nodes.
   *
@@@ -562,6 -541,20 +562,20 @@@ extern void meshlink_channel_close(mesh
   */
  extern ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
  
+ /// Hint that a hostname may be found at an address
+ /** This function indicates to meshlink that the given hostname is likely found
+  *  at the given IP address and port.
+  *
+  *  @param mesh               A handle which represents an instance of MeshLink.
+  *  @param hostname   The hostname which can be found at the given address.
+  *                    The caller is free to overwrite or free this string
+  *                    once meshlink returns.
+  *  @param addr               The IP address and port which should be tried for the
+  *                    given hostname. The caller is free to overwrite or free
+  *                    this memory once meshlink returns.
+  */
+ extern void meshlink_hint_address(meshlink_handle_t *mesh, char *hostname, struct sockaddr *addr);
  #ifdef __cplusplus
  }
  #endif