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.10 by pcg, Thu Oct 16 02:41:21 2003 UTC vs.
Revision 1.27 by root, Tue Feb 8 23:11:36 2011 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 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 }
68} tcp_si; 82};
69 83
84static tcp_si_map tcp_si;
85
70struct tcp_connection : io_watcher { 86struct tcp_connection : ev::io
87{
88 int tos;
71 tstamp last_activity; 89 tstamp last_activity;
72 const sockinfo si; 90 const sockinfo si;
73 vpn &v; 91 vpn &v;
74 bool active; // this connection has been actively established 92 bool active; // this connection has been actively established
75 enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; 93 enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state;
83#if ENABLE_HTTP_PROXY 101#if ENABLE_HTTP_PROXY
84 char *proxy_req; 102 char *proxy_req;
85 int proxy_req_len; 103 int proxy_req_len;
86#endif 104#endif
87 105
88 void tcpv4_ev (io_watcher &w, short revents); 106 inline void tcpv4_ev (ev::io &w, int revents);
89 107
90 bool send_packet (vpn_packet *pkt, int tos); 108 bool send_packet (vpn_packet *pkt, int tos);
91 bool write_packet (); 109 bool write_packet ();
92 110
93 void error (); // abort conenction && cleanup 111 void error (); // abort conenction && cleanup
94 112
95 operator tcp_si_map::value_type() 113 operator tcp_si_map::value_type()
96 { 114 {
97 return tcp_si_map::value_type (&si, this); 115 return tcp_si_map::value_type (&si, this);
98 } 116 }
99 117
100 tcp_connection (int fd_, const sockinfo &si_, vpn &v_); 118 tcp_connection (int fd_, const sockinfo &si_, vpn &v_);
101 ~tcp_connection (); 119 ~tcp_connection ();
102}; 120};
103 121
104void tcp_si_map::cleaner_cb (time_watcher &w) 122void
123tcp_si_map::cleaner_cb (ev::timer &w, int revents)
105{ 124{
106 w.at = NOW + 600;
107 tstamp to = NOW - ::conf.keepalive - 30 - 60; 125 tstamp to = ev_now () - ::conf.keepalive - 30 - 60;
108 126
109 for (iterator i = begin (); i != end(); ) 127 for (iterator i = begin (); i != end(); )
110 if (i->second->last_activity >= to) 128 if (i->second->last_activity >= to)
111 ++i; 129 ++i;
112 else 130 else
113 { 131 {
132 delete i->second;
114 erase (i); 133 erase (i);
115 i = begin (); 134 i = begin ();
116 } 135 }
117} 136}
118 137
119void 138void
120vpn::tcpv4_ev (io_watcher &w, short revents) 139vpn::tcpv4_ev (ev::io &w, int revents)
121{ 140{
122 if (revents & (POLLIN | POLLERR)) 141 if (revents & EV_READ)
123 { 142 {
124 struct sockaddr_in sa; 143 struct sockaddr_in sa;
125 socklen_t sa_len = sizeof (sa); 144 socklen_t sa_len = sizeof (sa);
126 int len; 145 int len;
127 146
128 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); 147 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len);
129 148
130 if (fd >= 0) 149 if (fd >= 0)
131 { 150 {
151 fcntl (fd, F_SETFL, O_NONBLOCK);
152 fcntl (fd, F_SETFD, FD_CLOEXEC);
153
132 sockinfo si(sa, PROT_TCPv4); 154 sockinfo si(sa, PROT_TCPv4);
133 155
134 slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D 156 slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D
135
136 fcntl (fd, F_SETFL, O_NONBLOCK);
137 157
138 tcp_connection *i = new tcp_connection (fd, si, *this); 158 tcp_connection *i = new tcp_connection (fd, si, *this);
139 tcp_si.insert (*i); 159 tcp_si.insert (*i);
140 } 160 }
141 } 161 }
195 return false; 215 return false;
196 } 216 }
197} 217}
198 218
199void 219void
200tcp_connection::tcpv4_ev (io_watcher &w, short revents) 220tcp_connection::tcpv4_ev (ev::io &w, int revents)
201{ 221{
202 last_activity = NOW; 222 last_activity = ev_now ();
203 223
204 if (revents & (POLLERR | POLLHUP)) 224 if (revents & EV_WRITE)
205 {
206 error ();
207 return;
208 }
209
210 if (revents & POLLOUT)
211 { 225 {
212 if (state == CONNECTING) 226 if (state == CONNECTING)
213 { 227 {
214 state = ESTABLISHED; 228 state = ESTABLISHED;
215 set (POLLIN); 229 set (EV_READ);
216#if ENABLE_HTTP_PROXY 230#if ENABLE_HTTP_PROXY
217 if (::conf.proxy_host && ::conf.proxy_port) 231 if (::conf.proxy_host && ::conf.proxy_port)
218 { 232 {
219 state = CONNECTING_PROXY; 233 state = CONNECTING_PROXY;
234
220 write (fd, proxy_req, proxy_req_len); 235 if (write (fd, proxy_req, proxy_req_len) == 0)
236 {
237 error ();
238 return;
239 }
240
221 free (proxy_req); proxy_req = 0; 241 free (proxy_req); proxy_req = 0;
222 } 242 }
223#endif 243#endif
224 } 244 }
225 else if (state == ESTABLISHED) 245 else if (state == ESTABLISHED)
228 { 248 {
229 if (write_packet ()) 249 if (write_packet ())
230 { 250 {
231 delete w_pkt; w_pkt = 0; 251 delete w_pkt; w_pkt = 0;
232 252
233 set (POLLIN); 253 set (EV_READ);
234 } 254 }
235 } 255 }
236 else 256 else
237 set (POLLIN); 257 set (EV_READ);
238 } 258 }
239 else 259 else
240 set (POLLIN); 260 set (EV_READ);
241 } 261 }
242 262
243 if (revents & POLLIN) 263 if (revents & EV_READ)
244 { 264 {
245 if (state == ESTABLISHED) 265 if (state == ESTABLISHED)
246 for (;;) 266 for (;;)
247 { 267 {
248 if (!r_pkt) 268 if (!r_pkt)
282 break; 302 break;
283 } 303 }
284 else if (len < 0 && (errno == EINTR || errno == EAGAIN)) 304 else if (len < 0 && (errno == EINTR || errno == EAGAIN))
285 break; 305 break;
286 306
307 // len == 0 <-> EOF
287 error (); 308 error ();
288 break; 309 break;
289 } 310 }
290#if ENABLE_HTTP_PROXY 311#if ENABLE_HTTP_PROXY
291 else if (state == CONNECTING_PROXY) 312 else if (state == CONNECTING_PROXY)
341} 362}
342 363
343bool 364bool
344tcp_connection::send_packet (vpn_packet *pkt, int tos) 365tcp_connection::send_packet (vpn_packet *pkt, int tos)
345{ 366{
346 last_activity = NOW; 367 last_activity = ev_now ();
347 368
348 if (state == IDLE) 369 if (state == IDLE)
349 { 370 {
350 // woaw, the first lost packet ;) 371 // woaw, the first lost packet ;)
351 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 372 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
385 fcntl (fd, F_SETFL, O_NONBLOCK); 406 fcntl (fd, F_SETFL, O_NONBLOCK);
386 407
387 if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 408 if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0
388 || errno == EINPROGRESS) 409 || errno == EINPROGRESS)
389 { 410 {
411 fcntl (fd, F_SETFL, O_NONBLOCK);
412 fcntl (fd, F_SETFD, FD_CLOEXEC);
413
390 state = CONNECTING; 414 state = CONNECTING;
391 start (fd, POLLOUT); 415 start (fd, EV_WRITE);
392 } 416 }
393 else 417 else
394 close (fd); 418 close (fd);
395 } 419 }
396 } 420 }
401 if (!w_pkt) 425 if (!w_pkt)
402 { 426 {
403 // how this maps to the underlying tcp packets we don't know 427 // how this maps to the underlying tcp packets we don't know
404 // and we don't care. at least we tried ;) 428 // and we don't care. at least we tried ;)
405#if defined(SOL_IP) && defined(IP_TOS) 429#if defined(SOL_IP) && defined(IP_TOS)
430 if (tos != this->tos)
431 {
432 this->tos = tos;
406 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); 433 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
434 }
407#endif 435#endif
408 436
409 w_pkt = pkt; 437 w_pkt = pkt;
410 w_ofs = 0; 438 w_ofs = 0;
411 w_len = pkt->len + 2; // length + size header 439 w_len = pkt->len + 2; // length + size header
415 else 443 else
416 { 444 {
417 w_pkt = new vpn_packet; 445 w_pkt = new vpn_packet;
418 w_pkt->set (*pkt); 446 w_pkt->set (*pkt);
419 447
420 set (POLLIN | POLLOUT); 448 set (EV_READ | EV_WRITE);
421 } 449 }
422 } 450 }
423 } 451 }
424 452
425 return state != ERROR; 453 return state != ERROR;
426} 454}
427 455
456void
428void tcp_connection::error () 457tcp_connection::error ()
429{ 458{
459 stop ();
460
430 if (fd >= 0) 461 if (fd >= 0)
431 { 462 {
432 close (fd); 463 close (fd);
464 tos = -1;
433 fd = -1; 465 fd = -1;
434 } 466 }
435 467
436 delete r_pkt; r_pkt = 0; 468 delete r_pkt; r_pkt = 0;
437 delete w_pkt; w_pkt = 0; 469 delete w_pkt; w_pkt = 0;
438#if ENABLE_HTTP_PROXY 470#if ENABLE_HTTP_PROXY
439 free (proxy_req); proxy_req = 0; 471 free (proxy_req); proxy_req = 0;
440#endif 472#endif
441 473
442 stop ();
443 state = active ? IDLE : ERROR; 474 state = active ? IDLE : ERROR;
444} 475}
445 476
446tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) 477tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_)
447: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) 478: v(v_), si(si_)
448{ 479{
480 set<tcp_connection, &tcp_connection::tcpv4_ev> (this);
481
449 last_activity = NOW; 482 last_activity = ev_now ();
450 r_pkt = 0; 483 r_pkt = 0;
451 w_pkt = 0; 484 w_pkt = 0;
485 tos = -1;
452 fd = fd_; 486 fd = fd_;
453#if ENABLE_HTTP_PROXY 487#if ENABLE_HTTP_PROXY
454 proxy_req = 0; 488 proxy_req = 0;
455#endif 489#endif
456 490
461 } 495 }
462 else 496 else
463 { 497 {
464 active = false; 498 active = false;
465 state = ESTABLISHED; 499 state = ESTABLISHED;
466 start (fd, POLLIN); 500 start (fd, EV_READ);
467 } 501 }
468} 502}
469 503
470tcp_connection::~tcp_connection () 504tcp_connection::~tcp_connection ()
471{ 505{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines