2 struct addrinfo *res, *ptr;
4 struct sockaddr_in *addr;
5 struct sockaddr_in6 *addr6;
6 char buffer[INET6_ADDRSTRLEN];
9 if ((argc - optind) != 2) {
10 printf("Wrong number of arguments %d\n", argc - optind);
14 ret = getaddrinfo(argv[1], argv[2], NULL, &res);
16 printf("Resolution error %s\n", gai_strerror(ret));
20 printf("Canonical name %s\n", ptr->ai_canonname);
22 if (ptr->ai_family == PF_INET) {
23 printf("IPv4 address: \n");
24 addr = (struct sockaddr_in *) ptr->ai_addr;
25 port = ntohs(addr->sin_port);
26 string = inet_ntop(addr->sin_family, &addr->sin_addr,
27 buffer, sizeof(buffer));
28 } else if (ptr->ai_family == PF_INET6) {
29 printf("IPv6 address: \n");
30 addr6 = (struct sockaddr_in *) ptr->ai_addr;
31 port = ntohs(addr6->sin6_port);
32 string = inet_ntop(addr6->sin6_family, &addr6->sin6_addr,
33 buffer, sizeof(buffer));
35 printf("Address family error\n");
38 printf("\tIndirizzo %s\n", string);
39 printf("\tProtocollo %i\n", ptr->ai_protocol);
40 printf("\tPorta %i\n", port);
44 } while (ptr != NULL);
48 * routine to print usage info and exit
51 printf("Program mygethost: do an hostname resolution \n");
53 printf(" mygethost [-h] hostname service\n");
54 printf(" -h print this help\n");