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.20 by pcg, Sat Dec 1 23:35:31 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{
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 void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner;
60 64
61 tcp_si_map () 65 tcp_si_map ()
62 : cleaner(this, &tcp_si_map::cleaner_cb) 66 : cleaner(this, &tcp_si_map::cleaner_cb)
63 { 67 {
64 cleaner.start (0); 68 cleaner.start (::conf.keepalive / 2, ::conf.keepalive / 2);
65 } 69 }
70
66} tcp_si; 71} tcp_si;
67 72
68struct tcp_connection : io_watcher { 73struct tcp_connection : ev::io {
69 tstamp last_activity; 74 tstamp last_activity;
70 const sockinfo si; 75 const sockinfo si;
71 vpn &v; 76 vpn &v;
72 bool active; // this connection has been actively established 77 bool active; // this connection has been actively established
73 enum { ERROR, IDLE, CONNECTING, ESTABLISHED } state; 78 enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state;
74 79
75 vpn_packet *r_pkt; 80 vpn_packet *r_pkt;
76 u32 r_len, r_ofs; 81 u32 r_len, r_ofs;
77 82
78 vpn_packet *w_pkt; 83 vpn_packet *w_pkt;
79 u32 w_len, w_ofs; 84 u32 w_len, w_ofs;
80 85
86#if ENABLE_HTTP_PROXY
87 char *proxy_req;
88 int proxy_req_len;
89#endif
90
81 void tcpv4_ev (io_watcher &w, short revents); 91 void tcpv4_ev (ev::io &w, int revents);
82 92
83 bool send_packet (vpn_packet *pkt, int tos); 93 bool send_packet (vpn_packet *pkt, int tos);
84 bool write_packet (); 94 bool write_packet ();
85 95
86 void error (); // abort conenction && cleanup 96 void error (); // abort conenction && cleanup
87 97
88 operator tcp_si_map::value_type() 98 operator tcp_si_map::value_type()
89 { 99 {
90 return tcp_si_map::value_type (&si, this); 100 return tcp_si_map::value_type (&si, this);
91 } 101 }
92 102
93 tcp_connection (int fd_, const sockinfo &si_, vpn &v_); 103 tcp_connection (int fd_, const sockinfo &si_, vpn &v_);
94 ~tcp_connection (); 104 ~tcp_connection ();
95}; 105};
96 106
97void tcp_si_map::cleaner_cb (time_watcher &w) 107void tcp_si_map::cleaner_cb (ev::timer &w, int revents)
98{ 108{
99 w.at = NOW + 600;
100 tstamp to = NOW - ::conf.keepalive - 30 - 60; 109 tstamp to = ev_now () - ::conf.keepalive - 30 - 60;
101 110
102 for (iterator i = begin (); i != end(); ) 111 for (iterator i = begin (); i != end(); )
103 if (i->second->last_activity >= to) 112 if (i->second->last_activity >= to)
104 ++i; 113 ++i;
105 else 114 else
106 { 115 {
116 delete i->second;
107 erase (i); 117 erase (i);
108 i = begin (); 118 i = begin ();
109 } 119 }
110} 120}
111 121
112void 122void
113vpn::tcpv4_ev (io_watcher &w, short revents) 123vpn::tcpv4_ev (ev::io &w, int revents)
114{ 124{
115 if (revents & (POLLIN | POLLERR)) 125 if (revents & EV_READ)
116 { 126 {
117 struct sockaddr_in sa; 127 struct sockaddr_in sa;
118 socklen_t sa_len = sizeof (sa); 128 socklen_t sa_len = sizeof (sa);
119 int len; 129 int len;
120 130
121 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); 131 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len);
122 132
123 if (fd >= 0) 133 if (fd >= 0)
124 { 134 {
135 fcntl (fd, F_SETFL, O_NONBLOCK);
136 fcntl (fd, F_SETFD, FD_CLOEXEC);
137
125 sockinfo si(sa, PROT_TCPv4); 138 sockinfo si(sa, PROT_TCPv4);
126 139
127 slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D 140 slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D
128
129 fcntl (fd, F_SETFL, O_NONBLOCK);
130 141
131 tcp_connection *i = new tcp_connection (fd, si, *this); 142 tcp_connection *i = new tcp_connection (fd, si, *this);
132 tcp_si.insert (*i); 143 tcp_si.insert (*i);
133 } 144 }
134 } 145 }
150 i = info->second; 161 i = info->second;
151 162
152 return i->send_packet (pkt, tos); 163 return i->send_packet (pkt, tos);
153} 164}
154 165
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 166bool
171tcp_connection::write_packet () 167tcp_connection::write_packet ()
172{ 168{
173 ssize_t len; 169 ssize_t len;
174 170
175 if (w_ofs < 2) 171 if (w_ofs < 2)
176 { 172 {
177 u16 plen = htons (w_pkt->len); 173 u16 plen = htons (w_pkt->len);
178 174
179 iovec vec[2]; 175 iovec vec[2];
176 //TODO: char* is the right type? hardly...
180 vec[0].iov_base = ((u8 *)&plen) + w_ofs; 177 vec[0].iov_base = (char *)((u8 *)&plen) + w_ofs;
181 vec[0].iov_len = 2 - w_ofs; 178 vec[0].iov_len = 2 - w_ofs;
182 vec[1].iov_base = &((*w_pkt)[0]); 179 vec[1].iov_base = (char *)&((*w_pkt)[0]);
183 vec[1].iov_len = w_len - 2; 180 vec[1].iov_len = w_len - 2;
184 181
185 len = writev (fd, vec, 2); 182 len = writev (fd, vec, 2);
186 } 183 }
187 else 184 else
202 return false; 199 return false;
203 } 200 }
204} 201}
205 202
206void 203void
207tcp_connection::tcpv4_ev (io_watcher &w, short revents) 204tcp_connection::tcpv4_ev (ev::io &w, int revents)
208{ 205{
209 last_activity = NOW; 206 last_activity = ev_now ();
210 207
211 if (revents & (POLLERR | POLLHUP)) 208 if (revents & EV_WRITE)
212 {
213 error ();
214 return;
215 }
216
217 if (revents & POLLOUT)
218 { 209 {
219 if (state == CONNECTING) 210 if (state == CONNECTING)
220 { 211 {
221 state = ESTABLISHED; 212 state = ESTABLISHED;
222 set (POLLIN); 213 set (EV_READ);
214#if ENABLE_HTTP_PROXY
215 if (::conf.proxy_host && ::conf.proxy_port)
216 {
217 state = CONNECTING_PROXY;
218
219 if (write (fd, proxy_req, proxy_req_len) == 0)
220 {
221 error ();
222 return;
223 }
224
225 free (proxy_req); proxy_req = 0;
226 }
227#endif
223 } 228 }
224 else if (state == ESTABLISHED) 229 else if (state == ESTABLISHED)
225 { 230 {
226 if (w_pkt) 231 if (w_pkt)
227 { 232 {
228 if (write_packet ()) 233 if (write_packet ())
229 { 234 {
230 delete w_pkt; w_pkt = 0; 235 delete w_pkt; w_pkt = 0;
231 236
232 set (POLLIN); 237 set (EV_READ);
233 } 238 }
234 } 239 }
235 else 240 else
236 set (POLLIN); 241 set (EV_READ);
237 } 242 }
238 else 243 else
239 set (POLLIN); 244 set (EV_READ);
240 }
241
242 if (revents & POLLIN)
243 { 245 }
246
247 if (revents & EV_READ)
248 {
249 if (state == ESTABLISHED)
244 for (;;) 250 for (;;)
245 { 251 {
246 if (!r_pkt) 252 if (!r_pkt)
247 { 253 {
248 r_pkt = new vpn_packet; 254 r_pkt = new vpn_packet;
249 r_ofs = 0; 255 r_ofs = 0;
250 r_len = 2; // header 256 r_len = 2; // header
251 } 257 }
252 258
253 ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len); 259 ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len);
254 260
255 if (len > 0) 261 if (len > 0)
256 { 262 {
257 r_len -= len; 263 r_len -= len;
258 r_ofs += len; 264 r_ofs += len;
259 265
260 if (r_len == 0) 266 if (r_len == 0)
267 {
268 if (r_ofs == 2)
269 {
270 r_len = ntohs (*(u16 *)&((*r_pkt)[0]));
271 r_pkt->len = r_len;
272
273 if (r_len > 0 && r_len < MAXSIZE)
274 continue;
275 }
276 else
277 {
278 v.recv_vpn_packet (r_pkt, si);
279 delete r_pkt;
280 r_pkt = 0;
281
282 continue;
283 }
284 }
285 else
286 break;
287 }
288 else if (len < 0 && (errno == EINTR || errno == EAGAIN))
289 break;
290
291 // len == 0 <-> EOF
292 error ();
293 break;
294 }
295#if ENABLE_HTTP_PROXY
296 else if (state == CONNECTING_PROXY)
297 {
298 fcntl (fd, F_SETFL, 0);
299 char r[1024];
300 int i;
301 bool emptyline = false;
302
303 // we do a blocking read of the response, to hell with it
304 for (i = 0; i < 1023; i++)
305 {
306 int l = read (fd, &r[i], 1);
307
308 if (l <= 0)
261 { 309 {
262 if (r_ofs == 2) 310 error ();
263 { 311 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 } 312 }
313
314 if (r[i] == '\012')
315 {
316 if (emptyline)
317 break;
270 else 318 else
271 {
272 v.recv_vpn_packet (r_pkt, si);
273 delete r_pkt;
274 r_pkt = 0;
275
276 continue; 319 emptyline = true;
277 } 320 }
321 else if (r[i] != '\015')
322 emptyline = false;
323 }
324
325 fcntl (fd, F_SETFL, O_NONBLOCK);
326
327 if (i < 12)
328 {
329 slog (L_ERR, _("(%s): unable to do proxy-forwarding, short response"),
330 (const char *)si);
331 error ();
332 }
333 else if (r[0] != 'H' || r[1] != 'T' || r[2] != 'T' || r[3] != 'P' || r[4] != '/'
334 || r[5] != '1' // http-major
335 || r[9] != '2') // response
336 {
337 slog (L_ERR, _("(%s): malformed or unexpected proxy response (%.12s)"),
338 (const char *)si, r);
339 error ();
340 }
341 else
342 state = ESTABLISHED;
343 }
344#endif
345 }
346}
347
348bool
349tcp_connection::send_packet (vpn_packet *pkt, int tos)
350{
351 last_activity = ev_now ();
352
353 if (state == IDLE)
354 {
355 // woaw, the first lost packet ;)
356 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
357
358 if (fd >= 0)
359 {
360 const sockinfo *csi = &si;
361
362#if ENABLE_HTTP_PROXY
363 sockinfo psi;
364
365 if (::conf.proxy_host && ::conf.proxy_port)
366 {
367 psi.set (::conf.proxy_host, ::conf.proxy_port, PROT_TCPv4);
368
369 if (psi.valid ())
370 {
371 csi = &psi;
372
373 proxy_req_len = asprintf (&proxy_req,
374 "CONNECT %s:%d HTTP/1.0\015\012"
375 "%s%s%s" // optional proxy-auth
376 "\015\012",
377 si.ntoa (),
378 ntohs (si.port),
379 ::conf.proxy_auth ? "Proxy-Authorization: Basic " : "",
380 ::conf.proxy_auth ? ::conf.proxy_auth : "",
381 ::conf.proxy_auth ? "\015\012" : "");
382
278 } 383 }
279 else 384 else
280 break; 385 slog (L_ERR, _("unable to resolve http proxy hostname '%s', trying direct"),
386 ::conf.proxy_host);
387 }
388#endif
281 } 389
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); 390 fcntl (fd, F_SETFL, O_NONBLOCK);
304 391
305 if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0 392 if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0
306 || errno == EINPROGRESS) 393 || errno == EINPROGRESS)
307 { 394 {
395 fcntl (fd, F_SETFL, O_NONBLOCK);
396 fcntl (fd, F_SETFD, FD_CLOEXEC);
397
308 state = CONNECTING; 398 state = CONNECTING;
309 start (fd, POLLOUT); 399 start (fd, EV_WRITE);
310 } 400 }
311 else 401 else
312 close (fd); 402 close (fd);
313 } 403 }
314 } 404 }
318 // right thing to do, not using tcp *is* the right thing to do. 408 // right thing to do, not using tcp *is* the right thing to do.
319 if (!w_pkt) 409 if (!w_pkt)
320 { 410 {
321 // how this maps to the underlying tcp packets we don't know 411 // how this maps to the underlying tcp packets we don't know
322 // and we don't care. at least we tried ;) 412 // and we don't care. at least we tried ;)
413#if defined(SOL_IP) && defined(IP_TOS)
323 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); 414 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
415#endif
324 416
325 w_pkt = pkt; 417 w_pkt = pkt;
326 w_ofs = 0; 418 w_ofs = 0;
327 w_len = pkt->len + 2; // length + size header 419 w_len = pkt->len + 2; // length + size header
328 420
331 else 423 else
332 { 424 {
333 w_pkt = new vpn_packet; 425 w_pkt = new vpn_packet;
334 w_pkt->set (*pkt); 426 w_pkt->set (*pkt);
335 427
336 set (POLLIN | POLLOUT); 428 set (EV_READ | EV_WRITE);
337 } 429 }
338 } 430 }
339 } 431 }
340 432
341 return state != ERROR; 433 return state != ERROR;
342} 434}
343 435
436void tcp_connection::error ()
437{
438 stop ();
439
440 if (fd >= 0)
441 {
442 close (fd);
443 fd = -1;
444 }
445
446 delete r_pkt; r_pkt = 0;
447 delete w_pkt; w_pkt = 0;
448#if ENABLE_HTTP_PROXY
449 free (proxy_req); proxy_req = 0;
450#endif
451
452 state = active ? IDLE : ERROR;
453}
454
344tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) 455tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_)
345: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) 456: v(v_), si(si_), ev::io(this, &tcp_connection::tcpv4_ev)
346{ 457{
347 last_activity = NOW; 458 last_activity = ev_now ();
348 r_pkt = 0; 459 r_pkt = 0;
349 w_pkt = 0; 460 w_pkt = 0;
350 fd = fd_; 461 fd = fd_;
462#if ENABLE_HTTP_PROXY
463 proxy_req = 0;
464#endif
351 465
352 if (fd < 0) 466 if (fd < 0)
353 { 467 {
354 active = true; 468 active = true;
355 state = IDLE; 469 state = IDLE;
356 } 470 }
357 else 471 else
358 { 472 {
359 active = false; 473 active = false;
360 state = ESTABLISHED; 474 state = ESTABLISHED;
361 start (fd, POLLIN); 475 start (fd, EV_READ);
362 } 476 }
363} 477}
364 478
365tcp_connection::~tcp_connection () 479tcp_connection::~tcp_connection ()
366{ 480{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines