… | |
… | |
19 | |
19 | |
20 | #include "config.h" |
20 | #include "config.h" |
21 | |
21 | |
22 | #include <list> |
22 | #include <list> |
23 | |
23 | |
|
|
24 | #include <cstdio> |
|
|
25 | #include <cstring> |
24 | #include <cstdlib> |
26 | #include <cstdlib> |
25 | #include <cstring> |
|
|
26 | #include <cstdio> |
|
|
27 | |
27 | |
28 | #include <sys/types.h> |
28 | #include <sys/types.h> |
29 | #include <sys/socket.h> |
29 | #include <sys/socket.h> |
30 | #include <sys/wait.h> |
30 | #include <sys/wait.h> |
31 | #include <errno.h> |
31 | #include <errno.h> |
… | |
… | |
98 | sockinfo si (THISNODE, PROT_IPv4); |
98 | sockinfo si (THISNODE, PROT_IPv4); |
99 | |
99 | |
100 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
100 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
101 | { |
101 | { |
102 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
102 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
103 | exit (1); |
103 | exit (EXIT_FAILURE); |
104 | } |
104 | } |
105 | |
105 | |
106 | ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); |
106 | ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); |
107 | } |
107 | } |
108 | |
108 | |
… | |
… | |
136 | sockinfo si (THISNODE, PROT_UDPv4); |
136 | sockinfo si (THISNODE, PROT_UDPv4); |
137 | |
137 | |
138 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
138 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
139 | { |
139 | { |
140 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
140 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
141 | exit (1); |
141 | exit (EXIT_FAILURE); |
142 | } |
142 | } |
143 | |
143 | |
144 | udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); |
144 | udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); |
145 | } |
145 | } |
146 | |
146 | |
… | |
… | |
180 | sockinfo si (THISNODE, PROT_ICMPv4); |
180 | sockinfo si (THISNODE, PROT_ICMPv4); |
181 | |
181 | |
182 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
182 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
183 | { |
183 | { |
184 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
184 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
185 | exit (1); |
185 | exit (EXIT_FAILURE); |
186 | } |
186 | } |
187 | |
187 | |
188 | icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); |
188 | icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); |
189 | } |
189 | } |
190 | #endif |
190 | #endif |
… | |
… | |
210 | sockinfo si (THISNODE, PROT_TCPv4); |
210 | sockinfo si (THISNODE, PROT_TCPv4); |
211 | |
211 | |
212 | if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
212 | if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
213 | { |
213 | { |
214 | slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
214 | slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
215 | exit (1); |
215 | exit (EXIT_FAILURE); |
216 | } |
216 | } |
217 | |
217 | |
218 | if (listen (tcpv4_fd, 5)) |
218 | if (listen (tcpv4_fd, 5)) |
219 | { |
219 | { |
220 | slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
220 | slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
221 | exit (1); |
221 | exit (EXIT_FAILURE); |
222 | } |
222 | } |
223 | |
223 | |
224 | tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); |
224 | tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); |
|
|
225 | } |
|
|
226 | #endif |
|
|
227 | |
|
|
228 | #if ENABLE_DNS |
|
|
229 | if (THISNODE->protocols & PROT_DNSv4) |
|
|
230 | { |
|
|
231 | dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
|
|
232 | |
|
|
233 | if (dnsv4_fd < 0) |
|
|
234 | return -1; |
|
|
235 | |
|
|
236 | // standard daemon practise... |
|
|
237 | { |
|
|
238 | int oval = 1; |
|
|
239 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
|
|
240 | } |
|
|
241 | |
|
|
242 | sockinfo si (THISNODE, PROT_DNSv4); |
|
|
243 | |
|
|
244 | if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
245 | { |
|
|
246 | slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
247 | exit (EXIT_FAILURE); |
|
|
248 | } |
|
|
249 | |
|
|
250 | dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); |
225 | } |
251 | } |
226 | #endif |
252 | #endif |
227 | |
253 | |
228 | tap = new tap_device (); |
254 | tap = new tap_device (); |
229 | if (!tap) //D this, of course, never catches |
255 | if (!tap) //D this, of course, never catches |
230 | { |
256 | { |
231 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
257 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
232 | exit (1); |
258 | exit (EXIT_FAILURE); |
233 | } |
259 | } |
234 | |
260 | |
235 | run_script (run_script_cb (this, &vpn::script_if_up), true); |
261 | run_script (run_script_cb (this, &vpn::script_if_up), true); |
236 | |
262 | |
237 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
263 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
… | |
… | |
259 | #endif |
285 | #endif |
260 | |
286 | |
261 | #if ENABLE_ICMP |
287 | #if ENABLE_ICMP |
262 | case PROT_ICMPv4: |
288 | case PROT_ICMPv4: |
263 | return send_icmpv4_packet (pkt, si, tos); |
289 | return send_icmpv4_packet (pkt, si, tos); |
|
|
290 | #endif |
|
|
291 | |
|
|
292 | #if ENABLE_DNS |
|
|
293 | case PROT_DNSv4: |
|
|
294 | return send_dnsv4_packet (pkt, si, tos); |
264 | #endif |
295 | #endif |
265 | |
296 | |
266 | default: |
297 | default: |
267 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
298 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
268 | return false; |
299 | return false; |
… | |
… | |
430 | else |
461 | else |
431 | { |
462 | { |
432 | slog (L_ERR, |
463 | slog (L_ERR, |
433 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
464 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
434 | revents); |
465 | revents); |
435 | exit (1); |
466 | exit (EXIT_FAILURE); |
436 | } |
467 | } |
437 | } |
468 | } |
438 | |
469 | |
439 | #if ENABLE_ICMP |
470 | #if ENABLE_ICMP |
440 | void |
471 | void |
… | |
… | |
478 | else |
509 | else |
479 | { |
510 | { |
480 | slog (L_ERR, |
511 | slog (L_ERR, |
481 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
512 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
482 | revents); |
513 | revents); |
483 | exit (1); |
514 | exit (EXIT_FAILURE); |
484 | } |
515 | } |
485 | } |
516 | } |
486 | #endif |
517 | #endif |
487 | |
518 | |
488 | void |
519 | void |
… | |
… | |
516 | else |
547 | else |
517 | { |
548 | { |
518 | slog (L_ERR, |
549 | slog (L_ERR, |
519 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
550 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
520 | revents); |
551 | revents); |
521 | exit (1); |
552 | exit (EXIT_FAILURE); |
522 | } |
553 | } |
523 | } |
554 | } |
524 | |
555 | |
525 | void |
556 | void |
526 | vpn::tap_ev (io_watcher &w, short revents) |
557 | vpn::tap_ev (io_watcher &w, short revents) |
… | |
… | |
541 | int src = mac2id (pkt->src); |
572 | int src = mac2id (pkt->src); |
542 | |
573 | |
543 | if (src != THISNODE->id) |
574 | if (src != THISNODE->id) |
544 | { |
575 | { |
545 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
576 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
546 | exit (1); |
577 | exit (EXIT_FAILURE); |
547 | } |
578 | } |
548 | |
579 | |
549 | if (dst == THISNODE->id) |
580 | if (dst == THISNODE->id) |
550 | { |
581 | { |
551 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
582 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
552 | exit (1); |
583 | exit (EXIT_FAILURE); |
553 | } |
584 | } |
554 | |
585 | |
555 | if (dst > conns.size ()) |
586 | if (dst > conns.size ()) |
556 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
587 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
557 | else |
588 | else |
… | |
… | |
572 | if (events & EVENT_SHUTDOWN) |
603 | if (events & EVENT_SHUTDOWN) |
573 | { |
604 | { |
574 | slog (L_INFO, _("preparing shutdown...")); |
605 | slog (L_INFO, _("preparing shutdown...")); |
575 | |
606 | |
576 | shutdown_all (); |
607 | shutdown_all (); |
577 | remove_pid (pidfilename); |
608 | remove_pid (conf.pidfilename); |
578 | slog (L_INFO, _("terminating")); |
609 | slog (L_INFO, _("terminating")); |
579 | exit (0); |
610 | exit (EXIT_SUCCESS); |
580 | } |
611 | } |
581 | |
612 | |
582 | if (events & EVENT_RECONNECT) |
613 | if (events & EVENT_RECONNECT) |
583 | { |
614 | { |
584 | slog (L_INFO, _("forced reconnect")); |
615 | slog (L_INFO, _("forced reconnect")); |
… | |
… | |
619 | } |
650 | } |
620 | } |
651 | } |
621 | |
652 | |
622 | connection *vpn::find_router () |
653 | connection *vpn::find_router () |
623 | { |
654 | { |
624 | u32 prio = 1; |
655 | u32 prio = 0; |
625 | connection *router = 0; |
656 | connection *router = 0; |
626 | |
657 | |
627 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
658 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
628 | { |
659 | { |
629 | connection *c = *i; |
660 | connection *c = *i; |
… | |
… | |
685 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
716 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
686 | #endif |
717 | #endif |
687 | #if ENABLE_ICMP |
718 | #if ENABLE_ICMP |
688 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
719 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
689 | #endif |
720 | #endif |
|
|
721 | #if ENABLE_DNS |
|
|
722 | , dnsv4_ev_watcher (this, &vpn::dnsv4_ev) |
|
|
723 | #endif |
690 | , tap_ev_watcher (this, &vpn::tap_ev) |
724 | , tap_ev_watcher (this, &vpn::tap_ev) |
691 | { |
725 | { |
692 | } |
726 | } |
693 | |
727 | |
694 | vpn::~vpn () |
728 | vpn::~vpn () |