struct hostent *gethostbyname(const char *name);
struct hostent *gethostbyaddr(const char *addr, int len, int type);
struct hostent {
char *h_name; // official name of host
char **h_aliases; // NULL-terminated list of aliases
int h_addrtype; // host address type, e.g. AF_INET
int h_length; // length of address (4 for IPv4 addresses)
char **h_addr_list; // NULL-terminated list of IP addresses
}; // h_addr_list is really a struct in_addr ** when known to be IPv4 addresses
struct in_addr {
unsigned int s_addr // stored in network byte order (big endian)
};static void publishIPAddressInfo(const string& host) {
struct hostent *he = gethostbyname(host.c_str());
if (he == NULL) { // NULL return value means resolution attempt failed
cout << host << " could not be resolved to an address." << endl;
return;
}
cout << "Official name is \"" << he->h_name << "\"" << endl;
cout << "IP Addresses: " << endl;
struct in_addr **addressList = (struct in_addr **) he->h_addr_list;
while (*addressList != NULL) {
cout << "+ " << inet_ntoa(**addressList) << endl;
addressList++;
}
}