From 4a575d210a34384787d4936dfb96d6b8cb2ea48c Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 29 Oct 2020 12:04:34 +0100 Subject: api: fallback to hard-coded version Windows 7 doesn't have DriverMajorVersion and DriverMinorVersion registry values yet. Signed-off-by: Simon Rozman --- api/adapter.c | 4 ++-- api/api.c | 18 ++++++------------ api/registry.c | 10 +++++++--- api/registry.h | 6 +++++- 4 files changed, 20 insertions(+), 18 deletions(-) (limited to 'api') diff --git a/api/adapter.c b/api/adapter.c index 0035e8d..63e1e76 100644 --- a/api/adapter.c +++ b/api/adapter.c @@ -541,7 +541,7 @@ CreateAdapterData( HeapFree(ModuleHeap, 0, ValueStr); /* Read the NetLuidIndex value. */ - Result = RegistryQueryDWORD(Key, L"NetLuidIndex", &(*Adapter)->LuidIndex); + Result = RegistryQueryDWORD(Key, L"NetLuidIndex", &(*Adapter)->LuidIndex, TRUE); if (Result != ERROR_SUCCESS) { LOG(WINTUN_LOG_ERR, L"Failed to query NetLuidIndex value"); @@ -549,7 +549,7 @@ CreateAdapterData( } /* Read the NetLuidIndex value. */ - Result = RegistryQueryDWORD(Key, L"*IfType", &(*Adapter)->IfType); + Result = RegistryQueryDWORD(Key, L"*IfType", &(*Adapter)->IfType, TRUE); if (Result != ERROR_SUCCESS) { LOG(WINTUN_LOG_ERR, L"Failed to query *IfType value"); diff --git a/api/api.c b/api/api.c index 7735a24..983596e 100644 --- a/api/api.c +++ b/api/api.c @@ -22,25 +22,19 @@ WintunGetVersion( RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services\\Wintun", 0, KEY_QUERY_VALUE, &Key); if (Result != ERROR_SUCCESS) return LOG_ERROR(L"Failed to open registry key", Result); - Result = RegistryQueryDWORD(Key, L"DriverMajorVersion", DriverVersionMaj); - if (Result != ERROR_SUCCESS) - { - LOG(WINTUN_LOG_ERR, L"Failed to query DriverMajorVersion value"); - goto cleanupKey; - } - Result = RegistryQueryDWORD(Key, L"DriverMinorVersion", DriverVersionMin); - if (Result != ERROR_SUCCESS) + if (RegistryQueryDWORD(Key, L"DriverMajorVersion", DriverVersionMaj, FALSE) != ERROR_SUCCESS || + RegistryQueryDWORD(Key, L"DriverMinorVersion", DriverVersionMin, FALSE) != ERROR_SUCCESS) { - LOG(WINTUN_LOG_ERR, L"Failed to query DriverMinorVersion value"); - goto cleanupKey; + *DriverVersionMaj = WINTUN_VERSION_MAJ; + *DriverVersionMin = WINTUN_VERSION_MIN; } - Result = RegistryQueryDWORD(Key, L"NdisMajorVersion", NdisVersionMaj); + Result = RegistryQueryDWORD(Key, L"NdisMajorVersion", NdisVersionMaj, TRUE); if (Result != ERROR_SUCCESS) { LOG(WINTUN_LOG_ERR, L"Failed to query NdisMajorVersion value"); goto cleanupKey; } - Result = RegistryQueryDWORD(Key, L"NdisMinorVersion", NdisVersionMin); + Result = RegistryQueryDWORD(Key, L"NdisMinorVersion", NdisVersionMin, TRUE); if (Result != ERROR_SUCCESS) LOG(WINTUN_LOG_ERR, L"Failed to query NdisMinorVersion value"); cleanupKey: diff --git a/api/registry.c b/api/registry.c index 3975eef..45e4b21 100644 --- a/api/registry.c +++ b/api/registry.c @@ -249,12 +249,16 @@ RegistryQueryStringWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD } WINTUN_STATUS -RegistryQueryDWORD(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _Out_ DWORD *Value) +RegistryQueryDWORD(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _Out_ DWORD *Value, _In_ BOOL Log) { DWORD ValueType, Size = sizeof(DWORD); DWORD Result = RegQueryValueExW(Key, Name, NULL, &ValueType, (BYTE *)Value, &Size); if (Result != ERROR_SUCCESS) - return LOG_ERROR(L"Querying failed", Result); + { + if (Log) + LOG_ERROR(L"Querying failed", Result); + return Result; + } if (ValueType != REG_DWORD) { LOG(WINTUN_LOG_ERR, L"Value is not a DWORD"); @@ -284,7 +288,7 @@ RegistryQueryDWORDWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD T LOG_ERROR(L"Failed to setup notification", Result); break; } - Result = RegistryQueryDWORD(Key, Name, Value); + Result = RegistryQueryDWORD(Key, Name, Value, FALSE); if (Result != ERROR_FILE_NOT_FOUND && Result != ERROR_PATH_NOT_FOUND) break; LONGLONG TimeLeft = Deadline - GetTickCount64(); diff --git a/api/registry.h b/api/registry.h index fb7d742..a8e5f5f 100644 --- a/api/registry.h +++ b/api/registry.h @@ -118,11 +118,15 @@ RegistryQueryStringWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD * * @param Value Pointer to DWORD to retrieve registry value. * + * @Log Set to TRUE to log all failures; FALSE to skip logging the innermost errors. Skipping innermost + * errors reduces log clutter when we are using RegistryQueryDWORD() from + * RegistryQueryDWORDWait() and some errors are expected to occur. + * * @return ERROR_SUCCESS on success; ERROR_INVALID_DATATYPE when registry value exist but not REG_DWORD type; * ERROR_INVALID_DATA when registry value size is not 4 bytes; Win32 error code otherwise. */ WINTUN_STATUS -RegistryQueryDWORD(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _Out_ DWORD *Value); +RegistryQueryDWORD(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _Out_ DWORD *Value, _In_ BOOL Log); /** * Reads a 32-bit DWORD value from registry key. It waits for the registry value to become available. -- cgit v1.2.3-59-g8ed1b