ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn.C
(Generate patch)

Comparing gvpe/src/vpn.C (file contents):
Revision 1.3 by pcg, Wed Apr 2 21:02:25 2003 UTC vs.
Revision 1.5 by pcg, Sat Apr 5 02:32:40 2003 UTC

38 38
39#include "connection.h" 39#include "connection.h"
40#include "util.h" 40#include "util.h"
41#include "vpn.h" 41#include "vpn.h"
42 42
43#if ENABLE_TCP
44# include <map>
45# include <unistd.h>
46# include <fcntl.h>
47# include <sys/poll.h>
48#endif
49
50///////////////////////////////////////////////////////////////////////////// 43/////////////////////////////////////////////////////////////////////////////
51 44
52const char *vpn::script_if_up () 45const char *vpn::script_if_up ()
53{ 46{
54 // the tunnel device mtu should be the physical mtu - overhead 47 // the tunnel device mtu should be the physical mtu - overhead
191 reconnect_all (); 184 reconnect_all ();
192 185
193 return 0; 186 return 0;
194} 187}
195 188
189// send a vpn packet out to other hosts
190void
191vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
192{
193 switch (si.prot)
194 {
195 case PROT_IPv4:
196 send_ipv4_packet (pkt, si, tos);
197 break;
198
199 case PROT_UDPv4:
200 send_udpv4_packet (pkt, si, tos);
201 break;
202
203#if ENABLE_TCP
204 case PROT_TCPv4:
205 send_tcpv4_packet (pkt, si, tos);
206 break;
207#endif
208
209 default:
210 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
211 abort ();
212 }
213}
214
196void 215void
197vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 216vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
198{ 217{
199 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 218 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
200 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 219 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
250 socklen_t sa_len = sizeof (sa); 269 socklen_t sa_len = sizeof (sa);
251 int len; 270 int len;
252 271
253 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 272 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
254 273
255 sockinfo si(sa); 274 sockinfo si(sa, PROT_UDPv4);
256 275
257 if (len > 0) 276 if (len > 0)
258 { 277 {
259 pkt->len = len; 278 pkt->len = len;
260 279
261 recv_vpn_packet (pkt, si); 280 recv_vpn_packet (pkt, si);
262 } 281 }
263 else 282 else
264 { 283 {
265 // probably ECONNRESET or somesuch 284 // probably ECONNRESET or somesuch
266 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 285 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno));
267 } 286 }
268 287
269 delete pkt; 288 delete pkt;
270 } 289 }
271 else if (revents & POLLHUP) 290 else if (revents & POLLHUP)
327 _("FATAL: unknown revents %08x in socket, terminating\n"), 346 _("FATAL: unknown revents %08x in socket, terminating\n"),
328 revents); 347 revents);
329 exit (1); 348 exit (1);
330 } 349 }
331} 350}
332
333#if ENABLE_TCP
334
335struct tcp_connection;
336
337struct lt_sockinfo
338{
339 bool operator()(const sockinfo *a, const sockinfo *b) const
340 {
341 return *a < *b;
342 }
343};
344
345struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo> {
346 void cleaner_cb (time_watcher &w); time_watcher cleaner;
347
348 tcp_si_map ()
349 : cleaner(this, &tcp_si_map::cleaner_cb)
350 {
351 cleaner.start (0);
352 }
353} tcp_si;
354
355struct tcp_connection : io_watcher {
356 tstamp last_activity;
357 const sockinfo si;
358 vpn &v;
359 bool ok;
360
361 void tcpv4_ev (io_watcher &w, short revents);
362
363 operator tcp_si_map::value_type()
364 {
365 return tcp_si_map::value_type (&si, this);
366 }
367
368 tcp_connection (int fd_, const sockinfo &si_, vpn &v_)
369 : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev), ok(false)
370 {
371 last_activity = NOW;
372 start (fd_, POLLOUT);
373 }
374
375 ~tcp_connection () { close (fd); }
376};
377
378void tcp_si_map::cleaner_cb (time_watcher &w)
379{
380 w.at = NOW + 600;
381 tstamp to = NOW - ::conf.keepalive - 30;
382
383 for (iterator i = begin (); i != end(); )
384 if (i->second->last_activity >= to)
385 ++i;
386 else
387 {
388 erase (i);
389 i = begin ();
390 }
391}
392
393void
394vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
395{
396 tcp_si_map::iterator info = tcp_si.find (&si);
397
398 if (info == tcp_si.end ())
399 {
400 // woaw, the first lost packet ;)
401 int fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
402
403 if (fd >= 0)
404 {
405 fcntl (fd, F_SETFL, O_NONBLOCK);
406
407 if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0
408 || errno == EINPROGRESS)
409 {
410 tcp_connection *i = new tcp_connection (fd, si, *this);
411
412 tcp_si.insert (*i);
413 }
414 else
415 close (fd);
416 }
417 }
418 else
419 {
420 tcp_connection *i = info->second;
421
422 i->last_activity = NOW;
423
424 if (i->ok)
425 {
426 setsockopt (i->fd, SOL_IP, IP_TOS, &tos, sizeof tos);
427
428 // we use none of the advantages of tcp
429 write (i->fd, (void *)pkt, pkt->len + sizeof (u32)) != pkt->len + sizeof (u32);
430 }
431 }
432
433#if 0
434 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
435 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
436#endif
437}
438
439void
440tcp_connection::tcpv4_ev (io_watcher &w, short revents)
441{
442 last_activity = NOW;
443
444 if (!ok) // just established?
445 {
446 ok = true;
447 fcntl (fd, F_SETFL, 0);
448 stop ();
449 start (fd, POLLIN);
450 }
451
452 if (revents & (POLLIN | POLLERR))
453 {
454 u32 len;
455
456 if (sizeof (len) == read (fd, &len, sizeof (len)))
457 {
458 vpn_packet *pkt = new vpn_packet;
459
460 if (len == read (fd, &((*pkt)[0]), len))
461 {
462 pkt->len = len;
463
464 v.recv_vpn_packet (pkt, si);
465 return;
466 }
467
468 delete pkt;
469 }
470
471 tcp_si.erase (&si);
472 stop ();
473 }
474}
475
476void
477vpn::tcpv4_ev (io_watcher &w, short revents)
478{
479 if (revents & (POLLIN | POLLERR))
480 {
481 struct sockaddr_in sa;
482 socklen_t sa_len = sizeof (sa);
483 int len;
484
485 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len);
486
487 if (fd >= 0)
488 {
489 fcntl (fd, F_SETFL, O_NONBLOCK);
490
491 sockinfo si(sa, PROT_TCPv4);
492 tcp_connection *i = new tcp_connection (fd, si, *this);
493
494 slog (L_ERR, "accepted %d\n", fd);//D
495
496 tcp_si.insert (*i);
497 }
498 }
499}
500
501#endif
502 351
503void 352void
504vpn::tap_ev (io_watcher &w, short revents) 353vpn::tap_ev (io_watcher &w, short revents)
505{ 354{
506 if (revents & POLLIN) 355 if (revents & POLLIN)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines