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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines