From: "cron2 (Code Review)" Date: Sat, 11 Oct 2025 08:33:13 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <1ef617bb666c2e7b26964cf855f3f363ee16aec8-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="luyMARyCUww="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Move build_dhcp_options_string from tun to dhcp List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld , plaisthos Cc: openvpn-devel --luyMARyCUww= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#2) to the change originally created by= flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1265?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 Change subject: Move build_dhcp_options_string from tun to dhcp ...................................................................... Move build_dhcp_options_string from tun to dhcp Seems suitably related and tun.c is one of the huge ones. In preparation of adding UTs for the code. Change-Id: I62a8f62b9c0938cfcb99d184b07034515c076303 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1265 Message-Id: <20251011082217.27568-1-gert@...1296...> URL: https://sourceforge.net/p/openvpn/mailman/message/59245240/ Signed-off-by: Gert Doering --- M src/openvpn/dhcp.c M src/openvpn/dhcp.h M src/openvpn/tun.c 3 files changed, 207 insertions(+), 196 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/65/1265/2 diff --git a/src/openvpn/dhcp.c b/src/openvpn/dhcp.c index 850a4b6..653127d 100644 --- a/src/openvpn/dhcp.c +++ b/src/openvpn/dhcp.c @@ -185,3 +185,203 @@ } return 0; } + +#if defined(_WIN32) + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + +/* + * Convert DHCP options from the command line / config file + * into a raw DHCP-format options string. + */ + +static void +write_dhcp_u8(struct buffer *buf, const int type, const int data, bool *er= ror) +{ + if (!buf_safe(buf, 3)) + { + *error =3D true; + msg(M_WARN, "write_dhcp_u8: buffer overflow building DHCP options"= ); + return; + } + buf_write_u8(buf, type); + buf_write_u8(buf, 1); + buf_write_u8(buf, data); +} + +static void +write_dhcp_u32_array(struct buffer *buf, const int type, const uint32_t *d= ata, + const unsigned int len, bool *error) +{ + if (len > 0) + { + int i; + const int size =3D len * sizeof(uint32_t); + + if (!buf_safe(buf, 2 + size)) + { + *error =3D true; + msg(M_WARN, "write_dhcp_u32_array: buffer overflow building DH= CP options"); + return; + } + if (size < 1 || size > 255) + { + *error =3D true; + msg(M_WARN, "write_dhcp_u32_array: size (%d) must be > 0 and <= =3D 255", size); + return; + } + buf_write_u8(buf, type); + buf_write_u8(buf, size); + for (i =3D 0; i < len; ++i) + { + buf_write_u32(buf, data[i]); + } + } +} + +static void +write_dhcp_str(struct buffer *buf, const int type, const char *str, bool *= error) +{ + const int len =3D strlen(str); + if (!buf_safe(buf, 2 + len)) + { + *error =3D true; + msg(M_WARN, "write_dhcp_str: buffer overflow building DHCP options= "); + return; + } + if (len < 1 || len > 255) + { + *error =3D true; + msg(M_WARN, "write_dhcp_str: string '%s' must be > 0 bytes and <= =3D 255 bytes", str); + return; + } + buf_write_u8(buf, type); + buf_write_u8(buf, len); + buf_write(buf, str, len); +} + +/* + * RFC3397 states that multiple searchdomains are encoded as follows: + * - at start the length of the entire option is given + * - each subdomain is preceded by its length + * - each searchdomain is separated by a NUL character + * e.g. if you want "openvpn.net" and "duckduckgo.com" then you end up with + * 0x1D 0x7 openvpn 0x3 net 0x00 0x0A duckduckgo 0x3 com 0x00 + */ +static void +write_dhcp_search_str(struct buffer *buf, const int type, const char *cons= t *str_array, + int array_len, bool *error) +{ + char tmp_buf[256]; + int i; + int len =3D 0; + int label_length_pos; + + for (i =3D 0; i < array_len; i++) + { + const char *ptr =3D str_array[i]; + + if (strlen(ptr) + len + 1 > sizeof(tmp_buf)) + { + *error =3D true; + msg(M_WARN, "write_dhcp_search_str: temp buffer overflow build= ing DHCP options"); + return; + } + /* Loop over all subdomains separated by a dot and replace the dot + * with the length of the subdomain */ + + /* label_length_pos points to the byte to be replaced by the length + * of the following domain label */ + label_length_pos =3D len++; + + while (true) + { + if (*ptr =3D=3D '.' || *ptr =3D=3D '\0') + { + tmp_buf[label_length_pos] =3D (len - label_length_pos) - 1; + label_length_pos =3D len; + if (*ptr =3D=3D '\0') + { + break; + } + } + tmp_buf[len++] =3D *ptr++; + } + /* And close off with an extra NUL char */ + tmp_buf[len++] =3D 0; + } + + if (!buf_safe(buf, 2 + len)) + { + *error =3D true; + msg(M_WARN, "write_search_dhcp_str: buffer overflow building DHCP = options"); + return; + } + if (len > 255) + { + *error =3D true; + msg(M_WARN, "write_dhcp_search_str: search domain string must be <= =3D 255 bytes"); + return; + } + + buf_write_u8(buf, type); + buf_write_u8(buf, len); + buf_write(buf, tmp_buf, len); +} + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif + +bool +build_dhcp_options_string(struct buffer *buf, const struct tuntap_options = *o) +{ + bool error =3D false; + if (o->domain) + { + write_dhcp_str(buf, 15, o->domain, &error); + } + + if (o->netbios_scope) + { + write_dhcp_str(buf, 47, o->netbios_scope, &error); + } + + if (o->netbios_node_type) + { + write_dhcp_u8(buf, 46, o->netbios_node_type, &error); + } + + write_dhcp_u32_array(buf, 6, (uint32_t *)o->dns, o->dns_len, &error); + write_dhcp_u32_array(buf, 44, (uint32_t *)o->wins, o->wins_len, &error= ); + write_dhcp_u32_array(buf, 42, (uint32_t *)o->ntp, o->ntp_len, &error); + write_dhcp_u32_array(buf, 45, (uint32_t *)o->nbdd, o->nbdd_len, &error= ); + + if (o->domain_search_list_len > 0) + { + write_dhcp_search_str(buf, 119, o->domain_search_list, o->domain_s= earch_list_len, &error); + } + + /* the MS DHCP server option 'Disable Netbios-over-TCP/IP + * is implemented as vendor option 001, value 002. + * A value of 001 means 'leave NBT alone' which is the default */ + if (o->disable_nbt) + { + if (!buf_safe(buf, 8)) + { + msg(M_WARN, "build_dhcp_options_string: buffer overflow buildi= ng DHCP options"); + return false; + } + buf_write_u8(buf, 43); + buf_write_u8(buf, 6); /* total length field */ + buf_write_u8(buf, 0x001); + buf_write_u8(buf, 4); /* length of the vendor specified field */ + buf_write_u32(buf, 0x002); + } + return !error; +} + +#endif /* defined(_WIN32) */ diff --git a/src/openvpn/dhcp.h b/src/openvpn/dhcp.h index af5c2c4..8e15a39 100644 --- a/src/openvpn/dhcp.h +++ b/src/openvpn/dhcp.h @@ -84,4 +84,10 @@ in_addr_t dhcp_extract_router_msg(struct buffer *ipbuf); +#if defined(_WIN32) +#include "tun.h" + +bool build_dhcp_options_string(struct buffer *buf, const struct tuntap_opt= ions *o); +#endif + #endif /* ifndef DHCP_H */ diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index e35f889..097b9e8 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -45,6 +45,7 @@ #include "win32.h" #include "wfp_block.h" #include "networking.h" +#include "dhcp.h" #include "memdbg.h" @@ -5554,202 +5555,6 @@ return ret; } -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wconversion" -#endif - -/* - * Convert DHCP options from the command line / config file - * into a raw DHCP-format options string. - */ - -static void -write_dhcp_u8(struct buffer *buf, const int type, const int data, bool *er= ror) -{ - if (!buf_safe(buf, 3)) - { - *error =3D true; - msg(M_WARN, "write_dhcp_u8: buffer overflow building DHCP options"= ); - return; - } - buf_write_u8(buf, type); - buf_write_u8(buf, 1); - buf_write_u8(buf, data); -} - -static void -write_dhcp_u32_array(struct buffer *buf, const int type, const uint32_t *d= ata, - const unsigned int len, bool *error) -{ - if (len > 0) - { - int i; - const int size =3D len * sizeof(uint32_t); - - if (!buf_safe(buf, 2 + size)) - { - *error =3D true; - msg(M_WARN, "write_dhcp_u32_array: buffer overflow building DH= CP options"); - return; - } - if (size < 1 || size > 255) - { - *error =3D true; - msg(M_WARN, "write_dhcp_u32_array: size (%d) must be > 0 and <= =3D 255", size); - return; - } - buf_write_u8(buf, type); - buf_write_u8(buf, size); - for (i =3D 0; i < len; ++i) - { - buf_write_u32(buf, data[i]); - } - } -} - -static void -write_dhcp_str(struct buffer *buf, const int type, const char *str, bool *= error) -{ - const int len =3D strlen(str); - if (!buf_safe(buf, 2 + len)) - { - *error =3D true; - msg(M_WARN, "write_dhcp_str: buffer overflow building DHCP options= "); - return; - } - if (len < 1 || len > 255) - { - *error =3D true; - msg(M_WARN, "write_dhcp_str: string '%s' must be > 0 bytes and <= =3D 255 bytes", str); - return; - } - buf_write_u8(buf, type); - buf_write_u8(buf, len); - buf_write(buf, str, len); -} - -/* - * RFC3397 states that multiple searchdomains are encoded as follows: - * - at start the length of the entire option is given - * - each subdomain is preceded by its length - * - each searchdomain is separated by a NUL character - * e.g. if you want "openvpn.net" and "duckduckgo.com" then you end up with - * 0x1D 0x7 openvpn 0x3 net 0x00 0x0A duckduckgo 0x3 com 0x00 - */ -static void -write_dhcp_search_str(struct buffer *buf, const int type, const char *cons= t *str_array, - int array_len, bool *error) -{ - char tmp_buf[256]; - int i; - int len =3D 0; - int label_length_pos; - - for (i =3D 0; i < array_len; i++) - { - const char *ptr =3D str_array[i]; - - if (strlen(ptr) + len + 1 > sizeof(tmp_buf)) - { - *error =3D true; - msg(M_WARN, "write_dhcp_search_str: temp buffer overflow build= ing DHCP options"); - return; - } - /* Loop over all subdomains separated by a dot and replace the dot - * with the length of the subdomain */ - - /* label_length_pos points to the byte to be replaced by the length - * of the following domain label */ - label_length_pos =3D len++; - - while (true) - { - if (*ptr =3D=3D '.' || *ptr =3D=3D '\0') - { - tmp_buf[label_length_pos] =3D (len - label_length_pos) - 1; - label_length_pos =3D len; - if (*ptr =3D=3D '\0') - { - break; - } - } - tmp_buf[len++] =3D *ptr++; - } - /* And close off with an extra NUL char */ - tmp_buf[len++] =3D 0; - } - - if (!buf_safe(buf, 2 + len)) - { - *error =3D true; - msg(M_WARN, "write_search_dhcp_str: buffer overflow building DHCP = options"); - return; - } - if (len > 255) - { - *error =3D true; - msg(M_WARN, "write_dhcp_search_str: search domain string must be <= =3D 255 bytes"); - return; - } - - buf_write_u8(buf, type); - buf_write_u8(buf, len); - buf_write(buf, tmp_buf, len); -} - -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic pop -#endif - -static bool -build_dhcp_options_string(struct buffer *buf, const struct tuntap_options = *o) -{ - bool error =3D false; - if (o->domain) - { - write_dhcp_str(buf, 15, o->domain, &error); - } - - if (o->netbios_scope) - { - write_dhcp_str(buf, 47, o->netbios_scope, &error); - } - - if (o->netbios_node_type) - { - write_dhcp_u8(buf, 46, o->netbios_node_type, &error); - } - - write_dhcp_u32_array(buf, 6, (uint32_t *)o->dns, o->dns_len, &error); - write_dhcp_u32_array(buf, 44, (uint32_t *)o->wins, o->wins_len, &error= ); - write_dhcp_u32_array(buf, 42, (uint32_t *)o->ntp, o->ntp_len, &error); - write_dhcp_u32_array(buf, 45, (uint32_t *)o->nbdd, o->nbdd_len, &error= ); - - if (o->domain_search_list_len > 0) - { - write_dhcp_search_str(buf, 119, o->domain_search_list, o->domain_s= earch_list_len, &error); - } - - /* the MS DHCP server option 'Disable Netbios-over-TCP/IP - * is implemented as vendor option 001, value 002. - * A value of 001 means 'leave NBT alone' which is the default */ - if (o->disable_nbt) - { - if (!buf_safe(buf, 8)) - { - msg(M_WARN, "build_dhcp_options_string: buffer overflow buildi= ng DHCP options"); - return false; - } - buf_write_u8(buf, 43); - buf_write_u8(buf, 6); /* total length field */ - buf_write_u8(buf, 0x001); - buf_write_u8(buf, 4); /* length of the vendor specified field */ - buf_write_u32(buf, 0x002); - } - return !error; -} - static void fork_dhcp_action(struct tuntap *tt) { -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1265?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I62a8f62b9c0938cfcb99d184b07034515c076303 Gerrit-Change-Number: 1265 Gerrit-PatchSet: 2 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --luyMARyCUww= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 upl= oaded patch set #2 to the change originally created by flichtenhel= d.

