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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines