Allow multiple listening sockets.

This commit is contained in:
Guus Sliepen 2002-02-26 23:26:41 +00:00
parent 2ac7be0d51
commit 50403909b6
6 changed files with 76 additions and 40 deletions

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: net.c,v 1.35.4.157 2002/02/20 16:04:07 guus Exp $ $Id: net.c,v 1.35.4.158 2002/02/26 23:26:41 guus Exp $
*/ */
#include "config.h" #include "config.h"
@ -80,6 +80,7 @@ void build_fdset(fd_set *fs)
{ {
avl_node_t *node; avl_node_t *node;
connection_t *c; connection_t *c;
int i;
cp cp
FD_ZERO(fs); FD_ZERO(fs);
@ -89,8 +90,12 @@ cp
FD_SET(c->socket, fs); FD_SET(c->socket, fs);
} }
FD_SET(tcp_socket, fs); for(i = 0; i < tcp_sockets; i++)
FD_SET(udp_socket, fs); FD_SET(tcp_socket[i], fs);
for(i = 0; i < udp_sockets; i++)
FD_SET(udp_socket[i], fs);
FD_SET(device_fd, fs); FD_SET(device_fd, fs);
cp cp
} }
@ -271,7 +276,7 @@ void check_network_activity(fd_set *f)
{ {
connection_t *c; connection_t *c;
avl_node_t *node; avl_node_t *node;
int result; int result, i;
int len = sizeof(result); int len = sizeof(result);
vpn_packet_t packet; vpn_packet_t packet;
cp cp
@ -281,8 +286,9 @@ cp
route_outgoing(&packet); route_outgoing(&packet);
} }
if(FD_ISSET(udp_socket, f)) for(i = 0; i < udp_sockets; i++)
handle_incoming_vpn_data(); if(FD_ISSET(udp_socket[i], f))
handle_incoming_vpn_data(udp_socket[i]);
for(node = connection_tree->head; node; node = node->next) for(node = connection_tree->head; node; node = node->next)
{ {
@ -316,8 +322,9 @@ cp
} }
} }
if(FD_ISSET(tcp_socket, f)) for(i = 0; i < tcp_sockets; i++)
handle_new_meta_connection(); if(FD_ISSET(tcp_socket[i], f))
handle_new_meta_connection(tcp_socket[i]);
cp cp
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: net.h,v 1.9.4.42 2002/02/26 22:47:51 guus Exp $ $Id: net.h,v 1.9.4.43 2002/02/26 23:26:41 guus Exp $
*/ */
#ifndef __TINC_NET_H__ #ifndef __TINC_NET_H__
@ -34,6 +34,7 @@
#define MAXSIZE 1600 /* MTU + header (seqno) and trailer (CBC padding and HMAC) */ #define MAXSIZE 1600 /* MTU + header (seqno) and trailer (CBC padding and HMAC) */
#define MAXBUFSIZE 2048 /* Probably way too much, but it must fit every possible request. */ #define MAXBUFSIZE 2048 /* Probably way too much, but it must fit every possible request. */
#define MAXSOCKETS 128 /* Overkill... */
typedef struct mac_t typedef struct mac_t
{ {
@ -105,8 +106,10 @@ extern char *status_text[];
#include "connection.h" /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */ #include "connection.h" /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */
extern int tcp_socket; extern int tcp_socket[MAXSOCKETS];
extern int udp_socket; extern int udp_socket[MAXSOCKETS];
extern int tcp_sockets;
extern int udp_sockets;
extern int keyexpires; extern int keyexpires;
extern int keylifetime; extern int keylifetime;
extern int do_prune; extern int do_prune;
@ -114,10 +117,10 @@ extern int do_purge;
extern char *myport; extern char *myport;
extern void retry_outgoing(outgoing_t *); extern void retry_outgoing(outgoing_t *);
extern void handle_incoming_vpn_data(void); extern void handle_incoming_vpn_data(int);
extern void finish_connecting(connection_t *); extern void finish_connecting(connection_t *);
extern void do_outgoing_connection(connection_t *); extern void do_outgoing_connection(connection_t *);
extern int handle_new_meta_connection(void); extern int handle_new_meta_connection(int);
extern int setup_listen_socket(sockaddr_t *); extern int setup_listen_socket(sockaddr_t *);
extern int setup_vpn_in_socket(sockaddr_t *); extern int setup_vpn_in_socket(sockaddr_t *);
extern void send_packet(struct node_t *, vpn_packet_t *); extern void send_packet(struct node_t *, vpn_packet_t *);

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: net_packet.c,v 1.1.2.4 2002/02/20 22:37:38 guus Exp $ $Id: net_packet.c,v 1.1.2.5 2002/02/26 23:26:41 guus Exp $
*/ */
#include "config.h" #include "config.h"
@ -258,7 +258,7 @@ cp
/* Send the packet */ /* Send the packet */
if((sendto(udp_socket, (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) if((sendto(udp_socket[0], (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0)
{ {
syslog(LOG_ERR, _("Error sending packet to %s (%s): %s"), syslog(LOG_ERR, _("Error sending packet to %s (%s): %s"),
n->name, n->hostname, strerror(errno)); n->name, n->hostname, strerror(errno));
@ -349,7 +349,7 @@ cp
cp cp
} }
void handle_incoming_vpn_data(void) void handle_incoming_vpn_data(int sock)
{ {
vpn_packet_t pkt; vpn_packet_t pkt;
int x, l = sizeof(x); int x, l = sizeof(x);
@ -358,10 +358,10 @@ void handle_incoming_vpn_data(void)
socklen_t fromlen = sizeof(from); socklen_t fromlen = sizeof(from);
node_t *n; node_t *n;
cp cp
if(getsockopt(udp_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
{ {
syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s"), syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s"),
__FILE__, __LINE__, udp_socket, strerror(errno)); __FILE__, __LINE__, sock, strerror(errno));
return; return;
} }
if(x) if(x)
@ -370,7 +370,7 @@ cp
return; return;
} }
if((pkt.len = recvfrom(udp_socket, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen)) <= 0) if((pkt.len = recvfrom(sock, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen)) <= 0)
{ {
syslog(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno)); syslog(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
return; return;

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: net_setup.c,v 1.1.2.4 2002/02/20 22:15:32 guus Exp $ $Id: net_setup.c,v 1.1.2.5 2002/02/26 23:26:41 guus Exp $
*/ */
#include "config.h" #include "config.h"
@ -219,9 +219,9 @@ int setup_myself(void)
{ {
config_t *cfg; config_t *cfg;
subnet_t *subnet; subnet_t *subnet;
char *name, *mode, *afname, *cipher, *digest; char *name, *hostname, *mode, *afname, *cipher, *digest;
struct addrinfo hint, *ai; struct addrinfo hint, *ai, *aip;
int choice, err; int choice, err, sock;
cp cp
myself = new_node(); myself = new_node();
myself->connection = new_connection(); myself->connection = new_connection();
@ -475,10 +475,20 @@ cp
return -1; return -1;
} }
if((tcp_socket = setup_listen_socket((sockaddr_t *)ai->ai_addr)) < 0) tcp_sockets = 0;
for(aip = ai; aip; aip = aip->ai_next)
{ {
syslog(LOG_ERR, _("Unable to set up a listening TCP socket!")); if((sock = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0)
return -1; continue;
tcp_socket[++tcp_sockets] = sock;
if(debug_lvl >= DEBUG_CONNECTIONS)
{
hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr);
syslog(LOG_NOTICE, _("Listening on %s/tcp"), hostname);
free(hostname);
}
} }
freeaddrinfo(ai); freeaddrinfo(ai);
@ -492,15 +502,25 @@ cp
return -1; return -1;
} }
if((udp_socket = setup_vpn_in_socket((sockaddr_t *)ai->ai_addr)) < 0) udp_sockets = 0;
for(aip = ai; aip; aip = aip->ai_next)
{ {
syslog(LOG_ERR, _("Unable to set up a listening UDP socket!")); if((sock = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0)
return -1; continue;
udp_socket[++udp_sockets] = sock;
if(debug_lvl >= DEBUG_CONNECTIONS)
{
hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr);
syslog(LOG_NOTICE, _("Listening on %s/udp"), hostname);
free(hostname);
}
} }
freeaddrinfo(ai); freeaddrinfo(ai);
syslog(LOG_NOTICE, _("Ready: listening on port %s"), myport); syslog(LOG_NOTICE, _("Ready"));
cp cp
return 0; return 0;
} }
@ -548,6 +568,7 @@ void close_network_connections(void)
{ {
avl_node_t *node, *next; avl_node_t *node, *next;
connection_t *c; connection_t *c;
int i;
cp cp
for(node = connection_tree->head; node; node = next) for(node = connection_tree->head; node; node = next)
{ {
@ -561,8 +582,10 @@ cp
if(myself && myself->connection) if(myself && myself->connection)
terminate_connection(myself->connection, 0); terminate_connection(myself->connection, 0);
close(udp_socket); for(i = 0; i < udp_sockets; i++)
close(tcp_socket); close(udp_socket[i]);
for(i = 0; i < tcp_sockets; i++)
close(tcp_socket[i]);
exit_events(); exit_events();
exit_edges(); exit_edges();

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: net_socket.c,v 1.1.2.3 2002/02/20 22:37:38 guus Exp $ $Id: net_socket.c,v 1.1.2.4 2002/02/26 23:26:41 guus Exp $
*/ */
#include "config.h" #include "config.h"
@ -70,8 +70,11 @@ int addressfamily = AF_INET;
int maxtimeout = 900; int maxtimeout = 900;
int seconds_till_retry = 5; int seconds_till_retry = 5;
int tcp_socket = -1; int tcp_socket[MAXSOCKETS];
int udp_socket = -1; int udp_socket[MAXSOCKETS];
int tcp_sockets = 0;
int udp_sockets = 0;
/* Setup sockets */ /* Setup sockets */
int setup_listen_socket(sockaddr_t *sa) int setup_listen_socket(sockaddr_t *sa)
@ -406,13 +409,13 @@ cp
accept a new tcp connect and create a accept a new tcp connect and create a
new connection new connection
*/ */
int handle_new_meta_connection() int handle_new_meta_connection(int sock)
{ {
connection_t *c; connection_t *c;
sockaddr_t sa; sockaddr_t sa;
int fd, len = sizeof(sa); int fd, len = sizeof(sa);
cp cp
if((fd = accept(tcp_socket, &sa.sa, &len)) < 0) if((fd = accept(sock, &sa.sa, &len)) < 0)
{ {
syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno)); syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
return -1; return -1;

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: protocol.h,v 1.5.4.26 2002/02/11 15:59:18 guus Exp $ $Id: protocol.h,v 1.5.4.27 2002/02/26 23:26:41 guus Exp $
*/ */
#ifndef __TINC_PROTOCOL_H__ #ifndef __TINC_PROTOCOL_H__
@ -50,8 +50,8 @@ enum {
/* Maximum size of strings in a request */ /* Maximum size of strings in a request */
#define MAX_STRING_SIZE 1024 #define MAX_STRING_SIZE 2048
#define MAX_STRING "%1024s" #define MAX_STRING "%2048s"
/* Basic functions */ /* Basic functions */