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.2 by pcg, Sun Apr 6 18:12:18 2003 UTC vs.
Revision 1.10 by pcg, Thu Oct 16 02:41:21 2003 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 Marc Lehmann <pcg@goof.com>
3 4
4 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 6 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 7 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version. 8 (at your option) any later version.
27 28
28#include <cstring> 29#include <cstring>
29 30
30#include <sys/types.h> 31#include <sys/types.h>
31#include <sys/socket.h> 32#include <sys/socket.h>
32#include <sys/poll.h>
33#include <sys/wait.h> 33#include <sys/wait.h>
34#include <netinet/in.h>
35#include <sys/uio.h> 34#include <sys/uio.h>
36#include <arpa/inet.h>
37#include <errno.h> 35#include <errno.h>
38#include <time.h> 36#include <time.h>
39#include <unistd.h> 37#include <unistd.h>
38#include <fcntl.h>
40 39
41#include <map> 40#include <map>
42#include <unistd.h> 41
43#include <fcntl.h> 42#include "netcompat.h"
44#include <sys/poll.h>
45 43
46#include "vpn.h" 44#include "vpn.h"
45
46#if ENABLE_HTTP_PROXY
47# include "conf.h"
48#endif
47 49
48struct tcp_connection; 50struct tcp_connection;
49 51
50struct lt_sockinfo 52struct lt_sockinfo
51{ 53{
68struct tcp_connection : io_watcher { 70struct tcp_connection : io_watcher {
69 tstamp last_activity; 71 tstamp last_activity;
70 const sockinfo si; 72 const sockinfo si;
71 vpn &v; 73 vpn &v;
72 bool active; // this connection has been actively established 74 bool active; // this connection has been actively established
73 enum { ERROR, IDLE, CONNECTING, ESTABLISHED } state; 75 enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state;
74 76
75 vpn_packet *r_pkt; 77 vpn_packet *r_pkt;
76 u32 r_len, r_ofs; 78 u32 r_len, r_ofs;
77 79
80 vpn_packet *w_pkt;
81 u32 w_len, w_ofs;
82
83#if ENABLE_HTTP_PROXY
84 char *proxy_req;
85 int proxy_req_len;
86#endif
87
78 void tcpv4_ev (io_watcher &w, short revents); 88 void tcpv4_ev (io_watcher &w, short revents);
79 89
80 bool send_packet (vpn_packet *pkt, int tos); 90 bool send_packet (vpn_packet *pkt, int tos);
91 bool write_packet ();
81 92
82 void error (); // abort conenction && cleanup 93 void error (); // abort conenction && cleanup
83 94
84 operator tcp_si_map::value_type() 95 operator tcp_si_map::value_type()
85 { 96 {
146 i = info->second; 157 i = info->second;
147 158
148 return i->send_packet (pkt, tos); 159 return i->send_packet (pkt, tos);
149} 160}
150 161
162bool
163tcp_connection::write_packet ()
164{
165 ssize_t len;
166
167 if (w_ofs < 2)
168 {
169 u16 plen = htons (w_pkt->len);
170
171 iovec vec[2];
172 //TODO: char* is the right type? hardly...
173 vec[0].iov_base = (char *)((u8 *)&plen) + w_ofs;
174 vec[0].iov_len = 2 - w_ofs;
175 vec[1].iov_base = (char *)&((*w_pkt)[0]);
176 vec[1].iov_len = w_len - 2;
177
178 len = writev (fd, vec, 2);
179 }
180 else
181 len = write (fd, &((*w_pkt)[w_ofs - 2]), w_len);
182
183 if (len > 0)
184 {
185 w_ofs += len;
186 w_len -= len;
187
188 return w_len == 0;
189 }
190 else if (len < 0 && (errno == EAGAIN || errno == EINTR))
191 return false;
192 else
193 {
194 error ();
195 return false;
196 }
197}
198
199void
200tcp_connection::tcpv4_ev (io_watcher &w, short revents)
201{
202 last_activity = NOW;
203
204 if (revents & (POLLERR | POLLHUP))
205 {
206 error ();
207 return;
208 }
209
210 if (revents & POLLOUT)
211 {
212 if (state == CONNECTING)
213 {
214 state = ESTABLISHED;
215 set (POLLIN);
216#if ENABLE_HTTP_PROXY
217 if (::conf.proxy_host && ::conf.proxy_port)
218 {
219 state = CONNECTING_PROXY;
220 write (fd, proxy_req, proxy_req_len);
221 free (proxy_req); proxy_req = 0;
222 }
223#endif
224 }
225 else if (state == ESTABLISHED)
226 {
227 if (w_pkt)
228 {
229 if (write_packet ())
230 {
231 delete w_pkt; w_pkt = 0;
232
233 set (POLLIN);
234 }
235 }
236 else
237 set (POLLIN);
238 }
239 else
240 set (POLLIN);
241 }
242
243 if (revents & POLLIN)
244 {
245 if (state == ESTABLISHED)
246 for (;;)
247 {
248 if (!r_pkt)
249 {
250 r_pkt = new vpn_packet;
251 r_ofs = 0;
252 r_len = 2; // header
253 }
254
255 ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len);
256
257 if (len > 0)
258 {
259 r_len -= len;
260 r_ofs += len;
261
262 if (r_len == 0)
263 {
264 if (r_ofs == 2)
265 {
266 r_len = ntohs (*(u16 *)&((*r_pkt)[0]));
267 r_pkt->len = r_len;
268
269 if (r_len > 0 && r_len < MAXSIZE)
270 continue;
271 }
272 else
273 {
274 v.recv_vpn_packet (r_pkt, si);
275 delete r_pkt;
276 r_pkt = 0;
277
278 continue;
279 }
280 }
281 else
282 break;
283 }
284 else if (len < 0 && (errno == EINTR || errno == EAGAIN))
285 break;
286
287 error ();
288 break;
289 }
290#if ENABLE_HTTP_PROXY
291 else if (state == CONNECTING_PROXY)
292 {
293 fcntl (fd, F_SETFL, 0);
294 char r[1024];
295 int i;
296 bool emptyline = false;
297
298 // we do a blocking read of the response, to hell with it
299 for (i = 0; i < 1023; i++)
300 {
301 int l = read (fd, &r[i], 1);
302
303 if (l <= 0)
304 {
305 error ();
306 return;
307 }
308
309 if (r[i] == '\012')
310 {
311 if (emptyline)
312 break;
313 else
314 emptyline = true;
315 }
316 else if (r[i] != '\015')
317 emptyline = false;
318 }
319
320 fcntl (fd, F_SETFL, O_NONBLOCK);
321
322 if (i < 12)
323 {
324 slog (L_ERR, _("(%s): unable to do proxy-forwarding, short response"),
325 (const char *)si);
326 error ();
327 }
328 else if (r[0] != 'H' || r[1] != 'T' || r[2] != 'T' || r[3] != 'P' || r[4] != '/'
329 || r[5] != '1' // http-major
330 || r[9] != '2') // response
331 {
332 slog (L_ERR, _("(%s): malformed or unexpected proxy response (%.12s)"),
333 (const char *)si, r);
334 error ();
335 }
336 else
337 state = ESTABLISHED;
338 }
339#endif
340 }
341}
342
343bool
344tcp_connection::send_packet (vpn_packet *pkt, int tos)
345{
346 last_activity = NOW;
347
348 if (state == IDLE)
349 {
350 // woaw, the first lost packet ;)
351 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
352
353 if (fd >= 0)
354 {
355 const sockinfo *csi = &si;
356
357#if ENABLE_HTTP_PROXY
358 sockinfo psi;
359
360 if (::conf.proxy_host && ::conf.proxy_port)
361 {
362 psi.set (::conf.proxy_host, ::conf.proxy_port, PROT_TCPv4);
363
364 if (psi.valid ())
365 {
366 csi = &psi;
367
368 proxy_req_len = asprintf (&proxy_req,
369 "CONNECT %s:%d HTTP/1.0\015\012"
370 "%s%s%s" // optional proxy-auth
371 "\015\012",
372 si.ntoa (),
373 ntohs (si.port),
374 ::conf.proxy_auth ? "Proxy-Authorization: Basic " : "",
375 ::conf.proxy_auth ? ::conf.proxy_auth : "",
376 ::conf.proxy_auth ? "\015\012" : "");
377
378 }
379 else
380 slog (L_ERR, _("unable to resolve http proxy hostname '%s', trying direct"),
381 ::conf.proxy_host);
382 }
383#endif
384
385 fcntl (fd, F_SETFL, O_NONBLOCK);
386
387 if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0
388 || errno == EINPROGRESS)
389 {
390 state = CONNECTING;
391 start (fd, POLLOUT);
392 }
393 else
394 close (fd);
395 }
396 }
397 else if (state == ESTABLISHED)
398 {
399 // drop packet if the tcp write buffer is full. this *is* the
400 // right thing to do, not using tcp *is* the right thing to do.
401 if (!w_pkt)
402 {
403 // how this maps to the underlying tcp packets we don't know
404 // and we don't care. at least we tried ;)
405#if defined(SOL_IP) && defined(IP_TOS)
406 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
407#endif
408
409 w_pkt = pkt;
410 w_ofs = 0;
411 w_len = pkt->len + 2; // length + size header
412
413 if (write_packet ())
414 w_pkt = 0;
415 else
416 {
417 w_pkt = new vpn_packet;
418 w_pkt->set (*pkt);
419
420 set (POLLIN | POLLOUT);
421 }
422 }
423 }
424
425 return state != ERROR;
426}
427
151void tcp_connection::error () 428void tcp_connection::error ()
152{ 429{
153 if (fd >= 0) 430 if (fd >= 0)
154 { 431 {
155 close (fd); 432 close (fd);
156 fd = -1; 433 fd = -1;
157 } 434 }
158 435
159 delete r_pkt; 436 delete r_pkt; r_pkt = 0;
160 r_pkt = 0; 437 delete w_pkt; w_pkt = 0;
438#if ENABLE_HTTP_PROXY
439 free (proxy_req); proxy_req = 0;
440#endif
161 441
162 stop (); 442 stop ();
163 state = active ? IDLE : ERROR; 443 state = active ? IDLE : ERROR;
164} 444}
165 445
166void
167tcp_connection::tcpv4_ev (io_watcher &w, short revents)
168{
169 last_activity = NOW;
170
171 if (revents & (POLLERR | POLLHUP))
172 error ();
173 else if (revents & POLLOUT && state == CONNECTING)
174 {
175 state = ESTABLISHED;
176 set (POLLIN);
177 }
178
179 else if (revents & POLLIN)
180 {
181 for (;;)
182 {
183 if (!r_pkt)
184 {
185 r_pkt = new vpn_packet;
186 r_ofs = 0;
187 r_len = 2; // header
188 }
189
190 ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len);
191
192 if (len > 0)
193 {
194 r_len -= len;
195 r_ofs += len;
196
197 if (r_len == 0)
198 {
199 if (r_ofs == 2)
200 {
201 r_len = ntohs (*(u16 *)&((*r_pkt)[0]));
202 r_pkt->len = r_len;
203
204 if (r_len > 0 && r_len < MAXSIZE)
205 continue;
206 }
207 else
208 {
209 v.recv_vpn_packet (r_pkt, si);
210 delete r_pkt;
211 r_pkt = 0;
212
213 continue;
214 }
215 }
216
217 }
218 else if (len < 0 && (errno == EINTR || errno == EAGAIN))
219 return;
220
221 error ();
222 return;
223 }
224 }
225}
226
227bool
228tcp_connection::send_packet (vpn_packet *pkt, int tos)
229{
230 last_activity = NOW;
231
232 if (state == IDLE)
233 {
234 // woaw, the first lost packet ;)
235 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
236
237 if (fd >= 0)
238 {
239 fcntl (fd, F_SETFL, O_NONBLOCK);
240
241 if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0
242 || errno == EINPROGRESS)
243 {
244 state = CONNECTING;
245 start (fd, POLLOUT);
246 }
247 else
248 close (fd);
249 }
250 }
251 else if (state == ESTABLISHED)
252 {
253 // how this maps to the underlying tcp packet we don't know
254 // and we don't care. at least we tried ;)
255 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
256
257 // we use none of the advantages of tcp; if an error occurs, just drop
258 // (this happens when a tcp connection gets stuck, too, which might not be
259 // the wisest thing to do.. either drop packet (too late) or make sure
260 // it gets delivered)
261 u16 len = htons (pkt->len);
262
263 iovec vec[2];
264 vec[0].iov_base = &len;
265 vec[0].iov_len = sizeof len;
266 vec[1].iov_base = &((*pkt)[0]);
267 vec[1].iov_len = pkt->len;
268
269 if (sizeof (u16) + pkt->len != writev (fd, vec, 2))
270 error ();
271 }
272
273 return state != ERROR;
274}
275
276tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) 446tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_)
277: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) 447: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev)
278{ 448{
279 last_activity = NOW; 449 last_activity = NOW;
280 r_pkt = 0; 450 r_pkt = 0;
451 w_pkt = 0;
281 fd = fd_; 452 fd = fd_;
453#if ENABLE_HTTP_PROXY
454 proxy_req = 0;
455#endif
282 456
283 if (fd < 0) 457 if (fd < 0)
284 { 458 {
285 active = true; 459 active = true;
286 state = IDLE; 460 state = IDLE;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines