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) {
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);
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;
}
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;
}
}
void meshlink_stop(meshlink_handle_t *mesh) {
- if(!mesh)
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
return;
+ }
// 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. */
}
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)
}
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 && !nodes)) {
+ meshlink_errno = MESHLINK_EINVAL;
return NULL;
+ }
meshlink_node_t **result, **p;
}
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);
}
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;