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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines