From: "stipa (Code Review)" Date: Fri, 6 Sep 2024 07:43:54 +0000 References: Reply-To: lstipakov@...277..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="G/G17lmi9lw="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: dco-win: factor out getting dco version info own function List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --G/G17lmi9lw= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello plaisthos, flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/733?usp=3Demail to review the following change. Change subject: dco-win: factor out getting dco version info own function ...................................................................... dco-win: factor out getting dco version info own function 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 --- M src/openvpn/dco_win.c M src/openvpn/dco_win.h 2 files changed, 51 insertions(+), 56 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/33/733/1 diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c index 189864b..cf825b3 100644 --- a/src/openvpn/dco_win.c +++ b/src/openvpn/dco_win.c @@ -52,11 +52,54 @@ return tt; } +static void +dco_get_version(OVPN_VERSION *version) +{ + HANDLE h =3D CreateFile("\\\\.\\ovpn-dco-ver", GENERIC_READ, + 0, NULL, OPEN_EXISTING, 0, NULL); + + if (h =3D=3D INVALID_HANDLE_VALUE) + { + /* fallback to a "normal" device, this will fail if device is alre= ady in use */ + h =3D CreateFile("\\\\.\\ovpn-dco", GENERIC_READ, + 0, NULL, OPEN_EXISTING, 0, NULL); + } + + if (h =3D=3D INVALID_HANDLE_VALUE) + { + goto done; + } + + DWORD bytes_returned =3D 0; + if (!DeviceIoControl(h, OVPN_IOCTL_GET_VERSION, NULL, 0, + version, sizeof(*version), &bytes_returned, NULL)) + { + goto done; + } + +done: + if (h !=3D INVALID_HANDLE_VALUE) + { + CloseHandle(h); + } + + msg(D_DCO_DEBUG, "dco version: %ld.%ld.%ld", version->Major, version->= Minor, version->Patch); +} + +static inline +bool +dco_version_supports_data_v3(OVPN_VERSION *version) +{ + return (version->Major > 1) || (version->Minor >=3D 4); +} + bool ovpn_dco_init(int mode, dco_context_t *dco) { - dco->supports_data_v3 =3D 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 =3D dco_version_supports_data_v3(&dco->version); + + msg(D_DCO_DEBUG, "dco data_v3: %d", dco->supports_data_v3); return true; } @@ -428,33 +471,9 @@ 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 =3D CreateFile("\\\\.\\ovpn-dco-ver", GENERIC_READ, - 0, NULL, OPEN_EXISTING, 0, NULL); - - if (h =3D=3D INVALID_HANDLE_VALUE) - { - /* fallback to a "normal" device, this will fail if device is alre= ady in use */ - h =3D CreateFile("\\\\.\\ovpn-dco", GENERIC_READ, - 0, NULL, OPEN_EXISTING, 0, NULL); - } - - if (h =3D=3D INVALID_HANDLE_VALUE) - { - return "N/A"; - } - - DWORD bytes_returned =3D 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 =3D alloc_buf_gc(256, gc); buf_printf(&out, "%ld.%ld.%ld", version.Major, version.Minor, version.= Patch); @@ -538,36 +557,11 @@ bool dco_supports_data_v3(struct context *c) { - bool res =3D false; - - HANDLE h =3D CreateFile("\\\\.\\ovpn-dco-ver", GENERIC_READ, - 0, NULL, OPEN_EXISTING, 0, NULL); - - if (h =3D=3D INVALID_HANDLE_VALUE) - { - goto done; - } - OVPN_VERSION version; - ZeroMemory(&version, sizeof(OVPN_VERSION)); + ZeroMemory(&version, sizeof(version)); + dco_get_version(&version); - DWORD bytes_returned =3D 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 =3D (version.Major > 1) || (version.Minor >=3D 4); - -done: - if (h !=3D INVALID_HANDLE_VALUE) - { - CloseHandle(h); - } - - return res; + return dco_version_supports_data_v3(&version); } #endif /* defined(_WIN32) */ diff --git a/src/openvpn/dco_win.h b/src/openvpn/dco_win.h index f58488f..ce57817 100644 --- a/src/openvpn/dco_win.h +++ b/src/openvpn/dco_win.h @@ -34,6 +34,7 @@ struct dco_context { struct tuntap *tt; bool supports_data_v3; + OVPN_VERSION version; }; typedef struct dco_context dco_context_t; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/733?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I8e8ddd35bd3cc3334faf7f57118d1892512ae9f7 Gerrit-Change-Number: 733 Gerrit-PatchSet: 1 Gerrit-Owner: stipa Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --G/G17lmi9lw= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

stipa would like plaisthos and flichtenheld to review t= his change.

Vi= ew Change

dco-win: factor out getting dco version info own function
=
Instead of having duplicated code to get dco version
for data_v3 che= cks 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-of= f-by: Lev Stipakov <lev@...515...>
---
M src/openvpn/dco_win.c<= br>M src/openvpn/dco_win.h
2 files changed, 51 insertions(+), 56 deletio= ns(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/change=
s/33/733/1
diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win=
.c
index 189864b..cf825b3 100644
--- a/src/= openvpn/dco_win.c
+++ b/src/openvpn/dco_win.c
@@ -52,11 +52,54 @@
return tt;
}

+sta= tic void
+dco_get_ver= sion(OVPN_VERSION *version)
+{
+ HANDLE= h =3D CreateFile("\\\\.\\ovpn-dco-ver", GENERIC_READ,
= + 0, N= ULL, OPEN_EXISTING, 0, NULL);
+
+ if (h= =3D=3D INVALID_HANDLE_VALUE)
+ {
+ = /* fallback to a "normal" device, this will fail if device is= already in use */
+ = h =3D CreateFile("\\\\.\\ovpn-dco", GENERIC_READ,+ 0, NU= LL, OPEN_EXISTING, 0, NULL);
+ }
+
+ if (h =3D=3D INVALID= _HANDLE_VALUE)
+ {=
+ goto done;<= /span>
+ }
+

+ DWORD bytes_returned =3D 0;
+ if (!DeviceIoControl(h, OVPN_IOCTL_GE= T_VERSION, NULL, 0,
+= version, sizeof(*version), &bytes_returned, NU= LL))
+ {+ goto done;
= + }
+
+done:
+ = if (h !=3D INVALID_HANDLE_VALUE)
+ {
= + CloseHandle(h);
+ }
++ msg(D_DCO_DEBUG, "d= co version: %ld.%ld.%ld", version->Major, version->Minor, versio= n->Patch);
+}
+
+static inline
+bool
+dco_version_supports_data_v3(OVPN_VERSION *version)
= +{
+ return (version->Major > 1) || (versio= n->Minor >=3D 4);
+}
+
bool

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

return true;
}
@@ -428,33 +471,9 @@
dco_ve= rsion_string(struct gc_arena *gc)
{
O= VPN_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 =3D CreateF= ile("\\\\.\\ovpn-dco-ver", GENERIC_READ,
- 0, NULL, OPEN_EXISTI= NG, 0, NULL);
-<= br>- if (h =3D=3D INVALID_HAND= LE_VALUE)
- {=
- /* fallback to a &qu= ot;normal" device, this will fail if device is already in use */
- h =3D CreateFile(&q= uot;\\\\.\\ovpn-dco", GENERIC_READ,
- 0, NULL, OPEN_EXISTING, 0, NULL);=
- }
-

- if (h =3D=3D INVALID_HANDLE_VALUE)
- {
- return "N/A";
- }
-
- DWORD= bytes_returned =3D 0;
= - if (!DeviceIoControl(h, OVPN_IOCTL_GET_VERSION, NULL, 0,
- &versi= on, sizeof(version), &bytes_returned, NULL))

- {
- CloseHandle(h);
- return "N/A";
- }
-
- CloseHandl= e(h);
+ dco_get_ve= rsion(&version);

struct buffer o= ut =3D alloc_buf_gc(256, gc);
buf_printf(&out, &qu= ot;%ld.%ld.%ld", version.Major, version.Minor, version.Patch);<= br>@@ -538,36 +557,11 @@
bool
dco_s= upports_data_v3(struct context *c)
{
- bool res =3D false;
-
- HANDLE h =3D CreateFile("\\\\.\\ovpn-dco-ver", G= ENERIC_READ,
- = 0, NULL, OPEN_EXISTING, 0, NULL);
-
- if (h =3D=3D INVALID_HANDLE_VALUE)
- {
- goto done;
- }
- OVPN_VERSION version;
- ZeroMemory(&version, sizeof(OVPN_VERSION));
<= span style=3D"color: hsl(120, 100%, 40%);">+ ZeroMemory(&version, si= zeof(version));

+ = dco_get_version(&version);

- DWORD bytes_returned =3D 0;
- if (!DeviceIoControl(h, OVPN_IOCTL= _GET_VERSION, NULL, 0,
= - &version, sizeof(version), &bytes_returne= d, NULL))
- {=
- goto done;- }
-
- /* data_v3 is supported starting from 1.4 */
- res =3D (version.Major > 1) || (ve= rsion.Minor >=3D 4);
-
-done:
- if (h !=3D INVALID_HANDLE_VALUE= )

- {
- CloseHandle(h);

- }

-
- return res;
+= return dco_version_supports_data_v3(&version);
}

#endif /* defined(_WIN32) */
diff --git a/src/openvpn/dco_win.h b/src/openvpn/dco_win.h

index f58488f..ce57817 100644
--- a/src/openvpn/dco_win= .h
+++ b/src/openvpn/dco_win.h
@@ -34,6 +34= ,7 @@
struct dco_context {
struct tun= tap *tt;
bool supports_data_v3;
+ OVPN_VERSION version;
};

typedef struct dco_context dco_cont= ext_t;

To view, visit change 733. To unsubsc= ribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I8e8ddd35bd3cc3334faf7f57118= d1892512ae9f7
Gerrit-Change-Number: 733
Gerrit-PatchSet: 1
Gerrit-Owner: stipa <lstipakov@...277...>= ;
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newchange
--G/G17lmi9lw=--