diff options
author | William Joye <wjoye@cfa.harvard.edu> | 2018-01-23 16:54:55 (GMT) |
---|---|---|
committer | William Joye <wjoye@cfa.harvard.edu> | 2018-01-23 16:54:55 (GMT) |
commit | 5bf617b1e9347322b8b16419fd5e74107e8b1c26 (patch) | |
tree | e5bbb785b82b35bc62df41bebca8b540078aaf76 /xpa/dns.c | |
parent | 51e1f85047b34f095ed69a3024d696997d2667c8 (diff) | |
parent | 0ebcd152c10d2eae6b62f16e7138aa187a8a1bdd (diff) | |
download | blt-5bf617b1e9347322b8b16419fd5e74107e8b1c26.zip blt-5bf617b1e9347322b8b16419fd5e74107e8b1c26.tar.gz blt-5bf617b1e9347322b8b16419fd5e74107e8b1c26.tar.bz2 |
Merge commit '0ebcd152c10d2eae6b62f16e7138aa187a8a1bdd' as 'xpa'
Diffstat (limited to 'xpa/dns.c')
-rw-r--r-- | xpa/dns.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/xpa/dns.c b/xpa/dns.c new file mode 100644 index 0000000..68f94bd --- /dev/null +++ b/xpa/dns.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory + */ + +#include <unistd.h> +#include <strings.h> +#include <stdio.h> +#include <stdlib.h> +#include <netdb.h> /* gethostbyname() */ +#include <arpa/inet.h> + +#define SZ_LINE 1024 + +int main(int argc, char **argv) +{ + int i, got; + char host[SZ_LINE]; + struct hostent *hostent; + struct addrinfo *hints=NULL, *addrinfo=NULL; + struct sockaddr_in * p; + + // gethost + if( argc > 1 ) + strcpy(host, argv[1]); + else{ + fprintf(stderr, "calling gethostname() ...\n"); + if( gethostname(host, SZ_LINE) == -1 ){ + perror("gethostname"); + exit(1); + } + else{ + fprintf(stderr, "host name is %s\n", host); + } + } + fprintf(stderr, "\n"); + + fprintf(stderr, "calling getaddrinfo (preferred) ...\n"); + hints = (struct addrinfo *)calloc(1, sizeof(struct addrinfo)); + hints->ai_flags |= AI_CANONNAME; + hints->ai_family = AF_INET; + got = getaddrinfo(host, NULL, hints, &addrinfo); + if( got != 0 ){ + fprintf(stderr, "getaddrinfo rtn: %d %s\n", got, gai_strerror(got)); + perror("getaddrinfo"); + exit(1); + } + else{ + fprintf(stderr, "getaddrinfo() succeeded\n"); + } + fprintf(stderr, "printing ip address via getaddrinfo ...\n"); + if( addrinfo ){ + p = (struct sockaddr_in *)(addrinfo->ai_addr); + fprintf(stderr, "%x (canonical: %s)\n", p->sin_addr.s_addr, addrinfo->ai_canonname); + freeaddrinfo(addrinfo); + if( hints ) free(hints); + } + else{ + fprintf(stderr, "ERROR: can't look up: %s\n", host); + } + fprintf(stderr, "\n"); + + fprintf(stderr, "calling gethostbyname (obsolete) ...\n"); + if( !(hostent = gethostbyname(host)) ){ + perror("gethostbyname"); + exit(1); + } + else{ + fprintf(stderr, "gethostbyname() succeeded\n"); + } + fprintf(stderr, "printing ip address(es) via gethostbyname ...\n"); + if( hostent ){ + for(i=0; hostent->h_addr_list[i]; i++){ + fprintf(stderr, "%x\n", *(int *)hostent->h_addr_list[i]); + } + } + else{ + fprintf(stderr, "ERROR: can't look up: %s\n", host); + } + fprintf(stderr, "\n"); + + return(0); +} |