--- gvpe/src/vpn_tcp.C 2005/07/09 02:43:19 1.15 +++ gvpe/src/vpn_tcp.C 2007/12/02 00:39:06 1.21 @@ -1,6 +1,6 @@ /* vpn_tcp.C -- handle the tcp part of the protocol. - Copyright (C) 2003-2005 Marc Lehmann + Copyright (C) 2003-2007 Marc Lehmann This file is part of GVPE. @@ -23,10 +23,10 @@ #if ENABLE_TCP -// tcp processing is extremely ugly, since the vpe protocol is simply +// tcp processing is extremely ugly, since the gvpe protocol is simply // designed for unreliable datagram networks. tcp is implemented by // multiplexing packets over tcp. errors are completely ignored, as we -// rely on the higher level protocol to time out and reconnect. +// rely on the higher level layers to time out and reconnect. #include @@ -60,15 +60,18 @@ }; struct tcp_si_map : public map { - void cleaner_cb (time_watcher &w); time_watcher cleaner; + void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; tcp_si_map () : cleaner(this, &tcp_si_map::cleaner_cb) - { } + { + cleaner.start (::conf.keepalive / 2, ::conf.keepalive / 2); + } } tcp_si; -struct tcp_connection : io_watcher { +struct tcp_connection : ev::io { + int tos; tstamp last_activity; const sockinfo si; vpn &v; @@ -86,7 +89,7 @@ int proxy_req_len; #endif - void tcpv4_ev (io_watcher &w, short revents); + void tcpv4_ev (ev::io &w, int revents); bool send_packet (vpn_packet *pkt, int tos); bool write_packet (); @@ -102,26 +105,25 @@ ~tcp_connection (); }; -void tcp_si_map::cleaner_cb (time_watcher &w) +void tcp_si_map::cleaner_cb (ev::timer &w, int revents) { - w.start (NOW + 600); - - tstamp to = NOW - ::conf.keepalive - 30 - 60; + tstamp to = ev_now () - ::conf.keepalive - 30 - 60; for (iterator i = begin (); i != end(); ) if (i->second->last_activity >= to) ++i; else { + delete i->second; erase (i); i = begin (); } } void -vpn::tcpv4_ev (io_watcher &w, short revents) +vpn::tcpv4_ev (ev::io &w, int revents) { - if (revents & EVENT_READ) + if (revents & EV_READ) { struct sockaddr_in sa; socklen_t sa_len = sizeof (sa); @@ -131,13 +133,13 @@ if (fd >= 0) { + fcntl (fd, F_SETFL, O_NONBLOCK); + fcntl (fd, F_SETFD, FD_CLOEXEC); + sockinfo si(sa, PROT_TCPv4); slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D - fcntl (fd, F_SETFL, O_NONBLOCK); - fcntl (fd, F_SETFD, FD_CLOEXEC); - tcp_connection *i = new tcp_connection (fd, si, *this); tcp_si.insert (*i); } @@ -200,16 +202,16 @@ } void -tcp_connection::tcpv4_ev (io_watcher &w, short revents) +tcp_connection::tcpv4_ev (ev::io &w, int revents) { - last_activity = NOW; + last_activity = ev_now (); - if (revents & EVENT_WRITE) + if (revents & EV_WRITE) { if (state == CONNECTING) { state = ESTABLISHED; - set (EVENT_READ); + set (EV_READ); #if ENABLE_HTTP_PROXY if (::conf.proxy_host && ::conf.proxy_port) { @@ -233,17 +235,17 @@ { delete w_pkt; w_pkt = 0; - set (EVENT_READ); + set (EV_READ); } } else - set (EVENT_READ); + set (EV_READ); } else - set (EVENT_READ); + set (EV_READ); } - if (revents & EVENT_READ) + if (revents & EV_READ) { if (state == ESTABLISHED) for (;;) @@ -347,7 +349,7 @@ bool tcp_connection::send_packet (vpn_packet *pkt, int tos) { - last_activity = NOW; + last_activity = ev_now (); if (state == IDLE) { @@ -391,8 +393,11 @@ if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 || errno == EINPROGRESS) { + fcntl (fd, F_SETFL, O_NONBLOCK); + fcntl (fd, F_SETFD, FD_CLOEXEC); + state = CONNECTING; - start (fd, EVENT_WRITE); + start (fd, EV_WRITE); } else close (fd); @@ -407,7 +412,11 @@ // how this maps to the underlying tcp packets we don't know // and we don't care. at least we tried ;) #if defined(SOL_IP) && defined(IP_TOS) - setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); + if (tos != this->tos) + { + this->tos = tos; + setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); + } #endif w_pkt = pkt; @@ -421,7 +430,7 @@ w_pkt = new vpn_packet; w_pkt->set (*pkt); - set (EVENT_READ | EVENT_WRITE); + set (EV_READ | EV_WRITE); } } } @@ -431,10 +440,13 @@ void tcp_connection::error () { + stop (); + if (fd >= 0) { close (fd); - fd = -1; + tos = -1; + fd = -1; } delete r_pkt; r_pkt = 0; @@ -443,19 +455,16 @@ free (proxy_req); proxy_req = 0; #endif - stop (); state = active ? IDLE : ERROR; } tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) -: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) +: v(v_), si(si_), ev::io(this, &tcp_connection::tcpv4_ev) { - if (!tcp_si.cleaner.active) - tcp_si.cleaner.start (0); - - last_activity = NOW; + last_activity = ev_now (); r_pkt = 0; w_pkt = 0; + tos = -1; fd = fd_; #if ENABLE_HTTP_PROXY proxy_req = 0; @@ -470,7 +479,7 @@ { active = false; state = ESTABLISHED; - start (fd, EVENT_READ); + start (fd, EV_READ); } }