OpenVPN
Macros | Functions | Variables
tap.c File Reference
#include "tap.h"
#include "error.h"
#include <windows.h>
#include <cfgmgr32.h>
#include <objbase.h>
#include <setupapi.h>
#include <tchar.h>
Include dependency graph for tap.c:

Go to the source code of this file.

Macros

#define INTERFACE_REGKEY_PATH_MAX   (_countof(TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\")) - 1 + 38 + _countof(TEXT("\\")) - 1 + 38 + _countof(TEXT("\\Connection")))
 

Functions

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 get_reg_string (_In_ HKEY hKey, _In_ LPCTSTR szName, _Out_ LPTSTR *pszValue)
 Reads string value from registry key. More...
 
static DWORD get_net_interface_guid (_In_ HDEVINFO hDeviceInfoSet, _In_ PSP_DEVINFO_DATA pDeviceInfoData, _In_ int iNumAttempts, _Out_ LPGUID pguidInterface)
 Returns network interface 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...
 
static size_t _tcszlen (_In_ LPCTSTR str)
 Returns length of list of strings. More...
 
DWORD tap_create_interface (_In_opt_ HWND hwndParent, _In_opt_ LPCTSTR szDeviceDescription, _Inout_ LPBOOL pbRebootRequired, _Out_ LPGUID pguidInterface)
 Creates a TUN/TAP interface. More...
 
DWORD tap_delete_interface (_In_opt_ HWND hwndParent, _In_ LPCGUID pguidInterface, _Inout_ LPBOOL pbRebootRequired)
 Deletes an interface. More...
 
DWORD tap_set_interface_name (_In_ LPCGUID pguidInterface, _In_ LPCTSTR szName)
 Sets interface name. More...
 
DWORD tap_list_interfaces (_In_opt_ HWND hwndParent, _Out_ struct tap_interface_node **ppInterface, _In_ BOOL bAll)
 Creates a list of available network interfaces. More...
 
void tap_free_interface_list (_In_ struct tap_interface_node *pInterfaceList)
 Frees a list of network interfaces. More...
 

Variables

static const GUID GUID_DEVCLASS_NET = { 0x4d36e972L, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } }
 
static const TCHAR szzHardwareIDs [] = TEXT("root\\") TEXT(TAP_WIN_COMPONENT_ID) TEXT("\0")
 
static const TCHAR szInterfaceRegKeyPathTemplate [] = TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\%") TEXT(PRIsLPOLESTR) TEXT("\\%") TEXT(PRIsLPOLESTR) TEXT("\\Connection")
 

Macro Definition Documentation

◆ INTERFACE_REGKEY_PATH_MAX

#define INTERFACE_REGKEY_PATH_MAX   (_countof(TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\")) - 1 + 38 + _countof(TEXT("\\")) - 1 + 38 + _countof(TEXT("\\Connection")))

Definition at line 47 of file tap.c.

Referenced by tap_list_interfaces(), and tap_set_interface_name().

Function Documentation

◆ _tcszlen()

static size_t _tcszlen ( _In_ LPCTSTR  str)
inlinestatic

Returns length of list of strings.

Parameters
strPointer to a list of strings terminated by an empty string.
Returns
Number of characters not counting the final zero terminator

Definition at line 437 of file tap.c.

Referenced by tap_list_interfaces().

◆ check_reboot()

static DWORD check_reboot ( _In_ HDEVINFO  hDeviceInfoSet,
_In_ PSP_DEVINFO_DATA  pDeviceInfoData,
_Inout_ LPBOOL  pbRebootRequired 
)
static

Checks device install parameters if a system reboot is required.

Parameters
hDeviceInfoSetA handle to a device information set that contains a device information element that represents the device for which to
pDeviceInfoDataA pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet.
pbRebootRequiredA pointer to a BOOL flag. If the interface installation 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 interface installations.
Returns
ERROR_SUCCESS on success; Win32 error code otherwise

Definition at line 67 of file tap.c.

References M_ERRNO, M_NONFATAL, and msg.

Referenced by tap_create_interface(), and tap_delete_interface().

◆ get_device_reg_property()

