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.7 by pcg, Sun Apr 6 18:12:18 2003 UTC vs.
Revision 1.20 by pcg, Thu Oct 16 14:12:00 2003 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 Marc Lehmann <pcg@goof.com>
3 4
4 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version. 8 (at your option) any later version.
24#include <cstring> 25#include <cstring>
25#include <cstdio> 26#include <cstdio>
26 27
27#include <sys/types.h> 28#include <sys/types.h>
28#include <sys/socket.h> 29#include <sys/socket.h>
29#include <sys/poll.h>
30#include <sys/wait.h> 30#include <sys/wait.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <errno.h> 31#include <errno.h>
34#include <time.h> 32#include <time.h>
35#include <unistd.h> 33#include <unistd.h>
34#include <fcntl.h>
35#include <sys/socket.h>
36
37#include "netcompat.h"
36 38
37#include "pidfile.h" 39#include "pidfile.h"
38 40
39#include "connection.h" 41#include "connection.h"
40#include "util.h" 42#include "util.h"
41#include "vpn.h" 43#include "vpn.h"
44
45vpn network; // THE vpn (bad design...)
42 46
43///////////////////////////////////////////////////////////////////////////// 47/////////////////////////////////////////////////////////////////////////////
44 48
45const char *vpn::script_if_up () 49const char *vpn::script_if_up ()
46{ 50{
71} 75}
72 76
73int 77int
74vpn::setup () 78vpn::setup ()
75{ 79{
80 ipv4_fd = -1;
81
82 if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto)
83 {
84 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto);
85
86 if (ipv4_fd < 0)
87 return -1;
88
89 fcntl (ipv4_fd, F_SETFL, O_NONBLOCK);
90
91#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
92 // this I really consider a linux bug. I am neither connected
93 // nor do I fragment myself. Linux still sets DF and doesn't
94 // fragment for me sometimes.
95 {
96 int oval = IP_PMTUDISC_DONT;
97 setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
98 }
99#endif
100
101 sockinfo si (THISNODE, PROT_IPv4);
102
103 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ()))
104 {
105 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno));
106 exit (1);
107 }
108
109 ipv4_ev_watcher.start (ipv4_fd, POLLIN);
110 }
111
76 udpv4_fd = -1; 112 udpv4_fd = -1;
77 113
78 if (THISNODE->protocols & PROT_UDPv4) 114 if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port)
79 { 115 {
80 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 116 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
81 117
82 if (udpv4_fd < 0) 118 if (udpv4_fd < 0)
83 return -1; 119 return -1;
120
121 fcntl (udpv4_fd, F_SETFL, O_NONBLOCK);
84 122
85 // standard daemon practise... 123 // standard daemon practise...
86 { 124 {
87 int oval = 1; 125 int oval = 1;
88 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 126 setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
89 } 127 }
90 128
91 sockinfo si (THISNODE, PROT_UDPv4); 129#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
92
93 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
94 {
95 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
96 exit (1);
97 }
98
99#ifdef IP_MTU_DISCOVER
100 // this I really consider a linux bug. I am neither connected 130 // this I really consider a linux bug. I am neither connected
101 // nor do I fragment myself. Linux still sets DF and doesn't 131 // nor do I fragment myself. Linux still sets DF and doesn't
102 // fragment for me sometimes. 132 // fragment for me sometimes.
103 { 133 {
104 int oval = IP_PMTUDISC_DONT; 134 int oval = IP_PMTUDISC_DONT;
105 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); 135 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
106 } 136 }
107#endif 137#endif
108 138
139 sockinfo si (THISNODE, PROT_UDPv4);
140
141 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
142 {
143 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
144 exit (1);
145 }
146
109 udpv4_ev_watcher.start (udpv4_fd, POLLIN); 147 udpv4_ev_watcher.start (udpv4_fd, POLLIN);
110 } 148 }
111 149
112 ipv4_fd = -1; 150 icmpv4_fd = -1;
151
152#if ENABLE_ICMP
113 if (THISNODE->protocols & PROT_IPv4) 153 if (THISNODE->protocols & PROT_ICMPv4)
114 { 154 {
115 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); 155 icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP);
116 156
117 if (ipv4_fd < 0) 157 if (icmpv4_fd < 0)
118 return -1; 158 return -1;
119 159
120 sockinfo si (THISNODE, PROT_IPv4); 160 fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK);
121 161
122 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) 162#ifdef ICMP_FILTER
123 { 163 {
124 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); 164 icmp_filter oval;
125 exit (1); 165 oval.data = 0xffffffff;
166 if (::conf.icmp_type < 32)
167 oval.data &= ~(1 << ::conf.icmp_type);
168
169 setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval);
126 } 170 }
171#endif
127 172
128#ifdef IP_MTU_DISCOVER 173#if defined(SOL_IP) && defined(IP_MTU_DISCOVER)
129 // this I really consider a linux bug. I am neither connected 174 // this I really consider a linux bug. I am neither connected
130 // nor do I fragment myself. Linux still sets DF and doesn't 175 // nor do I fragment myself. Linux still sets DF and doesn't
131 // fragment for me sometimes. 176 // fragment for me sometimes.
132 { 177 {
133 int oval = IP_PMTUDISC_DONT; 178 int oval = IP_PMTUDISC_DONT;
134 setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); 179 setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval);
135 } 180 }
136#endif 181#endif
137 182
183 sockinfo si (THISNODE, PROT_ICMPv4);
184
185 if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ()))
186 {
187 slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno));
188 exit (1);
189 }
190
138 ipv4_ev_watcher.start (ipv4_fd, POLLIN); 191 icmpv4_ev_watcher.start (icmpv4_fd, POLLIN);
139 } 192 }
193#endif
194
195 tcpv4_fd = -1;
140 196
141#if ENABLE_TCP 197#if ENABLE_TCP
142 if (THISNODE->protocols & PROT_TCPv4) 198 if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port)
143 { 199 {
144 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 200 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
145 201
146 if (tcpv4_fd < 0) 202 if (tcpv4_fd < 0)
147 return -1; 203 return -1;
204
205 fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK);
148 206
149 // standard daemon practise... 207 // standard daemon practise...
150 { 208 {
151 int oval = 1; 209 int oval = 1;
152 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 210 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
201#if ENABLE_TCP 259#if ENABLE_TCP
202 case PROT_TCPv4: 260 case PROT_TCPv4:
203 return send_tcpv4_packet (pkt, si, tos); 261 return send_tcpv4_packet (pkt, si, tos);
204#endif 262#endif
205 263
264#if ENABLE_ICMP
265 case PROT_ICMPv4:
266 return send_icmpv4_packet (pkt, si, tos);
267#endif
268
206 default: 269 default:
207 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); 270 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
208 return false; 271 return false;
209 } 272 }
210} 273}
211 274
212bool 275bool
213vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 276vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
214{ 277{
278#if defined(SOL_IP) && defined(IP_TOS)
215 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 279 setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
280#endif
216 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 281 sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
217 282
218 return true; 283 return true;
219} 284}
285
286static u16
287ipv4_checksum (u16 *data, unsigned int len)
288{
289 // use 32 bit accumulator and fold back carry bits at the end
290 u32 sum = 0;
291
292 while (len > 1)
293 {
294 sum += *data++;
295 len -= 2;
296 }
297
298 // odd byte left?
299 if (len)
300 sum += *(u8 *)data;
301
302 // add back carry bits
303 sum = (sum >> 16) + (sum & 0xffff); // lo += hi
304 sum += (sum >> 16); // carry
305
306 return ~sum;
307}
308
309#if ENABLE_ICMP
310bool
311vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
312{
313#if defined(SOL_IP) && defined(IP_TOS)
314 setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
315#endif
316
317 pkt->unshift_hdr (4);
318
319 icmp_header *hdr = (icmp_header *)&((*pkt)[0]);
320 hdr->type = ::conf.icmp_type;
321 hdr->code = 255;
322 hdr->checksum = 0;
323 hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len);
324
325 sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
326
327 return true;
328}
329#endif
220 330
221bool 331bool
222vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 332vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
223{ 333{
334#if defined(SOL_IP) && defined(IP_TOS)
224 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); 335 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
336#endif
225 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); 337 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
226 338
227 return true; 339 return true;
340}
341
342void
343vpn::inject_data_packet (tap_packet *pkt, int dst)
344{
345 if (dst)
346 {
347 // unicast
348 if (dst != THISNODE->id)
349 conns[dst - 1]->inject_data_packet (pkt);
350 }
351 else
352 {
353 // broadcast, this is ugly, but due to the security policy
354 // we have to connect to all hosts...
355 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
356 if ((*c)->conf != THISNODE)
357 (*c)->inject_data_packet (pkt, true);
358 }
228} 359}
229 360
230void 361void
231vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 362vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
232{ 363{
246 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); 377 (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ());
247 else 378 else
248 { 379 {
249 connection *c = conns[src - 1]; 380 connection *c = conns[src - 1];
250 381
251 if (dst == 0 && !THISNODE->routerprio) 382 if (dst == 0)
252 slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), 383 slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"),
253 c->conf->nodename, (const char *)rsi); 384 c->conf->nodename, (const char *)rsi);
254 else if (dst != 0 && dst != THISNODE->id) 385 else if (dst != THISNODE->id)
255 { 386 {
256 if (THISNODE->routerprio) 387 if (THISNODE->routerprio)
257 // the tos setting gets lost here. who cares. 388 // the tos setting gets lost here. who cares.
258 conns[dst - 1]->inject_vpn_packet (pkt); 389 conns[dst - 1]->inject_vpn_packet (pkt);
259 else 390 else
266 c->recv_vpn_packet (pkt, rsi); 397 c->recv_vpn_packet (pkt, rsi);
267 } 398 }
268} 399}
269 400
270void 401void
271vpn::udpv4_ev (io_watcher &w, short revents) 402vpn::ipv4_ev (io_watcher &w, short revents)
272{ 403{
273 if (revents & (POLLIN | POLLERR)) 404 if (revents & (POLLIN | POLLERR))
274 { 405 {
275 vpn_packet *pkt = new vpn_packet; 406 vpn_packet *pkt = new vpn_packet;
276 struct sockaddr_in sa; 407 struct sockaddr_in sa;
277 socklen_t sa_len = sizeof (sa); 408 socklen_t sa_len = sizeof (sa);
278 int len; 409 int len;
279 410
280 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 411 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
281 412
282 sockinfo si(sa, PROT_UDPv4); 413 sockinfo si(sa, PROT_IPv4);
283 414
284 if (len > 0) 415 if (len > 0)
285 { 416 {
286 pkt->len = len; 417 pkt->len = len;
287 418
419 // raw sockets deliver the ipv4, but don't expect it on sends
420 // this is slow, but...
421 pkt->skip_hdr (IP_OVERHEAD);
422
288 recv_vpn_packet (pkt, si); 423 recv_vpn_packet (pkt, si);
289 } 424 }
290 else 425 else
291 { 426 {
292 // probably ECONNRESET or somesuch 427 // probably ECONNRESET or somesuch
293 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); 428 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
294 } 429 }
295 430
296 delete pkt; 431 delete pkt;
297 } 432 }
298 else if (revents & POLLHUP) 433 else if (revents & POLLHUP)
299 { 434 {
300 // this cannot ;) happen on udp sockets 435 // this cannot ;) happen on udp sockets
301 slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); 436 slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating."));
302 exit (1); 437 exit (1);
303 } 438 }
304 else 439 else
305 { 440 {
306 slog (L_ERR, 441 slog (L_ERR,
308 revents); 443 revents);
309 exit (1); 444 exit (1);
310 } 445 }
311} 446}
312 447
448#if ENABLE_ICMP
313void 449void
314vpn::ipv4_ev (io_watcher &w, short revents) 450vpn::icmpv4_ev (io_watcher &w, short revents)
315{ 451{
316 if (revents & (POLLIN | POLLERR)) 452 if (revents & (POLLIN | POLLERR))
317 { 453 {
318 vpn_packet *pkt = new vpn_packet; 454 vpn_packet *pkt = new vpn_packet;
319 struct sockaddr_in sa; 455 struct sockaddr_in sa;
320 socklen_t sa_len = sizeof (sa); 456 socklen_t sa_len = sizeof (sa);
321 int len; 457 int len;
322 458
323 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 459 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
324 460
325 sockinfo si(sa, PROT_IPv4); 461 sockinfo si(sa, PROT_ICMPv4);
326 462
327 if (len > 0) 463 if (len > 0)
328 { 464 {
329 pkt->len = len; 465 pkt->len = len;
330 466
467 icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]);
468
469 if (hdr->type == ::conf.icmp_type
470 && hdr->code == 255)
471 {
331 // raw sockets deliver the ipv4, but don't expect it on sends 472 // raw sockets deliver the ipv4, but don't expect it on sends
332 // this is slow, but... 473 // this is slow, but...
333 pkt->skip_hdr (IP_OVERHEAD); 474 pkt->skip_hdr (ICMP_OVERHEAD);
334 475
335 recv_vpn_packet (pkt, si); 476 recv_vpn_packet (pkt, si);
477 }
336 } 478 }
337 else 479 else
338 { 480 {
339 // probably ECONNRESET or somesuch 481 // probably ECONNRESET or somesuch
340 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); 482 slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno));
343 delete pkt; 485 delete pkt;
344 } 486 }
345 else if (revents & POLLHUP) 487 else if (revents & POLLHUP)
346 { 488 {
347 // this cannot ;) happen on udp sockets 489 // this cannot ;) happen on udp sockets
348 slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); 490 slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating."));
349 exit (1); 491 exit (1);
350 } 492 }
351 else 493 else
352 { 494 {
353 slog (L_ERR, 495 slog (L_ERR,
354 _("FATAL: unknown revents %08x in socket, terminating\n"), 496 _("FATAL: unknown revents %08x in socket, terminating\n"),
355 revents); 497 revents);
356 exit (1); 498 exit (1);
357 } 499 }
358} 500}
501#endif
502
503void
504vpn::udpv4_ev (io_watcher &w, short revents)
505{
506 if (revents & (POLLIN | POLLERR))
507 {
508 vpn_packet *pkt = new vpn_packet;
509 struct sockaddr_in sa;
510 socklen_t sa_len = sizeof (sa);
511 int len;
512
513 len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
514
515 sockinfo si(sa, PROT_UDPv4);
516
517 if (len > 0)
518 {
519 pkt->len = len;
520
521 recv_vpn_packet (pkt, si);
522 }
523 else
524 {
525 // probably ECONNRESET or somesuch
526 slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno));
527 }
528
529 delete pkt;
530 }
531 else if (revents & POLLHUP)
532 {
533 // this cannot ;) happen on udp sockets
534 slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating."));
535 exit (1);
536 }
537 else
538 {
539 slog (L_ERR,
540 _("FATAL: unknown revents %08x in socket, terminating\n"),
541 revents);
542 exit (1);
543 }
544}
359 545
360void 546void
361vpn::tap_ev (io_watcher &w, short revents) 547vpn::tap_ev (io_watcher &w, short revents)
362{ 548{
363 if (revents & POLLIN) 549 if (revents & POLLIN)
365 /* process data */ 551 /* process data */
366 tap_packet *pkt; 552 tap_packet *pkt;
367 553
368 pkt = tap->recv (); 554 pkt = tap->recv ();
369 555
556 if (!pkt)
557 return;
558
559 if (pkt->len > 14)
560 {
370 int dst = mac2id (pkt->dst); 561 int dst = mac2id (pkt->dst);
371 int src = mac2id (pkt->src); 562 int src = mac2id (pkt->src);
372 563
373 if (src != THISNODE->id) 564 if (src != THISNODE->id)
374 {
375 slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating."));
376 exit (1);
377 }
378
379 if (dst == THISNODE->id)
380 {
381 slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating."));
382 exit (1);
383 }
384
385 if (dst > conns.size ())
386 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
387 else
388 {
389 if (dst)
390 { 565 {
391 // unicast 566 slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting."));
392 if (dst != THISNODE->id) 567 exit (1);
393 conns[dst - 1]->inject_data_packet (pkt);
394 } 568 }
569
570 if (dst == THISNODE->id)
571 {
572 slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting."));
573 exit (1);
574 }
575
576 if (dst > conns.size ())
577 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
395 else 578 else
396 {
397 // broadcast, first check router, then self, then english
398 connection *router = find_router ();
399
400 if (router)
401 router->inject_data_packet (pkt, true); 579 inject_data_packet (pkt, dst);
402 else
403 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
404 if ((*c)->conf != THISNODE)
405 (*c)->inject_data_packet (pkt);
406 }
407 } 580 }
408 581
409 delete pkt; 582 delete pkt;
410 } 583 }
411 else if (revents & (POLLHUP | POLLERR)) 584 else if (revents & (POLLHUP | POLLERR))
477 } 650 }
478} 651}
479 652
480connection *vpn::find_router () 653connection *vpn::find_router ()
481{ 654{
482 u32 prio = 0; 655 u32 prio = 1;
483 connection *router = 0; 656 connection *router = 0;
484 657
485 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 658 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
486 { 659 {
487 connection *c = *i; 660 connection *c = *i;
488 661
489 if (c->conf->routerprio > prio 662 if (c->conf->routerprio > prio
490 && c->connectmode == conf_node::C_ALWAYS 663 && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use
491 && c->conf != THISNODE
492 && c->ictx && c->octx) 664 && c->ictx && c->octx
665 && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode
493 { 666 {
494 prio = c->conf->routerprio; 667 prio = c->conf->routerprio;
495 router = c; 668 router = c;
496 } 669 }
497 } 670 }
506 if (c) 679 if (c)
507 c->send_connect_request (id); 680 c->send_connect_request (id);
508 else 681 else
509 // no router found, aggressively connect to all routers 682 // no router found, aggressively connect to all routers
510 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) 683 for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i)
511 if ((*i)->conf->routerprio) 684 if ((*i)->conf->routerprio && (*i)->conf != THISNODE)
512 (*i)->establish_connection (); 685 (*i)->establish_connection ();
513} 686}
514 687
515void 688void
516connection::dump_status () 689connection::dump_status ()
534 707
535 slog (L_NOTICE, _("END status dump")); 708 slog (L_NOTICE, _("END status dump"));
536} 709}
537 710
538vpn::vpn (void) 711vpn::vpn (void)
539: event(this, &vpn::event_cb) 712: event (this, &vpn::event_cb)
540, udpv4_ev_watcher(this, &vpn::udpv4_ev) 713, udpv4_ev_watcher (this, &vpn::udpv4_ev)
541, ipv4_ev_watcher (this, &vpn::ipv4_ev) 714, ipv4_ev_watcher (this, &vpn::ipv4_ev)
542, tap_ev_watcher (this, &vpn::tap_ev)
543#if ENABLE_TCP 715#if ENABLE_TCP
544, tcpv4_ev_watcher(this, &vpn::tcpv4_ev) 716, tcpv4_ev_watcher (this, &vpn::tcpv4_ev)
545#endif 717#endif
718#if ENABLE_ICMP
719, icmpv4_ev_watcher(this, &vpn::icmpv4_ev)
720#endif
721, tap_ev_watcher (this, &vpn::tap_ev)
546{ 722{
547} 723}
548 724
549vpn::~vpn () 725vpn::~vpn ()
550{ 726{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines