1 | /* |
1 | /* |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
3 | Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
|
|
5 | This file is part of GVPE. |
|
|
6 | |
5 | This program is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
9 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
10 | (at your option) any later version. |
9 | |
11 | |
10 | 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, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
14 | |
16 | |
15 | 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 |
16 | along with this program; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ |
20 | */ |
19 | |
21 | |
20 | #include "config.h" |
22 | #include "config.h" |
21 | |
23 | |
… | |
… | |
223 | |
225 | |
224 | tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); |
226 | tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); |
225 | } |
227 | } |
226 | #endif |
228 | #endif |
227 | |
229 | |
|
|
230 | #if ENABLE_DNS |
|
|
231 | if (THISNODE->protocols & PROT_DNSv4) |
|
|
232 | { |
|
|
233 | dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
|
|
234 | |
|
|
235 | dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
|
|
236 | |
|
|
237 | if (dnsv4_fd < 0) |
|
|
238 | return -1; |
|
|
239 | |
|
|
240 | // standard daemon practise... |
|
|
241 | { |
|
|
242 | int oval = 1; |
|
|
243 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
|
|
244 | } |
|
|
245 | |
|
|
246 | sockinfo si (THISNODE, PROT_DNSv4); |
|
|
247 | |
|
|
248 | if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
249 | { |
|
|
250 | slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
251 | exit (EXIT_FAILURE); |
|
|
252 | } |
|
|
253 | |
|
|
254 | dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); |
|
|
255 | } |
|
|
256 | #endif |
|
|
257 | |
228 | tap = new tap_device (); |
258 | tap = new tap_device (); |
229 | if (!tap) //D this, of course, never catches |
259 | if (!tap) //D this, of course, never catches |
230 | { |
260 | { |
231 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
261 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
232 | exit (EXIT_FAILURE); |
262 | exit (EXIT_FAILURE); |
… | |
… | |
237 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
267 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
238 | |
268 | |
239 | reconnect_all (); |
269 | reconnect_all (); |
240 | |
270 | |
241 | return 0; |
271 | return 0; |
242 | } |
|
|
243 | |
|
|
244 | // send a vpn packet out to other hosts |
|
|
245 | bool |
|
|
246 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
|
|
247 | { |
|
|
248 | switch (si.prot) |
|
|
249 | { |
|
|
250 | case PROT_IPv4: |
|
|
251 | return send_ipv4_packet (pkt, si, tos); |
|
|
252 | |
|
|
253 | case PROT_UDPv4: |
|
|
254 | return send_udpv4_packet (pkt, si, tos); |
|
|
255 | |
|
|
256 | #if ENABLE_TCP |
|
|
257 | case PROT_TCPv4: |
|
|
258 | return send_tcpv4_packet (pkt, si, tos); |
|
|
259 | #endif |
|
|
260 | |
|
|
261 | #if ENABLE_ICMP |
|
|
262 | case PROT_ICMPv4: |
|
|
263 | return send_icmpv4_packet (pkt, si, tos); |
|
|
264 | #endif |
|
|
265 | |
|
|
266 | default: |
|
|
267 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
|
|
268 | return false; |
|
|
269 | } |
|
|
270 | } |
272 | } |
271 | |
273 | |
272 | bool |
274 | bool |
273 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
275 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
274 | { |
276 | { |
… | |
… | |
572 | if (events & EVENT_SHUTDOWN) |
574 | if (events & EVENT_SHUTDOWN) |
573 | { |
575 | { |
574 | slog (L_INFO, _("preparing shutdown...")); |
576 | slog (L_INFO, _("preparing shutdown...")); |
575 | |
577 | |
576 | shutdown_all (); |
578 | shutdown_all (); |
577 | remove_pid (pidfilename); |
579 | remove_pid (conf.pidfilename); |
578 | slog (L_INFO, _("terminating")); |
580 | slog (L_INFO, _("terminating")); |
579 | exit (EXIT_SUCCESS); |
581 | exit (EXIT_SUCCESS); |
580 | } |
582 | } |
581 | |
583 | |
582 | if (events & EVENT_RECONNECT) |
584 | if (events & EVENT_RECONNECT) |
… | |
… | |
608 | connection_init (); |
610 | connection_init (); |
609 | |
611 | |
610 | for (configuration::node_vector::iterator i = conf.nodes.begin (); |
612 | for (configuration::node_vector::iterator i = conf.nodes.begin (); |
611 | i != conf.nodes.end (); ++i) |
613 | i != conf.nodes.end (); ++i) |
612 | { |
614 | { |
613 | connection *conn = new connection (this); |
615 | connection *conn = new connection (this, *i); |
614 | |
|
|
615 | conn->conf = *i; |
|
|
616 | conns.push_back (conn); |
616 | conns.push_back (conn); |
617 | |
|
|
618 | conn->establish_connection (); |
617 | conn->establish_connection (); |
619 | } |
618 | } |
620 | } |
619 | } |
621 | |
620 | |
622 | connection *vpn::find_router () |
621 | connection *vpn::find_router () |
… | |
… | |
685 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
684 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
686 | #endif |
685 | #endif |
687 | #if ENABLE_ICMP |
686 | #if ENABLE_ICMP |
688 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
687 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
689 | #endif |
688 | #endif |
|
|
689 | #if ENABLE_DNS |
|
|
690 | , dnsv4_ev_watcher (this, &vpn::dnsv4_ev) |
|
|
691 | #endif |
690 | , tap_ev_watcher (this, &vpn::tap_ev) |
692 | , tap_ev_watcher (this, &vpn::tap_ev) |
691 | { |
693 | { |
692 | } |
694 | } |
693 | |
695 | |
694 | vpn::~vpn () |
696 | vpn::~vpn () |