OpenVPN
|
#include "tap.h"
#include "error.h"
#include <windows.h>
#include <cfgmgr32.h>
#include <objbase.h>
#include <setupapi.h>
#include <stdio.h>
#include <tchar.h>
#include <newdev.h>
Go to the source code of this file.
Macros | |
#define | ADAPTER_REGKEY_PATH_MAX (_countof(TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\")) - 1 + 38 + _countof(TEXT("\\")) - 1 + 38 + _countof(TEXT("\\Connection"))) |
Typedefs | |
typedef DWORD(* | devop_func_t) (_In_ HDEVINFO hDeviceInfoSet, _In_ PSP_DEVINFO_DATA pDeviceInfoData, _Inout_ LPBOOL pbRebootRequired) |
Function that performs a specific task on a device. More... | |
Functions | |
static void * | find_function (const WCHAR *libname, const char *funcname, HMODULE *m) |
Dynamically load a library and find a function in it. More... | |
static size_t | _tcszlen (_In_z_ LPCTSTR szz) |
Returns length of string of strings. More... | |
static LPCTSTR | _tcszistr (_In_z_ LPCTSTR szzHay, _In_z_ LPCTSTR szNeedle) |
Checks if string is contained in the string of strings. More... | |
static DWORD | check_reboot (_In_ HDEVINFO hDeviceInfoSet, _In_ PSP_DEVINFO_DATA pDeviceInfoData, _Inout_ LPBOOL pbRebootRequired) |
Checks device install parameters if a system reboot is required. More... | |
static DWORD | delete_device (_In_ HDEVINFO hDeviceInfoSet, _In_ PSP_DEVINFO_DATA pDeviceInfoData, _Inout_ LPBOOL pbRebootRequired) |
Deletes the device. More... | |
static DWORD | change_device_state (_In_ HDEVINFO hDeviceInfoSet, _In_ PSP_DEVINFO_DATA pDeviceInfoData, _In_ BOOL bEnable, _Inout_ LPBOOL pbRebootRequired) |
Changes the device state. More... | |
static DWORD | enable_device (_In_ HDEVINFO hDeviceInfoSet, _In_ PSP_DEVINFO_DATA pDeviceInfoData, _Inout_ LPBOOL pbRebootRequired) |
Enables the device. More... | |
static DWORD | disable_device (_In_ HDEVINFO hDeviceInfoSet, _In_ PSP_DEVINFO_DATA pDeviceInfoData, _Inout_ LPBOOL pbRebootRequired) |
Disables the device. More... | |
static DWORD | get_reg_string (_In_ HKEY hKey, _In_ LPCTSTR szName, _Out_ LPTSTR *pszValue) |
Reads string value from registry key. More... | |
static DWORD | get_net_adapter_guid (_In_ HDEVINFO hDeviceInfoSet, _In_ PSP_DEVINFO_DATA pDeviceInfoData, _In_ int iNumAttempts, _Out_ LPGUID pguidAdapter) |
Returns network adapter ID. More... | |
static DWORD | get_device_reg_property (_In_ HDEVINFO hDeviceInfoSet, _In_ PSP_DEVINFO_DATA pDeviceInfoData, _In_ DWORD dwProperty, _Out_opt_ LPDWORD pdwPropertyRegDataType, _Out_ LPVOID *ppData) |
Returns a specified Plug and Play device property. More... | |
DWORD | tap_create_adapter (_In_opt_ HWND hwndParent, _In_opt_ LPCTSTR szDeviceDescription, _In_ LPCTSTR szHwId, _Inout_ LPBOOL pbRebootRequired, _Out_ LPGUID pguidAdapter) |
Creates a TUN/TAP adapter. More... | |
static DWORD | execute_on_first_adapter (_In_opt_ HWND hwndParent, _In_ LPCGUID pguidAdapter, _In_ devop_func_t funcOperation, _Inout_ LPBOOL pbRebootRequired) |
Performs a given task on an adapter. More... | |
DWORD | tap_delete_adapter (_In_opt_ HWND hwndParent, _In_ LPCGUID pguidAdapter, _Inout_ LPBOOL pbRebootRequired) |
Deletes an adapter. More... | |
DWORD | tap_enable_adapter (_In_opt_ HWND hwndParent, _In_ LPCGUID pguidAdapter, _In_ BOOL bEnable, _Inout_ LPBOOL pbRebootRequired) |
Enables or disables an adapter. More... | |
static DWORD | ExecCommand (const WCHAR *cmdline) |
DWORD | tap_set_adapter_name (_In_ LPCGUID pguidAdapter, _In_ LPCTSTR szName, _In_ BOOL bSilent) |
Sets adapter name. More... | |
DWORD | tap_list_adapters (_In_opt_ HWND hwndParent, _In_opt_ LPCTSTR szzHwIDs, _Out_ struct tap_adapter_node **ppAdapter) |
Creates a list of existing network adapters. More... | |
void | tap_free_adapter_list (_In_ struct tap_adapter_node *pAdapterList) |
Frees a list of network adapters. More... | |
Variables | |
const static GUID | GUID_DEVCLASS_NET = { 0x4d36e972L, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } } |
const static TCHAR | szAdapterRegKeyPathTemplate [] = TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\%") TEXT(PRIsLPOLESTR) TEXT("\\%") TEXT(PRIsLPOLESTR) TEXT("\\Connection") |
#define ADAPTER_REGKEY_PATH_MAX (_countof(TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\")) - 1 + 38 + _countof(TEXT("\\")) - 1 + 38 + _countof(TEXT("\\Connection"))) |
typedef DWORD(* devop_func_t) (_In_ HDEVINFO hDeviceInfoSet, _In_ PSP_DEVINFO_DATA pDeviceInfoData, _Inout_ LPBOOL pbRebootRequired) |
Function that performs a specific task on a device.
hDeviceInfoSet | A handle to a device information set that contains a device information element that represents the device. |
pDeviceInfoData | A pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet. |
pbRebootRequired | A pointer to a BOOL flag. If the device requires a system restart, this flag is set to TRUE. Otherwise, the flag is left unmodified. This allows the flag to be globally initialized to FALSE and reused for multiple adapter manipulations. |
Checks if string is contained in the string of strings.
Comparison is made case-insensitive.
szzHay | Pointer to a string of strings (terminated by an empty string) we are looking in |
szNeedle | The string we are searching for |
Definition at line 128 of file tap.c.
Referenced by tap_list_adapters().
|
inlinestatic |
Returns length of string of strings.
szz | Pointer to a string of strings (terminated by an empty string) |
Definition at line 107 of file tap.c.
Referenced by tap_list_adapters().
|
static |
Changes the device state.
hDeviceInfoSet | A handle to a device information set that contains a device information element that represents the device. |
pDeviceInfoData | A pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet. |
bEnable | TRUE to enable the device; FALSE to disable. |
pbRebootRequired | A pointer to a BOOL flag. If the device requires a system restart, this flag is set to TRUE. Otherwise, the flag is left unmodified. This allows the flag to be globally initialized to FALSE and reused for multiple adapter manipulations. |
Definition at line 292 of file tap.c.
References check_reboot(), M_ERRNO, M_NONFATAL, and msg.
Referenced by disable_device(), and enable_device().
|
static |
Checks device install parameters if a system reboot is required.
hDeviceInfoSet | A handle to a device information set that contains a device information element that represents the device. |
pDeviceInfoData | A pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet. |
pbRebootRequired | A pointer to a BOOL flag. If the device requires a system restart, this flag is set to TRUE. Otherwise, the flag is left unmodified. This allows the flag to be globally initialized to FALSE and reused for multiple adapter manipulations. |
Definition at line 181 of file tap.c.
References M_ERRNO, M_NONFATAL, and msg.
Referenced by change_device_state(), delete_device(), and tap_create_adapter().
|
static |
Deletes the device.
hDeviceInfoSet | A handle to a device information set that contains a device information element that represents the device. |
pDeviceInfoData | A pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet. |
pbRebootRequired | A pointer to a BOOL flag. If the device requires a system restart, this flag is set to TRUE. Otherwise, the flag is left unmodified. This allows the flag to be globally initialized to FALSE and reused for multiple adapter manipulations. |
Definition at line 228 of file tap.c.
References check_reboot(), M_ERRNO, M_NONFATAL, and msg.
Referenced by tap_delete_adapter().
|
static |
Disables the device.
hDeviceInfoSet | A handle to a device information set that contains a device information element that represents the device. |
pDeviceInfoData | A pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet. |
pbRebootRequired | A pointer to a BOOL flag. If the device requires a system restart, this flag is set to TRUE. Otherwise, the flag is left unmodified. This allows the flag to be globally initialized to FALSE and reused for multiple adapter manipulations. |
Definition at line 382 of file tap.c.
References change_device_state().
Referenced by tap_enable_adapter().
|
static |
Enables the device.
hDeviceInfoSet | A handle to a device information set that contains a device information element that represents the device. |
pDeviceInfoData | A pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet. |
pbRebootRequired | A pointer to a BOOL flag. If the device requires a system restart, this flag is set to TRUE. Otherwise, the flag is left unmodified. This allows the flag to be globally initialized to FALSE and reused for multiple adapter manipulations. |
Definition at line 356 of file tap.c.
References change_device_state().
Referenced by tap_enable_adapter().
|
static |
Definition at line 1026 of file tap.c.
Referenced by tap_set_adapter_name().
|
static |
Performs a given task on an adapter.
hwndParent | A handle to the top-level window to use for any user adapter that is related to non-device-specific actions (such as a select-device dialog box that uses the global class driver list). This handle is optional and can be NULL. If a specific top-level window is not required, set hwndParent to NULL. |
pguidAdapter | A pointer to GUID that contains network adapter ID. |
funcOperation | A pointer for the function to perform specific task on the adapter. |
pbRebootRequired | A pointer to a BOOL flag. If the device requires a system restart, this flag is set to TRUE. Otherwise, the flag is left unmodified. This allows the flag to be globally initialized to FALSE and reused for multiple adapter manipulations. |
Definition at line 916 of file tap.c.
References get_net_adapter_guid(), GUID_DEVCLASS_NET, M_ERRNO, M_NONFATAL, M_WARN, msg, and PRIsLPOLESTR.
Referenced by tap_delete_adapter(), and tap_enable_adapter().
|
static |
Dynamically load a library and find a function in it.
libname | Name of the library to load |
funcname | Name of the function to find |
m | Pointer to a module. On return this is set to the the handle to the loaded library. The caller must free it by calling FreeLibrary() if not NULL. |
Definition at line 63 of file tap.c.
Referenced by tap_create_adapter().
|
static |
Returns a specified Plug and Play device property.
hDeviceInfoSet | A handle to a device information set that contains a device information element that represents the device. |
pDeviceInfoData | A pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet. |
dwProperty | Specifies the property to be retrieved. See https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967.aspx |
@pdwPropertyRegDataType A pointer to a variable that receives the data type of the property that is being retrieved. This is one of the standard registry data types. This parameter is optional and can be NULL.
ppData | A pointer to pointer to data that receives the device property. The data must be released with free() after use. |
Definition at line 645 of file tap.c.
References M_ERRNO, M_FATAL, M_NONFATAL, and msg.
Referenced by tap_list_adapters().
|
static |
Returns network adapter ID.
hDeviceInfoSet | A handle to a device information set that contains a device information element that represents the device. |
pDeviceInfoData | A pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet. |
iNumAttempts | After the device is created, it might take some time before the registry key is populated. This parameter specifies the number of attempts to read NetCfgInstanceId value from registry. A 1sec sleep is inserted between retry attempts. |
pguidAdapter | A pointer to GUID that receives network adapter ID. |
Definition at line 556 of file tap.c.
References get_reg_string(), M_ERRNO, M_NONFATAL, and msg.
Referenced by execute_on_first_adapter(), tap_create_adapter(), and tap_list_adapters().
Reads string value from registry key.
hKey | Handle of the registry key to read from. Must be opened with read access. |
szName | Name of the value to read. |
pszValue | Pointer to string to retrieve registry value. If the value type is REG_EXPAND_SZ the value is expanded using ExpandEnvironmentStrings(). The string must be released with free() after use. |
Definition at line 406 of file tap.c.
References M_ERRNO, M_FATAL, M_NONFATAL, msg, PRIsLPTSTR, and tap_adapter_node::szName.
Referenced by get_net_adapter_guid(), tap_list_adapters(), and tap_set_adapter_name().
DWORD tap_create_adapter | ( | _In_opt_ HWND | hwndParent, |
_In_opt_ LPCTSTR | szDeviceDescription, | ||
_In_ LPCTSTR | szHwId, | ||
_Inout_ LPBOOL | pbRebootRequired, | ||
_Out_ LPGUID | pguidAdapter | ||
) |
Creates a TUN/TAP adapter.
hwndParent | A handle to the top-level window to use for any user adapter that is related to non-device-specific actions (such as a select-device dialog box that uses the global class driver list). This handle is optional and can be NULL. If a specific top-level window is not required, set hwndParent to NULL. |
szDeviceDescription | A pointer to a NULL-terminated string that supplies the text description of the device. This pointer is optional and can be NULL. |
szHwId | A pointer to a NULL-terminated string that supplies the hardware id of the device (e.g. "root\\tap0901", "Wintun"). |
pbRebootRequired | A pointer to a BOOL flag. If the device requires a system restart, this flag is set to TRUE. Otherwise, the flag is left unmodified. This allows the flag to be globally initialized to FALSE and reused for multiple adapter manipulations. |
pguidAdapter | A pointer to GUID that receives network adapter ID. |
Definition at line 723 of file tap.c.
References check_reboot(), find_function(), get_net_adapter_guid(), GUID_DEVCLASS_NET, M_ERRNO, M_NONFATAL, and msg.
Referenced by _tmain(), and ProcessDeferredAction().
DWORD tap_delete_adapter | ( | _In_opt_ HWND | hwndParent, |
_In_ LPCGUID | pguidAdapter, | ||
_Inout_ LPBOOL | pbRebootRequired | ||
) |
Deletes an adapter.
hwndParent | A handle to the top-level window to use for any user adapter that is related to non-device-specific actions (such as a select-device dialog box that uses the global class driver list). This handle is optional and can be NULL. If a specific top-level window is not required, set hwndParent to NULL. |
pguidAdapter | A pointer to GUID that contains network adapter ID. |
pbRebootRequired | A pointer to a BOOL flag. If the device requires a system restart, this flag is set to TRUE. Otherwise, the flag is left unmodified. This allows the flag to be globally initialized to FALSE and reused for multiple adapter manipulations. |
Definition at line 1005 of file tap.c.
References delete_device(), and execute_on_first_adapter().
Referenced by _tmain(), and ProcessDeferredAction().
DWORD tap_enable_adapter | ( | _In_opt_ HWND | hwndParent, |
_In_ LPCGUID | pguidAdapter, | ||
_In_ BOOL | bEnable, | ||
_Inout_ LPBOOL | pbRebootRequired | ||
) |
Enables or disables an adapter.
hwndParent | A handle to the top-level window to use for any user adapter that is related to non-device-specific actions (such as a select-device dialog box that uses the global class driver list). This handle is optional and can be NULL. If a specific top-level window is not required, set hwndParent to NULL. |
pguidAdapter | A pointer to GUID that contains network adapter ID. |
bEnable | TRUE to enable; FALSE to disable |
pbRebootRequired | A pointer to a BOOL flag. If the device requires a system restart, this flag is set to TRUE. Otherwise, the flag is left unmodified. This allows the flag to be globally initialized to FALSE and reused for multiple adapter manipulations. |
Definition at line 1015 of file tap.c.
References disable_device(), enable_device(), and execute_on_first_adapter().
Referenced by ProcessDeferredAction().
void tap_free_adapter_list | ( | _In_ struct tap_adapter_node * | pAdapterList | ) |
Frees a list of network adapters.
pAdapterList | A pointer to the first adapter in the list to free. |
Definition at line 1355 of file tap.c.
References tap_adapter_node::pNext.
Referenced by _tmain(), find_adapters(), ProcessDeferredAction(), schedule_adapter_create(), and schedule_adapter_delete().
DWORD tap_list_adapters | ( | _In_opt_ HWND | hwndParent, |
_In_opt_ LPCTSTR | szzHwIDs, | ||
_Out_ struct tap_adapter_node ** | ppAdapterList | ||
) |
Creates a list of existing network adapters.
hwndParent | A handle to the top-level window to use for any user adapter that is related to non-device-specific actions (such as a select-device dialog box that uses the global class driver list). This handle is optional and can be NULL. If a specific top-level window is not required, set hwndParent to NULL. |
szzHwIDs | A string of strings that supplies the list of hardware IDs of the device. This pointer is optional and can be NULL. When NULL, all network adapters found are added to the list. |
ppAdapterList | A pointer to the list to receive pointer to the first adapter in the list. After the list is no longer required, free it using tap_free_adapter_list(). |
Definition at line 1146 of file tap.c.
References _tcszistr(), _tcszlen(), ADAPTER_REGKEY_PATH_MAX, get_device_reg_property(), get_net_adapter_guid(), get_reg_string(), tap_adapter_node::guid, GUID_DEVCLASS_NET, M_ERRNO, M_FATAL, M_NONFATAL, M_WARN, msg, tap_adapter_node::pNext, PRIsLPOLESTR, PRIsLPTSTR, szAdapterRegKeyPathTemplate, tap_adapter_node::szName, and tap_adapter_node::szzHardwareIDs.
Referenced by _tmain(), find_adapters(), ProcessDeferredAction(), schedule_adapter_create(), and schedule_adapter_delete().
Sets adapter name.
pguidAdapter | A pointer to GUID that contains network adapter ID. |
szName | New adapter name - must be unique |
bSilent | If true, MSI installer won't display message box and only print error to log. |
Definition at line 1063 of file tap.c.
References ADAPTER_REGKEY_PATH_MAX, ExecCommand(), get_reg_string(), GUID_DEVCLASS_NET, M_ERRNO, M_NONFATAL, M_WARN, msg, PRIsLPTSTR, szAdapterRegKeyPathTemplate, and tap_adapter_node::szName.
Referenced by _tmain(), and ProcessDeferredAction().
|
static |
Definition at line 44 of file tap.c.
Referenced by execute_on_first_adapter(), tap_create_adapter(), tap_list_adapters(), and tap_set_adapter_name().
|
static |
Definition at line 46 of file tap.c.
Referenced by tap_list_adapters(), and tap_set_adapter_name().