--- /dev/null
+ struct addrinfo hint;
+ struct addrinfo *res, *ptr;
+ int ret, port;
+ struct sockaddr_in *addr;
+ struct sockaddr_in6 *addr6;
+ char buffer[INET6_ADDRSTRLEN];
+ char *string;
+ ...
+ if ((argc - optind) != 2) {
+ printf("Wrong number of arguments %d\n", argc - optind);
+ usage();
+ }
+/* main body */
+ ret = getaddrinfo(argv[1], argv[2], NULL, &res);
+ if (ret != 0) {
+ printf("Resolution error %s\n", gai_strerror(ret));
+ exit(1);
+ }
+ ptr = res;
+ printf("Canonical name %s\n", ptr->ai_canonname);
+ do {
+ if (ptr->ai_family == PF_INET) {
+ printf("IPv4 address: \n");
+ addr = (struct sockaddr_in *) ptr->ai_addr;
+ port = ntohs(addr->sin_port);
+ string = inet_ntop(addr->sin_family, &addr->sin_addr,
+ buffer, sizeof(buffer));
+ } else if (ptr->ai_family == PF_INET6) {
+ printf("IPv6 address: \n");
+ addr6 = (struct sockaddr_in *) ptr->ai_addr;
+ port = ntohs(addr6->sin6_port);
+ string = inet_ntop(addr6->sin6_family, &addr6->sin6_addr,
+ buffer, sizeof(buffer));
+ } else {
+ printf("Address family error\n");
+ exit(1);
+ }
+ printf("\tIndirizzo %s\n", string);
+ printf("\tProtocollo %i\n", ptr->ai_protocol);
+ printf("\tPorta %i\n", port);
+
+
+ ptr = ptr->ai_next;
+ } while (ptr != NULL);
+ exit(0);
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+ printf("Program mygethost: do an hostname resolution \n");
+ printf("Usage:\n");
+ printf(" mygethost [-h] hostname service\n");
+ printf(" -h print this help\n");
+ exit(1);
+}