#include "system.h"
#include <pthread.h>
+#include "adns.h"
#include "crypto.h"
#include "ecdsagen.h"
#include "logger.h"
#include "meshlink_internal.h"
+#include "net.h"
#include "netutl.h"
#include "node.h"
#include "submesh.h"
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = IPPROTO_UDP,
+ .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV,
};
if(getaddrinfo(destaddr, "80", &hint, &rai) || !rai) {
}
logger(mesh, MESHLINK_DEBUG, "Trying to discover externally visible hostname...\n");
- struct addrinfo *ai = str2addrinfo(host, port ? port : "80", SOCK_STREAM);
+ struct addrinfo *ai = adns_blocking_request(mesh, xstrdup(host), xstrdup(port ? port : "80"), 5);
char line[256];
char *hostname = NULL;
}
// Convert what we have to a sockaddr
- struct addrinfo *ai_in, *ai_out;
- struct addrinfo hint = {
- .ai_family = AF_UNSPEC,
- .ai_flags = AI_NUMERICSERV,
- .ai_socktype = SOCK_STREAM,
- };
- int err = getaddrinfo(hostname[i], port[i], &hint, &ai_in);
-
- if(err || !ai_in) {
+ struct addrinfo *ai_in = adns_blocking_request(mesh, xstrdup(hostname[i]), xstrdup(port[i]), 5);
+
+ if(!ai_in) {
continue;
}
node_add_recent_address(mesh, mesh->self, (sockaddr_t *)aip->ai_addr);
}
- if(flags & MESHLINK_INVITE_NUMERIC) {
- // We don't need to do any further conversion
- freeaddrinfo(ai_in);
- continue;
- }
-
- // Convert it to a hostname
- char resolved_host[NI_MAXHOST];
- 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 || !is_valid_hostname(resolved_host)) {
- freeaddrinfo(ai_in);
- continue;
- }
-
- // Convert the hostname back to a sockaddr
- hint.ai_family = ai_in->ai_family;
- err = getaddrinfo(resolved_host, resolved_port, &hint, &ai_out);
-
- if(err || !ai_out) {
- freeaddrinfo(ai_in);
- continue;
- }
-
- // Check if it's still the same sockaddr
- if(ai_in->ai_addrlen != ai_out->ai_addrlen || memcmp(ai_in->ai_addr, ai_out->ai_addr, ai_in->ai_addrlen)) {
- freeaddrinfo(ai_in);
- freeaddrinfo(ai_out);
- continue;
- }
-
- // Yes: replace the hostname with the resolved one
- free(hostname[i]);
- hostname[i] = xstrdup(resolved_host);
-
freeaddrinfo(ai_in);
- freeaddrinfo(ai_out);
+ continue;
}
// Remove duplicates again, since IPv4 and IPv6 addresses might map to the same hostname
return true;
}
-static struct timeval idle(event_loop_t *loop, void *data) {
+static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
+ if(a->tv_sec == b->tv_sec) {
+ return a->tv_nsec < b->tv_nsec;
+ } else {
+ return a->tv_sec < b->tv_sec;
+ }
+}
+
+static struct timespec idle(event_loop_t *loop, void *data) {
(void)loop;
meshlink_handle_t *mesh = data;
- struct timeval t, tmin = {3600, 0};
+ struct timespec t, tmin = {3600, 0};
for splay_each(node_t, n, mesh->nodes) {
if(!n->utcp) {
t = utcp_timeout(n->utcp);
- if(timercmp(&t, &tmin, <)) {
+ if(timespec_lt(&t, &tmin)) {
tmin = t;
}
}
mesh->submeshes = NULL;
mesh->log_cb = global_log_cb;
mesh->log_level = global_log_level;
+ mesh->packet = xmalloc(sizeof(vpn_packet_t));
randomize(&mesh->prng_state, sizeof(mesh->prng_state));
}
init_outgoings(mesh);
+ init_adns(mesh);
// Start the main thread
}
}
+ exit_adns(mesh);
exit_outgoings(mesh);
// Ensure we are considered unreachable
free(mesh->confbase);
free(mesh->config_key);
free(mesh->external_address_url);
+ free(mesh->packet);
ecdsa_free(mesh->private_key);
if(mesh->invitation_addresses) {
pthread_mutex_unlock(&mesh->mutex);
}
-bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len) {
+static bool prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len, vpn_packet_t *packet) {
meshlink_packethdr_t *hdr;
- // Validate arguments
- if(!mesh || !destination || len >= MAXSIZE - sizeof(*hdr)) {
- meshlink_errno = MESHLINK_EINVAL;
- return false;
- }
-
- if(!len) {
- return true;
- }
-
- if(!data) {
+ if(len >= MAXSIZE - sizeof(*hdr)) {
meshlink_errno = MESHLINK_EINVAL;
return false;
}
}
// Prepare the packet
- vpn_packet_t *packet = malloc(sizeof(*packet));
-
- if(!packet) {
- meshlink_errno = MESHLINK_ENOMEM;
- return false;
- }
-
packet->probe = false;
packet->tcp = false;
packet->len = len + sizeof(*hdr);
memcpy(packet->data + sizeof(*hdr), data, len);
+ return true;
+}
+
+static bool meshlink_send_immediate(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len) {
+ assert(mesh);
+ assert(destination);
+ assert(data);
+ assert(len);
+
+ // Prepare the packet
+ if(!prepare_packet(mesh, destination, data, len, mesh->packet)) {
+ return false;
+ }
+
+ // Send it immediately
+ route(mesh, mesh->self, mesh->packet);
+
+ return true;
+}
+
+bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len) {
+ // Validate arguments
+ if(!mesh || !destination) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ if(!len) {
+ return true;
+ }
+
+ if(!data) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ // Prepare the packet
+ vpn_packet_t *packet = malloc(sizeof(*packet));
+
+ if(!packet) {
+ meshlink_errno = MESHLINK_ENOMEM;
+ return false;
+ }
+
+ if(!prepare_packet(mesh, destination, data, len, packet)) {
+ free(packet);
+ }
+
// Queue it
if(!meshlink_queue_push(&mesh->outpacketqueue, packet)) {
free(packet);
}
// Connect to the meshlink daemon mentioned in the URL.
- struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
+ struct addrinfo *ai = adns_blocking_request(mesh, xstrdup(address), xstrdup(port), 5);
if(ai) {
for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
n->status.udp_confirmed = false;
if(n->status.reachable) {
- n->last_unreachable = mesh->loop.now.tv_sec;
+ n->last_unreachable = time(NULL);
}
/* Graph updates will suppress status updates for blacklisted nodes, so we need to
n->status.blacklisted = false;
if(n->status.reachable) {
- n->last_reachable = mesh->loop.now.tv_sec;
+ n->last_reachable = time(NULL);
update_node_status(mesh, n);
}
}
meshlink_handle_t *mesh = n->mesh;
- return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? (ssize_t)len : -1;
+ return meshlink_send_immediate(mesh, (meshlink_node_t *)n, data, len) ? (ssize_t)len : -1;
}
void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb) {
for splay_each(node_t, n, mesh->nodes) {
if(!n->utcp && n != mesh->self) {
n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
+ utcp_set_mtu(n->utcp, n->mtu - sizeof(meshlink_packethdr_t));
}
}
if(!n->utcp) {
n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
+ utcp_set_mtu(n->utcp, n->mtu - sizeof(meshlink_packethdr_t));
mesh->receive_cb = channel_receive;
if(!n->utcp) {
return utcp_get_recvq(channel->c);
}
+size_t meshlink_channel_get_mss(meshlink_handle_t *mesh, meshlink_channel_t *channel) {
+ if(!mesh || !channel) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return -1;
+ }
+
+ return utcp_get_mss(channel->node->utcp);
+}
+
void meshlink_set_node_channel_timeout(meshlink_handle_t *mesh, meshlink_node_t *node, int timeout) {
if(!mesh || !node) {
meshlink_errno = MESHLINK_EINVAL;
if(!n->utcp) {
n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
+ utcp_set_mtu(n->utcp, n->mtu - sizeof(meshlink_packethdr_t));
}
utcp_set_user_timeout(n->utcp, timeout);
void update_node_status(meshlink_handle_t *mesh, node_t *n) {
if(n->status.reachable && mesh->channel_accept_cb && !n->utcp) {
n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
+ utcp_set_mtu(n->utcp, n->mtu - sizeof(meshlink_packethdr_t));
}
if(mesh->node_status_cb) {
}
void update_node_pmtu(meshlink_handle_t *mesh, node_t *n) {
+ utcp_set_mtu(n->utcp, (n->minmtu > MINMTU ? n->minmtu : MINMTU) - sizeof(meshlink_packethdr_t));
+
if(mesh->node_pmtu_cb && !n->status.blacklisted) {
mesh->node_pmtu_cb(mesh, (meshlink_node_t *)n, n->minmtu);
}
pthread_mutex_unlock(&mesh->mutex);
}
+void meshlink_set_scheduling_granularity(struct meshlink_handle *mesh, long granularity) {
+ if(!mesh || granularity < 0) {
+ meshlink_errno = EINVAL;
+ return;
+ }
+
+ utcp_set_clock_granularity(granularity);
+}
+
void handle_network_change(meshlink_handle_t *mesh, bool online) {
(void)online;
static void __attribute__((constructor)) meshlink_init(void) {
crypto_init();
+ utcp_set_clock_granularity(10000);
}
static void __attribute__((destructor)) meshlink_exit(void) {