--- gvpe/src/device-cygwin.C 2003/10/15 00:25:15 1.3 +++ gvpe/src/device-cygwin.C 2008/08/07 17:54:26 1.12 @@ -1,22 +1,34 @@ /* device-cygwin.C -- Stub for Cygwin environment - Copyright (C) 2003 Marc Lehmann + Copyright (C) 2003-2008 Marc Lehmann Copyright (C) 2002-2003 Ivo Timmermans , 2002-2003 Guus Sliepen - This program 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. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - 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 - Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + 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 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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, see . + + Additional permission under GNU GPL version 3 section 7 + + If you modify this Program, or any covered work, by linking or + combining it with the OpenSSL project's OpenSSL library (or a modified + version of that library), containing parts covered by the terms of the + OpenSSL or SSLeay licenses, the licensors of this Program grant you + additional permission to convey the resulting work. Corresponding + Source for a non-source form of such a combination shall include the + source code for the parts of OpenSSL used as well as that of the + covered work. */ // unfortunately, there is be no way to set MAC addresses under windows, @@ -30,18 +42,19 @@ #include "config.h" -#include +#include +#include +#include #include #include #include #include #include -#include -#include #include "conf.h" #include "util.h" +#include #include #include @@ -53,11 +66,10 @@ #define TAP_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD | 8000, request, method, FILE_ANY_ACCESS) -#define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED) -#define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, METHOD_BUFFERED) -#define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED) - -static HANDLE device_handle = INVALID_HANDLE_VALUE; +#define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED) +#define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, METHOD_BUFFERED) +#define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED) +#define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE(7, METHOD_BUFFERED) static const char * wstrerror (int err) @@ -71,18 +83,63 @@ strncpy (buf, _("(unable to format errormessage)"), sizeof (buf)); }; - if ((char *newline = strchr (buf, '\r'))) - *newline = '\0'; + char *nl; + if ((nl = strchr (buf, '\r'))) + *nl = '\0'; return buf; } +static HANDLE device_handle = INVALID_HANDLE_VALUE; +static mac local_mac; +static tap_packet *rcv_pkt; +static int iopipe[2]; +static HANDLE pipe_handle, send_event, thread; + +static DWORD WINAPI +read_thread(void *) +{ + static OVERLAPPED overlapped; + static DWORD dlen; + static u32 len; + static u8 data[MAX_MTU]; + + overlapped.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); + + for (;;) + { + if (!ReadFile (device_handle, data, MAX_MTU, &dlen, &overlapped)) + { + if (GetLastError () == ERROR_IO_PENDING) + GetOverlappedResult (device_handle, &overlapped, &dlen, TRUE); + else + { + slog (L_ERR, "WIN32 TAP: ReadFile returned error: %s", wstrerror (GetLastError ())); + exit (EXIT_FAILURE); + } + } + + if (dlen > 0) + { + len = dlen; + WriteFile (pipe_handle, &len, sizeof (len), &dlen, NULL); + WriteFile (pipe_handle, data, len, &dlen, NULL); + } + } +} + const char * tap_device::info () { return _("cygwin cipe/openvpn tap device"); } +const char * +tap_device::if_up () +{ + return ""; +} + tap_device::tap_device () { HKEY key, key2; @@ -90,9 +147,9 @@ char regpath[1024]; char adapterid[1024]; - char adaptername[1024]; + BYTE adaptername[1024]; char tapname[1024]; - long len; + DWORD len; bool found = false; @@ -102,9 +159,9 @@ if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_CONTROL_NET, 0, KEY_READ, &key)) { - slog (L_ERR, _("Unable to read registry: %s"), + slog (L_ERR, _("WIN32 TAP: unable to read registry: %s"), wstrerror (GetLastError ())); - return false; + exit (EXIT_FAILURE); } for (i = 0;; i++) @@ -129,16 +186,20 @@ if (err) continue; - if (strcmp (conf.ifname, adapterid)) - continue; - - found = true; - break; - - snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, - adapterid); - device_handle = CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, indent: Standard input: 237: Error:Stmt nesting error.FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, - 0); + if (conf.ifname) + { + if (strcmp (conf.ifname, adapterid)) + continue; + } + else + { + found = true; + break; + } + + snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid); + device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, + OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); if (device_handle != INVALID_HANDLE_VALUE) { found = true; @@ -150,22 +211,15 @@ if (!found) { - slog (L_ERR, _("No Windows tap device found!")); - return false; + slog (L_ERR, _("WIN32 TAP: no windows tap device found!")); + exit (EXIT_FAILURE); } - if (!device) - device = xstrdup (adapterid); - - if (!iface) - iface = xstrdup (adaptername); - /* Try to open the corresponding tap device */ if (device_handle == INVALID_HANDLE_VALUE) { - snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, - device); + snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid); device_handle = CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, @@ -174,99 +228,47 @@ if (device_handle == INVALID_HANDLE_VALUE) { - slog (L_ERR, _("%s (%s) is not a usable Windows tap device: %s"), - device, iface, wstrerror (GetLastError ())); - return false; + slog (L_ERR, _("WIN32 TAP: %s is not a usable windows tap device %s: %s"), + adaptername, tapname, wstrerror (GetLastError ())); + exit (EXIT_FAILURE); } + strcpy (ifrname, (char *)tapname); + /* Get MAC address from tap device */ - if (!DeviceIoControl - (device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof (mymac.x), mymac.x, - sizeof (mymac.x), &len, 0)) + if (!DeviceIoControl (device_handle, TAP_IOCTL_GET_MAC, + &local_mac, sizeof (local_mac), &local_mac, sizeof (local_mac), + &len, 0)) { slog (L_ERR, - _ - ("Could not get MAC address from Windows tap device %s (%s): %s"), - device, iface, wstrerror (GetLastError ())); - return false; + _("WIN32 TAP: could not get MAC address from windows tap device %s: %s"), + adaptername, wstrerror (GetLastError ())); + exit (EXIT_FAILURE); } - if (routing_mode == RMODE_ROUTER) - { - overwrite_mac = 1; - } - - /* Create a listening socket */ - - err = getaddrinfo (NULL, myport, &hint, &ai); + pipe (iopipe); + fd = iopipe[0]; + pipe_handle = (HANDLE) get_osfhandle (iopipe[1]); - if (err || !ai) - { - slog (L_ERR, _("System call `%s' failed: %s"), "getaddrinfo", - gai_strerror (errno)); - return false; - } + send_event = CreateEvent (NULL, FALSE, FALSE, NULL); - sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol); - - if (sock < 0) - { - slog (L_ERR, _("System call `%s' failed: %s"), "socket", - strerror (errno)); - return false; - } - - if (bind (sock, ai->ai_addr, ai->ai_addrlen)) - { - slog (L_ERR, _("System call `%s' failed: %s"), "bind", - strerror (errno)); - return false; - } - - freeaddrinfo (ai); - - if (listen (sock, 1)) - { - slog (L_ERR, _("System call `%s' failed: %s"), "listen", - strerror (errno)); - return false; - } - - /* Start the tap reader */ - - thread = CreateThread (NULL, 0, tapreader, NULL, 0, NULL); - - if (!thread) - { - slog (L_ERR, _("System call `%s' failed: %s"), "CreateThread", - wstrerror (GetLastError ())); - return false; - } - - /* Wait for the tap reader to connect back to us */ - - if ((device_fd = accept (sock, NULL, 0)) == -1) - { - slog (L_ERR, _("System call `%s' failed: %s"), "accept", - strerror (errno)); - return false; - } - - closesocket (sock); - - device_info = _("Windows tap device"); - - slog (L_INFO, _("%s (%s) is a %s"), device, iface, device_info); - - return true; -} + thread = CreateThread (NULL, 0, read_thread, NULL, 0, NULL); + /* try to set driver media status to 'connected' */ + ULONG status = TRUE; + DeviceIoControl (device_handle, TAP_IOCTL_SET_MEDIA_STATUS, + &status, sizeof (status), + &status, sizeof (status), &len, NULL); + // ignore error here on purpose } tap_device::~tap_device () { - close (fd); + close (iopipe[0]); + close (iopipe[1]); + CloseHandle (device_handle); + CloseHandle (send_event); } tap_packet * @@ -274,24 +276,26 @@ { tap_packet *pkt = new tap_packet; - pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); - - if (pkt->len <= 0) + if (sizeof (u32) != read (iopipe[0], &pkt->len, sizeof (u32))) { - slog (L_ERR, _("error while reading from %s %s: %s"), - info (), conf.ifname, strerror (errno)); - free (pkt); + slog (L_ERR, _("WIN32 TAP: i/o thread delivered incomplete pkt length")); + delete pkt; return 0; } - + + if (pkt->len != read (iopipe[0], &((*pkt)[0]), pkt->len)) + { + slog (L_ERR, _("WIN32 TAP: i/o thread delivered incomplete pkt")); + delete pkt; + return 0; + } + id2mac (THISNODE->id, &((*pkt)[6])); if (pkt->is_arp ()) { - if ((*pkt)[22] == 0x08) - id2mac (THISNODE->id, &((*pkt)[22])); - if ((*pkt)[32] == 0x08) - id2mac (THISNODE->id, &((*pkt)[32])); + if (!memcmp (&(*pkt)[22], &local_mac, sizeof (mac))) id2mac (THISNODE->id, &((*pkt)[22])); + if (!memcmp (&(*pkt)[32], &local_mac, sizeof (mac))) id2mac (THISNODE->id, &((*pkt)[32])); } return pkt; @@ -300,130 +304,27 @@ void tap_device::send (tap_packet * pkt) { - (*pkt)[6] = 0x08; - (*pkt)[7] = 0x00; - (*pkt)[8] = 0x58; - (*pkt)[9] = 0x00; - (*pkt)[10] = 0x00; - (*pkt)[11] = 0x01; + memcpy (&(*pkt)[6], &local_mac, sizeof (mac)); if (pkt->is_arp ()) { if ((*pkt)[22] == 0xfe && (*pkt)[27] == THISNODE->id) - memcpy (&(*pkt)[22], &(*pkt)[6], sizeof (mac)); + memcpy (&(*pkt)[22], &local_mac, sizeof (mac)); if ((*pkt)[32] == 0xfe && (*pkt)[37] == THISNODE->id) - memcpy (&(*pkt)[32], &(*pkt)[6], sizeof (mac)); + memcpy (&(*pkt)[32], &local_mac, sizeof (mac)); } - if (write (fd, &((*pkt)[0]), pkt->len) < 0) - slog (L_ERR, _("can't write to %s %s: %s"), info (), conf.ifname, - strerror (errno)); -} - -#if 0 - -slog (L_DEBUG, _indent: Standard input:377: Error:Stmt nesting error. -("Tap reader running")); + DWORD dlen; + OVERLAPPED overlapped; + overlapped.hEvent = send_event; - /* Read from tap device and send to parent */ - -overlapped.hEvent = CreateEvent (NULL, TRUE, FALSE, NULL); - -for indent -: Standard input: 320: Error:Stmt nesting error.(;; - ) -{ - overlapped.Offset = 0; - overlapped.OffsetHigh = 0; - ResetEvent (overlapped.hEvent); - - status = ReadFile (device_handle, buf, sizeof (buf), &len, &overlapped); - - if (!status) + if (!WriteFile (device_handle, &((*pkt)[0]), pkt->len, &dlen, &overlapped)) { if (GetLastError () == ERROR_IO_PENDING) - { - WaitForSingleObject (overlapped.hEvent, INFINITE); - if (!GetOverlappedResult (device_handle, &overlapped, &len, FALSE)) - continue; - } + GetOverlappedResult (device_handle, &overlapped, &dlen, TRUE); else - { - slog (L_ERR, _("Error while reading from %s %s: %s"), - device_info, device, strerror (errno)); - return -1; - } - } - - if (send (sock, buf, len, 0) <= 0) - return -1; -} -} - -void -close_device (void) -{ - cp (); - - CloseHandle (device_handle); -} - -bool -read_packet (vpn_packet_t * packet) -{ - int lenin; - - cp (); - - if ((lenin = recv (device_fd, packet->data, MTU, 0)) <= 0) - { - slog (L_ERR, _("Error while reading from %s %s: %s"), device_info, - device, strerror (errno)); - return false; + slog (L_ERR, _("WIN32 TAP: can't write to %s %s: %s"), info (), conf.ifname, + wstrerror (GetLastError ())); } - - packet->len = lenin; - - device_total_in += packet->len; - - ifdebug (TRAFFIC) slog (L_DEBUG, _("Read packet of %d bytes from %s"), - packet->len, device_info); - - return true; -} - -bool -write_packet (vpn_packet_t * packet) -{ - long lenout; - OVERLAPPED overlapped = { 0 }; - - cp (); - - ifdebug (TRAFFIC) slog (L_DEBUG, _("Writing packet of %d bytes to %s"), - packet->len, device_info); - - if (!WriteFile - (device_handle, packet->data, packet->len, &lenout, &overlapped)) - { - slog (L_ERR, _("Error while writing to %s %s: %s"), device_info, - device, wstrerror (GetLastError ())); - return false; - } - - device_total_out += packet->len; - - return true; -} - -void -dump_device_stats (void) -{ - cp (); - - slog (L_DEBUG, _("Statistics for %s %s:"), device_info, device); - slog (L_DEBUG, _(" total bytes in: %10d"), device_total_in); - slog (L_DEBUG, _(" total bytes out: %10d"), device_total_out); } -#endif