diff options
author | Denis Vlasenko | 2007-01-22 22:43:05 +0000 |
---|---|---|
committer | Denis Vlasenko | 2007-01-22 22:43:05 +0000 |
commit | 448f0241e06e7df2003b7f8afcf01e7406762b4e (patch) | |
tree | bc4a002a534b4c0fb8bba09dbd46f93cea5a38c6 /networking/nslookup.c | |
parent | fdcd7c4237a99682724ffbf4e40ab50a40197c56 (diff) | |
download | busybox-448f0241e06e7df2003b7f8afcf01e7406762b4e.zip busybox-448f0241e06e7df2003b7f8afcf01e7406762b4e.tar.gz |
nslookup: full circle. Here we started IPv6 work. Use "new API"
and thus save a few bytes.
Diffstat (limited to 'networking/nslookup.c')
-rw-r--r-- | networking/nslookup.c | 117 |
1 files changed, 88 insertions, 29 deletions
diff --git a/networking/nslookup.c b/networking/nslookup.c index 5a08844..af08162 100644 --- a/networking/nslookup.c +++ b/networking/nslookup.c @@ -47,7 +47,7 @@ * ns3.kernel.org internet address = 204.152.191.36 */ -static int sockaddr_to_dotted(struct sockaddr *saddr, char *buf, int buflen) +/*static int sockaddr_to_dotted(struct sockaddr *saddr, char *buf, int buflen) { if (buflen <= 0) return -1; buf[0] = '\0'; @@ -61,9 +61,11 @@ static int sockaddr_to_dotted(struct sockaddr *saddr, char *buf, int buflen) } return -1; } +*/ static int print_host(const char *hostname, const char *header) { +#if 0 char str[128]; /* IPv6 address will fit, hostnames hopefully too */ struct addrinfo *result = NULL; int rc; @@ -76,6 +78,7 @@ static int print_host(const char *hostname, const char *header) hint.ai_socktype = SOCK_STREAM; // hint.ai_flags = AI_CANONNAME; rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result); + if (!rc) { struct addrinfo *cur = result; // printf("%s\n", cur->ai_canonname); ? @@ -93,6 +96,75 @@ static int print_host(const char *hostname, const char *header) } freeaddrinfo(result); return (rc != 0); + +#else + /* We can't use host2sockaddr() - we want to get ALL addresses, + * not just one */ + + struct addrinfo *result = NULL; + int rc; + struct addrinfo hint; + + memset(&hint, 0 , sizeof(hint)); + /* hint.ai_family = AF_UNSPEC; - zero anyway */ + /* Needed. Or else we will get each address thrice (or more) + * for each possible socket type (tcp,udp,raw...): */ + hint.ai_socktype = SOCK_STREAM; + // hint.ai_flags = AI_CANONNAME; + rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result); + + if (!rc) { + struct addrinfo *cur = result; + unsigned cnt = 0; + + printf("%-10s %s\n", header, hostname); + // printf("%s\n", cur->ai_canonname); ? + while (cur) { + char *dotted, *revhost; + dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr, cur->ai_addrlen); + revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr, cur->ai_addrlen); + + printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n'); + if (revhost) { + puts(revhost); + if (ENABLE_FEATURE_CLEAN_UP) + free(revhost); + } + if (ENABLE_FEATURE_CLEAN_UP) + free(dotted); + cur = cur->ai_next; + } + } else { +#if ENABLE_VERBOSE_RESOLUTION_ERRORS + bb_error_msg("getaddrinfo('%s') failed: %s", hostname, gai_strerror(rc)); +#else + bb_error_msg("can't resolve '%s'", hostname); +#endif + } + if (ENABLE_FEATURE_CLEAN_UP) + freeaddrinfo(result); + return (rc != 0); +#endif +} + + +/* lookup the default nameserver and display it */ +static void server_print(void) +{ + char *server; + + server = xmalloc_sockaddr2dotted_noport((struct sockaddr*)&_res.nsaddr_list[0], + sizeof(struct sockaddr_in)); + /* I honestly don't know what to do if DNS server has _IPv6 address_. + * Probably it is listed in + * _res._u._ext_.nsaddrs[MAXNS] (of type "struct sockaddr_in6*" each) + * but how to find out whether resolver uses + * _res.nsaddr_list[] or _res._u._ext_.nsaddrs[], or both? + * Looks like classic design from hell, BIND-grade. Hard to surpass. */ + print_host(server, "Server:"); + if (ENABLE_FEATURE_CLEAN_UP) + free(server); + puts(""); } @@ -109,40 +181,27 @@ static void set_default_dns(char *server) } -/* lookup the default nameserver and display it */ -static void server_print(void) -{ - char str[INET6_ADDRSTRLEN]; - - sockaddr_to_dotted((struct sockaddr*)&_res.nsaddr_list[0], str, sizeof(str)); - print_host(str, "Server:"); - puts(""); -} - - int nslookup_main(int argc, char **argv) { - /* - * initialize DNS structure _res used in printing the default - * name server and in the explicit name server option feature. - */ - - res_init(); - - /* - * We allow 1 or 2 arguments. - * The first is the name to be looked up and the second is an - * optional DNS server with which to do the lookup. - * More than 3 arguments is an error to follow the pattern of the - * standard nslookup - */ + /* We allow 1 or 2 arguments. + * The first is the name to be looked up and the second is an + * optional DNS server with which to do the lookup. + * More than 3 arguments is an error to follow the pattern of the + * standard nslookup */ if (argc < 2 || *argv[1] == '-' || argc > 3) bb_show_usage(); - else if(argc == 3) + + /* initialize DNS structure _res used in printing the default + * name server and in the explicit name server option feature. */ + res_init(); + /* rfc2133 says this enables IPv6 lookups */ + /* (but it also says "may be enabled in /etc/resolv.conf|) */ + /*_res.options |= RES_USE_INET6;*/ + + if(argc == 3) set_default_dns(argv[2]); server_print(); - return print_host(argv[1], "Name: "); + return print_host(argv[1], "Name:"); } - |