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.43 by pcg, Tue Dec 4 15:01:12 2007 UTC vs.
Revision 1.51 by pcg, Sun Aug 10 14:48:57 2008 UTC

1/* 1/*
2 vpn.C -- handle the protocol, encryption, handshaking etc. 2 vpn.C -- handle the protocol, encryption, handshaking etc.
3 Copyright (C) 2003-2007 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify it
8 it under the terms of the GNU General Public License as published by 8 under the terms of the GNU General Public License as published by the
9 the Free Software Foundation; either version 2 of the License, or 9 Free Software Foundation; either version 3 of the License, or (at your
10 (at your option) any later version. 10 option) any later version.
11 11
12 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful, but
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 GNU General Public License for more details. 15 Public License for more details.
16 16
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License along
18 along with gvpe; if not, write to the Free Software 18 with this program; if not, see <http://www.gnu.org/licenses/>.
19 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19
20 Additional permission under GNU GPL version 3 section 7
21
22 If you modify this Program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a modified
24 version of that library), containing parts covered by the terms of the
25 OpenSSL or SSLeay licenses, the licensors of this Program grant you
26 additional permission to convey the resulting work. Corresponding
27 Source for a non-source form of such a combination shall include the
28 source code for the parts of OpenSSL used as well as that of the
29 covered work.
20*/ 30*/
21 31
22#include "config.h" 32#include "config.h"
23 33
24#include <list> 34#include <list>
87 snprintf (ext, 16, "_%d", (*c)->conf->id); 97 snprintf (ext, 16, "_%d", (*c)->conf->id);
88 (*c)->script_init_env (ext); 98 (*c)->script_init_env (ext);
89 } 99 }
90} 100}
91 101
102inline const char *
92const char *vpn::script_if_init () 103vpn::script_if_init ()
93{ 104{
94 script_init_env (); 105 script_init_env ();
95 106
96 return tap->if_up (); 107 return tap->if_up ();
97} 108}
98 109
110inline const char *
99const char *vpn::script_if_up () 111vpn::script_if_up ()
100{ 112{
101 script_init_env (); 113 script_init_env ();
102 114
103 char *filename; 115 char *filename;
104 asprintf (&filename, 116 asprintf (&filename,
110} 122}
111 123
112int 124int
113vpn::setup () 125vpn::setup ()
114{ 126{
127 int success = 0;
128
115 ipv4_tos = -1; 129 ipv4_tos = -1;
116 ipv4_fd = -1; 130 ipv4_fd = -1;
117 131
118 if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) 132 if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto)
119 { 133 {
137 151
138 sockinfo si (THISNODE, PROT_IPv4); 152 sockinfo si (THISNODE, PROT_IPv4);
139 153
140 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) 154 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ()))
141 { 155 {
142 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); 156 slog (L_ERR, _("can't bind ipv4 socket on %s: %s, exiting."), (const char *)si, strerror (errno));
143 exit (EXIT_FAILURE); 157 exit (EXIT_FAILURE);
144 } 158 }
145 159
146 ipv4_ev_watcher.start (ipv4_fd, EV_READ); 160 ipv4_ev_watcher.start (ipv4_fd, EV_READ);
161 ++success;
147 } 162 }
163 else
164 THISNODE->protocols &= ~PROT_IPv4;
148 165
149 udpv4_tos = -1; 166 udpv4_tos = -1;
150 udpv4_fd = -1; 167 udpv4_fd = -1;
151 168
152 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) 169 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port)
177 194
178 sockinfo si (THISNODE, PROT_UDPv4); 195 sockinfo si (THISNODE, PROT_UDPv4);
179 196
180 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) 197 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
181 { 198 {
182 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); 199 slog (L_ERR, _("can't bind udpv4 on %s: %s, exiting."), (const char *)si, strerror (errno));
183 exit (EXIT_FAILURE); 200 exit (EXIT_FAILURE);
184 } 201 }
185 202
186 udpv4_ev_watcher.start (udpv4_fd, EV_READ); 203 udpv4_ev_watcher.start (udpv4_fd, EV_READ);
204 ++success;
187 } 205 }
206 else
207 THISNODE->protocols &= ~PROT_UDPv4;
188 208
189 icmpv4_tos = -1; 209 icmpv4_tos = -1;
190 icmpv4_fd = -1; 210 icmpv4_fd = -1;
191 211
192#if ENABLE_ICMP 212#if ENABLE_ICMP
223 243
224 sockinfo si (THISNODE, PROT_ICMPv4); 244 sockinfo si (THISNODE, PROT_ICMPv4);
225 245
226 if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) 246 if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ()))
227 { 247 {
228 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); 248 slog (L_ERR, _("can't bind icmpv4 on %s: %s, exiting."), (const char *)si, strerror (errno));
229 exit (EXIT_FAILURE); 249 exit (EXIT_FAILURE);
230 } 250 }
231 251
232 icmpv4_ev_watcher.start (icmpv4_fd, EV_READ); 252 icmpv4_ev_watcher.start (icmpv4_fd, EV_READ);
253 ++success;
233 } 254 }
234#endif 255#endif
235 256
236 tcpv4_fd = -1; 257 tcpv4_fd = -1;
237 258
254 275
255 sockinfo si (THISNODE, PROT_TCPv4); 276 sockinfo si (THISNODE, PROT_TCPv4);
256 277
257 if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) 278 if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ()))
258 { 279 {
259 slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); 280 slog (L_ERR, _("can't bind tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno));
260 exit (EXIT_FAILURE); 281 exit (EXIT_FAILURE);
261 } 282 }
262 283
263 if (listen (tcpv4_fd, 5)) 284 if (listen (tcpv4_fd, 5))
264 { 285 {
265 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); 286 slog (L_ERR, _("can't listen tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno));
266 exit (EXIT_FAILURE); 287 exit (EXIT_FAILURE);
267 } 288 }
268 289
269 tcpv4_ev_watcher.start (tcpv4_fd, EV_READ); 290 tcpv4_ev_watcher.start (tcpv4_fd, EV_READ);
291 ++success;
270 } 292 }
293 else
294 THISNODE->protocols &= ~PROT_TCPv4;
271#endif 295#endif
272 296
273 dnsv4_tos = -1; 297 dnsv4_tos = -1;
274 dnsv4_fd = -1; 298 dnsv4_fd = -1;
275 299
306 THISNODE->dns_hostname ? THISNODE->dns_port : 0, 330 THISNODE->dns_hostname ? THISNODE->dns_port : 0,
307 PROT_DNSv4); 331 PROT_DNSv4);
308 332
309 if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) 333 if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ()))
310 { 334 {
311 slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); 335 slog (L_ERR, _("can't bind dnsv4 on %s: %s, exiting."), (const char *)si, strerror (errno));
312 exit (EXIT_FAILURE); 336 exit (EXIT_FAILURE);
313 } 337 }
314 338
315 dnsv4_ev_watcher.start (dnsv4_fd, EV_READ); 339 dnsv4_ev_watcher.start (dnsv4_fd, EV_READ);
340 ++success;
316 } 341 }
317#endif 342#endif
318 343
319 ///////////////////////////////////////////////////////////////////////////// 344 /////////////////////////////////////////////////////////////////////////////
345
346 if (!success)
347 {
348 slog (L_ERR, _("no protocols enabled, exiting."));
349 exit (EXIT_FAILURE);
350 }
320 351
321 reconnect_all (); 352 reconnect_all ();
322 353
323 ///////////////////////////////////////////////////////////////////////////// 354 /////////////////////////////////////////////////////////////////////////////
324 355
325 tap = new tap_device (); 356 tap = new tap_device ();
326 if (!tap) //D this, of course, never catches 357 if (!tap) //D this, of course, never catches
327 { 358 {
328 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); 359 slog (L_ERR, _("cannot create network interface '%s', exiting."), conf.ifname);
329 exit (EXIT_FAILURE); 360 exit (EXIT_FAILURE);
330 } 361 }
331 362
332 fcntl (tap->fd, F_SETFD, FD_CLOEXEC); 363 fcntl (tap->fd, F_SETFD, FD_CLOEXEC);
333 364
334 run_script_cb cb; 365 run_script_cb cb;
366 cb.set<vpn, &vpn::script_if_init> (this);
335 367
336 callback_set (cb, this, vpn, script_if_init);
337 if (tap->if_up () && 368 if (tap->if_up () &&
338 !run_script (cb, true)) 369 !run_script (cb, true))
339 { 370 {
340 slog (L_ERR, _("interface initialization command '%s' failed, exiting."), 371 slog (L_ERR, _("interface initialization command '%s' failed, exiting."),
341 tap->if_up ()); 372 tap->if_up ());
342 exit (EXIT_FAILURE); 373 exit (EXIT_FAILURE);
343 } 374 }
344 375
345 callback_set (cb, this, vpn, script_if_up); 376 cb.set<vpn, &vpn::script_if_up> (this);
346 if (!run_script (cb, true)) 377 if (!run_script (cb, true))
347 { 378 {
348 slog (L_ERR, _("if-up command execution failed, exiting.")); 379 slog (L_ERR, _("if-up command execution failed, exiting."));
349 exit (EXIT_FAILURE); 380 exit (EXIT_FAILURE);
350 } 381 }
427 { 458 {
428 // broadcast, this is ugly, but due to the security policy 459 // broadcast, this is ugly, but due to the security policy
429 // we have to connect to all hosts... 460 // we have to connect to all hosts...
430 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) 461 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
431 if ((*c)->conf != THISNODE) 462 if ((*c)->conf != THISNODE)
432 (*c)->inject_data_packet (pkt, true); 463 (*c)->inject_data_packet (pkt);
433 } 464 }
434} 465}
435 466
436void 467void
437vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 468vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
438{ 469{
439 unsigned int src = pkt->src (); 470 unsigned int src = pkt->src ();
440 unsigned int dst = pkt->dst (); 471 unsigned int dst = pkt->dst ();
441 472
442 slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), 473 slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d."),
443 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); 474 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len);
444 475
445 if (src == 0 || src > conns.size () 476 if (src == 0 || src > conns.size ()
446 || dst > conns.size () 477 || dst > conns.size ()
447 || pkt->typ () >= vpn_packet::PT_MAX) 478 || pkt->typ () >= vpn_packet::PT_MAX)
448 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), 479 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."),
449 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 480 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
450 else if (dst > conns.size ()) 481 else if (dst > conns.size ())
451 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), 482 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."),
452 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 483 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
453 else 484 else
454 { 485 {
455 connection *c = conns[src - 1]; 486 connection *c = conns[src - 1];
456 487
457 if (dst == 0) 488 if (dst == 0)
458 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), 489 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)."),
459 c->conf->nodename, (const char *)rsi); 490 c->conf->nodename, (const char *)rsi);
460 else if (dst != THISNODE->id) 491 else if (dst != THISNODE->id)
461 { 492 {
462 if (THISNODE->routerprio) 493 if (THISNODE->routerprio)
463 // the tos setting gets lost here. who cares. 494 // the tos setting gets lost here. who cares.
464 conns[dst - 1]->inject_vpn_packet (pkt); 495 conns[dst - 1]->inject_vpn_packet (pkt);
465 else 496 else
466 slog (L_WARN, 497 slog (L_WARN,
467 _("%s(%s): forwarding request (=> %s), but we are no router"), 498 _("%s(%s): request to forward packet to %s, but we are no router (config mismatch?)."),
468 c->conf->nodename, (const char *)rsi, 499 c->conf->nodename, (const char *)rsi,
469 conns[dst - 1]->conf->nodename); 500 conns[dst - 1]->conf->nodename);
470 } 501 }
471 else 502 else
472 c->recv_vpn_packet (pkt, rsi); 503 c->recv_vpn_packet (pkt, rsi);
495#if ENABLE_DNS 526#if ENABLE_DNS
496 case PROT_DNSv4: 527 case PROT_DNSv4:
497 return send_dnsv4_packet (pkt, si, tos); 528 return send_dnsv4_packet (pkt, si, tos);
498#endif 529#endif
499 default: 530 default:
500 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); 531 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol."), (const char *)si);
501 } 532 }
502 533
503 return false; 534 return false;
504} 535}
505 536
506void 537inline void
507vpn::ipv4_ev (ev::io &w, int revents) 538vpn::ipv4_ev (ev::io &w, int revents)
508{ 539{
509 if (revents & EV_READ) 540 if (revents & EV_READ)
510 { 541 {
511 vpn_packet *pkt = new vpn_packet; 542 vpn_packet *pkt = new vpn_packet;
527 recv_vpn_packet (pkt, si); 558 recv_vpn_packet (pkt, si);
528 } 559 }
529 else 560 else
530 { 561 {
531 // probably ECONNRESET or somesuch 562 // probably ECONNRESET or somesuch
532 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 563 slog (L_DEBUG, _("%s: %s."), (const char *)si, strerror (errno));
533 } 564 }
534 565
535 delete pkt; 566 delete pkt;
536 } 567 }
537 else 568 else
538 { 569 {
539 slog (L_ERR, 570 slog (L_ERR,
540 _("FATAL: unknown revents %08x in socket, terminating\n"), 571 _("FATAL: unknown revents %08x in socket, exiting.\n"),
541 revents); 572 revents);
542 exit (EXIT_FAILURE); 573 exit (EXIT_FAILURE);
543 } 574 }
544} 575}
545 576
546#if ENABLE_ICMP 577#if ENABLE_ICMP
547void 578inline void
548vpn::icmpv4_ev (ev::io &w, int revents) 579vpn::icmpv4_ev (ev::io &w, int revents)
549{ 580{
550 if (revents & EV_READ) 581 if (revents & EV_READ)
551 { 582 {
552 vpn_packet *pkt = new vpn_packet; 583 vpn_packet *pkt = new vpn_packet;
575 } 606 }
576 } 607 }
577 else 608 else
578 { 609 {
579 // probably ECONNRESET or somesuch 610 // probably ECONNRESET or somesuch
580 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 611 slog (L_DEBUG, _("%s: %s."), (const char *)si, strerror (errno));
581 } 612 }
582 613
583 delete pkt; 614 delete pkt;
584 } 615 }
585 else 616 else
586 { 617 {
587 slog (L_ERR, 618 slog (L_ERR,
588 _("FATAL: unknown revents %08x in socket, terminating\n"), 619 _("FATAL: unknown revents %08x in socket, exiting.\n"),
589 revents); 620 revents);
590 exit (EXIT_FAILURE); 621 exit (EXIT_FAILURE);
591 } 622 }
592} 623}
593#endif 624#endif
594 625
595void 626inline void
596vpn::udpv4_ev (ev::io &w, int revents) 627vpn::udpv4_ev (ev::io &w, int revents)
597{ 628{
598 if (revents & EV_READ) 629 if (revents & EV_READ)
599 { 630 {
600 vpn_packet *pkt = new vpn_packet; 631 vpn_packet *pkt = new vpn_packet;
613 recv_vpn_packet (pkt, si); 644 recv_vpn_packet (pkt, si);
614 } 645 }
615 else 646 else
616 { 647 {
617 // probably ECONNRESET or somesuch 648 // probably ECONNRESET or somesuch
618 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); 649 slog (L_DEBUG, _("%s: fd %d, %s."), (const char *)si, w.fd, strerror (errno));
619 } 650 }
620 651
621 delete pkt; 652 delete pkt;
622 } 653 }
623 else 654 else
624 { 655 {
625 slog (L_ERR, 656 slog (L_ERR,
626 _("FATAL: unknown revents %08x in socket, terminating\n"), 657 _("FATAL: unknown revents %08x in socket, exiting.\n"),
627 revents); 658 revents);
628 exit (EXIT_FAILURE); 659 exit (EXIT_FAILURE);
629 } 660 }
630} 661}
631 662
632void 663inline void
633vpn::tap_ev (ev::io &w, int revents) 664vpn::tap_ev (ev::io &w, int revents)
634{ 665{
635 if (revents & EV_READ) 666 if (revents & EV_READ)
636 { 667 {
637 /* process data */ 668 /* process data */
669 } 700 }
670 else 701 else
671 abort (); 702 abort ();
672} 703}
673 704
674void 705inline void
675vpn::event_cb (ev::timer &w, int) 706vpn::event_cb (ev::timer &w, int)
676{ 707{
677 if (events) 708 if (events)
678 { 709 {
679 if (events & EVENT_SHUTDOWN) 710 if (events & EVENT_SHUTDOWN)
680 { 711 {
681 slog (L_INFO, _("preparing shutdown...")); 712 slog (L_INFO, _("preparing shutdown..."));
682 713
683 shutdown_all (); 714 shutdown_all ();
684 remove_pid (conf.pidfilename); 715 remove_pid (conf.pidfilename);
685 slog (L_INFO, _("terminating")); 716 slog (L_INFO, _("exiting."));
686 exit (EXIT_SUCCESS); 717 exit (EXIT_SUCCESS);
687 } 718 }
688 719
689 if (events & EVENT_RECONNECT) 720 if (events & EVENT_RECONNECT)
690 { 721 {
691 slog (L_INFO, _("forced reconnect")); 722 slog (L_INFO, _("forced reconnect."));
692 723
693 reconnect_all (); 724 reconnect_all ();
694 } 725 }
695 726
696 events = 0; 727 events = 0;
712 743
713 conns.clear (); 744 conns.clear ();
714 745
715 connection_init (); 746 connection_init ();
716 747
717 for (configuration::node_vector::iterator i = conf.nodes.begin (); 748 for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i)
718 i != conf.nodes.end (); ++i) 749 conns.push_back (new connection (this, *i));
719 { 750
720 connection *conn = new connection (this, *i); 751 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
721 conns.push_back (conn);
722 conn->establish_connection (); 752 (*c)->establish_connection ();
723 }
724} 753}
725 754
726connection *vpn::find_router () 755bool vpn::can_direct (conf_node *src, conf_node *dst) const
727{ 756{
728 u32 prio = 1; 757 return src != dst
758 && src->may_direct (dst)
759 && dst->may_direct (src)
760 && (((src->protocols & dst->protocols) && src->connectmode == conf_node::C_ALWAYS)
761 || (src->protocols & dst->connectable_protocols ()));
762}
763
764// only works for indirect and routed connections: find a router
765// from THISNODE to dst
766connection *vpn::find_router_for (const connection *dst)
767{
729 connection *router = 0; 768 connection *router = 0;
730 769
770 // first try to find a router with a direct connection
771 {
772 u32 prio = 1;
773
774 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
775 {
776 connection *c = *i;
777
778 if (c->conf->routerprio > prio
779 && c->conf != THISNODE
780 && c != dst
781 && can_direct (c->conf, dst->conf))
782 {
783 if (c->ictx && c->octx)
784 {
785 prio = c->conf->routerprio;
786 router = c;
787 }
788 else
789 c->establish_connection ();
790 }
791 }
792 }
793
794 if (router)
795 return router;
796
797 // second try find the router with the highest priority higher than ours
798 {
799 u32 prio = 1;
800
801 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
802 {
803 connection *c = *i;
804
805 if (c->conf->routerprio > prio
806 && c->conf->routerprio > THISNODE->routerprio
807 && c != dst
808 && c->conf != THISNODE)
809 {
810 if (c->ictx && c->octx)
811 {
812 prio = c->conf->routerprio;
813 router = c;
814 }
815 else
816 c->establish_connection ();
817 }
818 }
819 }
820 return router;
821}
822
823void vpn::connection_established (connection *c)
824{
731 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 825 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
732 { 826 {
733 connection *c = *i; 827 connection *o = *i;
734 828
735 if (c->conf->routerprio > prio 829 if (!o->is_direct
736 && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use 830 && o->si.valid ()
737 && c->ictx && c->octx 831 && c->si != o->si
738 && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode 832 && c == find_router_for (o))
739 {
740 prio = c->conf->routerprio;
741 router = c;
742 } 833 {
834 slog (L_DEBUG, _("%s: can now route packets via %s, re-keying connection."),
835 o->conf->nodename, c->conf->nodename);
836 o->rekey ();
837 }
743 } 838 }
744
745 return router;
746} 839}
747 840
748void vpn::send_connect_request (int id) 841void vpn::send_connect_request (connection *c)
749{ 842{
750 connection *c = find_router (); 843 connection *r = find_router_for (c);
751 844
752 if (c) 845 if (r)
846 {
847 slog (L_TRACE, _("%s: no way to connect, sending mediated connection request via %s."),
848 c->conf->nodename, r->conf->nodename);
753 c->send_connect_request (id); 849 r->send_connect_request (c->conf->id);
850 }
754 else 851 else
755 // no router found, aggressively connect to all routers 852 slog (L_DEBUG, _("%s: no way to connect and no router found: unable to connect."),
756 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 853 c->conf->nodename);
757 if ((*i)->conf->routerprio && (*i)->conf != THISNODE)
758 (*i)->establish_connection ();
759} 854}
760 855
761void 856void
762connection::dump_status () 857connection::dump_status ()
763{ 858{
764 slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id); 859 slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id);
765 slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"), 860 slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"),
766 connectmode, conf->connectmode, (const char *)si, (int)prot_minor); 861 connectmode, conf->connectmode, (const char *)si, (int)prot_minor);
767 slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"), 862 slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"),
768 (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt); 863 (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt);
769 slog (L_NOTICE, _(" establish_conn %ld / rekey %ld / keepalive %ld"),
770 (long)(establish_connection.at), (long)(rekey.at), (long)(keepalive.at));
771} 864}
772 865
773void 866void
774vpn::dump_status () 867vpn::dump_status ()
775{ 868{
780 873
781 slog (L_NOTICE, _("END status dump")); 874 slog (L_NOTICE, _("END status dump"));
782} 875}
783 876
784vpn::vpn (void) 877vpn::vpn (void)
878{
785: event (this, &vpn::event_cb) 879 event .set<vpn, &vpn::event_cb > (this);
786, udpv4_ev_watcher (this, &vpn::udpv4_ev) 880 udpv4_ev_watcher .set<vpn, &vpn::udpv4_ev > (this);
787, ipv4_ev_watcher (this, &vpn::ipv4_ev) 881 ipv4_ev_watcher .set<vpn, &vpn::ipv4_ev > (this);
788#if ENABLE_TCP 882#if ENABLE_TCP
789, tcpv4_ev_watcher (this, &vpn::tcpv4_ev) 883 tcpv4_ev_watcher .set<vpn, &vpn::tcpv4_ev > (this);
790#endif 884#endif
791#if ENABLE_ICMP 885#if ENABLE_ICMP
792, icmpv4_ev_watcher(this, &vpn::icmpv4_ev) 886 icmpv4_ev_watcher.set<vpn, &vpn::icmpv4_ev> (this);
793#endif 887#endif
794#if ENABLE_DNS 888#if ENABLE_DNS
795, dnsv4_ev_watcher (this, &vpn::dnsv4_ev) 889 dnsv4_ev_watcher .set<vpn, &vpn::dnsv4_ev > (this);
796#endif 890#endif
797, tap_ev_watcher (this, &vpn::tap_ev) 891 tap_ev_watcher .set<vpn, &vpn::tap_ev > (this);
798{
799} 892}
800 893
801vpn::~vpn () 894vpn::~vpn ()
802{ 895{
803} 896}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines