A hostent struct is defined as follows:
struct hostent
{
char *h_name; // official name of host
char **h_aliases; // alias list
int h_addrtype; // host address type (always AF_INET)
int h_length; // length of address
char **h_addr_list; // list of addresses
}
The address fields are binary values of the addresses, each address
requiring h_length bytes, the last address being equal to 0. These binary
values may be converted to character-representations by the
addressToString() member, which uses inet_ntop(), internally.
The class' members can only be used when the host whose name or address is searched can be resolved by a name resolution process, e.g., bind(1).
#include <iostream>
#include <algorithm>
#include <iterator>
#include <bobcat/hostent>
#include <bobcat/gethostent>
using namespace std;
using namespace FBB;
int main(int argc, char **argv)
try
{
if (argc == 1)
{
cerr << "Provide a host name or host address to solve\n";
return 1;
}
Hostent he(GetHostent::gethostent(argv[1], argv[1]));
cout << "Hostname: " << he.hostname() << endl;
cout << "Aliases:\n";
copy(he.beginAlias(), he.endAlias(),
ostream_iterator<char const *>(cout, "\n"));
cout << "Addresses:\n";
for (size_t idx = 0; idx < he.nAddresses(); idx++)
cout << he.dottedDecimalAddress(idx) << endl;
}
catch (Exception const &err)
{
cout << err.what() << endl;
return 1;
}