1*5bbd2a12SchristosGETHOSTBYNAME(3) FreeBSD Library Functions Manual GETHOSTBYNAME(3) 2b5677b36Schristos 3b5677b36SchristosNNAAMMEE 4b5677b36Schristos ggeetthhoossttbbyynnaammee, ggeetthhoossttbbyyaaddddrr, ggeetthhoosstteenntt, sseetthhoosstteenntt, eennddhhoosstteenntt, hheerrrroorr 5*5bbd2a12Schristos -- get network host entry 6b5677b36Schristos 7b5677b36SchristosSSYYNNOOPPSSIISS 8b5677b36Schristos ##iinncclluuddee <<nneettddbb..hh>> 9b5677b36Schristos 10b5677b36Schristos _e_x_t_e_r_n _i_n_t _h___e_r_r_n_o; 11b5677b36Schristos 12b5677b36Schristos _s_t_r_u_c_t _h_o_s_t_e_n_t _* 13b5677b36Schristos ggeetthhoossttbbyynnaammee(_c_h_a_r _*_n_a_m_e); 14b5677b36Schristos 15b5677b36Schristos _s_t_r_u_c_t _h_o_s_t_e_n_t _* 16b5677b36Schristos ggeetthhoossttbbyynnaammee22(_c_h_a_r _*_n_a_m_e, _i_n_t _a_f); 17b5677b36Schristos 18b5677b36Schristos _s_t_r_u_c_t _h_o_s_t_e_n_t _* 19b5677b36Schristos ggeetthhoossttbbyyaaddddrr(_c_h_a_r _*_a_d_d_r, _i_n_t _l_e_n_, _t_y_p_e); 20b5677b36Schristos 21b5677b36Schristos _s_t_r_u_c_t _h_o_s_t_e_n_t _* 22b5677b36Schristos ggeetthhoosstteenntt(); 23b5677b36Schristos 24b5677b36Schristos sseetthhoosstteenntt(_i_n_t _s_t_a_y_o_p_e_n); 25b5677b36Schristos 26b5677b36Schristos eennddhhoosstteenntt(); 27b5677b36Schristos 28b5677b36Schristos hheerrrroorr(_c_h_a_r _*_s_t_r_i_n_g); 29b5677b36Schristos 30b5677b36SchristosDDEESSCCRRIIPPTTIIOONN 31b5677b36Schristos GGeetthhoossttbbyynnaammee(), ggeetthhoossttbbyynnaammee22(), and ggeetthhoossttbbyyaaddddrr() each return a 32b5677b36Schristos pointer to a _h_o_s_t_e_n_t structure (see below) describing an internet host 33b5677b36Schristos referenced by name or by address, as the function names indicate. This 34b5677b36Schristos structure contains either the information obtained from the name server, 35b5677b36Schristos or broken-out fields from a line in _/_e_t_c_/_h_o_s_t_s. If the local name server 36b5677b36Schristos is not running, these routines do a lookup in _/_e_t_c_/_h_o_s_t_s. 37b5677b36Schristos 38b5677b36Schristos struct hostent { 39b5677b36Schristos char *h_name; /* official name of host */ 40b5677b36Schristos char **h_aliases; /* alias list */ 41b5677b36Schristos int h_addrtype; /* host address type */ 42b5677b36Schristos int h_length; /* length of address */ 43b5677b36Schristos char **h_addr_list; /* list of addresses from name server */ 44b5677b36Schristos }; 45b5677b36Schristos 46b5677b36Schristos #define h_addr h_addr_list[0] /* address, for backward compatibility */ 47b5677b36Schristos 48b5677b36Schristos The members of this structure are: 49b5677b36Schristos 50b5677b36Schristos h_name Official name of the host. 51b5677b36Schristos 52b5677b36Schristos h_aliases A zero-terminated array of alternate names for the host. 53b5677b36Schristos 54b5677b36Schristos h_addrtype The type of address being returned; usually AF_INET. 55b5677b36Schristos 56b5677b36Schristos h_length The length, in bytes, of the address. 57b5677b36Schristos 58b5677b36Schristos h_addr_list A zero-terminated array of network addresses for the host. 59b5677b36Schristos Host addresses are returned in network byte order. 60b5677b36Schristos 61*5bbd2a12Schristos h_addr The first address in h_addr_list; this is for backward com- 62b5677b36Schristos patibility. 63b5677b36Schristos 64b5677b36Schristos When using the nameserver, ggeetthhoossttbbyynnaammee() will search for the named host 65*5bbd2a12Schristos in each parent domain given in the ``search'' directive of resolv.conf(5) 66*5bbd2a12Schristos unless the name contains a dot (``.''). If the name contains no dot, and 67b5677b36Schristos if the environment variable HOSTALIASES contains the name of an alias 68b5677b36Schristos file, the alias file will first be searched for an alias matching the 69b5677b36Schristos input name. See hostname(7) for the domain search procedure and the 70b5677b36Schristos alias file format. 71b5677b36Schristos 72b5677b36Schristos GGeetthhoossttbbyynnaammee22() is an evolution of ggeetthhoossttbbyynnaammee() intended to allow 73b5677b36Schristos lookups in address families other than AF_INET, for example, AF_INET6. 74b5677b36Schristos Currently, the _a_f argument must be specified as AF_INET else the function 75b5677b36Schristos will return NULL after having set _h___e_r_r_n_o to NETDB_INTERNAL. 76b5677b36Schristos 77b5677b36Schristos SSeetthhoosstteenntt() may be used to request the use of a connected TCP socket for 78b5677b36Schristos queries. If the _s_t_a_y_o_p_e_n flag is non-zero, this sets the option to send 79b5677b36Schristos all queries to the name server using TCP and to retain the connection 80b5677b36Schristos after each call to ggeetthhoossttbbyynnaammee() or ggeetthhoossttbbyyaaddddrr(). Otherwise, 81b5677b36Schristos queries are performed using UDP datagrams. 82b5677b36Schristos 83b5677b36Schristos EEnnddhhoosstteenntt() closes the TCP connection. 84b5677b36Schristos 85b5677b36SchristosEENNVVIIRROONNMMEENNTT 86b5677b36Schristos 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. 87b5677b36Schristos 88b5677b36SchristosFFIILLEESS 89b5677b36Schristos /etc/hosts See hosts(5). 90b5677b36Schristos 91b5677b36SchristosDDIIAAGGNNOOSSTTIICCSS 92b5677b36Schristos Error return status from ggeetthhoossttbbyynnaammee() and ggeetthhoossttbbyyaaddddrr() is indicated 93b5677b36Schristos by return of a null pointer. The external integer _h___e_r_r_n_o may then be 94b5677b36Schristos checked to see whether this is a temporary failure or an invalid or 95b5677b36Schristos unknown host. The routine hheerrrroorr() can be used to print an error message 96b5677b36Schristos describing the failure. If its argument _s_t_r_i_n_g is non-NULL, it is 97b5677b36Schristos printed, followed by a colon and a space. The error message is printed 98b5677b36Schristos with a trailing newline. 99b5677b36Schristos 100b5677b36Schristos _h___e_r_r_n_o can have the following values: 101b5677b36Schristos 102b5677b36Schristos NETDB_INTERNAL This indicates an internal error in the library, 103b5677b36Schristos unrelated to the network or name service. _e_r_r_n_o 104b5677b36Schristos will be valid in this case; see perror. 105b5677b36Schristos 106b5677b36Schristos HOST_NOT_FOUND No such host is known. 107b5677b36Schristos 108b5677b36Schristos TRY_AGAIN This is usually a temporary error and means that 109b5677b36Schristos the local server did not receive a response from 110b5677b36Schristos an authoritative server. A retry at some later 111b5677b36Schristos time may succeed. 112b5677b36Schristos 113b5677b36Schristos NO_RECOVERY Some unexpected server failure was encountered. 114b5677b36Schristos This is a non-recoverable error, as one might 115b5677b36Schristos expect. 116b5677b36Schristos 117b5677b36Schristos NO_DATA The requested name is valid but does not have an 118b5677b36Schristos IP address; this is not a temporary error. This 119b5677b36Schristos means that the name is known to the name server 120b5677b36Schristos but there is no address associated with this 121b5677b36Schristos name. Another type of request to the name server 122b5677b36Schristos using this domain name will result in an answer; 123b5677b36Schristos for example, a mail-forwarder may be registered 124b5677b36Schristos for this domain. 125b5677b36Schristos 126b5677b36SchristosSSEEEE AALLSSOO 127b5677b36Schristos hosts(5), hostname(7), resolver(3), resolver(5). 128b5677b36Schristos 129b5677b36SchristosCCAAVVEEAATT 130b5677b36Schristos GGeetthhoosstteenntt() is defined, and sseetthhoosstteenntt() and eennddhhoosstteenntt() are redefined, 131b5677b36Schristos when _l_i_b_c is built to use only the routines to lookup in _/_e_t_c_/_h_o_s_t_s and 132b5677b36Schristos not the name server: 133b5677b36Schristos 134b5677b36Schristos 135b5677b36Schristos GGeetthhoosstteenntt() reads the next line of _/_e_t_c_/_h_o_s_t_s, opening the file if 136b5677b36Schristos necessary. 137b5677b36Schristos 138b5677b36Schristos SSeetthhoosstteenntt() is redefined to open and rewind the file. If the 139b5677b36Schristos _s_t_a_y_o_p_e_n argument is non-zero, the hosts data base will not be 140b5677b36Schristos closed after each call to ggeetthhoossttbbyynnaammee() or ggeetthhoossttbbyyaaddddrr(). 141b5677b36Schristos 142b5677b36Schristos EEnnddhhoosstteenntt() is redefined to close the file. 143b5677b36Schristos 144b5677b36SchristosBBUUGGSS 145b5677b36Schristos All information is contained in a static area so it must be copied if it 146*5bbd2a12Schristos is to be saved. Only the Internet address format is currently under- 147b5677b36Schristos stood. 148b5677b36Schristos 149b5677b36Schristos4th Berkeley Distribution June 23, 1990 4th Berkeley Distribution 150