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.47 by pcg, Thu Aug 7 17:54:27 2008 UTC vs.
Revision 1.55 by pcg, Fri Mar 27 22:02:57 2009 UTC

120 120
121 return filename; 121 return filename;
122} 122}
123 123
124int 124int
125vpn::setup_socket (u8 prot, int family, int type, int proto)
126{
127 int fd = socket (family, type, proto);
128
129 if (fd < 0)
130 {
131 slog (L_ERR, _("unable to create %s socket: %s."), strprotocol (prot), strerror (errno));
132 return fd;
133 }
134
135 fcntl (fd, F_SETFL, O_NONBLOCK);
136 fcntl (fd, F_SETFD, FD_CLOEXEC);
137
138#ifdef SO_MARK
139 if (::conf.nfmark)
140 if (setsockopt (fd, SOL_SOCKET, SO_MARK, &::conf.nfmark, sizeof ::conf.nfmark))
141 slog (L_WARN, _("unable to set nfmark on %s socket: %s"), strprotocol (prot), strerror (errno));
142#endif
143
144 return fd;
145}
146
147int
125vpn::setup () 148vpn::setup ()
126{ 149{
150 int success = 0;
151
127 ipv4_tos = -1; 152 ipv4_tos = -1;
128 ipv4_fd = -1; 153 ipv4_fd = -1;
129 154
130 if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) 155 if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto)
131 { 156 {
132 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); 157 ipv4_fd = setup_socket (PROT_IPv4, PF_INET, SOCK_RAW, ::conf.ip_proto);
133 158
134 if (ipv4_fd < 0) 159 if (ipv4_fd < 0)
135 return -1; 160 return -1;
136
137 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK);
138 fcntl (ipv4_fd, F_SETFD, FD_CLOEXEC);
139 161
140#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) 162#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
141 // this I really consider a linux bug. I am neither connected 163 // this I really consider a linux bug. I am neither connected
142 // nor do I fragment myself. Linux still sets DF and doesn't 164 // nor do I fragment myself. Linux still sets DF and doesn't
143 // fragment for me sometimes. 165 // fragment for me sometimes.
149 171
150 sockinfo si (THISNODE, PROT_IPv4); 172 sockinfo si (THISNODE, PROT_IPv4);
151 173
152 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) 174 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ()))
153 { 175 {
154 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); 176 slog (L_ERR, _("can't bind ipv4 socket on %s: %s, exiting."), (const char *)si, strerror (errno));
155 exit (EXIT_FAILURE); 177 return -1;
156 } 178 }
157 179
158 ipv4_ev_watcher.start (ipv4_fd, EV_READ); 180 ipv4_ev_watcher.start (ipv4_fd, EV_READ);
181 ++success;
159 } 182 }
183 else
184 THISNODE->protocols &= ~PROT_IPv4;
160 185
161 udpv4_tos = -1; 186 udpv4_tos = -1;
162 udpv4_fd = -1; 187 udpv4_fd = -1;
163 188
164 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) 189 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port)
165 { 190 {
166 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 191 udpv4_fd = setup_socket (PROT_UDPv4, PF_INET, SOCK_DGRAM, IPPROTO_UDP);
167 192
168 if (udpv4_fd < 0) 193 if (udpv4_fd < 0)
169 return -1; 194 return -1;
170
171 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK);
172 fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC);
173 195
174 // standard daemon practise... 196 // standard daemon practise...
175 { 197 {
176 int oval = 1; 198 int oval = 1;
177 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 199 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
189 211
190 sockinfo si (THISNODE, PROT_UDPv4); 212 sockinfo si (THISNODE, PROT_UDPv4);
191 213
192 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) 214 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
193 { 215 {
194 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); 216 slog (L_ERR, _("can't bind udpv4 on %s: %s, exiting."), (const char *)si, strerror (errno));
195 exit (EXIT_FAILURE); 217 return -1;
196 } 218 }
197 219
198 udpv4_ev_watcher.start (udpv4_fd, EV_READ); 220 udpv4_ev_watcher.start (udpv4_fd, EV_READ);
221 ++success;
199 } 222 }
223 else
224 THISNODE->protocols &= ~PROT_UDPv4;
200 225
201 icmpv4_tos = -1; 226 icmpv4_tos = -1;
202 icmpv4_fd = -1; 227 icmpv4_fd = -1;
203 228
204#if ENABLE_ICMP 229#if ENABLE_ICMP
205 if (THISNODE->protocols & PROT_ICMPv4) 230 if (THISNODE->protocols & PROT_ICMPv4)
206 { 231 {
207 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); 232 icmpv4_fd = setup_socket (PROT_ICMPv4, PF_INET, SOCK_RAW, IPPROTO_ICMP);
208 233
209 if (icmpv4_fd < 0) 234 if (icmpv4_fd < 0)
210 return -1; 235 return -1;
211
212 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK);
213 fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC);
214 236
215#ifdef ICMP_FILTER 237#ifdef ICMP_FILTER
216 { 238 {
217 icmp_filter oval; 239 icmp_filter oval;
218 oval.data = 0xffffffff; 240 oval.data = 0xffffffff;
235 257
236 sockinfo si (THISNODE, PROT_ICMPv4); 258 sockinfo si (THISNODE, PROT_ICMPv4);
237 259
238 if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) 260 if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ()))
239 { 261 {
240 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); 262 slog (L_ERR, _("can't bind icmpv4 on %s: %s, exiting."), (const char *)si, strerror (errno));
241 exit (EXIT_FAILURE); 263 return -1;
242 } 264 }
243 265
244 icmpv4_ev_watcher.start (icmpv4_fd, EV_READ); 266 icmpv4_ev_watcher.start (icmpv4_fd, EV_READ);
267 ++success;
245 } 268 }
246#endif 269#endif
247 270
248 tcpv4_fd = -1; 271 tcpv4_fd = -1;
249 272
250#if ENABLE_TCP 273#if ENABLE_TCP
251 if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) 274 if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port)
252 { 275 {
253 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 276 tcpv4_fd = setup_socket (PROT_TCPv4, PF_INET, SOCK_STREAM, IPPROTO_TCP);
254 277
255 if (tcpv4_fd < 0) 278 if (tcpv4_fd < 0)
256 return -1; 279 return -1;
257 280
258 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK);
259 fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC);
260
261 // standard daemon practise... 281 // standard daemon practise...
262 { 282 {
263 int oval = 1; 283 int oval = 1;
264 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 284 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
265 } 285 }
266 286
267 sockinfo si (THISNODE, PROT_TCPv4); 287 sockinfo si (THISNODE, PROT_TCPv4);
268 288
269 if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) 289 if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ()))
270 { 290 {
271 slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); 291 slog (L_ERR, _("can't bind tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno));
272 exit (EXIT_FAILURE); 292 return -1;
273 } 293 }
274 294
275 if (listen (tcpv4_fd, 5)) 295 if (listen (tcpv4_fd, 5))
276 { 296 {
277 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); 297 slog (L_ERR, _("can't listen tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno));
278 exit (EXIT_FAILURE); 298 return -1;
279 } 299 }
280 300
281 tcpv4_ev_watcher.start (tcpv4_fd, EV_READ); 301 tcpv4_ev_watcher.start (tcpv4_fd, EV_READ);
302 ++success;
282 } 303 }
304 else
305 THISNODE->protocols &= ~PROT_TCPv4;
283#endif 306#endif
284 307
285 dnsv4_tos = -1; 308 dnsv4_tos = -1;
286 dnsv4_fd = -1; 309 dnsv4_fd = -1;
287 310
288#if ENABLE_DNS 311#if ENABLE_DNS
289 if (THISNODE->protocols & PROT_DNSv4) 312 if (THISNODE->protocols & PROT_DNSv4)
290 { 313 {
291 dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); 314 dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4);
292 315
293 dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 316 dnsv4_fd = setup_socket (PROT_DNSv4, PF_INET, SOCK_DGRAM, IPPROTO_UDP);
294 317
295 if (dnsv4_fd < 0) 318 if (dnsv4_fd < 0)
296 return -1; 319 return -1;
297
298 fcntl (dnsv4_fd, F_SETFL, O_NONBLOCK);
299 fcntl (dnsv4_fd, F_SETFD, FD_CLOEXEC);
300 320
301# if defined(SOL_IP) && defined(IP_MTU_DISCOVER) 321# if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
302 // this I really consider a linux bug. I am neither connected 322 // this I really consider a linux bug. I am neither connected
303 // nor do I fragment myself. Linux still sets DF and doesn't 323 // nor do I fragment myself. Linux still sets DF and doesn't
304 // fragment for me sometimes. 324 // fragment for me sometimes.
318 THISNODE->dns_hostname ? THISNODE->dns_port : 0, 338 THISNODE->dns_hostname ? THISNODE->dns_port : 0,
319 PROT_DNSv4); 339 PROT_DNSv4);
320 340
321 if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) 341 if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ()))
322 { 342 {
323 slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); 343 slog (L_ERR, _("can't bind dnsv4 on %s: %s, exiting."), (const char *)si, strerror (errno));
324 exit (EXIT_FAILURE); 344 return -1;
325 } 345 }
326 346
327 dnsv4_ev_watcher.start (dnsv4_fd, EV_READ); 347 dnsv4_ev_watcher.start (dnsv4_fd, EV_READ);
348 ++success;
328 } 349 }
329#endif 350#endif
330 351
331 ///////////////////////////////////////////////////////////////////////////// 352 /////////////////////////////////////////////////////////////////////////////
353
354 if (!success)
355 {
356 slog (L_ERR, _("no protocols enabled."));
357 return -1;
358 }
332 359
333 reconnect_all (); 360 reconnect_all ();
334 361
335 ///////////////////////////////////////////////////////////////////////////// 362 /////////////////////////////////////////////////////////////////////////////
336 363
337 tap = new tap_device (); 364 tap = new tap_device ();
338 if (!tap) //D this, of course, never catches 365 if (!tap) //D this, of course, never catches
339 { 366 {
340 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); 367 slog (L_ERR, _("cannot create network interface '%s'."), conf.ifname);
341 exit (EXIT_FAILURE); 368 return -1;
342 } 369 }
343 370
344 fcntl (tap->fd, F_SETFD, FD_CLOEXEC); 371 fcntl (tap->fd, F_SETFD, FD_CLOEXEC);
345 372
346 run_script_cb cb; 373 run_script_cb cb;
347 cb.set<vpn, &vpn::script_if_init> (this); 374 cb.set<vpn, &vpn::script_if_init> (this);
348 375
349 if (tap->if_up () && 376 if (tap->if_up () &&
350 !run_script (cb, true)) 377 !run_script (cb, true))
351 { 378 {
352 slog (L_ERR, _("interface initialization command '%s' failed, exiting."), 379 slog (L_ERR, _("interface initialization command '%s' failed."),
353 tap->if_up ()); 380 tap->if_up ());
354 exit (EXIT_FAILURE); 381 return -1;
355 } 382 }
356 383
357 cb.set<vpn, &vpn::script_if_up> (this); 384 cb.set<vpn, &vpn::script_if_up> (this);
358 if (!run_script (cb, true)) 385 if (!run_script (cb, true))
359 { 386 {
360 slog (L_ERR, _("if-up command execution failed, exiting.")); 387 slog (L_ERR, _("if-up command execution failed."));
361 exit (EXIT_FAILURE); 388 return -1;
362 } 389 }
363 390
364 tap_ev_watcher.start (tap->fd, EV_READ); 391 tap_ev_watcher.start (tap->fd, EV_READ);
365 392
366 return 0; 393 return 0;
449vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 476vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
450{ 477{
451 unsigned int src = pkt->src (); 478 unsigned int src = pkt->src ();
452 unsigned int dst = pkt->dst (); 479 unsigned int dst = pkt->dst ();
453 480
454 slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), 481 slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d."),
455 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); 482 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len);
456 483
457 if (src == 0 || src > conns.size () 484 if (src == 0 || src > conns.size ()
458 || dst > conns.size () 485 || dst > conns.size ()
459 || pkt->typ () >= vpn_packet::PT_MAX) 486 || pkt->typ () >= vpn_packet::PT_MAX)
460 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), 487 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."),
461 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 488 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
462 else if (dst > conns.size ()) 489 else if (dst > conns.size ())
463 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), 490 slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."),
464 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 491 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
465 else 492 else
466 { 493 {
467 connection *c = conns[src - 1]; 494 connection *c = conns[src - 1];
468 495
469 if (dst == 0) 496 if (dst == 0)
470 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), 497 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)."),
471 c->conf->nodename, (const char *)rsi); 498 c->conf->nodename, (const char *)rsi);
472 else if (dst != THISNODE->id) 499 else if (dst != THISNODE->id)
473 { 500 {
474 if (THISNODE->routerprio) 501 if (THISNODE->routerprio)
475 // the tos setting gets lost here. who cares. 502 // the tos setting gets lost here. who cares.
476 conns[dst - 1]->inject_vpn_packet (pkt); 503 conns[dst - 1]->inject_vpn_packet (pkt);
477 else 504 else
478 slog (L_WARN, 505 slog (L_WARN,
479 _("%s(%s): forwarding request (=> %s), but we are no router"), 506 _("%s(%s): request to forward packet to %s, but we are no router (config mismatch?)."),
480 c->conf->nodename, (const char *)rsi, 507 c->conf->nodename, (const char *)rsi,
481 conns[dst - 1]->conf->nodename); 508 conns[dst - 1]->conf->nodename);
482 } 509 }
483 else 510 else
484 c->recv_vpn_packet (pkt, rsi); 511 c->recv_vpn_packet (pkt, rsi);
507#if ENABLE_DNS 534#if ENABLE_DNS
508 case PROT_DNSv4: 535 case PROT_DNSv4:
509 return send_dnsv4_packet (pkt, si, tos); 536 return send_dnsv4_packet (pkt, si, tos);
510#endif 537#endif
511 default: 538 default:
512 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); 539 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol."), (const char *)si);
513 } 540 }
514 541
515 return false; 542 return false;
516} 543}
517 544
539 recv_vpn_packet (pkt, si); 566 recv_vpn_packet (pkt, si);
540 } 567 }
541 else 568 else
542 { 569 {
543 // probably ECONNRESET or somesuch 570 // probably ECONNRESET or somesuch
544 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 571 slog (L_DEBUG, _("%s: %s."), (const char *)si, strerror (errno));
545 } 572 }
546 573
547 delete pkt; 574 delete pkt;
548 } 575 }
549 else 576 else
550 { 577 {
551 slog (L_ERR, 578 slog (L_ERR,
552 _("FATAL: unknown revents %08x in socket, terminating\n"), 579 _("FATAL: unknown revents %08x in socket, exiting.\n"),
553 revents); 580 revents);
554 exit (EXIT_FAILURE); 581 exit (EXIT_FAILURE);
555 } 582 }
556} 583}
557 584
587 } 614 }
588 } 615 }
589 else 616 else
590 { 617 {
591 // probably ECONNRESET or somesuch 618 // probably ECONNRESET or somesuch
592 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 619 slog (L_DEBUG, _("%s: %s."), (const char *)si, strerror (errno));
593 } 620 }
594 621
595 delete pkt; 622 delete pkt;
596 } 623 }
597 else 624 else
598 { 625 {
599 slog (L_ERR, 626 slog (L_ERR,
600 _("FATAL: unknown revents %08x in socket, terminating\n"), 627 _("FATAL: unknown revents %08x in socket, exiting.\n"),
601 revents); 628 revents);
602 exit (EXIT_FAILURE); 629 exit (EXIT_FAILURE);
603 } 630 }
604} 631}
605#endif 632#endif
625 recv_vpn_packet (pkt, si); 652 recv_vpn_packet (pkt, si);
626 } 653 }
627 else 654 else
628 { 655 {
629 // probably ECONNRESET or somesuch 656 // probably ECONNRESET or somesuch
630 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); 657 slog (L_DEBUG, _("%s: fd %d, %s."), (const char *)si, w.fd, strerror (errno));
631 } 658 }
632 659
633 delete pkt; 660 delete pkt;
634 } 661 }
635 else 662 else
636 { 663 {
637 slog (L_ERR, 664 slog (L_ERR,
638 _("FATAL: unknown revents %08x in socket, terminating\n"), 665 _("FATAL: unknown revents %08x in socket, exiting.\n"),
639 revents); 666 revents);
640 exit (EXIT_FAILURE); 667 exit (EXIT_FAILURE);
641 } 668 }
642} 669}
643 670
692 { 719 {
693 slog (L_INFO, _("preparing shutdown...")); 720 slog (L_INFO, _("preparing shutdown..."));
694 721
695 shutdown_all (); 722 shutdown_all ();
696 remove_pid (conf.pidfilename); 723 remove_pid (conf.pidfilename);
697 slog (L_INFO, _("terminating")); 724 slog (L_INFO, _("exiting."));
698 exit (EXIT_SUCCESS); 725 exit (EXIT_SUCCESS);
699 } 726 }
700 727
701 if (events & EVENT_RECONNECT) 728 if (events & EVENT_RECONNECT)
702 { 729 {
703 slog (L_INFO, _("forced reconnect")); 730 slog (L_INFO, _("forced reconnect."));
704 731
705 reconnect_all (); 732 reconnect_all ();
706 } 733 }
707 734
708 events = 0; 735 events = 0;
724 751
725 conns.clear (); 752 conns.clear ();
726 753
727 connection_init (); 754 connection_init ();
728 755
729 for (configuration::node_vector::iterator i = conf.nodes.begin (); 756 for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i)
730 i != conf.nodes.end (); ++i) 757 conns.push_back (new connection (this, *i));
731 { 758
732 connection *conn = new connection (this, *i); 759 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
733 conns.push_back (conn);
734 conn->establish_connection (); 760 (*c)->establish_connection ();
735 }
736} 761}
737 762
738connection *vpn::find_router () 763bool vpn::can_direct (conf_node *src, conf_node *dst) const
739{ 764{
740 u32 prio = 1; 765 return src != dst
766 && src->may_direct (dst)
767 && dst->may_direct (src)
768 && (((src->protocols & dst->protocols) && src->connectmode == conf_node::C_ALWAYS)
769 || (src->protocols & dst->connectable_protocols ()));
770}
771
772// only works for indirect and routed connections: find a router
773// from THISNODE to dst
774connection *vpn::find_router_for (const connection *dst)
775{
741 connection *router = 0; 776 connection *router = 0;
742 777
778 // first try to find a router with a direct connection, route there
779 // regardless of any other considerations.
780 {
781 u32 prio = 1;
782
783 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
784 {
785 connection *c = *i;
786
787 if (c->conf->routerprio > prio
788 && c->conf != THISNODE
789 && can_direct (c->conf, dst->conf)
790 && c->ictx && c->octx)
791 {
792 prio = c->conf->routerprio;
793 router = c;
794 }
795 }
796 }
797
798 if (router)
799 return router;
800
801 // second try find the router with the highest priority, higher than ours
802 {
803 u32 prio = THISNODE->routerprio ? THISNODE->routerprio : 1;
804
805 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
806 {
807 connection *c = *i;
808
809 if (c->conf->routerprio > prio
810 && c != dst
811 && c->conf != THISNODE
812 && c->ictx && c->octx)
813 {
814 prio = c->conf->routerprio;
815 router = c;
816 }
817 }
818 }
819
820 return router;
821}
822
823void vpn::connection_established (connection *c)
824{
743 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 825 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
744 { 826 {
745 connection *c = *i; 827 connection *o = *i;
746 828
747 if (c->conf->routerprio > prio 829 if (!o->is_direct
748 && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use 830 && o->si.valid ()
749 && c->ictx && c->octx 831 && c->si != o->si
750 && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode 832 && c == find_router_for (o))
751 {
752 prio = c->conf->routerprio;
753 router = c;
754 } 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 }
755 } 838 }
756
757 return router;
758} 839}
759 840
760void vpn::send_connect_request (int id) 841void vpn::send_connect_request (connection *c)
761{ 842{
762 connection *c = find_router (); 843 connection *r = find_router_for (c);
763 844
764 if (c) 845 if (r)
846 {
847 slog (L_TRACE, _("%s: no address known, sending mediated connection request via %s."),
848 c->conf->nodename, r->conf->nodename);
765 c->send_connect_request (id); 849 r->send_connect_request (c->conf->id);
850 }
766 else 851 else
767 // no router found, aggressively connect to all routers 852 slog (L_DEBUG, _("%s: no way to connect and no router found: unable to connect at this time."),
768 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 853 c->conf->nodename);
769 if ((*i)->conf->routerprio && (*i)->conf != THISNODE)
770 (*i)->establish_connection ();
771} 854}
772 855
773void 856void
774connection::dump_status () 857connection::dump_status ()
775{ 858{
776 slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id); 859 slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id);
777 slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"), 860 slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"),
778 connectmode, conf->connectmode, (const char *)si, (int)prot_minor); 861 connectmode, conf->connectmode, (const char *)si, (int)prot_minor);
779 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"),
780 (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt); 863 (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt);
781 slog (L_NOTICE, _(" establish_conn %ld / rekey %ld / keepalive %ld"),
782 (long)(establish_connection.at), (long)(rekey.at), (long)(keepalive.at));
783} 864}
784 865
785void 866void
786vpn::dump_status () 867vpn::dump_status ()
787{ 868{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines