diff options
author | Daniel Friesel <derf@derf.homelinux.org> | 2009-08-09 10:58:06 +0200 |
---|---|---|
committer | Daniel Friesel <derf@derf.homelinux.org> | 2009-08-09 10:58:06 +0200 |
commit | e33f30a012de730926e49dd4b5c5d9c510d621be (patch) | |
tree | 837a7a70a9b2cdbb42e89b9c0e36145917de2e13 | |
parent | fc3931d51b5f17de59aeb629a58b3664cd6002ab (diff) |
Don't print redundant information or rdns
-rw-r--r-- | src/host.c | 26 |
1 files changed, 20 insertions, 6 deletions
@@ -5,11 +5,15 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> +#define INPUT_HOST 1 +#define INPUT_IP 2 + /** * \brief convert addrinfo to simple IP address * @@ -45,6 +49,7 @@ int main(int argc, char **argv) { char hostname[NI_MAXHOST]; char ip_address[INET6_ADDRSTRLEN]; int ret; + int input_type = 0; if (argc < 2) { fprintf(stderr, "Usage: %s <hostname>\n", argv[0]); @@ -60,13 +65,22 @@ int main(int argc, char **argv) { for (address = result; address != NULL; address = address->ai_next) { if (addr_to_ip(address, ip_address, sizeof(ip_address)) == 0) continue; - printf("%-40s ", ip_address); - ret = getnameinfo(address->ai_addr, address->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0); - if (ret != 0) { - fprintf(stderr, "getnameinfo: %s\n", gai_strerror(ret)); - continue; + if (input_type == 0) { + if (strcmp(ip_address, argv[1]) == 0) + input_type = INPUT_IP; + else + input_type = INPUT_HOST; + } + if (input_type == INPUT_HOST) + puts(ip_address); + else { + ret = getnameinfo(address->ai_addr, address->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0); + if (ret != 0) { + fprintf(stderr, "getnameinfo: %s\n", gai_strerror(ret)); + continue; + } + puts(hostname); } - puts(hostname); } freeaddrinfo(address); return(EXIT_SUCCESS); |