.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" Modified 1993-05-22, David Metcalfe .\" Modified 1993-07-25, Rik Faith (faith@cs.unc.edu) .\" Modified 1997-02-16, Andries Brouwer (aeb@cwi.nl) .\" Modified 1998-12-21, Andries Brouwer (aeb@cwi.nl) .\" Modified 2000-08-12, Andries Brouwer (aeb@cwi.nl) .\" Modified 2001-05-19, Andries Brouwer (aeb@cwi.nl) .\" Modified 2002-08-05, Michael Kerrisk .\" Modified 2004-10-31, Andries Brouwer .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1998-2000 NAKANO Takeo all rights reserved. .\" Translated 1998-04-30, NAKANO Takeo .\" Modified 1998-12-06, NAKANO Takeo .\" Updated & Modified 1999-10-12, NAKANO Takeo .\" Updated & Modified 2001-07-01, Yuichi SATO .\" Updated & Modified 2002-01-03, Yuichi SATO .\" Updated & Modified 2003-11-27, Yuichi SATO .\" Updated & Modified 2005-01-10, Yuichi SATO .\" Updated 2006-01-18, Akihiro MOTOKI .\" Updated 2007-06-11, Akihiro MOTOKI, LDP v2.54 .\" Updated 2008-08-08, Akihiro MOTOKI, LDP v3.05 .\" .TH GETHOSTBYNAME 3 2020\-12\-21 "" "Linux Programmer's Manual" .SH 名前 gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent, h_errno, herror, hstrerror, gethostbyaddr_r, gethostbyname2, gethostbyname2_r, gethostbyname_r, gethostent_r \- ネットワーク上のホストのエントリーを取得する .SH 書式 .nf \fB#include \fP \fBextern int h_errno;\fP .PP \fBstruct hostent *gethostbyname(const char *\fP\fIname\fP\fB);\fP \fB#include \fP /* AF_INET を使う場合 */ \fBstruct hostent *gethostbyaddr(const void *\fP\fIaddr\fP\fB,\fP \fB socklen_t \fP\fIlen\fP\fB, int \fP\fItype\fP\fB);\fP .PP \fBvoid sethostent(int \fP\fIstayopen\fP\fB);\fP .PP \fBvoid endhostent(void);\fP .PP \fBvoid herror(const char *\fP\fIs\fP\fB);\fP .PP \fBconst char *hstrerror(int \fP\fIerr\fP\fB);\fP .PP /* System V/POSIX 拡張 */ \fBstruct hostent *gethostent(void);\fP .PP /* GNU 拡張 */ \fBstruct hostent *gethostbyname2(const char *\fP\fIname\fP\fB, int \fP\fIaf\fP\fB);\fP .PP \fBint gethostent_r(\fP \fB struct hostent *\fP\fIret\fP\fB, char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB,\fP \fB struct hostent **\fP\fIresult\fP\fB, int *\fP\fIh_errnop\fP\fB);\fP .PP \fBint gethostbyaddr_r(const void *\fP\fIaddr\fP\fB, socklen_t \fP\fIlen\fP\fB, int \fP\fItype\fP\fB,\fP \fB struct hostent *\fP\fIret\fP\fB, char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB,\fP \fB struct hostent **\fP\fIresult\fP\fB, int *\fP\fIh_errnop\fP\fB);\fP .PP \fBint gethostbyname_r(const char *\fP\fIname\fP\fB,\fP \fB struct hostent *\fP\fIret\fP\fB, char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB,\fP \fB struct hostent **\fP\fIresult\fP\fB, int *\fP\fIh_errnop\fP\fB);\fP .PP \fBint gethostbyname2_r(const char *\fP\fIname\fP\fB, int \fP\fIaf,\fP \fB struct hostent *\fP\fIret\fP\fB, char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB,\fP \fB struct hostent **\fP\fIresult\fP\fB, int *\fP\fIh_errnop\fP\fB);\fP .fi .PP .RS -4 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .RE .PP .PD 0 .ad l \fBgethostbyname2\fP(), \fBgethostent_r\fP(), \fBgethostbyaddr_r\fP(), \fBgethostbyname_r\fP(), \fBgethostbyname2_r\fP(): .RS 4 .TP 4 glibc 2.19 以降: _DEFAULT_SOURCE .TP 4 Glibc versions up to and including 2.19: _BSD_SOURCE || _SVID_SOURCE .RE .PD .PP .PD 0 \fBherror\fP(), \fBhstrerror\fP(): .RS 4 .TP 4 glibc 2.19 以降: _DEFAULT_SOURCE .TP 4 Glibc 2.8 to 2.19: _BSD_SOURCE || _SVID_SOURCE .TP glibc 2.8 より前: なし .RE .PD .PP .PD 0 \fBh_errno\fP: .RS 4 .TP 4 glibc 2.19 以降: _DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L .TP 4 glibc 2.12 から 2.19 まで: _BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L .TP glibc 2.12 より前: なし .RE .ad b .PD .SH 説明 関数 \fBgethostbyname*\fP(), \fBgethostbyaddr*\fP(), \fBherror\fP(), \fBhstrerror\fP は過去のものである。 アプリケーションは、代わりに \fBgetaddrinfo\fP(3), \fBgetnameinfo\fP(3), \fBgai_strerror\fP(3) を使用すること。 .PP The \fBgethostbyname\fP() function returns a structure of type \fIhostent\fP for the given host \fIname\fP. Here \fIname\fP is either a hostname or an IPv4 address in standard dot notation (as for \fBinet_addr\fP(3)). If \fIname\fP is an IPv4 address, no lookup is performed and \fBgethostbyname\fP() simply copies \fIname\fP into the \fIh_name\fP field and its \fIstruct in_addr\fP equivalent into the \fIh_addr_list[0]\fP field of the returned \fIhostent\fP structure. If \fIname\fP doesn't end in a dot and the environment variable \fBHOSTALIASES\fP is set, the alias file pointed to by \fBHOSTALIASES\fP will first be searched for \fIname\fP (see \fBhostname\fP(7) for the file format). The current domain and its parents are searched unless \fIname\fP ends in a dot. .PP \fBgethostbyaddr\fP() 関数は与えられたホストアドレス \fIaddr\fP (長さ \fIlen\fP、 タイプ \fItype\fP) に対応する構造体 \fIhostent\fP を返す。 用いることのできるタイプは \fBAF_INET\fP と \fBAF_INET6\fP である。 ホストアドレス引数はアドレスタイプに依存した 構造体へのポインターである。 例えば、アドレスタイプ \fBAF_INET\fP に対しては (\fBinet_addr\fP(3) の呼び出しで得られる) \fIstruct in_addr *\fP である。 .PP \fBsethostent\fP() 関数は、ネームサーバへの接続形態を指定する。 \fIstayopen\fP が真 (1) ならば、ネームサーバへの問い合わせには、 接続された TCP ソケットを用い、連続した問い合わせの間に接続を維持する。 偽ならばネームサーバへの問い合わせに UDP データグラムを用いる。 .PP \fBendhostent\fP() 関数はネームサーバへの問い合わせに用いた TCP 接続の利用を終了する。 .PP (廃止予定の) \fBherror\fP() 関数は現在の \fIh_errno\fP に対応するエラーメッセージを標準エラー \fIstderr\fP に出力する。 .PP (廃止予定の) \fBhstrerror\fP() 関数はエラー番号 (通常は \fIh_errno\fP) を引数に取り、 対応するエラーメッセージ文字列を返す。 .PP .\" The domain name queries carried out by \fBgethostbyname\fP() and \fBgethostbyaddr\fP() rely on the Name Service Switch (\fBnsswitch.conf\fP(5)) configured sources or a local name server (\fBnamed\fP(8)). The default action is to query the Name Service Switch (\fBnsswitch.conf\fP(5)) configured sources, failing that, a local name server (\fBnamed\fP(8)). .SS Historical The \fBnsswitch.conf\fP(5) file is the modern way of controlling the order of host lookups. .PP In glibc 2.4 and earlier, the \fIorder\fP keyword was used to control the order of host lookups as defined in \fI/etc/host.conf\fP (\fBhost.conf\fP(5)). .PP \fIhostent\fP 構造体は \fI\fP で以下のように定義されている: .PP .in +4n .EX 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] /* 過去との互換性のため */ .EE .in .PP \fIhostent\fP 構造体のメンバは以下の通り。 .TP \fIh_name\fP ホストの正式名 (official name)。 .TP \fIh_aliases\fP ホストの別名の配列。配列はヌルポインターで終端される。 .TP \fIh_addrtype\fP アドレスのタイプ。現在はすべて \fBAF_INET\fP または \fBAF_INET6\fP である。 .TP \fIh_length\fP バイト単位で表したアドレスの長さ。 .TP \fIh_addr_list\fP ホストのネットワークアドレスへのポインターの配列。 配列はヌルポインターで終端される。 ネットワークアドレスはネットワークバイトオーダ形式である。 .TP \fIh_addr\fP \fIh_addr_list\fP の最初のアドレス。過去との互換性を保つためのものである。 .SH 返り値 \fBgethostbyname\fP() および \fBgethostbyaddr\fP() 関数は \fIhostent\fP 構造体を返す。エラーが起こったらヌルポインターを返す。エラーの際には \fIh_errno\fP 変数がエラーの番号を保持する。 返り値が NULL でない場合、静的データをポインターで指していることもある。 以下の「注意」を参照すること。 .SH エラー \fIh_errno\fP 変数は以下の値を取りうる。 .TP \fBHOST_NOT_FOUND\fP 指定したホストが見つからない。 .TP \fBNO_DATA\fP 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 \fBNO_ADDRESS\fP is a synonym for \fBNO_DATA\fP. .TP \fBNO_RECOVERY\fP ネームサーバの復旧不能なエラーが起こった。 .TP \fBTRY_AGAIN\fP authoritative なネームサーバで一時的なエラーが起こった。 時間をおいてもう一度試すこと。 .SH ファイル .TP \fI/etc/host.conf\fP 名前解決の設定ファイル .TP \fI/etc/hosts\fP ホストのデータベースファイル .TP \fI/etc/nsswitch.conf\fP ネームサービス切替設定 .SH 属性 この節で使用されている用語の説明は \fBattributes\fP(7) を参照のこと。 .TS allbox; lbw18 lb lbw29 l l l. Interface Attribute Value T{ \fBgethostbyname\fP() T} Thread safety T{ MT\-Unsafe race:hostbyname env .br locale T} T{ \fBgethostbyaddr\fP() T} Thread safety T{ MT\-Unsafe race:hostbyaddr env .br locale T} T{ \fBsethostent\fP(), .br \fBendhostent\fP(), .br \fBgethostent_r\fP() T} Thread safety T{ MT\-Unsafe race:hostent env .br locale T} T{ \fBherror\fP(), .br \fBhstrerror\fP() T} Thread safety MT\-Safe T{ \fBgethostent\fP() T} Thread safety T{ MT\-Unsafe race:hostent .br race:hostentbuf env locale T} T{ \fBgethostbyname2\fP() T} Thread safety T{ MT\-Unsafe race:hostbyname2 .br env locale T} T{ \fBgethostbyaddr_r\fP(), \fBgethostbyname_r\fP(), \fBgethostbyname2_r\fP() T} Thread safety MT\-Safe env locale .TE .sp 1 In the above table, \fIhostent\fP in \fIrace:hostent\fP signifies that if any of the functions \fBsethostent\fP(), \fBgethostent\fP(), \fBgethostent_r\fP(), or \fBendhostent\fP() are used in parallel in different threads of a program, then data races could occur. .SH 準拠 POSIX.1\-2001 では、 \fBgethostbyname\fP(), \fBgethostbyaddr\fP(), \fBsethostent\fP(), \fBendhostent\fP(), \fBgethostent\fP(), \fIh_errno\fP が規定されており、 \fBgethostbyaddr\fP() と \fBgethostbyname\fP() は廃止予定であるとされている。 POSIX.1\-2008 では \fBgethostbyname\fP(), \fBgethostbyaddr\fP(), \fIh_errno\fP の仕様が削除されている。 代わりに、 \fBgetaddrinfo\fP(3) と \fBgetnameinfo\fP(3) の使用が推奨されている。 .SH 注意 \fBgethostbyname\fP() および \fBgethostbyaddr\fP() 関数は静的データへのポインターを返す。 このポインターは、その後の呼び出しで上書きされるかもしれない。 \fIhostent\fP 構造体はポインターを含んでいるので、構造体のコピーだけでは不十分である; より深いコピーが必要である。 .PP オリジナルの BSD の実装では、 \fBgethostbyname\fP() の \fIlen\fP 引数は \fIint\fP であった。 SUSv2 標準はバグが多く、 \fBgethostbyaddr\fP() の \fIlen\fP パラメーターを \fIsize_t\fP 型として宣言している。 (これは誤りで、 \fIsize_t\fP 型ではなく \fIint\fP 型でなければならない。 POSIX.1\-2001 ではこれを \fIsocklen_t\fP としているが、これは OK。) \fBaccept\fP(2) も参照。 .PP \fBgethostbyaddr\fP() の BSD のプロトタイプは、最初の引数として \fIconst char\ *\fP を使う。 .SS "System V/POSIX 拡張" .\" e.g., Linux, FreeBSD, UnixWare, HP-UX .\" e.g., FreeBSD, AIX POSIX では、 \fBgethostent\fP() が必須とされている。 この関数はホストデータベースの次のエントリーを返す。 DNS/BIND を使う場合はあまり意味を持たないが、 ホストデータベースが 1 行ずつ読み込まれるファイルである場合は意味がある。 多くのシステムでは、この名前のルーチンはファイル \fI/etc/hosts\fP を読み込む。 DNS サポートなしでライブラリがビルドされた場合にのみ利用可能である。 glibc 版は ipv6 エントリーを無視する。 この関数はリエントラント (reentrant) ではなく、 glibc にはリエントラント版の \fBgethostent_r\fP() が追加された。 .SS "GNU 拡張" glibc2 には \fBgethostbyname2\fP() もあり、 \fBgethostbyname\fP() と同じように動作するが、 こちらはアドレスが属するアドレスファミリーを指定することができる。 .PP glibc2 にはリエントラントな \fBgethostent_r\fP(), \fBgethostbyaddr_r\fP(), \fBgethostbyname_r\fP() と \fBgethostbyname2_r\fP() もある。 呼び出し側は、成功時に結果が格納される \fIhostent\fP 構造体 \fIret\fP と、大きさ \fIbuflen\fP の一時的な作業バッファー \fIbuf\fP を提供する。 コール終了後、成功した場合 \fIresult\fP は結果を指している。 エラーの場合、またはエントリーが見つからなかった場合、 \fIresult\fP は NULL になる。 これらの関数は、成功した場合 0 を返し、失敗の場合は 0 以外のエラー番号を返す。 これらの関数のリエントラントでないバージョンが返すエラーに加えて、 これらの関数は、 \fIbuf\fP が小さすぎた場合に \fBERANGE\fP を返す。この場合はもっと大きなバッファーを用意して 関数呼び出しを再度行うべきである。 大域変数 \fIh_errno\fP は変更されないが、エラー番号を格納する変数のアドレスが \fIh_errnop\fP に渡される。 .SH バグ .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482973 \fBgethostbyname\fP() は、16進数表現のドット区切りの IPv4 アドレス文字列の要素を認識しない。 .SH 関連項目 .\" .BR getipnodebyaddr (3), .\" .BR getipnodebyname (3), .\" .BR resolv+ (8) \fBgetaddrinfo\fP(3), \fBgetnameinfo\fP(3), \fBinet\fP(3), \fBinet_ntop\fP(3), \fBinet_pton\fP(3), \fBresolver\fP(3), \fBhosts\fP(5), \fBnsswitch.conf\fP(5), \fBhostname\fP(7), \fBnamed\fP(8) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。