Compare commits

...

3 Commits

Author SHA1 Message Date
bol-van
8a09df7097 winws: save win32 error in windivert send 2024-05-01 21:23:16 +03:00
bol-van
1e8e46c14f winws: return win32 error code on windivert recv failure 2024-05-01 20:05:31 +03:00
bol-van
a145fe73fe winws: fix error messages and error code in windivert init 2024-05-01 19:02:28 +03:00
5 changed files with 16 additions and 9 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 };
uint32_t 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
@ -1079,7 +1082,9 @@ bool windivert_recv(uint8_t *packet, size_t *len, WINDIVERT_ADDRESS *wa)
static bool windivert_send_filter(HANDLE hFilter, const uint8_t *packet, size_t len, const WINDIVERT_ADDRESS *wa) static bool windivert_send_filter(HANDLE hFilter, const uint8_t *packet, size_t len, const WINDIVERT_ADDRESS *wa)
{ {
return WinDivertSend(hFilter,packet,(UINT)len,NULL,wa); bool b = WinDivertSend(hFilter,packet,(UINT)len,NULL,wa);
w_win32_error = GetLastError();
return b;
} }
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

@ -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 uint32_t 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");
@ -433,7 +433,7 @@ static int win_main(const char *windivert_filter)
break; break;
} }
fprintf(stderr, "windivert: recv failed. errno %d\n", errno); fprintf(stderr, "windivert: recv failed. errno %d\n", errno);
break; return w_win32_error;
} }
*ifout=0; *ifout=0;
if (wa.Outbound) snprintf(ifout,sizeof(ifout),"%u.%u", wa.Network.IfIdx, wa.Network.SubIfIdx); if (wa.Outbound) snprintf(ifout,sizeof(ifout),"%u.%u", wa.Network.IfIdx, wa.Network.SubIfIdx);