X-Git-Url: http://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet_socket.c;h=cbe9e6884684b9b6bff0c15d9966a917a09cec49;hb=5fc1ed17f41f0c535cf57a4b7e00cd6d45759503;hp=fdeac79ae945ea1611284a9e6872a87bc20840a3;hpb=7f58ed7685f9fcd5271359a8c896670a835e1f95;p=tinc diff --git a/src/net_socket.c b/src/net_socket.c index fdeac79a..cbe9e688 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -1,7 +1,7 @@ /* net_socket.c -- Handle various kinds of sockets. - Copyright (C) 1998-2002 Ivo Timmermans , - 2000-2002 Guus Sliepen + Copyright (C) 1998-2002 Ivo Timmermans , + 2000-2002 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_socket.c,v 1.1.2.5 2002/03/01 11:18:34 guus Exp $ + $Id: net_socket.c,v 1.1.2.19 2002/09/09 19:39:59 guus Exp $ */ #include "config.h" @@ -26,8 +26,13 @@ #include #include #include -#ifdef HAVE_LINUX +#ifdef HAVE_NETINET_IN_SYSTM_H + #include +#endif +#ifdef HAVE_NETINET_IP_H #include +#endif +#ifdef HAVE_NETINET_TCP_H #include #endif #include @@ -66,14 +71,16 @@ #include "system.h" +#ifndef HAVE_RAND_PSEUDO_BYTES +#define RAND_pseudo_bytes RAND_bytes +#endif + int addressfamily = AF_INET; int maxtimeout = 900; int seconds_till_retry = 5; -int tcp_socket[MAXSOCKETS]; -int udp_socket[MAXSOCKETS]; -int tcp_sockets = 0; -int udp_sockets = 0; +listen_socket_t listen_socket[MAXSOCKETS]; +int listen_sockets; /* Setup sockets */ @@ -82,12 +89,14 @@ int setup_listen_socket(sockaddr_t *sa) int nfd, flags; char *addrstr; int option; -#ifdef HAVE_LINUX +#if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) char *interface; struct ifreq ifr; #endif -cp - if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0) + cp(); + nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP); + + if(nfd < 0) { syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno)); return -1; @@ -105,14 +114,19 @@ cp option = 1; setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); -#ifdef HAVE_LINUX + +#if defined(SOL_TCP) && defined(TCP_NODELAY) setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option)); +#endif +#if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY) option = IPTOS_LOWDELAY; setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option)); +#endif if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface)) { +#if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ); if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) @@ -121,8 +135,10 @@ cp syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno)); return -1; } - } +#else + syslog(LOG_WARNING, _("BindToDevice not supported on this platform")); #endif + } if(bind(nfd, &sa->sa, SALEN(sa->sa))) { @@ -139,7 +155,7 @@ cp syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno)); return -1; } -cp + cp(); return nfd; } @@ -148,12 +164,14 @@ int setup_vpn_in_socket(sockaddr_t *sa) int nfd, flags; char *addrstr; int option; -#ifdef HAVE_LINUX +#if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) char *interface; struct ifreq ifr; #endif -cp - if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0) + cp(); + nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP); + + if(nfd < 0) { syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno)); return -1; @@ -169,7 +187,8 @@ cp option = 1; setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); -#ifdef HAVE_LINUX + +#if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface)) { memset(&ifr, 0, sizeof(ifr)); @@ -191,33 +210,33 @@ cp free(addrstr); return -1; } -cp + cp(); return nfd; } void retry_outgoing(outgoing_t *outgoing) { event_t *event; -cp + cp(); outgoing->timeout += 5; if(outgoing->timeout > maxtimeout) outgoing->timeout = maxtimeout; event = new_event(); event->handler = (event_handler_t)setup_outgoing_connection; - event->time = time(NULL) + outgoing->timeout; + event->time = now + outgoing->timeout; event->data = outgoing; event_add(event); if(debug_lvl >= DEBUG_CONNECTIONS) syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout); -cp + cp(); } int setup_outgoing_socket(connection_t *c) { int option; -cp + cp(); if(debug_lvl >= DEBUG_CONNECTIONS) syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname); @@ -231,10 +250,12 @@ cp /* Optimize TCP settings */ -#ifdef HAVE_LINUX +#if defined(SOL_TCP) && defined(TCP_NODELAY) option = 1; setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option)); +#endif +#if defined(SOL_IP) && defined(IP_TOS) option = IPTOS_LOWDELAY; setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option)); #endif @@ -250,28 +271,28 @@ cp if(debug_lvl >= DEBUG_CONNECTIONS) syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname); -cp + cp(); return 0; } void finish_connecting(connection_t *c) { -cp + cp(); if(debug_lvl >= DEBUG_CONNECTIONS) syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname); - c->last_ping_time = time(NULL); + c->last_ping_time = now; send_id(c); -cp + cp(); } void do_outgoing_connection(connection_t *c) { char *address, *port; int option, result, flags; -cp + cp(); begin: if(!c->outgoing->ai) { @@ -280,7 +301,6 @@ begin: if(debug_lvl >= DEBUG_CONNECTIONS) syslog(LOG_ERR, _("Could not set up a meta connection to %s"), c->name); c->status.remove = 1; - do_prune = 1; retry_outgoing(c->outgoing); return; } @@ -328,10 +348,12 @@ begin: /* Optimize TCP settings */ -#ifdef HAVE_LINUX +#if defined(SOL_TCP) && defined(TCP_NODELAY) option = 1; setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option)); +#endif +#if defined(SOL_IP) && defined(IP_TOS) option = IPTOS_LOWDELAY; setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option)); #endif @@ -367,14 +389,14 @@ begin: finish_connecting(c); return; -cp + cp(); } void setup_outgoing_connection(outgoing_t *outgoing) { connection_t *c; node_t *n; -cp + cp(); n = lookup_node(outgoing->name); if(n) @@ -408,7 +430,7 @@ cp } c->outgoing = outgoing; - c->last_ping_time = time(NULL); + c->last_ping_time = now; connection_add(c); @@ -424,13 +446,17 @@ int handle_new_meta_connection(int sock) connection_t *c; sockaddr_t sa; int fd, len = sizeof(sa); -cp - if((fd = accept(sock, &sa.sa, &len)) < 0) + cp(); + fd = accept(sock, &sa.sa, &len); + + if(fd < 0) { syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno)); return -1; } + sockaddrunmap(&sa); + c = new_connection(); c->outcipher = myself->connection->outcipher; c->outdigest = myself->connection->outdigest; @@ -440,7 +466,7 @@ cp c->address = sa; c->hostname = sockaddr2hostname(&sa); c->socket = fd; - c->last_ping_time = time(NULL); + c->last_ping_time = now; if(debug_lvl >= DEBUG_CONNECTIONS) syslog(LOG_NOTICE, _("Connection from %s"), c->hostname); @@ -449,7 +475,7 @@ cp c->allow_request = ID; send_id(c); -cp + cp(); return 0; } @@ -458,7 +484,7 @@ void try_outgoing_connections(void) static config_t *cfg = NULL; char *name; outgoing_t *outgoing; -cp + cp(); for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) { get_config_string(cfg, &name);