gethostbyname(3) Library Functions Manual gethostbyname(3) gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent, h_errno, herror, hstrerror, gethostbyaddr_r, gethostbyname2, gethostbyname2_r, gethostbyname_r, gethostent_r - C (libc, -lc) #include void sethostent(int stayopen); void endhostent(void); [[deprecated]] extern int h_errno; [[deprecated]] struct hostent *gethostbyname(const char *name); [[deprecated]] struct hostent *gethostbyaddr(const void addr[.len], socklen_t len, int type); [[deprecated]] void herror(const char *s); [[deprecated]] const char *hstrerror(int err); /* System V/POSIX */ struct hostent *gethostent(void); /* GNU extensions */ [[deprecated]] struct hostent *gethostbyname2(const char *name, int af); int gethostent_r(struct hostent *restrict ret, char buf[restrict .buflen], size_t buflen, struct hostent **restrict result, int *restrict h_errnop); [[deprecated]] int gethostbyaddr_r(const void addr[restrict .len], socklen_t len, int type, struct hostent *restrict ret, char buf[restrict .buflen], size_t buflen, struct hostent **restrict result, int *restrict h_errnop); [[deprecated]] int gethostbyname_r(const char *restrict name, struct hostent *restrict ret, char buf[restrict .buflen], size_t buflen, struct hostent **restrict result, int *restrict h_errnop); [[deprecated]] int gethostbyname2_r(const char *restrict name, int af, struct hostent *restrict ret, char buf[restrict .buflen], size_t buflen, struct hostent **restrict result, int *restrict h_errnop); glibc (. feature_test_macros(7)): gethostbyname2(), gethostent_r(), gethostbyaddr_r(), gethostbyname_r(), gethostbyname2_r(): Since glibc 2.19: _DEFAULT_SOURCE glibc up to and including 2.19: _BSD_SOURCE || _SVID_SOURCE herror(), hstrerror(): Since glibc 2.19: _DEFAULT_SOURCE glibc 2.8 to glibc 2.19: _BSD_SOURCE || _SVID_SOURCE Before glibc 2.8: none h_errno: Since glibc 2.19 _DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L glibc 2.12 to glibc 2.19: _BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L Before glibc 2.12: none gethostbyname*(), gethostbyaddr*(), herror() hstrerror() . getaddrinfo(3), getnameinfo(3) gai_strerror(3). sethostent() ( stayopen (1)), TCP . UDP. endhostent() TCP. gethostbyname() hostent name. name , IPv4 ( inet_addr(3)). name -- IPv4, gethostbyname() name h_name, struct in_addr -- h_addr_list[0] hostent name HOSTALIASES, name , HOSTALIASES ( hostname(7)). name , . The gethostbyaddr() function returns a structure of type hostent for the given host address addr of length len and address type type. Valid address types are AF_INET and AF_INET6 (defined in ). The host address argument is a pointer to a struct of a type depending on the address type, for example a struct in_addr * (probably obtained via a call to inet_addr(3)) for address type AF_INET. herror() () stderr h_errno. hstrerror() () (, h_errno) . The domain name queries carried out by gethostbyname() and gethostbyaddr() rely on the Name Service Switch (nsswitch.conf(5)) configured sources or a local name server (named(8)). The default action is to query the Name Service Switch (nsswitch.conf(5)) configured sources, failing that, a local name server (named(8)). nsswitch.conf(5). glibc 2.4 , order /etc/host.conf (host.conf(5)). hostent : 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] /* */ hostent: h_name . h_aliases , null. h_addrtype ; AF_INET AF_INET6. h_length . h_addr_list ( ), null. h_addr h_addr_list, . gethostbyname() gethostbyaddr() hostent null . h_errno . NULL, , . h_errno : HOST_NOT_FOUND . NO_DATA , IP-. . NO_DATA NO_ADDRESS. NO_RECOVERY . TRY_AGAIN . . /etc/host.conf /etc/hosts /etc/nsswitch.conf attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |gethostbyname() | | MT-Unsafe | | | | race:hostbyname env | | | | locale | +----------------------------+----------------------------------------------------------+--------------------------+ |gethostbyaddr() | | MT-Unsafe | | | | race:hostbyaddr env | | | | locale | +----------------------------+----------------------------------------------------------+--------------------------+ |sethostent(), endhostent(), | | MT-Unsafe race:hostent | |gethostent_r() | | env locale | +----------------------------+----------------------------------------------------------+--------------------------+ |herror(), hstrerror() | | MT-Safe | +----------------------------+----------------------------------------------------------+--------------------------+ |gethostent() | | MT-Unsafe race:hostent | | | | race:hostentbuf env | | | | locale | +----------------------------+----------------------------------------------------------+--------------------------+ |gethostbyname2() | | MT-Unsafe | | | | race:hostbyname2 env | | | | locale | +----------------------------+----------------------------------------------------------+--------------------------+ |gethostbyaddr_r(), | | MT-Safe env locale | |gethostbyname_r(), | | | |gethostbyname2_r() | | | +----------------------------+----------------------------------------------------------+--------------------------+ In the above table, hostent in race:hostent signifies that if any of the functions sethostent(), gethostent(), gethostent_r(), or endhostent() are used in parallel in different threads of a program, then data races could occur. sethostent() endhostent() gethostent() POSIX.1-2008. gethostent_r() GNU. : . sethostent() endhostent() gethostent() POSIX.1-2001. gethostbyname() gethostbyaddr() h_errno Marked obsolescent in POSIX.1-2001. Removed in POSIX.1-2008, recommending the use of getaddrinfo(3) and getnameinfo(3) instead. gethostbyname() gethostbyaddr() , . struct hostent , ; . BSD len gethostbyname() int. SUSv2 len gethostbyaddr() size_t ( , int, size_t. POSIX.1-2001 -- socklen_t, ). accept(2). BSD gethostbyaddr() const char *. System V/POSIX POSIX gethostent(), . DNS/BIND , , -- , . /etc/hosts. DNS. glibc ipv6. ; glibc gethostent_r(). GNU glibc2 also has a gethostbyname2() that works like gethostbyname(), but permits to specify the address family to which the address must belong. glibc2 also has reentrant versions gethostent_r(), gethostbyaddr_r(), gethostbyname_r(), and gethostbyname2_r(). The caller supplies a hostent structure ret which will be filled in on success, and a temporary work buffer buf of size buflen. After the call, result will point to the result on success. In case of an error or if no entry is found result will be NULL. The functions return 0 on success and a nonzero error number on failure. In addition to the errors returned by the nonreentrant versions of these functions, if buf is too small, the functions will return ERANGE, and the call should be retried with a larger buffer. The global variable h_errno is not modified, but the address of a variable in which to store error numbers is passed in h_errnop. gethostbyname() IPv4 , . getaddrinfo(3), getnameinfo(3), inet(3), inet_ntop(3), inet_pton(3), resolver(3), hosts(5), nsswitch.conf(5), hostname(7), named(8) () Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov , Kirill Rekhov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 2 2024 . gethostbyname(3)