#include #include #include /* for gethostbyname */ extern int errno; /* for inet_ntop */ int main(int argc, char **argv) { struct hostent *entry; char **p; char buffer[INET6_ADDRSTRLEN]; int error; if (argc != 2) { fprintf(stderr, "%s: need hostname\n", argv[0]); return 1; } entry = getipnodebyname(argv[1], AF_INET6, AI_DEFAULT, &error); if (entry == NULL) { fprintf(stderr, "%s: error %d from getipnodebyname\n", argv[0], error); return 2; } switch (entry->h_addrtype) { case AF_INET: case AF_INET6: for (p = entry->h_addr_list; *p != NULL; ++p) { if (inet_ntop(entry->h_addrtype, *p, buffer, sizeof buffer) == NULL) { perror(argv[0]); return 3; } printf("%s\n", buffer); } break; default: fprintf(stderr, "%s: unknown address type %d\n", argv[0], entry->h_addrtype); return 4; } return EXIT_SUCCESS; }