ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/device-cygwin.C
Revision: 1.10
Committed: Wed Mar 23 21:55:39 2005 UTC (19 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_9
Changes since 1.9: +6 -0 lines
Log Message:
*** empty log message ***

File Contents

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