Skip to content

Commit

Permalink
dco-win: factor out getting dco version info own function
Browse files Browse the repository at this point in the history
Instead of having duplicated code to get dco version
for data_v3 checks and for version info, have it in
one place and store the version in dco context - the
latter is a preparation for dco v2 support (multi-peer).

Change-Id: I8e8ddd35bd3cc3334faf7f57118d1892512ae9f7
Signed-off-by: Lev Stipakov <[email protected]>
  • Loading branch information
lstipakov committed Sep 5, 2024
1 parent a9d38cd commit 76d42f1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 56 deletions.
107 changes: 51 additions & 56 deletions src/openvpn/dco_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,55 @@ create_dco_handle(const char *devname, struct gc_arena *gc)
return tt;
}

static void
dco_get_version(OVPN_VERSION *version)
{
bool res = false;

HANDLE h = CreateFile("\\\\.\\ovpn-dco-ver", GENERIC_READ,
0, NULL, OPEN_EXISTING, 0, NULL);

if (h == INVALID_HANDLE_VALUE)
{
/* fallback to a "normal" device, this will fail if device is already in use */
h = CreateFile("\\\\.\\ovpn-dco", GENERIC_READ,
0, NULL, OPEN_EXISTING, 0, NULL);
}

if (h == INVALID_HANDLE_VALUE)
{
goto done;
}

DWORD bytes_returned = 0;
if (!DeviceIoControl(h, OVPN_IOCTL_GET_VERSION, NULL, 0,
version, sizeof(*version), &bytes_returned, NULL))
{
goto done;
}

done:
if (h != INVALID_HANDLE_VALUE)
{
CloseHandle(h);
}

msg(D_DCO_DEBUG, "dco version: %d.%d.%d", version->Major, version->Minor, version->Patch);
}

static inline
bool dco_version_supports_data_v3(OVPN_VERSION *version)
{
return (version->Major > 1) || (version->Minor >= 4);
}

bool
ovpn_dco_init(int mode, dco_context_t *dco)
{
dco->supports_data_v3 = dco_supports_data_v3(NULL);
msg(D_DCO_DEBUG, "dco supports data_v3: %d", dco->supports_data_v3);
dco_get_version(&dco->version);
dco->supports_data_v3 = dco_version_supports_data_v3(&dco->version);

msg(D_DCO_DEBUG, "dco data_v3: %d", dco->supports_data_v3);

return true;
}
Expand Down Expand Up @@ -428,33 +472,9 @@ const char *
dco_version_string(struct gc_arena *gc)
{
OVPN_VERSION version;
ZeroMemory(&version, sizeof(OVPN_VERSION));
ZeroMemory(&version, sizeof(version));

/* first, try a non-exclusive control device, available from 1.3.0 */
HANDLE h = CreateFile("\\\\.\\ovpn-dco-ver", GENERIC_READ,
0, NULL, OPEN_EXISTING, 0, NULL);

if (h == INVALID_HANDLE_VALUE)
{
/* fallback to a "normal" device, this will fail if device is already in use */
h = CreateFile("\\\\.\\ovpn-dco", GENERIC_READ,
0, NULL, OPEN_EXISTING, 0, NULL);
}

if (h == INVALID_HANDLE_VALUE)
{
return "N/A";
}

DWORD bytes_returned = 0;
if (!DeviceIoControl(h, OVPN_IOCTL_GET_VERSION, NULL, 0,
&version, sizeof(version), &bytes_returned, NULL))
{
CloseHandle(h);
return "N/A";
}

CloseHandle(h);
dco_get_version(&version);

struct buffer out = alloc_buf_gc(256, gc);
buf_printf(&out, "%ld.%ld.%ld", version.Major, version.Minor, version.Patch);
Expand Down Expand Up @@ -538,36 +558,11 @@ dco_get_supported_ciphers()
bool
dco_supports_data_v3(struct context *c)
{
bool res = false;

HANDLE h = CreateFile("\\\\.\\ovpn-dco-ver", GENERIC_READ,
0, NULL, OPEN_EXISTING, 0, NULL);

if (h == INVALID_HANDLE_VALUE)
{
goto done;
}

OVPN_VERSION version;
ZeroMemory(&version, sizeof(OVPN_VERSION));

DWORD bytes_returned = 0;
if (!DeviceIoControl(h, OVPN_IOCTL_GET_VERSION, NULL, 0,
&version, sizeof(version), &bytes_returned, NULL))
{
goto done;
}

/* data_v3 is supported starting from 1.4 */
res = (version.Major > 1) || (version.Minor >= 4);

done:
if (h != INVALID_HANDLE_VALUE)
{
CloseHandle(h);
}
ZeroMemory(&version, sizeof(version));
dco_get_version(&version);

return res;
return dco_version_supports_data_v3(&version);
}

#endif /* defined(_WIN32) */
1 change: 1 addition & 0 deletions src/openvpn/dco_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef OVPN_CIPHER_ALG dco_cipher_t;
struct dco_context {
struct tuntap *tt;
bool supports_data_v3;
OVPN_VERSION version;
};

typedef struct dco_context dco_context_t;
Expand Down

0 comments on commit 76d42f1

Please sign in to comment.