Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


gethostbyname(3) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |FILES |ATTRIBUTES |STANDARDS |HISTORY |NOTES |BUGS |SEE ALSO |COLOPHON

gethostbyname(3)         Library Functions Manualgethostbyname(3)

NAME        top

       gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,       h_errno, herror, hstrerror, gethostbyaddr_r, gethostbyname2,       gethostbyname2_r, gethostbyname_r, gethostent_r - get network host       entry

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <netdb.h>void sethostent(intstayopen);void endhostent(void);[[deprecated]] extern int h_errno;[[deprecated]] struct hostent *gethostbyname(const char *name);[[deprecated]] struct hostent *gethostbyaddr(socklen_t size;const voidaddr[size],socklen_tsize, inttype);[[deprecated]] void herror(const char *s);[[deprecated]] const char *hstrerror(interr);       /* System V/POSIX extension */struct hostent *gethostent(void);       /* GNU extensions */[[deprecated]]struct hostent *gethostbyname2(const char *name, intaf);int gethostent_r(size_t bufsize;struct hostent *restrictret,charbuf[restrictbufsize], size_tbufsize,struct hostent **restrictresult,int *restricth_errnop);[[deprecated]]int gethostbyaddr_r(socklen_t size, size_t bufsize;const voidaddr[restrictsize], socklen_tsize,inttype,struct hostent *restrictret,charbuf[restrictbufsize], size_tbufsize,struct hostent **restrictresult,int *restricth_errnop);[[deprecated]]int gethostbyname_r(size_t bufsize;const char *restrictname,struct hostent *restrictret,charbuf[restrictbufsize], size_tbufsize,struct hostent **restrictresult,int *restricth_errnop);[[deprecated]]int gethostbyname2_r(size_t bufsize;const char *restrictname, intaf,struct hostent *restrictret,charbuf[restrictbufsize], size_tbufsize,struct hostent **restrictresult,int *restricth_errnop);   Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):gethostbyname2(),gethostent_r(),gethostbyaddr_r(),gethostbyname_r(),gethostbyname2_r():           Since glibc 2.19:               _DEFAULT_SOURCE           glibc up to and including 2.19:               _BSD_SOURCE || _SVID_SOURCEherror(),hstrerror():           Since glibc 2.19:               _DEFAULT_SOURCE           glibc 2.8 to glibc 2.19:               _BSD_SOURCE || _SVID_SOURCE           Before glibc 2.8:               noneh_errno:           Since glibc 2.19               _DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L           glibc 2.12 to glibc 2.19:               _BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L           Before glibc 2.12:               none

DESCRIPTION        top

       Thegethostbyname*(),gethostbyaddr*(),herror(), andhstrerror()       functions are obsolete.  Applications should usegetaddrinfo(3),getnameinfo(3), andgai_strerror(3) instead.       Thesethostent() function specifies, ifstayopen 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.  Otherwise, name server queries will use UDP datagrams.       Theendhostent() function ends the use of a TCP connection for       name server queries.       Thegethostbyname() function returns a structure of typehostent       for the given hostname.  Herename is either a hostname or an       IPv4 address in standard dot notation (as forinet_addr(3)).  Ifname is an IPv4 address, no lookup is performed andgethostbyname() simply copiesname into theh_name field and itsstruct in_addr equivalent into theh_addr_list[0] field of the       returnedhostent structure.  Ifname doesn't end in a dot and the       environment variableHOSTALIASESis set, the alias file pointed to       byHOSTALIASESwill first be searched forname (seehostname(7)       for the file format).  The current domain and its parents are       searched unlessname ends in a dot.       Thegethostbyaddr() function returns a structure of typehostent       for the given host addressaddr of sizelen and address typetype.       Valid address types areAF_INETandAF_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 astruct in_addr * (probably obtained via a call toinet_addr(3))       for address typeAF_INET.       The (obsolete)herror() function prints the error message       associated with the current value ofh_errno onstderr.       The (obsolete)hstrerror() function takes an error number       (typicallyh_errno) and returns the corresponding message string.       The domain name queries carried out bygethostbyname() andgethostbyaddr() 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       Thensswitch.conf(5) file is the modern way of controlling the       order of host lookups.       In glibc 2.4 and earlier, theorder keyword was used to control       the order of host lookups as defined in/etc/host.conf       (host.conf(5)).       Thehostent structure is defined in<netdb.h> 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;          /* size of address */               char **h_addr_list;       /* list of addresses */           }           #define h_addr h_addr_list[0] /* for backward compatibility */       The members of thehostent 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; alwaysAF_INETorAF_INET6at present.h_length              The size of the address in bytes.h_addr_list              An array of pointers to network addresses for the host (in              network byte order), terminated by a null pointer.h_addr The first address inh_addr_list for backward              compatibility.

RETURN VALUE        top

       Thegethostbyname() andgethostbyaddr() functions return thehostent structure or a null pointer if an error occurs.  On error,       theh_errno variable holds an error number.  When non-NULL, the       return value may point at static data, see the notes below.

