#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
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);
}
-static vpn_packet_t *prepare_packet(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;
if(len >= MAXSIZE - sizeof(*hdr)) {
meshlink_errno = MESHLINK_EINVAL;
- return NULL;
+ return false;
}
node_t *n = (node_t *)destination;
if(n->status.blacklisted) {
logger(mesh, MESHLINK_ERROR, "Node %s blacklisted, dropping packet\n", n->name);
meshlink_errno = MESHLINK_EBLACKLISTED;
- return NULL;
+ return false;
}
// Prepare the packet
- vpn_packet_t *packet = malloc(sizeof(*packet));
-
- if(!packet) {
- meshlink_errno = MESHLINK_ENOMEM;
- return NULL;
- }
-
packet->probe = false;
packet->tcp = false;
packet->len = len + sizeof(*hdr);
memcpy(packet->data + sizeof(*hdr), data, len);
- return packet;
+ return true;
}
static bool meshlink_send_immediate(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len) {
assert(len);
// Prepare the packet
- vpn_packet_t *packet = prepare_packet(mesh, destination, data, len);
-
- if(!packet) {
+ if(!prepare_packet(mesh, destination, data, len, mesh->packet)) {
return false;
}
// Send it immediately
- route(mesh, mesh->self, packet);
- free(packet);
+ route(mesh, mesh->self, mesh->packet);
return true;
}
}
// Prepare the packet
- vpn_packet_t *packet = prepare_packet(mesh, destination, data, len);
+ 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) {
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) {