Delete dead code in res_comp.cpp and misc cleanups
No functionality changes, this is mostly meant to improve our code
coverage and cleanup our headers a bit.
- delete dead functions res_ownok() and res_mailok()
- Move prototypes of the remaining functions to the new header res_comp.h
- Replace resolv_private.h with res_comp.h as needed
- Drop the dependency on Bionic's <resolv.h> wherever possible
- Eliminate the horrible maybe_XXX() macros
- Make res_hnok() and res_dnok() return a proper bool instead of 0 and 1
Change-Id: Ic2e27753355d873925044a7561ccb78bd2c2d162
diff --git a/DnsProxyListener.cpp b/DnsProxyListener.cpp
index 4eef162..2046c34 100644
--- a/DnsProxyListener.cpp
+++ b/DnsProxyListener.cpp
@@ -24,6 +24,7 @@
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
+#include <resolv.h> // b64_pton()
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
diff --git a/getaddrinfo.cpp b/getaddrinfo.cpp
index d5ec682..3683920 100644
--- a/getaddrinfo.cpp
+++ b/getaddrinfo.cpp
@@ -56,6 +56,8 @@
#include <android-base/logging.h>
#include "netd_resolv/resolv.h"
+#include "res_comp.h"
+#include "res_debug.h"
#include "res_init.h"
#include "resolv_cache.h"
#include "resolv_private.h"
@@ -848,7 +850,6 @@
int type, ancount, qdcount;
int haveanswer, had_error;
char tbuf[MAXDNAME];
- int (*name_ok)(const char*);
char hostbuf[8 * 1024];
assert(qname != NULL);
@@ -858,6 +859,8 @@
canonname = NULL;
eom = answer.data() + anslen;
+
+ bool (*name_ok)(const char* dn);
switch (qtype) {
case T_A:
case T_AAAA:
diff --git a/gethnamaddr.cpp b/gethnamaddr.cpp
index 6dcd358..23f94bb 100644
--- a/gethnamaddr.cpp
+++ b/gethnamaddr.cpp
@@ -74,9 +74,10 @@
#include "hostent.h"
#include "netd_resolv/resolv.h"
+#include "res_comp.h"
+#include "res_debug.h" // p_class(), p_type()
#include "res_init.h"
#include "resolv_cache.h"
-#include "resolv_private.h"
#include "stats.pb.h"
using android::net::NetworkDnsEventReported;
@@ -91,10 +92,6 @@
#define ALIGNBYTES (sizeof(uintptr_t) - 1)
#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
-#define maybe_ok(res, nm, ok) ((ok)(nm) != 0)
-#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
-#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
-
constexpr int MAXADDRS = 35;
typedef union {
@@ -142,7 +139,6 @@
char tbuf[MAXDNAME];
char* addr_ptrs[MAXADDRS];
const char* tname;
- int (*name_ok)(const char*);
std::vector<char*> aliases;
_DIAGASSERT(answer != NULL);
@@ -151,6 +147,8 @@
tname = qname;
hent->h_name = NULL;
eom = answer->buf + anslen;
+
+ bool (*name_ok)(const char* dn);
switch (qtype) {
case T_A:
case T_AAAA:
@@ -177,7 +175,7 @@
if (qdcount != 1) goto no_recovery;
n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
- if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
+ if ((n < 0) || !name_ok(bp)) goto no_recovery;
BOUNDED_INCR(n + QFIXEDSZ);
if (qtype == T_A || qtype == T_AAAA) {
@@ -198,7 +196,7 @@
had_error = 0;
while (ancount-- > 0 && cp < eom && !had_error) {
n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
- if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
+ if ((n < 0) || !name_ok(bp)) {
had_error++;
continue;
}
@@ -219,7 +217,7 @@
}
if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
- if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
+ if ((n < 0) || !name_ok(tbuf)) {
had_error++;
continue;
}
@@ -246,7 +244,7 @@
}
if (qtype == T_PTR && type == T_CNAME) {
n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
- if (n < 0 || !maybe_dnok(res, tbuf)) {
+ if (n < 0 || !res_dnok(tbuf)) {
had_error++;
continue;
}
@@ -279,7 +277,7 @@
continue; /* XXX - had_error++ ? */
}
n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
- if ((n < 0) || !maybe_hnok(res, bp)) {
+ if ((n < 0) || !res_hnok(bp)) {
had_error++;
break;
}
diff --git a/res_cache.cpp b/res_cache.cpp
index c2030ad..5f87cca 100644
--- a/res_cache.cpp
+++ b/res_cache.cpp
@@ -59,6 +59,7 @@
#include <server_configurable_flags/get_flags.h>
#include "DnsStats.h"
+#include "res_comp.h"
#include "res_debug.h"
#include "resolv_private.h"
#include "util.h"
diff --git a/res_comp.cpp b/res_comp.cpp
index c9889cb..9a191fa 100644
--- a/res_comp.cpp
+++ b/res_comp.cpp
@@ -70,14 +70,13 @@
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include "res_comp.h"
+
#include <arpa/nameser.h>
-#include <ctype.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
-#include "resolv_private.h"
-
/*
* Expand compressed domain name 'src' to full domain name.
* 'msg' is a pointer to the begining of the message,
@@ -89,7 +88,7 @@
int n = ns_name_uncompress(msg, eom, src, dst, (size_t) dstsiz);
if (n > 0 && dst[0] == '.') dst[0] = '\0';
- return (n);
+ return n;
}
/*
@@ -141,7 +140,7 @@
#define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
#define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
-int res_hnok(const char* dn) {
+bool res_hnok(const char* dn) {
int pch = PERIOD, ch = *dn++;
while (ch != '\0') {
@@ -150,60 +149,25 @@
if (periodchar(ch)) {
;
} else if (periodchar(pch)) {
- if (!borderchar(ch)) return (0);
+ if (!borderchar(ch)) return false;
} else if (periodchar(nch) || nch == '\0') {
- if (!borderchar(ch)) return (0);
+ if (!borderchar(ch)) return false;
} else {
- if (!middlechar(ch)) return (0);
+ if (!middlechar(ch)) return false;
}
pch = ch, ch = nch;
}
- return (1);
-}
-
-/*
- * hostname-like (A, MX, WKS) owners can have "*" as their first label
- * but must otherwise be as a host name.
- */
-int res_ownok(const char* dn) {
- if (asterchar(dn[0])) {
- if (periodchar(dn[1])) return (res_hnok(dn + 2));
- if (dn[1] == '\0') return (1);
- }
- return (res_hnok(dn));
-}
-
-/*
- * SOA RNAMEs and RP RNAMEs can have any printable character in their first
- * label, but the rest of the name has to look like a host name.
- */
-int res_mailok(const char* dn) {
- int ch, escaped = 0;
-
- /* "." is a valid missing representation */
- if (*dn == '\0') return (1);
-
- /* otherwise <label>.<hostname> */
- while ((ch = *dn++) != '\0') {
- if (!domainchar(ch)) return (0);
- if (!escaped && periodchar(ch)) break;
- if (escaped)
- escaped = 0;
- else if (bslashchar(ch))
- escaped = 1;
- }
- if (periodchar(ch)) return (res_hnok(dn));
- return (0);
+ return true;
}
/*
* This function is quite liberal, since RFC 1034's character sets are only
* recommendations.
*/
-int res_dnok(const char* dn) {
+bool res_dnok(const char* dn) {
int ch;
while ((ch = *dn++) != '\0')
- if (!domainchar(ch)) return (0);
- return (1);
+ if (!domainchar(ch)) return false;
+ return true;
}
diff --git a/res_comp.h b/res_comp.h
new file mode 100644
index 0000000..6022838
--- /dev/null
+++ b/res_comp.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <stdint.h>
+
+int dn_expand(const uint8_t* msg, const uint8_t* eom, const uint8_t* src, char* dst, int dstsiz);
+int dn_comp(const char* src, uint8_t* dst, int dstsiz, uint8_t** dnptrs, uint8_t** lastdnptr);
+int dn_skipname(const uint8_t* ptr, const uint8_t* eom);
+
+bool res_hnok(const char* dn);
+bool res_dnok(const char* dn);
diff --git a/res_mkquery.cpp b/res_mkquery.cpp
index fb8a074..b95d31f 100644
--- a/res_mkquery.cpp
+++ b/res_mkquery.cpp
@@ -80,8 +80,11 @@
#include <string.h>
#include <android-base/logging.h>
+#include <netd_resolv/resolv.h> // NET_CONTEXT_FLAG_USE_DNS_OVER_TLS
-#include "resolv_private.h"
+#include "res_comp.h"
+#include "res_debug.h"
+#include "resolv_private.h" // res_state
// Queries will be padded to a multiple of this length when EDNS0 is active.
constexpr uint16_t kEdns0Padding = 128;
diff --git a/res_send.cpp b/res_send.cpp
index 831f27b..66548bd 100644
--- a/res_send.cpp
+++ b/res_send.cpp
@@ -104,6 +104,8 @@
#include "PrivateDnsConfiguration.h"
#include "netd_resolv/resolv.h"
#include "private/android_filesystem_config.h"
+
+#include "res_comp.h"
#include "res_debug.h"
#include "res_init.h"
#include "resolv_cache.h"
diff --git a/resolv_private.h b/resolv_private.h
index 384517b..a062c1a 100644
--- a/resolv_private.h
+++ b/resolv_private.h
@@ -50,7 +50,6 @@
#include <android-base/logging.h>
#include <net/if.h>
-#include <resolv.h>
#include <time.h>
#include <string>
#include <vector>
@@ -138,14 +137,6 @@
extern const char* const _res_opcodes[];
-int res_hnok(const char*);
-int res_ownok(const char*);
-int res_mailok(const char*);
-int res_dnok(const char*);
-int dn_skipname(const uint8_t*, const uint8_t*);
-void putlong(uint32_t, uint8_t*);
-void putshort(uint16_t, uint8_t*);
-
int res_nameinquery(const char*, int, int, const uint8_t*, const uint8_t*);
int res_queriesmatch(const uint8_t*, const uint8_t*, const uint8_t*, const uint8_t*);