#define MSG_NOSIGNAL 0
#endif
-int addressfamily = AF_UNSPEC;
-int seconds_till_retry = 5;
-int max_connection_burst = 100;
+static const int max_connection_burst = 100;
/* Setup sockets */
#endif
}
-static bool bind_to_address(meshlink_handle_t *mesh, connection_t *c) {
- int s = -1;
-
- for(int i = 0; i < mesh->listen_sockets && mesh->listen_socket[i].bindto; i++) {
- if(mesh->listen_socket[i].sa.sa.sa_family != c->address.sa.sa_family) {
- continue;
- }
-
- if(s >= 0) {
- return false;
- }
-
- s = i;
- }
-
- if(s < 0) {
- return false;
- }
-
- sockaddr_t sa = mesh->listen_socket[s].sa;
-
- if(sa.sa.sa_family == AF_INET) {
- sa.in.sin_port = 0;
- } else if(sa.sa.sa_family == AF_INET6) {
- sa.in6.sin6_port = 0;
- }
-
- if(bind(c->socket, &sa.sa, SALEN(sa.sa))) {
- logger(mesh, MESHLINK_WARNING, "Can't bind outgoing socket: %s", strerror(errno));
- return false;
- }
-
- return true;
-}
-
-int setup_listen_socket(const sockaddr_t *sa) {
- int nfd;
- char *addrstr;
- int option;
-
- nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
-
- if(nfd < 0) {
- logger(NULL, MESHLINK_ERROR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
- return -1;
- }
-
+bool setup_listen_socket(meshlink_handle_t *mesh, int nfd, int sa_family) {
#ifdef FD_CLOEXEC
fcntl(nfd, F_SETFD, FD_CLOEXEC);
#endif
- /* Optimize TCP settings */
-
- option = 1;
+ int option = 1;
setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
#if defined(IPV6_V6ONLY)
- if(sa->sa.sa_family == AF_INET6) {
+ if(sa_family == AF_INET6) {
setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
}
#warning IPV6_V6ONLY not defined
#endif
- if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
- closesocket(nfd);
- addrstr = sockaddr2hostname(sa);
- logger(NULL, MESHLINK_ERROR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
- free(addrstr);
- return -1;
- }
-
if(listen(nfd, 3)) {
- closesocket(nfd);
- logger(NULL, MESHLINK_ERROR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
- return -1;
+ logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
+ return false;
}
- return nfd;
+ return true;
}
-int setup_vpn_in_socket(meshlink_handle_t *mesh, const sockaddr_t *sa) {
- int nfd;
- char *addrstr;
- int option;
-
- nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
-
- if(nfd < 0) {
- logger(mesh, MESHLINK_ERROR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
- return -1;
- }
-
+bool setup_vpn_in_socket(meshlink_handle_t *mesh, int nfd, int sa_family) {
#ifdef FD_CLOEXEC
fcntl(nfd, F_SETFD, FD_CLOEXEC);
#endif
closesocket(nfd);
logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "fcntl",
strerror(errno));
- return -1;
+ return false;
}
}
#elif defined(WIN32)
if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
closesocket(nfd);
logger(mesh, MESHLINK_ERROR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
- return -1;
+ return false;
}
}
#endif
- option = 1;
+ int option = 1;
setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
#if defined(IPV6_V6ONLY)
- if(sa->sa.sa_family == AF_INET6) {
+ if(sa_family == AF_INET6) {
setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
}
#endif
#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
-
- if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
- option = IP_PMTUDISC_DO;
- setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
- }
-
+ option = IP_PMTUDISC_DO;
+ setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
#elif defined(IP_DONTFRAGMENT)
-
- if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
- option = 1;
- setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
- }
-
-#else
-#warning No way to disable IPv4 fragmentation
+ option = 1;
+ setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
#endif
#if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
-
- if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
- option = IPV6_PMTUDISC_DO;
- setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
- }
-
+ option = IPV6_PMTUDISC_DO;
+ setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
#elif defined(IPV6_DONTFRAG)
-
- if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
- option = 1;
- setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
- }
-
-#else
-#warning No way to disable IPv6 fragmentation
+ option = 1;
+ setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
#endif
- if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
- closesocket(nfd);
- addrstr = sockaddr2hostname(sa);
- logger(mesh, MESHLINK_ERROR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
- free(addrstr);
- return -1;
- }
-
- return nfd;
+ return true;
} /* int setup_vpn_in_socket */
static void retry_outgoing_handler(event_loop_t *loop, void *data) {
+ assert(data);
+
meshlink_handle_t *mesh = loop->data;
outgoing_t *outgoing = data;
setup_outgoing_connection(mesh, outgoing);
}
void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
- outgoing->timeout += 5;
+ if(!mesh->reachable && mesh->loop.now.tv_sec < mesh->last_unreachable + mesh->dev_class_traits[mesh->devclass].fast_retry_period) {
+ outgoing->timeout = 1;
+ } else {
+ outgoing->timeout += 5;
+ }
if(outgoing->timeout > mesh->maxtimeout) {
outgoing->timeout = mesh->maxtimeout;
}
timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timeval) {
- outgoing->timeout, rand() % 100000
+ outgoing->timeout, prng(mesh, TIMER_FUDGE)
});
logger(mesh, MESHLINK_INFO, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout);
return ai;
}
+// Build a list of recently seen addresses.
+static struct addrinfo *get_recent_addresses(node_t *n) {
+ struct addrinfo *ai = NULL;
+ struct addrinfo *aip;
+
+ for(int i = 0; i < 5; i++) {
+ if(!n->recent[i].sa.sa_family) {
+ break;
+ }
+
+ // Create a new struct addrinfo, and put it at the end of the list.
+ struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(n->recent[i].sa));
+
+ if(!ai) {
+ ai = nai;
+ } else {
+ aip->ai_next = nai;
+ }
+
+ aip = nai;
+
+ nai->ai_family = n->recent[i].sa.sa_family;
+ nai->ai_socktype = SOCK_STREAM;
+ nai->ai_protocol = IPPROTO_TCP;
+ nai->ai_addrlen = SALEN(n->recent[i].sa);
+ nai->ai_addr = (struct sockaddr *)(nai + 1);
+ memcpy(nai->ai_addr, &n->recent[i], nai->ai_addrlen);
+ }
+
+ return ai;
+}
+
// Free struct addrinfo list from get_known_addresses().
static void free_known_addresses(struct addrinfo *ai) {
for(struct addrinfo *aip = ai, *next; aip; aip = next) {
}
}
-bool do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
- char *address, *port, *space;
- struct addrinfo *proxyai = NULL;
- int result;
+static struct addrinfo *get_canonical_address(node_t *n) {
+ if(!n->canonical_address) {
+ return false;
+ }
-begin:
+ char *address = xstrdup(n->canonical_address);
+ char *port = strchr(address, ' ');
- if(!outgoing->ai && !outgoing->nai) {
- if(!outgoing->cfg) {
- logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->name);
- retry_outgoing(mesh, outgoing);
- return false;
- }
+ if(!port) {
+ free(address);
+ return false;
+ }
+
+ *port++ = 0;
+
+ struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
+ free(address);
- get_config_string(outgoing->cfg, &address);
+ return ai;
+}
+
+static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) {
+ (void)mesh;
- space = strchr(address, ' ');
+ bool start = false;
- if(space) {
- port = xstrdup(space + 1);
- *space = 0;
+ if(outgoing->state == OUTGOING_START) {
+ start = true;
+ outgoing->state = OUTGOING_CANONICAL;
+ }
+
+ if(outgoing->state == OUTGOING_CANONICAL) {
+ if(!outgoing->aip) {
+ outgoing->ai = get_canonical_address(outgoing->node);
+ outgoing->aip = outgoing->ai;
} else {
- // TODO: Only allow Address statements?
- if(!get_config_string(lookup_config(outgoing->config_tree, "Port"), &port)) {
- logger(mesh, MESHLINK_ERROR, "No Port known for %s", outgoing->name);
- retry_outgoing(mesh, outgoing);
- return false;
- }
+ outgoing->aip = outgoing->aip->ai_next;
}
- outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
- free(address);
- free(port);
+ if(outgoing->aip) {
+ return true;
+ }
- outgoing->aip = outgoing->ai;
- outgoing->cfg = lookup_config_next(outgoing->config_tree, outgoing->cfg);
+ freeaddrinfo(outgoing->ai);
+ outgoing->ai = NULL;
+ outgoing->aip = NULL;
+ outgoing->state = OUTGOING_RECENT;
}
- if(!outgoing->aip) {
- if(outgoing->ai) {
- freeaddrinfo(outgoing->ai);
+ if(outgoing->state == OUTGOING_RECENT) {
+ if(!outgoing->aip) {
+ outgoing->ai = get_recent_addresses(outgoing->node);
+ outgoing->aip = outgoing->ai;
+ } else {
+ outgoing->aip = outgoing->aip->ai_next;
}
+ if(outgoing->aip) {
+ return true;
+ }
+
+ free_known_addresses(outgoing->ai);
outgoing->ai = NULL;
+ outgoing->aip = NULL;
+ outgoing->state = OUTGOING_KNOWN;
+ }
- if(outgoing->nai) {
- free_known_addresses(outgoing->nai);
+ if(outgoing->state == OUTGOING_KNOWN) {
+ if(!outgoing->aip) {
+ outgoing->ai = get_known_addresses(outgoing->node);
+ outgoing->aip = outgoing->ai;
+ } else {
+ outgoing->aip = outgoing->aip->ai_next;
}
- outgoing->nai = NULL;
+ if(outgoing->aip) {
+ return true;
+ }
- goto begin;
+ free_known_addresses(outgoing->ai);
+ outgoing->ai = NULL;
+ outgoing->aip = NULL;
+ outgoing->state = OUTGOING_END;
+ }
+
+ if(start) {
+ outgoing->state = OUTGOING_NO_KNOWN_ADDRESSES;
+ }
+
+ return false;
+}
+
+void do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
+ struct addrinfo *proxyai = NULL;
+ int result;
+
+begin:
+
+ if(!get_next_outgoing_address(mesh, outgoing)) {
+ if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) {
+ logger(mesh, MESHLINK_ERROR, "No known addresses for %s", outgoing->node->name);
+ } else {
+ logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->node->name);
+ retry_outgoing(mesh, outgoing);
+ }
+
+ return;
}
connection_t *c = new_connection();
c->outgoing = outgoing;
memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
- outgoing->aip = outgoing->aip->ai_next;
char *hostname = sockaddr2hostname(&c->address);
- logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->name, hostname);
+ logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname);
if(!mesh->proxytype) {
c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
}
if(c->socket == -1) {
- logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, c->hostname, sockstrerror(sockerrno));
+ logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno));
free_connection(c);
free(hostname);
goto begin;
#endif
- bind_to_address(mesh, c);
-
/* Connect */
if(!mesh->proxytype) {
}
if(result == -1 && !sockinprogress(sockerrno)) {
- logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->name, sockstrerror(sockerrno));
+ logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno));
free_connection(c);
goto begin;
/* Now that there is a working socket, fill in the rest and register this connection. */
c->status.connecting = true;
- c->name = xstrdup(outgoing->name);
- c->outcompression = mesh->self->connection->outcompression;
+ c->status.initiator = true;
+ c->name = xstrdup(outgoing->node->name);
c->last_ping_time = mesh->loop.now.tv_sec;
connection_add(mesh, c);
io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ | IO_WRITE);
+}
- return true;
+void reset_outgoing(outgoing_t *outgoing) {
+ if(outgoing->ai) {
+ if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
+ free_known_addresses(outgoing->ai);
+ } else {
+ freeaddrinfo(outgoing->ai);
+ }
+ }
+
+ outgoing->ai = NULL;
+ outgoing->aip = NULL;
+ outgoing->state = OUTGOING_START;
}
void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
- bool blacklisted = false;
timeout_del(&mesh->loop, &outgoing->ev);
- node_t *n = lookup_node(mesh, outgoing->name);
+ if(outgoing->node->connection) {
+ logger(mesh, MESHLINK_INFO, "Already connected to %s", outgoing->node->name);
- if(n && n->connection) {
- logger(mesh, MESHLINK_INFO, "Already connected to %s", outgoing->name);
+ outgoing->node->connection->outgoing = outgoing;
+ return;
+ }
+
+ reset_outgoing(outgoing);
- n->connection->outgoing = outgoing;
+ if(outgoing->node->status.blacklisted) {
return;
}
- exit_configuration(&outgoing->config_tree); // discard old configuration if present
- init_configuration(&outgoing->config_tree);
- read_host_config(mesh, outgoing->config_tree, outgoing->name);
- outgoing->cfg = lookup_config(outgoing->config_tree, "Address");
+ if(mesh->connection_try_cb) {
+ mesh->connection_try_cb(mesh, (meshlink_node_t *)outgoing->node);
+ }
- get_config_bool(lookup_config(outgoing->config_tree, "blacklisted"), &blacklisted);
+ do_outgoing_connection(mesh, outgoing);
+}
- if(blacklisted) {
+/// Delayed close of a filedescriptor.
+static void tarpit(meshlink_handle_t *mesh, int fd) {
+ if(!fd) {
return;
}
- if(!outgoing->cfg) {
- if(n) {
- outgoing->aip = outgoing->nai = get_known_addresses(n);
- }
-
- if(!outgoing->nai) {
- logger(mesh, MESHLINK_ERROR, "No address known for %s", outgoing->name);
- return;
- }
+ if(mesh->pits[mesh->next_pit]) {
+ closesocket(mesh->pits[mesh->next_pit]);
}
- do_outgoing_connection(mesh, outgoing);
+ mesh->pits[mesh->next_pit++] = fd;
+
+ if(mesh->next_pit >= (int)(sizeof mesh->pits / sizeof mesh->pits[0])) {
+ mesh->next_pit = 0;
+ }
}
/*
meshlink_handle_t *mesh = loop->data;
listen_socket_t *l = data;
connection_t *c;
- sockaddr_t sa = {0};
+ sockaddr_t sa;
int fd;
socklen_t len = sizeof(sa);
+ memset(&sa, 0, sizeof(sa));
+
fd = accept(l->tcp.fd, &sa.sa, &len);
if(fd < 0) {
sockaddrunmap(&sa);
- // Check if we get many connections from the same host
-
- static sockaddr_t prev_sa;
- static int tarpit = -1;
+ /* Rate limit incoming connections to max_connection_burst/second. */
- if(tarpit >= 0) {
- closesocket(tarpit);
- tarpit = -1;
+ if(mesh->loop.now.tv_sec != mesh->connection_burst_time) {
+ mesh->connection_burst_time = mesh->loop.now.tv_sec;
+ mesh->connection_burst = 0;
}
- if(!sockaddrcmp_noport(&sa, &prev_sa)) {
- static int samehost_burst;
- static int samehost_burst_time;
-
- if(mesh->loop.now.tv_sec - samehost_burst_time > samehost_burst) {
- samehost_burst = 0;
- } else {
- samehost_burst -= mesh->loop.now.tv_sec - samehost_burst_time;
- }
-
- samehost_burst_time = mesh->loop.now.tv_sec;
- samehost_burst++;
-
- if(samehost_burst > max_connection_burst) {
- tarpit = fd;
- return;
- }
- }
-
- memcpy(&prev_sa, &sa, sizeof(sa));
-
- // Check if we get many connections from different hosts
-
- static int connection_burst;
- static int connection_burst_time;
-
- if(mesh->loop.now.tv_sec - connection_burst_time > connection_burst) {
- connection_burst = 0;
- } else {
- connection_burst -= mesh->loop.now.tv_sec - connection_burst_time;
- }
-
- connection_burst_time = mesh->loop.now.tv_sec;
- connection_burst++;
-
- if(connection_burst >= max_connection_burst) {
- connection_burst = max_connection_burst;
- tarpit = fd;
+ if(mesh->connection_burst >= max_connection_burst) {
+ tarpit(mesh, fd);
return;
}
+ mesh->connection_burst++;
+
// Accept the new connection
c = new_connection();
c->name = xstrdup("<unknown>");
- c->outcompression = mesh->self->connection->outcompression;
c->address = sa;
c->socket = fd;
c->last_ping_time = mesh->loop.now.tv_sec;
char *hostname = sockaddr2hostname(&sa);
- logger(mesh, MESHLINK_INFO, "Connection from %s", c->hostname);
+ logger(mesh, MESHLINK_INFO, "Connection from %s", hostname);
free(hostname);
io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ);
}
static void free_outgoing(outgoing_t *outgoing) {
- meshlink_handle_t *mesh = outgoing->mesh;
+ meshlink_handle_t *mesh = outgoing->node->mesh;
timeout_del(&mesh->loop, &outgoing->ev);
if(outgoing->ai) {
- freeaddrinfo(outgoing->ai);
- }
-
- if(outgoing->nai) {
- free_known_addresses(outgoing->nai);
- }
-
- if(outgoing->config_tree) {
- exit_configuration(&outgoing->config_tree);
- }
-
- if(outgoing->name) {
- free(outgoing->name);
+ if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
+ free_known_addresses(outgoing->ai);
+ } else {
+ freeaddrinfo(outgoing->ai);
+ }
}
free(outgoing);
}
-void try_outgoing_connections(meshlink_handle_t *mesh) {
- /* If there is no outgoing list yet, create one. Otherwise, mark all outgoings as deleted. */
-
- if(!mesh->outgoings) {
- mesh->outgoings = list_alloc((list_action_t)free_outgoing);
- } else {
- for list_each(outgoing_t, outgoing, mesh->outgoings) {
- outgoing->timeout = -1;
- }
- }
-
- /* Make sure there is one outgoing_t in the list for each ConnectTo. */
-
- // TODO: Drop support for ConnectTo since AutoConnect is now always on?
- for(config_t *cfg = lookup_config(mesh->config, "ConnectTo"); cfg; cfg = lookup_config_next(mesh->config, cfg)) {
- char *name;
- get_config_string(cfg, &name);
-
- if(!check_id(name)) {
- logger(mesh, MESHLINK_ERROR,
- "Invalid name for outgoing connection in %s line %d",
- cfg->file, cfg->line);
- free(name);
- continue;
- }
-
- bool found = false;
-
- for list_each(outgoing_t, outgoing, mesh->outgoings) {
- if(!strcmp(outgoing->name, name)) {
- found = true;
- outgoing->timeout = 0;
- break;
- }
- }
-
- if(!found) {
- outgoing_t *outgoing = xzalloc(sizeof(*outgoing));
- outgoing->mesh = mesh;
- outgoing->name = name;
- list_insert_tail(mesh->outgoings, outgoing);
- setup_outgoing_connection(mesh, outgoing);
- }
- }
-
- /* Terminate any connections whose outgoing_t is to be deleted. */
+void init_outgoings(meshlink_handle_t *mesh) {
+ mesh->outgoings = list_alloc((list_action_t)free_outgoing);
+}
- for list_each(connection_t, c, mesh->connections) {
- if(c->outgoing && c->outgoing->timeout == -1) {
- c->outgoing = NULL;
- logger(mesh, MESHLINK_INFO, "No more outgoing connection to %s", c->name);
- terminate_connection(mesh, c, c->status.active);
- }
+void exit_outgoings(meshlink_handle_t *mesh) {
+ if(mesh->outgoings) {
+ list_delete_list(mesh->outgoings);
+ mesh->outgoings = NULL;
}
-
- /* Delete outgoing_ts for which there is no ConnectTo. */
-
- for list_each(outgoing_t, outgoing, mesh->outgoings)
- if(outgoing->timeout == -1) {
- list_delete_node(mesh->outgoings, node);
- }
}