diff options
author | Simon Rozman <simon@rozman.si> | 2020-11-04 01:08:41 +0100 |
---|---|---|
committer | Simon Rozman <simon@rozman.si> | 2020-11-04 13:21:42 +0100 |
commit | 552821f59ab3d19d26fdfc4958c9fa2ee2e2e123 (patch) | |
tree | 1ee2138c0980540b7494e5b1457810471b4ccb28 | |
parent | api: use GetLastError() to report failures like standard Win32 (diff) | |
download | wintun-552821f59ab3d19d26fdfc4958c9fa2ee2e2e123.tar.xz wintun-552821f59ab3d19d26fdfc4958c9fa2ee2e2e123.zip |
api: translate NTSTATUS to Win32 error codes
Signed-off-by: Simon Rozman <simon@rozman.si>
-rw-r--r-- | api/adapter.c | 2 | ||||
-rw-r--r-- | api/namespace.c | 23 | ||||
-rw-r--r-- | api/wintun.h | 3 |
3 files changed, 15 insertions, 13 deletions
diff --git a/api/adapter.c b/api/adapter.c index 24626e8..62ef05c 100644 --- a/api/adapter.c +++ b/api/adapter.c @@ -1128,7 +1128,7 @@ WintunGetVersion(void) if (Status == STATUS_INFO_LENGTH_MISMATCH) continue; LOG(WINTUN_LOG_ERR, L"Failed to enumerate drivers"); - SetLastError(ERROR_GEN_FAILURE); + SetLastError(RtlNtStatusToDosError(Status)); return 0; } DWORD LastError = ERROR_SUCCESS, Version = 0; diff --git a/api/namespace.c b/api/namespace.c index 883a1a7..44105ca 100644 --- a/api/namespace.c +++ b/api/namespace.c @@ -8,6 +8,7 @@ #include "namespace.h" #include <Windows.h> +#include <winternl.h> #include <bcrypt.h> #include <wchar.h> @@ -53,10 +54,11 @@ static _Return_type_success_(return != FALSE) BOOL NamespaceRuntimeInit(void) 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"); - LastError = ERROR_GEN_FAILURE; + LastError = RtlNtStatusToDosError(Status); goto cleanupLeaveCriticalSection; } @@ -116,19 +118,20 @@ _Return_type_success_(return != NULL) HANDLE NamespaceTakePoolMutex(_In_z_ const return 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"); - SetLastError(ERROR_GEN_FAILURE); + SetLastError(RtlNtStatusToDosError(Status)); return NULL; } DWORD LastError; static const WCHAR mutex_label[] = L"Wintun Adapter Name Mutex Stable Suffix v1 jason@zx2c4.com"; 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"); - LastError = ERROR_GEN_FAILURE; + LastError = RtlNtStatusToDosError(Status); goto cleanupSha256; } WCHAR *PoolNorm = NormalizeStringAlloc(NormalizationC, Pool); @@ -138,17 +141,17 @@ _Return_type_success_(return != NULL) HANDLE NamespaceTakePoolMutex(_In_z_ const goto cleanupSha256; } 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"); - LastError = ERROR_GEN_FAILURE; + LastError = RtlNtStatusToDosError(Status); goto cleanupPoolNorm; } 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"); - LastError = ERROR_GEN_FAILURE; + LastError = RtlNtStatusToDosError(Status); goto cleanupPoolNorm; } static const WCHAR MutexNamePrefix[] = L"Wintun\\Wintun-Name-Mutex-"; diff --git a/api/wintun.h b/api/wintun.h index 459a882..01870b2 100644 --- a/api/wintun.h +++ b/api/wintun.h @@ -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 * zero. To get extended error information, call GetLastError. Possible errors include the following: - * ERROR_FILE_NOT_FOUND Wintun not loaded; - * ERROR_GEN_FAILURE Enumerating drivers failed + * ERROR_FILE_NOT_FOUND Wintun not loaded */ typedef DWORD(WINAPI *WINTUN_GET_VERSION_FUNC)(void); |