--- gvpe/src/device-tincd.C 2003/10/14 03:59:32 1.2 +++ gvpe/src/device-tincd.C 2005/03/25 15:38:07 1.17 @@ -1,7 +1,10 @@ /* device-tincd.C -- include one of the tincd low level implementations. + Copyright (C) 2003-2005 Marc Lehmann - This program is free software; you can redistribute it and/or modify + This file is part of GVPE. + + GVPE is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -12,7 +15,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software + along with gvpe; if not, write to the Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ @@ -29,6 +32,12 @@ #include "conf.h" +// following headers used by cygwin (maybe others) +#include "netcompat.h" +#include + +#define xstrdup(strd) strdup(str) + /* make the tincd sources feel comfortable in our environment. */ /* this was reasonably easy to do. */ #define routing_mode 1 @@ -54,7 +63,7 @@ #define cp() #define lookup_config(config_tree,key) (key) -#define MTU MAXSIZE +#define MTU MAX_MTU // BIGGEST hack of 'em all // will be casted to data_packet, due to structural similarity @@ -62,8 +71,6 @@ u8 data[MAXSIZE]; }; -static tap_device *self; - static bool overwrite_mac; static bool @@ -86,24 +93,56 @@ #if IF_linux # include "tincd/linux/device.c" +const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME hw ether $MAC mtu $MTU"; } + #elif IF_freebsd # include "tincd/freebsd/device.c" +const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME ether $MAC mtu $MTU"; } + #elif IF_netbsd +# define IF_istun 1 # include "tincd/netbsd/device.c" +const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME mtu $MTU"; } + +#elif IF_openbsd +# define IF_istun 1 +# include "tincd/openbsd/device.c" +const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME mtu $MTU"; } + #elif IF_solaris +# define IF_istun 1 # include "tincd/solaris/device.c" +const char * tap_device::if_up () { return ""; } + #elif IF_cygwin # include "tincd/cygwin/device.c" +const char * tap_device::if_up () { return ""; } + #elif IF_mingw # include "tincd/mingw/device.c" +const char * tap_device::if_up () { return ""; } + #elif IF_darwin +# define IF_istun 1 # include "tincd/darwin/device.c" +const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME ether $MAC mtu $MTU"; } + #elif IF_raw_socket # include "tincd/raw_socket/device.c" +const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME ether $MAC mtu $MTU"; } + +#elif IF_uml_socket +# include "tincd/uml_socket/device.c" +const char * tap_device::if_up () { return 0; } + #else # error No interface implementation for your IFTYPE/IFSUBTYPE combination. #endif +#if IF_istun +# include "ether_emu.C" +#endif + const char * tap_device::info () { @@ -112,18 +151,23 @@ tap_device::tap_device () { - self = this; + device = "(null)"; - if (setup_device ()) + bool ok = setup_device (); + + if (device_info) + device = device_info; + + if (ok) { - //slog (L_DEBUG, _("%s is a %s"), device, info ()); + slog (L_DEBUG, _("interface %s on %s initialized"), info (), device); fd = device_fd; strcpy (ifrname, iface); } else { - slog (L_ERR, _("error while configuring tincd device (%s/%s)"), device, info ()); - exit (1); + slog (L_ERR, _("error while configuring tincd device %s on %s"), info (), device); + exit (EXIT_FAILURE); } } @@ -140,19 +184,35 @@ if (!read_packet (reinterpret_cast(pkt))) { delete pkt; - slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE, + slog (L_ERR, _("can't read from to %s %s: %s"), info (), device, strerror (errno)); return 0; } +#if IF_istun + // assume ipv4 + (*pkt)[12] = 0x08; + (*pkt)[13] = 0x00; + + if (!ether_emu.tun_to_tap (pkt)) + { + delete pkt; + return 0; + } +#endif + return pkt; } void tap_device::send (tap_packet *pkt) { - if (!write_packet (reinterpret_cast(pkt))) - slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, + if ( +#if IF_istun + ether_emu.tap_to_tun (pkt) && +#endif + !write_packet (reinterpret_cast(pkt))) + slog (L_ERR, _("can't write to %s %s: %s"), info (), device, strerror (errno)); }