--- gvpe/src/vpn_tcp.C 2003/10/16 02:41:21 1.10 +++ gvpe/src/vpn_tcp.C 2005/07/09 02:43:19 1.15 @@ -1,8 +1,10 @@ /* vpn_tcp.C -- handle the tcp part of the protocol. - Copyright (C) 2003 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,8 +15,8 @@ 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 - Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + along with gvpe; if not, write to the Free Software + Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "config.h" @@ -61,10 +63,9 @@ void cleaner_cb (time_watcher &w); time_watcher cleaner; tcp_si_map () - : cleaner(this, &tcp_si_map::cleaner_cb) - { - cleaner.start (0); - } + : cleaner(this, &tcp_si_map::cleaner_cb) + { } + } tcp_si; struct tcp_connection : io_watcher { @@ -93,9 +94,9 @@ void error (); // abort conenction && cleanup operator tcp_si_map::value_type() - { - return tcp_si_map::value_type (&si, this); - } + { + return tcp_si_map::value_type (&si, this); + } tcp_connection (int fd_, const sockinfo &si_, vpn &v_); ~tcp_connection (); @@ -103,7 +104,8 @@ void tcp_si_map::cleaner_cb (time_watcher &w) { - w.at = NOW + 600; + w.start (NOW + 600); + tstamp to = NOW - ::conf.keepalive - 30 - 60; for (iterator i = begin (); i != end(); ) @@ -119,7 +121,7 @@ void vpn::tcpv4_ev (io_watcher &w, short revents) { - if (revents & (POLLIN | POLLERR)) + if (revents & EVENT_READ) { struct sockaddr_in sa; socklen_t sa_len = sizeof (sa); @@ -134,6 +136,7 @@ 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); @@ -201,23 +204,23 @@ { last_activity = NOW; - if (revents & (POLLERR | POLLHUP)) - { - error (); - return; - } - - if (revents & POLLOUT) + if (revents & EVENT_WRITE) { if (state == CONNECTING) { state = ESTABLISHED; - set (POLLIN); + set (EVENT_READ); #if ENABLE_HTTP_PROXY if (::conf.proxy_host && ::conf.proxy_port) { state = CONNECTING_PROXY; - write (fd, proxy_req, proxy_req_len); + + if (write (fd, proxy_req, proxy_req_len) == 0) + { + error (); + return; + } + free (proxy_req); proxy_req = 0; } #endif @@ -230,17 +233,17 @@ { delete w_pkt; w_pkt = 0; - set (POLLIN); + set (EVENT_READ); } } else - set (POLLIN); + set (EVENT_READ); } else - set (POLLIN); + set (EVENT_READ); } - if (revents & POLLIN) + if (revents & EVENT_READ) { if (state == ESTABLISHED) for (;;) @@ -284,6 +287,7 @@ else if (len < 0 && (errno == EINTR || errno == EAGAIN)) break; + // len == 0 <-> EOF error (); break; } @@ -388,7 +392,7 @@ || errno == EINPROGRESS) { state = CONNECTING; - start (fd, POLLOUT); + start (fd, EVENT_WRITE); } else close (fd); @@ -417,7 +421,7 @@ w_pkt = new vpn_packet; w_pkt->set (*pkt); - set (POLLIN | POLLOUT); + set (EVENT_READ | EVENT_WRITE); } } } @@ -446,6 +450,9 @@ tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) { + if (!tcp_si.cleaner.active) + tcp_si.cleaner.start (0); + last_activity = NOW; r_pkt = 0; w_pkt = 0; @@ -463,7 +470,7 @@ { active = false; state = ESTABLISHED; - start (fd, POLLIN); + start (fd, EVENT_READ); } }