gethostbynameのヘルプ・マニュアル
日本語 英語
gethostbyname --help
man gethostbyname
GETHOSTBYNAME(3) Linux Programmer’s Manual GETHOSTBYNAME(3)
名前
gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
h_errno, herror, hstrerror, gethostbyaddr_r, gethostbyname2, gethostby-
name2_r, gethostbyname_r, gethostent_r - ネットワーク上のホストのエント
リを取得する
書式
#include
extern int h_errno;
struct hostent *gethostbyname(const char *name);
#include /* AF_INET を使う場合 */
struct hostent *gethostbyaddr(const void *addr,
socklen_t len, int type);
void sethostent(int stayopen);
void endhostent(void);
void herror(const char *s);
const char *hstrerror(int err);
/* System V/POSIX 拡張 */
struct hostent *gethostent(void);
/* GNU 拡張 */
struct hostent *gethostbyname2(const char *name, int af);
int gethostent_r(
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyaddr_r(const void *addr, socklen_t len, int type,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyname_r(const char *name,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyname2_r(const char *name, int af,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
gethostbyname2(), gethostent_r(), gethostbyaddr_r(), gethostbyname_r(),
gethostbyname2_r(): _BSD_SOURCE || _SVID_SOURCE
説明
gethostbyname*() と gethostbyaddr*() は過去のものである。アプリケーショ
ンでは、代わりに getaddrinfo(3) と getnameinfo(3) を使用すること。
gethostbyname() 関数は与えられたホスト名 name に対応する構造体 hostent
を 返す。 name にはホスト名、ドット区切りの IPv4 アドレス (inet_addr(3)
参照)、コロン区切りの IPv6 アドレス (おそらくドット区切りでも大丈夫) の
いずれかを指定する (IPv6 アドレスの記述方法については RFC 1884 を参考に
してほしい)。 name が IPv4 か IPv6 のアドレスだった場 合 、 名 前 解 決
(lookup) は行われない。その場合には、 gethostbyname() は name をそのま
ま hostent 構造体の h_name フィールドにコピーし、さらに name を struct
in_addr 形式で表したデータを hostent 構造体の h_addr_list[0] フィールド
に入れて、その hostent 構造体を返す。 name がドットで終了していて、かつ
環 境変数 HOSTALIASES が設定されている場合、まず HOSTALIASES で指定され
ているエイリアスファイルから name のエントリが検索される (ファイルの フ
ォ ーマットについては hostname(7) を参照のこと)。 name がドットで終了し
ていなければ、現在のドメインとその親ドメインが検索される。
gethostbyaddr() 関数は与えられたホストアドレス addr (長さ len、 タイ プ
type) に 対 応 す る構造体 hostent を返す。用いることのできるタイプは
AF_INET と AF_INET6 である。ホストアドレス引き数はアドレスタイプに依 存
した構造体へのポインタである。例えば、アドレスタイプ AF_INET に対しては
(inet_addr(3) の呼び出しで得られる) struct in_addr * である。
sethostent() 関数は、ネームサーバへの接続形態を指定する。 stayopen が真
(1) ならば、ネームサーバへの問い合わせには、接続された TCP ソケットを用
い、連続した問い合わせの間に接続を維持する。偽ならばネームサーバへの 問
い合わせに UDP データグラムを用いる。
endhostent() 関数はネームサーバへの問い合わせに用いた TCP 接続の利用を
終了する。
(廃止予定の) herror() 関数は現在の h_errno に対応するエラーメッセージを
標準エラー stderr に出力する。
(廃止予定の) hstrerror() 関数はエラー番号 (通常は h_errno) を引き数に取
り、対応するエラーメッセージ文字列を返す。
gethostbyname() と gethostbyaddr() によって実行されるドメイン名の問い合
わせでは、ネームサーバ named(8)、 /etc/hosts のデータ行、および Network
Information Service (NIS または YP) が組み合わせて使用される。何が使 用
されるかは、 /etc/host.conf の order 行の内容により決まる。デフォルトで
は、まず named(8) に問い合わせを行い、次いで /etc/hosts を参照する。
hostent 構造体は で以下のように定義されている:
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
}
#define h_addr h_addr_list[0] /* 過去との互換性のため */
hostent 構造体のメンバは以下の通り。
h_name ホストの正式名 (official 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_ADDRESS または NO_DATA
指定した名前は有効だが IP アドレスを持っていない。
NO_RECOVERY
ネームサーバの復旧不能なエラーが起こった。
TRY_AGAIN
authoritative なネームサーバで一時的なエラーが起こった。時間をお
いてもう一度試すこと。
ファイル
/etc/host.conf
名前解決の設定ファイル
/etc/hosts
ホストのデータベースファイル
/etc/nsswitch.conf
ネームサービス切替設定
準拠
POSIX.1-2001 で は 、 gethostbyname(), gethostbyaddr(), sethostent(),
endhostent(), gethostent(), h_errno が規定されており、 gethostbyaddr()
と gethostbyname() は 廃止予定であるとされている。 POSIX.1-2008 では
gethostbyname(), gethostbyaddr(), h_errno の仕様が削除されている。代 わ
りに、 getaddrinfo(3) と getnameinfo(3) の使用が推奨されている。
注意
gethostbyname() および gethostbyaddr() 関数は静的データへのポインタを返
す。このポインタは、その後の呼び出しで上書きされるかもしれない 。 hos-
tent 構造体はポインタを含んでいるので、構造体のコピーだけでは不十分であ
る; より深いコピーが必要である。
オリジナルの BSD の実装では、 gethostbyname() の len 引き数は int で あ
っ た 。 SUSv2 標準はバグが多く、 gethostbyaddr() の len パラメータを
size_t 型として宣言している。 (これは誤りで、 size_t 型ではなく int 型
でなければならない。 POSIX.1-2001 ではこれを socklen_t としているが、こ
れは OK。) 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 にはリエントラントな gethostent_r(), gethostbyaddr_r(), gethost-
byname_r() と gethostbyname2_r() もある。呼び出し側は、成功時に結果が格
納される hostent 構造体 ret と、大きさ buflen の一時的な作業バ ッ フ ァ
buf を提供する。コール終了後、成功した場合 result は結果を指している。
エラーの場合、またはエントリが見つからなかった場合、 result は NULL に
な る。これらの関数は、成功した場合 0 を返し、失敗の場合は 0 以外のエラ
ー番号を返す。これらの関数のリエントラントでないバージョンが返すエラ ー
に加えて、これらの関数は、 buf が小さすぎた場合に ERANGE を返す。この場
合はもっと大きなバッファを用意して関数呼び出しを再度行うべきである。 大
域 変 数 h_errno は変更されないが、エラー番号を格納する変数のアドレスが
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)
2009-03-15 GETHOSTBYNAME(3)
GETHOSTBYNAME(3) Linux Programmer’s Manual GETHOSTBYNAME(3)
NAME
gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
h_errno, herror, hstrerror, gethostbyaddr_r, gethostbyname2, gethostby-
name2_r, gethostbyname_r, gethostent_r - get network host entry
SYNOPSIS
#include
extern int h_errno;
struct hostent *gethostbyname(const char *name);
#include /* for AF_INET */
struct hostent *gethostbyaddr(const void *addr,
socklen_t len, int type);
void sethostent(int stayopen);
void endhostent(void);
void herror(const char *s);
const char *hstrerror(int err);
/* System V/POSIX extension */
struct hostent *gethostent(void);
/* GNU extensions */
struct hostent *gethostbyname2(const char *name, int af);
int gethostent_r(
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyaddr_r(const void *addr, socklen_t len, int type,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyname_r(const char *name,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyname2_r(const char *name, int af,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
gethostbyname2(), gethostent_r(), gethostbyaddr_r(), gethostbyname_r(),
gethostbyname2_r(): _BSD_SOURCE || _SVID_SOURCE
DESCRIPTION
The gethostbyname*() and gethostbyaddr*() functions are obsolete.
Applications should use getaddrinfo(3) and getnameinfo(3) instead.
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)), or an IPv6
address in colon (and possibly dot) notation. (See RFC 1884 for the
description of IPv6 addresses.) If name is an IPv4 or IPv6 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 length len and address type type. Valid
address types are AF_INET and AF_INET6. The host address argument is a
pointer to a struct of a type depending on the address type, for exam-
ple a struct in_addr * (probably obtained via a call to inet_addr(3))
for address type AF_INET.
The sethostent() function specifies, if stayopen is true (1), that a
connected TCP socket should be used for the name server queries and
that the connection should remain open during successive queries. Oth-
erwise, name server queries will use UDP datagrams.
The endhostent() function ends the use of a TCP connection for name
server queries.
The (obsolete) herror() function prints the error message associated
with the current value of h_errno on 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 gethost-
byaddr() use a combination of any or all of the name server named(8), a
broken out line from /etc/hosts, and the Network Information Service
(NIS or YP), depending upon the contents of the order line in
/etc/host.conf. The default action is to query named(8), followed by
/etc/hosts.
The hostent structure is defined in as follows:
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
}
#define h_addr h_addr_list[0] /* for backward compatibility */
The members of the hostent structure are:
h_name The official name of the host.
h_aliases
An array of alternative names for the host, terminated by a NULL
pointer.
h_addrtype
The type of address; always AF_INET or AF_INET6 at present.
h_length
The length of the address in bytes.
h_addr_list
An array of pointers to network addresses for the host (in net-
work byte order), terminated by a NULL pointer.
h_addr The first address in h_addr_list for backward compatibility.
RETURN VALUE
The gethostbyname() and gethostbyaddr() functions return the hostent
structure or a NULL pointer if an error occurs. On error, the h_errno
variable holds an error number. When non-NULL, the return value may
point at static data, see the notes below.
ERRORS
The variable h_errno can have the following values:
HOST_NOT_FOUND
The specified host is unknown.
NO_ADDRESS or NO_DATA
The requested name is valid but does not have an IP address.
NO_RECOVERY
A non-recoverable name server error occurred.
TRY_AGAIN
A temporary error occurred on an authoritative name server. Try
again later.
FILES
/etc/host.conf
resolver configuration file
/etc/hosts
host database file
/etc/nsswitch.conf
name service switch configuration
CONFORMING TO
POSIX.1-2001 specifies gethostbyname(), gethostbyaddr(), sethostent(),
endhostent(), gethostent(), and h_errno; gethostbyname(), gethost-
byaddr(), and h_errno are marked obsolescent in that standard.
POSIX.1-2008 removes the specifications of gethostbyname(), gethost-
byaddr(), and h_errno, recommending the use of getaddrinfo(3) and get-
nameinfo(3) instead.
NOTES
The functions gethostbyname() and gethostbyaddr() may return pointers
to static data, which may be overwritten by later calls. Copying the
struct hostent does not suffice, since it contains pointers; a deep
copy is required.
In the original BSD implementation the len argument of gethostbyname()
was an int. The SUSv2 standard is buggy and declares the len 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).
The BSD prototype for gethostbyaddr() uses const char * for the first
argument.
System V/POSIX Extension
POSIX requires the gethostent() call, that should return the next entry
in the host data base. When using DNS/BIND this does not make much
sense, but it may be reasonable if the host data base is a file that
can be read line by line. On many systems a routine of this name reads
from the file /etc/hosts. It may be available only when the library
was built without DNS support. The glibc version will ignore ipv6
entries. This function is not reentrant, and glibc adds a reentrant
version gethostent_r().
GNU Extensions
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 hos-
tent 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 non-zero
error number on failure. In addition to the errors returned by the
non-reentrant 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.
BUGS
gethostbyname() does not recognize components of a dotted IPv4 address
string that are expressed in hexadecimal.
SEE ALSO
getaddrinfo(3), getnameinfo(3), inet(3), inet_ntop(3), inet_pton(3),
resolver(3), hosts(5), nsswitch.conf(5), hostname(7), named(8)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
2009-03-15 GETHOSTBYNAME(3)