View Change

The following approvals got outdated and were remove= d: Code-Review+2 by cron2

Move build_dhcp_options_string from=
 tun to dhcp

Seems suitably related and tun.c is one of the
huge = ones.

In preparation of adding UTs for the code.

Change-Id: I= 62a8f62b9c0938cfcb99d184b07034515c076303
Signed-off-by: Frank Lichtenhel= d <frank@...2641...>
Acked-by: Gert Doering <gert@...1296...>= ;
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1265
Message-Id:= <20251011082217.27568-1-gert@...1296...>
URL: https://sourceforge= .net/p/openvpn/mailman/message/59245240/
Signed-off-by: Gert Doering <= ;gert@...1296...>
---
M src/openvpn/dhcp.c
M src/openvpn/dhcp.h=
M src/openvpn/tun.c
3 files changed, 207 insertions(+), 196 deletion= s(-)

git pull ssh://gerrit.openvpn.net:29418/ope=
nvpn refs/changes/65/1265/2
diff --git a/src/openvpn/dhcp.c b/src/o=
penvpn/dhcp.c
index 850a4b6..653127d 100644
--- a/src/openvpn/dhcp.c
+++ b/src/openvpn/dhcp.c@@ -185,3 +185,203 @@
}
r= eturn 0;
}
+
+#if defined= (_WIN32)
+
= +#if defined(__GNUC__) || defin= ed(__clang__)
+#pragm= a GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wconversion"
+#endif
+
+/*
+ * Convert = DHCP options from the command line / config file
+ * into a raw DHCP-format options string.
+ */
+
+static void
+write_dhcp_u8(struct buffer *buf, const int type, const int data, b= ool *error)
+{=
+ if (!buf_safe(buf, 3))=
+ {
+ *error =3D true;
+ msg(M_WARN, "wr= ite_dhcp_u8: buffer overflow building DHCP options");
+ return;
+ }
+ buf_write_u8(buf, type);
+ buf_write_u8(buf, 1);
+ buf_write_u8(buf, data);
= +}
+
+static void
+= write_dhcp_u32_array(struct buffer *buf, const int type, const uint32_t *da= ta,
+ = const unsigned int len, bool *error)
+{
+ if (len > 0)
+ {
+ in= t i;
+ const i= nt size =3D len * sizeof(uint32_t);
+
+ = if (!buf_safe(buf, 2 + size))
+ {
+ *error =3D true;
+ msg(M_WARN, "write_dhcp_u32_array: buffer ov= erflow building DHCP options");
+ return;
+ }
+ if (size < 1 || size > 255)
+ {
+ *error =3D true;
+ msg(M_WARN, "write_dhcp_u32_ar= ray: size (%d) must be > 0 and <=3D 255", size);
+ return;
+ }
+ buf_write_u8(buf, type);
+ buf_write_u8(buf, size);=
+ for (i =3D = 0; i < len; ++i)
+= {
+ = buf_write_u32(buf, data[i]);
+ }
= + }
+}
<= span style=3D"color: hsl(120, 100%, 40%);">+
+static void
+write_dhcp_str(struct buffer *buf, const int type, const c= har *str, bool *error)
+ const int l= en =3D strlen(str);
+= if (!buf_safe(buf, 2 + len))
+ {
+ = *error =3D true;
+ msg(M_WARN, "write_dhcp_str: buffer overflow building DHCP= options");
+ = return;
+ }
+ if (len < 1 ||= len > 255)
+ {=
+ *error =3D = true;
+ msg(M_= WARN, "write_dhcp_str: string '%s' must be > 0 bytes and &l= t;=3D 255 bytes", str);
+ return;
+ }
+ buf_w= rite_u8(buf, type);
+= buf_write_u8(buf, len);
+ buf_write(buf, str, len);
+}
+=
+/*
+ * RFC3397 states that multiple sear= chdomains are encoded as follows:
+ * - at start the length of the entire option is given

+ * - each subdomain is p= receded by its length
+ * - each searchdomain is separated by a NUL character
+ * e.g. if you want "openvpn.net= " and "duckduckgo.com" then you end up with
+ * 0x1D 0x7 openvpn 0x3 net 0x00 0= x0A duckduckgo 0x3 com 0x00
+ */
+static v= oid
+write_dhcp_searc= h_str(struct buffer *buf, const int type, const char *const *str_array,
+ i= nt array_len, bool *error)
+{
+ char tm= p_buf[256];
+ int = i;
+ int len =3D 0= ;
+ int label_leng= th_pos;
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ for (i =3D 0; i < array_= len; i++)

+ {
+ const char *ptr = =3D str_array[i];
+
+ if (strlen(pt= r) + len + 1 > sizeof(tmp_buf))
+ {
+ *error =3D true;
+ msg(M_WARN, "write_dhcp_search_str: temp buf= fer overflow building DHCP options");
+ return;
+ }
+ /* Loop over all subdomains separated by a dot and rep= lace the dot
+ = * with the length of the subdomain */
+
= + /* label_length_pos points to the byte to be replaced by the lengt= h
+ * of the = following domain label */
+ label_length_pos =3D len++;
+
+ {
+ = if (*ptr =3D=3D '.' || *ptr =3D=3D '\0')
+ {

+ tmp_buf[label_length_p= os] =3D (len - label_length_pos) - 1;
+ label_length_pos =3D len;
+ if (*ptr =3D=3D = 9;\0')

+ = {
+ = break;
+ = }
+ = }
+ = tmp_buf[len++] =3D *ptr++;
+ }
+ = /* And close off with an extra NUL char */
+ tmp_buf[len++] =3D 0;
+ }
+
+ if (!buf_safe(buf, 2 + len))
+ {
+ *error =3D true;
+ msg(M_WARN, "write_search_dhcp_str: buffer overflow b= uilding DHCP options");
+ return;
+ }
+ if (l= en > 255)
+ {
+ *error =3D tr= ue;
+ msg(M_WA= RN, "write_dhcp_search_str: search domain string must be <=3D 255 b= ytes");
+ = return;
+ }

+
+ buf_write_u8(buf, type);
+ buf_write_u8(buf, len);<= br>+ buf_write(buf, tmp_buf,= len);
+}
<= span style=3D"color: hsl(120, 100%, 40%);">+
+#if defined(__GNUC__) || defined(__clang__)=
+#pragma GCC diagnostic pop=
+#endif
+
+bool
+build_dhcp_options_string(struct buffer *buf, const struct tuntap_= options *o)
+{=
+ bool error =3D false;<= /span>
+ if (o->domain= )
+ {
+ write_dhcp_str(buf, 15, = o->domain, &error);

+ }
+=
+ if (o->netbios_scop= e)
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ write_dhcp_str(buf, 47,= o->netbios_scope, &error);
+ }
= +
+ if (o->netb= ios_node_type)
+ {=
+ write_dhcp_= u8(buf, 46, o->netbios_node_type, &error);
+ }
+
+ = write_dhcp_u32_array(buf, 6, (uint32_t *)o->dns, o->dns_len, &er= ror);
+ write_dhcp= _u32_array(buf, 44, (uint32_t *)o->wins, o->wins_len, &error);

+ write_dhcp_u32_arr= ay(buf, 42, (uint32_t *)o->ntp, o->ntp_len, &error);
+ write_dhcp_u32_array(buf, 45= , (uint32_t *)o->nbdd, o->nbdd_len, &error);
+
+ if (o->domain_search_list_len > 0)
+ {
+ write_dhcp_search_str(buf, 119, o->do= main_search_list, o->domain_search_list_len, &error);
+ }
+
+ /* the MS DHCP server option 'Disable Netbios-over-TCP/IP
+ * is implemented= as vendor option 001, value 002.
+ * A value of 001 means 'leave NBT alone' which i= s the default */
+ = if (o->disable_nbt)
+ {
+ if= (!buf_safe(buf, 8))
= + {
+ = msg(M_WARN, "build_dhcp_options_string: buffer overflow building DH= CP options");
+ = return false;
+ }
+ = buf_write_u8(buf, 43);
+ buf_write_u8(buf, 6); /* total length field */
+ buf_write_u8(buf, 0x001);<= /span>
+ buf_write_u8= (buf, 4); /* length of the vendor specified field */
+ buf_write_u32(buf, 0x002);=
+ }
+ return !error;
+}
+
+#en= dif /* defined(_WIN32) */
diff --git a/src/openvpn/dhcp.h b= /src/openvpn/dhcp.h
index af5c2c4..8e15a39 100644--- a/src/openvpn/dhcp.h
+++ b/src/openvpn/dhcp.h