ERRORS        top

       The variableh_errno can have the following values:HOST_NOT_FOUND              The specified host is unknown.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 constantNO_ADDRESS              is a synonym forNO_DATA.NO_RECOVERY              A nonrecoverable name server error occurred.TRY_AGAIN              A temporary error occurred on an authoritative name server.              Try again later.

FILES        top

/etc/host.conf              resolver configuration file/etc/hosts              host database file/etc/nsswitch.conf              name service switch configuration

ATTRIBUTES        top

       For an explanation of the terms used in this section, seeattributes(7).       ┌────────────────────┬───────────────┬───────────────────────────┐       │InterfaceAttributeValue│       ├────────────────────┼───────────────┼───────────────────────────┤       │gethostbyname()    │ Thread safety │ MT-Unsafe race:hostbyname │       │                    │               │ env locale                │       ├────────────────────┼───────────────┼───────────────────────────┤       │gethostbyaddr()    │ Thread safety │ MT-Unsafe race:hostbyaddr │       │                    │               │ env locale                │       ├────────────────────┼───────────────┼───────────────────────────┤       │sethostent(),      │ Thread safety │ MT-Unsafe race:hostent    │       │endhostent(),      │               │ env locale                │       │gethostent_r()     │               │                           │       ├────────────────────┼───────────────┼───────────────────────────┤       │herror(),          │ Thread safety │ MT-Safe                   │       │hstrerror()        │               │                           │       ├────────────────────┼───────────────┼───────────────────────────┤       │gethostent()       │ Thread safety │ MT-Unsafe race:hostent    │       │                    │               │ race:hostentbuf env       │       │                    │               │ locale                    │       ├────────────────────┼───────────────┼───────────────────────────┤       │gethostbyname2()   │ Thread safety │ MT-Unsafe                 │       │                    │               │ race:hostbyname2 env      │       │                    │               │ locale                    │       ├────────────────────┼───────────────┼───────────────────────────┤       │gethostbyaddr_r(), │ Thread safety │ MT-Safe env locale        │       │gethostbyname_r(), │               │                           │       │gethostbyname2_r() │               │                           │       └────────────────────┴───────────────┴───────────────────────────┘       In the above table,hostent inrace:hostent signifies that if any       of the functionssethostent(),gethostent(),gethostent_r(), orendhostent() are used in parallel in different threads of a       program, then data races could occur.

STANDARDS        top

sethostent()endhostent()gethostent()              POSIX.1-2008.gethostent_r()              GNU.       Others:              None.

HISTORY        top

sethostent()endhostent()gethostent()              POSIX.1-2001.gethostbyname()gethostbyaddr()h_errno              Marked obsolescent in POSIX.1-2001.  Removed in              POSIX.1-2008, recommending the use ofgetaddrinfo(3) andgetnameinfo(3) instead.

NOTES        top

       The functionsgethostbyname() andgethostbyaddr() may return       pointers to static data, which may be overwritten by later calls.       Copying thestruct hostent does not suffice, since it contains       pointers; a deep copy is required.       In the original BSD implementation thesize argument ofgethostbyname() was anint.  The SUSv2 standard is buggy and       declares thesize argument ofgethostbyaddr() to be of typesize_t.  (That is wrong, because it has to beint, andsize_t is       not.  POSIX.1-2001 makes itsocklen_t, which is OK.)  See alsoaccept(2).       The BSD prototype forgethostbyaddr() usesconst char * for the       first argument.System V/POSIX extension       POSIX requires thegethostent() call, which 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 versiongethostent_r().GNU extensions       glibc2 also has agethostbyname2() that works likegethostbyname(), but permits to specify the address family to       which the address must belong.       glibc2 also has reentrant versionsgethostent_r(),gethostbyaddr_r(),gethostbyname_r(), andgethostbyname2_r().  The       caller supplies ahostent structureret which will be filled in on       success, and a temporary work bufferbuf of sizebufsize.  After       the call,result will point to the result on success.  In case of       an error or if no entry is foundresult 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, ifbuf is too small, the functions       will returnERANGE, and the call should be retried with a larger       buffer.  The global variableh_errno is not modified, but the       address of a variable in which to store error numbers is passed inh_errnop.

BUGS        top

gethostbyname() does not recognize components of a dotted IPv4       address string that are expressed in hexadecimal.

SEE ALSO        top

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

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-06-28gethostbyname(3)

Pages that refer to this page:getent(1)pmhostname(1)byteorder(3)getaddrinfo(3)gethostid(3)getipnodebyname(3)getnameinfo(3)inet(3)pmdatrace(3)pmgetarchivelabel(3)pmnewcontext(3)pmreconnectcontext(3)rcmd(3)resolver(3)rexec(3)setnetgrent(3)sockaddr(3type)host.conf(5)nsswitch.conf(5)resolv.conf(5)environ(7)hostname(7)ip(7)nscd(8)rpcctl(8)systemd-machined.service(8)systemd-resolved.service(8)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp