diff --git a/api/api.c b/api/api.c index bb8b0db..a470e04 100644 --- a/api/api.c +++ b/api/api.c @@ -31,28 +31,28 @@ WintunGetVersion( DWORD Result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services\\Wintun", 0, KEY_QUERY_VALUE, &Key); if (Result != ERROR_SUCCESS) - return WINTUN_LOGGER_ERROR(L"Failed to open registry key", Result); + return LOG_ERROR(L"Failed to open registry key", Result); Result = RegistryQueryDWORD(Key, L"DriverMajorVersion", DriverVersionMaj); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query DriverMajorVersion value", Result); + LOG_ERROR(L"Failed to query DriverMajorVersion value", Result); goto cleanupKey; } Result = RegistryQueryDWORD(Key, L"DriverMinorVersion", DriverVersionMin); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query DriverMinorVersion value", Result); + LOG_ERROR(L"Failed to query DriverMinorVersion value", Result); goto cleanupKey; } Result = RegistryQueryDWORD(Key, L"NdisMajorVersion", NdisVersionMaj); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query NdisMajorVersion value", Result); + LOG_ERROR(L"Failed to query NdisMajorVersion value", Result); goto cleanupKey; } Result = RegistryQueryDWORD(Key, L"NdisMinorVersion", NdisVersionMin); if (Result != ERROR_SUCCESS) - WINTUN_LOGGER_ERROR(L"Failed to query NdisMinorVersion value", Result); + LOG_ERROR(L"Failed to query NdisMinorVersion value", Result); cleanupKey: RegCloseKey(Key); return Result; diff --git a/api/devmgmt.c b/api/devmgmt.c index 01e96a2..c57cdf1 100644 --- a/api/devmgmt.c +++ b/api/devmgmt.c @@ -51,7 +51,7 @@ GetDeviceRegistryProperty( DWORD Result = GetLastError(); HeapFree(Heap, 0, *Buf); if (Result != ERROR_INSUFFICIENT_BUFFER) - return WINTUN_LOGGER_ERROR(L"Querying property failed", Result); + return LOG_ERROR(L"Querying property failed", Result); } } @@ -91,7 +91,7 @@ GetDeviceRegistryString( HeapFree(GetProcessHeap(), 0, *Buf); return Result; default: - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Property is not a string"); + LOG(WINTUN_LOG_ERR, L"Property is not a string"); HeapFree(GetProcessHeap(), 0, *Buf); return ERROR_INVALID_DATATYPE; } @@ -133,7 +133,7 @@ GetDeviceRegistryMultiString( HeapFree(GetProcessHeap(), 0, *Buf); return Result; default: - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Property is not a string"); + LOG(WINTUN_LOG_ERR, L"Property is not a string"); HeapFree(GetProcessHeap(), 0, *Buf); return ERROR_INVALID_DATATYPE; } @@ -164,7 +164,7 @@ CheckReboot(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA *DevInfoData) SP_DEVINSTALL_PARAMS_W DevInstallParams = { .cbSize = sizeof(SP_DEVINSTALL_PARAMS_W) }; if (!SetupDiGetDeviceInstallParamsW(DevInfo, DevInfoData, &DevInstallParams)) { - WINTUN_LOGGER_LAST_ERROR(L"Retrieving device installation parameters failed"); + LOG_LAST_ERROR(L"Retrieving device installation parameters failed"); return FALSE; } return (DevInstallParams.Flags & (DI_NEEDREBOOT | DI_NEEDRESTART)) != 0; @@ -178,10 +178,10 @@ SetQuietInstall(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA *DevInfoData) { SP_DEVINSTALL_PARAMS_W DevInstallParams = { .cbSize = sizeof(SP_DEVINSTALL_PARAMS_W) }; if (!SetupDiGetDeviceInstallParamsW(DevInfo, DevInfoData, &DevInstallParams)) - return WINTUN_LOGGER_LAST_ERROR(L"Retrieving device installation parameters failed"); + return LOG_LAST_ERROR(L"Retrieving device installation parameters failed"); DevInstallParams.Flags |= DI_QUIETINSTALL; if (!SetupDiSetDeviceInstallParamsW(DevInfo, DevInfoData, &DevInstallParams)) - return WINTUN_LOGGER_LAST_ERROR(L"Setting device installation parameters failed"); + return LOG_LAST_ERROR(L"Setting device installation parameters failed"); return ERROR_SUCCESS; } @@ -202,17 +202,17 @@ GetNetCfgInstanceId(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA *DevInfoData, _O { HKEY Key = SetupDiOpenDevRegKey(DevInfo, DevInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE); if (Key == INVALID_HANDLE_VALUE) - return WINTUN_LOGGER_LAST_ERROR(L"Opening device registry key failed"); + return LOG_LAST_ERROR(L"Opening device registry key failed"); WCHAR *ValueStr; DWORD Result = RegistryQueryString(Key, L"NetCfgInstanceId", &ValueStr); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query NetCfgInstanceId value", Result); + LOG_ERROR(L"Failed to query NetCfgInstanceId value", Result); goto cleanupKey; } if (FAILED(CLSIDFromString(ValueStr, CfgInstanceID))) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"NetCfgInstanceId is not a GUID"); + LOG(WINTUN_LOG_ERR, L"NetCfgInstanceId is not a GUID"); Result = ERROR_INVALID_DATA; } else @@ -242,7 +242,7 @@ GetDevInfoData(_In_ const GUID *CfgInstanceID, _Out_ HDEVINFO *DevInfo, _Out_ SP { *DevInfo = SetupDiGetClassDevsExW(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT, NULL, NULL, NULL); if (!*DevInfo) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to get present class devices"); + return LOG_LAST_ERROR(L"Failed to get present class devices"); for (DWORD EnumIndex = 0;; ++EnumIndex) { DevInfoData->cbSize = sizeof(SP_DEVINFO_DATA); @@ -312,13 +312,13 @@ IsPoolMember( DWORD Result = GetDeviceRegistryString(DevInfo, DevInfoData, SPDRP_DEVICEDESC, &DeviceDesc); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query device description property", Result); + LOG_ERROR(L"Failed to query device description property", Result); return Result; } Result = GetDeviceRegistryString(DevInfo, DevInfoData, SPDRP_FRIENDLYNAME, &FriendlyName); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query friendly name property", Result); + LOG_ERROR(L"Failed to query friendly name property", Result); goto cleanupDeviceDesc; } WCHAR PoolDeviceTypeName[MAX_POOL_DEVICE_TYPE]; @@ -371,7 +371,7 @@ CreateAdapterData( /* Open HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\\ registry key. */ HKEY Key = SetupDiOpenDevRegKey(DevInfo, DevInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE); if (Key == INVALID_HANDLE_VALUE) - return WINTUN_LOGGER_LAST_ERROR(L"Opening device registry key failed"); + return LOG_LAST_ERROR(L"Opening device registry key failed"); HANDLE Heap = GetProcessHeap(); *Adapter = HeapAlloc(Heap, 0, sizeof(WINTUN_ADAPTER)); @@ -386,12 +386,12 @@ CreateAdapterData( Result = RegistryQueryString(Key, L"NetCfgInstanceId", &ValueStr); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query NetCfgInstanceId value", Result); + LOG_ERROR(L"Failed to query NetCfgInstanceId value", Result); goto cleanupAdapter; } if (FAILED(CLSIDFromString(ValueStr, &(*Adapter)->CfgInstanceID))) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"NetCfgInstanceId is not a GUID"); + LOG(WINTUN_LOG_ERR, L"NetCfgInstanceId is not a GUID"); HeapFree(Heap, 0, ValueStr); Result = ERROR_INVALID_DATA; goto cleanupAdapter; @@ -402,7 +402,7 @@ CreateAdapterData( Result = RegistryQueryDWORD(Key, L"NetLuidIndex", &(*Adapter)->LuidIndex); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query NetLuidIndex value", Result); + LOG_ERROR(L"Failed to query NetLuidIndex value", Result); goto cleanupAdapter; } @@ -410,7 +410,7 @@ CreateAdapterData( Result = RegistryQueryDWORD(Key, L"*IfType", &(*Adapter)->IfType); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query *IfType value", Result); + LOG_ERROR(L"Failed to query *IfType value", Result); goto cleanupAdapter; } @@ -418,7 +418,7 @@ CreateAdapterData( if (!SetupDiGetDeviceInstanceIdW( DevInfo, DevInfoData, (*Adapter)->DevInstanceID, _countof((*Adapter)->DevInstanceID), &Size)) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to get device instance ID"); + Result = LOG_LAST_ERROR(L"Failed to get device instance ID"); goto cleanupAdapter; } @@ -476,17 +476,17 @@ GetTcpipInterfaceRegPath(_In_ const WINTUN_ADAPTER *Adapter, _Out_cap_c_(MAX_REG GetTcpipAdapterRegPath(Adapter, TcpipAdapterRegPath); Result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, TcpipAdapterRegPath, 0, KEY_QUERY_VALUE, &TcpipAdapterRegKey); if (Result != ERROR_SUCCESS) - return WINTUN_LOGGER_ERROR(L"Failed to open registry key", Result); + return LOG_ERROR(L"Failed to open registry key", Result); WCHAR *Paths; Result = RegistryQueryString(TcpipAdapterRegKey, L"IpConfig", &Paths); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query IpConfig value", Result); + LOG_ERROR(L"Failed to query IpConfig value", Result); goto cleanupTcpipAdapterRegKey; } if (!Paths[0]) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"IpConfig is empty"); + LOG(WINTUN_LOG_ERR, L"IpConfig is empty"); Result = ERROR_INVALID_DATA; goto cleanupPaths; } @@ -535,7 +535,7 @@ WintunGetAdapter( HDEVINFO DevInfo = SetupDiGetClassDevsExW(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT, NULL, NULL, NULL); if (DevInfo == INVALID_HANDLE_VALUE) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to get present class devices"); + Result = LOG_LAST_ERROR(L"Failed to get present class devices"); goto cleanupMutex; } @@ -572,7 +572,7 @@ WintunGetAdapter( Result = GetDeviceRegistryMultiString(DevInfo, &DevInfoData, SPDRP_HARDWAREID, &Hwids); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query hardware ID", Result); + LOG_ERROR(L"Failed to query hardware ID", Result); goto cleanupDevInfo; } if (!IsOurHardwareID(Hwids)) @@ -593,7 +593,7 @@ WintunGetAdapter( Result = IsPoolMember(Pool, DevInfo, &DevInfoData, &IsMember); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to determine pool membership", Result); + LOG_ERROR(L"Failed to determine pool membership", Result); goto cleanupDevInfo; } if (!IsMember) @@ -604,7 +604,7 @@ WintunGetAdapter( Result = CreateAdapterData(Pool, DevInfo, &DevInfoData, Adapter); if (Result != ERROR_SUCCESS) - WINTUN_LOGGER_ERROR(L"Failed to create adapter data", Result); + LOG_ERROR(L"Failed to create adapter data", Result); goto cleanupDevInfo; } @@ -631,7 +631,7 @@ ConvertInterfaceAliasToGuid(_In_z_ const WCHAR *Name, _Out_ GUID *Guid) NET_LUID Luid; DWORD Result = ConvertInterfaceAliasToLuid(Name, &Luid); if (Result != NO_ERROR) - return WINTUN_LOGGER_ERROR(L"Failed convert interface alias name to the locally unique identifier", Result); + return LOG_ERROR(L"Failed convert interface alias name to the locally unique identifier", Result); return ConvertInterfaceLuidToGuid(&Luid, Guid); } @@ -676,7 +676,7 @@ WintunSetAdapterName(_In_ const WINTUN_ADAPTER *Adapter, _In_z_count_c_(MAX_ADAP if (Result == ERROR_SUCCESS) break; if (i > MaxSuffix || Result != ERROR_DUP_NAME) - return WINTUN_LOGGER_ERROR(L"Setting adapter name failed", Result); + return LOG_ERROR(L"Setting adapter name failed", Result); _snwprintf_s(AvailableName, _countof(AvailableName), _TRUNCATE, L"%.*s %d", MAX_ADAPTER_NAME, Name, i + 1); } @@ -686,7 +686,7 @@ WintunSetAdapterName(_In_ const WINTUN_ADAPTER *Adapter, _In_z_count_c_(MAX_ADAP GetDeviceRegPath(Adapter, DeviceRegPath); Result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, DeviceRegPath, 0, KEY_SET_VALUE, &DeviceRegKey); if (Result != ERROR_SUCCESS) - return WINTUN_LOGGER_ERROR(L"Failed to open registry key", Result); + return LOG_ERROR(L"Failed to open registry key", Result); WCHAR PoolDeviceTypeName[MAX_POOL_DEVICE_TYPE]; GetPoolDeviceTypeName(Adapter->Pool, PoolDeviceTypeName); Result = RegSetKeyValueW( @@ -804,14 +804,14 @@ WintunCreateAdapter( HDEVINFO DevInfo = SetupDiCreateDeviceInfoListExW(&GUID_DEVCLASS_NET, NULL, NULL, NULL); if (DevInfo == INVALID_HANDLE_VALUE) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Creating empty device information set failed"); + Result = LOG_LAST_ERROR(L"Creating empty device information set failed"); goto cleanupMutex; } WCHAR ClassName[MAX_CLASS_NAME_LEN]; if (!SetupDiClassNameFromGuidExW(&GUID_DEVCLASS_NET, ClassName, _countof(ClassName), NULL, NULL, NULL)) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Retrieving class name associated with class GUID failed"); + Result = LOG_LAST_ERROR(L"Retrieving class name associated with class GUID failed"); goto cleanupDevInfo; } @@ -822,26 +822,26 @@ WintunCreateAdapter( if (!SetupDiCreateDeviceInfoW( DevInfo, ClassName, &GUID_DEVCLASS_NET, PoolDeviceTypeName, NULL, DICD_GENERATE_ID, &DevInfoData)) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Creating new device information element failed"); + Result = LOG_LAST_ERROR(L"Creating new device information element failed"); goto cleanupDevInfo; } SetQuietInstall(DevInfo, &DevInfoData); if (!SetupDiSetSelectedDevice(DevInfo, &DevInfoData)) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed selecting device"); + Result = LOG_LAST_ERROR(L"Failed selecting device"); goto cleanupDevInfo; } static const WCHAR Hwids[_countof(WINTUN_HWID) + 1 /*Multi-string terminator*/] = WINTUN_HWID; if (!SetupDiSetDeviceRegistryPropertyW(DevInfo, &DevInfoData, SPDRP_HARDWAREID, (const BYTE *)Hwids, sizeof(Hwids))) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed setting hardware ID"); + Result = LOG_LAST_ERROR(L"Failed setting hardware ID"); goto cleanupDevInfo; } if (!SetupDiBuildDriverInfoList(DevInfo, &DevInfoData, SPDIT_COMPATDRIVER)) /* TODO: This takes ~510ms */ { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed building driver info list"); + Result = LOG_LAST_ERROR(L"Failed building driver info list"); goto cleanupDevInfo; } @@ -883,18 +883,18 @@ WintunCreateAdapter( if (!DriverVersion) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"No appropriate drivers found"); + LOG(WINTUN_LOG_ERR, L"No appropriate drivers found"); Result = ERROR_FILE_NOT_FOUND; goto cleanupDriverInfoList; } if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE, DevInfo, &DevInfoData)) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Registering device failed"); + Result = LOG_LAST_ERROR(L"Registering device failed"); goto cleanupDevice; } if (!SetupDiCallClassInstaller(DIF_REGISTER_COINSTALLERS, DevInfo, &DevInfoData)) - WINTUN_LOGGER_LAST_ERROR(L"Registering coinstallers failed"); + LOG_LAST_ERROR(L"Registering coinstallers failed"); HKEY NetDevRegKey = INVALID_HANDLE_VALUE; const int PollTimeout = 50 /* ms */; @@ -907,7 +907,7 @@ WintunCreateAdapter( } if (NetDevRegKey == INVALID_HANDLE_VALUE) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to open device-specific registry key"); + Result = LOG_LAST_ERROR(L"Failed to open device-specific registry key"); goto cleanupDevice; } if (RequestedGUID) @@ -922,17 +922,17 @@ WintunCreateAdapter( StringFromGUID2(RequestedGUID, RequestedGUIDStr, _countof(RequestedGUIDStr)) * sizeof(WCHAR)); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_LAST_ERROR(L"Failed to set NetSetupAnticipatedInstanceId"); + LOG_LAST_ERROR(L"Failed to set NetSetupAnticipatedInstanceId"); goto cleanupNetDevRegKey; } } if (!SetupDiCallClassInstaller(DIF_INSTALLINTERFACES, DevInfo, &DevInfoData)) - WINTUN_LOGGER_LAST_ERROR(L"Installing interfaces failed"); + LOG_LAST_ERROR(L"Installing interfaces failed"); if (!SetupDiCallClassInstaller(DIF_INSTALLDEVICE, DevInfo, &DevInfoData)) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Installing device failed"); + Result = LOG_LAST_ERROR(L"Installing device failed"); goto cleanupNetDevRegKey; } *RebootRequired = *RebootRequired || CheckReboot(DevInfo, &DevInfoData); @@ -944,7 +944,7 @@ WintunCreateAdapter( (const BYTE *)PoolDeviceTypeName, (DWORD)((wcslen(PoolDeviceTypeName) + 1) * sizeof(WCHAR)))) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to set device description"); + Result = LOG_LAST_ERROR(L"Failed to set device description"); goto cleanupNetDevRegKey; } @@ -954,7 +954,7 @@ WintunCreateAdapter( Result = RegistryQueryStringWait(NetDevRegKey, L"NetCfgInstanceId", WAIT_FOR_REGISTRY_TIMEOUT, &DummyStr); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query NetCfgInstanceId value", Result); + LOG_ERROR(L"Failed to query NetCfgInstanceId value", Result); goto cleanupNetDevRegKey; } HeapFree(Heap, 0, DummyStr); @@ -962,20 +962,20 @@ WintunCreateAdapter( Result = RegistryQueryDWORDWait(NetDevRegKey, L"NetLuidIndex", WAIT_FOR_REGISTRY_TIMEOUT, &DummyDWORD); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query NetLuidIndex value", Result); + LOG_ERROR(L"Failed to query NetLuidIndex value", Result); goto cleanupNetDevRegKey; } Result = RegistryQueryDWORDWait(NetDevRegKey, L"*IfType", WAIT_FOR_REGISTRY_TIMEOUT, &DummyDWORD); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query *IfType value", Result); + LOG_ERROR(L"Failed to query *IfType value", Result); goto cleanupNetDevRegKey; } Result = CreateAdapterData(Pool, DevInfo, &DevInfoData, Adapter); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to create adapter data", Result); + LOG_ERROR(L"Failed to create adapter data", Result); goto cleanupNetDevRegKey; } @@ -990,13 +990,13 @@ WintunCreateAdapter( &TcpipAdapterRegKey); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to open adapter-specific TCP/IP adapter registry key", Result); + LOG_ERROR(L"Failed to open adapter-specific TCP/IP adapter registry key", Result); goto cleanupAdapter; } Result = RegistryQueryStringWait(TcpipAdapterRegKey, L"IpConfig", WAIT_FOR_REGISTRY_TIMEOUT, &DummyStr); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query IpConfig value", Result); + LOG_ERROR(L"Failed to query IpConfig value", Result); goto cleanupTcpipAdapterRegKey; } HeapFree(Heap, 0, DummyStr); @@ -1006,7 +1006,7 @@ WintunCreateAdapter( Result = GetTcpipInterfaceRegPath(*Adapter, TcpipInterfaceRegPath); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to determine interface-specific TCP/IP network registry key path", Result); + LOG_ERROR(L"Failed to determine interface-specific TCP/IP network registry key path", Result); goto cleanupTcpipAdapterRegKey; } Result = RegistryOpenKeyWait( @@ -1017,7 +1017,7 @@ WintunCreateAdapter( &TcpipInterfaceRegKey); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to open interface-specific TCP/IP network registry key", Result); + LOG_ERROR(L"Failed to open interface-specific TCP/IP network registry key", Result); goto cleanupTcpipAdapterRegKey; } @@ -1025,11 +1025,11 @@ WintunCreateAdapter( Result = RegSetKeyValueW( TcpipInterfaceRegKey, NULL, L"EnableDeadGWDetect", REG_DWORD, &EnableDeadGWDetect, sizeof(EnableDeadGWDetect)); if (Result != ERROR_SUCCESS) - WINTUN_LOGGER_ERROR(L"Failed to set EnableDeadGWDetect", Result); + LOG_ERROR(L"Failed to set EnableDeadGWDetect", Result); Result = WintunSetAdapterName(*Adapter, Name); if (Result != ERROR_SUCCESS) - WINTUN_LOGGER_ERROR(L"Failed to set adapter name", Result); + LOG_ERROR(L"Failed to set adapter name", Result); RegCloseKey(TcpipInterfaceRegKey); cleanupTcpipAdapterRegKey: RegCloseKey(TcpipAdapterRegKey); @@ -1079,7 +1079,7 @@ WintunDeleteAdapter(_In_ const WINTUN_ADAPTER *Adapter, _Inout_ BOOL *RebootRequ return ERROR_SUCCESS; if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to get device info data", Result); + LOG_ERROR(L"Failed to get device info data", Result); return Result; } SetQuietInstall(DevInfo, &DevInfoData); @@ -1091,7 +1091,7 @@ WintunDeleteAdapter(_In_ const WINTUN_ADAPTER *Adapter, _Inout_ BOOL *RebootRequ SetupDiCallClassInstaller(DIF_REMOVE, DevInfo, &DevInfoData)) *RebootRequired = *RebootRequired || CheckReboot(DevInfo, &DevInfoData); else - Result = WINTUN_LOGGER_LAST_ERROR(L"Unable to remove existing adapter"); + Result = LOG_LAST_ERROR(L"Unable to remove existing adapter"); SetupDiDestroyDeviceInfoList(DevInfo); return Result; } @@ -1118,7 +1118,7 @@ WintunEnumAdapters(_In_z_count_c_(MAX_POOL) const WCHAR *Pool, _In_ WINTUN_ENUM_ HDEVINFO DevInfo = SetupDiGetClassDevsExW(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT, NULL, NULL, NULL); if (DevInfo == INVALID_HANDLE_VALUE) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to get present class devices"); + Result = LOG_LAST_ERROR(L"Failed to get present class devices"); goto cleanupMutex; } HANDLE Heap = GetProcessHeap(); @@ -1141,7 +1141,7 @@ WintunEnumAdapters(_In_z_count_c_(MAX_POOL) const WCHAR *Pool, _In_ WINTUN_ENUM_ Result = GetDeviceRegistryMultiString(DevInfo, &DevInfoData, SPDRP_HARDWAREID, &Hwids); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to query hardware ID", Result); + LOG_ERROR(L"Failed to query hardware ID", Result); break; } if (!IsOurHardwareID(Hwids)) @@ -1158,7 +1158,7 @@ WintunEnumAdapters(_In_z_count_c_(MAX_POOL) const WCHAR *Pool, _In_ WINTUN_ENUM_ Result = IsPoolMember(Pool, DevInfo, &DevInfoData, &IsMember); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to determine pool membership", Result); + LOG_ERROR(L"Failed to determine pool membership", Result); break; } if (!IsMember) @@ -1168,7 +1168,7 @@ WintunEnumAdapters(_In_z_count_c_(MAX_POOL) const WCHAR *Pool, _In_ WINTUN_ENUM_ Result = CreateAdapterData(Pool, DevInfo, &DevInfoData, &Adapter); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to create adapter data", Result); + LOG_ERROR(L"Failed to create adapter data", Result); break; } if (Func(Adapter, Param)) diff --git a/api/driver.c b/api/driver.c index 8884acc..3a27f0a 100644 --- a/api/driver.c +++ b/api/driver.c @@ -53,7 +53,7 @@ _Return_type_success_(return != NULL) SP_DRVINFO_DETAIL_DATA_W *DriverGetDrvInfo HeapFree(Heap, 0, DrvInfoDetailData); if (Result != ERROR_INSUFFICIENT_BUFFER) { - WINTUN_LOGGER_ERROR(L"Failed", Result); + LOG_ERROR(L"Failed", Result); goto out; } } @@ -78,7 +78,7 @@ DriverIsWintunAdapter(_In_ HDEVINFO DevInfo, _In_opt_ SP_DEVINFO_DATA *DevInfoDa BOOL Found = FALSE; if (!SetupDiBuildDriverInfoList(DevInfo, DevInfoData, SPDIT_COMPATDRIVER)) { - WINTUN_LOGGER_LAST_ERROR(L"Failed to build list of drivers"); + LOG_LAST_ERROR(L"Failed to build list of drivers"); return FALSE; } HANDLE Heap = GetProcessHeap(); @@ -119,7 +119,7 @@ _Return_type_success_(return != INVALID_HANDLE_VALUE) HANDLE &InterfacesLen, (GUID *)&GUID_DEVINTERFACE_NET, (DEVINSTID_W)InstanceId, CM_GET_DEVICE_INTERFACE_LIST_PRESENT); if (Result != CR_SUCCESS) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to get device associated device instances size"); + LOG(WINTUN_LOG_ERR, L"Failed to get device associated device instances size"); SetLastError(ERROR_GEN_FAILURE); return INVALID_HANDLE_VALUE; } @@ -137,7 +137,7 @@ _Return_type_success_(return != INVALID_HANDLE_VALUE) HANDLE CM_GET_DEVICE_INTERFACE_LIST_PRESENT); if (Result != CR_SUCCESS) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to get device associated device instances"); + LOG(WINTUN_LOG_ERR, L"Failed to get device associated device instances"); Result = ERROR_GEN_FAILURE; goto cleanupBuf; } @@ -149,7 +149,7 @@ _Return_type_success_(return != INVALID_HANDLE_VALUE) HANDLE OPEN_EXISTING, 0, NULL); - Result = Handle != INVALID_HANDLE_VALUE ? ERROR_SUCCESS : WINTUN_LOGGER_LAST_ERROR(L"Failed to connect to device"); + Result = Handle != INVALID_HANDLE_VALUE ? ERROR_SUCCESS : LOG_LAST_ERROR(L"Failed to connect to device"); cleanupBuf: HeapFree(Heap, 0, Interfaces); SetLastError(Result); @@ -238,7 +238,7 @@ DriverGetVersion(_Out_ FILETIME *DriverDate, _Out_ DWORDLONG *DriverVersion) DWORD SizeResource; DWORD Result = ResourceGetAddress(HaveWHQL() ? L"wintun-whql.inf" : L"wintun.inf", &LockedResource, &SizeResource); if (Result != ERROR_SUCCESS) - return WINTUN_LOGGER_ERROR(L"Failed to locate resource", Result); + return LOG_ERROR(L"Failed to locate resource", Result); enum { SectNone, @@ -283,13 +283,13 @@ DriverGetVersion(_Out_ FILETIME *DriverDate, _Out_ DWORDLONG *DriverVersion) break; if (*p != '/' && *p != '-') { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Unexpected date delimiter"); + LOG(WINTUN_LOG_ERR, L"Unexpected date delimiter"); return ERROR_INVALID_DATA; } } if (date[0] < 1 || date[0] > 12 || date[1] < 1 || date[1] > 31 || date[2] < 1601 || date[2] > 30827) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Invalid date"); + LOG(WINTUN_LOG_ERR, L"Invalid date"); return ERROR_INVALID_DATA; } const SYSTEMTIME st = { .wYear = (WORD)date[2], .wMonth = (WORD)date[0], .wDay = (WORD)date[1] }; @@ -304,7 +304,7 @@ DriverGetVersion(_Out_ FILETIME *DriverDate, _Out_ DWORDLONG *DriverVersion) version[i] = strtoul(p, &p_next, 10); if (version[i] > 0xffff) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Version field may not exceed 65535"); + LOG(WINTUN_LOG_ERR, L"Version field may not exceed 65535"); return ERROR_INVALID_DATA; } p = p_next; @@ -312,7 +312,7 @@ DriverGetVersion(_Out_ FILETIME *DriverDate, _Out_ DWORDLONG *DriverVersion) break; if (*p != '.') { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Unexpected version delimiter"); + LOG(WINTUN_LOG_ERR, L"Unexpected version delimiter"); return ERROR_INVALID_DATA; } } @@ -324,7 +324,7 @@ DriverGetVersion(_Out_ FILETIME *DriverDate, _Out_ DWORDLONG *DriverVersion) } Inf = SkipNonLF(Inf, InfEnd); } - WINTUN_LOGGER(WINTUN_LOG_ERR, L"DriverVer not found in INF resource"); + LOG(WINTUN_LOG_ERR, L"DriverVer not found in INF resource"); return ERROR_FILE_NOT_FOUND; } @@ -400,12 +400,12 @@ static BOOL EnsureDriverUnloaded(VOID) static WINTUN_STATUS InstallCertificate(_In_z_ const WCHAR *SignedResource) { - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Trusting code signing certificate"); + LOG(WINTUN_LOG_INFO, L"Trusting code signing certificate"); const VOID *LockedResource; DWORD SizeResource; DWORD Result = ResourceGetAddress(SignedResource, &LockedResource, &SizeResource); if (Result != ERROR_SUCCESS) - return WINTUN_LOGGER_ERROR("Failed to locate resource", Result); + return LOG_ERROR("Failed to locate resource", Result); const CERT_BLOB CertBlob = { .cbData = SizeResource, .pbData = (BYTE *)LockedResource }; HCERTSTORE QueriedStore; if (!CryptQueryObject( @@ -420,12 +420,12 @@ InstallCertificate(_In_z_ const WCHAR *SignedResource) &QueriedStore, 0, NULL)) - return WINTUN_LOGGER_LAST_ERROR("Failed to find certificate"); + return LOG_LAST_ERROR("Failed to find certificate"); HCERTSTORE TrustedStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"TrustedPublisher"); if (!TrustedStore) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to open store"); + Result = LOG_LAST_ERROR(L"Failed to open store"); goto cleanupQueriedStore; } LPSTR CodeSigningOid[] = { szOID_PKIX_KP_CODE_SIGNING }; @@ -455,7 +455,7 @@ InstallCertificate(_In_z_ const WCHAR *SignedResource) !Constraints.fCA) if (!CertAddCertificateContextToStore(TrustedStore, CertContext, CERT_STORE_ADD_REPLACE_EXISTING, NULL)) { - WINTUN_LOGGER_LAST_ERROR(L"Failed to add certificate to store"); + LOG_LAST_ERROR(L"Failed to add certificate to store"); Result = Result != ERROR_SUCCESS ? Result : GetLastError(); } } @@ -477,14 +477,14 @@ InstallDriver(_In_ BOOL UpdateExisting) { WCHAR WindowsDirectory[MAX_PATH]; if (!GetWindowsDirectoryW(WindowsDirectory, _countof(WindowsDirectory))) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to get Windows folder"); + return LOG_LAST_ERROR(L"Failed to get Windows folder"); WCHAR WindowsTempDirectory[MAX_PATH]; if (!PathCombineW(WindowsTempDirectory, WindowsDirectory, L"Temp")) return ERROR_BUFFER_OVERFLOW; UCHAR RandomBytes[32] = { 0 }; # pragma warning(suppress : 6387) if (!RtlGenRandom(RandomBytes, sizeof(RandomBytes))) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to generate random"); + return LOG_LAST_ERROR(L"Failed to generate random"); WCHAR RandomSubDirectory[sizeof(RandomBytes) * 2 + 1]; for (int i = 0; i < sizeof(RandomBytes); ++i) swprintf_s(&RandomSubDirectory[i * 2], 3, L"%02x", RandomBytes[i]); @@ -494,11 +494,11 @@ InstallDriver(_In_ BOOL UpdateExisting) SECURITY_ATTRIBUTES SecurityAttributes = { .nLength = sizeof(SecurityAttributes) }; if (!ConvertStringSecurityDescriptorToSecurityDescriptorW( L"O:SYD:P(A;;GA;;;SY)", SDDL_REVISION_1, &SecurityAttributes.lpSecurityDescriptor, NULL)) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to convert security descriptor"); + return LOG_LAST_ERROR(L"Failed to convert security descriptor"); DWORD Result = ERROR_SUCCESS; if (!CreateDirectoryW(RandomTempSubDirectory, &SecurityAttributes)) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to create temporary folder"); + Result = LOG_LAST_ERROR(L"Failed to create temporary folder"); goto cleanupFree; } @@ -515,9 +515,9 @@ InstallDriver(_In_ BOOL UpdateExisting) BOOL UseWHQL = HaveWHQL(); if (!UseWHQL && (Result = InstallCertificate(L"wintun.sys")) != ERROR_SUCCESS) - WINTUN_LOGGER_ERROR(L"Unable to install code signing certificate", Result); + LOG_ERROR(L"Unable to install code signing certificate", Result); - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Copying resources to temporary path"); + LOG(WINTUN_LOG_INFO, L"Copying resources to temporary path"); if ((Result = ResourceCopyToFile(CatPath, &SecurityAttributes, UseWHQL ? L"wintun-whql.cat" : L"wintun.cat")) != ERROR_SUCCESS || (Result = ResourceCopyToFile(SysPath, &SecurityAttributes, UseWHQL ? L"wintun-whql.sys" : L"wintun.sys")) != @@ -525,20 +525,20 @@ InstallDriver(_In_ BOOL UpdateExisting) (Result = ResourceCopyToFile(InfPath, &SecurityAttributes, UseWHQL ? L"wintun-whql.inf" : L"wintun.inf")) != ERROR_SUCCESS) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to copy resources"); + Result = LOG_LAST_ERROR(L"Failed to copy resources"); goto cleanupDelete; } - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Installing driver"); + LOG(WINTUN_LOG_INFO, L"Installing driver"); if (!SetupCopyOEMInfW(InfPath, NULL, SPOST_PATH, 0, NULL, 0, NULL, NULL)) - Result = WINTUN_LOGGER_LAST_ERROR(L"Could not install driver to store"); + Result = LOG_LAST_ERROR(L"Could not install driver to store"); BOOL RebootRequired = FALSE; if (UpdateExisting && !UpdateDriverForPlugAndPlayDevicesW( NULL, WINTUN_HWID, InfPath, INSTALLFLAG_FORCE | INSTALLFLAG_NONINTERACTIVE, &RebootRequired)) - WINTUN_LOGGER_LAST_ERROR(L"Could not update existing adapters"); + LOG_LAST_ERROR(L"Could not update existing adapters"); if (RebootRequired) - WINTUN_LOGGER(WINTUN_LOG_WARN, L"A reboot might be required, which really should not be the case"); + LOG(WINTUN_LOG_WARN, L"A reboot might be required, which really should not be the case"); cleanupDelete: DeleteFileW(CatPath); @@ -559,11 +559,11 @@ static WINTUN_STATUS RemoveDriver(VOID) { HDEVINFO DevInfo = SetupDiGetClassDevsW(&GUID_DEVCLASS_NET, NULL, NULL, 0); if (!DevInfo) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to request device information"); + return LOG_LAST_ERROR(L"Failed to request device information"); DWORD Result = ERROR_SUCCESS; if (!SetupDiBuildDriverInfoList(DevInfo, NULL, SPDIT_CLASSDRIVER)) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to build list of drivers"); + Result = LOG_LAST_ERROR(L"Failed to build list of drivers"); goto cleanupDeviceInfoSet; } HANDLE Heap = GetProcessHeap(); @@ -582,10 +582,10 @@ static WINTUN_STATUS RemoveDriver(VOID) if (!_wcsicmp(DrvInfoDetailData->HardwareID, WINTUN_HWID)) { PathStripPathW(DrvInfoDetailData->InfFileName); - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Removing existing driver"); + LOG(WINTUN_LOG_INFO, L"Removing existing driver"); if (!SetupUninstallOEMInfW(DrvInfoDetailData->InfFileName, SUOI_FORCEDELETE, NULL)) { - WINTUN_LOGGER_LAST_ERROR(L"Unable to remove existing driver"); + LOG_LAST_ERROR(L"Unable to remove existing driver"); Result = Result != ERROR_SUCCESS ? Result : GetLastError(); } } @@ -616,14 +616,14 @@ ForceCloseWintunAdapterHandle(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA *DevIn DWORD RequiredBytes; if (SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, NULL, 0, &RequiredBytes) || (Result = GetLastError()) != ERROR_INSUFFICIENT_BUFFER) - return WINTUN_LOGGER_ERROR(L"Failed to query device instance ID size", Result); + return LOG_ERROR(L"Failed to query device instance ID size", Result); HANDLE Heap = GetProcessHeap(); WCHAR *InstanceId = HeapAlloc(Heap, HEAP_ZERO_MEMORY, sizeof(*InstanceId) * RequiredBytes); if (!InstanceId) return ERROR_OUTOFMEMORY; if (!SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, InstanceId, RequiredBytes, &RequiredBytes)) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to get device instance ID"); + Result = LOG_LAST_ERROR(L"Failed to get device instance ID"); goto out; } HANDLE NdisHandle = DriverGetAdapterDeviceObject(InstanceId); @@ -634,7 +634,7 @@ ForceCloseWintunAdapterHandle(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA *DevIn } Result = DeviceIoControl(NdisHandle, TUN_IOCTL_FORCE_CLOSE_HANDLES, NULL, 0, NULL, 0, &RequiredBytes, NULL) ? ERROR_SUCCESS - : WINTUN_LOGGER_LAST_ERROR(L"Failed to perform ioctl"); + : LOG_LAST_ERROR(L"Failed to perform ioctl"); CloseHandle(NdisHandle); out: HeapFree(Heap, 0, InstanceId); @@ -682,16 +682,16 @@ DisableWintunAdapters(_In_ HDEVINFO DevInfo, _Inout_ SP_DEVINFO_DATA_LIST **Disa ((Status & DN_HAS_PROBLEM) && ProblemCode == CM_PROB_DISABLED)) goto cleanupDeviceInfoData; - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Force closing all open handles for existing adapter"); + LOG(WINTUN_LOG_INFO, L"Force closing all open handles for existing adapter"); if (ForceCloseWintunAdapterHandle(DevInfo, &DeviceNode->Data) != ERROR_SUCCESS) - WINTUN_LOGGER(WINTUN_LOG_WARN, L"Failed to force close adapter handles"); + LOG(WINTUN_LOG_WARN, L"Failed to force close adapter handles"); Sleep(200); - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Disabling existing adapter"); + LOG(WINTUN_LOG_INFO, L"Disabling existing adapter"); if (!SetupDiSetClassInstallParamsW(DevInfo, &DeviceNode->Data, &Params.ClassInstallHeader, sizeof(Params)) || !SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, DevInfo, &DeviceNode->Data)) { - WINTUN_LOGGER_LAST_ERROR(L"Unable to disable existing adapter"); + LOG_LAST_ERROR(L"Unable to disable existing adapter"); Result = Result != ERROR_SUCCESS ? Result : GetLastError(); goto cleanupDeviceInfoData; } @@ -734,16 +734,16 @@ RemoveWintunAdapters(_In_ HDEVINFO DevInfo) if (!DriverIsWintunAdapter(DevInfo, &DevInfoData)) continue; - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Force closing all open handles for existing adapter"); + LOG(WINTUN_LOG_INFO, L"Force closing all open handles for existing adapter"); if (ForceCloseWintunAdapterHandle(DevInfo, &DevInfoData) != ERROR_SUCCESS) - WINTUN_LOGGER(WINTUN_LOG_WARN, L"Failed to force close adapter handles"); + LOG(WINTUN_LOG_WARN, L"Failed to force close adapter handles"); Sleep(200); - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Removing existing adapter"); + LOG(WINTUN_LOG_INFO, L"Removing existing adapter"); if (!SetupDiSetClassInstallParamsW(DevInfo, &DevInfoData, &Params.ClassInstallHeader, sizeof(Params)) || !SetupDiCallClassInstaller(DIF_REMOVE, DevInfo, &DevInfoData)) { - WINTUN_LOGGER_LAST_ERROR(L"Unable to remove existing adapter"); + LOG_LAST_ERROR(L"Unable to remove existing adapter"); Result = Result != ERROR_SUCCESS ? Result : GetLastError(); } } @@ -769,11 +769,11 @@ EnableWintunAdapters(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA_LIST *AdaptersT DWORD Result = ERROR_SUCCESS; for (SP_DEVINFO_DATA_LIST *DeviceNode = AdaptersToEnable; DeviceNode; DeviceNode = DeviceNode->Next) { - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Enabling existing adapter"); + LOG(WINTUN_LOG_INFO, L"Enabling existing adapter"); if (!SetupDiSetClassInstallParamsW(DevInfo, &DeviceNode->Data, &Params.ClassInstallHeader, sizeof(Params)) || !SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, DevInfo, &DeviceNode->Data)) { - WINTUN_LOGGER_LAST_ERROR(L"Unable to enable existing adapter"); + LOG_LAST_ERROR(L"Unable to enable existing adapter"); Result = Result != ERROR_SUCCESS ? Result : GetLastError(); } } @@ -790,27 +790,27 @@ WINTUN_STATUS DriverInstallOrUpdate(VOID) HANDLE Heap = GetProcessHeap(); HDEVINFO DevInfo = SetupDiGetClassDevsExW(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT, NULL, NULL, NULL); if (DevInfo == INVALID_HANDLE_VALUE) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to get present class devices"); + return LOG_LAST_ERROR(L"Failed to get present class devices"); SP_DEVINFO_DATA_LIST *ExistingAdapters = NULL; if (IsDriverLoaded()) { DisableWintunAdapters(DevInfo, &ExistingAdapters); - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Waiting for driver to unload from kernel"); + LOG(WINTUN_LOG_INFO, L"Waiting for driver to unload from kernel"); if (!EnsureDriverUnloaded()) - WINTUN_LOGGER(WINTUN_LOG_WARN, L"Unable to unload driver, which means a reboot will likely be required"); + LOG(WINTUN_LOG_WARN, L"Unable to unload driver, which means a reboot will likely be required"); } DWORD Result = ERROR_SUCCESS; if ((Result = RemoveDriver()) != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to uninstall old drivers", Result); + LOG_ERROR(L"Failed to uninstall old drivers", Result); goto cleanupAdapters; } if ((Result = InstallDriver(!!ExistingAdapters)) != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to install driver", Result); + LOG_ERROR(L"Failed to install driver", Result); goto cleanupAdapters; } - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Installation successful"); + LOG(WINTUN_LOG_INFO, L"Installation successful"); cleanupAdapters:; if (ExistingAdapters) @@ -836,13 +836,13 @@ WINTUN_STATUS DriverUninstall(VOID) { HDEVINFO DevInfo = SetupDiGetClassDevsExW(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT, NULL, NULL, NULL); if (DevInfo == INVALID_HANDLE_VALUE) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to get present class devices"); + return LOG_LAST_ERROR(L"Failed to get present class devices"); RemoveWintunAdapters(DevInfo); DWORD Result = RemoveDriver(); if (Result != ERROR_SUCCESS) - WINTUN_LOGGER_ERROR(L"Failed to uninstall driver", Result); + LOG_ERROR(L"Failed to uninstall driver", Result); else - WINTUN_LOGGER(WINTUN_LOG_INFO, L"Uninstallation successful"); + LOG(WINTUN_LOG_INFO, L"Uninstallation successful"); return Result; } diff --git a/api/logger.h b/api/logger.h index 15b6bbb..f795a00 100644 --- a/api/logger.h +++ b/api/logger.h @@ -33,6 +33,6 @@ LoggerLastError(_In_z_ const WCHAR *Prefix) return Error; } -#define WINTUN_LOGGER(lvl, msg) Logger((lvl), _L(__FUNCTION__) L": " msg) -#define WINTUN_LOGGER_ERROR(msg, err) LoggerError(_L(__FUNCTION__) L": " msg, (err)) -#define WINTUN_LOGGER_LAST_ERROR(msg) LoggerLastError(_L(__FUNCTION__) L": " msg) +#define LOG(lvl, msg) Logger((lvl), _L(__FUNCTION__) L": " msg) +#define LOG_ERROR(msg, err) LoggerError(_L(__FUNCTION__) L": " msg, (err)) +#define LOG_LAST_ERROR(msg) LoggerLastError(_L(__FUNCTION__) L": " msg) diff --git a/api/registry.c b/api/registry.c index 57d948d..e591a46 100644 --- a/api/registry.c +++ b/api/registry.c @@ -15,13 +15,13 @@ OpenKeyWait(_In_ HKEY Key, _Inout_z_ WCHAR *Path, _In_ DWORD Access, _In_ ULONGL HANDLE Event = CreateEventW(NULL, FALSE, FALSE, NULL); if (!Event) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to create event"); + return LOG_LAST_ERROR(L"Failed to create event"); for (;;) { Result = RegNotifyChangeKeyValue(Key, FALSE, REG_NOTIFY_CHANGE_NAME, Event, TRUE); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to setup notification", Result); + LOG_ERROR(L"Failed to setup notification", Result); break; } @@ -40,7 +40,7 @@ OpenKeyWait(_In_ HKEY Key, _Inout_z_ WCHAR *Path, _In_ DWORD Access, _In_ ULONGL } if (Result != ERROR_FILE_NOT_FOUND && Result != ERROR_PATH_NOT_FOUND) { - WINTUN_LOGGER_ERROR(L"Failed to open", Result); + LOG_ERROR(L"Failed to open", Result); break; } @@ -49,7 +49,7 @@ OpenKeyWait(_In_ HKEY Key, _Inout_z_ WCHAR *Path, _In_ DWORD Access, _In_ ULONGL TimeLeft = 0; if (WaitForSingleObject(Event, (DWORD)TimeLeft) != WAIT_OBJECT_0) { - WINTUN_LOGGER(WINTUN_LOG_ERR, "Timeout waiting"); + LOG(WINTUN_LOG_ERR, "Timeout waiting"); break; } } @@ -145,7 +145,7 @@ RegistryGetString(_Inout_ WCHAR **Buf, _In_ DWORD Len, _In_ DWORD ValueType) DWORD Result = ExpandEnvironmentStringsW(*Buf, Expanded, Len); if (!Result) { - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to expand environment variables"); + Result = LOG_LAST_ERROR(L"Failed to expand environment variables"); HeapFree(Heap, 0, Expanded); return Result; } @@ -266,7 +266,7 @@ RegistryQuery( return ERROR_SUCCESS; HeapFree(Heap, 0, *Buf); if (Result != ERROR_MORE_DATA) - return WINTUN_LOGGER_ERROR(L"Querying value failed", Result); + return LOG_ERROR(L"Querying value failed", Result); } } @@ -302,7 +302,7 @@ RegistryQueryString(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _Out_ WCHAR **V HeapFree(GetProcessHeap(), 0, *Value); return Result; default: - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Value is not a string"); + LOG(WINTUN_LOG_ERR, L"Value is not a string"); HeapFree(GetProcessHeap(), 0, *Value); return ERROR_INVALID_DATATYPE; } @@ -332,13 +332,13 @@ RegistryQueryStringWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD ULONGLONG Deadline = GetTickCount64() + Timeout; HANDLE Event = CreateEventW(NULL, FALSE, FALSE, NULL); if (!Event) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to create event"); + return LOG_LAST_ERROR(L"Failed to create event"); for (;;) { Result = RegNotifyChangeKeyValue(Key, FALSE, REG_NOTIFY_CHANGE_LAST_SET, Event, TRUE); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to setup notification", Result); + LOG_ERROR(L"Failed to setup notification", Result); break; } Result = RegistryQueryString(Key, Name, Value); @@ -349,7 +349,7 @@ RegistryQueryStringWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD TimeLeft = 0; if (WaitForSingleObject(Event, (DWORD)TimeLeft) != WAIT_OBJECT_0) { - WINTUN_LOGGER(WINTUN_LOG_ERR, "Timeout waiting"); + LOG(WINTUN_LOG_ERR, "Timeout waiting"); break; } } @@ -375,15 +375,15 @@ RegistryQueryDWORD(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _Out_ DWORD *Val DWORD ValueType, Size = sizeof(DWORD); DWORD Result = RegQueryValueExW(Key, Name, NULL, &ValueType, (BYTE *)Value, &Size); if (Result != ERROR_SUCCESS) - return WINTUN_LOGGER_ERROR(L"Querying failed", Result); + return LOG_ERROR(L"Querying failed", Result); if (ValueType != REG_DWORD) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Value is not a DWORD"); + LOG(WINTUN_LOG_ERR, L"Value is not a DWORD"); return ERROR_INVALID_DATATYPE; } if (Size != sizeof(DWORD)) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Value size is not 4 bytes"); + LOG(WINTUN_LOG_ERR, L"Value size is not 4 bytes"); return ERROR_INVALID_DATA; } return ERROR_SUCCESS; @@ -410,13 +410,13 @@ RegistryQueryDWORDWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD T ULONGLONG Deadline = GetTickCount64() + Timeout; HANDLE Event = CreateEventW(NULL, FALSE, FALSE, NULL); if (!Event) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to create event"); + return LOG_LAST_ERROR(L"Failed to create event"); for (;;) { Result = RegNotifyChangeKeyValue(Key, FALSE, REG_NOTIFY_CHANGE_LAST_SET, Event, TRUE); if (Result != ERROR_SUCCESS) { - WINTUN_LOGGER_ERROR(L"Failed to setup notification", Result); + LOG_ERROR(L"Failed to setup notification", Result); break; } Result = RegistryQueryDWORD(Key, Name, Value); @@ -427,7 +427,7 @@ RegistryQueryDWORDWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD T TimeLeft = 0; if (WaitForSingleObject(Event, (DWORD)TimeLeft) != WAIT_OBJECT_0) { - WINTUN_LOGGER(WINTUN_LOG_ERR, "Timeout waiting"); + LOG(WINTUN_LOG_ERR, "Timeout waiting"); break; } } diff --git a/api/resource.c b/api/resource.c index 4c5be64..b59911e 100644 --- a/api/resource.c +++ b/api/resource.c @@ -21,17 +21,17 @@ ResourceGetAddress(_In_z_ const WCHAR *ResourceName, _Out_ const VOID **Address, { HRSRC FoundResource = FindResourceW(ResourceModule, ResourceName, RT_RCDATA); if (!FoundResource) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to find resource"); + return LOG_LAST_ERROR(L"Failed to find resource"); *Size = SizeofResource(ResourceModule, FoundResource); if (!*Size) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to size resource"); + return LOG_LAST_ERROR(L"Failed to size resource"); HGLOBAL LoadedResource = LoadResource(ResourceModule, FoundResource); if (!LoadedResource) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to load resource"); + return LOG_LAST_ERROR(L"Failed to load resource"); *Address = LockResource(LoadedResource); if (!*Address) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to lock resource"); + LOG(WINTUN_LOG_ERR, L"Failed to lock resource"); return ERROR_LOCK_FAILED; } return ERROR_SUCCESS; @@ -58,7 +58,7 @@ ResourceCopyToFile( DWORD SizeResource; DWORD Result = ResourceGetAddress(ResourceName, &LockedResource, &SizeResource); if (Result != ERROR_SUCCESS) - return WINTUN_LOGGER_ERROR("Failed to locate resource", Result); + return LOG_ERROR("Failed to locate resource", Result); HANDLE DestinationHandle = CreateFileW( DestinationPath, GENERIC_WRITE, @@ -68,13 +68,13 @@ ResourceCopyToFile( FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY, NULL); if (DestinationHandle == INVALID_HANDLE_VALUE) - return WINTUN_LOGGER_LAST_ERROR(L"Failed to create file"); + return LOG_LAST_ERROR(L"Failed to create file"); DWORD BytesWritten; if (!WriteFile(DestinationHandle, LockedResource, SizeResource, &BytesWritten, NULL)) - Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to write file"); + Result = LOG_LAST_ERROR(L"Failed to write file"); if (BytesWritten != SizeResource) { - WINTUN_LOGGER(WINTUN_LOG_ERR, L"Incomplete write"); + LOG(WINTUN_LOG_ERR, L"Incomplete write"); Result = Result != ERROR_SUCCESS ? Result : ERROR_WRITE_FAULT; } CloseHandle(DestinationHandle);