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.2 by pcg, Wed Apr 2 05:15:00 2003 UTC vs.
Revision 1.3 by pcg, Wed Apr 2 21:02:25 2003 UTC

87 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 87 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
88 88
89 if (udpv4_fd < 0) 89 if (udpv4_fd < 0)
90 return -1; 90 return -1;
91 91
92 // standard daemon practise...
93 {
94 int oval = 1;
95 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
96 }
97
92 sockinfo si (THISNODE, PROT_UDPv4); 98 sockinfo si (THISNODE, PROT_UDPv4);
93 99
94 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) 100 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
95 { 101 {
96 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); 102 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
105 int oval = IP_PMTUDISC_DONT; 111 int oval = IP_PMTUDISC_DONT;
106 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); 112 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
107 } 113 }
108#endif 114#endif
109 115
110 // standard daemon practise...
111 {
112 int oval = 1;
113 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
114 }
115
116 udpv4_ev_watcher.start (udpv4_fd, POLLIN); 116 udpv4_ev_watcher.start (udpv4_fd, POLLIN);
117 } 117 }
118 118
119 ipv4_fd = -1; 119 ipv4_fd = -1;
120 if (THISNODE->protocols & PROT_IPv4) 120 if (THISNODE->protocols & PROT_IPv4)
151 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 151 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
152 152
153 if (tcpv4_fd < 0) 153 if (tcpv4_fd < 0)
154 return -1; 154 return -1;
155 155
156 sockinfo si (THISNODE, PROT_TCPv4);
157
158 if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ()))
159 {
160 slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno));
161 exit (1);
162 }
163
164 if (listen (tcpv4_fd, 5))
165 {
166 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno));
167 exit (1);
168 }
169
170 // standard daemon practise... 156 // standard daemon practise...
171 { 157 {
172 int oval = 1; 158 int oval = 1;
173 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 159 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
174 } 160 }
175 161
162 sockinfo si (THISNODE, PROT_TCPv4);
163
164 if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ()))
165 {
166 slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno));
167 exit (1);
168 }
169
170 if (listen (tcpv4_fd, 5))
171 {
172 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno));
173 exit (1);
174 }
175
176 tcpv4_accept_watcher.start (tcpv4_fd, POLLIN); 176 tcpv4_ev_watcher.start (tcpv4_fd, POLLIN);
177 } 177 }
178#endif 178#endif
179 179
180 tap = new tap_device (); 180 tap = new tap_device ();
181 if (!tap) //D this, of course, never catches 181 if (!tap) //D this, of course, never catches
239 c->recv_vpn_packet (pkt, rsi); 239 c->recv_vpn_packet (pkt, rsi);
240 } 240 }
241} 241}
242 242
243void 243void
244vpn::udpv4_ev (int fd, short revents) 244vpn::udpv4_ev (io_watcher &w, short revents)
245{ 245{
246 if (revents & (POLLIN | POLLERR)) 246 if (revents & (POLLIN | POLLERR))
247 { 247 {
248 vpn_packet *pkt = new vpn_packet; 248 vpn_packet *pkt = new vpn_packet;
249 struct sockaddr_in sa; 249 struct sockaddr_in sa;
250 socklen_t sa_len = sizeof (sa); 250 socklen_t sa_len = sizeof (sa);
251 int len; 251 int len;
252 252
253 len = recvfrom (fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 253 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
254 254
255 sockinfo si(sa); 255 sockinfo si(sa);
256 256
257 if (len > 0) 257 if (len > 0)
258 { 258 {
282 exit (1); 282 exit (1);
283 } 283 }
284} 284}
285 285
286void 286void
287vpn::ipv4_ev (int fd, short revents) 287vpn::ipv4_ev (io_watcher &w, short revents)
288{ 288{
289 if (revents & (POLLIN | POLLERR)) 289 if (revents & (POLLIN | POLLERR))
290 { 290 {
291 vpn_packet *pkt = new vpn_packet; 291 vpn_packet *pkt = new vpn_packet;
292 struct sockaddr_in sa; 292 struct sockaddr_in sa;
293 socklen_t sa_len = sizeof (sa); 293 socklen_t sa_len = sizeof (sa);
294 int len; 294 int len;
295 295
296 len = recvfrom (fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 296 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
297 297
298 sockinfo si(sa, PROT_IPv4); 298 sockinfo si(sa, PROT_IPv4);
299 299
300 if (len > 0) 300 if (len > 0)
301 { 301 {
330 } 330 }
331} 331}
332 332
333#if ENABLE_TCP 333#if ENABLE_TCP
334 334
335struct tcp_info { 335struct tcp_connection;
336 int fd; 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;
337 bool ok; 359 bool ok;
338 io_watcher r;
339 360
340 tcp_info (vpn *v) 361 void tcpv4_ev (io_watcher &w, short revents);
341 : r(v, &vpn::tcpv4_ev) 362
342 { 363 operator tcp_si_map::value_type()
343 fd = -1;
344 } 364 {
365 return tcp_si_map::value_type (&si, this);
366 }
345 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
346 ~tcp_info () { close (fd); } 375 ~tcp_connection () { close (fd); }
347}; 376};
348 377
349typedef map<sockinfo, tcp_info *> tcp_si_map; 378void tcp_si_map::cleaner_cb (time_watcher &w)
350typedef map<int, tcp_info *> tcp_fd_map; // unneecssary if iom would be cooler 379{
351static tcp_si_map tcp_si; 380 w.at = NOW + 600;
352static tcp_fd_map tcp_fd; 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}
353 392
354void 393void
355vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 394vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
356{ 395{
357 tcp_si_map::iterator info = tcp_si.find (si); 396 tcp_si_map::iterator info = tcp_si.find (&si);
358 397
359 if (info == tcp_si.end ()) 398 if (info == tcp_si.end ())
360 { 399 {
361 // woaw, the first lost packet ;) 400 // woaw, the first lost packet ;)
362 tcp_info *i = new tcp_info (this);
363
364 i->ok = false;
365 i->fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 401 int fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
366 402
367 if (i->fd >= 0) 403 if (fd >= 0)
368 { 404 {
369 fcntl (i->fd, F_SETFL, O_NONBLOCK); 405 fcntl (fd, F_SETFL, O_NONBLOCK);
370 406
371 if (connect (i->fd, si.sav4 (), si.salenv4 ()) >= 0 407 if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0
372 || errno == EINPROGRESS) 408 || errno == EINPROGRESS)
373 { 409 {
374 tcp_si.insert (tcp_si_map::value_type (si, i)); 410 tcp_connection *i = new tcp_connection (fd, si, *this);
375 tcp_fd.insert (tcp_fd_map::value_type (i->fd, i)); 411
376 return; 412 tcp_si.insert (*i);
377 } 413 }
378
379 delete i; 414 else
415 close (fd);
380 } 416 }
381 } 417 }
382 else 418 else
383 { 419 {
384 tcp_info *i = info->second; 420 tcp_connection *i = info->second;
421
422 i->last_activity = NOW;
385 423
386 if (i->ok) 424 if (i->ok)
387 { 425 {
388 setsockopt (i->fd, SOL_IP, IP_TOS, &tos, sizeof tos); 426 setsockopt (i->fd, SOL_IP, IP_TOS, &tos, sizeof tos);
389 427
428 // we use none of the advantages of tcp
390 if (write (i->fd, (void *)pkt, pkt->len + sizeof (u32)) != pkt->len + sizeof (u32)) 429 write (i->fd, (void *)pkt, pkt->len + sizeof (u32)) != pkt->len + sizeof (u32);
391 {
392 // error, close socket, forget it and retry immediately
393 tcp_si.erase (info);
394 tcp_fd.erase (i->fd);
395 delete i;
396
397 // tail recursion is... better than goto(?)
398 send_tcpv4_packet (pkt, si, tos);
399 }
400 } 430 }
401 } 431 }
402 432
403#if 0 433#if 0
404 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 434 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
405 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 435 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
406#endif 436#endif
407} 437}
408 438
409void 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
410vpn::tcpv4_accept (int fd, short revents) 477vpn::tcpv4_ev (io_watcher &w, short revents)
411{ 478{
412 if (revents & (POLLIN | POLLERR)) 479 if (revents & (POLLIN | POLLERR))
413 { 480 {
414 struct sockaddr_in sa; 481 struct sockaddr_in sa;
415 socklen_t sa_len = sizeof (sa); 482 socklen_t sa_len = sizeof (sa);
416 int len; 483 int len;
417 484
418 fd = accept (fd, (sockaddr *)&sa, &sa_len); 485 int fd = accept (w.fd, (sockaddr *)&sa, &sa_len);
419 486
420 if (fd >= 0) 487 if (fd >= 0)
421 { 488 {
422 fcntl (fd, F_SETFL, O_NONBLOCK); 489 fcntl (fd, F_SETFL, O_NONBLOCK);
423 490
424 sockinfo si(sa, PROT_TCPv4); 491 sockinfo si(sa, PROT_TCPv4);
425 tcp_info *i = new tcp_info (this); 492 tcp_connection *i = new tcp_connection (fd, si, *this);
426 493
427 i->fd = fd; 494 slog (L_ERR, "accepted %d\n", fd);//D
428 i->ok = true;
429 i->r.start (fd, POLLIN);
430 495
431 tcp_si.insert (tcp_si_map::value_type (si, i));
432 tcp_fd.insert (tcp_fd_map::value_type (fd, i));
433 }
434 }
435}
436
437void
438vpn::tcpv4_ev (int fd, short revents)
439{
440 if (revents & (POLLIN | POLLERR))
441 {
442 tcp_fd_map::iterator info = tcp_fd.find (fd);
443
444 if (info != tcp_fd.end ())
445 {
446 tcp_info *i = info->second;
447
448 if (!i->ok) // not yet established
449 {
450 i->ok = true;
451 fcntl (i->fd, F_SETFL, 0);
452 }
453
454 u32 len;
455
456 if (sizeof (len) == read (fd, &len, sizeof (len)))
457 {
458 slog (L_ERR, "%d bytes received\n", len);
459 }
460
461 //tcp_si.erase (i); 496 tcp_si.insert (*i);
462 tcp_fd.erase (fd);
463 delete i;
464 } 497 }
465 } 498 }
466} 499}
467 500
468#endif 501#endif
469 502
470void 503void
471vpn::tap_ev (int fd, short revents) 504vpn::tap_ev (io_watcher &w, short revents)
472{ 505{
473 if (revents & POLLIN) 506 if (revents & POLLIN)
474 { 507 {
475 /* process data */ 508 /* process data */
476 tap_packet *pkt; 509 tap_packet *pkt;
526 else 559 else
527 abort (); 560 abort ();
528} 561}
529 562
530void 563void
531vpn::event_cb (tstamp &ts) 564vpn::event_cb (time_watcher &w)
532{ 565{
533 if (events) 566 if (events)
534 { 567 {
535 if (events & EVENT_SHUTDOWN) 568 if (events & EVENT_SHUTDOWN)
536 { 569 {
553 } 586 }
554 587
555 events = 0; 588 events = 0;
556 } 589 }
557 590
558 ts = TSTAMP_CANCEL; 591 w.at = TSTAMP_CANCEL;
559} 592}
560 593
561void 594void
562vpn::shutdown_all () 595vpn::shutdown_all ()
563{ 596{
649: event(this, &vpn::event_cb) 682: event(this, &vpn::event_cb)
650, udpv4_ev_watcher(this, &vpn::udpv4_ev) 683, udpv4_ev_watcher(this, &vpn::udpv4_ev)
651, ipv4_ev_watcher (this, &vpn::ipv4_ev) 684, ipv4_ev_watcher (this, &vpn::ipv4_ev)
652, tap_ev_watcher (this, &vpn::tap_ev) 685, tap_ev_watcher (this, &vpn::tap_ev)
653#if ENABLE_TCP 686#if ENABLE_TCP
654, tcpv4_accept_watcher(this, &vpn::tcpv4_accept) 687, tcpv4_ev_watcher(this, &vpn::tcpv4_ev)
655#endif 688#endif
656{ 689{
657} 690}
658 691
659vpn::~vpn () 692vpn::~vpn ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines