GETHOSTBYNAME(3) FreeBSD Library Functions Manual GETHOSTBYNAME(3) NNAAMMEE ggeetthhoossttbbyynnaammee, ggeetthhoossttbbyyaaddddrr, ggeetthhoosstteenntt, sseetthhoosstteenntt, eennddhhoosstteenntt, hheerrrroorr -- get network host entry SSYYNNOOPPSSIISS ##iinncclluuddee <> _e_x_t_e_r_n _i_n_t _h___e_r_r_n_o; _s_t_r_u_c_t _h_o_s_t_e_n_t _* ggeetthhoossttbbyynnaammee(_c_h_a_r _*_n_a_m_e); _s_t_r_u_c_t _h_o_s_t_e_n_t _* ggeetthhoossttbbyynnaammee22(_c_h_a_r _*_n_a_m_e, _i_n_t _a_f); _s_t_r_u_c_t _h_o_s_t_e_n_t _* ggeetthhoossttbbyyaaddddrr(_c_h_a_r _*_a_d_d_r, _i_n_t _l_e_n_, _t_y_p_e); _s_t_r_u_c_t _h_o_s_t_e_n_t _* ggeetthhoosstteenntt(); sseetthhoosstteenntt(_i_n_t _s_t_a_y_o_p_e_n); eennddhhoosstteenntt(); hheerrrroorr(_c_h_a_r _*_s_t_r_i_n_g); DDEESSCCRRIIPPTTIIOONN GGeetthhoossttbbyynnaammee(), ggeetthhoossttbbyynnaammee22(), and ggeetthhoossttbbyyaaddddrr() each return a pointer to a _h_o_s_t_e_n_t structure (see below) describing an internet host referenced by name or by address, as the function names indicate. This structure contains either the information obtained from the name server, or broken-out fields from a line in _/_e_t_c_/_h_o_s_t_s. If the local name server is not running, these routines do a lookup in _/_e_t_c_/_h_o_s_t_s. struct hostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses from name server */ }; #define h_addr h_addr_list[0] /* address, for backward compatibility */ The members of this structure are: h_name Official name of the host. h_aliases A zero-terminated array of alternate names for the host. h_addrtype The type of address being returned; usually AF_INET. h_length The length, in bytes, of the address. h_addr_list A zero-terminated array of network addresses for the host. Host addresses are returned in network byte order. h_addr The first address in h_addr_list; this is for backward com- patibility. When using the nameserver, ggeetthhoossttbbyynnaammee() will search for the named host in each parent domain given in the ``search'' directive of resolv.conf(5) unless the name contains a dot (``.''). If the name contains no dot, and if the environment variable HOSTALIASES contains the name of an alias file, the alias file will first be searched for an alias matching the input name. See hostname(7) for the domain search procedure and the alias file format. GGeetthhoossttbbyynnaammee22() is an evolution of ggeetthhoossttbbyynnaammee() intended to allow lookups in address families other than AF_INET, for example, AF_INET6. Currently, the _a_f argument must be specified as AF_INET else the function will return NULL after having set _h___e_r_r_n_o to NETDB_INTERNAL. SSeetthhoosstteenntt() may be used to request the use of a connected TCP socket for queries. If the _s_t_a_y_o_p_e_n flag is non-zero, this sets the option to send all queries to the name server using TCP and to retain the connection after each call to ggeetthhoossttbbyynnaammee() or ggeetthhoossttbbyyaaddddrr(). Otherwise, queries are performed using UDP datagrams. EEnnddhhoosstteenntt() closes the TCP connection. EENNVVIIRROONNMMEENNTT HOSTALIASES Name of file containing (_h_o_s_t _a_l_i_a_s, _f_u_l_l _h_o_s_t_n_a_m_e) pairs. FFIILLEESS /etc/hosts See hosts(5). DDIIAAGGNNOOSSTTIICCSS Error return status from ggeetthhoossttbbyynnaammee() and ggeetthhoossttbbyyaaddddrr() is indicated by return of a null pointer. The external integer _h___e_r_r_n_o may then be checked to see whether this is a temporary failure or an invalid or unknown host. The routine hheerrrroorr() can be used to print an error message describing the failure. If its argument _s_t_r_i_n_g is non-NULL, it is printed, followed by a colon and a space. The error message is printed with a trailing newline. _h___e_r_r_n_o can have the following values: NETDB_INTERNAL This indicates an internal error in the library, unrelated to the network or name service. _e_r_r_n_o will be valid in this case; see perror. HOST_NOT_FOUND No such host is known. TRY_AGAIN This is usually a temporary error and means that the local server did not receive a response from an authoritative server. A retry at some later time may succeed. NO_RECOVERY Some unexpected server failure was encountered. This is a non-recoverable error, as one might expect. NO_DATA The requested name is valid but does not have an IP address; this is not a temporary error. This means that the name is known to the name server but there is no address associated with this name. Another type of request to the name server using this domain name will result in an answer; for example, a mail-forwarder may be registered for this domain. SSEEEE AALLSSOO hosts(5), hostname(7), resolver(3), resolver(5). CCAAVVEEAATT GGeetthhoosstteenntt() is defined, and sseetthhoosstteenntt() and eennddhhoosstteenntt() are redefined, when _l_i_b_c is built to use only the routines to lookup in _/_e_t_c_/_h_o_s_t_s and not the name server: GGeetthhoosstteenntt() reads the next line of _/_e_t_c_/_h_o_s_t_s, opening the file if necessary. SSeetthhoosstteenntt() is redefined to open and rewind the file. If the _s_t_a_y_o_p_e_n argument is non-zero, the hosts data base will not be closed after each call to ggeetthhoossttbbyynnaammee() or ggeetthhoossttbbyyaaddddrr(). EEnnddhhoosstteenntt() is redefined to close the file. BBUUGGSS All information is contained in a static area so it must be copied if it is to be saved. Only the Internet address format is currently under- stood. 4th Berkeley Distribution June 23, 1990 4th Berkeley Distribution