#include <netdb.h> struct hostent *gethostbyname(char *host_name)
#define _XOPEN_SOURCE 520 #include <netdb.h> struct hostent *gethostbyname(const char *host_name)
The gethostbyname() function is used to retrieve information about a host.
There are two versions of the API, as shown above. The base i5/OS API uses BSD 4.3 structures and syntax. The other uses syntax and structures compatible with the UNIX 98 programming interface specifications. You can select the UNIX 98 compatible interface with the _XOPEN_SOURCE macro.
Authorization of *R (allow access to the object) to the host aliases file specified by the HOSTALIASES environment variable.
You also need *X authority to each directory in the path of the host aliases file.
gethostbyname() returns a pointer. Possible values are:
The structure struct hostent is defined in <netdb.h>.
struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; }; #define h_addr h_addr_list[0]
h_name points to the character string that contains the name of the host. h_aliases is a pointer to a NULL-terminated list of pointers, each of which points to a character string that represents an alternative name for the host. h_addrtype contains the address type of the host (for example, AF_INET). h_length contains the address length. h_addr_list is a pointer to a NULL-terminated list of pointers, each of which points to a network address for the host, in network byte order. Note that the array of address pointers points to structures of type in_addr defined in <netinet/in.h>.
When gethostbyname() fails, h_errno (defined in <netdb.h>) can be set to one of the following:
The host name specified by the host_name parameter was not found.
The host name is a valid name, but there is no corresponding IP address.
An unrecoverable error has occurred.
The local server did not receive a response from an authoritative server. An attempt at a later time may succeed.
When the gethostbyname() function fails, errno can be set to:
Permission denied. The process does not have the appropriate privileges to the host aliases file specified by the HOSTALIASES environment variable.
Note: A person with a UNIX background would expect this information to exist in a file known as /etc/resolv.conf.
If the IP address is found (indicating that the local network is a domain network), the gethostbyaddr() function attempts to query the domain name server for information about a host. If the query fails, the information is obtained from the host database file. If the name server IP address is not found (indicating that local network is a flat network), the host database file is used to obtain the address.
If the host information is retrieved from the domain name server, sockets converts the host name specified by the host_name parameter from the default (CCSID) to ASCII before communicating with the domain name server. If the host information is retrieved from the host database file, no conversion is done on the host name specified by the host_name parameter unless the CCSID of the job is something other than 65535. In addition, the host names returned in the hostent structure will be returned in the default CCSID of the job if they are obtained from the domain name server. For translation to occur for the host names returned in the hostent structure when they are obtained from the host database file, you must use a job CCSID of something other than 65535.
Top | UNIX-Type APIs | APIs by category |