2020-11-01 06:01:04 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
2021-01-30 16:45:26 +01:00
|
|
|
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
|
2020-11-01 06:01:04 +01:00
|
|
|
*/
|
|
|
|
|
2020-11-02 22:09:52 +01:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <ws2ipdef.h>
|
|
|
|
#include <iphlpapi.h>
|
2021-07-28 13:50:40 +02:00
|
|
|
#include <mstcpip.h>
|
2020-11-02 22:09:52 +01:00
|
|
|
#include <ip2string.h>
|
2021-10-12 07:21:31 +02:00
|
|
|
#include <winternl.h>
|
2020-11-01 06:01:04 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2021-07-28 13:50:40 +02:00
|
|
|
#include <stdlib.h>
|
2020-11-02 22:09:52 +01:00
|
|
|
#include "wintun.h"
|
2020-11-01 06:01:04 +01:00
|
|
|
|
2021-10-12 07:21:31 +02:00
|
|
|
static WINTUN_CREATE_ADAPTER_FUNC *WintunCreateAdapter;
|
|
|
|
static WINTUN_CLOSE_ADAPTER_FUNC *WintunCloseAdapter;
|
|
|
|
static WINTUN_OPEN_ADAPTER_FUNC *WintunOpenAdapter;
|
|
|
|
static WINTUN_GET_ADAPTER_LUID_FUNC *WintunGetAdapterLUID;
|
|
|
|
static WINTUN_GET_RUNNING_DRIVER_VERSION_FUNC *WintunGetRunningDriverVersion;
|
|
|
|
static WINTUN_DELETE_DRIVER_FUNC *WintunDeleteDriver;
|
|
|
|
static WINTUN_SET_LOGGER_FUNC *WintunSetLogger;
|
|
|
|
static WINTUN_START_SESSION_FUNC *WintunStartSession;
|
|
|
|
static WINTUN_END_SESSION_FUNC *WintunEndSession;
|
|
|
|
static WINTUN_GET_READ_WAIT_EVENT_FUNC *WintunGetReadWaitEvent;
|
|
|
|
static WINTUN_RECEIVE_PACKET_FUNC *WintunReceivePacket;
|
|
|
|
static WINTUN_RELEASE_RECEIVE_PACKET_FUNC *WintunReleaseReceivePacket;
|
|
|
|
static WINTUN_ALLOCATE_SEND_PACKET_FUNC *WintunAllocateSendPacket;
|
|
|
|
static WINTUN_SEND_PACKET_FUNC *WintunSendPacket;
|
2020-11-01 06:01:04 +01:00
|
|
|
|
2020-11-05 12:43:00 +01:00
|
|
|
static HMODULE
|
|
|
|
InitializeWintun(void)
|
|
|
|
{
|
|
|
|
HMODULE Wintun =
|
|
|
|
LoadLibraryExW(L"wintun.dll", NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
|
|
|
|
if (!Wintun)
|
|
|
|
return NULL;
|
2021-10-15 02:41:12 +02:00
|
|
|
#define X(Name) ((*(FARPROC *)&Name = GetProcAddress(Wintun, #Name)) == NULL)
|
|
|
|
if (X(WintunCreateAdapter) || X(WintunCloseAdapter) || X(WintunOpenAdapter) || X(WintunGetAdapterLUID) ||
|
|
|
|
X(WintunGetRunningDriverVersion) || X(WintunDeleteDriver) || X(WintunSetLogger) || X(WintunStartSession) ||
|
|
|
|
X(WintunEndSession) || X(WintunGetReadWaitEvent) || X(WintunReceivePacket) || X(WintunReleaseReceivePacket) ||
|
|
|
|
X(WintunAllocateSendPacket) || X(WintunSendPacket))
|
2020-11-05 12:43:00 +01:00
|
|
|
#undef X
|
|
|
|
{
|
|
|
|
DWORD LastError = GetLastError();
|
|
|
|
FreeLibrary(Wintun);
|
|
|
|
SetLastError(LastError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return Wintun;
|
|
|
|
}
|
2020-11-01 06:01:04 +01:00
|
|
|
|
2020-11-10 22:06:25 +01:00
|
|
|
static void CALLBACK
|
2021-10-12 07:21:31 +02:00
|
|
|
ConsoleLogger(_In_ WINTUN_LOGGER_LEVEL Level, _In_ DWORD64 Timestamp, _In_z_ const WCHAR *LogLine)
|
2020-11-01 06:01:04 +01:00
|
|
|
{
|
|
|
|
SYSTEMTIME SystemTime;
|
2021-10-12 07:21:31 +02:00
|
|
|
FileTimeToSystemTime((FILETIME *)&Timestamp, &SystemTime);
|
2020-11-01 06:01:04 +01:00
|
|
|
WCHAR LevelMarker;
|
|
|
|
switch (Level)
|
|
|
|
{
|
|
|
|
case WINTUN_LOG_INFO:
|
|
|
|
LevelMarker = L'+';
|
|
|
|
break;
|
|
|
|
case WINTUN_LOG_WARN:
|
|
|
|
LevelMarker = L'-';
|
|
|
|
break;
|
|
|
|
case WINTUN_LOG_ERR:
|
|
|
|
LevelMarker = L'!';
|
|
|
|
break;
|
|
|
|
default:
|
2020-11-10 22:06:25 +01:00
|
|
|
return;
|
2020-11-01 06:01:04 +01:00
|
|
|
}
|
|
|
|
fwprintf(
|
|
|
|
stderr,
|
2021-07-28 20:19:34 +02:00
|
|
|
L"%04u-%02u-%02u %02u:%02u:%02u.%04u [%c] %s\n",
|
2020-11-01 06:01:04 +01:00
|
|
|
SystemTime.wYear,
|
|
|
|
SystemTime.wMonth,
|
|
|
|
SystemTime.wDay,
|
|
|
|
SystemTime.wHour,
|
|
|
|
SystemTime.wMinute,
|
|
|
|
SystemTime.wSecond,
|
|
|
|
SystemTime.wMilliseconds,
|
|
|
|
LevelMarker,
|
|
|
|
LogLine);
|
|
|
|
}
|
|
|
|
|
2021-10-12 07:21:31 +02:00
|
|
|
static DWORD64 Now(VOID)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER Timestamp;
|
|
|
|
NtQuerySystemTime(&Timestamp);
|
|
|
|
return Timestamp.QuadPart;
|
|
|
|
}
|
|
|
|
|
2020-11-01 06:01:04 +01:00
|
|
|
static DWORD
|
2020-11-02 22:09:52 +01:00
|
|
|
LogError(_In_z_ const WCHAR *Prefix, _In_ DWORD Error)
|
2020-11-01 06:01:04 +01:00
|
|
|
{
|
|
|
|
WCHAR *SystemMessage = NULL, *FormattedMessage = NULL;
|
|
|
|
FormatMessageW(
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
|
|
|
NULL,
|
|
|
|
HRESULT_FROM_SETUPAPI(Error),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(void *)&SystemMessage,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
FormatMessageW(
|
|
|
|
FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY |
|
|
|
|
FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
|
|
|
SystemMessage ? L"%1: %3(Code 0x%2!08X!)" : L"%1: Code 0x%2!08X!",
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(void *)&FormattedMessage,
|
|
|
|
0,
|
|
|
|
(va_list *)(DWORD_PTR[]){ (DWORD_PTR)Prefix, (DWORD_PTR)Error, (DWORD_PTR)SystemMessage });
|
|
|
|
if (FormattedMessage)
|
2021-10-12 07:21:31 +02:00
|
|
|
ConsoleLogger(WINTUN_LOG_ERR, Now(), FormattedMessage);
|
2020-11-01 06:01:04 +01:00
|
|
|
LocalFree(FormattedMessage);
|
|
|
|
LocalFree(SystemMessage);
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
static DWORD
|
|
|
|
LogLastError(_In_z_ const WCHAR *Prefix)
|
|
|
|
{
|
|
|
|
DWORD LastError = GetLastError();
|
|
|
|
LogError(Prefix, LastError);
|
|
|
|
SetLastError(LastError);
|
|
|
|
return LastError;
|
|
|
|
}
|
|
|
|
|
2020-11-01 06:01:04 +01:00
|
|
|
static void
|
|
|
|
Log(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *Format, ...)
|
|
|
|
{
|
|
|
|
WCHAR LogLine[0x200];
|
|
|
|
va_list args;
|
|
|
|
va_start(args, Format);
|
|
|
|
_vsnwprintf_s(LogLine, _countof(LogLine), _TRUNCATE, Format, args);
|
|
|
|
va_end(args);
|
2021-10-12 07:21:31 +02:00
|
|
|
ConsoleLogger(Level, Now(), LogLine);
|
2020-11-01 06:01:04 +01:00
|
|
|
}
|
|
|
|
|
2020-11-05 12:43:00 +01:00
|
|
|
static HANDLE QuitEvent;
|
|
|
|
static volatile BOOL HaveQuit;
|
|
|
|
|
2020-11-01 06:01:04 +01:00
|
|
|
static BOOL WINAPI
|
2020-11-02 22:09:52 +01:00
|
|
|
CtrlHandler(_In_ DWORD CtrlType)
|
2020-11-01 06:01:04 +01:00
|
|
|
{
|
|
|
|
switch (CtrlType)
|
|
|
|
{
|
|
|
|
case CTRL_C_EVENT:
|
|
|
|
case CTRL_BREAK_EVENT:
|
|
|
|
case CTRL_CLOSE_EVENT:
|
|
|
|
case CTRL_LOGOFF_EVENT:
|
|
|
|
case CTRL_SHUTDOWN_EVENT:
|
2020-11-02 22:09:52 +01:00
|
|
|
Log(WINTUN_LOG_INFO, L"Cleaning up and shutting down...");
|
2020-11-01 06:01:04 +01:00
|
|
|
HaveQuit = TRUE;
|
|
|
|
SetEvent(QuitEvent);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-11-02 22:09:52 +01:00
|
|
|
static void
|
|
|
|
PrintPacket(_In_ const BYTE *Packet, _In_ DWORD PacketSize)
|
|
|
|
{
|
|
|
|
if (PacketSize < 20)
|
|
|
|
{
|
|
|
|
Log(WINTUN_LOG_INFO, L"Received packet without room for an IP header");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BYTE IpVersion = Packet[0] >> 4, Proto;
|
|
|
|
WCHAR Src[46], Dst[46];
|
|
|
|
if (IpVersion == 4)
|
|
|
|
{
|
|
|
|
RtlIpv4AddressToStringW((struct in_addr *)&Packet[12], Src);
|
|
|
|
RtlIpv4AddressToStringW((struct in_addr *)&Packet[16], Dst);
|
|
|
|
Proto = Packet[9];
|
|
|
|
Packet += 20, PacketSize -= 20;
|
|
|
|
}
|
|
|
|
else if (IpVersion == 6 && PacketSize < 40)
|
|
|
|
{
|
|
|
|
Log(WINTUN_LOG_INFO, L"Received packet without room for an IP header");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (IpVersion == 6)
|
|
|
|
{
|
|
|
|
RtlIpv6AddressToStringW((struct in6_addr *)&Packet[8], Src);
|
|
|
|
RtlIpv6AddressToStringW((struct in6_addr *)&Packet[24], Dst);
|
|
|
|
Proto = Packet[6];
|
|
|
|
Packet += 40, PacketSize -= 40;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(WINTUN_LOG_INFO, L"Received packet that was not IP");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Proto == 1 && PacketSize >= 8 && Packet[0] == 0)
|
|
|
|
Log(WINTUN_LOG_INFO, L"Received IPv%d ICMP echo reply from %s to %s", IpVersion, Src, Dst);
|
|
|
|
else
|
|
|
|
Log(WINTUN_LOG_INFO, L"Received IPv%d proto 0x%x packet from %s to %s", IpVersion, Proto, Src, Dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
static USHORT
|
2020-11-03 11:08:35 +01:00
|
|
|
IPChecksum(_In_reads_bytes_(Len) BYTE *Buffer, _In_ DWORD Len)
|
2020-11-02 22:09:52 +01:00
|
|
|
{
|
|
|
|
ULONG Sum = 0;
|
|
|
|
for (; Len > 1; Len -= 2, Buffer += 2)
|
|
|
|
Sum += *(USHORT *)Buffer;
|
|
|
|
if (Len)
|
|
|
|
Sum += *Buffer;
|
|
|
|
Sum = (Sum >> 16) + (Sum & 0xffff);
|
|
|
|
Sum += (Sum >> 16);
|
|
|
|
return (USHORT)(~Sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-11-03 11:08:35 +01:00
|
|
|
MakeICMP(_Out_writes_bytes_all_(28) BYTE Packet[28])
|
2020-11-02 22:09:52 +01:00
|
|
|
{
|
|
|
|
memset(Packet, 0, 28);
|
|
|
|
Packet[0] = 0x45;
|
|
|
|
*(USHORT *)&Packet[2] = htons(28);
|
|
|
|
Packet[8] = 255;
|
|
|
|
Packet[9] = 1;
|
|
|
|
*(ULONG *)&Packet[12] = htonl((10 << 24) | (6 << 16) | (7 << 8) | (8 << 0)); /* 10.6.7.8 */
|
|
|
|
*(ULONG *)&Packet[16] = htonl((10 << 24) | (6 << 16) | (7 << 8) | (7 << 0)); /* 10.6.7.7 */
|
|
|
|
*(USHORT *)&Packet[10] = IPChecksum(Packet, 20);
|
|
|
|
Packet[20] = 8;
|
|
|
|
*(USHORT *)&Packet[22] = IPChecksum(&Packet[20], 8);
|
|
|
|
Log(WINTUN_LOG_INFO, L"Sending IPv4 ICMP echo request to 10.6.7.8 from 10.6.7.7");
|
|
|
|
}
|
|
|
|
|
2020-11-01 06:01:04 +01:00
|
|
|
static DWORD WINAPI
|
2020-11-02 22:09:52 +01:00
|
|
|
ReceivePackets(_Inout_ DWORD_PTR SessionPtr)
|
2020-11-01 06:01:04 +01:00
|
|
|
{
|
2020-11-02 22:09:52 +01:00
|
|
|
WINTUN_SESSION_HANDLE Session = (WINTUN_SESSION_HANDLE)SessionPtr;
|
|
|
|
HANDLE WaitHandles[] = { WintunGetReadWaitEvent(Session), QuitEvent };
|
|
|
|
|
2020-11-01 06:01:04 +01:00
|
|
|
while (!HaveQuit)
|
|
|
|
{
|
2020-11-02 22:09:52 +01:00
|
|
|
DWORD PacketSize;
|
2020-11-03 12:29:34 +01:00
|
|
|
BYTE *Packet = WintunReceivePacket(Session, &PacketSize);
|
|
|
|
if (Packet)
|
2020-11-01 06:01:04 +01:00
|
|
|
{
|
2020-11-02 22:09:52 +01:00
|
|
|
PrintPacket(Packet, PacketSize);
|
2020-11-05 13:01:41 +01:00
|
|
|
WintunReleaseReceivePacket(Session, Packet);
|
2020-11-03 12:29:34 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD LastError = GetLastError();
|
|
|
|
switch (LastError)
|
|
|
|
{
|
|
|
|
case ERROR_NO_MORE_ITEMS:
|
|
|
|
if (WaitForMultipleObjects(_countof(WaitHandles), WaitHandles, FALSE, INFINITE) == WAIT_OBJECT_0)
|
|
|
|
continue;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
default:
|
|
|
|
LogError(L"Packet read failed", LastError);
|
|
|
|
return LastError;
|
|
|
|
}
|
2020-11-01 06:01:04 +01:00
|
|
|
}
|
2020-11-02 22:09:52 +01:00
|
|
|
}
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2020-11-01 06:01:04 +01:00
|
|
|
|
2020-11-02 22:09:52 +01:00
|
|
|
static DWORD WINAPI
|
|
|
|
SendPackets(_Inout_ DWORD_PTR SessionPtr)
|
|
|
|
{
|
|
|
|
WINTUN_SESSION_HANDLE Session = (WINTUN_SESSION_HANDLE)SessionPtr;
|
|
|
|
while (!HaveQuit)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
BYTE *Packet = WintunAllocateSendPacket(Session, 28);
|
2020-11-04 14:46:06 +01:00
|
|
|
if (Packet)
|
|
|
|
{
|
|
|
|
MakeICMP(Packet);
|
|
|
|
WintunSendPacket(Session, Packet);
|
|
|
|
}
|
|
|
|
else if (GetLastError() != ERROR_BUFFER_OVERFLOW)
|
2020-11-03 12:29:34 +01:00
|
|
|
return LogLastError(L"Packet write failed");
|
2020-11-01 06:01:04 +01:00
|
|
|
|
2020-11-02 22:09:52 +01:00
|
|
|
switch (WaitForSingleObject(QuitEvent, 1000 /* 1 second */))
|
2020-11-01 06:01:04 +01:00
|
|
|
{
|
2020-11-02 22:09:52 +01:00
|
|
|
case WAIT_ABANDONED:
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
return ERROR_SUCCESS;
|
2020-11-01 06:01:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-10-12 07:21:31 +02:00
|
|
|
int __cdecl main(void)
|
2020-11-02 22:09:52 +01:00
|
|
|
{
|
|
|
|
HMODULE Wintun = InitializeWintun();
|
|
|
|
if (!Wintun)
|
|
|
|
return LogError(L"Failed to initialize Wintun", GetLastError());
|
|
|
|
WintunSetLogger(ConsoleLogger);
|
|
|
|
Log(WINTUN_LOG_INFO, L"Wintun library loaded");
|
2020-11-01 06:01:04 +01:00
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError;
|
2020-11-01 06:01:04 +01:00
|
|
|
HaveQuit = FALSE;
|
|
|
|
QuitEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!QuitEvent)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = LogError(L"Failed to create event", GetLastError());
|
2020-11-01 06:01:04 +01:00
|
|
|
goto cleanupWintun;
|
|
|
|
}
|
|
|
|
if (!SetConsoleCtrlHandler(CtrlHandler, TRUE))
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = LogError(L"Failed to set console handler", GetLastError());
|
2020-11-01 06:01:04 +01:00
|
|
|
goto cleanupQuit;
|
|
|
|
}
|
|
|
|
|
2020-11-02 22:09:52 +01:00
|
|
|
GUID ExampleGuid = { 0xdeadbabe, 0xcafe, 0xbeef, { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef } };
|
2021-10-12 07:21:31 +02:00
|
|
|
WINTUN_ADAPTER_HANDLE Adapter = WintunCreateAdapter(L"Demo", L"Example", &ExampleGuid);
|
2020-11-03 12:29:34 +01:00
|
|
|
if (!Adapter)
|
2020-11-02 22:09:52 +01:00
|
|
|
{
|
2021-10-12 07:21:31 +02:00
|
|
|
LastError = GetLastError();
|
|
|
|
LogError(L"Failed to create adapter", LastError);
|
|
|
|
goto cleanupQuit;
|
2020-11-02 22:09:52 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 14:53:39 +01:00
|
|
|
DWORD Version = WintunGetRunningDriverVersion();
|
2020-11-03 12:27:42 +01:00
|
|
|
Log(WINTUN_LOG_INFO, L"Wintun v%u.%u loaded", (Version >> 16) & 0xff, (Version >> 0) & 0xff);
|
2020-11-02 22:09:52 +01:00
|
|
|
|
|
|
|
MIB_UNICASTIPADDRESS_ROW AddressRow;
|
|
|
|
InitializeUnicastIpAddressEntry(&AddressRow);
|
|
|
|
WintunGetAdapterLUID(Adapter, &AddressRow.InterfaceLuid);
|
|
|
|
AddressRow.Address.Ipv4.sin_family = AF_INET;
|
|
|
|
AddressRow.Address.Ipv4.sin_addr.S_un.S_addr = htonl((10 << 24) | (6 << 16) | (7 << 8) | (7 << 0)); /* 10.6.7.7 */
|
2020-11-03 12:27:42 +01:00
|
|
|
AddressRow.OnLinkPrefixLength = 24; /* This is a /24 network */
|
2021-09-09 22:42:59 +02:00
|
|
|
AddressRow.DadState = IpDadStatePreferred;
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = CreateUnicastIpAddressEntry(&AddressRow);
|
|
|
|
if (LastError != ERROR_SUCCESS && LastError != ERROR_OBJECT_ALREADY_EXISTS)
|
2020-11-02 22:09:52 +01:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LogError(L"Failed to set IP address", LastError);
|
2020-11-02 22:09:52 +01:00
|
|
|
goto cleanupAdapter;
|
|
|
|
}
|
|
|
|
|
2020-11-05 14:22:53 +01:00
|
|
|
WINTUN_SESSION_HANDLE Session = WintunStartSession(Adapter, 0x400000);
|
2020-11-03 12:29:34 +01:00
|
|
|
if (!Session)
|
2020-11-02 22:09:52 +01:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = LogLastError(L"Failed to create adapter");
|
2020-11-02 22:09:52 +01:00
|
|
|
goto cleanupAdapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(WINTUN_LOG_INFO, L"Launching threads and mangling packets...");
|
|
|
|
|
|
|
|
HANDLE Workers[] = { CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReceivePackets, (LPVOID)Session, 0, NULL),
|
|
|
|
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendPackets, (LPVOID)Session, 0, NULL) };
|
|
|
|
if (!Workers[0] || !Workers[1])
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = LogError(L"Failed to create threads", GetLastError());
|
2020-11-02 22:09:52 +01:00
|
|
|
goto cleanupWorkers;
|
|
|
|
}
|
2020-11-01 06:01:04 +01:00
|
|
|
WaitForMultipleObjectsEx(_countof(Workers), Workers, TRUE, INFINITE, TRUE);
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_SUCCESS;
|
2020-11-02 22:09:52 +01:00
|
|
|
|
2020-11-01 06:01:04 +01:00
|
|
|
cleanupWorkers:
|
|
|
|
HaveQuit = TRUE;
|
|
|
|
SetEvent(QuitEvent);
|
|
|
|
for (size_t i = 0; i < _countof(Workers); ++i)
|
2020-11-02 22:09:52 +01:00
|
|
|
{
|
2020-11-01 06:01:04 +01:00
|
|
|
if (Workers[i])
|
|
|
|
{
|
|
|
|
WaitForSingleObject(Workers[i], INFINITE);
|
|
|
|
CloseHandle(Workers[i]);
|
|
|
|
}
|
2020-11-02 22:09:52 +01:00
|
|
|
}
|
|
|
|
WintunEndSession(Session);
|
|
|
|
cleanupAdapter:
|
2021-10-12 07:21:31 +02:00
|
|
|
WintunCloseAdapter(Adapter);
|
2020-11-01 06:01:04 +01:00
|
|
|
cleanupQuit:
|
2020-11-02 22:09:52 +01:00
|
|
|
SetConsoleCtrlHandler(CtrlHandler, FALSE);
|
2020-11-01 06:01:04 +01:00
|
|
|
CloseHandle(QuitEvent);
|
|
|
|
cleanupWintun:
|
|
|
|
FreeLibrary(Wintun);
|
2020-11-03 12:29:34 +01:00
|
|
|
return LastError;
|
2020-11-01 06:01:04 +01:00
|
|
|
}
|