[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH 1/3] nslookup: add support for DNS query class
From:       Henrique de Moraes Holschuh <henrique () nic ! br>
Date:       2021-12-08 14:19:58
Message-ID: db663e4148fd135fb06f0870d2af6aebf0da6ff5.1629297592.git.henrique () nic ! br
[Download RAW message or body]

There is more (although not *much* more) to the DNS than class IN, and
the major implementations of nslookup do support the other query
classes.

Class CH is relevant for dnsops, e.g. when dealing with DNS anycast (see
RFC7108) and looking for specific DNS server information (such as the
classic "version.bind" query).

This is the full-feature proposal (supports also HS and ANY query
classes) with better code readability and no increase in bss usage.

make bloatcheck against master, after this commit is applied:

function                                             old     new   delta
nslookup_main                                        848     959    +111
qclasses                                               -      32     +32
.rodata                                            95434   95465     +31
add_query_with_search                                141     160     +19
packed_usage                                       34128   34140     +12
add_query                                            113     117      +4
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 5/0 up/down: 209/0)             Total: 209 bytes

Signed-off-by: Henrique de Moraes Holschuh <henrique@nic.br>
---
 networking/nslookup.c | 49 +++++++++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 13 deletions(-)

diff --git a/networking/nslookup.c b/networking/nslookup.c
index de7b5c0e7..35e29eaae 100644
--- a/networking/nslookup.c
+++ b/networking/nslookup.c
@@ -21,7 +21,7 @@
 //kbuild:lib-$(CONFIG_NSLOOKUP) += nslookup.o
 
 //usage:#define nslookup_trivial_usage
-//usage:       IF_FEATURE_NSLOOKUP_BIG("[-type=QUERY_TYPE] [-debug] ") "HOST \
[DNS_SERVER]" +//usage:       IF_FEATURE_NSLOOKUP_BIG("[-class=QUERY_CLASS] \
[-type=QUERY_TYPE] [-debug] ") "HOST [DNS_SERVER]"  //usage:#define \
nslookup_full_usage "\n\n"  //usage:       "Query DNS about HOST"
 //usage:       IF_FEATURE_NSLOOKUP_BIG("\n")
@@ -270,6 +270,16 @@ struct query {
 //	unsigned char reply[512];
 };
 
+static const struct {
+	int class;
+	char name[3];
+} qclasses[] ALIGN1 = {
+	{ C_IN,    "IN"  },
+	{ C_CHAOS, "CH"  },
+	{ C_HS,    "HS"  },
+	{ C_ANY,   "ANY" },
+};
+
 static const struct {
 	unsigned char type;
 	char name[7];
@@ -772,7 +782,7 @@ static void parse_resolvconf(void)
 		G.search = NULL;
 }
 
-static void add_query(int type, const char *dname)
+static void add_query(int class, int type, const char *dname)
 {
 	struct query *new_q;
 	unsigned count;
@@ -783,10 +793,10 @@ static void add_query(int type, const char *dname)
 	G.query = xrealloc_vector(G.query, /*4=2^2:*/ 2, count);
 	new_q = &G.query[count];
 
-	dbg("new query#%u type %u for '%s'\n", count, type, dname);
+	dbg("new query#%u class %u type %u for '%s'\n", count, class, type, dname);
 	new_q->name = dname;
 
-	qlen = res_mkquery(QUERY, dname, C_IN, type,
+	qlen = res_mkquery(QUERY, dname, class, type,
 			/*data:*/ NULL, /*datalen:*/ 0,
 			/*newrr:*/ NULL,
 			new_q->query, sizeof(new_q->query)
@@ -794,12 +804,12 @@ static void add_query(int type, const char *dname)
 	new_q->qlen = qlen;
 }
 
-static void add_query_with_search(int type, const char *dname)
+static void add_query_with_search(int class, int type, const char *dname)
 {
 	char *s;
 
 	if (type == T_PTR || !G.search || strchr(dname, '.')) {
-		add_query(type, dname);
+		add_query(class, type, dname);
 		return;
 	}
 
@@ -809,7 +819,7 @@ static void add_query_with_search(int type, const char *dname)
 
 		e = skip_non_whitespace(s);
 		fullname = xasprintf("%s.%.*s", dname, (int)(e - s), s);
-		add_query(type, fullname);
+		add_query(class, type, fullname);
 		s = skip_whitespace(e);
 		if (!*s)
 			break;
@@ -852,6 +862,7 @@ int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int nslookup_main(int argc UNUSED_PARAM, char **argv)
 {
 	unsigned types;
+	int qclass;
 	int rc;
 	int err;
 
@@ -861,6 +872,7 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
 	 * if they precede the arguments and are prefixed with a hyphen."
 	 */
 	types = 0;
+	qclass = C_IN;
 	argv++;
 	for (;;) {
 		const char *options =
@@ -893,8 +905,9 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
 			"port\0"      /* 2 */
 			"retry\0"     /* 3 */
 			"debug\0"     /* 4 */
+			"class\0"     /* 5 */
 			"t\0" /* disambiguate with "type": else -t=2 fails */
-			"timeout\0"   /* 6 */
+			"timeout\0"   /* 7 */
 			"";
 		int i;
 		char *arg;
@@ -935,7 +948,17 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
 		if (i == 4) {
 			option_mask32 |= OPT_debug;
 		}
-		if (i > 4) {
+		if (i == 5) {
+			for (i = 0;; i++) {
+				if (i >= ARRAY_SIZE(qclasses))
+					bb_error_msg_and_die("invalid query class \"%s\"", val);
+				if (strcasecmp(qclasses[i].name, val) == 0) {
+					qclass = qclasses[i].class;
+					break;
+				}
+			}
+		}
+		if (i > 5) {
 			G.default_timeout = xatou_range(val, 1, INT_MAX / 1000);
 		}
 	}
@@ -963,18 +986,18 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
 
 		ptr = make_ptr(argv[0]);
 		if (ptr) {
-			add_query(T_PTR, ptr);
+			add_query(qclass, T_PTR, ptr);
 		} else {
-			add_query_with_search(T_A, argv[0]);
+			add_query_with_search(qclass, T_A, argv[0]);
 #if ENABLE_FEATURE_IPV6
-			add_query_with_search(T_AAAA, argv[0]);
+			add_query_with_search(qclass, T_AAAA, argv[0]);
 #endif
 		}
 	} else {
 		int c;
 		for (c = 0; c < ARRAY_SIZE(qtypes); c++) {
 			if (types & (1 << c))
-				add_query_with_search(qtypes[c].type, argv[0]);
+				add_query_with_search(qclass, qtypes[c].type, argv[0]);
 		}
 	}
 
-- 
2.20.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic