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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines