ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_tcp.C
(Generate patch)

Comparing gvpe/src/vpn_tcp.C (file contents):
Revision 1.11 by pcg, Sat Jan 17 01:18:36 2004 UTC vs.
Revision 1.26 by pcg, Thu Aug 7 17:54:27 2008 UTC

1/* 1/*
2 vpn_tcp.C -- handle the tcp part of the protocol. 2 vpn_tcp.C -- handle the tcp part of the protocol.
3 Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> 3 Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE.
6
5 This program is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify it
6 it under the terms of the GNU General Public License as published by 8 under the terms of the GNU General Public License as published by the
7 the Free Software Foundation; either version 2 of the License, or 9 Free Software Foundation; either version 3 of the License, or (at your
8 (at your option) any later version. 10 option) any later version.
9 11
10 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful, but
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 GNU General Public License for more details. 15 Public License for more details.
14 16
15 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License along
16 along with this program; if not, write to the Free Software 18 with this program; if not, see <http://www.gnu.org/licenses/>.
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19
20 Additional permission under GNU GPL version 3 section 7
21
22 If you modify this Program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a modified
24 version of that library), containing parts covered by the terms of the
25 OpenSSL or SSLeay licenses, the licensors of this Program grant you
26 additional permission to convey the resulting work. Corresponding
27 Source for a non-source form of such a combination shall include the
28 source code for the parts of OpenSSL used as well as that of the
29 covered work.
18*/ 30*/
19 31
20#include "config.h" 32#include "config.h"
21 33
22#if ENABLE_TCP 34#if ENABLE_TCP
23 35
24// tcp processing is extremely ugly, since the vpe protocol is simply 36// tcp processing is extremely ugly, since the gvpe protocol is simply
25// designed for unreliable datagram networks. tcp is implemented by 37// designed for unreliable datagram networks. tcp is implemented by
26// multiplexing packets over tcp. errors are completely ignored, as we 38// multiplexing packets over tcp. errors are completely ignored, as we
27// rely on the higher level protocol to time out and reconnect. 39// rely on the higher level layers to time out and reconnect.
28 40
29#include <cstring> 41#include <cstring>
30 42
31#include <sys/types.h> 43#include <sys/types.h>
32#include <sys/socket.h> 44#include <sys/socket.h>
55 { 67 {
56 return *a < *b; 68 return *a < *b;
57 } 69 }
58}; 70};
59 71
60struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo> { 72struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo>
61 void cleaner_cb (time_watcher &w); time_watcher cleaner; 73{
74 inline void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner;
62 75
63 tcp_si_map () 76 tcp_si_map ()
64 : cleaner(this, &tcp_si_map::cleaner_cb)
65 { 77 {
66 cleaner.start (0); 78 ev_default_loop (0);
79 cleaner.set<tcp_si_map, &tcp_si_map::cleaner_cb> (this);
80 cleaner.start (::conf.keepalive / 2, ::conf.keepalive / 2);
67 } 81 }
82
68} tcp_si; 83} tcp_si;
69 84
70struct tcp_connection : io_watcher { 85struct tcp_connection : ev::io
86{
87 int tos;
71 tstamp last_activity; 88 tstamp last_activity;
72 const sockinfo si; 89 const sockinfo si;
73 vpn &v; 90 vpn &v;
74 bool active; // this connection has been actively established 91 bool active; // this connection has been actively established
75 enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; 92 enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state;
83#if ENABLE_HTTP_PROXY 100#if ENABLE_HTTP_PROXY
84 char *proxy_req; 101 char *proxy_req;
85 int proxy_req_len; 102 int proxy_req_len;
86#endif 103#endif
87 104
88 void tcpv4_ev (io_watcher &w, short revents); 105 inline void tcpv4_ev (ev::io &w, int revents);
89 106
90 bool send_packet (vpn_packet *pkt, int tos); 107 bool send_packet (vpn_packet *pkt, int tos);
91 bool write_packet (); 108 bool write_packet ();
92 109
93 void error (); // abort conenction && cleanup 110 void error (); // abort conenction && cleanup
94 111
95 operator tcp_si_map::value_type() 112 operator tcp_si_map::value_type()
96 { 113 {
97 return tcp_si_map::value_type (&si, this); 114 return tcp_si_map::value_type (&si, this);
98 } 115 }
99 116
100 tcp_connection (int fd_, const sockinfo &si_, vpn &v_); 117 tcp_connection (int fd_, const sockinfo &si_, vpn &v_);
101 ~tcp_connection (); 118 ~tcp_connection ();
102}; 119};
103 120
104void tcp_si_map::cleaner_cb (time_watcher &w) 121void tcp_si_map::cleaner_cb (ev::timer &w, int revents)
105{ 122{
106 w.start (NOW + 600);
107
108 tstamp to = NOW - ::conf.keepalive - 30 - 60; 123 tstamp to = ev_now () - ::conf.keepalive - 30 - 60;
109 124
110 for (iterator i = begin (); i != end(); ) 125 for (iterator i = begin (); i != end(); )
111 if (i->second->last_activity >= to) 126 if (i->second->last_activity >= to)
112 ++i; 127 ++i;
113 else 128 else
114 { 129 {
130 delete i->second;
115 erase (i); 131 erase (i);
116 i = begin (); 132 i = begin ();
117 } 133 }
118} 134}
119 135
120void 136void
121vpn::tcpv4_ev (io_watcher &w, short revents) 137vpn::tcpv4_ev (ev::io &w, int revents)
122{ 138{
123 if (revents & EVENT_READ) 139 if (revents & EV_READ)
124 { 140 {
125 struct sockaddr_in sa; 141 struct sockaddr_in sa;
126 socklen_t sa_len = sizeof (sa); 142 socklen_t sa_len = sizeof (sa);
127 int len; 143 int len;
128 144
129 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); 145 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len);
130 146
131 if (fd >= 0) 147 if (fd >= 0)
132 { 148 {
149 fcntl (fd, F_SETFL, O_NONBLOCK);
150 fcntl (fd, F_SETFD, FD_CLOEXEC);
151
133 sockinfo si(sa, PROT_TCPv4); 152 sockinfo si(sa, PROT_TCPv4);
134 153
135 slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D 154 slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D
136
137 fcntl (fd, F_SETFL, O_NONBLOCK);
138 155
139 tcp_connection *i = new tcp_connection (fd, si, *this); 156 tcp_connection *i = new tcp_connection (fd, si, *this);
140 tcp_si.insert (*i); 157 tcp_si.insert (*i);
141 } 158 }
142 } 159 }
196 return false; 213 return false;
197 } 214 }
198} 215}
199 216
200void 217void
201tcp_connection::tcpv4_ev (io_watcher &w, short revents) 218tcp_connection::tcpv4_ev (ev::io &w, int revents)
202{ 219{
203 last_activity = NOW; 220 last_activity = ev_now ();
204 221
205 if (revents & EVENT_WRITE) 222 if (revents & EV_WRITE)
206 { 223 {
207 if (state == CONNECTING) 224 if (state == CONNECTING)
208 { 225 {
209 state = ESTABLISHED; 226 state = ESTABLISHED;
210 set (EVENT_READ); 227 set (EV_READ);
211#if ENABLE_HTTP_PROXY 228#if ENABLE_HTTP_PROXY
212 if (::conf.proxy_host && ::conf.proxy_port) 229 if (::conf.proxy_host && ::conf.proxy_port)
213 { 230 {
214 state = CONNECTING_PROXY; 231 state = CONNECTING_PROXY;
215 232
229 { 246 {
230 if (write_packet ()) 247 if (write_packet ())
231 { 248 {
232 delete w_pkt; w_pkt = 0; 249 delete w_pkt; w_pkt = 0;
233 250
234 set (EVENT_READ); 251 set (EV_READ);
235 } 252 }
236 } 253 }
237 else 254 else
238 set (EVENT_READ); 255 set (EV_READ);
239 } 256 }
240 else 257 else
241 set (EVENT_READ); 258 set (EV_READ);
242 } 259 }
243 260
244 if (revents & EVENT_READ) 261 if (revents & EV_READ)
245 { 262 {
246 if (state == ESTABLISHED) 263 if (state == ESTABLISHED)
247 for (;;) 264 for (;;)
248 { 265 {
249 if (!r_pkt) 266 if (!r_pkt)
343} 360}
344 361
345bool 362bool
346tcp_connection::send_packet (vpn_packet *pkt, int tos) 363tcp_connection::send_packet (vpn_packet *pkt, int tos)
347{ 364{
348 last_activity = NOW; 365 last_activity = ev_now ();
349 366
350 if (state == IDLE) 367 if (state == IDLE)
351 { 368 {
352 // woaw, the first lost packet ;) 369 // woaw, the first lost packet ;)
353 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 370 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
387 fcntl (fd, F_SETFL, O_NONBLOCK); 404 fcntl (fd, F_SETFL, O_NONBLOCK);
388 405
389 if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 406 if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0
390 || errno == EINPROGRESS) 407 || errno == EINPROGRESS)
391 { 408 {
409 fcntl (fd, F_SETFL, O_NONBLOCK);
410 fcntl (fd, F_SETFD, FD_CLOEXEC);
411
392 state = CONNECTING; 412 state = CONNECTING;
393 start (fd, EVENT_WRITE); 413 start (fd, EV_WRITE);
394 } 414 }
395 else 415 else
396 close (fd); 416 close (fd);
397 } 417 }
398 } 418 }
403 if (!w_pkt) 423 if (!w_pkt)
404 { 424 {
405 // how this maps to the underlying tcp packets we don't know 425 // how this maps to the underlying tcp packets we don't know
406 // and we don't care. at least we tried ;) 426 // and we don't care. at least we tried ;)
407#if defined(SOL_IP) && defined(IP_TOS) 427#if defined(SOL_IP) && defined(IP_TOS)
428 if (tos != this->tos)
429 {
430 this->tos = tos;
408 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); 431 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
432 }
409#endif 433#endif
410 434
411 w_pkt = pkt; 435 w_pkt = pkt;
412 w_ofs = 0; 436 w_ofs = 0;
413 w_len = pkt->len + 2; // length + size header 437 w_len = pkt->len + 2; // length + size header
417 else 441 else
418 { 442 {
419 w_pkt = new vpn_packet; 443 w_pkt = new vpn_packet;
420 w_pkt->set (*pkt); 444 w_pkt->set (*pkt);
421 445
422 set (EVENT_READ | EVENT_WRITE); 446 set (EV_READ | EV_WRITE);
423 } 447 }
424 } 448 }
425 } 449 }
426 450
427 return state != ERROR; 451 return state != ERROR;
428} 452}
429 453
430void tcp_connection::error () 454void tcp_connection::error ()
431{ 455{
456 stop ();
457
432 if (fd >= 0) 458 if (fd >= 0)
433 { 459 {
434 close (fd); 460 close (fd);
461 tos = -1;
435 fd = -1; 462 fd = -1;
436 } 463 }
437 464
438 delete r_pkt; r_pkt = 0; 465 delete r_pkt; r_pkt = 0;
439 delete w_pkt; w_pkt = 0; 466 delete w_pkt; w_pkt = 0;
440#if ENABLE_HTTP_PROXY 467#if ENABLE_HTTP_PROXY
441 free (proxy_req); proxy_req = 0; 468 free (proxy_req); proxy_req = 0;
442#endif 469#endif
443 470
444 stop ();
445 state = active ? IDLE : ERROR; 471 state = active ? IDLE : ERROR;
446} 472}
447 473
448tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) 474tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_)
449: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) 475: v(v_), si(si_)
450{ 476{
477 set<tcp_connection, &tcp_connection::tcpv4_ev> (this);
478
451 last_activity = NOW; 479 last_activity = ev_now ();
452 r_pkt = 0; 480 r_pkt = 0;
453 w_pkt = 0; 481 w_pkt = 0;
482 tos = -1;
454 fd = fd_; 483 fd = fd_;
455#if ENABLE_HTTP_PROXY 484#if ENABLE_HTTP_PROXY
456 proxy_req = 0; 485 proxy_req = 0;
457#endif 486#endif
458 487
463 } 492 }
464 else 493 else
465 { 494 {
466 active = false; 495 active = false;
467 state = ESTABLISHED; 496 state = ESTABLISHED;
468 start (fd, EVENT_READ); 497 start (fd, EV_READ);
469 } 498 }
470} 499}
471 500
472tcp_connection::~tcp_connection () 501tcp_connection::~tcp_connection ()
473{ 502{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines