--- gvpe/src/connection.C 2007/11/10 05:14:22 1.60 +++ gvpe/src/connection.C 2008/08/08 16:48:00 1.72 @@ -1,27 +1,39 @@ /* connection.C -- manage a single connection - Copyright (C) 2003-2005 Marc Lehmann + Copyright (C) 2003-2008 Marc Lehmann 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. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with gvpe; if not, write to the Free Software - Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + 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 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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, see . + + Additional permission under GNU GPL version 3 section 7 + + If you modify this Program, or any covered work, by linking or + combining it with the OpenSSL project's OpenSSL library (or a modified + version of that library), containing parts covered by the terms of the + OpenSSL or SSLeay licenses, the licensors of this Program grant you + additional permission to convey the resulting work. Corresponding + Source for a non-source form of such a combination shall include the + source code for the parts of OpenSSL used as well as that of the + covered work. */ #include "config.h" #include +#include +#include #include #include @@ -48,6 +60,47 @@ #include "lzf/lzf_c.c" #include "lzf/lzf_d.c" +////////////////////////////////////////////////////////////////////////////// + +static std::queue< std::pair > rs_queue; +static ev::child rs_child_ev; + +void // c++ requires external linkage here, apparently :( +rs_child_cb (ev::child &w, int revents) +{ + w.stop (); + + if (rs_queue.empty ()) + return; + + pid_t pid = run_script (*rs_queue.front ().first, false); + if (pid) + { + w.set (pid); + w.start (); + } + else + slog (L_WARN, rs_queue.front ().second); + + delete rs_queue.front ().first; + rs_queue.pop (); +} + +// despite the fancy name, this is quite a hack +static void +run_script_queued (run_script_cb *cb, const char *warnmsg) +{ + rs_queue.push (std::make_pair (cb, warnmsg)); + + if (!rs_child_ev.is_active ()) + { + rs_child_ev.set (); + rs_child_ev (); + } +} + +////////////////////////////////////////////////////////////////////////////// + struct crypto_ctx { EVP_CIPHER_CTX cctx; @@ -84,7 +137,8 @@ EVP_MD_CTX_cleanup (&ctx); } -struct rsa_entry { +struct rsa_entry +{ tstamp expire; rsaid id; rsachallenge chg; @@ -92,13 +146,13 @@ struct rsa_cache : list { - void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; + inline void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; bool find (const rsaid &id, rsachallenge &chg) { for (iterator i = begin (); i != end (); ++i) { - if (!memcmp (&id, &i->id, sizeof id) && i->expire > ev::ev_now ()) + if (!memcmp (&id, &i->id, sizeof id) && i->expire > ev_now ()) { memcpy (&chg, &i->chg, sizeof chg); @@ -120,7 +174,7 @@ RAND_bytes ((unsigned char *)&id, sizeof id); RAND_bytes ((unsigned char *)&chg, sizeof chg); - e.expire = ev::ev_now () + RSA_TTL; + e.expire = ev_now () + RSA_TTL; e.id = id; memcpy (&e.chg, &chg, sizeof chg); @@ -131,8 +185,8 @@ } rsa_cache () - : cleaner (this, &rsa_cache::cleaner_cb) { + cleaner.set (this); cleaner.set (RSA_TTL, RSA_TTL); } @@ -145,7 +199,7 @@ else { for (iterator i = begin (); i != end (); ) - if (i->expire <= ev::ev_now ()) + if (i->expire <= ev_now ()) i = erase (i); else ++i; @@ -154,46 +208,80 @@ ////////////////////////////////////////////////////////////////////////////// -void pkt_queue::put (net_packet *p) +pkt_queue::pkt_queue (double max_ttl, int max_queue) +: max_ttl (max_ttl), max_queue (max_queue) { - if (queue[i]) - { - delete queue[i]; - j = (j + 1) % QUEUEDEPTH; - } + queue = new pkt [max_queue]; - queue[i] = p; + i = 0; + j = 0; - i = (i + 1) % QUEUEDEPTH; + expire.set (this); } -net_packet *pkt_queue::get () +pkt_queue::~pkt_queue () +{ + while (net_packet *p = get ()) + delete p; + + delete [] queue; +} + +void pkt_queue::expire_cb (ev::timer &w, int revents) { - net_packet *p = queue[j]; + ev_tstamp expire = ev_now () - max_ttl; - if (p) + for (;;) { - queue[j] = 0; - j = (j + 1) % QUEUEDEPTH; - } + if (empty ()) + break; - return p; + double diff = queue[j].tstamp - expire; + + if (diff >= 0.) + { + w.start (diff > 0.5 ? diff : 0.5); + break; + } + + delete get (); + } } -pkt_queue::pkt_queue () +void pkt_queue::put (net_packet *p) { - memset (queue, 0, sizeof (queue)); - i = 0; - j = 0; + ev_tstamp now = ev_now (); + + // start expiry timer + if (empty ()) + expire.start (max_ttl); + + int ni = i + 1 == max_queue ? 0 : i + 1; + + if (ni == j) + delete get (); + + queue[i].pkt = p; + queue[i].tstamp = now; + + i = ni; } -pkt_queue::~pkt_queue () +net_packet *pkt_queue::get () { - for (i = QUEUEDEPTH; --i > 0; ) - delete queue[i]; + if (empty ()) + return 0; + + net_packet *p = queue[j].pkt; + queue[j].pkt = 0; + + j = j + 1 == max_queue ? 0 : j + 1; + + return p; } -struct net_rateinfo { +struct net_rateinfo +{ u32 host; double pcnt, diff; tstamp last; @@ -222,7 +310,7 @@ for (i = begin (); i != end (); ) if (i->host == host) break; - else if (i->last < ev::ev_now () - NRL_EXPIRE) + else if (i->last < ev_now () - NRL_EXPIRE) i = erase (i); else i++; @@ -234,7 +322,7 @@ ri.host = host; ri.pcnt = 1.; ri.diff = NRL_MAXDIF; - ri.last = ev::ev_now (); + ri.last = ev_now (); push_front (ri); @@ -246,9 +334,9 @@ erase (i); ri.pcnt = ri.pcnt * NRL_ALPHA; - ri.diff = ri.diff * NRL_ALPHA + (ev::ev_now () - ri.last); + ri.diff = ri.diff * NRL_ALPHA + (ev_now () - ri.last); - ri.last = ev::ev_now (); + ri.last = ev_now (); double dif = ri.diff / ri.pcnt; @@ -586,10 +674,10 @@ void connection::connection_established () { + slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); + if (ictx && octx) { - connectmode = conf->connectmode; - // make sure rekeying timeouts are slightly asymmetric ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); rekey.start (rekey_interval, rekey_interval); @@ -600,13 +688,13 @@ { while (tap_packet *p = (tap_packet *)data_queue.get ()) { - send_data_packet (p); + if (p->len) send_data_packet (p); delete p; } while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) { - send_vpn_packet (p, si, IPTOS_RELIABILITY); + if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); delete p; } } @@ -625,7 +713,7 @@ { protocol = best_protocol (THISNODE->protocols & conf->protocols); - // mask out protocols we cannot establish + // mask out endpoints we can't connect to if (!conf->udp_port) protocol &= ~PROT_UDPv4; if (!conf->tcp_port) protocol &= ~PROT_TCPv4; if (!conf->dns_port) protocol &= ~PROT_DNSv4; @@ -656,7 +744,7 @@ return r->si; } else - slog (L_DEBUG, _("%s: node unreachable, no common protocol"), + slog (L_DEBUG, _("%s: node unreachable, no common protocol, no router"), conf->nodename); } @@ -743,7 +831,7 @@ delete r; } -void +inline void connection::establish_connection_cb (ev::timer &w, int revents) { if (!ictx @@ -752,6 +840,16 @@ && connectmode != conf_node::C_DISABLED && !w.is_active ()) { + // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection + // and stop trying. should probably be handled by a per-connection expire handler. + if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) + { + reset_connection (); + return; + } + + last_establish_attempt = ev_now (); + ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 ? (retry_cnt & 3) + 1 : 1 << (retry_cnt >> 2)); @@ -762,12 +860,15 @@ if (si.prot && !si.host) { + slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename); /*TODO*/ /* start the timer so we don't recurse endlessly */ w.start (1); vpn->send_connect_request (conf->id); } else { + slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); + const sockinfo &dsi = forward_si (si); slow = slow || (dsi.prot & PROT_SLOW); @@ -781,7 +882,7 @@ } } - retry_int *= slow ? 8. : 0.7; + retry_int *= slow ? 8. : 0.9; if (retry_int < conf->max_retry) retry_cnt++; @@ -801,8 +902,11 @@ conf->nodename, (const char *)si); if (::conf.script_node_down) - if (!run_script (run_script_cb (this, &connection::script_node_down), false)) - slog (L_WARN, _("node-down command execution failed, continuing.")); + { + run_script_cb *cb = new run_script_cb; + cb->set (this); + run_script_queued (cb, _("node-down command execution failed, continuing.")); + } } delete ictx; ictx = 0; @@ -830,7 +934,7 @@ reset_connection (); } -void +inline void connection::rekey_cb (ev::timer &w, int revents) { reset_connection (); @@ -857,16 +961,24 @@ } void -connection::inject_data_packet (tap_packet *pkt, bool broadcast/*TODO DDD*/) +connection::post_inject_queue () +{ + // force a connection every now and when when packets are sent (max 1/s) + if (ev_now () - last_establish_attempt >= 0.95) // arbitrary + establish_connection.stop (); + + establish_connection (); +} + +void +connection::inject_data_packet (tap_packet *pkt) { if (ictx && octx) send_data_packet (pkt); else { - if (!broadcast) - data_queue.put (new tap_packet (*pkt)); - - establish_connection (); + data_queue.put (new tap_packet (*pkt)); + post_inject_queue (); } } @@ -877,19 +989,21 @@ else { vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); - - establish_connection (); + post_inject_queue (); } } void connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) { - last_activity = ev::ev_now (); + last_activity = ev_now (); slog (L_NOISE, "<<%d received packet type %d from %d to %d", conf->id, pkt->typ (), pkt->src (), pkt->dst ()); + if (connectmode == conf_node::C_DISABLED) + return; + switch (pkt->typ ()) { case vpn_packet::PT_PING: @@ -1033,8 +1147,11 @@ p->prot_major, p->prot_minor); if (::conf.script_node_up) - if (!run_script (run_script_cb (this, &connection::script_node_up), false)) - slog (L_WARN, _("node-up command execution failed, continuing.")); + { + run_script_cb *cb = new run_script_cb; + cb->set (this); + run_script_queued (cb, _("node-up command execution failed, continuing.")); + } break; } @@ -1160,14 +1277,15 @@ } } -void connection::keepalive_cb (ev::timer &w, int revents) +inline void +connection::keepalive_cb (ev::timer &w, int revents) { - if (ev::ev_now () >= last_activity + ::conf.keepalive + 30) + if (ev_now () >= last_activity + ::conf.keepalive + 30) { reset_connection (); establish_connection (); } - else if (ev::ev_now () < last_activity + ::conf.keepalive) + else if (ev_now () < last_activity + ::conf.keepalive) w.start (last_activity + ::conf.keepalive - ev::now ()); else if (conf->connectmode != conf_node::C_ONDEMAND || THISNODE->connectmode != conf_node::C_ONDEMAND) @@ -1175,7 +1293,7 @@ send_ping (si); w.start (5); } - else if (ev::ev_now () < last_activity + ::conf.keepalive + 10) + else if (ev_now () < last_activity + ::conf.keepalive + 10) // hold ondemand connections implicitly a few seconds longer // should delete octx, though, or something like that ;) w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); @@ -1215,7 +1333,8 @@ asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); } -const char *connection::script_node_up () +inline const char * +connection::script_node_up () { script_init_connect_env (); @@ -1230,7 +1349,8 @@ return filename; } -const char *connection::script_node_down () +inline const char * +connection::script_node_down () { script_init_connect_env (); @@ -1246,21 +1366,29 @@ } 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) +: vpn(vpn), conf(conf), #if ENABLE_DNS -, dns (0) + dns (0), #endif + data_queue(conf->max_ttl, conf->max_queue + 1), + vpn_queue(conf->max_ttl, conf->max_queue + 1) { + rekey .set (this); + keepalive .set (this); + establish_connection.set (this); + + last_establish_attempt = 0.; 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 + connectmode = conf->connectmode; + + // queue a dummy packet to force an initial connection attempt + if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) + vpn_queue.put (new net_packet); + reset_connection (); }