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