/*
device.c -- Interaction with Windows tap driver in a MinGW environment
- Copyright (C) 2002-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
- 2002-2004 Guus Sliepen <guus@tinc-vpn.org>
+ Copyright (C) 2002-2005 Ivo Timmermans,
+ 2002-2007 Guus Sliepen <guus@tinc-vpn.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "mingw/common.h"
int device_fd = 0;
-HANDLE device_handle = INVALID_HANDLE_VALUE;
+static HANDLE device_handle = INVALID_HANDLE_VALUE;
char *device = NULL;
char *iface = NULL;
-char *device_info = NULL;
+static char *device_info = NULL;
-int device_total_in = 0;
-int device_total_out = 0;
+static int device_total_in = 0;
+static int device_total_out = 0;
extern char *myport;
-DWORD WINAPI tapreader(void *bla) {
+static struct packetbuf {
+ uint8_t data[MTU];
+ length_t len;
+} *bufs;
+
+static int nbufs = 64;
+
+static DWORD WINAPI tapreader(void *bla) {
int sock, err, status;
struct addrinfo *ai;
struct addrinfo hint = {
.ai_family = AF_UNSPEC,
- .ai_socktype = SOCK_DGRAM,
- .ai_protocol = IPPROTO_UDP,
+ .ai_socktype = SOCK_STREAM,
+ .ai_protocol = IPPROTO_TCP,
.ai_flags = 0,
};
- char buf[MTU];
+ unsigned char bufno = 0;
long len;
OVERLAPPED overlapped;
return -1;
}
- sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
-
- freeaddrinfo(ai);
+ sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
if(sock < 0) {
logger(LOG_ERR, _("System call `%s' failed: %s"), "socket", strerror(errno));
+ freeaddrinfo(ai);
return -1;
}
if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
logger(LOG_ERR, _("System call `%s' failed: %s"), "connect", strerror(errno));
+ freeaddrinfo(ai);
return -1;
}
+ freeaddrinfo(ai);
+
logger(LOG_DEBUG, _("Tap reader running"));
/* Read from tap device and send to parent */
overlapped.OffsetHigh = 0;
ResetEvent(overlapped.hEvent);
- status = ReadFile(device_handle, buf, sizeof(buf), &len, &overlapped);
+ status = ReadFile(device_handle, bufs[bufno].data, MTU, &len, &overlapped);
if(!status) {
if(GetLastError() == ERROR_IO_PENDING) {
}
}
- if(send(sock, buf, len, 0) <= 0)
+ bufs[bufno].len = len;
+ if(send(sock, &bufno, 1, 0) <= 0)
return -1;
+ if(++bufno >= nbufs)
+ bufno = 0;
}
}
struct addrinfo *ai;
struct addrinfo hint = {
.ai_family = AF_UNSPEC,
- .ai_socktype = SOCK_DGRAM,
- .ai_protocol = IPPROTO_UDP,
+ .ai_socktype = SOCK_STREAM,
+ .ai_protocol = IPPROTO_TCP,
.ai_flags = 0,
};
overwrite_mac = 1;
}
+ /* Set up ringbuffer */
+
+ get_config_int(lookup_config(config_tree, "RingBufferSize"), &nbufs);
+ if(nbufs <= 1)
+ nbufs = 1;
+ else if(nbufs > 256)
+ nbufs = 256;
+
+ bufs = xmalloc_and_zero(nbufs * sizeof *bufs);
+
/* Create a listening socket */
err = getaddrinfo(NULL, myport, &hint, &ai);
return false;
}
- sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
if(sock < 0) {
logger(LOG_ERR, _("System call `%s' failed: %s"), "socket", strerror(errno));
cp();
CloseHandle(device_handle);
+
+ free(device);
+ free(iface);
}
bool read_packet(vpn_packet_t *packet)
{
- int lenin;
+ unsigned char bufno;
cp();
- if((lenin = recv(device_fd, packet->data, MTU, 0)) <= 0) {
+ if((recv(device_fd, &bufno, 1, 0)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno));
return false;
}
- packet->len = lenin;
+ packet->len = bufs[bufno].len;
+ memcpy(packet->data, bufs[bufno].data, bufs[bufno].len);
device_total_in += packet->len;