#include <netdb.h>
void sethostent(int stayopen);
void endhostent(void);
[[deprecated]] extern int h_errno;
[[deprecated]] struct hostent *gethostbyname(const char *name);
[[deprecated]] struct hostent *gethostbyaddr(socklen_t size;
const void addr[size],
socklen_t size, int type);
[[deprecated]] void herror(const char *s);
[[deprecated]] const char *hstrerror(int err);
/* System V/POSIX 拡張 */
struct hostent *gethostent(void);
/* GNU 拡張 */
[[deprecated]]
struct hostent *gethostbyname2(const char *name, int af);
int gethostent_r(size_t bufsize;
struct hostent *restrict ret,
char buf[restrict bufsize], size_t bufsize,
struct hostent **restrict result,
int *restrict h_errnop);
[[deprecated]]
int gethostbyaddr_r(socklen_t size, size_t bufsize;
const void addr[restrict size], socklen_t size,
int type,
struct hostent *restrict ret,
char buf[restrict bufsize], size_t bufsize,
struct hostent **restrict result,
int *restrict h_errnop);
[[deprecated]]
int gethostbyname_r(size_t bufsize;
const char *restrict name,
struct hostent *restrict ret,
char buf[restrict bufsize], size_t bufsize,
struct hostent **restrict result,
int *restrict h_errnop);
[[deprecated]]
int gethostbyname2_r(size_t bufsize;
const char *restrict name, int af,
struct hostent *restrict ret,
char buf[restrict bufsize], size_t bufsize,
struct hostent **restrict result,
int *restrict h_errnop);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
gethostbyname2(), gethostent_r(), gethostbyaddr_r(), gethostbyname_r(), gethostbyname2_r():
glibc 2.19 以降:
_DEFAULT_SOURCE
glibc 2.19 以前:
_BSD_SOURCE || _SVID_SOURCE
herror(), hstrerror():
glibc 2.19 以降:
_DEFAULT_SOURCE
glibc 2.8 から glibc 2.19:
_BSD_SOURCE || _SVID_SOURCE
glibc 2.8 より前:
none
h_errno:
glibc 2.19 以降
_DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L
glibc 2.12 から glibc 2.19:
_BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L
glibc 2.12 より前:
none
sethostent() 関数は、ネームサーバへの接続形態を指定する。 stayopen が真 (1) ならば、ネームサーバへの問い合わせには、 接続された TCP ソケットを用い、連続した問い合わせの間に接続を維持する。 偽ならばネームサーバへの問い合わせに UDP データグラムを用いる。
endhostent() 関数はネームサーバへの問い合わせに用いた TCP 接続の利用を終了する。
The gethostbyname() function returns a structure of type hostent for the given host name. Here name is either a hostname or an IPv4 address in standard dot notation (as for inet_addr(3)). If name is an IPv4 address, no lookup is performed and gethostbyname() simply copies name into the h_name field and its struct in_addr equivalent into the h_addr_list[0] field of the returned hostent structure. If name doesn't end in a dot and the environment variable HOSTALIASES is set, the alias file pointed to by HOSTALIASES will first be searched for name (see hostname(7) for the file format). The current domain and its parents are searched unless name ends in a dot.
The gethostbyaddr() function returns a structure of type hostent for the given host address addr of size size and address type type. Valid address types are AF_INET and AF_INET6 (defined in <sys/socket.h>). 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() 関数は現在の h_errno に対応するエラーメッセージを標準エラー stderr に出力する。
The (obsolete) hstrerror() function takes an error number (typically h_errno) and returns the corresponding message string.
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)).
In glibc 2.4 and earlier, the order keyword was used to control the order of host lookups as defined in /etc/host.conf (host.conf(5)).
hostent 構造体は <netdb.h> で以下のように定義されている:
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* size of address */
char **h_addr_list; /* list of addresses */
}
#define h_addr h_addr_list[0] /* for backward compatibility */
hostent 構造体のメンバは以下の通り。
| Interface | Attribute | Value |
| gethostbyname() | Thread safety |
MT-Unsafe race:hostbyname env
locale
|
| gethostbyaddr() | Thread safety |
MT-Unsafe race:hostbyaddr env
locale
|
| sethostent(), endhostent(), gethostent_r() | Thread safety |
MT-Unsafe race:hostent env
locale
|
| herror(), hstrerror() | Thread safety | MT-Safe |
| gethostent() | Thread safety |
MT-Unsafe race:hostent
race:hostentbuf env locale
|
| gethostbyname2() | Thread safety |
MT-Unsafe race:hostbyname2
env locale
|
| gethostbyaddr_r(), gethostbyname_r(), gethostbyname2_r() | Thread safety | MT-Safe env locale |
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.
In the original BSD implementation the size argument of gethostbyname() was an int. The SUSv2 standard is buggy and declares the size argument of gethostbyaddr() to be of type size_t. (That is wrong, because it has to be int, and size_t is not. POSIX.1-2001 makes it socklen_t, which is OK.) See also accept(2).
gethostbyaddr() の BSD のプロトタイプは、最初の引数として const char * を使う。
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 bufsize. 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.