[Openvpn-devel,v1] options: share the plain --dns server lookup
Commit Message
From: Razvan Cojocaru <razvanc@mailbox.org>
tuntap_options_postprocess_dns() and dhcp_options_postprocess_dns() had
identical loops to find the first server without a non-plain transport,
DNSSEC or a non-standard port. Move them to first_plain_dns_server().
Change-Id: Ia7a1b164810c9304c72d1fe2ae66b2fa9a5ade2d
Signed-off-by: Razvan Cojocaru <razvanc@mailbox.org>
Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2371
---
This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2371
This mail reflects revision 1 of this Change.
Acked-by according to Gerrit (reflected above):
Frank Lichtenheld <frank@lichtenheld.com>
@@ -2727,6 +2727,35 @@
}
}
+/**
+ * Return the first server that needs no more than plain DNS on port 53,
+ * or NULL if there is none.
+ */
+static const struct dns_server *
+first_plain_dns_server(const struct dns_server *s)
+{
+ for (; s; s = s->next)
+ {
+ bool non_standard_server_port = false;
+ for (size_t i = 0; i < s->addr_count; ++i)
+ {
+ if (s->addr[i].port && s->addr[i].port != 53)
+ {
+ non_standard_server_port = true;
+ break;
+ }
+ }
+ if ((s->transport && s->transport != DNS_TRANSPORT_PLAIN)
+ || (s->dnssec && s->dnssec != DNS_SECURITY_NO) || non_standard_server_port)
+ {
+ /* Skip servers requiring unsupported config to be set */
+ continue;
+ }
+ return s;
+ }
+ return NULL;
+}
+
#if defined(_WIN32) || defined(TARGET_ANDROID)
/**
* @brief Postprocess DNS related settings
@@ -2793,49 +2822,30 @@
tt->dns_len = 0;
tt->dns6_len = 0;
- const struct dns_server *s = dns->servers;
- while (s)
+ const struct dns_server *s = first_plain_dns_server(dns->servers);
+ if (s)
{
- bool non_standard_server_port = false;
+ bool overflow = false;
for (size_t i = 0; i < s->addr_count; ++i)
{
- if (s->addr[i].port && s->addr[i].port != 53)
+ if (s->addr[i].family == AF_INET && tt->dns_len + 1 < N_DHCP_ADDR)
{
- non_standard_server_port = true;
- break;
+ tt->dns[tt->dns_len++] = ntohl(s->addr[i].in.a4.s_addr);
+ }
+ else if (tt->dns6_len + 1 < N_DHCP_ADDR)
+ {
+ tt->dns6[tt->dns6_len++] = s->addr[i].in.a6;
+ }
+ else
+ {
+ overflow = true;
}
}
- if ((s->transport && s->transport != DNS_TRANSPORT_PLAIN)
- || (s->dnssec && s->dnssec != DNS_SECURITY_NO) || non_standard_server_port)
+ if (overflow)
{
- /* Skip servers requiring unsupported config to be set */
- s = s->next;
+ msg(M_WARN, "WARNING: couldn't copy all --dns server addresses to TUN/TAP");
}
- else
- {
- bool overflow = false;
- for (size_t i = 0; i < s->addr_count; ++i)
- {
- if (s->addr[i].family == AF_INET && tt->dns_len + 1 < N_DHCP_ADDR)
- {
- tt->dns[tt->dns_len++] = ntohl(s->addr[i].in.a4.s_addr);
- }
- else if (tt->dns6_len + 1 < N_DHCP_ADDR)
- {
- tt->dns6[tt->dns6_len++] = s->addr[i].in.a6;
- }
- else
- {
- overflow = true;
- }
- }
- if (overflow)
- {
- msg(M_WARN, "WARNING: couldn't copy all --dns server addresses to TUN/TAP");
- }
- tt->dhcp_options |= DHCP_OPTIONS_DHCP_OPTIONAL;
- return;
- }
+ tt->dhcp_options |= DHCP_OPTIONS_DHCP_OPTIONAL;
}
}
@@ -2948,43 +2958,24 @@
d = d->next;
}
- const struct dns_server *s = dns->servers;
- while (s)
+ const struct dns_server *s = first_plain_dns_server(dns->servers);
+ if (s)
{
- bool non_standard_server_port = false;
for (size_t i = 0; i < s->addr_count; ++i)
{
- if (s->addr[i].port && s->addr[i].port != 53)
+ const char *option;
+ const char *value;
+ if (s->addr[i].family == AF_INET)
{
- non_standard_server_port = true;
- break;
+ option = "DNS";
+ value = print_in_addr_t(s->addr[i].in.a4.s_addr, IA_NET_ORDER, &gc);
}
- }
- if ((s->transport && s->transport != DNS_TRANSPORT_PLAIN)
- || (s->dnssec && s->dnssec != DNS_SECURITY_NO) || non_standard_server_port)
- {
- /* Skip servers requiring unsupported config to be set */
- s = s->next;
- }
- else
- {
- for (size_t i = 0; i < s->addr_count; ++i)
+ else
{
- const char *option;
- const char *value;
- if (s->addr[i].family == AF_INET)
- {
- option = "DNS";
- value = print_in_addr_t(s->addr[i].in.a4.s_addr, IA_NET_ORDER, &gc);
- }
- else
- {
- option = "DNS6";
- value = print_in6_addr(s->addr[i].in.a6, 0, &gc);
- }
- setenv_foreign_option(o, option, value, es);
+ option = "DNS6";
+ value = print_in6_addr(s->addr[i].in.a6, 0, &gc);
}
- break;
+ setenv_foreign_option(o, option, value, es);
}
}
}