1 | /* |
1 | /* |
2 | device.c -- Interaction with Windows tap driver in a Cygwin environment |
2 | device.c -- Interaction with Windows tap driver in a Cygwin environment |
3 | Copyright (C) 2002-2003 Ivo Timmermans <ivo@o2w.nl>, |
3 | Copyright (C) 2002-2004 Ivo Timmermans <ivo@tinc-vpn.org>, |
4 | 2002-2003 Guus Sliepen <guus@sliepen.eu.org> |
4 | 2002-2004 Guus Sliepen <guus@tinc-vpn.org> |
5 | |
5 | |
6 | This program is free software; you can redistribute it and/or modify |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
7 | it under the terms of the GNU General Public License as published by |
8 | the Free Software Foundation; either version 2 of the License, or |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. |
9 | (at your option) any later version. |
… | |
… | |
15 | |
15 | |
16 | You should have received a copy of the GNU General Public License |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | |
19 | |
20 | $Id: device.c,v 1.1 2003/10/14 03:22:09 pcg Exp $ |
20 | $Id: device.c,v 1.2 2005/03/17 23:59:37 pcg Exp $ |
21 | */ |
21 | */ |
22 | |
|
|
23 | |
22 | |
24 | #include <w32api/windows.h> |
23 | #include <w32api/windows.h> |
25 | #include <w32api/winioctl.h> |
24 | #include <w32api/winioctl.h> |
26 | |
25 | |
27 | |
26 | #include "mingw/common.h" |
28 | #define REG_CONTROL_NET "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}" |
|
|
29 | |
|
|
30 | #define USERMODEDEVICEDIR "\\\\.\\" |
|
|
31 | #define USERDEVICEDIR "\\??\\" |
|
|
32 | #define TAPSUFFIX ".tap" |
|
|
33 | |
|
|
34 | #define TAP_CONTROL_CODE(request,method) CTL_CODE(FILE_DEVICE_PHYSICAL_NETCARD | 8000, request, method, FILE_ANY_ACCESS) |
|
|
35 | |
|
|
36 | #define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED) |
|
|
37 | #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, METHOD_BUFFERED) |
|
|
38 | #define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED) |
|
|
39 | |
27 | |
40 | int device_fd = -1; |
28 | int device_fd = -1; |
41 | static HANDLE device_handle = INVALID_HANDLE_VALUE; |
29 | static HANDLE device_handle = INVALID_HANDLE_VALUE; |
42 | char *device = NULL; |
30 | char *device = NULL; |
43 | char *iface = NULL; |
31 | char *iface = NULL; |
44 | char *device_info = NULL; |
32 | char *device_info = NULL; |
45 | |
33 | |
46 | int device_total_in = 0; |
34 | static int device_total_in = 0; |
47 | int device_total_out = 0; |
35 | static int device_total_out = 0; |
48 | |
36 | |
49 | pid_t reader_pid; |
37 | static pid_t reader_pid; |
50 | int sp[2]; |
38 | static int sp[2]; |
51 | |
39 | |
52 | bool setup_device(void) |
40 | bool setup_device(void) |
53 | { |
41 | { |
54 | HKEY key, key2; |
42 | HKEY key, key2; |
55 | int i; |
43 | int i, err; |
56 | |
44 | |
57 | char regpath[1024]; |
45 | char regpath[1024]; |
58 | char adapterid[1024]; |
46 | char adapterid[1024]; |
59 | char adaptername[1024]; |
47 | char adaptername[1024]; |
60 | char tapname[1024]; |
48 | char tapname[1024]; |
… | |
… | |
68 | get_config_string(lookup_config(config_tree, "Device"), &device); |
56 | get_config_string(lookup_config(config_tree, "Device"), &device); |
69 | get_config_string(lookup_config(config_tree, "Interface"), &iface); |
57 | get_config_string(lookup_config(config_tree, "Interface"), &iface); |
70 | |
58 | |
71 | /* Open registry and look for network adapters */ |
59 | /* Open registry and look for network adapters */ |
72 | |
60 | |
73 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_CONTROL_NET, 0, KEY_READ, &key)) { |
61 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &key)) { |
74 | logger(LOG_ERR, _("Unable to read registry: %s"), winerror(GetLastError())); |
62 | logger(LOG_ERR, _("Unable to read registry: %s"), winerror(GetLastError())); |
75 | return false; |
63 | return false; |
76 | } |
64 | } |
77 | |
65 | |
78 | for (i = 0; ; i++) { |
66 | for (i = 0; ; i++) { |
… | |
… | |
80 | if(RegEnumKeyEx(key, i, adapterid, &len, 0, 0, 0, NULL)) |
68 | if(RegEnumKeyEx(key, i, adapterid, &len, 0, 0, 0, NULL)) |
81 | break; |
69 | break; |
82 | |
70 | |
83 | /* Find out more about this adapter */ |
71 | /* Find out more about this adapter */ |
84 | |
72 | |
85 | snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", REG_CONTROL_NET, adapterid); |
73 | snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, adapterid); |
86 | |
74 | |
87 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2)) |
75 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2)) |
88 | continue; |
76 | continue; |
89 | |
77 | |
90 | len = sizeof(adaptername); |
78 | len = sizeof(adaptername); |