gethostbyname

Section: C Library Functions (3)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent, h_errno, herror, hstrerror, gethostbyaddr_r, gethostbyname2, gethostbyname2_r, gethostbyname_r, gethostent_r - ネットワーク上のホストのエントリーを取得する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#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
 

説明

関数 gethostbyname*(), gethostbyaddr*(), herror(), hstrerror は過去のものである。 アプリケーションは、代わりに getaddrinfo(3), getnameinfo(3), gai_strerror(3) を使用すること。

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)).  

Historical

The nsswitch.conf(5) file is the modern way of controlling the order of host lookups.

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 構造体のメンバは以下の通り。

h_name
ホストの正式名 (official name)。
h_aliases
ホストの別名の配列。配列はヌルポインターで終端される。
h_addrtype
アドレスのタイプ。現在はすべて AF_INET または AF_INET6 である。
h_length
The size of the address in bytes.
h_addr_list
ホストのネットワークアドレスへのポインターの配列。 配列はヌルポインターで終端される。 ネットワークアドレスはネットワークバイトオーダ形式である。
h_addr
h_addr_list の最初のアドレス。過去との互換性を保つためのものである。
 

返り値

gethostbyname() および gethostbyaddr() 関数は hostent 構造体を返す。エラーが起こったらヌルポインターを返す。エラーの際には h_errno 変数がエラーの番号を保持する。 返り値が NULL でない場合、静的データをポインターで指していることもある。 以下の「注意」を参照すること。  

エラー

h_errno 変数は以下の値を取りうる。
HOST_NOT_FOUND
指定したホストが見つからない。
NO_DATA
The requested name is valid but does not have an IP address. Another type of request to the name server for this domain may return an answer. The constant NO_ADDRESS is a synonym for NO_DATA.
NO_RECOVERY
ネームサーバの復旧不能なエラーが起こった。
TRY_AGAIN
authoritative なネームサーバで一時的なエラーが起こった。 時間をおいてもう一度試すこと。
 

ファイル

/etc/host.conf
名前解決の設定ファイル
/etc/hosts
ホストのデータベースファイル
/etc/nsswitch.conf
ネームサービス切替設定
 

属性

この節で使用されている用語の説明は attributes(7) を参照のこと。
InterfaceAttributeValue
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 safetyMT-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 safetyMT-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.  

標準規格

sethostent()
endhostent()
gethostent()
POSIX.1-2008.
gethostent_r()
GNU.
Others:
なし。
 

歴史

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() 関数は静的データへのポインターを返す。 このポインターは、その後の呼び出しで上書きされるかもしれない。 hostent 構造体はポインターを含んでいるので、構造体のコピーだけでは不十分である; より深いコピーが必要である。

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 * を使う。  

System V/POSIX 拡張

POSIX では、 gethostent() が必須とされている。 この関数はホストデータベースの次のエントリーを返す。 DNS/BIND を使う場合はあまり意味を持たないが、 ホストデータベースが 1 行ずつ読み込まれるファイルである場合は意味がある。 多くのシステムでは、この名前のルーチンはファイル /etc/hosts を読み込む。 DNS サポートなしでライブラリがビルドされた場合にのみ利用可能である。 glibc 版は ipv6 エントリーを無視する。 この関数はリエントラント (reentrant) ではなく、 glibc にはリエントラント版の gethostent_r() が追加された。  

GNU 拡張

glibc2 には gethostbyname2() もあり、 gethostbyname() と同じように動作するが、 こちらはアドレスが属するアドレスファミリーを指定することができる。

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.  

バグ

gethostbyname() は、16進数表現のドット区切りの IPv4 アドレス文字列の要素を認識しない。  

関連項目

getaddrinfo(3), getnameinfo(3), inet(3), inet_ntop(3), inet_pton(3), resolver(3), hosts(5), nsswitch.conf(5), hostname(7), named(8)


 

Index

名前
ライブラリ
書式
説明
Historical
返り値
エラー
ファイル
属性
標準規格
歴史
注意
System V/POSIX 拡張
GNU 拡張
バグ
関連項目

This document was created by man2html, using the manual pages.