gethostbyname

GETHOSTBYNAME(3)           Linux Programmer's Manual          GETHOSTBYNAME(3)



åå
       gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
       h_errno, herror, hstrerror, gethostbyaddr_r, gethostbyname2,
       gethostbyname2_r, gethostbyname_r, gethostent_r -
       ãããã¯ã¼ã¯ä¸ã®ãã¹ãã®ã¨ã³ããªãåå¾ãã

æ¸å¼
       #include <netdb.h>
       extern int h_errno;

       struct hostent *gethostbyname(const char *name);

       #include <sys/socket.h>       /* 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

       herror(), hstrerror():
           glibc 2.8 以é:
               _BSD_SOURCE || _SVID_SOURCE || _GNU_SOURCE
           glibc 2.8 ããå:
               ãªã

説æ
       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 æ§é ä½ã¯ <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;          /* 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()  é¢æ°ã¯éçãã¼ã¿ã¸ã®ãã¤ã³ã¿ãè¿ãã
       ãã®ãã¤ã³ã¿ã¯ããã®å¾ã®å¼ã³åºãã§ä¸æ¸ãããããããããªãã hostent
       æ§é ä½ã¯ãã¤ã³ã¿ãå«ãã§ããã®ã§ãæ§é ä½ã®ã³ãã¼ã ãã§ã¯ä¸ååã§ãã;
       ããæ·±ãã³ãã¼ãå¿è¦ã§ããã

       ãªãªã¸ãã«ã® 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(),
       gethostbyname_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)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



                                  2010-10-04                  GETHOSTBYNAME(3)