/* hostinfo.c * * from CMU's Computer Systems text * http://csapp.cs.cmu.edu/3e/ics3/code/netp/hostinfo.c * requires csapp.c, csapp.h * * $ gcc -O2 -pthread hostinfo.c csapp.c -o hostinfo * $ ./hostinfo cs.bennington.college * 96.126.108.24 */ #include "csapp.h" int main(int argc, char **argv){ struct addrinfo *p, *listp, hints; char buf[MAXLINE]; int rc, flags; if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(0); } /* Get a list of addrinfo records */ memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_INET; /* IPv4 only */ hints.ai_socktype = SOCK_STREAM; /* Connections only */ if ((rc = getaddrinfo(argv[1], NULL, &hints, &listp)) != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(rc)); exit(1); } /* Walk the list and display each IP address */ flags = NI_NUMERICHOST; /* Display address string instead of domain name */ for (p = listp; p; p = p->ai_next) { Getnameinfo(p->ai_addr, p->ai_addrlen, buf, MAXLINE, NULL, 0, flags); printf("%s\n", buf); } /* Clean up */ Freeaddrinfo(listp); exit(0); }