xatonum.h: add comment
[oweals/busybox.git] / networking / nslookup.c
index af0816215e95e54cd1ec3ba9411fddc51db3aca1..183ae152de0bcfe8493798adbb4edc6bfd99b24a 100644 (file)
@@ -12,7 +12,7 @@
  */
 
 #include <resolv.h>
-#include "busybox.h"
+#include "libbb.h"
 
 /*
  *  I'm only implementing non-interactive mode;
  * ns3.kernel.org  internet address = 204.152.191.36
  */
 
-/*static int sockaddr_to_dotted(struct sockaddr *saddr, char *buf, int buflen)
-{
-       if (buflen <= 0) return -1;
-       buf[0] = '\0';
-       if (saddr->sa_family == AF_INET) {
-               inet_ntop(AF_INET, &((struct sockaddr_in*)saddr)->sin_addr, buf, buflen);
-               return 0;
-       }
-       if (saddr->sa_family == AF_INET6) {
-               inet_ntop(AF_INET6, &((struct sockaddr_in6*)saddr)->sin6_addr, buf, buflen);
-               return 0;
-       }
-       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;
-       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;
-               // printf("%s\n", cur->ai_canonname); ?
-               while (cur) {
-                       sockaddr_to_dotted(cur->ai_addr, str, sizeof(str));
-                       printf("%s  %s\nAddress: %s", header, hostname, str);
-                       str[0] = ' ';
-                       if (getnameinfo(cur->ai_addr, cur->ai_addrlen, str+1, sizeof(str)-1, NULL, 0, NI_NAMEREQD))
-                               str[0] = '\0';
-                       puts(str);
-                       cur = cur->ai_next;
-               }
-       } else {
-               bb_error_msg("getaddrinfo('%s') failed: %s", hostname, gai_strerror(rc));
-       }
-       freeaddrinfo(result);
-       return (rc != 0);
-
-#else
-       /* We can't use host2sockaddr() - we want to get ALL addresses,
+       /* We can't use xhost2sockaddr() - we want to get ALL addresses,
         * not just one */
 
        struct addrinfo *result = NULL;
@@ -118,11 +69,11 @@ static int print_host(const char *hostname, const char *header)
                unsigned cnt = 0;
 
                printf("%-10s %s\n", header, hostname);
-               // printf("%s\n", cur->ai_canonname); ?
+               // puts(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);
+                       dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr);
+                       revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr);
 
                        printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n');
                        if (revhost) {
@@ -136,7 +87,7 @@ static int print_host(const char *hostname, const char *header)
                }
        } else {
 #if ENABLE_VERBOSE_RESOLUTION_ERRORS
-               bb_error_msg("getaddrinfo('%s') failed: %s", hostname, gai_strerror(rc));
+               bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc));
 #else
                bb_error_msg("can't resolve '%s'", hostname);
 #endif
@@ -144,17 +95,14 @@ static int print_host(const char *hostname, const char *header)
        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));
+       server = xmalloc_sockaddr2dotted_noport((struct sockaddr*)&_res.nsaddr_list[0]);
        /* 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)
@@ -164,10 +112,9 @@ static void server_print(void)
        print_host(server, "Server:");
        if (ENABLE_FEATURE_CLEAN_UP)
                free(server);
-       puts("");
+       bb_putchar('\n');
 }
 
-
 /* alter the global _res nameserver structure to use
    an explicit dns server instead of what is in /etc/resolv.h */
 static void set_default_dns(char *server)
@@ -180,7 +127,7 @@ static void set_default_dns(char *server)
        }
 }
 
-
+int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int nslookup_main(int argc, char **argv)
 {
        /* We allow 1 or 2 arguments.
@@ -199,7 +146,7 @@ int nslookup_main(int argc, char **argv)
        /* (but it also says "may be enabled in /etc/resolv.conf|) */
        /*_res.options |= RES_USE_INET6;*/
 
-       if(argc == 3)
+       if (argc == 3)
                set_default_dns(argv[2]);
 
        server_print();