static DWORD get_device_reg_property ( _In_ HDEVINFO  hDeviceInfoSet,
_In_ PSP_DEVINFO_DATA  pDeviceInfoData,
_In_ DWORD  dwProperty,
_Out_opt_ LPDWORD  pdwPropertyRegDataType,
_Out_ LPVOID *  ppData 
)
static

Returns a specified Plug and Play device property.

Parameters
hDeviceInfoSetA handle to a device information set that contains a device information element that represents the device for which to retrieve a Plug and Play property.
pDeviceInfoDataA pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet.
dwPropertySpecifies the property to be retrieved. See https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967.aspx

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.

Parameters
ppDataA pointer to pointer to data that receives the device property. The data must be released with free() after use.
Returns
ERROR_SUCCESS on success; Win32 error code otherwise

Definition at line 352 of file tap.c.

References M_ERRNO, M_FATAL, M_NONFATAL, malloc, and msg.

Referenced by tap_list_interfaces().

◆ get_net_interface_guid()

static DWORD get_net_interface_guid ( _In_ HDEVINFO  hDeviceInfoSet,
_In_ PSP_DEVINFO_DATA  pDeviceInfoData,
_In_ int  iNumAttempts,
_Out_ LPGUID  pguidInterface 
)
static

Returns network interface ID.

Parameters
hDeviceInfoSetA handle to a device information set that contains a device information element that represents the device for which to
pDeviceInfoDataA pointer to an SP_DEVINFO_DATA structure that specifies the device information element in hDeviceInfoSet.
iNumAttemptsAfter 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.
pguidInterfaceA pointer to GUID that receives network interface ID.
Returns
ERROR_SUCCESS on success; Win32 error code otherwise

Definition at line 262 of file tap.c.

References free, get_reg_string(), M_ERRNO, M_NONFATAL, and msg.

Referenced by tap_create_interface(), tap_delete_interface(), and tap_list_interfaces().

◆ get_reg_string()

static DWORD get_reg_string ( _In_ HKEY  hKey,
_In_ LPCTSTR  szName,
_Out_ LPTSTR *  pszValue 
)
static

Reads string value from registry key.

Parameters
hKeyHandle of the registry key to read from. Must be opened with read access.
szNameName of the value to read.
pszValuePointer 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.
Returns
ERROR_SUCCESS on success; Win32 error code otherwise

Definition at line 112 of file tap.c.

References free, M_ERRNO, M_FATAL, M_NONFATAL, malloc, msg, and PRIsLPTSTR.

Referenced by get_net_interface_guid(), and tap_list_interfaces().

◆ tap_create_interface()

DWORD tap_create_interface ( _In_opt_ HWND  hwndParent,
_In_opt_ LPCTSTR  szDeviceDescription,
_Inout_ LPBOOL  pbRebootRequired,
_Out_ LPGUID  pguidInterface 
)

Creates a TUN/TAP interface.

Parameters
hwndParentA handle to the top-level window to use for any user interface 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.
szDeviceDescriptionA pointer to a NULL-terminated string that supplies the text description of the device. This pointer is optional and can be NULL.
pbRebootRequiredA pointer to a BOOL flag. If the interface installation 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 interface installations.
pguidInterfaceA pointer to GUID that receives network interface ID.
Returns
ERROR_SUCCESS on success; Win32 error code otherwise

Definition at line 448 of file tap.c.

References check_reboot(), free, get_net_interface_guid(), GUID_DEVCLASS_NET, M_ERRNO, M_FATAL, M_NONFATAL, M_WARN, malloc, msg, PRIsLPTSTR, and szzHardwareIDs.

Referenced by _tmain(), and msica_op_tap_interface_create_exec().

◆ tap_delete_interface()

DWORD tap_delete_interface ( _In_opt_ HWND  hwndParent,
_In_ LPCGUID  pguidInterface,
_Inout_ LPBOOL  pbRebootRequired 
)

Deletes an interface.

Parameters
hwndParentA handle to the top-level window to use for any user interface 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.
pguidInterfaceA pointer to GUID that contains network interface ID.
pbRebootRequiredA pointer to a BOOL flag. If the interface installation 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 interface installations.
Returns
ERROR_SUCCESS on success; Win32 error code otherwise

