--- gvpe/src/connection.C 2005/06/03 05:07:31 1.56 +++ gvpe/src/connection.C 2008/08/07 16:34:21 1.67 @@ -21,8 +21,6 @@ #include "config.h" -#include - #include #include @@ -86,7 +84,8 @@ EVP_MD_CTX_cleanup (&ctx); } -struct rsa_entry { +struct rsa_entry +{ tstamp expire; rsaid id; rsachallenge chg; @@ -94,13 +93,13 @@ struct rsa_cache : list { - void cleaner_cb (time_watcher &w); time_watcher 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 > NOW) + if (!memcmp (&id, &i->id, sizeof id) && i->expire > ev_now ()) { memcpy (&chg, &i->chg, sizeof chg); @@ -109,8 +108,8 @@ } } - if (cleaner.at < NOW) - cleaner.start (NOW + RSA_TTL); + if (!cleaner.is_active ()) + cleaner.again (); return false; } @@ -122,30 +121,32 @@ RAND_bytes ((unsigned char *)&id, sizeof id); RAND_bytes ((unsigned char *)&chg, sizeof chg); - e.expire = NOW + RSA_TTL; + e.expire = ev_now () + RSA_TTL; e.id = id; memcpy (&e.chg, &chg, sizeof chg); push_back (e); - if (cleaner.at < NOW) - cleaner.start (NOW + RSA_TTL); + if (!cleaner.is_active ()) + cleaner.again (); } rsa_cache () - : cleaner (this, &rsa_cache::cleaner_cb) - { } + { + cleaner.set (this); + cleaner.set (RSA_TTL, RSA_TTL); + } } rsa_cache; -void rsa_cache::cleaner_cb (time_watcher &w) +void rsa_cache::cleaner_cb (ev::timer &w, int revents) { - if (!empty ()) + if (empty ()) + w.stop (); + else { - w.start (NOW + RSA_TTL); - for (iterator i = begin (); i != end (); ) - if (i->expire <= NOW) + if (i->expire <= ev_now ()) i = erase (i); else ++i; @@ -154,46 +155,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 () { - net_packet *p = queue[j]; + while (net_packet *p = get ()) + delete p; - if (p) + delete [] queue; +} + +void pkt_queue::expire_cb (ev::timer &w, int revents) +{ + ev_tstamp expire = ev_now () - max_ttl; + + 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 +257,7 @@ for (i = begin (); i != end (); ) if (i->host == host) break; - else if (i->last < NOW - NRL_EXPIRE) + else if (i->last < ev_now () - NRL_EXPIRE) i = erase (i); else i++; @@ -234,7 +269,7 @@ ri.host = host; ri.pcnt = 1.; ri.diff = NRL_MAXDIF; - ri.last = NOW; + ri.last = ev_now (); push_front (ri); @@ -246,9 +281,9 @@ erase (i); ri.pcnt = ri.pcnt * NRL_ALPHA; - ri.diff = ri.diff * NRL_ALPHA + (NOW - ri.last); + ri.diff = ri.diff * NRL_ALPHA + (ev_now () - ri.last); - ri.last = NOW; + ri.last = ev_now (); double dif = ri.diff / ri.pcnt; @@ -468,6 +503,9 @@ #if ENABLE_ROHC f |= FEATURE_ROHC; #endif +#if ENABLE_BRIDGING + f |= FEATURE_BRIDGING; +#endif return f; } }; @@ -583,27 +621,27 @@ 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 - rekey.start (NOW + ::conf.rekey - + (conf->id > THISNODE->id ? 10 : 0)); - keepalive.start (NOW + ::conf.keepalive); + ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); + rekey.start (rekey_interval, rekey_interval); + keepalive.start (::conf.keepalive); // send queued packets if (ictx && octx) { 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; } } @@ -611,7 +649,7 @@ else { retry_cnt = 0; - establish_connection.start (NOW + 5); + establish_connection.start (5); keepalive.stop (); rekey.stop (); } @@ -627,6 +665,14 @@ if (!conf->tcp_port) protocol &= ~PROT_TCPv4; if (!conf->dns_port) protocol &= ~PROT_DNSv4; + if (protocol + && (!conf->can_direct (THISNODE) + || !THISNODE->can_direct (conf))) + { + slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); + protocol = 0; + } + si.set (conf, protocol); } @@ -721,7 +767,7 @@ void connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) { - slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", + slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", conf->id, rid, (const char *)rsi); connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); @@ -732,29 +778,42 @@ delete r; } -void -connection::establish_connection_cb (time_watcher &w) +inline void +connection::establish_connection_cb (ev::timer &w, int revents) { if (!ictx && conf != THISNODE && connectmode != conf_node::C_NEVER && connectmode != conf_node::C_DISABLED - && NOW > w.at) + && !w.is_active ()) { - w.at = TSTAMP_MAX; // first disable this watcher in case of recursion + // 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; + } - double retry_int = double (retry_cnt & 3 - ? (retry_cnt & 3) + 1 - : 1 << (retry_cnt >> 2)); + ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 + ? (retry_cnt & 3) + 1 + : 1 << (retry_cnt >> 2)); reset_si (); bool slow = si.prot & PROT_SLOW; if (si.prot && !si.host) - vpn->send_connect_request (conf->id); + { + 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); @@ -775,7 +834,7 @@ else retry_int = conf->max_retry; - w.start (NOW + retry_int); + w.start (retry_int); } } @@ -788,8 +847,12 @@ 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; + cb.set (this); + if (!run_script (cb, false)) + slog (L_WARN, _("node-down command execution failed, continuing.")); + } } delete ictx; ictx = 0; @@ -817,8 +880,8 @@ reset_connection (); } -void -connection::rekey_cb (time_watcher &w) +inline void +connection::rekey_cb (ev::timer &w, int revents) { reset_connection (); establish_connection (); @@ -872,11 +935,14 @@ void connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) { - last_activity = 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: @@ -1020,8 +1086,12 @@ 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; + cb.set (this); + if (!run_script (cb, false)) + slog (L_WARN, _("node-up command execution failed, continuing.")); + } break; } @@ -1086,22 +1156,28 @@ { connect_req_packet *p = (connect_req_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]; - conf->protocols = p->protocols; + if (p->id > 0 && p->id <= vpn->conns.size ()) + { + connection *c = vpn->conns[p->id - 1]; + conf->protocols = p->protocols; - slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", - conf->id, p->id, c->ictx && c->octx); + slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", + conf->id, p->id, c->ictx && c->octx); - if (c->ictx && c->octx) - { - // send connect_info packets to both sides, in case one is - // behind a nat firewall (or both ;) - c->send_connect_info (conf->id, si, conf->protocols); - send_connect_info (c->conf->id, c->si, c->conf->protocols); + if (c->ictx && c->octx) + { + // send connect_info packets to both sides, in case one is + // behind a nat firewall (or both ;) + c->send_connect_info (conf->id, si, conf->protocols); + send_connect_info (c->conf->id, c->si, c->conf->protocols); + } + else + c->establish_connection (); } else - c->establish_connection (); + slog (L_WARN, + _("received authenticated connection request from unknown node #%d, config file mismatch?"), + p->id); } break; @@ -1111,7 +1187,7 @@ { connect_info_packet *p = (connect_info_packet *)pkt; - if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything + if (p->id > 0 && p->id <= vpn->conns.size ()) { connection *c = vpn->conns[p->id - 1]; @@ -1127,6 +1203,10 @@ if (dsi.valid ()) c->send_auth_request (dsi, true); } + else + slog (L_WARN, + _("received authenticated connection request from unknown node #%d, config file mismatch?"), + p->id); } break; @@ -1137,25 +1217,26 @@ } } -void connection::keepalive_cb (time_watcher &w) +inline void +connection::keepalive_cb (ev::timer &w, int revents) { - if (NOW >= last_activity + ::conf.keepalive + 30) + if (ev_now () >= last_activity + ::conf.keepalive + 30) { reset_connection (); establish_connection (); } - else if (NOW < last_activity + ::conf.keepalive) - w.start (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) { send_ping (si); - w.start (NOW + 5); + w.start (5); } - else if (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); + w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); else reset_connection (); } @@ -1192,11 +1273,12 @@ 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 (); - putenv ("STATE=up"); + putenv ((char *)"STATE=up"); char *filename; asprintf (&filename, @@ -1207,11 +1289,12 @@ return filename; } -const char *connection::script_node_down () +inline const char * +connection::script_node_down () { script_init_connect_env (); - putenv ("STATE=down"); + putenv ((char *)"STATE=down"); char *filename; asprintf (&filename, @@ -1223,21 +1306,33 @@ } 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), + vpn_queue(conf->max_ttl, conf->max_queue) { + rekey .set (this); + keepalive .set (this); + establish_connection.set (this); + 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) + { + net_packet *p = new net_packet; + p->len = 0; + vpn_queue.put (p); + } + reset_connection (); }