2020-10-13 19:40:52 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
2021-01-30 16:45:26 +01:00
|
|
|
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
|
2020-10-13 19:40:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-17 15:11:34 +02:00
|
|
|
#include "wintun.h"
|
2021-07-28 13:50:40 +02:00
|
|
|
#include "main.h"
|
2021-02-02 13:12:45 +01:00
|
|
|
#include "registry.h"
|
2020-10-31 11:55:26 +01:00
|
|
|
#include <Windows.h>
|
2021-02-02 13:12:45 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <wchar.h>
|
2020-10-13 19:40:52 +02:00
|
|
|
|
2020-11-04 15:45:27 +01:00
|
|
|
extern WINTUN_LOGGER_CALLBACK Logger;
|
2020-10-13 19:40:52 +02:00
|
|
|
|
2020-10-17 15:11:34 +02:00
|
|
|
/**
|
|
|
|
* @copydoc WINTUN_SET_LOGGER_FUNC
|
|
|
|
*/
|
|
|
|
void WINAPI
|
2020-11-04 15:45:27 +01:00
|
|
|
WintunSetLogger(_In_ WINTUN_LOGGER_CALLBACK NewLogger);
|
2020-10-13 19:40:52 +02:00
|
|
|
|
2020-11-04 12:24:46 +01:00
|
|
|
_Post_equals_last_error_ DWORD
|
|
|
|
LoggerLog(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *Function, _In_z_ const WCHAR *LogLine);
|
2020-11-03 12:29:34 +01:00
|
|
|
|
2020-10-13 19:40:52 +02:00
|
|
|
_Post_equals_last_error_ DWORD
|
2021-02-02 13:12:45 +01:00
|
|
|
LoggerLogV(
|
|
|
|
_In_ WINTUN_LOGGER_LEVEL Level,
|
|
|
|
_In_z_ const WCHAR *Function,
|
|
|
|
_In_z_ _Printf_format_string_ const WCHAR *Format,
|
|
|
|
_In_ va_list Args);
|
2020-10-13 19:40:52 +02:00
|
|
|
|
2020-11-01 03:19:21 +01:00
|
|
|
static inline _Post_equals_last_error_ DWORD
|
2021-02-02 13:12:45 +01:00
|
|
|
LoggerLogFmt(
|
|
|
|
_In_ WINTUN_LOGGER_LEVEL Level,
|
|
|
|
_In_z_ const WCHAR *Function,
|
|
|
|
_In_z_ _Printf_format_string_ const WCHAR *Format,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list Args;
|
|
|
|
va_start(Args, Format);
|
|
|
|
DWORD LastError = LoggerLogV(Level, Function, Format, Args);
|
|
|
|
va_end(Args);
|
|
|
|
return LastError;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Post_equals_last_error_ DWORD
|
|
|
|
LoggerError(_In_ DWORD Error, _In_z_ const WCHAR *Function, _In_z_ const WCHAR *Prefix);
|
|
|
|
|
|
|
|
_Post_equals_last_error_ DWORD
|
|
|
|
LoggerErrorV(
|
|
|
|
_In_ DWORD Error,
|
|
|
|
_In_z_ const WCHAR *Function,
|
|
|
|
_In_z_ _Printf_format_string_ const WCHAR *Format,
|
|
|
|
_In_ va_list Args);
|
|
|
|
|
|
|
|
static inline _Post_equals_last_error_ DWORD
|
|
|
|
LoggerErrorFmt(_In_ DWORD Error, _In_z_ const WCHAR *Function, _In_z_ _Printf_format_string_ const WCHAR *Format, ...)
|
|
|
|
{
|
|
|
|
va_list Args;
|
|
|
|
va_start(Args, Format);
|
|
|
|
DWORD LastError = LoggerErrorV(Error, Function, Format, Args);
|
|
|
|
va_end(Args);
|
|
|
|
return LastError;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline _Post_equals_last_error_ DWORD
|
|
|
|
LoggerLastErrorV(_In_z_ const WCHAR *Function, _In_z_ _Printf_format_string_ const WCHAR *Format, _In_ va_list Args)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError = GetLastError();
|
2021-02-02 13:12:45 +01:00
|
|
|
LoggerErrorV(LastError, Function, Format, Args);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return LastError;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
|
|
|
|
2021-02-02 13:12:45 +01:00
|
|
|
static inline _Post_equals_last_error_ DWORD
|
|
|
|
LoggerLastErrorFmt(_In_z_ const WCHAR *Function, _In_z_ _Printf_format_string_ const WCHAR *Format, ...)
|
|
|
|
{
|
|
|
|
va_list Args;
|
|
|
|
va_start(Args, Format);
|
|
|
|
DWORD LastError = LoggerLastErrorV(Function, Format, Args);
|
|
|
|
va_end(Args);
|
|
|
|
return LastError;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
LoggerGetRegistryKeyPath(_In_ HKEY Key, _Out_cap_c_(MAX_REG_PATH) WCHAR *Path);
|
|
|
|
|
2020-11-03 11:42:48 +01:00
|
|
|
#define __L(x) L##x
|
|
|
|
#define _L(x) __L(x)
|
2021-02-02 13:12:45 +01:00
|
|
|
#define LOG(lvl, msg, ...) (LoggerLogFmt((lvl), _L(__FUNCTION__), msg, __VA_ARGS__))
|
|
|
|
#define LOG_ERROR(err, msg, ...) (LoggerErrorFmt((err), _L(__FUNCTION__), msg, __VA_ARGS__))
|
|
|
|
#define LOG_LAST_ERROR(msg, ...) (LoggerLastErrorFmt(_L(__FUNCTION__), msg, __VA_ARGS__))
|
2020-11-04 12:55:25 +01:00
|
|
|
|
|
|
|
#define RET_ERROR(Ret, Error) ((Error) == ERROR_SUCCESS ? (Ret) : (SetLastError(Error), 0))
|
|
|
|
|
|
|
|
static inline _Return_type_success_(return != NULL) _Ret_maybenull_
|
|
|
|
_Post_writable_byte_size_(Size) void *LoggerAlloc(_In_z_ const WCHAR *Function, _In_ DWORD Flags, _In_ SIZE_T Size)
|
|
|
|
{
|
|
|
|
void *Data = HeapAlloc(ModuleHeap, Flags, Size);
|
|
|
|
if (!Data)
|
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LoggerLogFmt(WINTUN_LOG_ERR, Function, L"Out of memory (flags: 0x%x, requested size: 0x%zx)", Flags, Size);
|
2020-11-04 12:55:25 +01:00
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
}
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
#define Alloc(Size) LoggerAlloc(_L(__FUNCTION__), 0, Size)
|
|
|
|
#define Zalloc(Size) LoggerAlloc(_L(__FUNCTION__), HEAP_ZERO_MEMORY, Size)
|
2021-02-02 13:12:45 +01:00
|
|
|
|
2020-11-04 12:55:25 +01:00
|
|
|
static inline void
|
|
|
|
Free(void *Ptr)
|
|
|
|
{
|
|
|
|
if (!Ptr)
|
|
|
|
return;
|
|
|
|
DWORD LastError = GetLastError();
|
|
|
|
HeapFree(ModuleHeap, 0, Ptr);
|
|
|
|
SetLastError(LastError);
|
|
|
|
}
|