break;
}
- if(found) {
+ if(found || !is_valid_hostname(host[i])) {
free(host[i]);
free(port[i]);
host[i] = NULL;
char resolved_port[NI_MAXSERV];
err = getnameinfo(ai_in->ai_addr, ai_in->ai_addrlen, resolved_host, sizeof resolved_host, resolved_port, sizeof resolved_port, NI_NUMERICSERV);
- if(err) {
+ if(err || !is_valid_hostname(resolved_host)) {
freeaddrinfo(ai_in);
continue;
}
return true;
}
-int check_port(meshlink_handle_t *mesh) {
+static int check_port(meshlink_handle_t *mesh) {
for(int i = 0; i < 1000; i++) {
int port = 0x1000 + (rand() & 0x7fff);
}
}
+ /* Ensure the configuration directory metadata is on disk */
+ if(!config_sync(mesh, "current")) {
+ return false;
+ }
+
sptps_send_record(&(mesh->sptps), 1, ecdsa_get_public_key(mesh->private_key), 32);
logger(mesh, MESHLINK_DEBUG, "Configuration stored in: %s\n", mesh->confbase);
// Get our local address(es) by simulating connecting to an Internet host.
static void add_local_addresses(meshlink_handle_t *mesh) {
struct sockaddr_storage sn;
+ sn.ss_family = AF_UNKNOWN;
socklen_t sl = sizeof(sn);
// IPv4 example.org
return false;
}
+ /* Ensure the configuration directory metadata is on disk */
+ if(!config_sync(mesh, "current")) {
+ return false;
+ }
+
if(!main_config_lock(mesh)) {
logger(NULL, MESHLINK_ERROR, "Cannot lock main config file\n");
meshlink_errno = MESHLINK_ESTORAGE;
if(!name || !*name) {
logger(NULL, MESHLINK_ERROR, "No name given!\n");
- //return NULL;
- } else { //check name only if there is a name != NULL
- if(!check_id(name)) {
- logger(NULL, MESHLINK_ERROR, "Invalid name given!\n");
- meshlink_errno = MESHLINK_EINVAL;
- return NULL;
- }
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ };
+
+ if(!check_id(name)) {
+ logger(NULL, MESHLINK_ERROR, "Invalid name given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
}
if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
}
/* Create a temporary struct on the stack, to avoid allocating and freeing one. */
- meshlink_open_params_t params = {NULL};
+ meshlink_open_params_t params;
+ memset(¶ms, 0, sizeof(params));
params.confbase = (char *)confbase;
params.name = (char *)name;
meshlink_handle_t *meshlink_open_ephemeral(const char *name, const char *appname, dev_class_t devclass) {
/* Create a temporary struct on the stack, to avoid allocating and freeing one. */
- meshlink_open_params_t params = {NULL};
+ meshlink_open_params_t params;
+ memset(¶ms, 0, sizeof(params));
params.name = (char *)name;
params.appname = (char *)appname;
close(mesh->netns);
}
+ for(vpn_packet_t *packet; (packet = meshlink_queue_pop(&mesh->outpacketqueue));) {
+ free(packet);
+ }
+
+ meshlink_queue_exit(&mesh->outpacketqueue);
+
free(mesh->name);
free(mesh->appname);
free(mesh->confbase);
pthread_mutex_unlock(&(mesh->mesh_mutex));
}
+void meshlink_set_node_pmtu_cb(meshlink_handle_t *mesh, meshlink_node_pmtu_cb_t cb) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ mesh->node_pmtu_cb = cb;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+}
+
void meshlink_set_node_duplicate_cb(meshlink_handle_t *mesh, meshlink_node_duplicate_cb_t cb) {
if(!mesh) {
meshlink_errno = MESHLINK_EINVAL;
mesh->self->in_packets++;
mesh->self->in_bytes += packet->len;
route(mesh, mesh->self, packet);
+
+ free(packet);
}
ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination) {
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));
+
+ if(!node) {
+ meshlink_errno = MESHLINK_ENOENT;
+ }
+
return node;
}
pthread_mutex_lock(&(mesh->mesh_mutex));
submesh = (meshlink_submesh_t *)lookup_submesh(mesh, name);
pthread_mutex_unlock(&(mesh->mesh_mutex));
+
+ if(!submesh) {
+ meshlink_errno = MESHLINK_ENOENT;
+ }
+
return submesh;
}
return -1;
}
- return atoi(mesh->myport);
+ int port;
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ port = atoi(mesh->myport);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+
+ return port;
}
bool meshlink_set_port(meshlink_handle_t *mesh, int port) {
free(mesh->myport);
xasprintf(&mesh->myport, "%d", port);
- /* Write meshlink.conf with the updated port number */
- write_main_config_files(mesh);
-
/* Close down the network. This also deletes mesh->self. */
close_network_connections(mesh);
meshlink_errno = MESHLINK_ESTORAGE;
free_node(mesh->self);
mesh->self = NULL;
+ goto done;
} else if(!setup_network(mesh)) {
meshlink_errno = MESHLINK_ENETWORK;
- } else {
- rval = true;
+ goto done;
}
+ /* Rebuild our own list of recent addresses */
+ memset(mesh->self->recent, 0, sizeof(mesh->self->recent));
+ add_local_addresses(mesh);
+
+ /* Write meshlink.conf with the updated port number */
+ write_main_config_files(mesh);
+
+ rval = config_sync(mesh, "current");
+
done:
pthread_mutex_unlock(&(mesh->mesh_mutex));
* Note: make sure we only add config files of nodes that are in the core mesh or the same submesh,
* and are not blacklisted.
*/
- config_t configs[5] = {NULL};
+ config_t configs[5];
+ memset(configs, 0, sizeof(configs));
int count = 0;
if(config_read(mesh, "current", mesh->self->name, &configs[count], mesh->config_key)) {
pthread_mutex_unlock(&(mesh->mesh_mutex));
+ free(buf);
+
if(!packmsg_done(&in)) {
logger(mesh, MESHLINK_ERROR, "Invalid data\n");
meshlink_errno = MESHLINK_EPEER;
return false;
}
+ if(!config_sync(mesh, "current")) {
+ return false;
+ }
+
return true;
}
n->status.blacklisted = true;
node_write_config(mesh, n);
+ config_sync(mesh, "current");
+
logger(mesh, MESHLINK_DEBUG, "Blacklisted %s.\n", node->name);
//Immediately terminate any connections we have with the blacklisted node
n->status.blacklisted = false;
node_write_config(mesh, n);
+ config_sync(mesh, "current");
+
+ if(n->status.reachable) {
+ update_node_status(mesh, n);
+ }
pthread_mutex_unlock(&(mesh->mesh_mutex));
return;
}
void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_poll_cb_t cb) {
- (void)mesh;
+ if(!mesh || !channel) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&mesh->mesh_mutex);
channel->poll_cb = cb;
utcp_set_poll_cb(channel->c, (cb || channel->aio_send) ? channel_poll : NULL);
+ pthread_mutex_unlock(&mesh->mesh_mutex);
}
void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) {
return NULL;
}
+ pthread_mutex_lock(&mesh->mesh_mutex);
+
node_t *n = (node_t *)node;
if(!n->utcp) {
if(!n->utcp) {
meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&mesh->mesh_mutex);
return NULL;
}
}
if(n->status.blacklisted) {
logger(mesh, MESHLINK_ERROR, "Cannot open a channel with blacklisted node\n");
+ pthread_mutex_unlock(&mesh->mesh_mutex);
return NULL;
}
channel->receive_cb = cb;
channel->c = utcp_connect_ex(n->utcp, port, channel_recv, channel, flags);
+ pthread_mutex_unlock(&mesh->mesh_mutex);
+
if(!channel->c) {
meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL;
free(channel);
return;
}
+ pthread_mutex_lock(&mesh->mesh_mutex);
utcp_shutdown(channel->c, direction);
+ pthread_mutex_unlock(&mesh->mesh_mutex);
}
void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel) {
return;
}
+ pthread_mutex_lock(&mesh->mesh_mutex);
+
utcp_close(channel->c);
/* Clean up any outstanding AIO buffers. */
free(aio);
}
+ pthread_mutex_unlock(&mesh->mesh_mutex);
+
free(channel);
}
if(mesh->node_status_cb) {
mesh->node_status_cb(mesh, (meshlink_node_t *)n, n->status.reachable && !n->status.blacklisted);
}
+
+ if(mesh->node_pmtu_cb) {
+ mesh->node_pmtu_cb(mesh, (meshlink_node_t *)n, n->minmtu);
+ }
+}
+
+void update_node_pmtu(meshlink_handle_t *mesh, node_t *n) {
+ if(mesh->node_pmtu_cb && !n->status.blacklisted) {
+ mesh->node_pmtu_cb(mesh, (meshlink_node_t *)n, n->minmtu);
+ }
}
void handle_duplicate_node(meshlink_handle_t *mesh, node_t *n) {