setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay));
#endif
-#if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
+#if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
int lowdelay = IPTOS_LOWDELAY;
- setsockopt(c->socket, SOL_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay));
+ setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay));
#endif
}
option = 1;
setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
-#if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
+#if defined(IPV6_V6ONLY)
if(sa->sa.sa_family == AF_INET6) {
- setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
+ setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
}
+#else
+#warning IPV6_V6ONLY not defined
#endif
if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
-#if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
+#if defined(IPV6_V6ONLY)
if(sa->sa.sa_family == AF_INET6) {
setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
#define IP_DONTFRAGMENT IP_DONTFRAG
#endif
-#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
+#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
option = IP_PMTUDISC_DO;
- setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
+ setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
}
-#elif defined(IPPROTO_IP) && defined(IP_DONTFRAGMENT)
+#elif defined(IP_DONTFRAGMENT)
if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
option = 1;
#warning No way to disable IPv4 fragmentation
#endif
-#if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
+#if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
option = IPV6_PMTUDISC_DO;
- setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
+ setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
}
-#elif defined(IPPROTO_IPV6) && defined(IPV6_DONTFRAG)
+#elif defined(IPV6_DONTFRAG)
if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
option = 1;
send_id(mesh, c);
}
-static void do_outgoing_pipe(meshlink_handle_t *mesh, connection_t *c, char *command) {
-#ifndef HAVE_MINGW
- int fd[2];
-
- if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
- logger(mesh, MESHLINK_ERROR, "Could not create socketpair: %s", strerror(errno));
- return;
- }
-
- if(fork()) {
- c->socket = fd[0];
- close(fd[1]);
- logger(mesh, MESHLINK_DEBUG, "Using proxy %s", command);
- return;
- }
-
- close(0);
- close(1);
- close(fd[0]);
- dup2(fd[1], 0);
- dup2(fd[1], 1);
- close(fd[1]);
-
- // Other filedescriptors should be closed automatically by CLOEXEC
-
- char *host = NULL;
- char *port = NULL;
-
- sockaddr2str(&c->address, &host, &port);
- setenv("REMOTEADDRESS", host, true);
- setenv("REMOTEPORT", port, true);
- setenv("NODE", c->name, true);
- setenv("NAME", mesh->self->name, true);
-
- int result = system(command);
-
- if(result < 0) {
- logger(mesh, MESHLINK_ERROR, "Could not execute %s: %s", command, strerror(errno));
- } else if(result) {
- logger(mesh, MESHLINK_ERROR, "%s exited with non-zero status %d", command, result);
- }
-
- exit(result);
-#else
- logger(mesh, MESHLINK_ERROR, "Proxy type exec not supported on this platform!");
- return;
-#endif
-}
-
static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) {
if(c->outbuf.len <= c->outbuf.offset) {
return;
}
}
-bool do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
- char *address, *port, *space;
- struct addrinfo *proxyai = NULL;
- int result;
+static bool get_recent(meshlink_handle_t *mesh, outgoing_t *outgoing) {
+ node_t *n = lookup_node(mesh, outgoing->name);
-begin:
+ if(!n) {
+ return false;
+ }
- 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;
+ outgoing->ai = get_known_addresses(n);
+ outgoing->aip = outgoing->ai;
+ return outgoing->aip;
+}
+
+static bool get_next_ai(meshlink_handle_t *mesh, outgoing_t *outgoing) {
+ if(!outgoing->ai) {
+ char *address = NULL;
+
+ if(get_config_string(outgoing->cfg, &address)) {
+ char *port;
+ char *space = strchr(address, ' ');
+
+ if(space) {
+ port = xstrdup(space + 1);
+ *space = 0;
+ } else {
+ if(!get_config_string(lookup_config(outgoing->config_tree, "Port"), &port)) {
+ logger(mesh, MESHLINK_ERROR, "No Port known for %s", outgoing->name);
+ return false;
+ }
+ }
+
+ outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
+ free(port);
+ free(address);
}
- get_config_string(outgoing->cfg, &address);
+ outgoing->aip = outgoing->ai;
+ } else {
+ outgoing->aip = outgoing->aip->ai_next;
+ }
+
+ return outgoing->aip;
+}
- space = strchr(address, ' ');
+static bool get_next_cfg(meshlink_handle_t *mesh, outgoing_t *outgoing, char *variable) {
+ (void)mesh;
- if(space) {
- port = xstrdup(space + 1);
- *space = 0;
- } 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;
+ if(!outgoing->cfg) {
+ outgoing->cfg = lookup_config(outgoing->config_tree, variable);
+ } else {
+ outgoing->cfg = lookup_config_next(outgoing->config_tree, outgoing->cfg);
+ }
+
+ return outgoing->cfg;
+}
+
+static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) {
+ bool start = false;
+
+ if(outgoing->state == OUTGOING_START) {
+ start = true;
+ outgoing->state = OUTGOING_CANONICAL;
+ }
+
+ if(outgoing->state == OUTGOING_CANONICAL) {
+ while(outgoing->aip || get_next_cfg(mesh, outgoing, "CanonicalAddress")) {
+ if(get_next_ai(mesh, outgoing)) {
+ return true;
+ } else {
+ freeaddrinfo(outgoing->ai);
+ outgoing->ai = NULL;
+ outgoing->aip = NULL;
}
}
- outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
- free(address);
- free(port);
-
- outgoing->aip = outgoing->ai;
- outgoing->cfg = lookup_config_next(outgoing->config_tree, outgoing->cfg);
+ outgoing->state = OUTGOING_RECENT;
}
- if(!outgoing->aip) {
- if(outgoing->ai) {
- freeaddrinfo(outgoing->ai);
+ if(outgoing->state == OUTGOING_RECENT) {
+ while(outgoing->aip || get_next_cfg(mesh, outgoing, "Address")) {
+ if(get_next_ai(mesh, outgoing)) {
+ return true;
+ } else {
+ freeaddrinfo(outgoing->ai);
+ outgoing->ai = NULL;
+ outgoing->aip = NULL;
+ }
}
- outgoing->ai = NULL;
+ outgoing->state = OUTGOING_KNOWN;
+ }
+
+ if(outgoing->state == OUTGOING_KNOWN) {
+ if(!outgoing->aip) {
+ get_recent(mesh, outgoing);
+ } else {
+ outgoing->aip = outgoing->aip->ai_next;
+ }
- if(outgoing->nai) {
- free_known_addresses(outgoing->nai);
+ if(outgoing->aip) {
+ return true;
+ } else {
+ free_known_addresses(outgoing->ai);
+ outgoing->ai = NULL;
+ outgoing->aip = NULL;
}
- outgoing->nai = NULL;
+ outgoing->state = OUTGOING_END;
+ }
- goto begin;
+ if(start) {
+ outgoing->state = OUTGOING_NO_KNOWN_ADDRESSES;
+ }
+
+ return false;
+}
+
+bool 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->name);
+ } else {
+ logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->name);
+ retry_outgoing(mesh, outgoing);
+ }
+
+ return false;
}
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);
if(!mesh->proxytype) {
c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
configure_tcp(c);
- } else if(mesh->proxytype == PROXY_EXEC) {
- do_outgoing_pipe(mesh, c, mesh->proxyhost);
} else {
proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
}
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;
fcntl(c->socket, F_SETFD, FD_CLOEXEC);
#endif
- if(mesh->proxytype != PROXY_EXEC) {
-#if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
- int option = 1;
+#if defined(IPV6_V6ONLY)
- if(c->address.sa.sa_family == AF_INET6) {
- setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
- }
+ if(c->address.sa.sa_family == AF_INET6) {
+ static const int option = 1;
+ setsockopt(c->socket, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
+ }
#endif
- bind_to_address(mesh, c);
- }
+ bind_to_address(mesh, c);
/* Connect */
if(!mesh->proxytype) {
result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
- } else if(mesh->proxytype == PROXY_EXEC) {
- result = 0;
} else {
result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
freeaddrinfo(proxyai);
return;
}
+
+ if(outgoing->ai) {
+ if(outgoing->state == OUTGOING_KNOWN) {
+ free_known_addresses(outgoing->ai);
+ } else {
+ freeaddrinfo(outgoing->ai);
+ }
+ }
+
+ outgoing->cfg = NULL;
+
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");
-
get_config_bool(lookup_config(outgoing->config_tree, "blacklisted"), &blacklisted);
+ outgoing->state = OUTGOING_START;
+
if(blacklisted) {
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->connection_try_cb) {
+ node_t *n = lookup_node(mesh, outgoing->name);
+ mesh->connection_try_cb(mesh, (struct meshlink_node *)n);
}
do_outgoing_connection(mesh, outgoing);
}
+/// Delayed close of a filedescriptor.
+static void tarpit(int fd) {
+ static int pits[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
+ static int next_pit = 0;
+
+ if(pits[next_pit] != -1) {
+ closesocket(pits[next_pit]);
+ }
+
+ pits[next_pit++] = fd;
+
+ if(next_pit >= (int)(sizeof pits / sizeof pits[0])) {
+ next_pit = 0;
+ }
+}
+
/*
accept a new tcp connect and create a
new connection
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;
-
- if(tarpit >= 0) {
- closesocket(tarpit);
- tarpit = -1;
- }
-
- 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
+ /* Rate limit incoming connections to max_connection_burst/second. */
static int connection_burst;
static int connection_burst_time;
- if(mesh->loop.now.tv_sec - connection_burst_time > connection_burst) {
+ if(mesh->loop.now.tv_sec != connection_burst_time) {
+ connection_burst_time = mesh->loop.now.tv_sec;
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;
+ tarpit(fd);
return;
}
+ connection_burst++;
+
// Accept the new connection
c = new_connection();
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);
timeout_del(&mesh->loop, &outgoing->ev);
if(outgoing->ai) {
- freeaddrinfo(outgoing->ai);
- }
-
- if(outgoing->nai) {
- free_known_addresses(outgoing->nai);
+ if(outgoing->state == OUTGOING_KNOWN) {
+ free_known_addresses(outgoing->ai);
+ } else {
+ freeaddrinfo(outgoing->ai);
+ }
}
if(outgoing->config_tree) {
if(!check_id(name)) {
logger(mesh, MESHLINK_ERROR,
- "Invalid name for outgoing connection in %s line %d",
- cfg->file, cfg->line);
+ "Invalid name for outgoing connection in line %d",
+ cfg->line);
free(name);
continue;
}