busybox: nslookup_lede: mimic output format of old Busybox applet

When invoking "nslookup_lede" with a domain argument and without explicit
query type, issue both A and AAAA queries and display the resulting IP
addresses in a numbered list style, similar to how the old BusyBox nslookup
used to output the records.

This is required for compatibility with certain scripts.

Ref: https://forum.lede-project.org/t/nslookup-ipv6-in-lede-17-01-1

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2017-04-25 14:21:08 +02:00
parent f1e3285461
commit 76871a8dbb
2 changed files with 61 additions and 22 deletions

View file

@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=busybox
PKG_VERSION:=1.26.2
PKG_RELEASE:=3
PKG_RELEASE:=4
PKG_FLAGS:=essential
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.bz2

View file

@ -1,4 +1,4 @@
From 1188f159e0b0a9c07bed835c7313bedf5322a9e3 Mon Sep 17 00:00:00 2001
From ab0f8bb80527928f513297ab93e3ec8c8b48dd50 Mon Sep 17 00:00:00 2001
From: Jo-Philipp Wich <jo@mein.io>
Date: Tue, 14 Mar 2017 22:21:34 +0100
Subject: [PATCH] networking: add LEDE nslookup applet
@ -12,16 +12,34 @@ and the libresolv primitives to parse received DNS responses.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
---
networking/nslookup_lede.c | 894 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 894 insertions(+)
Makefile.flags | 6 +
networking/nslookup_lede.c | 915 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 921 insertions(+)
create mode 100644 networking/nslookup_lede.c
diff --git a/Makefile.flags b/Makefile.flags
index 65021de25..096ab7756 100644
--- a/Makefile.flags
+++ b/Makefile.flags
@@ -134,6 +134,12 @@ else
LDLIBS += m
endif
+# nslookup_lede might need the resolv library
+RESOLV_AVAILABLE := $(shell echo 'int main(void){res_init();return 0;}' >resolvtest.c; $(CC) $(CFLAGS) -include resolv.h -lresolv -o /dev/null resolvtest.c >/dev/null 2>&1 && echo "y"; rm resolvtest.c)
+ifeq ($(RESOLV_AVAILABLE),y)
+LDLIBS += resolv
+endif
+
# libpam may use libpthread, libdl and/or libaudit.
# On some platforms that requires an explicit -lpthread, -ldl, -laudit.
# However, on *other platforms* it fails when some of those flags
diff --git a/networking/nslookup_lede.c b/networking/nslookup_lede.c
new file mode 100644
index 000000000..f3e42d3bb
index 000000000..c6c90ddf3
--- /dev/null
+++ b/networking/nslookup_lede.c
@@ -0,0 +1,894 @@
@@ -0,0 +1,915 @@
+/*
+ * nslookup_lede - musl compatible replacement for busybox nslookup
+ *
@ -145,7 +163,7 @@ index 000000000..f3e42d3bb
+static unsigned int default_timeout = 5;
+
+
+static int parse_reply(const unsigned char *msg, size_t len)
+static int parse_reply(const unsigned char *msg, size_t len, int *bb_style_counter)
+{
+ ns_msg handle;
+ ns_rr rr;
@ -165,6 +183,9 @@ index 000000000..f3e42d3bb
+ return -1;
+ }
+
+ if (bb_style_counter && *bb_style_counter == 1)
+ printf("Name: %s\n", ns_rr_name(rr));
+
+ rdlen = ns_rr_rdlen(rr);
+
+ switch (ns_rr_type(rr))
@ -175,7 +196,10 @@ index 000000000..f3e42d3bb
+ return -1;
+ }
+ inet_ntop(AF_INET, ns_rr_rdata(rr), astr, sizeof(astr));
+ printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr);
+ if (bb_style_counter)
+ printf("Address %d: %s\n", (*bb_style_counter)++, astr);
+ else
+ printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr);
+ break;
+
+#if ENABLE_FEATURE_IPV6
@ -185,7 +209,10 @@ index 000000000..f3e42d3bb
+ return -1;
+ }
+ inet_ntop(AF_INET6, ns_rr_rdata(rr), astr, sizeof(astr));
+ printf("%s\thas AAAA address %s\n", ns_rr_name(rr), astr);
+ if (bb_style_counter)
+ printf("Address %d: %s\n", (*bb_style_counter)++, astr);
+ else
+ printf("%s\thas AAAA address %s\n", ns_rr_name(rr), astr);
+ break;
+#endif
+
@ -752,7 +779,7 @@ index 000000000..f3e42d3bb
+ llist_t *type_strings = NULL;
+ int n_ns = 0, n_queries = 0;
+ int c, opts, option_index = 0;
+ int stats = 0;
+ int stats = 0, bb_style_counter = 0;
+ unsigned int types = 0;
+ HEADER *header;
+
@ -769,10 +796,8 @@ index 000000000..f3e42d3bb
+ ptr = llist_pop(&type_strings);
+
+ /* skip leading text, e.g. when invoked with -querytype=AAAA */
+ if ((chr = strchr(ptr, '=')) != NULL) {
+ ptr = chr;
+ *ptr++ = 0;
+ }
+ if ((chr = strchr(ptr, '=')) != NULL)
+ ptr = chr + 1;
+
+ for (c = 0; qtypes[c].name; c++)
+ if (!strcmp(qtypes[c].name, ptr))
@ -813,14 +838,21 @@ index 000000000..f3e42d3bb
+ /* No explicit type given, guess query type.
+ * If we can convert the domain argument into a ptr (means that
+ * inet_pton() could read it) we assume a PTR request, else
+ * we issue A queries. */
+ * we issue A+AAAA queries and switch to an output format
+ * mimicking the one of the traditional nslookup applet. */
+ if (types == 0) {
+ ptr = make_ptr(argv[option_index]);
+
+ if (ptr)
+ if (ptr) {
+ add_query(&queries, &n_queries, T_PTR, ptr);
+ else
+ }
+ else {
+ bb_style_counter = 1;
+ add_query(&queries, &n_queries, T_A, argv[option_index]);
+#if ENABLE_FEATURE_IPV6
+ add_query(&queries, &n_queries, T_AAAA, argv[option_index]);
+#endif
+ }
+ }
+ else {
+ for (c = 0; qtypes[c].name; c++)
@ -889,12 +921,18 @@ index 000000000..f3e42d3bb
+ c = 0;
+
+ if (queries[rc].rlen) {
+ header = (HEADER *)queries[rc].reply;
+ if (!bb_style_counter) {
+ header = (HEADER *)queries[rc].reply;
+
+ if (!header->aa)
+ printf("Non-authoritative answer:\n");
+ if (!header->aa)
+ printf("Non-authoritative answer:\n");
+
+ c = parse_reply(queries[rc].reply, queries[rc].rlen);
+ c = parse_reply(queries[rc].reply, queries[rc].rlen, NULL);
+ }
+ else {
+ c = parse_reply(queries[rc].reply, queries[rc].rlen,
+ &bb_style_counter);
+ }
+ }
+
+ if (c == 0)
@ -902,7 +940,8 @@ index 000000000..f3e42d3bb
+ else if (c < 0)
+ printf("*** Can't find %s: Parse error\n", queries[rc].name);
+
+ printf("\n");
+ if (!bb_style_counter)
+ printf("\n");
+ }
+
+ rc = 0;