From c72499584abbf32b3757024bb0cd53f23d5d0d72 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 15 Apr 2018 20:04:57 +0200 Subject: [PATCH] nslookup: simplify make_ptr function old new delta nslookup_main 2644 2611 -33 Signed-off-by: Denys Vlasenko --- networking/nslookup.c | 71 +++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/networking/nslookup.c b/networking/nslookup.c index 99f781e1b..fd241a5ca 100644 --- a/networking/nslookup.c +++ b/networking/nslookup.c @@ -484,40 +484,6 @@ static int parse_reply(const unsigned char *msg, size_t len) return i; } -static char *make_ptr(char resbuf[80], const char *addrstr) -{ - unsigned char addr[16]; - int i; - -#if ENABLE_FEATURE_IPV6 - if (inet_pton(AF_INET6, addrstr, addr)) { - if (memcmp(addr, v4_mapped, 12) != 0) { - char *ptr = resbuf; - for (i = 0; i < 16; i++) { - *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] & 0xf]; - *ptr++ = '.'; - *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] >> 4]; - *ptr++ = '.'; - } - strcpy(ptr, "ip6.arpa"); - } - else { - sprintf(resbuf, "%u.%u.%u.%u.in-addr.arpa", - addr[15], addr[14], addr[13], addr[12]); - } - return resbuf; - } -#endif - - if (inet_pton(AF_INET, addrstr, addr)) { - sprintf(resbuf, "%u.%u.%u.%u.in-addr.arpa", - addr[3], addr[2], addr[1], addr[0]); - return resbuf; - } - - return NULL; -} - /* * Function logic borrowed & modified from musl libc, res_msend.c * G.query_count is always > 0. @@ -743,6 +709,38 @@ static void add_query(int type, const char *dname) new_q->qlen = qlen; } +static char *make_ptr(const char *addrstr) +{ + unsigned char addr[16]; + int i; + +#if ENABLE_FEATURE_IPV6 + if (inet_pton(AF_INET6, addrstr, addr)) { + if (memcmp(addr, v4_mapped, 12) != 0) { + char resbuf[80]; + char *ptr = resbuf; + for (i = 0; i < 16; i++) { + *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] & 0xf]; + *ptr++ = '.'; + *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] >> 4]; + *ptr++ = '.'; + } + strcpy(ptr, "ip6.arpa"); + return xstrdup(resbuf); + } + return xasprintf("%u.%u.%u.%u.in-addr.arpa", + addr[15], addr[14], addr[13], addr[12]); + } +#endif + + if (inet_pton(AF_INET, addrstr, addr)) { + return xasprintf("%u.%u.%u.%u.in-addr.arpa", + addr[3], addr[2], addr[1], addr[0]); + } + + return NULL; +} + int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int nslookup_main(int argc UNUSED_PARAM, char **argv) { @@ -843,11 +841,10 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv) * mimicking the one of the traditional nslookup applet. */ char *ptr; - char buf80[80]; - ptr = make_ptr(buf80, argv[0]); + ptr = make_ptr(argv[0]); if (ptr) { - add_query(T_PTR, xstrdup(ptr)); + add_query(T_PTR, ptr); } else { add_query(T_A, argv[0]); #if ENABLE_FEATURE_IPV6 -- 2.25.1