api: translate NTSTATUS to Win32 error codes

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2020-11-04 01:08:41 +01:00
parent f657e6fd27
commit 552821f59a
3 changed files with 15 additions and 13 deletions

View File

@ -1128,7 +1128,7 @@ WintunGetVersion(void)
if (Status == STATUS_INFO_LENGTH_MISMATCH) if (Status == STATUS_INFO_LENGTH_MISMATCH)
continue; continue;
LOG(WINTUN_LOG_ERR, L"Failed to enumerate drivers"); LOG(WINTUN_LOG_ERR, L"Failed to enumerate drivers");
SetLastError(ERROR_GEN_FAILURE); SetLastError(RtlNtStatusToDosError(Status));
return 0; return 0;
} }
DWORD LastError = ERROR_SUCCESS, Version = 0; DWORD LastError = ERROR_SUCCESS, Version = 0;

View File

@ -8,6 +8,7 @@
#include "namespace.h" #include "namespace.h"
#include <Windows.h> #include <Windows.h>
#include <winternl.h>
#include <bcrypt.h> #include <bcrypt.h>
#include <wchar.h> #include <wchar.h>
@ -53,10 +54,11 @@ static _Return_type_success_(return != FALSE) BOOL NamespaceRuntimeInit(void)
return TRUE; return TRUE;
} }
if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&AlgProvider, BCRYPT_SHA256_ALGORITHM, NULL, 0))) NTSTATUS Status;
if (!BCRYPT_SUCCESS(Status = BCryptOpenAlgorithmProvider(&AlgProvider, BCRYPT_SHA256_ALGORITHM, NULL, 0)))
{ {
LOG(WINTUN_LOG_ERR, L"Failed to open algorithm provider"); LOG(WINTUN_LOG_ERR, L"Failed to open algorithm provider");
LastError = ERROR_GEN_FAILURE; LastError = RtlNtStatusToDosError(Status);
goto cleanupLeaveCriticalSection; goto cleanupLeaveCriticalSection;
} }
@ -116,19 +118,20 @@ _Return_type_success_(return != NULL) HANDLE NamespaceTakePoolMutex(_In_z_ const
return NULL; return NULL;
BCRYPT_HASH_HANDLE Sha256 = NULL; BCRYPT_HASH_HANDLE Sha256 = NULL;
if (!BCRYPT_SUCCESS(BCryptCreateHash(AlgProvider, &Sha256, NULL, 0, NULL, 0, 0))) NTSTATUS Status;
if (!BCRYPT_SUCCESS(Status = BCryptCreateHash(AlgProvider, &Sha256, NULL, 0, NULL, 0, 0)))
{ {
LOG(WINTUN_LOG_ERR, L"Failed to create hash"); LOG(WINTUN_LOG_ERR, L"Failed to create hash");
SetLastError(ERROR_GEN_FAILURE); SetLastError(RtlNtStatusToDosError(Status));
return NULL; return NULL;
} }
DWORD LastError; DWORD LastError;
static const WCHAR mutex_label[] = L"Wintun Adapter Name Mutex Stable Suffix v1 jason@zx2c4.com"; static const WCHAR mutex_label[] = L"Wintun Adapter Name Mutex Stable Suffix v1 jason@zx2c4.com";
if (!BCRYPT_SUCCESS( if (!BCRYPT_SUCCESS(
BCryptHashData(Sha256, (PUCHAR)mutex_label, sizeof(mutex_label) /* Including NULL 2 bytes */, 0))) Status = BCryptHashData(Sha256, (PUCHAR)mutex_label, sizeof(mutex_label) /* Including NULL 2 bytes */, 0)))
{ {
LOG(WINTUN_LOG_ERR, L"Failed to hash data"); LOG(WINTUN_LOG_ERR, L"Failed to hash data");
LastError = ERROR_GEN_FAILURE; LastError = RtlNtStatusToDosError(Status);
goto cleanupSha256; goto cleanupSha256;
} }
WCHAR *PoolNorm = NormalizeStringAlloc(NormalizationC, Pool); WCHAR *PoolNorm = NormalizeStringAlloc(NormalizationC, Pool);
@ -138,17 +141,17 @@ _Return_type_success_(return != NULL) HANDLE NamespaceTakePoolMutex(_In_z_ const
goto cleanupSha256; goto cleanupSha256;
} }
if (!BCRYPT_SUCCESS( if (!BCRYPT_SUCCESS(
BCryptHashData(Sha256, (PUCHAR)PoolNorm, (int)wcslen(PoolNorm) + 2 /* Add in NULL 2 bytes */, 0))) Status = BCryptHashData(Sha256, (PUCHAR)PoolNorm, (int)wcslen(PoolNorm) + 2 /* Add in NULL 2 bytes */, 0)))
{ {
LOG(WINTUN_LOG_ERR, L"Failed to hash data"); LOG(WINTUN_LOG_ERR, L"Failed to hash data");
LastError = ERROR_GEN_FAILURE; LastError = RtlNtStatusToDosError(Status);
goto cleanupPoolNorm; goto cleanupPoolNorm;
} }
BYTE Hash[32]; BYTE Hash[32];
if (!BCRYPT_SUCCESS(BCryptFinishHash(Sha256, Hash, sizeof(Hash), 0))) if (!BCRYPT_SUCCESS(Status = BCryptFinishHash(Sha256, Hash, sizeof(Hash), 0)))
{ {
LOG(WINTUN_LOG_ERR, L"Failed to calculate hash"); LOG(WINTUN_LOG_ERR, L"Failed to calculate hash");
LastError = ERROR_GEN_FAILURE; LastError = RtlNtStatusToDosError(Status);
goto cleanupPoolNorm; goto cleanupPoolNorm;
} }
static const WCHAR MutexNamePrefix[] = L"Wintun\\Wintun-Name-Mutex-"; static const WCHAR MutexNamePrefix[] = L"Wintun\\Wintun-Name-Mutex-";

View File

@ -185,8 +185,7 @@ typedef _Return_type_success_(return != FALSE)
* *
* @return If the function succeeds, the return value is the version number. If the function fails, the return value is * @return If the function succeeds, the return value is the version number. If the function fails, the return value is
* zero. To get extended error information, call GetLastError. Possible errors include the following: * zero. To get extended error information, call GetLastError. Possible errors include the following:
* ERROR_FILE_NOT_FOUND Wintun not loaded; * ERROR_FILE_NOT_FOUND Wintun not loaded
* ERROR_GEN_FAILURE Enumerating drivers failed
*/ */
typedef DWORD(WINAPI *WINTUN_GET_VERSION_FUNC)(void); typedef DWORD(WINAPI *WINTUN_GET_VERSION_FUNC)(void);