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.9 by pcg, Wed Oct 15 00:41:59 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines