winws: fix error messages and error code in windivert init

This commit is contained in:
bol-van 2024-05-01 19:02:28 +03:00
parent da1226ceee
commit a145fe73fe
5 changed files with 12 additions and 7 deletions

Binary file not shown.

View File

@ -959,6 +959,7 @@ void tcp_rewrite_winsize(struct tcphdr *tcp, uint16_t winsize, uint8_t scale_fac
static HANDLE w_filter = NULL; static HANDLE w_filter = NULL;
static OVERLAPPED ovl = { .hEvent = NULL }; static OVERLAPPED ovl = { .hEvent = NULL };
DWORD w_win32_error=0;
static HANDLE windivert_init_filter(const char *filter, UINT64 flags) static HANDLE windivert_init_filter(const char *filter, UINT64 flags)
{ {
@ -971,24 +972,25 @@ static HANDLE windivert_init_filter(const char *filter, UINT64 flags)
hMutex = CreateMutexA(NULL,TRUE,mutex_name); hMutex = CreateMutexA(NULL,TRUE,mutex_name);
if (hMutex && GetLastError()==ERROR_ALREADY_EXISTS) if (hMutex && GetLastError()==ERROR_ALREADY_EXISTS)
WaitForSingleObject(hMutex,INFINITE); WaitForSingleObject(hMutex,INFINITE);
h = WinDivertOpen(filter, WINDIVERT_LAYER_NETWORK, 0, flags); h = WinDivertOpen(filter, WINDIVERT_LAYER_NETWORK, 0, flags);
w_win32_error = GetLastError();
if (hMutex) if (hMutex)
{ {
ReleaseMutex(hMutex); ReleaseMutex(hMutex);
CloseHandle(hMutex); CloseHandle(hMutex);
SetLastError(w_win32_error);
} }
if (h != INVALID_HANDLE_VALUE) return h; if (h != INVALID_HANDLE_VALUE) return h;
errorcode = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorcode, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), (LPTSTR)&errormessage, 0, NULL); NULL, w_win32_error, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), (LPTSTR)&errormessage, 0, NULL);
fprintf(stderr, "windivert: error opening filter: %s", errormessage); fprintf(stderr, "windivert: error opening filter: %s", errormessage);
LocalFree(errormessage); LocalFree(errormessage);
if (errorcode == 577) if (w_win32_error == ERROR_INVALID_IMAGE_HASH)
fprintf(stderr,"windivert: try to disable secure boot and install OS patches\n"); fprintf(stderr,"windivert: try to disable secure boot and install OS patches\n");
return NULL; return NULL;
} }
void rawsend_cleanup(void) void rawsend_cleanup(void)
@ -1014,6 +1016,7 @@ bool windivert_init(const char *filter)
ovl.hEvent = CreateEventW(NULL,FALSE,FALSE,NULL); ovl.hEvent = CreateEventW(NULL,FALSE,FALSE,NULL);
if (!ovl.hEvent) if (!ovl.hEvent)
{ {
w_win32_error = GetLastError();
rawsend_cleanup(); rawsend_cleanup();
return false; return false;
} }
@ -1041,8 +1044,8 @@ static bool windivert_recv_filter(HANDLE hFilter, uint8_t *packet, size_t *len,
} }
for(;;) for(;;)
{ {
err = GetLastError(); w_win32_error = GetLastError();
switch(err) switch(w_win32_error)
{ {
case ERROR_IO_PENDING: case ERROR_IO_PENDING:
// make signals working // make signals working

View File

@ -149,6 +149,8 @@ uint8_t tcp_find_scale_factor(const struct tcphdr *tcp);
bool tcp_has_fastopen(const struct tcphdr *tcp); bool tcp_has_fastopen(const struct tcphdr *tcp);
#ifdef __CYGWIN__ #ifdef __CYGWIN__
extern DWORD w_win32_error;
bool windivert_init(const char *filter); bool windivert_init(const char *filter);
bool windivert_recv(uint8_t *packet, size_t *len, WINDIVERT_ADDRESS *wa); bool windivert_recv(uint8_t *packet, size_t *len, WINDIVERT_ADDRESS *wa);
bool windivert_send(const uint8_t *packet, size_t len, const WINDIVERT_ADDRESS *wa); bool windivert_send(const uint8_t *packet, size_t len, const WINDIVERT_ADDRESS *wa);

View File

@ -411,7 +411,7 @@ static int win_main(const char *windivert_filter)
char ifout[22]; char ifout[22];
if (!windivert_init(windivert_filter)) if (!windivert_init(windivert_filter))
return 1; return w_win32_error;
printf("windivert initialized. capture is started.\n"); printf("windivert initialized. capture is started.\n");