Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1458?usp=email

to review the following change.


Change subject: openvpnserv: Factor out the string conversion from 
GetItfDnsDomains
......................................................................

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 <[email protected]>
---
M src/openvpnserv/interactive.c
1 file changed, 101 insertions(+), 71 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/58/1458/1

diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c
index 8b418ce..f30394a 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 domain.
+ *
+ * @param[in]     search_domains  optional 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
+ *                                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 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 = sizeof(*domains);
+    const DWORD buf_len = 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 = domains;
+    while (TRUE)
+    {
+        /* Terminate the domain at the next comma */
+        PWCHAR comma = wcschr(pos, ',');
+        if (comma)
+        {
+            *comma = '\0';
+        }
+
+        DWORD domain_len = (DWORD)wcslen(pos);
+        DWORD domain_size = domain_len * glyph_size;
+        DWORD converted_size = (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 -= domain_size + glyph_size;
+                continue;
+            }
+            else
+            {
+                /* This was the last domain */
+                *pos = '\0';
+                *size -= domain_size;
+                return wcslen(domains) ? NO_ERROR : ERROR_FILE_NOT_FOUND;
+            }
+        }
+
+        /* Add space for the leading dot */
+        domain_len += 1;
+        domain_size += glyph_size;
+
+        /* Space for the terminating zeros */
+        const DWORD extra_size = 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 = '\0';
+            *size = converted_size == 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] = '\0';
+        *pos = '.';
+        *size += glyph_size;
+
+        if (!comma)
+        {
+            /* Conversion is done */
+            *(pos + domain_len) = '\0';
+            *size += glyph_size;
+            return NO_ERROR;
+        }
+
+        /* Comma pos is now +1 after adding leading dot */
+        pos = comma + 2;
+    }
+}
+
+/**
  * Return interface specific domain suffix(es)
  *
  * The \p domains paramter will be set to a MULTI_SZ domains string.
@@ -2183,77 +2283,7 @@
         err = RegGetValueW(itf, NULL, values[i], RRF_RT_REG_SZ, NULL, 
(PBYTE)domains, size);
         if (!err && *size > glyph_size && domains[(*size / glyph_size) - 1] == 
'\0' && wcschr(domains, '.'))
         {
-            /*
-             * Found domain(s), now convert them:
-             *   - prefix each domain with a dot
-             *   - convert comma separated list to MULTI_SZ
-             */
-            PWCHAR pos = domains;
-            while (TRUE)
-            {
-                /* Terminate the domain at the next comma */
-                PWCHAR comma = wcschr(pos, ',');
-                if (comma)
-                {
-                    *comma = '\0';
-                }
-
-                DWORD domain_len = (DWORD)wcslen(pos);
-                DWORD domain_size = domain_len * glyph_size;
-                DWORD converted_size = (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 -= domain_size + glyph_size;
-                        continue;
-                    }
-                    else
-                    {
-                        /* This was the last domain */
-                        *pos = '\0';
-                        *size -= domain_size;
-                        return wcslen(domains) ? NO_ERROR : 
ERROR_FILE_NOT_FOUND;
-                    }
-                }
-
-                /* Add space for the leading dot */
-                domain_len += 1;
-                domain_size += glyph_size;
-
-                /* Space for the terminating zeros */
-                const DWORD extra_size = 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 = '\0';
-                    *size = converted_size == 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] = '\0';
-                *pos = '.';
-                *size += glyph_size;
-
-                if (!comma)
-                {
-                    /* Conversion is done */
-                    *(pos + domain_len) = '\0';
-                    *size += glyph_size;
-                    return NO_ERROR;
-                }
-
-                /* Comma pos is now +1 after adding leading dot */
-                pos = comma + 2;
-            }
+            return ConvertItfDnsDomains(search_domains, domains, size, 
buf_size);
         }
     }


--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1458?usp=email
To unsubscribe, or for help writing mail filters, visit 
http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ib517457015b754d59aeb70827c4795aa6154728c
Gerrit-Change-Number: 1458
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]>
Gerrit-Reviewer: plaisthos <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to