ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_tcp.C
Revision: 1.19
Committed: Tue Nov 13 02:12:08 2007 UTC (16 years, 6 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.18: +4 -6 lines
Log Message:
do not leak tcp conenctions

File Contents

# User Rev Content
1 pcg 1.1 /*
2     vpn_tcp.C -- handle the tcp part of the protocol.
3 pcg 1.18 Copyright (C) 2003-2007 Marc Lehmann <gvpe@schmorp.de>
4 pcg 1.1
5 pcg 1.12 This file is part of GVPE.
6    
7     GVPE is free software; you can redistribute it and/or modify
8 pcg 1.1 it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18 pcg 1.12 along with gvpe; if not, write to the Free Software
19 pcg 1.14 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 pcg 1.1 */
21    
22     #include "config.h"
23    
24     #if ENABLE_TCP
25    
26 pcg 1.19 // tcp processing is extremely ugly, since the gvpe protocol is simply
27 pcg 1.1 // designed for unreliable datagram networks. tcp is implemented by
28     // multiplexing packets over tcp. errors are completely ignored, as we
29 pcg 1.19 // rely on the higher level layers to time out and reconnect.
30 pcg 1.1
31     #include <cstring>
32    
33     #include <sys/types.h>
34     #include <sys/socket.h>
35     #include <sys/wait.h>
36     #include <sys/uio.h>
37     #include <errno.h>
38     #include <time.h>
39     #include <unistd.h>
40 pcg 1.9 #include <fcntl.h>
41 pcg 1.1
42     #include <map>
43 pcg 1.8
44     #include "netcompat.h"
45 pcg 1.1
46     #include "vpn.h"
47    
48 pcg 1.4 #if ENABLE_HTTP_PROXY
49     # include "conf.h"
50     #endif
51    
52 pcg 1.1 struct tcp_connection;
53    
54     struct lt_sockinfo
55     {
56     bool operator()(const sockinfo *a, const sockinfo *b) const
57     {
58     return *a < *b;
59     }
60     };
61    
62     struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo> {
63 pcg 1.17 void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner;
64 pcg 1.1
65     tcp_si_map ()
66 pcg 1.13 : cleaner(this, &tcp_si_map::cleaner_cb)
67 pcg 1.17 {
68 pcg 1.19 cleaner.start (::conf.keepalive / 2, ::conf.keepalive / 2);
69 pcg 1.17 }
70 pcg 1.13
71 pcg 1.1 } tcp_si;
72    
73 pcg 1.17 struct tcp_connection : ev::io {
74 pcg 1.1 tstamp last_activity;
75     const sockinfo si;
76     vpn &v;
77     bool active; // this connection has been actively established
78 pcg 1.4 enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state;
79 pcg 1.1
80     vpn_packet *r_pkt;
81     u32 r_len, r_ofs;
82    
83 pcg 1.3 vpn_packet *w_pkt;
84     u32 w_len, w_ofs;
85    
86 pcg 1.4 #if ENABLE_HTTP_PROXY
87     char *proxy_req;
88     int proxy_req_len;
89     #endif
90    
91 pcg 1.17 void tcpv4_ev (ev::io &w, int revents);
92 pcg 1.1
93 pcg 1.2 bool send_packet (vpn_packet *pkt, int tos);
94 pcg 1.3 bool write_packet ();
95 pcg 1.1
96     void error (); // abort conenction && cleanup
97    
98     operator tcp_si_map::value_type()
99 pcg 1.13 {
100     return tcp_si_map::value_type (&si, this);
101     }
102 pcg 1.1
103     tcp_connection (int fd_, const sockinfo &si_, vpn &v_);
104     ~tcp_connection ();
105     };
106    
107 pcg 1.17 void tcp_si_map::cleaner_cb (ev::timer &w, int revents)
108 pcg 1.1 {
109 pcg 1.17 tstamp to = ev::ev_now () - ::conf.keepalive - 30 - 60;
110 pcg 1.1
111     for (iterator i = begin (); i != end(); )
112     if (i->second->last_activity >= to)
113     ++i;
114     else
115     {
116 pcg 1.19 delete i->second;
117 pcg 1.1 erase (i);
118     i = begin ();
119     }
120     }
121    
122     void
123 pcg 1.17 vpn::tcpv4_ev (ev::io &w, int revents)
124 pcg 1.1 {
125 pcg 1.17 if (revents & EV_READ)
126 pcg 1.1 {
127     struct sockaddr_in sa;
128     socklen_t sa_len = sizeof (sa);
129     int len;
130    
131     int fd = accept (w.fd, (sockaddr *)&sa, &sa_len);
132    
133     if (fd >= 0)
134     {
135 pcg 1.16 fcntl (fd, F_SETFL, O_NONBLOCK);
136     fcntl (fd, F_SETFD, FD_CLOEXEC);
137    
138 pcg 1.1 sockinfo si(sa, PROT_TCPv4);
139    
140     slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D
141    
142     tcp_connection *i = new tcp_connection (fd, si, *this);
143     tcp_si.insert (*i);
144     }
145     }
146     }
147    
148 pcg 1.2 bool
149 pcg 1.1 vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
150     {
151     tcp_si_map::iterator info = tcp_si.find (&si);
152    
153     tcp_connection *i;
154    
155     if (info == tcp_si.end ())
156     {
157     i = new tcp_connection (-1, si, *this);
158     tcp_si.insert (*i);
159     }
160     else
161     i = info->second;
162    
163 pcg 1.2 return i->send_packet (pkt, tos);
164 pcg 1.1 }
165    
166 pcg 1.3 bool
167     tcp_connection::write_packet ()
168     {
169     ssize_t len;
170    
171     if (w_ofs < 2)
172     {
173     u16 plen = htons (w_pkt->len);
174    
175     iovec vec[2];
176 pcg 1.7 //TODO: char* is the right type? hardly...
177     vec[0].iov_base = (char *)((u8 *)&plen) + w_ofs;
178 pcg 1.3 vec[0].iov_len = 2 - w_ofs;
179 pcg 1.7 vec[1].iov_base = (char *)&((*w_pkt)[0]);
180 pcg 1.3 vec[1].iov_len = w_len - 2;
181    
182     len = writev (fd, vec, 2);
183     }
184     else
185     len = write (fd, &((*w_pkt)[w_ofs - 2]), w_len);
186    
187     if (len > 0)
188     {
189     w_ofs += len;
190     w_len -= len;
191    
192     return w_len == 0;
193     }
194     else if (len < 0 && (errno == EAGAIN || errno == EINTR))
195     return false;
196     else
197     {
198     error ();
199     return false;
200     }
201     }
202    
203 pcg 1.1 void
204 pcg 1.17 tcp_connection::tcpv4_ev (ev::io &w, int revents)
205 pcg 1.1 {
206 pcg 1.17 last_activity = ev::ev_now ();
207 pcg 1.1
208 pcg 1.17 if (revents & EV_WRITE)
209 pcg 1.3 {
210     if (state == CONNECTING)
211     {
212     state = ESTABLISHED;
213 pcg 1.17 set (EV_READ);
214 pcg 1.4 #if ENABLE_HTTP_PROXY
215     if (::conf.proxy_host && ::conf.proxy_port)
216     {
217     state = CONNECTING_PROXY;
218 pcg 1.11
219     if (write (fd, proxy_req, proxy_req_len) == 0)
220     {
221     error ();
222     return;
223     }
224    
225 pcg 1.4 free (proxy_req); proxy_req = 0;
226     }
227     #endif
228 pcg 1.3 }
229     else if (state == ESTABLISHED)
230     {
231     if (w_pkt)
232     {
233     if (write_packet ())
234     {
235     delete w_pkt; w_pkt = 0;
236    
237 pcg 1.17 set (EV_READ);
238 pcg 1.3 }
239     }
240     else
241 pcg 1.17 set (EV_READ);
242 pcg 1.3 }
243     else
244 pcg 1.17 set (EV_READ);
245 pcg 1.3 }
246    
247 pcg 1.17 if (revents & EV_READ)
248 pcg 1.1 {
249 pcg 1.4 if (state == ESTABLISHED)
250     for (;;)
251     {
252     if (!r_pkt)
253     {
254     r_pkt = new vpn_packet;
255     r_ofs = 0;
256     r_len = 2; // header
257     }
258    
259     ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len);
260    
261     if (len > 0)
262     {
263     r_len -= len;
264     r_ofs += len;
265    
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 pcg 1.11 // len == 0 <-> EOF
292 pcg 1.4 error ();
293     break;
294     }
295     #if ENABLE_HTTP_PROXY
296     else if (state == CONNECTING_PROXY)
297 pcg 1.1 {
298 pcg 1.4 fcntl (fd, F_SETFL, 0);
299     char r[1024];
300     int i;
301     bool emptyline = false;
302 pcg 1.1
303 pcg 1.4 // we do a blocking read of the response, to hell with it
304     for (i = 0; i < 1023; i++)
305 pcg 1.1 {
306 pcg 1.4 int l = read (fd, &r[i], 1);
307 pcg 1.1
308 pcg 1.4 if (l <= 0)
309 pcg 1.1 {
310 pcg 1.4 error ();
311     return;
312     }
313 pcg 1.1
314 pcg 1.4 if (r[i] == '\012')
315     {
316     if (emptyline)
317     break;
318 pcg 1.1 else
319 pcg 1.4 emptyline = true;
320 pcg 1.1 }
321 pcg 1.4 else if (r[i] != '\015')
322     emptyline = false;
323 pcg 1.1 }
324    
325 pcg 1.4 fcntl (fd, F_SETFL, O_NONBLOCK);
326    
327     if (i < 12)
328     {
329 pcg 1.6 slog (L_ERR, _("(%s): unable to do proxy-forwarding, short response"),
330 pcg 1.5 (const char *)si);
331 pcg 1.4 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 pcg 1.6 slog (L_ERR, _("(%s): malformed or unexpected proxy response (%.12s)"),
338 pcg 1.5 (const char *)si, r);
339 pcg 1.4 error ();
340     }
341     else
342     state = ESTABLISHED;
343 pcg 1.1 }
344 pcg 1.4 #endif
345 pcg 1.1 }
346     }
347    
348 pcg 1.2 bool
349 pcg 1.1 tcp_connection::send_packet (vpn_packet *pkt, int tos)
350     {
351 pcg 1.17 last_activity = ev::ev_now ();
352 pcg 1.1
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 pcg 1.4 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    
383     }
384     else
385     slog (L_ERR, _("unable to resolve http proxy hostname '%s', trying direct"),
386     ::conf.proxy_host);
387     }
388     #endif
389    
390 pcg 1.1 fcntl (fd, F_SETFL, O_NONBLOCK);
391 pcg 1.4
392     if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0
393 pcg 1.1 || errno == EINPROGRESS)
394     {
395 pcg 1.16 fcntl (fd, F_SETFL, O_NONBLOCK);
396     fcntl (fd, F_SETFD, FD_CLOEXEC);
397    
398 pcg 1.1 state = CONNECTING;
399 pcg 1.17 start (fd, EV_WRITE);
400 pcg 1.1 }
401     else
402     close (fd);
403     }
404     }
405     else if (state == ESTABLISHED)
406     {
407 pcg 1.3 // drop packet if the tcp write buffer is full. this *is* the
408     // right thing to do, not using tcp *is* the right thing to do.
409     if (!w_pkt)
410     {
411     // how this maps to the underlying tcp packets we don't know
412     // and we don't care. at least we tried ;)
413 pcg 1.7 #if defined(SOL_IP) && defined(IP_TOS)
414 pcg 1.3 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
415 pcg 1.7 #endif
416 pcg 1.3
417     w_pkt = pkt;
418     w_ofs = 0;
419     w_len = pkt->len + 2; // length + size header
420    
421     if (write_packet ())
422     w_pkt = 0;
423     else
424     {
425     w_pkt = new vpn_packet;
426     w_pkt->set (*pkt);
427 pcg 1.1
428 pcg 1.17 set (EV_READ | EV_WRITE);
429 pcg 1.3 }
430     }
431 pcg 1.1 }
432 pcg 1.2
433     return state != ERROR;
434 pcg 1.1 }
435    
436 pcg 1.4 void tcp_connection::error ()
437     {
438 pcg 1.18 stop ();
439    
440 pcg 1.4 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    
455 pcg 1.1 tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_)
456 pcg 1.17 : v(v_), si(si_), ev::io(this, &tcp_connection::tcpv4_ev)
457 pcg 1.1 {
458 pcg 1.17 last_activity = ev::ev_now ();
459 pcg 1.1 r_pkt = 0;
460 pcg 1.3 w_pkt = 0;
461 pcg 1.1 fd = fd_;
462 pcg 1.4 #if ENABLE_HTTP_PROXY
463     proxy_req = 0;
464     #endif
465 pcg 1.1
466     if (fd < 0)
467     {
468     active = true;
469     state = IDLE;
470     }
471     else
472     {
473     active = false;
474     state = ESTABLISHED;
475 pcg 1.17 start (fd, EV_READ);
476 pcg 1.1 }
477     }
478    
479     tcp_connection::~tcp_connection ()
480     {
481     error ();
482     }
483    
484     #endif
485