#define MSG_NOSIGNAL 0
#endif
-static pthread_mutex_t global_mutex;
-
__thread meshlink_errno_t meshlink_errno;
meshlink_log_cb_t global_log_cb;
meshlink_log_level_t global_log_level;
}
char *b64key = ecdsa_get_base64_public_key(mesh->self->connection->ecdsa);
- if(!b64key)
+ if(!b64key) {
+ fclose(fh);
return false;
+ }
fprintf(fh, "ECDSAPublicKey = %s\n", b64key);
fprintf(fh, "Port = %s\n", mesh->myport);
break;
case 1:
+ mesh->thedatalen = 0;
return finalize_join(mesh);
case 2:
return true;
}
-meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname, dclass_t dclass) {
- return meshlink_open_with_size(confbase, name, appname, dclass, sizeof(meshlink_handle_t));
+meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname, dev_class_t devclass) {
+ return meshlink_open_with_size(confbase, name, appname, devclass, sizeof(meshlink_handle_t));
}
-meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, const char* appname, dclass_t dclass, size_t size) {
+meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, const char* appname, dev_class_t devclass, size_t size) {
// Validate arguments provided by the application
bool usingname = false;
+
+ logger(NULL, MESHLINK_DEBUG, "meshlink_open called\n");
if(!confbase || !*confbase) {
logger(NULL, MESHLINK_ERROR, "No confbase given!\n");
} else { usingname = true;}
}
+ if(devclass < 0 || devclass > _DEV_CLASS_MAX) {
+ logger(NULL, MESHLINK_ERROR, "Invalid devclass given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
meshlink_handle_t *mesh = xzalloc(size);
mesh->confbase = xstrdup(confbase);
mesh->appname = xstrdup(appname);
- mesh->dclass = dclass;
+ mesh->devclass = devclass;
if (usingname) mesh->name = xstrdup(name);
- pthread_mutex_init ( &(mesh->nodes_mutex), NULL);
+
+ // initialize mutex
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init(&(mesh->mesh_mutex), &attr);
+
mesh->threadstarted = false;
event_loop_init(&mesh->loop);
mesh->loop.data = mesh;
return NULL;
}
+ logger(NULL, MESHLINK_DEBUG, "meshlink_open returning\n");
return mesh;
}
static void *meshlink_main_loop(void *arg) {
meshlink_handle_t *mesh = arg;
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
try_outgoing_connections(mesh);
logger(mesh, MESHLINK_DEBUG, "Starting main_loop...\n");
main_loop(mesh);
logger(mesh, MESHLINK_DEBUG, "main_loop returned.\n");
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return NULL;
}
bool meshlink_start(meshlink_handle_t *mesh) {
-
- logger(mesh, MESHLINK_DEBUG, "meshlink_start called\n");
-
if(!mesh) {
meshlink_errno = MESHLINK_EINVAL;
return false;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ logger(mesh, MESHLINK_DEBUG, "meshlink_start called\n");
+
+ mesh->thedatalen = 0;
// TODO: open listening sockets first
if(!mesh->name ) {
logger(mesh, MESHLINK_DEBUG, "No name given!\n");
meshlink_errno = MESHLINK_EINVAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno));
memset(&mesh->thread, 0, sizeof mesh->thread);
meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
discovery_start(mesh);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return true;
}
void meshlink_stop(meshlink_handle_t *mesh) {
-
- logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n");
-
if(!mesh) {
meshlink_errno = MESHLINK_EINVAL;
return;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n");
+
// Stop discovery
discovery_stop(mesh);
shutdown(s->tcp.fd, SHUT_RDWR);
// Wait for the main thread to finish
-
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
pthread_join(mesh->thread, NULL);
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
mesh->threadstarted = false;
// Fix the socket
logger(mesh, MESHLINK_ERROR, "Could not repair listenen socket!");
else
io_add(&mesh->loop, &s->tcp, handle_new_meta_connection, s, s->tcp.fd, IO_READ);
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
}
void meshlink_close(meshlink_handle_t *mesh) {
return;
}
+ // lock is not released after this
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
// Close and free all resources used.
close_network_connections(mesh);
free(mesh->name);
free(mesh->appname);
free(mesh->confbase);
+ pthread_mutex_destroy(&(mesh->mesh_mutex));
memset(mesh, 0, sizeof *mesh);
return;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
mesh->receive_cb = cb;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
}
void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
return;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
mesh->node_status_cb = cb;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
}
void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb) {
if(mesh) {
+ pthread_mutex_lock(&(mesh->mesh_mutex));
mesh->log_cb = cb;
mesh->log_level = cb ? level : 0;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
} else {
global_log_cb = cb;
global_log_level = cb ? level : 0;
return false;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
//add packet to the queue
outpacketqueue_t *packet_in_queue = xzalloc(sizeof *packet_in_queue);
packet_in_queue->destination=destination;
packet_in_queue->len=len;
if(!meshlink_queue_push(&mesh->outpacketqueue, packet_in_queue)) {
free(packet_in_queue);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
//notify event loop
signal_trigger(&(mesh->loop),&(mesh->datafromapp));
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return true;
}
void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh) {
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
vpn_packet_t packet;
meshlink_packethdr_t *hdr = (meshlink_packethdr_t *)packet.data;
outpacketqueue_t* p = meshlink_queue_pop(&mesh->outpacketqueue);
if(!p)
+ {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return;
+ }
if (sizeof(meshlink_packethdr_t) + p->len > MAXSIZE) {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
//log something
- return ;
+ return;
}
packet.probe = false;
mesh->self->in_packets++;
mesh->self->in_bytes += packet.len;
route(mesh, mesh->self, &packet);
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return ;
}
meshlink_errno = MESHLINK_EINVAL;
return -1;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
node_t *n = (node_t *)destination;
- if(!n->status.reachable)
+ if(!n->status.reachable) {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return 0;
- else if(n->mtuprobes > 30 && n->minmtu)
+
+ }
+ else if(n->mtuprobes > 30 && n->minmtu) {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return n->minmtu;
- else
+ }
+ else {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return MTU;
+ }
}
char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node) {
meshlink_errno = MESHLINK_EINVAL;
return NULL;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
node_t *n = (node_t *)node;
if(!node_read_ecdsa_public_key(mesh, n) || !n->ecdsa) {
meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(!fingerprint)
meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return fingerprint;
}
return NULL;
}
- return (meshlink_node_t *)lookup_node(mesh, (char *)name); // TODO: make lookup_node() use const
+ meshlink_node_t *node = NULL;
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ node = (meshlink_node_t *)lookup_node(mesh, (char *)name); // TODO: make lookup_node() use const
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return node;
}
meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t *nmemb) {
meshlink_node_t **result;
//lock mesh->nodes
- pthread_mutex_lock(&(mesh->nodes_mutex));
+ pthread_mutex_lock(&(mesh->mesh_mutex));
*nmemb = mesh->nodes->count;
result = realloc(nodes, *nmemb * sizeof *nodes);
meshlink_errno = MESHLINK_ENOMEM;
}
- pthread_mutex_unlock(&(mesh->nodes_mutex));
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return result;
}
return false;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
if(!ecdsa_sign(mesh->self->connection->ecdsa, data, len, signature)) {
meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
*siglen = MESHLINK_SIGLEN;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return true;
}
return false;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ bool rval = false;
+
struct node_t *n = (struct node_t *)source;
node_read_ecdsa_public_key(mesh, n);
if(!n->ecdsa) {
meshlink_errno = MESHLINK_EINTERNAL;
- return false;
+ rval = false;
+ } else {
+ rval = ecdsa_verify(((struct node_t *)source)->ecdsa, data, len, signature);
}
-
- return ecdsa_verify(((struct node_t *)source)->ecdsa, data, len, signature);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return rval;
}
static bool refresh_invitation_key(meshlink_handle_t *mesh) {
char filename[PATH_MAX];
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
snprintf(filename, sizeof filename, "%s" SLASH "invitations", mesh->confbase);
if(mkdir(filename, 0700) && errno != EEXIST) {
logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", filename, strerror(errno));
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(!dir) {
logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", filename, strerror(errno));
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", filename, strerror(errno));
closedir(dir);
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
}
}
- if(mesh->invitation_key)
+ if(mesh->invitation_key) {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return true;
+ }
// Create a new key if necessary.
FILE *f = fopen(filename, "r");
if(errno != ENOENT) {
logger(mesh, MESHLINK_DEBUG, "Could not read %s: %s\n", filename, strerror(errno));
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(!mesh->invitation_key) {
logger(mesh, MESHLINK_DEBUG, "Could not generate a new key!\n");
meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
f = fopen(filename, "w");
if(!f) {
logger(mesh, MESHLINK_DEBUG, "Could not write %s: %s\n", filename, strerror(errno));
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
chmod(filename, 0600);
}
}
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return mesh->invitation_key;
}
meshlink_errno = MESHLINK_EINVAL;
return false;
}
+
+ bool rval = false;
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
for(const char *p = address; *p; p++) {
if(isalnum(*p) || *p == '-' || *p == '.' || *p == ':')
continue;
logger(mesh, MESHLINK_DEBUG, "Invalid character in address: %s\n", address);
meshlink_errno = MESHLINK_EINVAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
- return append_config_file(mesh, mesh->self->name, "Address", address);
+ rval = append_config_file(mesh, mesh->self->name, "Address", address);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return rval;
}
char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
meshlink_errno = MESHLINK_EINVAL;
return NULL;
}
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
// Check validity of the new node's name
if(!check_id(name)) {
logger(mesh, MESHLINK_DEBUG, "Invalid name for node.\n");
meshlink_errno = MESHLINK_EINVAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return NULL;
}
if(!access(filename, F_OK)) {
logger(mesh, MESHLINK_DEBUG, "A host config file for %s already exists!\n", name);
meshlink_errno = MESHLINK_EEXIST;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return NULL;
}
if(meshlink_get_node(mesh, name)) {
logger(mesh, MESHLINK_DEBUG, "A node with name %s is already known!\n", name);
meshlink_errno = MESHLINK_EEXIST;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return NULL;
}
if(!address) {
logger(mesh, MESHLINK_DEBUG, "No Address known for ourselves!\n");
meshlink_errno = MESHLINK_ERESOLV;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return NULL;
}
if(!refresh_invitation_key(mesh)) {
meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return NULL;
}
if(!ifd) {
logger(mesh, MESHLINK_DEBUG, "Could not create invitation file %s: %s\n", filename, strerror(errno));
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return NULL;
}
FILE *f = fdopen(ifd, "w");
} else {
logger(mesh, MESHLINK_DEBUG, "Could not create %s: %s\n", filename, strerror(errno));
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return NULL;
}
xasprintf(&url, "%s/%s%s", address, hash, cookie);
free(address);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return url;
}
meshlink_errno = MESHLINK_EINVAL;
return false;
}
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
//TODO: think of a better name for this variable, or of a different way to tokenize the invitation URL.
char copy[strlen(invitation) + 1];
ecdsa_t *key = ecdsa_generate();
if(!key) {
meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
if(!ai) {
meshlink_errno = MESHLINK_ERESOLV;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
logger(mesh, MESHLINK_DEBUG, "Could not open socket: %s\n", strerror(errno));
freeaddrinfo(ai);
meshlink_errno = MESHLINK_ENETWORK;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
closesocket(mesh->sock);
freeaddrinfo(ai);
meshlink_errno = MESHLINK_ENETWORK;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
logger(mesh, MESHLINK_DEBUG, "Error sending request to %s port %s: %s\n", address, port, strerror(errno));
closesocket(mesh->sock);
meshlink_errno = MESHLINK_ENETWORK;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
logger(mesh, MESHLINK_DEBUG, "Cannot read greeting from peer\n");
closesocket(mesh->sock);
meshlink_errno = MESHLINK_ENETWORK;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(sha512(fingerprint, strlen(fingerprint), hishash)) {
logger(mesh, MESHLINK_DEBUG, "Could not create hash\n%s\n", mesh->line + 2);
meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(memcmp(hishash, mesh->hash, 18)) {
logger(mesh, MESHLINK_DEBUG, "Peer has an invalid key!\n%s\n", mesh->line + 2);
meshlink_errno = MESHLINK_EPEER;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
ecdsa_t *hiskey = ecdsa_set_base64_public_key(fingerprint);
if(!hiskey) {
meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
// Start an SPTPS session
if(!sptps_start(&mesh->sptps, mesh, true, false, key, hiskey, "meshlink invitation", 15, invitation_send, invitation_receive)) {
meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
// Feed rest of input buffer to SPTPS
if(!sptps_receive_data(&mesh->sptps, mesh->buffer, mesh->blen)) {
meshlink_errno = MESHLINK_EPEER;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
continue;
logger(mesh, MESHLINK_DEBUG, "Error reading data from %s port %s: %s\n", address, port, strerror(errno));
meshlink_errno = MESHLINK_ENETWORK;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(!sptps_receive_data(&mesh->sptps, mesh->line, len)) {
meshlink_errno = MESHLINK_EPEER;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
}
if(!mesh->success) {
logger(mesh, MESHLINK_DEBUG, "Connection closed by peer, invitation cancelled.\n");
meshlink_errno = MESHLINK_EPEER;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return true;
invalid:
logger(mesh, MESHLINK_DEBUG, "Invalid invitation URL or you are already connected to a Mesh ?\n");
meshlink_errno = MESHLINK_EINVAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
return NULL;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
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) {
logger(mesh, MESHLINK_DEBUG, "Could not open %s: %s\n", filename, strerror(errno));
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return NULL;
}
logger(mesh, MESHLINK_DEBUG, "Error reading from %s: %s\n", filename, strerror(errno));
fclose(f);
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return NULL;
}
fclose(f);
buf[len - 1] = 0;
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return buf;
}
meshlink_errno = MESHLINK_EINVAL;
return false;
}
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
if(strncmp(data, "Name = ", 7)) {
logger(mesh, MESHLINK_DEBUG, "Invalid data\n");
meshlink_errno = MESHLINK_EPEER;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(!end) {
logger(mesh, MESHLINK_DEBUG, "Invalid data\n");
meshlink_errno = MESHLINK_EPEER;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(!check_id(name)) {
logger(mesh, MESHLINK_DEBUG, "Invalid Name\n");
meshlink_errno = MESHLINK_EPEER;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(!access(filename, F_OK)) {
logger(mesh, MESHLINK_DEBUG, "File %s already exists, not importing\n", filename);
meshlink_errno = MESHLINK_EEXIST;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(errno != ENOENT) {
logger(mesh, MESHLINK_DEBUG, "Error accessing %s: %s\n", filename, strerror(errno));
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
if(!f) {
logger(mesh, MESHLINK_DEBUG, "Could not create %s: %s\n", filename, strerror(errno));
meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return false;
}
load_all_nodes(mesh);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return true;
}
return;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
node_t *n;
n = (node_t*)node;
n->status.blacklisted=true;
//Make blacklisting persistent in the config file
append_config_file(mesh, n->name, "blacklisted", "yes");
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
return;
}
return;
}
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
node_t *n = (node_t *)node;
n->status.blacklisted = false;
//TODO: remove blacklisted = yes from the config file
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
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, meshlink_node_t *node, struct sockaddr *addr) {
+void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const struct sockaddr *addr) {
if(!mesh || !node || !addr)
return;
- 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));
+ pthread_mutex_lock(&(mesh->mesh_mutex));
- // 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 *host = NULL, *port = NULL, *str = NULL;
+ sockaddr2str((const sockaddr_t *)addr, &host, &port);
- char *full_addr_str = malloc(full_addr_len*sizeof(char));
- memset(full_addr_str, 0, full_addr_len*sizeof(char));
-
- // get address and port number
- if(!get_ip_str(addr, addr_str, MAX_ADDRESS_LENGTH))
- goto fail;
- if(!get_port_str(addr, port_str, MAX_ADDRESS_LENGTH))
- goto fail;
-
- // 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, node->name, "Address", full_addr_str);
+ 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);
+ else
+ logger(mesh, MESHLINK_DEBUG, "Not adding Link Local IPv6 Address to config\n");
+ }
-fail:
- free(addr_str);
- free(port_str);
- free(full_addr_str);
+ free(str);
+ free(host);
+ free(port);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
// @TODO do we want to fire off a connection attempt right away?
}
-static void __attribute__((constructor)) meshlink_init(void) {
- crypto_init();
+/* 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;
+ free(result);
+ meshlink_errno = MESHLINK_ENOMEM;
+ }
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+
+ return result;
}
-static void __attribute__((destructor)) meshlink_exit(void) {
- crypto_exit();
+static bool channel_pre_accept(struct utcp *utcp, uint16_t port) {
+ //TODO: implement
+ return false;
}
-int weight_from_dclass(dclass_t dclass)
-{
- switch(dclass)
- {
- case BACKBONE:
- return 1;
+static void channel_accept(struct utcp_connection *utcp_connection, uint16_t port) {
+ //TODO: implement
+}
- case STATIONARY:
- return 3;
+static int channel_recv(struct utcp_connection *connection, const void *data, size_t len) {
+ meshlink_channel_t *channel = connection->priv;
+ node_t *n = channel->node;
+ meshlink_handle_t *mesh = n->mesh;
+ if(!channel->receive_cb)
+ return -1;
+ else {
+ channel->receive_cb(mesh, channel, data, len);
+ return 0;
+ }
+}
+
+static int 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;
+}
+
+void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) {
+ mesh->channel_accept_cb = cb;
+}
- case PORTABLE:
- return 6;
+void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb) {
+ channel->receive_cb = cb;
+}
+
+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) {
+ node_t *n = (node_t *)node;
+ if(!n->utcp) {
+ n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
+ if(!n->utcp)
+ return NULL;
}
+ meshlink_channel_t *channel = xzalloc(sizeof *channel);
+ channel->node = n;
+ channel->receive_cb = cb;
+ channel->c = utcp_connect(n->utcp, port, channel_recv, channel);
+ if(!channel->c) {
+ free(channel);
+ return NULL;
+ }
+ return channel;
+}
- return 9;
+void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *channel, int direction) {
+ utcp_shutdown(channel->c, direction);
}
+
+void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel) {
+ utcp_close(channel->c);
+ free(channel);
+}
+
+ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
+ // TODO: locking.
+ // Ideally we want to put the data into the UTCP connection's send buffer.
+ // Then, preferrably only if there is room in the receiver window,
+ // kick the meshlink thread to go send packets.
+ return utcp_send(channel->c, data, len);
+}
+
+static void __attribute__((constructor)) meshlink_init(void) {
+ crypto_init();
+}
+
+static void __attribute__((destructor)) meshlink_exit(void) {
+ crypto_exit();
+}
+
+
+/// Device class traits
+dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX +1] = {
+ { .min_connects = 3, .max_connects = 10000, .edge_weight = 1 }, // DEV_CLASS_BACKBONE
+ { .min_connects = 3, .max_connects = 100, .edge_weight = 3 }, // DEV_CLASS_STATIONARY
+ { .min_connects = 3, .max_connects = 3, .edge_weight = 6 }, // DEV_CLASS_PORTABLE
+ { .min_connects = 1, .max_connects = 1, .edge_weight = 9 }, // DEV_CLASS_UNKNOWN
+};