Move a few internal constants out of resolv_params.h
This is a preliminary step to cleanly separate the public headers
from the internal ones. No funtionality changes.
In the process, I also un-ifdefd RES_USE_EDNS0 and EDNS0_PADDING, since
they were always defined.
For now I'm leaving alone these #ifdefs, which need some more thought:
- HAVE_SIN6_SCOPE_ID: currently disabled, but <linux/in6.h> has it
- HAS_INET6_STRUCTS: currently disabled, will need test coverage?
- CANNOT_CONNECT_DGRAM: currently disabled
Test: build, flash, then 'atest netd_integration_test'
Change-Id: I4cee37d484a5183a2db082d502b2badcaa83e7e4
diff --git a/resolv/res_cache.cpp b/resolv/res_cache.cpp
index 39a0c1e..880037d 100644
--- a/resolv/res_cache.cpp
+++ b/resolv/res_cache.cpp
@@ -187,6 +187,13 @@
* printf( "%s", buff );
*/
+/* Defaults used for initializing __res_params */
+
+// If successes * 100 / total_samples is less than this value, the server is considered failing
+#define SUCCESS_THRESHOLD 75
+// Sample validity in seconds. Set to -1 to disable skipping failing servers.
+#define NSSAMPLE_VALIDITY 1800
+
/* add a char to a bounded buffer */
static char* bprint_c(char* p, char* end, int c) {
if (p < end) {
@@ -1740,7 +1747,7 @@
return cache_info;
}
-void _resolv_set_default_params(struct __res_params* params) {
+static void resolv_set_default_params(struct __res_params* params) {
params->sample_validity = NSSAMPLE_VALIDITY;
params->success_threshold = SUCCESS_THRESHOLD;
params->min_samples = 0;
@@ -1791,7 +1798,7 @@
if (params != NULL) {
cache_info->params = *params;
} else {
- _resolv_set_default_params(&cache_info->params);
+ resolv_set_default_params(&cache_info->params);
}
if (!resolv_is_nameservers_equal_locked(cache_info, servers, numservers)) {
diff --git a/resolv/res_init.cpp b/resolv/res_init.cpp
index 808f44c..d222ac2 100644
--- a/resolv/res_init.cpp
+++ b/resolv/res_init.cpp
@@ -269,11 +269,9 @@
} else if (!strncmp(cp, "no-check-names", sizeof("no-check-names") - 1)) {
statp->options |= RES_NOCHECKNAME;
}
-#ifdef RES_USE_EDNS0
else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
statp->options |= RES_USE_EDNS0;
}
-#endif
else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
statp->options |= RES_USE_DNAME;
} else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
diff --git a/resolv/res_mkquery.cpp b/resolv/res_mkquery.cpp
index f747fce..1f2879d 100644
--- a/resolv/res_mkquery.cpp
+++ b/resolv/res_mkquery.cpp
@@ -70,12 +70,13 @@
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <algorithm> // std::min()
+
#include <arpa/nameser.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
-#include <sys/param.h>
#include <sys/types.h>
#include "resolv_private.h"
@@ -85,6 +86,10 @@
#define DEBUG
#endif
+// Queries will be padded to a multiple of this length when EDNS0 is active.
+constexpr uint16_t kEdns0Padding = 128;
+
+// Defined in res_data.cpp
extern const char* _res_opcodes[];
/*
@@ -150,7 +155,7 @@
n = dn_comp((const char*) data, cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
if (n < 0) return (-1);
cp += n;
- ns_put16(T_NULL, cp);
+ ns_put16(ns_t_null, cp);
cp += INT16SZ;
ns_put16(cl, cp);
cp += INT16SZ;
@@ -188,12 +193,6 @@
return (cp - buf);
}
-#ifdef RES_USE_EDNS0
-/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
-#ifndef T_OPT
-#define T_OPT 41
-#endif
-
int res_nopt(res_state statp, int n0, /* current offset in buffer */
u_char* buf, /* buffer to put query */
int buflen, /* size of buffer */
@@ -215,7 +214,8 @@
*cp++ = 0; /* "." */
- ns_put16(T_OPT, cp); /* TYPE */
+ // Attach OPT pseudo-RR, as documented in RFC2671 (EDNS0).
+ ns_put16(ns_t_opt, cp); /* TYPE */
cp += INT16SZ;
if (anslen > 0xffff) anslen = 0xffff;
ns_put16(anslen, cp); /* CLASS = UDP payload size */
@@ -230,30 +230,24 @@
}
ns_put16(flags, cp);
cp += INT16SZ;
-#ifdef EDNS0_PADDING
- {
- u_int16_t minlen = (cp - buf) + 3 * INT16SZ;
- u_int16_t extra = minlen % EDNS0_PADDING;
- u_int16_t padlen = (EDNS0_PADDING - extra) % EDNS0_PADDING;
- if (minlen > buflen) {
- return (-1);
- }
- padlen = MIN(padlen, buflen - minlen);
- ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */
- cp += INT16SZ;
- ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */
- cp += INT16SZ;
- ns_put16(padlen, cp); /* OPTION-LENGTH */
- cp += INT16SZ;
- memset(cp, 0, padlen);
- cp += padlen;
- }
-#else
- ns_put16(0, cp); /* RDLEN */
- cp += INT16SZ;
-#endif
- hp->arcount = htons(ntohs(hp->arcount) + 1);
+ // EDNS0 padding
+ const uint16_t minlen = static_cast<uint16_t>(cp - buf) + 3 * INT16SZ;
+ const uint16_t extra = minlen % kEdns0Padding;
+ uint16_t padlen = (kEdns0Padding - extra) % kEdns0Padding;
+ if (minlen > buflen) {
+ return -1;
+ }
+ padlen = std::min(padlen, static_cast<uint16_t>(buflen - minlen));
+ ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */
+ cp += INT16SZ;
+ ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */
+ cp += INT16SZ;
+ ns_put16(padlen, cp); /* OPTION-LENGTH */
+ cp += INT16SZ;
+ memset(cp, 0, padlen);
+ cp += padlen;
+
+ hp->arcount = htons(ntohs(hp->arcount) + 1);
return (cp - buf);
}
-#endif
diff --git a/resolv/res_query.cpp b/resolv/res_query.cpp
index dd6b1fb..0b46409 100644
--- a/resolv/res_query.cpp
+++ b/resolv/res_query.cpp
@@ -126,11 +126,9 @@
#endif
n = res_nmkquery(statp, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
-#ifdef RES_USE_EDNS0
if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
(statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U)
n = res_nopt(statp, n, buf, sizeof(buf), anslen);
-#endif
if (n <= 0) {
#ifdef DEBUG
if (statp->options & RES_DEBUG) printf(";; res_query: mkquery failed\n");
@@ -140,7 +138,6 @@
}
n = res_nsend(statp, buf, n, answer, anslen);
if (n < 0) {
-#ifdef RES_USE_EDNS0
/* if the query choked with EDNS0, retry without EDNS0 */
if ((statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U &&
((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
@@ -148,7 +145,6 @@
if (statp->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
goto again;
}
-#endif
#ifdef DEBUG
if (statp->options & RES_DEBUG) printf(";; res_query: send error\n");
#endif
diff --git a/resolv/res_send.cpp b/resolv/res_send.cpp
index 86db060..95605f4 100644
--- a/resolv/res_send.cpp
+++ b/resolv/res_send.cpp
@@ -1145,7 +1145,6 @@
(stdout, ";; not our server:\n"), ans, (resplen > anssiz) ? anssiz : resplen);
goto retry;
}
-#ifdef RES_USE_EDNS0
if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
/*
* Do not retry if the server do not understand EDNS0.
@@ -1159,7 +1158,6 @@
res_nclose(statp);
return (0);
}
-#endif
if (!(statp->options & RES_INSECURE2) &&
!res_queriesmatch(buf, buf + buflen, ans, ans + anssiz)) {
/*
diff --git a/resolv/resolv_params.h b/resolv/resolv_params.h
index d2f058b..4930d74 100644
--- a/resolv/resolv_params.h
+++ b/resolv/resolv_params.h
@@ -25,21 +25,6 @@
#define MAXDNSRCHPATH 256 /* max length of domain search paths */
#define MAXNSSAMPLES 64 /* max # samples to store per server */
-/* Defaults used for initializing __res_params */
-#define SUCCESS_THRESHOLD \
- 75 /* if successes * 100 / total_samples is less than \
- * this value, the server is considered failing \
- */
-#define NSSAMPLE_VALIDITY \
- 1800 /* Sample validity in seconds. \
- * Set to -1 to disable skipping failing \
- * servers. \
- */
-
-/* If EDNS0_PADDING is defined, queries will be padded to a multiple of this length
-when EDNS0 is active. */
-#define EDNS0_PADDING 128
-
/* per-netid configuration parameters passed from netd to the resolver */
struct __res_params {
uint16_t sample_validity; // sample lifetime in s