ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_tcp.C
Revision: 1.11
Committed: Sat Jan 17 01:18:36 2004 UTC (20 years, 5 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: VPE_1_4, VPE_1_6, rel-1_7, VPE-1_6_1
Changes since 1.10: +21 -19 lines
Log Message:
*** empty log message ***

File Contents

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