ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/device-cygwin.C
(Generate patch)

Comparing gvpe/src/device-cygwin.C (file contents):
Revision 1.2 by pcg, Tue Oct 14 03:22:09 2003 UTC vs.
Revision 1.9 by pcg, Thu Mar 3 16:54:34 2005 UTC

1/* 1/*
2 device-cygwin.C -- Stub for Cygwin environment 2 device-cygwin.C -- Stub for Cygwin environment
3 Copyright (C) 2003 Marc Lehmann <ocg@goof.com> 3 Copyright (C) 2003-2005 Marc Lehmann <ocg@goof.com>
4 Copyright (C) 2002-2003 Ivo Timmermans <ivo@o2w.nl>,
5 2002-2003 Guus Sliepen <guus@sliepen.eu.org>
4 6
7 This file is part of GVPE.
8
5 This program is free software; you can redistribute it and/or modify 9 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 10 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 11 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 12 (at your option) any later version.
9 13
10 This program is distributed in the hope that it will be useful, 14 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 17 GNU General Public License for more details.
14 18
15 You should have received a copy of the GNU General Public License 19 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software 20 along with gvpe; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/ 22*/
19 23
20// unfortunately, there is be no way to set MAC addresses under windows, 24// unfortunately, there is be no way to set MAC addresses under windows,
21// and the default cipedrvr uses a different MAC address than we do, 25// and the default cipedrvr uses a different MAC address than we do,
22// so this module tries to fix mac addresses in packets and arp packets. 26// so this module tries to fix mac addresses in packets and arp packets.
23// this is probably not very fast, but neither is cygwin nor poll. 27// this is probably not very fast, but neither is cygwin nor poll.
24// 28//
25// http://cipe-win32.sourceforge.net/ 29// http://cipe-win32.sourceforge.net/
30// a newer driver is available as part of the openvpn package:
31// http://openvpn.sf.net/
26 32
27#include "config.h" 33#include "config.h"
28 34
29#include <stdio.h> 35#include <cstdio>
36#include <cstring>
37#include <cstdlib>
30#include <errno.h> 38#include <errno.h>
31#include <sys/types.h> 39#include <sys/types.h>
32#include <sys/stat.h> 40#include <sys/stat.h>
33#include <fcntl.h> 41#include <fcntl.h>
34#include <unistd.h> 42#include <unistd.h>
35#include <syslog.h>
36#include <cstring>
37 43
38#include "conf.h" 44#include "conf.h"
39#include "util.h" 45#include "util.h"
40 46
47#include <io.h>
48#include <w32api/windows.h>
49#include <w32api/winioctl.h>
50
51#define REG_CONTROL_NET "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
52
53#define USERMODEDEVICEDIR "\\\\.\\"
54#define USERDEVICEDIR "\\??\\"
55#define TAPSUFFIX ".tap"
56
57#define TAP_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD | 8000, request, method, FILE_ANY_ACCESS)
58
59#define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED)
60#define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, METHOD_BUFFERED)
61#define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED)
62#define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE(7, METHOD_BUFFERED)
63
64static const char *
65wstrerror (int err)
66{
67 static char buf[1024];
68
69 if (!FormatMessage
70 (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err,
71 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), buf, sizeof (buf), NULL))
72 {
73 strncpy (buf, _("(unable to format errormessage)"), sizeof (buf));
74 };
75
76 char *nl;
77 if ((nl = strchr (buf, '\r')))
78 *nl = '\0';
79
80 return buf;
81}
82
83static HANDLE device_handle = INVALID_HANDLE_VALUE;
84static mac local_mac;
85static tap_packet *rcv_pkt;
86static int iopipe[2];
87static HANDLE pipe_handle, send_event, thread;
88
89static DWORD WINAPI
90read_thread(void *)
91{
92 static OVERLAPPED overlapped;
93 static DWORD dlen;
94 static u32 len;
95 static u8 data[MAX_MTU];
96
97 overlapped.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
98
99 for (;;)
100 {
101 if (!ReadFile (device_handle, data, MAX_MTU, &dlen, &overlapped))
102 {
103 if (GetLastError () == ERROR_IO_PENDING)
104 GetOverlappedResult (device_handle, &overlapped, &dlen, TRUE);
105 else
106 {
107 slog (L_ERR, "WIN32 TAP: ReadFile returned error: %s", wstrerror (GetLastError ()));
108 exit (EXIT_FAILURE);
109 }
110 }
111
112 if (dlen > 0)
113 {
114 len = dlen;
115 WriteFile (pipe_handle, &len, sizeof (len), &dlen, NULL);
116 WriteFile (pipe_handle, data, len, &dlen, NULL);
117 }
118 }
119}
120
41const char * 121const char *
42tap_device::info () 122tap_device::info ()
43{ 123{
44 return _("broken cygwin cipe device"); 124 return _("cygwin cipe/openvpn tap device");
45} 125}
46 126
47tap_device::tap_device () 127tap_device::tap_device ()
48{ 128{
49 if ((fd = open (conf.ifname, O_RDWR)) < 0) 129 HKEY key, key2;
50 { 130 int i;
51 slog (L_CRIT, _("could not open %s: %s"), conf.ifname, strerror (errno)); 131
52 exit (1); 132 char regpath[1024];
133 char adapterid[1024];
134 BYTE adaptername[1024];
135 char tapname[1024];
136 DWORD len;
137
138 bool found = false;
139
140 int sock, err;
141
142 /* Open registry and look for network adapters */
143
144 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_CONTROL_NET, 0, KEY_READ, &key))
53 } 145 {
146 slog (L_ERR, _("WIN32 TAP: unable to read registry: %s"),
147 wstrerror (GetLastError ()));
148 exit (EXIT_FAILURE);
149 }
150
151 for (i = 0;; i++)
152 {
153 len = sizeof (adapterid);
154 if (RegEnumKeyEx (key, i, adapterid, &len, 0, 0, 0, NULL))
155 break;
156
157 /* Find out more about this adapter */
158
159 snprintf (regpath, sizeof (regpath), "%s\\%s\\Connection",
160 REG_CONTROL_NET, adapterid);
161
162 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2))
163 continue;
164
165 len = sizeof (adaptername);
166 err = RegQueryValueEx (key2, "Name", 0, 0, adaptername, &len);
167
168 RegCloseKey (key2);
169
170 if (err)
171 continue;
172
173 if (conf.ifname)
174 {
175 if (strcmp (conf.ifname, adapterid))
176 continue;
177 }
178 else
179 {
180 found = true;
181 break;
182 }
183
184 snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
185 device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0,
186 OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
187 if (device_handle != INVALID_HANDLE_VALUE)
188 {
189 found = true;
190 break;
191 }
192 }
193
194 RegCloseKey (key);
195
196 if (!found)
197 {
198 slog (L_ERR, _("WIN32 TAP: no windows tap device found!"));
199 exit (EXIT_FAILURE);
200 }
201
202 /* Try to open the corresponding tap device */
203
204 if (device_handle == INVALID_HANDLE_VALUE)
205 {
206 snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
207 device_handle =
208 CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0,
209 OPEN_EXISTING,
210 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
211 }
212
213 if (device_handle == INVALID_HANDLE_VALUE)
214 {
215 slog (L_ERR, _("WIN32 TAP: %s is not a usable windows tap device %s: %s"),
216 adaptername, tapname, wstrerror (GetLastError ()));
217 exit (EXIT_FAILURE);
218 }
219
220 strcpy (ifrname, (char *)tapname);
221
222 /* Get MAC address from tap device */
223
224 if (!DeviceIoControl (device_handle, TAP_IOCTL_GET_MAC,
225 &local_mac, sizeof (local_mac), &local_mac, sizeof (local_mac),
226 &len, 0))
227 {
228 slog (L_ERR,
229 _("WIN32 TAP: could not get MAC address from windows tap device %s: %s"),
230 adaptername, wstrerror (GetLastError ()));
231 exit (EXIT_FAILURE);
232 }
233
234 pipe (iopipe);
235 fd = iopipe[0];
236 pipe_handle = (HANDLE) get_osfhandle (iopipe[1]);
237
238 send_event = CreateEvent (NULL, FALSE, FALSE, NULL);
239
240 thread = CreateThread (NULL, 0, read_thread, NULL, 0, NULL);
241
242 /* try to set driver media status to 'connected' */
243 ULONG status = TRUE;
244 DeviceIoControl (device_handle, TAP_IOCTL_SET_MEDIA_STATUS,
245 &status, sizeof (status),
246 &status, sizeof (status), &len, NULL);
247 // ignore error here on purpose
54} 248}
55 249
56tap_device::~tap_device () 250tap_device::~tap_device ()
57{ 251{
58 close (fd); 252 close (iopipe[0]);
253 close (iopipe[1]);
254 CloseHandle (device_handle);
255 CloseHandle (send_event);
59} 256}
60 257
61tap_packet * 258tap_packet *
62tap_device::recv () 259tap_device::recv ()
63{ 260{
64 tap_packet *pkt = new tap_packet; 261 tap_packet *pkt = new tap_packet;
65 262
66 pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); 263 if (sizeof (u32) != read (iopipe[0], &pkt->len, sizeof (u32)))
67
68 if (pkt->len <= 0)
69 { 264 {
70 slog (L_ERR, _("error while reading from %s %s: %s"), 265 slog (L_ERR, _("WIN32 TAP: i/o thread delivered incomplete pkt length"));
71 info (), conf.ifname, strerror (errno)); 266 delete pkt;
72 free (pkt);
73 return 0; 267 return 0;
74 } 268 }
75 269
270 if (pkt->len != read (iopipe[0], &((*pkt)[0]), pkt->len))
271 {
272 slog (L_ERR, _("WIN32 TAP: i/o thread delivered incomplete pkt"));
273 delete pkt;
274 return 0;
275 }
276
76 id2mac (THISNODE->id, &((*pkt)[6])); 277 id2mac (THISNODE->id, &((*pkt)[6]));
77 278
78 if (pkt->is_arp ()) 279 if (pkt->is_arp ())
79 { 280 {
80 if ((*pkt)[22] == 0x08) id2mac (THISNODE->id, &((*pkt)[22])); 281 if (!memcmp (&(*pkt)[22], &local_mac, sizeof (mac))) id2mac (THISNODE->id, &((*pkt)[22]));
81 if ((*pkt)[32] == 0x08) id2mac (THISNODE->id, &((*pkt)[32])); 282 if (!memcmp (&(*pkt)[32], &local_mac, sizeof (mac))) id2mac (THISNODE->id, &((*pkt)[32]));
82 } 283 }
83 284
84 return pkt; 285 return pkt;
85} 286}
86 287
87void 288void
88tap_device::send (tap_packet *pkt) 289tap_device::send (tap_packet * pkt)
89{ 290{
90 (*pkt)[ 6] = 0x08; (*pkt)[ 7] = 0x00; (*pkt)[ 8] = 0x58; 291 memcpy (&(*pkt)[6], &local_mac, sizeof (mac));
91 (*pkt)[ 9] = 0x00; (*pkt)[10] = 0x00; (*pkt)[11] = 0x01;
92 292
93 if (pkt->is_arp ()) 293 if (pkt->is_arp ())
94 { 294 {
95 if ((*pkt)[22] == 0xfe && (*pkt)[27] == THISNODE->id) 295 if ((*pkt)[22] == 0xfe && (*pkt)[27] == THISNODE->id)
96 memcpy (&(*pkt)[22], &(*pkt)[6], sizeof (mac)); 296 memcpy (&(*pkt)[22], &local_mac, sizeof (mac));
97 297
98 if ((*pkt)[32] == 0xfe && (*pkt)[37] == THISNODE->id) 298 if ((*pkt)[32] == 0xfe && (*pkt)[37] == THISNODE->id)
99 memcpy (&(*pkt)[32], &(*pkt)[6], sizeof (mac)); 299 memcpy (&(*pkt)[32], &local_mac, sizeof (mac));
300 }
301
302 DWORD dlen;
303 OVERLAPPED overlapped;
304 overlapped.hEvent = send_event;
305
306 if (!WriteFile (device_handle, &((*pkt)[0]), pkt->len, &dlen, &overlapped))
100 } 307 {
101 308 if (GetLastError () == ERROR_IO_PENDING)
102 if (write (fd, &((*pkt)[0]), pkt->len) < 0) 309 GetOverlappedResult (device_handle, &overlapped, &dlen, TRUE);
310 else
103 slog (L_ERR, _("can't write to %s %s: %s"), info (), conf.ifname, 311 slog (L_ERR, _("WIN32 TAP: can't write to %s %s: %s"), info (), conf.ifname,
104 strerror (errno)); 312 wstrerror (GetLastError ()));
313 }
105} 314}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines