From: "cron2 (Code Review)" Date: Sat, 24 Jan 2026 17:21:37 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., heiko@...515..., 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="h30v8yPlLGA="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: openvpnserv: Factor out the string conversion from GetItfDnsDomains List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: d12fk , plaisthos , openvpn-devel --h30v8yPlLGA= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/14= 58?usp=3Demail ) Change subject: openvpnserv: Factor out the string conversion from GetItfDn= sDomains ...................................................................... openvpnserv: Factor out the string conversion from GetItfDnsDomains Mostly so that we can actually test it. Since that code does some in-place conversions a test would be good. Change-Id: Ib517457015b754d59aeb70827c4795aa6154728c Signed-off-by: Frank Lichtenheld Acked-by: Heiko Hund Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1458 Message-Id: <20260119214927.27766-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg35= 343.html Signed-off-by: Gert Doering --- M src/openvpnserv/interactive.c 1 file changed, 101 insertions(+), 72 deletions(-) diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c index b6ab2b8..ad19e2c 100644 --- a/src/openvpnserv/interactive.c +++ b/src/openvpnserv/interactive.c @@ -2143,6 +2143,106 @@ } /** + * Convert interface specific domain suffix(es) from comma-separated + * string to MULTI_SZ string. + * + * The \p domains paramter will be set to a MULTI_SZ domains string. + * In case of an error \p size is set to 0 and the contents of \p domains + * are invalid. + * Note that domains are deleted from the string if they match a search do= main. + * + * @param[in] search_domains optional list of search domains + * @param[in,out] domains buffer that contains the input comma-sep= arated + * string and will contain the MULTI_SZ out= put string + * @param[in,out] size pointer to size of the input string in b= ytes. Will be + * set to the size of the string returned, = including + * the terminating zeros or 0. + * @param[in] buf_size size of the \p domains buffer + * + * @return LSTATUS NO_ERROR if the domain suffix(es) were read successfull= y, + * ERROR_FILE_NOT_FOUND if no domain was found for the interface, + * ERROR_MORE_DATA if the list did not fit into the buffer + */ +static LSTATUS +ConvertItfDnsDomains(PCWSTR search_domains, PWSTR domains, PDWORD size, co= nst DWORD buf_size) +{ + const DWORD glyph_size =3D sizeof(*domains); + const DWORD buf_len =3D buf_size / glyph_size; + + /* + * Found domain(s), now convert them: + * - prefix each domain with a dot + * - convert comma separated list to MULTI_SZ + */ + PWCHAR pos =3D domains; + while (TRUE) + { + /* Terminate the domain at the next comma */ + PWCHAR comma =3D wcschr(pos, ','); + if (comma) + { + *comma =3D '\0'; + } + + DWORD domain_len =3D (DWORD)wcslen(pos); + DWORD domain_size =3D domain_len * glyph_size; + DWORD converted_size =3D (DWORD)(pos - domains) * glyph_size; + + /* Ignore itf domains which match a pushed search domain */ + if (ListContainsDomain(search_domains, pos, domain_len)) + { + if (comma) + { + /* Overwrite the ignored domain with remaining one(s) */ + memmove(pos, comma + 1, buf_size - converted_size); + *size -=3D domain_size + glyph_size; + continue; + } + else + { + /* This was the last domain */ + *pos =3D '\0'; + *size -=3D domain_size; + return wcslen(domains) ? NO_ERROR : ERROR_FILE_NOT_FOUND; + } + } + + /* Add space for the leading dot */ + domain_len +=3D 1; + domain_size +=3D glyph_size; + + /* Space for the terminating zeros */ + const DWORD extra_size =3D 2 * glyph_size; + + /* Check for enough space to convert this domain */ + if (converted_size + domain_size + extra_size > buf_size) + { + /* Domain doesn't fit, bad luck if it's the first one */ + *pos =3D '\0'; + *size =3D converted_size =3D=3D 0 ? 0 : converted_size + glyph= _size; + return ERROR_MORE_DATA; + } + + /* Prefix domain at pos with the dot */ + memmove(pos + 1, pos, buf_size - converted_size - glyph_size); + domains[buf_len - 1] =3D '\0'; + *pos =3D '.'; + *size +=3D glyph_size; + + if (!comma) + { + /* Conversion is done */ + *(pos + domain_len) =3D '\0'; + *size +=3D glyph_size; + return NO_ERROR; + } + + /* Comma pos is now +1 after adding leading dot */ + pos =3D comma + 2; + } +} + +/** * Return interface specific domain suffix(es) * * The \p domains paramter will be set to a MULTI_SZ domains string. @@ -2174,7 +2274,6 @@ LSTATUS err =3D ERROR_FILE_NOT_FOUND; const DWORD buf_size =3D *size; const DWORD glyph_size =3D sizeof(*domains); - const DWORD buf_len =3D buf_size / glyph_size; PWSTR values[] =3D { L"SearchList", L"Domain", L"DhcpDomainSearchList"= , L"DhcpDomain", NULL }; for (int i =3D 0; values[i]; i++) @@ -2183,77 +2282,7 @@ err =3D RegGetValueW(itf, NULL, values[i], RRF_RT_REG_SZ, NULL, (P= BYTE)domains, size); if (!err && *size > glyph_size && domains[(*size / glyph_size) - 1= ] =3D=3D '\0' && wcschr(domains, '.')) { - /* - * Found domain(s), now convert them: - * - prefix each domain with a dot - * - convert comma separated list to MULTI_SZ - */ - PWCHAR pos =3D domains; - while (TRUE) - { - /* Terminate the domain at the next comma */ - PWCHAR comma =3D wcschr(pos, ','); - if (comma) - { - *comma =3D '\0'; - } - - DWORD domain_len =3D (DWORD)wcslen(pos); - DWORD domain_size =3D domain_len * glyph_size; - DWORD converted_size =3D (DWORD)(pos - domains) * glyph_si= ze; - - /* Ignore itf domains which match a pushed search domain */ - if (ListContainsDomain(search_domains, pos, domain_len)) - { - if (comma) - { - /* Overwrite the ignored domain with remaining one= (s) */ - memmove(pos, comma + 1, buf_size - converted_size); - *size -=3D domain_size + glyph_size; - continue; - } - else - { - /* This was the last domain */ - *pos =3D '\0'; - *size -=3D domain_size; - return wcslen(domains) ? NO_ERROR : ERROR_FILE_NOT= _FOUND; - } - } - - /* Add space for the leading dot */ - domain_len +=3D 1; - domain_size +=3D glyph_size; - - /* Space for the terminating zeros */ - const DWORD extra_size =3D 2 * glyph_size; - - /* Check for enough space to convert this domain */ - if (converted_size + domain_size + extra_size > buf_size) - { - /* Domain doesn't fit, bad luck if it's the first one = */ - *pos =3D '\0'; - *size =3D converted_size =3D=3D 0 ? 0 : converted_size= + glyph_size; - return ERROR_MORE_DATA; - } - - /* Prefix domain at pos with the dot */ - memmove(pos + 1, pos, buf_size - converted_size - glyph_si= ze); - domains[buf_len - 1] =3D '\0'; - *pos =3D '.'; - *size +=3D glyph_size; - - if (!comma) - { - /* Conversion is done */ - *(pos + domain_len) =3D '\0'; - *size +=3D glyph_size; - return NO_ERROR; - } - - /* Comma pos is now +1 after adding leading dot */ - pos =3D comma + 2; - } + return ConvertItfDnsDomains(search_domains, domains, size, buf= _size); } } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1458?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: merged Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ib517457015b754d59aeb70827c4795aa6154728c Gerrit-Change-Number: 1458 Gerrit-PatchSet: 7 Gerrit-Owner: flichtenheld Gerrit-Reviewer: d12fk Gerrit-Reviewer: plaisthos Gerrit-CC: cron2 Gerrit-CC: openvpn-devel --h30v8yPlLGA= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 sub= mitted this change.

View Change

openvpnserv: Factor out the string conversion fr=
om GetItfDnsDomains

Mostly so that we can actually test it. Since th= at
code does some in-place conversions a test would be
good.

C= hange-Id: Ib517457015b754d59aeb70827c4795aa6154728c
Signed-off-by: Frank= Lichtenheld <frank@...2641...>
Acked-by: Heiko Hund <heiko@...493...= 515...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1458
Me= ssage-Id: <20260119214927.27766-1-gert@...1296...>
URL: https://ww= w.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg35343.html
Sig= ned-off-by: Gert Doering <gert@...1296...>
---
M src/openvpnser= v/interactive.c
1 file changed, 101 insertions(+), 72 deletions(-)
diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive=
.c
index b6ab2b8..ad19e2c 100644
--- a/src/= openvpnserv/interactive.c
+++ b/src/openvpnserv/interactive= .c
@@ -2143,6 +2143,106 @@
}

/**
+ * Convert interface specific domain suffix(es) from comma-separated<= /span>
+ * string to MULTI_S= Z string.
+ *<= br>+ * The \p domains paramter = will be set to a MULTI_SZ domains string.
+ * In case of an error \p size is set to 0 and the co= ntents of \p domains
= + * are invalid.
+ * = Note that domains are deleted from the string if they match a search domain= .
+ *
+ * @param[in] search_domains o= ptional list of search domains

+ * @param[in,out] domains buffer that contains the input= comma-separated
+ * = string and will contain the MULTI_SZ output = string
+ * @param[in,= out] size pointer to size of the input string in bytes. Will be<= /span>
+ * = set to the size of the string returned, including
<= span style=3D"color: hsl(120, 100%, 40%);">+ * = the terminating zeros or 0.

+ * @param[in] buf_size size of the \p domains buffer<= /span>
+ *
+ * @return LSTATUS NO_ERROR if the do= main suffix(es) were read successfully,
+ * ERROR_FILE_NOT_FOUND if no domain was found = for the interface,
+ = * ERROR_MORE_DATA if the list did not fit into the buffer+ */
+static LSTATUS
+ConvertItfDnsDomains(PCWSTR search_domains, PWSTR = domains, PDWORD size, const DWORD buf_size)
+{
+ const DWORD glyph_size =3D sizeof(*domains);
+ const DWORD buf_len =3D buf_size / g= lyph_size;
++ /*
+ * Found domain(s), now convert them:=
+ * - prefix e= ach domain with a dot
+ * - convert comma separated list to MULTI_SZ
+ */
+ PWCHAR pos =3D domains;
+ while (TRUE)
+ {
+ /* Terminate the domain at the next comma */+ PWCHAR comma =3D wcsc= hr(pos, ',');
+ if (comma)
= + {
+ = *comma =3D '\0';
+ }
+
+ DWORD domain_= len =3D (DWORD)wcslen(pos);
+ DWORD domain_size =3D domain_len * glyph_size;
+ DWORD converted_size =3D= (DWORD)(pos - domains) * glyph_size;

+
+ = /* Ignore itf domains which match a pushed search domain */+ if (ListContainsDoma= in(search_domains, pos, domain_len))
+ {
+ if (comma)
+ {
+ /* Overwrite the ignored domain with remaining one(s) *= /
+ me= mmove(pos, comma + 1, buf_size - converted_size);
+ *size -=3D domain_size + glyp= h_size;
+ = continue;
+ = }
+ = else
+ {
+ /* Th= is was the last domain */
+ *pos =3D '\0';
+ *size -=3D domain_size;
= + return wcslen(= domains) ? NO_ERROR : ERROR_FILE_NOT_FOUND;
+ }
+ }
+
+ /* = Add space for the leading dot */
+ domain_len +=3D 1;
+ domain_size +=3D glyph_size;
+
+ /* Space for the terminating zeros */
<= span style=3D"color: hsl(120, 100%, 40%);">+ const DWORD extra_size = =3D 2 * glyph_size;

+=
+ /* Check fo= r enough space to convert this domain */
+ if (converted_size + domain_size + extra_size = > buf_size)
+ = {
+ /* = Domain doesn't fit, bad luck if it's the first one */
+ *pos =3D '\0'= ;

+ *size = =3D converted_size =3D=3D 0 ? 0 : converted_size + glyph_size;
+ return ERROR_MORE_DA= TA;
+ }=
+
+ /* Prefix domain at pos with the dot = */
+ memmove(p= os + 1, pos, buf_size - converted_size - glyph_size);
+ domains[buf_len - 1] =3D '\0&= #39;;
+ *pos = =3D '.';
+ = *size +=3D glyph_size;
+
+ if = (!comma)
+ {
+ /* Conver= sion is done */
+ = *(pos + domain_len) =3D '\0';
+ *size +=3D glyph_size;
+ return NO_ERROR;
+ }
+

+ /* Comma pos is now +1 after adding leading do= t */
+ pos =3D= comma + 2;
+ }

+}
+
+/**
* Return interface specific domain su= ffix(es)
*
* The \p domains paramter wi= ll be set to a MULTI_SZ domains string.
@@ -2174,7 +2274,6 = @@
LSTATUS err =3D ERROR_FILE_NOT_FOUND;
const DWORD buf_size =3D *size;
const DWORD gl= yph_size =3D sizeof(*domains);
- const DWORD buf_len =3D buf_size / glyph_size;
PWSTR values[] =3D { L"SearchList", L"Domain", L&= quot;DhcpDomainSearchList", L"DhcpDomain", NULL };
for (int i =3D 0; values[i]; i++)
<= span>@@ -2183,77 +2282,7 @@
err =3D RegGetValueW(i= tf, NULL, values[i], RRF_RT_REG_SZ, NULL, (PBYTE)domains, size);
= if (!err && *size > glyph_size && domains= [(*size / glyph_size) - 1] =3D=3D '\0' && wcschr(domains, &= #39;.'))
{
- /*
- * Found domain(s), now convert them:
- * - prefix each domai= n with a dot
- = * - convert comma separated list to MULTI_SZ
- */
- PWCHAR pos =3D domains;
- while (TRUE)
- {
- /* Terminate the domain at = the next comma */
- = PWCHAR comma =3D wcschr(pos, ',');
- if (comma)
- {
- *comma =3D '\0= 9;;
- }<= /span>
-
- DWORD domain_len =3D (DWORD= )wcslen(pos);
- = DWORD domain_size =3D domain_len * glyph_size;
- DWORD converted_size =3D (= DWORD)(pos - domains) * glyph_size;
-
- = /* Ignore itf domains which match a pushed search domain */=
- if (ListCont= ainsDomain(search_domains, pos, domain_len))
- {
- if (comma)
- {
- /* Overwrite the ignore= d domain with remaining one(s) */
- memmove(pos, comma + 1, buf_size - conv= erted_size);
- = *size -=3D domain_size + glyph_size;
- continue;- }- else=
- {=
- /* T= his was the last domain */
- *pos =3D '\0';
- *size -=3D domain_s= ize;
- = return wcslen(domains) ? NO_ERROR : ERROR_FILE_NOT_FOUND;
= - }
= - }
-

- /* Add space for the leading dot */

- domain_len = +=3D 1;
- = domain_size +=3D glyph_size;
-
- = /* Space for the terminating zeros */
- const DWORD extra_size =3D 2 * glyph_si= ze;
-
- /* Check for enough spa= ce to convert this domain */
- if (converted_size + domain_size + extra_size > b= uf_size)
- = {
- = /* Domain doesn't fit, bad luck if it's the first one */<= br>- *pos =3D = '\0';
- = *size =3D converted_size =3D=3D 0 ? 0 : converted_size + glyph_= size;
- = return ERROR_MORE_DATA;
- }
-
- /* = Prefix domain at pos with the dot */
- memmove(pos + 1, pos, buf_size - converted_s= ize - glyph_size);
- = domains[buf_len - 1] =3D '\0';
- *pos =3D '.';

- *size +=3D = glyph_size;
-- if (!comma)

- {<= br>- /* Conver= sion is done */
- = *(pos + domain_len) =3D '\0';
- *size +=3D glyph_size;<= /span>
- re= turn NO_ERROR;
- = }
-
= - /* Comma pos is = now +1 after adding leading dot */
- pos =3D comma + 2;
- }
+ return ConvertItfDnsDomains(search_domains,= domains, size, buf_size);
}
= }


To view, visit change 1458<= /a>. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ib517457015b754d59aeb70827c4= 795aa6154728c
Gerrit-Change-Number: 1458
Gerrit-PatchSet: 7
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: d12fk <heiko@...515...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: cron2 <gert@...1296...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --h30v8yPlLGA=--