Move ResetEvent() call before ReadFile().
[tinc] / src / mingw / device.c
index 468ba50..d90c69b 100644 (file)
@@ -43,7 +43,7 @@ static vpn_packet_t device_read_packet;
 static vpn_packet_t device_write_packet;
 char *device = NULL;
 char *iface = NULL;
-static char *device_info = NULL;
+static const char *device_info = "Windows tap device";
 
 extern char *myport;
 
@@ -54,6 +54,8 @@ static void device_issue_read() {
        int status;
 
        for(;;) {
+               ResetEvent(device_read_overlapped.hEvent);
+
                DWORD len;
                status = ReadFile(device_handle, (void *)device_read_packet.data, MTU, &len, &device_read_overlapped);
 
@@ -72,10 +74,7 @@ static void device_issue_read() {
 }
 
 static void device_handle_read(void *data, int flags) {
-       ResetEvent(device_read_overlapped.hEvent);
-
        DWORD len;
-
        if(!GetOverlappedResult(device_handle, &device_read_overlapped, &len, FALSE)) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Error getting read result from %s %s: %s", device_info,
                       device, strerror(errno));
@@ -200,7 +199,7 @@ static bool setup_device(void) {
                ULONG info[3] = {0};
                DWORD len;
 
-               if(!DeviceIoControl(device_handle, TAP_IOCTL_GET_VERSION, &info, sizeof(info), &info, sizeof info, &len, NULL)) {
+               if(!DeviceIoControl(device_handle, TAP_IOCTL_GET_VERSION, &info, sizeof(info), &info, sizeof(info), &len, NULL)) {
                        logger(DEBUG_ALWAYS, LOG_WARNING, "Could not get version information from Windows tap device %s (%s): %s", device, iface, winerror(GetLastError()));
                } else {
                        logger(DEBUG_ALWAYS, LOG_INFO, "TAP-Windows driver version: %lu.%lu%s", info[0], info[1], info[2] ? " (DEBUG)" : "");
@@ -216,7 +215,7 @@ static bool setup_device(void) {
 
        /* Get MAC address from tap device */
 
-       if(!DeviceIoControl(device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof(mymac.x), mymac.x, sizeof mymac.x, &len, 0)) {
+       if(!DeviceIoControl(device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof(mymac.x), mymac.x, sizeof(mymac.x), &len, 0)) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Could not get MAC address from Windows tap device %s (%s): %s", device, iface, winerror(GetLastError()));
                return false;
        }
@@ -240,7 +239,7 @@ static void enable_device(void) {
 
        ULONG status = 1;
        DWORD len;
-       DeviceIoControl(device_handle, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof status, &len, NULL);
+       DeviceIoControl(device_handle, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof(status), &len, NULL);
 
        /* We don't use the write event directly, but GetOverlappedResult() does, internally. */
 
@@ -255,7 +254,7 @@ static void disable_device(void) {
 
        ULONG status = 0;
        DWORD len;
-       DeviceIoControl(device_handle, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof status, &len, NULL);
+       DeviceIoControl(device_handle, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof(status), &len, NULL);
 
        /* Note that we don't try to cancel ongoing I/O here - we just stop listening.
           This is because some TAP-Win32 drivers don't seem to handle cancellation very well,