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.3 by pcg, Sun Apr 6 20:01:53 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-2008 Marc Lehmann <gvpe@schmorp.de>
3 4
5 This file is part of GVPE.
6
4 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
5 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
6 the Free Software Foundation; either version 2 of the License, or 9 Free Software Foundation; either version 3 of the License, or (at your
7 (at your option) any later version. 10 option) any later version.
8 11
9 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
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 GNU General Public License for more details. 15 Public License for more details.
13 16
14 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
15 along with this program; if not, write to the Free Software 18 with this program; if not, see <http://www.gnu.org/licenses/>.
16 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.
17*/ 30*/
18 31
19#include "config.h" 32#include "config.h"
20 33
21#if ENABLE_TCP 34#if ENABLE_TCP
22 35
23// tcp processing is extremely ugly, since the vpe protocol is simply 36// tcp processing is extremely ugly, since the gvpe protocol is simply
24// designed for unreliable datagram networks. tcp is implemented by 37// designed for unreliable datagram networks. tcp is implemented by
25// multiplexing packets over tcp. errors are completely ignored, as we 38// multiplexing packets over tcp. errors are completely ignored, as we
26// rely on the higher level protocol to time out and reconnect. 39// rely on the higher level layers to time out and reconnect.
27 40
28#include <cstring> 41#include <cstring>
29 42
30#include <sys/types.h> 43#include <sys/types.h>
31#include <sys/socket.h> 44#include <sys/socket.h>
32#include <sys/poll.h>
33#include <sys/wait.h> 45#include <sys/wait.h>
34#include <netinet/in.h>
35#include <sys/uio.h> 46#include <sys/uio.h>
36#include <arpa/inet.h>
37#include <errno.h> 47#include <errno.h>
38#include <time.h> 48#include <time.h>
39#include <unistd.h> 49#include <unistd.h>
50#include <fcntl.h>
40 51
41#include <map> 52#include <map>
42#include <unistd.h> 53
43#include <fcntl.h> 54#include "netcompat.h"
44#include <sys/poll.h>
45 55
46#include "vpn.h" 56#include "vpn.h"
57
58#if ENABLE_HTTP_PROXY
59# include "conf.h"
60#endif
47 61
48struct tcp_connection; 62struct tcp_connection;
49 63
50struct lt_sockinfo 64struct lt_sockinfo
51{ 65{
53 { 67 {
54 return *a < *b; 68 return *a < *b;
55 } 69 }
56}; 70};
57 71
58struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo> { 72struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo>
59 void cleaner_cb (time_watcher &w); time_watcher cleaner; 73{
74 inline void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner;
60 75
61 tcp_si_map () 76 tcp_si_map ()
62 : cleaner(this, &tcp_si_map::cleaner_cb)
63 { 77 {
64 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);
65 } 81 }
82
66} tcp_si; 83} tcp_si;
67 84
68struct tcp_connection : io_watcher { 85struct tcp_connection : ev::io
86{
87 int tos;
69 tstamp last_activity; 88 tstamp last_activity;
70 const sockinfo si; 89 const sockinfo si;
71 vpn &v; 90 vpn &v;
72 bool active; // this connection has been actively established 91 bool active; // this connection has been actively established
73 enum { ERROR, IDLE, CONNECTING, ESTABLISHED } state; 92 enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state;
74 93
75 vpn_packet *r_pkt; 94 vpn_packet *r_pkt;
76 u32 r_len, r_ofs; 95 u32 r_len, r_ofs;
77 96
78 vpn_packet *w_pkt; 97 vpn_packet *w_pkt;
79 u32 w_len, w_ofs; 98 u32 w_len, w_ofs;
80 99
81 void tcpv4_ev (io_watcher &w, short revents); 100#if ENABLE_HTTP_PROXY
101 char *proxy_req;
102 int proxy_req_len;
103#endif
104
105 inline void tcpv4_ev (ev::io &w, int revents);
82 106
83 bool send_packet (vpn_packet *pkt, int tos); 107 bool send_packet (vpn_packet *pkt, int tos);
84 bool write_packet (); 108 bool write_packet ();
85 109
86 void error (); // abort conenction && cleanup 110 void error (); // abort conenction && cleanup
87 111
88 operator tcp_si_map::value_type() 112 operator tcp_si_map::value_type()
89 { 113 {
90 return tcp_si_map::value_type (&si, this); 114 return tcp_si_map::value_type (&si, this);
91 } 115 }
92 116
93 tcp_connection (int fd_, const sockinfo &si_, vpn &v_); 117 tcp_connection (int fd_, const sockinfo &si_, vpn &v_);
94 ~tcp_connection (); 118 ~tcp_connection ();
95}; 119};
96 120
97void tcp_si_map::cleaner_cb (time_watcher &w) 121void tcp_si_map::cleaner_cb (ev::timer &w, int revents)
98{ 122{
99 w.at = NOW + 600;
100 tstamp to = NOW - ::conf.keepalive - 30 - 60; 123 tstamp to = ev_now () - ::conf.keepalive - 30 - 60;
101 124
102 for (iterator i = begin (); i != end(); ) 125 for (iterator i = begin (); i != end(); )
103 if (i->second->last_activity >= to) 126 if (i->second->last_activity >= to)
104 ++i; 127 ++i;
105 else 128 else
106 { 129 {
130 delete i->second;
107 erase (i); 131 erase (i);
108 i = begin (); 132 i = begin ();
109 } 133 }
110} 134}
111 135
112void 136void
113vpn::tcpv4_ev (io_watcher &w, short revents) 137vpn::tcpv4_ev (ev::io &w, int revents)
114{ 138{
115 if (revents & (POLLIN | POLLERR)) 139 if (revents & EV_READ)
116 { 140 {
117 struct sockaddr_in sa; 141 struct sockaddr_in sa;
118 socklen_t sa_len = sizeof (sa); 142 socklen_t sa_len = sizeof (sa);
119 int len; 143 int len;
120 144
121 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); 145 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len);
122 146
123 if (fd >= 0) 147 if (fd >= 0)
124 { 148 {
149 fcntl (fd, F_SETFL, O_NONBLOCK);
150 fcntl (fd, F_SETFD, FD_CLOEXEC);
151
125 sockinfo si(sa, PROT_TCPv4); 152 sockinfo si(sa, PROT_TCPv4);
126 153
127 slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D 154 slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D
128
129 fcntl (fd, F_SETFL, O_NONBLOCK);
130 155
131 tcp_connection *i = new tcp_connection (fd, si, *this); 156 tcp_connection *i = new tcp_connection (fd, si, *this);
132 tcp_si.insert (*i); 157 tcp_si.insert (*i);
133 } 158 }
134 } 159 }
150 i = info->second; 175 i = info->second;
151 176
152 return i->send_packet (pkt, tos); 177 return i->send_packet (pkt, tos);
153} 178}
154 179
155void tcp_connection::error ()
156{
157 if (fd >= 0)
158 {
159 close (fd);
160 fd = -1;
161 }
162
163 delete r_pkt; r_pkt = 0;
164 delete w_pkt; w_pkt = 0;
165
166 stop ();
167 state = active ? IDLE : ERROR;
168}
169
170bool 180bool
171tcp_connection::write_packet () 181tcp_connection::write_packet ()
172{ 182{
173 ssize_t len; 183 ssize_t len;
174 184
175 if (w_ofs < 2) 185 if (w_ofs < 2)
176 { 186 {
177 u16 plen = htons (w_pkt->len); 187 u16 plen = htons (w_pkt->len);
178 188
179 iovec vec[2]; 189 iovec vec[2];
190 //TODO: char* is the right type? hardly...
180 vec[0].iov_base = ((u8 *)&plen) + w_ofs; 191 vec[0].iov_base = (char *)((u8 *)&plen) + w_ofs;
181 vec[0].iov_len = 2 - w_ofs; 192 vec[0].iov_len = 2 - w_ofs;
182 vec[1].iov_base = &((*w_pkt)[0]); 193 vec[1].iov_base = (char *)&((*w_pkt)[0]);
183 vec[1].iov_len = w_len - 2; 194 vec[1].iov_len = w_len - 2;
184 195
185 len = writev (fd, vec, 2); 196 len = writev (fd, vec, 2);
186 } 197 }
187 else 198 else
202 return false; 213 return false;
203 } 214 }
204} 215}
205 216
206void 217void
207tcp_connection::tcpv4_ev (io_watcher &w, short revents) 218tcp_connection::tcpv4_ev (ev::io &w, int revents)
208{ 219{
209 last_activity = NOW; 220 last_activity = ev_now ();
210 221
211 if (revents & (POLLERR | POLLHUP)) 222 if (revents & EV_WRITE)
212 {
213 error ();
214 return;
215 }
216
217 if (revents & POLLOUT)
218 { 223 {
219 if (state == CONNECTING) 224 if (state == CONNECTING)
220 { 225 {
221 state = ESTABLISHED; 226 state = ESTABLISHED;
222 set (POLLIN); 227 set (EV_READ);
228#if ENABLE_HTTP_PROXY
229 if (::conf.proxy_host && ::conf.proxy_port)
230 {
231 state = CONNECTING_PROXY;
232
233 if (write (fd, proxy_req, proxy_req_len) == 0)
234 {
235 error ();
236 return;
237 }
238
239 free (proxy_req); proxy_req = 0;
240 }
241#endif
223 } 242 }
224 else if (state == ESTABLISHED) 243 else if (state == ESTABLISHED)
225 { 244 {
226 if (w_pkt) 245 if (w_pkt)
227 { 246 {
228 if (write_packet ()) 247 if (write_packet ())
229 { 248 {
230 delete w_pkt; w_pkt = 0; 249 delete w_pkt; w_pkt = 0;
231 250
232 set (POLLIN); 251 set (EV_READ);
233 } 252 }
234 } 253 }
235 else 254 else
236 set (POLLIN); 255 set (EV_READ);
237 } 256 }
238 else 257 else
239 set (POLLIN); 258 set (EV_READ);
240 }
241
242 if (revents & POLLIN)
243 { 259 }
260
261 if (revents & EV_READ)
262 {
263 if (state == ESTABLISHED)
244 for (;;) 264 for (;;)
265 {
266 if (!r_pkt)
267 {
268 r_pkt = new vpn_packet;
269 r_ofs = 0;
270 r_len = 2; // header
271 }
272
273 ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len);
274
275 if (len > 0)
276 {
277 r_len -= len;
278 r_ofs += len;
279
280 if (r_len == 0)
281 {
282 if (r_ofs == 2)
283 {
284 r_len = ntohs (*(u16 *)&((*r_pkt)[0]));
285 r_pkt->len = r_len;
286
287 if (r_len > 0 && r_len < MAXSIZE)
288 continue;
289 }
290 else
291 {
292 v.recv_vpn_packet (r_pkt, si);
293 delete r_pkt;
294 r_pkt = 0;
295
296 continue;
297 }
298 }
299 else
300 break;
301 }
302 else if (len < 0 && (errno == EINTR || errno == EAGAIN))
303 break;
304
305 // len == 0 <-> EOF
306 error ();
307 break;
308 }
309#if ENABLE_HTTP_PROXY
310 else if (state == CONNECTING_PROXY)
245 { 311 {
246 if (!r_pkt) 312 fcntl (fd, F_SETFL, 0);
247 { 313 char r[1024];
248 r_pkt = new vpn_packet; 314 int i;
249 r_ofs = 0; 315 bool emptyline = false;
250 r_len = 2; // header 316
317 // we do a blocking read of the response, to hell with it
318 for (i = 0; i < 1023; i++)
251 } 319 {
320 int l = read (fd, &r[i], 1);
252 321
253 ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len);
254
255 if (len > 0)
256 {
257 r_len -= len;
258 r_ofs += len;
259
260 if (r_len == 0) 322 if (l <= 0)
261 { 323 {
262 if (r_ofs == 2) 324 error ();
263 { 325 return;
264 r_len = ntohs (*(u16 *)&((*r_pkt)[0]));
265 r_pkt->len = r_len;
266
267 if (r_len > 0 && r_len < MAXSIZE)
268 continue;
269 } 326 }
327
328 if (r[i] == '\012')
329 {
330 if (emptyline)
331 break;
270 else 332 else
271 {
272 v.recv_vpn_packet (r_pkt, si);
273 delete r_pkt;
274 r_pkt = 0;
275
276 continue; 333 emptyline = true;
277 } 334 }
335 else if (r[i] != '\015')
336 emptyline = false;
337 }
338
339 fcntl (fd, F_SETFL, O_NONBLOCK);
340
341 if (i < 12)
342 {
343 slog (L_ERR, _("(%s): unable to do proxy-forwarding, short response"),
344 (const char *)si);
345 error ();
346 }
347 else if (r[0] != 'H' || r[1] != 'T' || r[2] != 'T' || r[3] != 'P' || r[4] != '/'
348 || r[5] != '1' // http-major
349 || r[9] != '2') // response
350 {
351 slog (L_ERR, _("(%s): malformed or unexpected proxy response (%.12s)"),
352 (const char *)si, r);
353 error ();
354 }
355 else
356 state = ESTABLISHED;
357 }
358#endif
359 }
360}
361
362bool
363tcp_connection::send_packet (vpn_packet *pkt, int tos)
364{
365 last_activity = ev_now ();
366
367 if (state == IDLE)
368 {
369 // woaw, the first lost packet ;)
370 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
371
372 if (fd >= 0)
373 {
374 const sockinfo *csi = &si;
375
376#if ENABLE_HTTP_PROXY
377 sockinfo psi;
378
379 if (::conf.proxy_host && ::conf.proxy_port)
380 {
381 psi.set (::conf.proxy_host, ::conf.proxy_port, PROT_TCPv4);
382
383 if (psi.valid ())
384 {
385 csi = &psi;
386
387 proxy_req_len = asprintf (&proxy_req,
388 "CONNECT %s:%d HTTP/1.0\015\012"
389 "%s%s%s" // optional proxy-auth
390 "\015\012",
391 si.ntoa (),
392 ntohs (si.port),
393 ::conf.proxy_auth ? "Proxy-Authorization: Basic " : "",
394 ::conf.proxy_auth ? ::conf.proxy_auth : "",
395 ::conf.proxy_auth ? "\015\012" : "");
396
278 } 397 }
279 else 398 else
280 break; 399 slog (L_ERR, _("unable to resolve http proxy hostname '%s', trying direct"),
400 ::conf.proxy_host);
401 }
402#endif
281 } 403
282 else if (len < 0 && (errno == EINTR || errno == EAGAIN))
283 break;
284
285 error ();
286 break;
287 }
288 }
289}
290
291bool
292tcp_connection::send_packet (vpn_packet *pkt, int tos)
293{
294 last_activity = NOW;
295
296 if (state == IDLE)
297 {
298 // woaw, the first lost packet ;)
299 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
300
301 if (fd >= 0)
302 {
303 fcntl (fd, F_SETFL, O_NONBLOCK); 404 fcntl (fd, F_SETFL, O_NONBLOCK);
304 405
305 if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0 406 if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0
306 || errno == EINPROGRESS) 407 || errno == EINPROGRESS)
307 { 408 {
409 fcntl (fd, F_SETFL, O_NONBLOCK);
410 fcntl (fd, F_SETFD, FD_CLOEXEC);
411
308 state = CONNECTING; 412 state = CONNECTING;
309 start (fd, POLLOUT); 413 start (fd, EV_WRITE);
310 } 414 }
311 else 415 else
312 close (fd); 416 close (fd);
313 } 417 }
314 } 418 }
318 // right thing to do, not using tcp *is* the right thing to do. 422 // right thing to do, not using tcp *is* the right thing to do.
319 if (!w_pkt) 423 if (!w_pkt)
320 { 424 {
321 // 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
322 // and we don't care. at least we tried ;) 426 // and we don't care. at least we tried ;)
427#if defined(SOL_IP) && defined(IP_TOS)
428 if (tos != this->tos)
429 {
430 this->tos = tos;
323 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); 431 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
432 }
433#endif
324 434
325 w_pkt = pkt; 435 w_pkt = pkt;
326 w_ofs = 0; 436 w_ofs = 0;
327 w_len = pkt->len + 2; // length + size header 437 w_len = pkt->len + 2; // length + size header
328 438
331 else 441 else
332 { 442 {
333 w_pkt = new vpn_packet; 443 w_pkt = new vpn_packet;
334 w_pkt->set (*pkt); 444 w_pkt->set (*pkt);
335 445
336 set (POLLIN | POLLOUT); 446 set (EV_READ | EV_WRITE);
337 } 447 }
338 } 448 }
339 } 449 }
340 450
341 return state != ERROR; 451 return state != ERROR;
342} 452}
343 453
454void tcp_connection::error ()
455{
456 stop ();
457
458 if (fd >= 0)
459 {
460 close (fd);
461 tos = -1;
462 fd = -1;
463 }
464
465 delete r_pkt; r_pkt = 0;
466 delete w_pkt; w_pkt = 0;
467#if ENABLE_HTTP_PROXY
468 free (proxy_req); proxy_req = 0;
469#endif
470
471 state = active ? IDLE : ERROR;
472}
473
344tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) 474tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_)
345: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) 475: v(v_), si(si_)
346{ 476{
477 set<tcp_connection, &tcp_connection::tcpv4_ev> (this);
478
347 last_activity = NOW; 479 last_activity = ev_now ();
348 r_pkt = 0; 480 r_pkt = 0;
349 w_pkt = 0; 481 w_pkt = 0;
482 tos = -1;
350 fd = fd_; 483 fd = fd_;
484#if ENABLE_HTTP_PROXY
485 proxy_req = 0;
486#endif
351 487
352 if (fd < 0) 488 if (fd < 0)
353 { 489 {
354 active = true; 490 active = true;
355 state = IDLE; 491 state = IDLE;
356 } 492 }
357 else 493 else
358 { 494 {
359 active = false; 495 active = false;
360 state = ESTABLISHED; 496 state = ESTABLISHED;
361 start (fd, POLLIN); 497 start (fd, EV_READ);
362 } 498 }
363} 499}
364 500
365tcp_connection::~tcp_connection () 501tcp_connection::~tcp_connection ()
366{ 502{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines