#include "meshlink.h"
#include "sockaddr.h"
+#define MAXSOCKETS 8 /* Probably overkill... */
+
+typedef struct listen_socket_t {
+ struct io_t tcp;
+ struct io_t udp;
+ sockaddr_t sa;
+ bool bindto;
+} listen_socket_t;
+
typedef enum proxytype_t {
PROXY_NONE = 0,
PROXY_SOCKS4,
meshlink_log_level_t log_level;
pthread_t thread;
- struct list_t *sockets;
+ listen_socket_t listen_socket[MAXSOCKETS];
+ int listen_sockets;
struct node_t *self;
/* MAXBUFSIZE is the maximum size of a request: enough for a MAXSIZEd packet or a 8192 bits RSA key */
#define MAXBUFSIZE ((MAXSIZE > 2048 ? MAXSIZE : 2048) + 128)
-#define MAXSOCKETS 8 /* Probably overkill... */
-
typedef struct vpn_packet_t {
struct {
unsigned int probe:1;
PACKET_PROBE
} packet_type_t;
-typedef struct listen_socket_t {
- struct io_t tcp;
- struct io_t udp;
- sockaddr_t sa;
- bool bindto;
-} listen_socket_t;
-
#include "conf.h"
#include "list.h"
extern int addressfamily;
extern unsigned replaywin;
-extern listen_socket_t listen_socket[MAXSOCKETS];
-extern int listen_sockets;
extern int keylifetime;
extern int max_connection_burst;
extern bool do_prune;
if(candidate) {
*sa = &candidate->address;
- *sock = rand() % listen_sockets;
+ *sock = rand() % mesh->listen_sockets;
}
/* Make sure we have a suitable socket for the chosen address */
- if(listen_socket[*sock].sa.sa.sa_family != (*sa)->sa.sa_family) {
- for(int i = 0; i < listen_sockets; i++) {
- if(listen_socket[i].sa.sa.sa_family == (*sa)->sa.sa_family) {
+ if(mesh->listen_socket[*sock].sa.sa.sa_family != (*sa)->sa.sa_family) {
+ for(int i = 0; i < mesh->listen_sockets; i++) {
+ if(mesh->listen_socket[i].sa.sa.sa_family == (*sa)->sa.sa_family) {
*sock = i;
break;
}
}
};
- *sock = rand() % listen_sockets;
+ *sock = rand() % mesh->listen_sockets;
- if(listen_socket[*sock].sa.sa.sa_family == AF_INET6) {
+ if(mesh->listen_socket[*sock].sa.sa.sa_family == AF_INET6) {
if(mesh->localdiscovery_address.sa.sa_family == AF_INET6) {
mesh->localdiscovery_address.in6.sin6_port = n->prevedge->address.in.sin_port;
*sa = &mesh->localdiscovery_address;
} else {
broadcast_ipv6.in6.sin6_port = n->prevedge->address.in.sin_port;
- broadcast_ipv6.in6.sin6_scope_id = listen_socket[*sock].sa.in6.sin6_scope_id;
+ broadcast_ipv6.in6.sin6_scope_id = mesh->listen_socket[*sock].sa.in6.sin6_scope_id;
*sa = &broadcast_ipv6;
}
} else {
else
choose_udp_address(to, &sa, &sock);
- if(sendto(listen_socket[sock].udp.fd, data, len, 0, &sa->sa, SALEN(sa->sa)) < 0 && !sockwouldblock(sockerrno)) {
+ if(sendto(mesh->listen_socket[sock].udp.fd, data, len, 0, &sa->sa, SALEN(sa->sa)) < 0 && !sockwouldblock(sockerrno)) {
if(sockmsgsize(sockerrno)) {
if(to->maxmtu >= len)
to->maxmtu = len - 1;
return;
}
- n->sock = ls - listen_socket;
+ n->sock = ls - mesh->listen_socket;
receive_udppacket(n, &pkt);
}
// Ignore duplicate addresses
bool found = false;
- for(int i = 0; i < listen_sockets; i++)
- if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
+ for(int i = 0; i < mesh->listen_sockets; i++)
+ if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
found = true;
break;
}
if(found)
continue;
- if(listen_sockets >= MAXSOCKETS) {
+ if(mesh->listen_sockets >= MAXSOCKETS) {
logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
return false;
}
continue;
}
- io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
- io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
+ io_add(&mesh->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ);
+ io_add(&mesh->listen_socket[mesh->listen_sockets].udp, handle_incoming_vpn_data, &mesh->listen_socket[mesh->listen_sockets], udp_fd, IO_READ);
if(debug_level >= DEBUG_CONNECTIONS) {
char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
free(hostname);
}
- listen_socket[listen_sockets].bindto = bindto;
- memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
- listen_sockets++;
+ mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
+ memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
+ mesh->listen_sockets++;
}
freeaddrinfo(ai);
/* Open sockets */
- listen_sockets = 0;
+ mesh->listen_sockets = 0;
int cfgs = 0;
if(!add_listen_address(address, NULL))
return false;
- if(!listen_sockets) {
+ if(!mesh->listen_sockets) {
logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
return false;
}
free_connection(mesh->self->connection);
}
- for(int i = 0; i < listen_sockets; i++) {
- io_del(&listen_socket[i].tcp);
- io_del(&listen_socket[i].udp);
- close(listen_socket[i].tcp.fd);
- close(listen_socket[i].udp.fd);
+ for(int i = 0; i < mesh->listen_sockets; i++) {
+ io_del(&mesh->listen_socket[i].tcp);
+ io_del(&mesh->listen_socket[i].udp);
+ close(mesh->listen_socket[i].tcp.fd);
+ close(mesh->listen_socket[i].udp.fd);
}
exit_requests();
int seconds_till_retry = 5;
int max_connection_burst = 100;
-listen_socket_t listen_socket[MAXSOCKETS];
-int listen_sockets;
-
/* Setup sockets */
static void configure_tcp(connection_t *c) {
static bool bind_to_address(connection_t *c) {
int s = -1;
- for(int i = 0; i < listen_sockets && listen_socket[i].bindto; i++) {
- if(listen_socket[i].sa.sa.sa_family != c->address.sa.sa_family)
+ 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;
if(s < 0)
return false;
- sockaddr_t sa = listen_socket[s].sa;
+ 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)
if(sa) {
n->address = *sa;
n->sock = 0;
- for(int i = 0; i < listen_sockets; i++) {
- if(listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
+ for(int i = 0; i < mesh->listen_sockets; i++) {
+ if(mesh->listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
n->sock = i;
break;
}