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.3 by pcg, Wed Oct 15 00:25:15 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>, 4 Copyright (C) 2002-2003 Ivo Timmermans <ivo@o2w.nl>,
5 2002-2003 Guus Sliepen <guus@sliepen.eu.org> 5 2002-2003 Guus Sliepen <guus@sliepen.eu.org>
6 6
7 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
8 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
34#include <errno.h> 34#include <errno.h>
35#include <sys/types.h> 35#include <sys/types.h>
36#include <sys/stat.h> 36#include <sys/stat.h>
37#include <fcntl.h> 37#include <fcntl.h>
38#include <unistd.h> 38#include <unistd.h>
39#include <syslog.h>
40#include <cstring> 39#include <cstring>
41 40
42#include "conf.h" 41#include "conf.h"
43#include "util.h" 42#include "util.h"
44 43
44#include <io.h>
45#include <w32api/windows.h> 45#include <w32api/windows.h>
46#include <w32api/winioctl.h> 46#include <w32api/winioctl.h>
47 47
48#define REG_CONTROL_NET "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}" 48#define REG_CONTROL_NET "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
49 49
51#define USERDEVICEDIR "\\??\\" 51#define USERDEVICEDIR "\\??\\"
52#define TAPSUFFIX ".tap" 52#define TAPSUFFIX ".tap"
53 53
54#define TAP_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD | 8000, request, method, FILE_ANY_ACCESS) 54#define TAP_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD | 8000, request, method, FILE_ANY_ACCESS)
55 55
56#define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED) 56#define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED)
57#define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, 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) 58#define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED)
59 59#define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE(7, METHOD_BUFFERED)
60static HANDLE device_handle = INVALID_HANDLE_VALUE;
61 60
62static const char * 61static const char *
63wstrerror (int err) 62wstrerror (int err)
64{ 63{
65 static char buf[1024]; 64 static char buf[1024];
69 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), buf, sizeof (buf), NULL)) 68 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), buf, sizeof (buf), NULL))
70 { 69 {
71 strncpy (buf, _("(unable to format errormessage)"), sizeof (buf)); 70 strncpy (buf, _("(unable to format errormessage)"), sizeof (buf));
72 }; 71 };
73 72
73 char *nl;
74 if ((char *newline = strchr (buf, '\r'))) 74 if ((nl = strchr (buf, '\r')))
75 *newline = '\0'; 75 *nl = '\0';
76 76
77 return buf; 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 }
78} 116}
79 117
80const char * 118const char *
81tap_device::info () 119tap_device::info ()
82{ 120{
88 HKEY key, key2; 126 HKEY key, key2;
89 int i; 127 int i;
90 128
91 char regpath[1024]; 129 char regpath[1024];
92 char adapterid[1024]; 130 char adapterid[1024];
93 char adaptername[1024]; 131 BYTE adaptername[1024];
94 char tapname[1024]; 132 char tapname[1024];
95 long len; 133 DWORD len;
96 134
97 bool found = false; 135 bool found = false;
98 136
99 int sock, err; 137 int sock, err;
100 138
101 /* Open registry and look for network adapters */ 139 /* Open registry and look for network adapters */
102 140
103 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_CONTROL_NET, 0, KEY_READ, &key)) 141 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_CONTROL_NET, 0, KEY_READ, &key))
104 { 142 {
105 slog (L_ERR, _("Unable to read registry: %s"), 143 slog (L_ERR, _("WIN32 TAP: unable to read registry: %s"),
106 wstrerror (GetLastError ())); 144 wstrerror (GetLastError ()));
107 return false; 145 exit (1);
108 } 146 }
109 147
110 for (i = 0;; i++) 148 for (i = 0;; i++)
111 { 149 {
112 len = sizeof (adapterid); 150 len = sizeof (adapterid);
127 RegCloseKey (key2); 165 RegCloseKey (key2);
128 166
129 if (err) 167 if (err)
130 continue; 168 continue;
131 169
170 if (conf.ifname)
171 {
132 if (strcmp (conf.ifname, adapterid)) 172 if (strcmp (conf.ifname, adapterid))
133 continue; 173 continue;
134 174 }
175 else
176 {
135 found = true; 177 found = true;
136 break; 178 break;
179 }
137 180
138 snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, 181 snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
139 adapterid); 182 device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0,
140 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, 183 OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
141 0);
142 if (device_handle != INVALID_HANDLE_VALUE) 184 if (device_handle != INVALID_HANDLE_VALUE)
143 { 185 {
144 found = true; 186 found = true;
145 break; 187 break;
146 } 188 }
148 190
149 RegCloseKey (key); 191 RegCloseKey (key);
150 192
151 if (!found) 193 if (!found)
152 { 194 {
153 slog (L_ERR, _("No Windows tap device found!")); 195 slog (L_ERR, _("WIN32 TAP: no windows tap device found!"));
154 return false; 196 exit (1);
155 } 197 }
156
157 if (!device)
158 device = xstrdup (adapterid);
159
160 if (!iface)
161 iface = xstrdup (adaptername);
162 198
163 /* Try to open the corresponding tap device */ 199 /* Try to open the corresponding tap device */
164 200
165 if (device_handle == INVALID_HANDLE_VALUE) 201 if (device_handle == INVALID_HANDLE_VALUE)
166 { 202 {
167 snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, 203 snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
168 device);
169 device_handle = 204 device_handle =
170 CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, 205 CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0,
171 OPEN_EXISTING, 206 OPEN_EXISTING,
172 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); 207 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
173 } 208 }
174 209
175 if (device_handle == INVALID_HANDLE_VALUE) 210 if (device_handle == INVALID_HANDLE_VALUE)
176 { 211 {
177 slog (L_ERR, _("%s (%s) is not a usable Windows tap device: %s"), 212 slog (L_ERR, _("WIN32 TAP: %s is not a usable windows tap device %s: %s"),
178 device, iface, wstrerror (GetLastError ())); 213 adaptername, tapname, wstrerror (GetLastError ()));
179 return false; 214 exit (1);
180 } 215 }
216
217 strcpy (ifrname, (char *)tapname);
181 218
182 /* Get MAC address from tap device */ 219 /* Get MAC address from tap device */
183 220
184 if (!DeviceIoControl 221 if (!DeviceIoControl (device_handle, TAP_IOCTL_GET_MAC,
185 (device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof (mymac.x), mymac.x, 222 &local_mac, sizeof (local_mac), &local_mac, sizeof (local_mac),
186 sizeof (mymac.x), &len, 0)) 223 &len, 0))
187 { 224 {
188 slog (L_ERR, 225 slog (L_ERR,
189 _
190 ("Could not get MAC address from Windows tap device %s (%s): %s"), 226 _("WIN32 TAP: could not get MAC address from windows tap device %s: %s"),
191 device, iface, wstrerror (GetLastError ())); 227 adaptername, wstrerror (GetLastError ()));
192 return false; 228 exit (1);
193 }
194
195 if (routing_mode == RMODE_ROUTER)
196 { 229 }
197 overwrite_mac = 1;
198 }
199 230
200 /* Create a listening socket */ 231 pipe (iopipe);
232 fd = iopipe[0];
233 pipe_handle = (HANDLE) get_osfhandle (iopipe[1]);
201 234
202 err = getaddrinfo (NULL, myport, &hint, &ai); 235 send_event = CreateEvent (NULL, FALSE, FALSE, NULL);
203 236
204 if (err || !ai)
205 {
206 slog (L_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
207 gai_strerror (errno));
208 return false;
209 }
210
211 sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
212
213 if (sock < 0)
214 {
215 slog (L_ERR, _("System call `%s' failed: %s"), "socket",
216 strerror (errno));
217 return false;
218 }
219
220 if (bind (sock, ai->ai_addr, ai->ai_addrlen))
221 {
222 slog (L_ERR, _("System call `%s' failed: %s"), "bind",
223 strerror (errno));
224 return false;
225 }
226
227 freeaddrinfo (ai);
228
229 if (listen (sock, 1))
230 {
231 slog (L_ERR, _("System call `%s' failed: %s"), "listen",
232 strerror (errno));
233 return false;
234 }
235
236 /* Start the tap reader */
237
238 thread = CreateThread (NULL, 0, tapreader, NULL, 0, NULL); 237 thread = CreateThread (NULL, 0, read_thread, NULL, 0, NULL);
239 238
240 if (!thread) 239 /* try to set driver media status to 'connected' */
241 { 240 ULONG status = TRUE;
242 slog (L_ERR, _("System call `%s' failed: %s"), "CreateThread", 241 DeviceIoControl (device_handle, TAP_IOCTL_SET_MEDIA_STATUS,
243 wstrerror (GetLastError ())); 242 &status, sizeof (status),
244 return false; 243 &status, sizeof (status), &len, NULL);
245 } 244 // ignore error here on purpose
246
247 /* Wait for the tap reader to connect back to us */
248
249 if ((device_fd = accept (sock, NULL, 0)) == -1)
250 {
251 slog (L_ERR, _("System call `%s' failed: %s"), "accept",
252 strerror (errno));
253 return false;
254 }
255
256 closesocket (sock);
257
258 device_info = _("Windows tap device");
259
260 slog (L_INFO, _("%s (%s) is a %s"), device, iface, device_info);
261
262 return true;
263}
264
265} 245}
266 246
267tap_device::~tap_device () 247tap_device::~tap_device ()
268{ 248{
269 close (fd); 249 close (iopipe[0]);
250 close (iopipe[1]);
251 CloseHandle (device_handle);
252 CloseHandle (send_event);
270} 253}
271 254
272tap_packet * 255tap_packet *
273tap_device::recv () 256tap_device::recv ()
274{ 257{
275 tap_packet *pkt = new tap_packet; 258 tap_packet *pkt = new tap_packet;
276 259
277 pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); 260 if (sizeof (u32) != read (iopipe[0], &pkt->len, sizeof (u32)))
278
279 if (pkt->len <= 0)
280 { 261 {
281 slog (L_ERR, _("error while reading from %s %s: %s"), 262 slog (L_ERR, _("WIN32 TAP: i/o thread delivered incomplete pkt length"));
282 info (), conf.ifname, strerror (errno)); 263 delete pkt;
283 free (pkt);
284 return 0; 264 return 0;
285 } 265 }
286 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
287 id2mac (THISNODE->id, &((*pkt)[6])); 274 id2mac (THISNODE->id, &((*pkt)[6]));
288 275
289 if (pkt->is_arp ()) 276 if (pkt->is_arp ())
290 { 277 {
291 if ((*pkt)[22] == 0x08) 278 if (!memcmp (&(*pkt)[22], &local_mac, sizeof (mac))) id2mac (THISNODE->id, &((*pkt)[22]));
292 id2mac (THISNODE->id, &((*pkt)[22])); 279 if (!memcmp (&(*pkt)[32], &local_mac, sizeof (mac))) id2mac (THISNODE->id, &((*pkt)[32]));
293 if ((*pkt)[32] == 0x08)
294 id2mac (THISNODE->id, &((*pkt)[32]));
295 } 280 }
296 281
297 return pkt; 282 return pkt;
298} 283}
299 284
300void 285void
301tap_device::send (tap_packet * pkt) 286tap_device::send (tap_packet * pkt)
302{ 287{
303 (*pkt)[6] = 0x08; 288 memcpy (&(*pkt)[6], &local_mac, sizeof (mac));
304 (*pkt)[7] = 0x00;
305 (*pkt)[8] = 0x58;
306 (*pkt)[9] = 0x00;
307 (*pkt)[10] = 0x00;
308 (*pkt)[11] = 0x01;
309 289
310 if (pkt->is_arp ()) 290 if (pkt->is_arp ())
311 { 291 {
312 if ((*pkt)[22] == 0xfe && (*pkt)[27] == THISNODE->id) 292 if ((*pkt)[22] == 0xfe && (*pkt)[27] == THISNODE->id)
313 memcpy (&(*pkt)[22], &(*pkt)[6], sizeof (mac)); 293 memcpy (&(*pkt)[22], &local_mac, sizeof (mac));
314 294
315 if ((*pkt)[32] == 0xfe && (*pkt)[37] == THISNODE->id) 295 if ((*pkt)[32] == 0xfe && (*pkt)[37] == THISNODE->id)
316 memcpy (&(*pkt)[32], &(*pkt)[6], sizeof (mac)); 296 memcpy (&(*pkt)[32], &local_mac, sizeof (mac));
317 } 297 }
318 298
319 if (write (fd, &((*pkt)[0]), pkt->len) < 0) 299 DWORD dlen;
320 slog (L_ERR, _("can't write to %s %s: %s"), info (), conf.ifname, 300 OVERLAPPED overlapped;
321 strerror (errno)); 301 overlapped.hEvent = send_event;
322}
323 302
324#if 0 303 if (!WriteFile (device_handle, &((*pkt)[0]), pkt->len, &dlen, &overlapped))
325
326slog (L_DEBUG, _indent: Standard input:377: Error:Stmt nesting error.
327("Tap reader running"));
328
329 /* Read from tap device and send to parent */
330
331overlapped.hEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
332
333for indent
334: Standard input: 320: Error:Stmt nesting error.(;;
335 )
336{
337 overlapped.Offset = 0;
338 overlapped.OffsetHigh = 0;
339 ResetEvent (overlapped.hEvent);
340
341 status = ReadFile (device_handle, buf, sizeof (buf), &len, &overlapped);
342
343 if (!status)
344 { 304 {
345 if (GetLastError () == ERROR_IO_PENDING) 305 if (GetLastError () == ERROR_IO_PENDING)
346 {
347 WaitForSingleObject (overlapped.hEvent, INFINITE);
348 if (!GetOverlappedResult (device_handle, &overlapped, &len, FALSE)) 306 GetOverlappedResult (device_handle, &overlapped, &dlen, TRUE);
349 continue;
350 }
351 else 307 else
352 { 308 slog (L_ERR, _("WIN32 TAP: can't write to %s %s: %s"), info (), conf.ifname,
353 slog (L_ERR, _("Error while reading from %s %s: %s"), 309 wstrerror (GetLastError ()));
354 device_info, device, strerror (errno));
355 return -1;
356 }
357 }
358
359 if (send (sock, buf, len, 0) <= 0)
360 return -1;
361}
362}
363
364void
365close_device (void)
366{
367 cp ();
368
369 CloseHandle (device_handle);
370}
371
372bool
373read_packet (vpn_packet_t * packet)
374{
375 int lenin;
376
377 cp ();
378
379 if ((lenin = recv (device_fd, packet->data, MTU, 0)) <= 0)
380 { 310 }
381 slog (L_ERR, _("Error while reading from %s %s: %s"), device_info,
382 device, strerror (errno));
383 return false;
384 }
385
386 packet->len = lenin;
387
388 device_total_in += packet->len;
389
390 ifdebug (TRAFFIC) slog (L_DEBUG, _("Read packet of %d bytes from %s"),
391 packet->len, device_info);
392
393 return true;
394} 311}
395
396bool
397write_packet (vpn_packet_t * packet)
398{
399 long lenout;
400 OVERLAPPED overlapped = { 0 };
401
402 cp ();
403
404 ifdebug (TRAFFIC) slog (L_DEBUG, _("Writing packet of %d bytes to %s"),
405 packet->len, device_info);
406
407 if (!WriteFile
408 (device_handle, packet->data, packet->len, &lenout, &overlapped))
409 {
410 slog (L_ERR, _("Error while writing to %s %s: %s"), device_info,
411 device, wstrerror (GetLastError ()));
412 return false;
413 }
414
415 device_total_out += packet->len;
416
417 return true;
418}
419
420void
421dump_device_stats (void)
422{
423 cp ();
424
425 slog (L_DEBUG, _("Statistics for %s %s:"), device_info, device);
426 slog (L_DEBUG, _(" total bytes in: %10d"), device_total_in);
427 slog (L_DEBUG, _(" total bytes out: %10d"), device_total_out);
428}
429#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines