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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines