OpenVPN
Functions | Variables
main.c File Reference
#include "tap.h"
#include "error.h"
#include <objbase.h>
#include <setupapi.h>
#include <stdio.h>
#include <wchar.h>
Include dependency graph for main.c:

Go to the source code of this file.

Functions

static void usage (void)
 Print the help message.
 
static struct tap_adapter_nodefind_adapter_by_name (LPCWSTR name, struct tap_adapter_node *adapter_list)
 Locate an adapter node by its friendly name within the enumerated list.
 
static BOOL registry_name_exists (LPCWSTR name)
 Check whether the registry still reserves a given network-connection name.
 
static BOOL tap_name_in_use (LPCWSTR name, struct tap_adapter_node *adapter_list)
 Determine whether a friendly name is currently in use by an adapter or reserved in the registry.
 
BOOL tap_is_valid_adapter_name (LPCWSTR name)
 Check whether a proposed adapter name satisfies Windows connection-name rules.
 
static LPWSTR tap_resolve_adapter_name (LPCWSTR requested_name, LPCWSTR hwid, struct tap_adapter_node *adapter_list)
 Resolve the adapter name we should apply:
 
static int command_create (int argc, LPCWSTR argv[], BOOL *bRebootRequired)
 
static int command_list (int argc, LPCWSTR argv[])
 
static int command_delete (int argc, LPCWSTR argv[], BOOL *bRebootRequired)
 
int __cdecl wmain (int argc, LPCWSTR argv[])
 Program entry point.
 
bool dont_mute (unsigned int flags)
 Check muting filter.
 
void x_msg_va (const unsigned int flags, const char *format, va_list arglist)
 

Variables

const WCHAR title_string []
 
static const WCHAR usage_message []
 
static const WCHAR usage_message_create []
 
static const WCHAR usage_message_list []
 
static const WCHAR usage_message_delete []
 

Function Documentation

◆ command_create()

static int command_create ( int  argc,
LPCWSTR  argv[],
BOOL *  bRebootRequired 
)
static

◆ command_delete()

static int command_delete ( int  argc,
LPCWSTR  argv[],
BOOL *  bRebootRequired 
)
static

Definition at line 556 of file main.c.

References tap_adapter_node::pNext, tap_delete_adapter(), tap_free_adapter_list(), and tap_list_adapters().

Referenced by wmain().

◆ command_list()

static int command_list ( int  argc,
LPCWSTR  argv[] 
)
static

Definition at line 505 of file main.c.

References _L, tap_adapter_node::pNext, tap_free_adapter_list(), and tap_list_adapters().

Referenced by wmain().

◆ dont_mute()

bool dont_mute ( unsigned int  flags)

Check muting filter.

Definition at line 689 of file main.c.

◆ find_adapter_by_name()

static struct tap_adapter_node * find_adapter_by_name ( LPCWSTR  name,
struct tap_adapter_node adapter_list 
)
static

Locate an adapter node by its friendly name within the enumerated list.

Parameters
nameFriendly name to search for. Comparison is case-insensitive.
adapter_listHead of the adapter list returned by tap_list_adapters().
Returns
Pointer to the matching node, or NULL when not found.

Definition at line 136 of file main.c.

References tap_adapter_node::pNext.

Referenced by tap_name_in_use(), and tap_resolve_adapter_name().

◆ registry_name_exists()

static BOOL registry_name_exists ( LPCWSTR  name)
static

Check whether the registry still reserves a given network-connection name.

Windows keeps friendly names under \HKLM\SYSTEM\CurrentControlSet\Control\Network\{NETCLASS}\{GUID}\Connection\Name, even after an adapter is removed. netsh refuses to rename to any reserved name.

Parameters
nameFriendly name to test.
Returns
TRUE if the name exists in the registry, FALSE otherwise.

Definition at line 161 of file main.c.

Referenced by tap_name_in_use(), and tap_resolve_adapter_name().

◆ tap_is_valid_adapter_name()

BOOL tap_is_valid_adapter_name ( LPCWSTR  name)

Check whether a proposed adapter name satisfies Windows connection-name rules.

Tabs, control characters (except space), and the following characters are disallowed: \ / : * ? " < > | Names must also be non-empty and no longer than 255 characters.

Definition at line 261 of file main.c.

Referenced by tap_resolve_adapter_name().

◆ tap_name_in_use()

static BOOL tap_name_in_use ( LPCWSTR  name,
struct tap_adapter_node adapter_list 
)
static

Determine whether a friendly name is currently in use by an adapter or reserved in the registry.

Parameters
nameFriendly name to test.
adapter_listHead of the adapter list returned by tap_list_adapters().
Returns
TRUE when the name is taken/reserved, FALSE when available.

Definition at line 238 of file main.c.

References find_adapter_by_name(), and registry_name_exists().

Referenced by tap_resolve_adapter_name().

◆ tap_resolve_adapter_name()

static LPWSTR tap_resolve_adapter_name ( LPCWSTR  requested_name,
LPCWSTR  hwid,
struct tap_adapter_node adapter_list 
)
static

Resolve the adapter name we should apply:

  • For user-specified names, ensure they are unique both in the adapter list and in the registry. On conflict, an explanatory message is printed and NULL is returned.
  • For automatic naming, derive the base string from HWID and append the first available suffix recognised by Windows.
Parameters
requested_nameName provided via CLI or configuration (may be NULL/empty).
hwidHardware identifier of the adapter being created.
adapter_listExisting adapters enumerated via tap_list_adapters().
Returns
Newly allocated wide string containing the final name, or NULL on failure.

Definition at line 306 of file main.c.

References _L, find_adapter_by_name(), tap_adapter_node::guid, registry_name_exists(), tap_adapter_node::szName, tap_is_valid_adapter_name(), and tap_name_in_use().

Referenced by command_create().

◆ usage()

static void usage ( void  )
static

Print the help message.

Definition at line 122 of file main.c.

References title_string, and usage_message.

Referenced by wmain().

◆ wmain()

int __cdecl wmain ( int  argc,
LPCWSTR  argv[] 
)

◆ x_msg_va()

void x_msg_va ( const unsigned int  flags,
const char *  format,
va_list  arglist 
)

Definition at line 698 of file main.c.

References M_ERRNO.

Variable Documentation

◆ title_string

const WCHAR title_string[]
Initial value:
=
_L(PACKAGE_NAME) L" " _L(PACKAGE_VERSION)
#define _L(q)
Definition basic.h:38

Definition at line 40 of file main.c.

Referenced by usage(), and wmain().

◆ usage_message

const WCHAR usage_message[]
static
Initial value:
=
L"%ls\n"
L"\n"
L"Usage:\n"
L"\n"
L"tapctl <command> [<command specific options>]\n"
L"\n"
L"Commands:\n"
L"\n"
L"create Create a new VPN network adapter\n"
L"list List VPN network adapters\n"
L"delete Delete specified VPN network adapter\n"
L"help Display this text\n"
L"\n"
L"Hint: Use \"tapctl help <command>\" to display help for particular command.\n"

Definition at line 44 of file main.c.

Referenced by usage().

◆ usage_message_create

const WCHAR usage_message_create[]
static
Initial value:
=
L"%ls\n"
L"\n"
L"Creates a new VPN network adapter\n"
L"\n"
L"Usage:\n"
L"\n"
L"tapctl create [<options>]\n"
L"\n"
L"Options:\n"
L"\n"
L"--name <name> Set VPN network adapter name. Should the adapter with given \n"
L" name already exist, an error is returned. If this option is not \n"
L" specified, an OpenVPN-specific default name is chosen. \n"
L" Note: This name can also be specified as OpenVPN's --dev-node \n"
L" option. \n"
L"--hwid <hwid> Adapter hardware ID. Default value is ovpn-dco, which uses \n"
L" the OpenVPN Data Channel Offload driver. To work with \n"
L" tap-windows6 driver, specify root\\tap0901 or tap0901. \n"
L"\n"
L"Output:\n"
L"\n"
L"This command prints newly created VPN network adapter's GUID, name and \n"
L"hardware ID to stdout. \n"

Definition at line 61 of file main.c.

Referenced by wmain().

◆ usage_message_delete

const WCHAR usage_message_delete[]
static
Initial value:
=
L"%ls\n"
L"\n"
L"Deletes the specified VPN network adapter\n"
L"\n"
L"Usage:\n"
L"\n"
L"tapctl delete <adapter GUID | adapter name>\n"

Definition at line 106 of file main.c.

Referenced by wmain().

◆ usage_message_list

const WCHAR usage_message_list[]
static
Initial value:
=
L"%ls\n"
L"\n"
L"Lists VPN network adapters\n"
L"\n"
L"Usage:\n"
L"\n"
L"tapctl list\n"
L"\n"
L"Options:\n"
L"\n"
L"--hwid <hwid> Adapter hardware ID. By default, root\\tap0901, tap0901 and \n"
L" ovpn-dco adapters are listed. Use this switch to limit the list.\n"
L"\n"
L"Output:\n"
L"\n"
L"This command prints VPN network adapter GUID, name and hardware ID to stdout. \n"

Definition at line 87 of file main.c.

Referenced by wmain().