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