/* ** Compile: gcc -Wall dump-addrinfo.c -o dump-addrinfo */ #include #include #include #include #include #include #include #include #include int dump_addrinfo(const char *host, const char *service) { struct addrinfo *ai, *runp; struct addrinfo hints; char hostbuf[50], portbuf[10]; int e; memset(&hints, '\0', sizeof (hints)); hints.ai_flags = AI_ADDRCONFIG; hints.ai_socktype = SOCK_STREAM; e = getaddrinfo(host, service, &hints, &ai); if (e != 0) { /* error(EXIT_FAILURE, 0, "getaddrinfo: %s", gai_strerror(e)); */ return -1; } for (runp = ai; runp != NULL; runp = runp->ai_next) { printf("family: %d, socktype: %d, protocol: %d, ", runp->ai_family, runp->ai_socktype, runp->ai_protocol); e = getnameinfo( runp->ai_addr, runp->ai_addrlen, hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV ); printf("host: %s, port: %s\n", hostbuf, portbuf); } freeaddrinfo(ai); return 0; } int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: dump-addrinfo host serivce\n"); } dump_addrinfo(argv[1], argv[2]); exit(0); }