Definition at line 753 of file tap.c.

References check_reboot(), get_net_interface_guid(), GUID_DEVCLASS_NET, M_ERRNO, M_NONFATAL, M_WARN, msg, and PRIsLPOLESTR.

Referenced by _tmain(), msica_op_tap_interface_create_exec(), and msica_op_tap_interface_delete().

◆ tap_free_interface_list()

void tap_free_interface_list ( _In_ struct tap_interface_node pInterfaceList)

Frees a list of network interfaces.

Parameters
pInterfaceListA pointer to the first interface in the list to free.

Definition at line 1163 of file tap.c.

References free, and tap_interface_node::pNext.

Referenced by _tmain(), FindTAPInterfaces(), msica_op_tap_interface_create_exec(), msica_op_tap_interface_delete_by_guid_exec(), msica_op_tap_interface_delete_by_name_exec(), and msica_op_tap_interface_set_name_exec().

◆ tap_list_interfaces()

DWORD tap_list_interfaces ( _In_opt_ HWND  hwndParent,
_Out_ struct tap_interface_node **  ppInterfaceList,
_In_ BOOL  bAll 
)

Creates a list of available network interfaces.

Parameters
hwndParentA handle to the top-level window to use for any user interface 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.
ppInterfaceListA pointer to the list to receive pointer to the first interface in the list. After the list is no longer required, free it using tap_free_interface_list().
bAllWhen TRUE, all network interfaces found are added to the list. When FALSE, only TUN/TAP interfaces found are added.
Returns
ERROR_SUCCESS on success; Win32 error code otherwise

Definition at line 954 of file tap.c.

References _tcszlen(), free, get_device_reg_property(), get_net_interface_guid(), get_reg_string(), tap_interface_node::guid, GUID_DEVCLASS_NET, INTERFACE_REGKEY_PATH_MAX, M_ERRNO, M_FATAL, M_NONFATAL, M_WARN, malloc, msg, tap_interface_node::pNext, PRIsLPOLESTR, PRIsLPTSTR, szInterfaceRegKeyPathTemplate, szName, tap_interface_node::szName, szzHardwareIDs, and tap_interface_node::szzHardwareIDs.

Referenced by _tmain(), FindTAPInterfaces(), msica_op_tap_interface_create_exec(), msica_op_tap_interface_delete_by_guid_exec(), msica_op_tap_interface_delete_by_name_exec(), and msica_op_tap_interface_set_name_exec().

◆ tap_set_interface_name()

DWORD tap_set_interface_name ( _In_ LPCGUID  pguidInterface,
_In_ LPCTSTR  szName 
)

Sets interface name.

Parameters
pguidInterfaceA pointer to GUID that contains network interface ID.
szNameNew interface name - must be unique
Returns
ERROR_SUCCESS on success; Win32 error code otherwise

Definition at line 878 of file tap.c.

References GUID_DEVCLASS_NET, INTERFACE_REGKEY_PATH_MAX, M_ERRNO, M_NONFATAL, msg, PRIsLPTSTR, and szInterfaceRegKeyPathTemplate.

Referenced by _tmain(), msica_op_tap_interface_create_exec(), and msica_op_tap_interface_set_name_exec().

Variable Documentation

◆ GUID_DEVCLASS_NET

const GUID GUID_DEVCLASS_NET = { 0x4d36e972L, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } }
static

◆ szInterfaceRegKeyPathTemplate

const TCHAR szInterfaceRegKeyPathTemplate[] = TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\%") TEXT(PRIsLPOLESTR) TEXT("\\%") TEXT(PRIsLPOLESTR) TEXT("\\Connection")
static

Definition at line 46 of file tap.c.

Referenced by tap_list_interfaces(), and tap_set_interface_name().

◆ szzHardwareIDs

const TCHAR szzHardwareIDs[] = TEXT("root\\") TEXT(TAP_WIN_COMPONENT_ID) TEXT("\0")
static

Definition at line 44 of file tap.c.

Referenced by tap_create_interface(), and tap_list_interfaces().