Linux Howtos: manpages: gethostbyname(3) (2024)

Section: Linux Programmer's Manual (3)
Updated: 2017-09-15
IndexReturn to Main Contents

NAME

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

SYNOPSIS

#include <netdb.h>extern int h_errno;struct hostent *gethostbyname(const char *name);#include <sys/socket.h> /* 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 (seefeature_test_macros(7)):

gethostbyname2(),gethostent_r(),gethostbyaddr_r(),gethostbyname_r(),gethostbyname2_r():

Since glibc 2.19:
_DEFAULT_SOURCE
Glibc versions up to and including 2.19:
_BSD_SOURCE || _SVID_SOURCE

herror(),hstrerror():

Since glibc 2.19:
_DEFAULT_SOURCE
Glibc 2.8 to 2.19:
_BSD_SOURCE || _SVID_SOURCE
Before glibc 2.8:
none

h_errno:

Since glibc 2.19
_DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L
Glibc 2.12 to 2.19:
_BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L
Before glibc 2.12:
none

DESCRIPTION

Thegethostbyname*(),gethostbyaddr*(),herror(),andhstrerror()functions are obsolete.Applications should usegetaddrinfo(3),getnameinfo(3),andgai_strerror(3)instead.

Thegethostbyname()function returns a structure of typehostentfor the given hostname.Herenameis either a hostname or an IPv4 address in standard dot notation (as forinet_addr(3)).Ifnameis an IPv4 address, no lookup is performed andgethostbyname()simply copiesnameinto theh_namefield and itsstruct in_addrequivalent into theh_addr_list[0]field of the returnedhostentstructure.Ifnamedoesn'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 unless nameends in a dot.

Thegethostbyaddr()function returns a structure of type hostentfor the given host address addr of length len and address typetype.Valid address types areAF_INETandAF_INET6.The host address argument is a pointer to a struct of a type dependingon the address type, for example a struct in_addr * (probablyobtained via a call toinet_addr(3))for address typeAF_INET.

Thesethostent()function specifies, if stayopen is true (1),that a connected TCP socket should be used for the name server queries andthat 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 nameserver queries.

The (obsolete)herror()function prints the error message associatedwith 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 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, theorderkeyword was used to control the order of host lookups as defined in/etc/host.conf(host.conf(5)).

The hostent structure is defined in <netdb.h> as follows:

struct hostent {
char*h_name;/*officialnameofhost*/
char**h_aliases;/*aliaslist*/
inth_addrtype;/*hostaddresstype*/
inth_length;/*lengthofaddress*/
char**h_addr_list;/*listofaddresses*/}#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; alwaysAF_INETorAF_INET6at present.
h_length
The length of the address in bytes.
h_addr_list
An array of pointers to network addresses for the host (in network byteorder), terminated by a null pointer.
h_addr
The first address in h_addr_list for backward compatibility.

RETURN VALUE

Thegethostbyname()andgethostbyaddr()functions return thehostentstructure or a null pointer if an error occurs.On error, theh_errnovariable 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_DATA
The requested name is valid but does not have an IP address.Another type of request to the name server for this domainmay return an answer.The constantNO_ADDRESSis 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

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

ATTRIBUTES

For an explanation of the terms used in this section, seeattributes(7).

InterfaceAttributeValue
gethostbyname()Thread safetyMT-Unsafe race:hostbyname env
locale
gethostbyaddr()Thread safetyMT-Unsafe race:hostbyaddr env
locale
sethostent(),
endhostent(),
gethostent_r()
Thread safetyMT-Unsafe race:hostent env
locale
herror(),
hstrerror()
Thread safetyMT-Safe
gethostent()Thread safetyMT-Unsafe race:hostent
race:hostentbuf env locale
gethostbyname2()Thread safetyMT-Unsafe race:hostbyname2
env locale
gethostbyaddr_r(),gethostbyname_r(),gethostbyname2_r()Thread safetyMT-Safe env locale

In the above table,hostentinrace:hostentsignifies 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.

CONFORMING TO

POSIX.1-2001 specifiesgethostbyname(),gethostbyaddr(),sethostent(),endhostent(),gethostent(),andh_errno;gethostbyname(),gethostbyaddr(),andh_errnoare marked obsolescent in that standard.POSIX.1-2008 removes the specifications ofgethostbyname(),gethostbyaddr(),andh_errno,recommending the use ofgetaddrinfo(3)andgetnameinfo(3)instead.

NOTES

The functionsgethostbyname()andgethostbyaddr()may return pointers to static data, which may be overwritten bylater calls.Copying thestruct hostentdoes not suffice, since it contains pointers; a deep copy is required.

In the original BSD implementation thelenargumentofgethostbyname()was anint.The SUSv2 standard is buggy and declares thelenargument ofgethostbyaddr()to be of typesize_t.(That is wrong, because it has to beint,andsize_tis 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 maybe reasonable if the host data base is a file that can be readline by line.On many systems, a routine of this name readsfrom 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 ahostentstructureretwhich will be filled in on success, and a temporary work bufferbufof sizebuflen.After the call,resultwill point to the result on success.In case of an erroror if no entry is foundresultwill be NULL.The functions return 0 on success and a nonzero error number on failure.In addition to the errors returned by the nonreentrantversions of these functions, ifbufis too small, the functions will returnERANGE,and the call should be retried with a larger buffer.The global variableh_errnois not modified, but the address of a variable in which to store error numbersis passed inh_errnop.

BUGS

gethostbyname()does not recognize components of a dotted IPv4 address stringthat 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 4.13 of the Linuxman-pagesproject.A description of the project,information about reporting bugs,and the latest version of this page,can be found athttps://www.kernel.org/doc/man-pages/.

Index

NAME
SYNOPSIS
DESCRIPTION
Historical
RETURN VALUE
ERRORS
FILES
ATTRIBUTES
CONFORMING TO
NOTES
System V/POSIX extension
GNU extensions
BUGS
SEE ALSO
COLOPHON
Linux Howtos: manpages: gethostbyname(3) (2024)
Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 6567

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.