From efbc70635b18eeb2d6356d6e9ddac29329b40892 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Sat, 31 Oct 2020 10:28:25 +0100 Subject: [PATCH] api expose Send.TailMoved event to clients This allows clients to use it in WaitForMultipleObjects(). Signed-off-by: Simon Rozman --- api/exports.def | 1 - api/session.c | 13 ++++++------- api/wintun.h | 34 +++++++++++++++------------------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/api/exports.def b/api/exports.def index 2c9e78b..58add40 100644 --- a/api/exports.def +++ b/api/exports.def @@ -17,4 +17,3 @@ EXPORTS WintunSetAdapterName WintunSetLogger WintunStartSession - WintunWaitForPacket diff --git a/api/session.c b/api/session.c index e39f21b..eb71147 100644 --- a/api/session.c +++ b/api/session.c @@ -65,7 +65,11 @@ typedef struct _TUN_SESSION } TUN_SESSION; WINTUN_STATUS WINAPI -WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity, _Out_ TUN_SESSION **Session) +WintunStartSession( + _In_ const WINTUN_ADAPTER *Adapter, + _In_ DWORD Capacity, + _Out_ TUN_SESSION **Session, + _Out_ HANDLE *ReadWait) { TUN_SESSION *s = HeapAlloc(ModuleHeap, HEAP_ZERO_MEMORY, sizeof(TUN_SESSION)); if (!s) @@ -127,6 +131,7 @@ WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity, _Out (void)InitializeCriticalSectionAndSpinCount(&s->Receive.Lock, LOCK_SPIN_COUNT); (void)InitializeCriticalSectionAndSpinCount(&s->Send.Lock, LOCK_SPIN_COUNT); *Session = s; + *ReadWait = s->Descriptor.Send.TailMoved; return ERROR_SUCCESS; cleanupHandle: CloseHandle(s->Handle); @@ -156,12 +161,6 @@ WintunEndSession(_In_ TUN_SESSION *Session) HeapFree(ModuleHeap, 0, Session); } -WINTUN_STATUS WINAPI -WintunWaitForPacket(_In_ TUN_SESSION *Session, _In_ DWORD Milliseconds) -{ - return WaitForSingleObject(Session->Descriptor.Send.TailMoved, Milliseconds); -} - WINTUN_STATUS WINAPI WintunReceivePacket(_In_ TUN_SESSION *Session, _Out_bytecapcount_(*PacketSize) BYTE **Packet, _Out_ DWORD *PacketSize) { diff --git a/api/wintun.h b/api/wintun.h index 67a87be..b6e1663 100644 --- a/api/wintun.h +++ b/api/wintun.h @@ -250,12 +250,18 @@ typedef void *WINTUN_SESSION_HANDLE; * * @param Session Pointer to a variable to receive Wintun session handle * + * @param ReadWait Pointer to receive event handle to wait for available data when reading. Should + * WintunReceivePackets return ERROR_NO_MORE_ITEMS (after spinning on it for a while under heavy + * load), wait for this event to become signaled before retrying WintunReceivePackets. Do not call + * CloseHandle on this event - it is managed by the session. + * * @return ERROR_SUCCESS on success; Win32 error code otherwise. */ typedef WINTUN_STATUS(WINAPI *WINTUN_START_SESSION_FUNC)( _In_ WINTUN_ADAPTER_HANDLE Adapter, _In_ DWORD Capacity, - _Out_ WINTUN_SESSION_HANDLE *Session); + _Out_ WINTUN_SESSION_HANDLE *Session, + _Out_ HANDLE *ReadWait); /** * Ends Wintun session. @@ -269,20 +275,6 @@ typedef void(WINAPI *WINTUN_END_SESSION_FUNC)(_In_ WINTUN_SESSION_HANDLE Session */ #define WINTUN_MAX_IP_PACKET_SIZE 0xFFFF -/** - * Waits for a packet to become available for reading. - * - * @param Session Wintun session handle obtained with WintunStartSession - * - * @param Milliseconds The time-out interval, in milliseconds. If a nonzero value is specified, the function waits - * until a packet is available or the interval elapses. If Milliseconds is zero, the function - * does not enter a wait state if a packet is not available; it always returns immediately. - * If Milliseconds is INFINITE, the function will return only when a packet is available. - * - * @return See WaitForSingleObject() for return values. - */ -typedef WINTUN_STATUS(WINAPI *WINTUN_WAIT_FOR_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _In_ DWORD Milliseconds); - /** * Retrieves one or packet. After the packet content is consumed, call WintunReceiveRelease with Packet returned * from this function to release internal buffer. This function is thread-safe. @@ -301,8 +293,10 @@ typedef WINTUN_STATUS(WINAPI *WINTUN_WAIT_FOR_PACKET_FUNC)(_In_ WINTUN_SESSION_H * ERROR_SUCCESS on success. * Regardless, if the error was returned, some packets might have been read nevertheless. */ -typedef WINTUN_STATUS(WINAPI *WINTUN_RECEIVE_PACKETS_FUNC) -(_In_ WINTUN_SESSION_HANDLE *Session, _Out_bytecapcount_(*PacketSize) BYTE **Packet, _Out_ DWORD *PacketSize); +typedef WINTUN_STATUS(WINAPI *WINTUN_RECEIVE_PACKETS_FUNC)( + _In_ WINTUN_SESSION_HANDLE *Session, + _Out_bytecapcount_(*PacketSize) BYTE **Packet, + _Out_ DWORD *PacketSize); /** * Releases internal buffer after the received packet has been processed by the client. This function is thread-safe. @@ -329,8 +323,10 @@ typedef void(WINAPI *WINTUN_RECEIVE_RELEASE_FUNC)(_In_ WINTUN_SESSION_HANDLE *Se * ERROR_BUFFER_OVERFLOW Wintun buffer is full; * ERROR_SUCCESS on success. */ -typedef WINTUN_STATUS(WINAPI *WINTUN_ALLOCATE_SEND_PACKET_FUNC) -(_In_ WINTUN_SESSION_HANDLE *Session, _In_ DWORD PacketSize, _Out_bytecapcount_(PacketSize) BYTE **Packet); +typedef WINTUN_STATUS(WINAPI *WINTUN_ALLOCATE_SEND_PACKET_FUNC)( + _In_ WINTUN_SESSION_HANDLE *Session, + _In_ DWORD PacketSize, + _Out_bytecapcount_(PacketSize) BYTE **Packet); /** * Sends the packet and releases internal buffer. WintunSendPacket is thread-safe, but the WintunAllocateSendPacket