#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc, char **argv) { /* argv[1] is a domain name */
    struct addrinfo hints;
    struct addrinfo *firsthost = NULL;
    struct addrinfo *host;
    struct sockaddr_in *addr;
    int error;
    char buf[80];

    memset(&hints, 0, sizeof hints);
    hints.ai_flags = AI_CANONNAME;
    hints.ai_family = AF_UNSPEC;
    error = getaddrinfo(argv[1], NULL, &hints, &firsthost);
    if (error != 0) {
	freeaddrinfo(firsthost);
	fprintf (stderr, "%s: %s\n", argv[1], gai_strerror(error));
	exit(1);
    }

    printf("official hostname: %s\n", firsthost->ai_canonname);
    
    for (host = firsthost;  host != NULL;  host = host->ai_next) {
	addr = (struct sockaddr_in *)host->ai_addr;
	printf("address: %s\n", inet_ntop(addr->sin_family, &addr->sin_addr,
	  buf, sizeof buf));
    }

    freeaddrinfo(firsthost);
    exit(0);
}

