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.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 void tcpv4_ev (io_watcher &w, short revents); 97 vpn_packet *w_pkt;
98 u32 w_len, w_ofs;
99
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);
79 106
80 bool send_packet (vpn_packet *pkt, int tos); 107 bool send_packet (vpn_packet *pkt, int tos);
108 bool write_packet ();
81 109
82 void error (); // abort conenction && cleanup 110 void error (); // abort conenction && cleanup
83 111
84 operator tcp_si_map::value_type() 112 operator tcp_si_map::value_type()
85 { 113 {
86 return tcp_si_map::value_type (&si, this); 114 return tcp_si_map::value_type (&si, this);
87 } 115 }
88 116
89 tcp_connection (int fd_, const sockinfo &si_, vpn &v_); 117 tcp_connection (int fd_, const sockinfo &si_, vpn &v_);
90 ~tcp_connection (); 118 ~tcp_connection ();
91}; 119};
92 120
93void tcp_si_map::cleaner_cb (time_watcher &w) 121void tcp_si_map::cleaner_cb (ev::timer &w, int revents)
94{ 122{
95 w.at = NOW + 600;
96 tstamp to = NOW - ::conf.keepalive - 30 - 60; 123 tstamp to = ev_now () - ::conf.keepalive - 30 - 60;
97 124
98 for (iterator i = begin (); i != end(); ) 125 for (iterator i = begin (); i != end(); )
99 if (i->second->last_activity >= to) 126 if (i->second->last_activity >= to)
100 ++i; 127 ++i;
101 else 128 else
102 { 129 {
130 delete i->second;
103 erase (i); 131 erase (i);
104 i = begin (); 132 i = begin ();
105 } 133 }
106} 134}
107 135
108void 136void
109vpn::tcpv4_ev (io_watcher &w, short revents) 137vpn::tcpv4_ev (ev::io &w, int revents)
110{ 138{
111 if (revents & (POLLIN | POLLERR)) 139 if (revents & EV_READ)
112 { 140 {
113 struct sockaddr_in sa; 141 struct sockaddr_in sa;
114 socklen_t sa_len = sizeof (sa); 142 socklen_t sa_len = sizeof (sa);
115 int len; 143 int len;
116 144
117 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); 145 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len);
118 146
119 if (fd >= 0) 147 if (fd >= 0)
120 { 148 {
149 fcntl (fd, F_SETFL, O_NONBLOCK);
150 fcntl (fd, F_SETFD, FD_CLOEXEC);
151
121 sockinfo si(sa, PROT_TCPv4); 152 sockinfo si(sa, PROT_TCPv4);
122 153
123 slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D 154 slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D
124
125 fcntl (fd, F_SETFL, O_NONBLOCK);
126 155
127 tcp_connection *i = new tcp_connection (fd, si, *this); 156 tcp_connection *i = new tcp_connection (fd, si, *this);
128 tcp_si.insert (*i); 157 tcp_si.insert (*i);
129 } 158 }
130 } 159 }
146 i = info->second; 175 i = info->second;
147 176
148 return i->send_packet (pkt, tos); 177 return i->send_packet (pkt, tos);
149} 178}
150 179
151void tcp_connection::error () 180bool
181tcp_connection::write_packet ()
152{ 182{
153 if (fd >= 0) 183 ssize_t len;
154 { 184
155 close (fd); 185 if (w_ofs < 2)
156 fd = -1;
157 } 186 {
187 u16 plen = htons (w_pkt->len);
158 188
159 delete r_pkt; 189 iovec vec[2];
160 r_pkt = 0; 190 //TODO: char* is the right type? hardly...
191 vec[0].iov_base = (char *)((u8 *)&plen) + w_ofs;
192 vec[0].iov_len = 2 - w_ofs;
193 vec[1].iov_base = (char *)&((*w_pkt)[0]);
194 vec[1].iov_len = w_len - 2;
161 195
162 stop (); 196 len = writev (fd, vec, 2);
163 state = active ? IDLE : ERROR; 197 }
198 else
199 len = write (fd, &((*w_pkt)[w_ofs - 2]), w_len);
200
201 if (len > 0)
202 {
203 w_ofs += len;
204 w_len -= len;
205
206 return w_len == 0;
207 }
208 else if (len < 0 && (errno == EAGAIN || errno == EINTR))
209 return false;
210 else
211 {
212 error ();
213 return false;
214 }
164} 215}
165 216
166void 217void
167tcp_connection::tcpv4_ev (io_watcher &w, short revents) 218tcp_connection::tcpv4_ev (ev::io &w, int revents)
168{ 219{
169 last_activity = NOW; 220 last_activity = ev_now ();
170 221
171 if (revents & (POLLERR | POLLHUP)) 222 if (revents & EV_WRITE)
172 error ();
173 else if (revents & POLLOUT && state == CONNECTING)
174 {
175 state = ESTABLISHED;
176 set (POLLIN);
177 } 223 {
178 224 if (state == CONNECTING)
179 else if (revents & POLLIN)
180 {
181 for (;;)
182 { 225 {
183 if (!r_pkt) 226 state = ESTABLISHED;
184 { 227 set (EV_READ);
185 r_pkt = new vpn_packet; 228#if ENABLE_HTTP_PROXY
186 r_ofs = 0; 229 if (::conf.proxy_host && ::conf.proxy_port)
187 r_len = 2; // header
188 } 230 {
231 state = CONNECTING_PROXY;
189 232
190 ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len); 233 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 { 234 {
235 error ();
236 return;
237 }
238
239 free (proxy_req); proxy_req = 0;
240 }
241#endif
242 }
243 else if (state == ESTABLISHED)
244 {
245 if (w_pkt)
246 {
247 if (write_packet ())
248 {
249 delete w_pkt; w_pkt = 0;
250
251 set (EV_READ);
252 }
253 }
254 else
255 set (EV_READ);
256 }
257 else
258 set (EV_READ);
259 }
260
261 if (revents & EV_READ)
262 {
263 if (state == ESTABLISHED)
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 {
199 if (r_ofs == 2) 282 if (r_ofs == 2)
200 { 283 {
201 r_len = ntohs (*(u16 *)&((*r_pkt)[0])); 284 r_len = ntohs (*(u16 *)&((*r_pkt)[0]));
202 r_pkt->len = r_len; 285 r_pkt->len = r_len;
203 286
204 if (r_len > 0 && r_len < MAXSIZE) 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
205 continue; 296 continue;
206 } 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)
311 {
312 fcntl (fd, F_SETFL, 0);
313 char r[1024];
314 int i;
315 bool emptyline = false;
316
317 // we do a blocking read of the response, to hell with it
318 for (i = 0; i < 1023; i++)
319 {
320 int l = read (fd, &r[i], 1);
321
322 if (l <= 0)
323 {
324 error ();
325 return;
326 }
327
328 if (r[i] == '\012')
329 {
330 if (emptyline)
331 break;
207 else 332 else
208 {
209 v.recv_vpn_packet (r_pkt, si);
210 delete r_pkt;
211 r_pkt = 0;
212
213 continue; 333 emptyline = true;
214 }
215 } 334 }
335 else if (r[i] != '\015')
336 emptyline = false;
216 337 }
338
339 fcntl (fd, F_SETFL, O_NONBLOCK);
340
341 if (i < 12)
217 } 342 {
218 else if (len < 0 && (errno == EINTR || errno == EAGAIN)) 343 slog (L_ERR, _("(%s): unable to do proxy-forwarding, short response"),
219 return; 344 (const char *)si);
220
221 error (); 345 error ();
222 return; 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;
223 } 357 }
358#endif
224 } 359 }
225} 360}
226 361
227bool 362bool
228tcp_connection::send_packet (vpn_packet *pkt, int tos) 363tcp_connection::send_packet (vpn_packet *pkt, int tos)
229{ 364{
230 last_activity = NOW; 365 last_activity = ev_now ();
231 366
232 if (state == IDLE) 367 if (state == IDLE)
233 { 368 {
234 // woaw, the first lost packet ;) 369 // woaw, the first lost packet ;)
235 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 370 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
236 371
237 if (fd >= 0) 372 if (fd >= 0)
238 { 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
397 }
398 else
399 slog (L_ERR, _("unable to resolve http proxy hostname '%s', trying direct"),
400 ::conf.proxy_host);
401 }
402#endif
403
239 fcntl (fd, F_SETFL, O_NONBLOCK); 404 fcntl (fd, F_SETFL, O_NONBLOCK);
240 405
241 if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0 406 if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0
242 || errno == EINPROGRESS) 407 || errno == EINPROGRESS)
243 { 408 {
409 fcntl (fd, F_SETFL, O_NONBLOCK);
410 fcntl (fd, F_SETFD, FD_CLOEXEC);
411
244 state = CONNECTING; 412 state = CONNECTING;
245 start (fd, POLLOUT); 413 start (fd, EV_WRITE);
246 } 414 }
247 else 415 else
248 close (fd); 416 close (fd);
249 } 417 }
250 } 418 }
251 else if (state == ESTABLISHED) 419 else if (state == ESTABLISHED)
252 { 420 {
421 // drop packet if the tcp write buffer is full. this *is* the
422 // right thing to do, not using tcp *is* the right thing to do.
423 if (!w_pkt)
424 {
253 // how this maps to the underlying tcp packet we don't know 425 // how this maps to the underlying tcp packets we don't know
254 // 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;
255 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); 431 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
432 }
433#endif
256 434
257 // we use none of the advantages of tcp; if an error occurs, just drop 435 w_pkt = pkt;
258 // (this happens when a tcp connection gets stuck, too, which might not be 436 w_ofs = 0;
259 // the wisest thing to do.. either drop packet (too late) or make sure 437 w_len = pkt->len + 2; // length + size header
260 // it gets delivered)
261 u16 len = htons (pkt->len);
262 438
263 iovec vec[2]; 439 if (write_packet ())
264 vec[0].iov_base = &len; 440 w_pkt = 0;
265 vec[0].iov_len = sizeof len; 441 else
266 vec[1].iov_base = &((*pkt)[0]); 442 {
267 vec[1].iov_len = pkt->len; 443 w_pkt = new vpn_packet;
444 w_pkt->set (*pkt);
445
446 set (EV_READ | EV_WRITE);
447 }
268 448 }
269 if (sizeof (u16) + pkt->len != writev (fd, vec, 2))
270 error ();
271 } 449 }
272 450
273 return state != ERROR; 451 return state != ERROR;
274} 452}
275 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
276tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) 474tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_)
277: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) 475: v(v_), si(si_)
278{ 476{
477 set<tcp_connection, &tcp_connection::tcpv4_ev> (this);
478
279 last_activity = NOW; 479 last_activity = ev_now ();
280 r_pkt = 0; 480 r_pkt = 0;
481 w_pkt = 0;
482 tos = -1;
281 fd = fd_; 483 fd = fd_;
484#if ENABLE_HTTP_PROXY
485 proxy_req = 0;
486#endif
282 487
283 if (fd < 0) 488 if (fd < 0)
284 { 489 {
285 active = true; 490 active = true;
286 state = IDLE; 491 state = IDLE;
287 } 492 }
288 else 493 else
289 { 494 {
290 active = false; 495 active = false;
291 state = ESTABLISHED; 496 state = ESTABLISHED;
292 start (fd, POLLIN); 497 start (fd, EV_READ);
293 } 498 }
294} 499}
295 500
296tcp_connection::~tcp_connection () 501tcp_connection::~tcp_connection ()
297{ 502{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines