2 device.c -- Interaction with Windows tap driver in a MinGW environment
3 Copyright (C) 2002-2005 Ivo Timmermans <ivo@tinc-vpn.org>,
4 2002-2005 Guus Sliepen <guus@tinc-vpn.org>
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
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
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
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include "mingw/common.h"
38 static HANDLE device_handle = INVALID_HANDLE_VALUE;
41 char *device_info = NULL;
43 static int device_total_in = 0;
44 static int device_total_out = 0;
48 DWORD WINAPI tapreader(void *bla) {
49 int sock, err, status;
51 struct addrinfo hint = {
52 .ai_family = AF_UNSPEC,
53 .ai_socktype = SOCK_DGRAM,
54 .ai_protocol = IPPROTO_UDP,
59 OVERLAPPED overlapped;
61 /* Open a socket to the parent process */
63 err = getaddrinfo(NULL, myport, &hint, &ai);
66 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(errno));
70 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
75 logger(LOG_ERR, _("System call `%s' failed: %s"), "socket", strerror(errno));
79 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
80 logger(LOG_ERR, _("System call `%s' failed: %s"), "connect", strerror(errno));
84 logger(LOG_DEBUG, _("Tap reader running"));
86 /* Read from tap device and send to parent */
88 overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
91 overlapped.Offset = 0;
92 overlapped.OffsetHigh = 0;
93 ResetEvent(overlapped.hEvent);
95 status = ReadFile(device_handle, buf, sizeof(buf), &len, &overlapped);
98 if(GetLastError() == ERROR_IO_PENDING) {
99 WaitForSingleObject(overlapped.hEvent, INFINITE);
100 if(!GetOverlappedResult(device_handle, &overlapped, &len, FALSE))
103 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
104 device, strerror(errno));
109 if(send(sock, buf, len, 0) <= 0)
114 bool setup_device(void)
120 char adapterid[1024];
121 char adaptername[1024];
124 unsigned long status;
132 struct addrinfo hint = {
133 .ai_family = AF_UNSPEC,
134 .ai_socktype = SOCK_DGRAM,
135 .ai_protocol = IPPROTO_UDP,
141 get_config_string(lookup_config(config_tree, "Device"), &device);
142 get_config_string(lookup_config(config_tree, "Interface"), &iface);
144 /* Open registry and look for network adapters */
146 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &key)) {
147 logger(LOG_ERR, _("Unable to read registry: %s"), winerror(GetLastError()));
152 len = sizeof(adapterid);
153 if(RegEnumKeyEx(key, i, adapterid, &len, 0, 0, 0, NULL))
156 /* Find out more about this adapter */
158 snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, adapterid);
160 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &key2))
163 len = sizeof(adaptername);
164 err = RegQueryValueEx(key2, "Name", 0, 0, adaptername, &len);
172 if(!strcmp(device, adapterid)) {
180 if(!strcmp(iface, adaptername)) {
187 snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
188 device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
189 if(device_handle != INVALID_HANDLE_VALUE) {
198 logger(LOG_ERR, _("No Windows tap device found!"));
203 device = xstrdup(adapterid);
206 iface = xstrdup(adaptername);
208 /* Try to open the corresponding tap device */
210 if(device_handle == INVALID_HANDLE_VALUE) {
211 snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, device);
212 device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
215 if(device_handle == INVALID_HANDLE_VALUE) {
216 logger(LOG_ERR, _("%s (%s) is not a usable Windows tap device: %s"), device, iface, winerror(GetLastError()));
220 /* Get MAC address from tap device */
222 if(!DeviceIoControl(device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof(mymac.x), mymac.x, sizeof(mymac.x), &len, 0)) {
223 logger(LOG_ERR, _("Could not get MAC address from Windows tap device %s (%s): %s"), device, iface, winerror(GetLastError()));
227 if(routing_mode == RMODE_ROUTER) {
231 /* Create a listening socket */
233 err = getaddrinfo(NULL, myport, &hint, &ai);
236 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(errno));
240 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
243 logger(LOG_ERR, _("System call `%s' failed: %s"), "socket", strerror(errno));
247 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
248 logger(LOG_ERR, _("System call `%s' failed: %s"), "bind", strerror(errno));
254 if(listen(sock, 1)) {
255 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
259 /* Start the tap reader */
261 thread = CreateThread(NULL, 0, tapreader, NULL, 0, NULL);
264 logger(LOG_ERR, _("System call `%s' failed: %s"), "CreateThread", winerror(GetLastError()));
268 /* Wait for the tap reader to connect back to us */
270 if((device_fd = accept(sock, NULL, 0)) == -1) {
271 logger(LOG_ERR, _("System call `%s' failed: %s"), "accept", strerror(errno));
277 /* Set media status for newer TAP-Win32 devices */
280 DeviceIoControl(device_handle, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof(status), &len, NULL);
282 device_info = _("Windows tap device");
284 logger(LOG_INFO, _("%s (%s) is a %s"), device, iface, device_info);
289 void close_device(void)
293 CloseHandle(device_handle);
296 bool read_packet(vpn_packet_t *packet)
302 if((lenin = recv(device_fd, packet->data, MTU, 0)) <= 0) {
303 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
304 device, strerror(errno));
310 device_total_in += packet->len;
312 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
318 bool write_packet(vpn_packet_t *packet)
321 OVERLAPPED overlapped = {0};
325 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
326 packet->len, device_info);
328 if(!WriteFile(device_handle, packet->data, packet->len, &lenout, &overlapped)) {
329 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info, device, winerror(GetLastError()));
333 device_total_out += packet->len;
338 void dump_device_stats(void)
342 logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
343 logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
344 logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);