@@ -84,4 +84,10 @@

in_addr= _t dhcp_extract_router_msg(struct buffer *ipbuf);
<= br>+#if defined(_WIN32)<= br>+#include "tun.h"<= /span>
+
+bool build_dhcp_options_string(struct b= uffer *buf, const struct tuntap_options *o);
+#endif
+
#endif /* ifndef DHCP_H */
di= ff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index e35f= 889..097b9e8 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c

@@ -45,6 +45,7 @@
= #include "win32.h"
#include "wfp_block.h&q= uot;
#include "networking.h"
+#include "dhcp.h"
<= span>
#include "memdbg.h"

@@ -5554,202 +5555,6 @@
return ret;

}

-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "= -Wconversion"
-#en= dif
-
-/*
- * Convert DHCP options from the command line / config fil= e
- * into a raw DHCP-f= ormat options string.
-= */
-
-static void
-write_dhcp_u8(struct buffer *buf, const int type,= const int data, bool *error)
-{
- if (!buf= _safe(buf, 3))
- {
- *error =3D true= ;
- msg(M_WARN, = "write_dhcp_u8: buffer overflow building DHCP options");- return;
- }

- buf_write_u8(buf, type);
- buf_write_u8(buf, 1);
- buf_write_u8(buf, data);
= -}
-
-static void
-write_dh= cp_u32_array(struct buffer *buf, const int type, const uint32_t *data,
- const= unsigned int len, bool *error)
-{
- if (le= n > 0)
- {=
- int i;
- const int size =3D len * si= zeof(uint32_t);

-

- if (!buf_safe(buf, = 2 + size))
- {
- *error =3D = true;
- msg(= M_WARN, "write_dhcp_u32_array: buffer overflow building DHCP options&q= uot;);
- ret= urn;
- }<= br>- if (size < 1 || si= ze > 255)
- {=
- *error = =3D true;
- = msg(M_WARN, "write_dhcp_u32_array: size (%d) must be > 0 and <= =3D 255", size);
-= return;
- = }
- buf_wr= ite_u8(buf, type);
- = buf_write_u8(buf, size);
- for (i =3D 0; i < len; ++i)
- {
- buf_write_u32(buf, data[i]);
- }
- }
-}
-
-static void

-write_dhcp_str(struct buffer *buf, const int ty= pe, const char *str, bool *error)
-{
- cons= t int len =3D strlen(str);
- if (!buf_safe(buf, 2 + len))
- {
-= *error =3D true;
- = return;
- }=
- if (len < 1 || len &g= t; 255)
- {- *error =3D true;

- msg(M_WARN, "w= rite_dhcp_str: string '%s' must be > 0 bytes and <=3D 255 byt= es", str);
- = return;
- }=
- buf_write_u8(buf, type);=
- buf_write_u8(buf,= len);
- buf_write(b= uf, str, len);
-}
-
-/*
- * RFC3397 states that multiple searchdomains are encoded as follow= s:
- * - at start the = length of the entire option is given
- * - each subdomain is preceded by its length
- * - each searchdomain is separate= d by a NUL character

- = * e.g. if you want "openvpn.net" and "duckduckgo.com" t= hen you end up with
- *= 0x1D 0x7 openvpn 0x3 net 0x00 0x0A duckduckgo 0x3 com 0x00
- */
-static void
-write_dhcp_search_str(struct buffer *buf, const int type, const= char *const *str_array,
-{
- char tmp_buf[256];
- int i;
- int len =3D 0;
- = int label_length_pos;
- for (i =3D 0; = i < array_len; i++)
= - {
- const c= har *ptr =3D str_array[i];
-
- if (strl= en(ptr) + len + 1 > sizeof(tmp_buf))
- {
- *error =3D true;
- msg(M_WARN, "write_dhcp_search_str: temp buff= er overflow building DHCP options");
- return;
- }
- /* Loop over all subdomains separated by a dot and replace th= e dot
- * with = the length of the subdomain */
-
- /* l= abel_length_pos points to the byte to be replaced by the length
<= span style=3D"color: hsl(0, 100%, 40%);">- * of the following domai= n label */
- lab= el_length_pos =3D len++;
- while (tru= e)
- {- if (*ptr =3D=3D = 9;.' || *ptr =3D=3D '\0')
- {
- tmp_buf[label_length_pos] =3D (len - label_length_p= os) - 1;
- = label_length_pos =3D len;
- if (*ptr =3D=3D '\0')
- {
- break;
- }
- }
- tmp_buf[len++] =3D *ptr++;
- }
- /* And close off with an extra NUL char *= /
- tmp_buf[len+= +] =3D 0;
- }=
-
- if (!buf_safe(buf, 2 + len))
- {
- *error =3D true;
- msg(M_WARN, "write_search_dhcp_str: bu= ffer overflow building DHCP options");
- return;
- }
-= if (len > 255)
-= {
- *error = =3D true;
- msg(= M_WARN, "write_dhcp_search_str: search domain string must be <=3D 2= 55 bytes");
- = return;
- }

-
- buf_write_u8(buf, type);
- buf_write_u8(buf, len);
- buf_write(buf, tmp_buf, len);
-}
-
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
-static bool
-build_dh= cp_options_string(struct buffer *buf, const struct tuntap_options *o)

-{
- bool error =3D false;
- if (o->domain)
- {
- write_dhcp_str(buf, 15, o->domain, &error);<= /span>
- }
-
- if (o->netbios_scope)
- {
- write_dhcp_str(buf, 47, o->netbios_scope, &error);
- }
-
- if (o->netbios_node_type)
- {
- write_dhcp_u8(buf, 46, o->netbios_node_type, &error= );
- }
-

- write_dhcp_u32_array(buf, 6, (uint32_t *)o->dns, = o->dns_len, &error);
- write_dhcp_u32_array(buf, 44, (uint32_t *)o->wins, o->wins_= len, &error);
- = write_dhcp_u32_array(buf, 42, (uint32_t *)o->ntp, o->ntp_len, &er= ror);
- write_dhcp_u= 32_array(buf, 45, (uint32_t *)o->nbdd, o->nbdd_len, &error);
-
- if (o->domain_search_list_len > 0)
- {
- write_dhcp_search_str(buf, 119, = o->domain_search_list, o->domain_search_list_len, &error);=
- }
-
- /* the MS DHCP server option 'Disable Netbios-over-TCP/I= P
- * is implemente= d as vendor option 001, value 002.
- * A value of 001 means 'leave NBT alone' which is= the default */
- if= (o->disable_nbt)
- = {
- if (!buf_= safe(buf, 8))
- = {
- msg(M_WA= RN, "build_dhcp_options_string: buffer overflow building DHCP options&= quot;);
- re= turn false;
- }<= /span>
- buf_write_u8(b= uf, 43);
- buf_w= rite_u8(buf, 6); /* total length field */
- buf_write_u8(buf, 0x001);
- buf_write_u8(buf, 4); /* length of = the vendor specified field */
- buf_write_u32(buf, 0x002);
- }
- return !error;
-}
-
= static void
fork_dhcp_action(struct tuntap *tt)
= {

To view, visit change 1265. To uns= ubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I62a8f62b9c0938cfcb99d184b07= 034515c076303
Gerrit-Change-Number: 1265
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --luyMARyCUww=--