--- gvpe/src/connection.C 2004/05/10 18:57:07 1.34 +++ gvpe/src/connection.C 2005/03/04 08:15:04 1.44 @@ -1,8 +1,10 @@ /* connection.C -- manage a single connection - Copyright (C) 2003-2004 Marc Lehmann + Copyright (C) 2003-2005 Marc Lehmann - This program is free software; you can redistribute it and/or modify + This file is part of GVPE. + + GVPE is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -13,7 +15,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software + along with gvpe; if not, write to the Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ @@ -204,10 +206,10 @@ // but low on resources. struct net_rate_limiter : list { -# define NRL_ALPHA 1. - 1. / 600. // allow bursts -# define NRL_CUTOFF 10. // one event every CUTOFF seconds -# define NRL_EXPIRE NRL_CUTOFF * 30. // expire entries after this time -# define NRL_MAXDIF NRL_CUTOFF * (1. / (1. - NRL_ALPHA)) // maximum diff /count value +# define NRL_ALPHA (1. - 1. / 600.) // allow bursts +# define NRL_CUTOFF 10. // one event every CUTOFF seconds +# define NRL_EXPIRE (NRL_CUTOFF * 30.) // expire entries after this time +# define NRL_MAXDIF (NRL_CUTOFF * (1. / (1. - NRL_ALPHA))) // maximum diff /count value bool can (const sockinfo &si) { return can((u32)si.host); } bool can (u32 host); @@ -591,7 +593,9 @@ { connectmode = conf->connectmode; - rekey.start (NOW + ::conf.rekey); + // make sure rekeying timeouts are slightly asymmetric + rekey.start (NOW + ::conf.rekey + + (conf->id > THISNODE->id ? 10 : 0)); keepalive.start (NOW + ::conf.keepalive); // send queued packets @@ -627,6 +631,7 @@ // mask out protocols we cannot establish if (!conf->udp_port) protocol &= ~PROT_UDPv4; if (!conf->tcp_port) protocol &= ~PROT_TCPv4; + if (!conf->dns_port) protocol &= ~PROT_DNSv4; si.set (conf, protocol); } @@ -656,7 +661,33 @@ void connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) { - if (!vpn->send_vpn_packet (pkt, si, tos)) + bool ok; + + switch (si.prot) + { + case PROT_IPv4: + ok = vpn->send_ipv4_packet (pkt, si, tos); break; + case PROT_UDPv4: + ok = vpn->send_udpv4_packet (pkt, si, tos); break; +#if ENABLE_TCP + case PROT_TCPv4: + ok = vpn->send_tcpv4_packet (pkt, si, tos); break; +#endif +#if ENABLE_ICMP + case PROT_ICMPv4: + ok = vpn->send_icmpv4_packet (pkt, si, tos); break; +#endif +#if ENABLE_DNS + case PROT_DNSv4: + ok = send_dnsv4_packet (pkt, si, tos); break; +#endif + + default: + slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); + ok = false; + } + + if (!ok) reset_connection (); } @@ -744,8 +775,10 @@ { double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; - if (retry_int < 3600 * 8) + if (retry_int < conf->max_retry) retry_cnt++; + else + retry_int = conf->max_retry; w.start (NOW + retry_int); @@ -782,8 +815,11 @@ delete ictx; ictx = 0; delete octx; octx = 0; +#if ENABLE_DNS + delete dns; dns = 0; +#endif - si.host= 0; + si.host = 0; last_activity = 0; retry_cnt = 0; @@ -1096,23 +1132,24 @@ case vpn_packet::PT_CONNECT_INFO: if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) { - connect_info_packet *p = (connect_info_packet *) pkt; + connect_info_packet *p = (connect_info_packet *)pkt; - assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything - - connection *c = vpn->conns[p->id - 1]; + if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything + { + connection *c = vpn->conns[p->id - 1]; - c->conf->protocols = p->protocols; - protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); - p->si.upgrade_protocol (protocol, c->conf); + c->conf->protocols = p->protocols; + protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); + p->si.upgrade_protocol (protocol, c->conf); - slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", - conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); + slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", + conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); - const sockinfo &dsi = forward_si (p->si); + const sockinfo &dsi = forward_si (p->si); - if (dsi.valid ()) - c->send_auth_request (dsi, true); + if (dsi.valid ()) + c->send_auth_request (dsi, true); + } } break; @@ -1186,15 +1223,21 @@ return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; } -connection::connection(struct vpn *vpn_) -: vpn(vpn_) +connection::connection (struct vpn *vpn, conf_node *conf) +: vpn(vpn), conf(conf) , rekey (this, &connection::rekey_cb) , keepalive (this, &connection::keepalive_cb) , establish_connection (this, &connection::establish_connection_cb) +#if ENABLE_DNS +, dns (0) +#endif { octx = ictx = 0; retry_cnt = 0; + if (!conf->protocols) // make sure some protocol is enabled + conf->protocols = PROT_UDPv4; + connectmode = conf_node::C_ALWAYS; // initial setting reset_connection (); }