1 | /* |
1 | /* |
2 | device.c -- Interaction with Windows tap driver in a MinGW environment |
2 | device.c -- Interaction with Windows tap driver in a MinGW 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:38 pcg Exp $ |
21 | */ |
21 | */ |
22 | |
22 | |
|
|
23 | #include "system.h" |
23 | |
24 | |
24 | #include <windows.h> |
25 | #include <windows.h> |
25 | #include <winioctl.h> |
26 | #include <winioctl.h> |
26 | |
27 | |
27 | |
28 | #include "tincd/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 | |
29 | |
40 | int device_fd = 0; |
30 | int device_fd = 0; |
41 | HANDLE device_handle = INVALID_HANDLE_VALUE; |
31 | static HANDLE device_handle = INVALID_HANDLE_VALUE; |
42 | char *device = NULL; |
32 | char *device = NULL; |
43 | char *iface = NULL; |
33 | char *iface = NULL; |
44 | char *device_info = NULL; |
34 | char *device_info = NULL; |
45 | |
35 | |
46 | int device_total_in = 0; |
36 | static int device_total_in = 0; |
47 | int device_total_out = 0; |
37 | static int device_total_out = 0; |
48 | |
38 | |
49 | extern char *myport; |
39 | extern char *myport; |
50 | |
40 | |
51 | DWORD WINAPI tapreader(void *bla) { |
41 | DWORD WINAPI tapreader(void *bla) { |
52 | int sock, err, status; |
42 | int sock, err, status; |
… | |
… | |
122 | char regpath[1024]; |
112 | char regpath[1024]; |
123 | char adapterid[1024]; |
113 | char adapterid[1024]; |
124 | char adaptername[1024]; |
114 | char adaptername[1024]; |
125 | char tapname[1024]; |
115 | char tapname[1024]; |
126 | long len; |
116 | long len; |
|
|
117 | unsigned long status; |
127 | |
118 | |
128 | bool found = false; |
119 | bool found = false; |
129 | |
120 | |
130 | int sock, err; |
121 | int sock, err; |
131 | HANDLE thread; |
122 | HANDLE thread; |
… | |
… | |
143 | get_config_string(lookup_config(config_tree, "Device"), &device); |
134 | get_config_string(lookup_config(config_tree, "Device"), &device); |
144 | get_config_string(lookup_config(config_tree, "Interface"), &iface); |
135 | get_config_string(lookup_config(config_tree, "Interface"), &iface); |
145 | |
136 | |
146 | /* Open registry and look for network adapters */ |
137 | /* Open registry and look for network adapters */ |
147 | |
138 | |
148 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_CONTROL_NET, 0, KEY_READ, &key)) { |
139 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &key)) { |
149 | logger(LOG_ERR, _("Unable to read registry: %s"), winerror(GetLastError())); |
140 | logger(LOG_ERR, _("Unable to read registry: %s"), winerror(GetLastError())); |
150 | return false; |
141 | return false; |
151 | } |
142 | } |
152 | |
143 | |
153 | for (i = 0; ; i++) { |
144 | for (i = 0; ; i++) { |
… | |
… | |
155 | if(RegEnumKeyEx(key, i, adapterid, &len, 0, 0, 0, NULL)) |
146 | if(RegEnumKeyEx(key, i, adapterid, &len, 0, 0, 0, NULL)) |
156 | break; |
147 | break; |
157 | |
148 | |
158 | /* Find out more about this adapter */ |
149 | /* Find out more about this adapter */ |
159 | |
150 | |
160 | snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", REG_CONTROL_NET, adapterid); |
151 | snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, adapterid); |
161 | |
152 | |
162 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2)) |
153 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2)) |
163 | continue; |
154 | continue; |
164 | |
155 | |
165 | len = sizeof(adaptername); |
156 | len = sizeof(adaptername); |
… | |
… | |
274 | return false; |
265 | return false; |
275 | } |
266 | } |
276 | |
267 | |
277 | closesocket(sock); |
268 | closesocket(sock); |
278 | |
269 | |
|
|
270 | /* Set media status for newer TAP-Win32 devices */ |
|
|
271 | |
|
|
272 | status = true; |
|
|
273 | DeviceIoControl(device_handle, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof(status), &len, NULL); |
|
|
274 | |
279 | device_info = _("Windows tap device"); |
275 | device_info = _("Windows tap device"); |
280 | |
276 | |
281 | logger(LOG_INFO, _("%s (%s) is a %s"), device, iface, device_info); |
277 | logger(LOG_INFO, _("%s (%s) is a %s"), device, iface, device_info); |
282 | |
278 | |
283 | return true; |
279 | return true; |