Home

gethostbyname

gethostbyname is a function from the BSD sockets API used to translate a hostname into one or more IPv4 addresses. It is declared in netdb.h and is part of the legacy resolver interface. The function signature is struct hostent *gethostbyname(const char *name);

On success it returns a pointer to a statically allocated hostent structure containing information about the

The hostent structure includes several fields: h_name (official name of the host), h_aliases (null-terminated array of

Limitations and modern usage: gethostbyname is largely considered deprecated for new code due to thread safety

host,
and
on
failure
it
returns
NULL.
When
NULL
is
returned,
the
global
h_errno
variable
is
set
to
indicate
the
error,
such
as
HOST_NOT_FOUND,
NO_ADDRESS,
or
NO_RECOVERY.
The
returned
data
is
stored
in
a
static
buffer
and
is
overwritten
by
subsequent
calls,
which
makes
the
function
not
thread-safe
without
external
synchronization.
alternative
names),
h_addrtype
(address
type,
typically
AF_INET
for
IPv4),
h_length
(length
of
the
address
in
bytes,
usually
4),
and
h_addr_list
(NULL-terminated
array
of
pointers
to
network-order
IPv4
addresses).
The
addresses
returned
are
in
network
byte
order,
and
the
list
may
contain
multiple
addresses
for
a
single
hostname.
concerns
and
limited
IPv6
support.
It
can
consult
local
sources
such
as
/etc/hosts
or
a
DNS
resolver,
but
its
behavior
varies
across
systems.
Modern
programs
are
encouraged
to
use
getaddrinfo
and
related
functions,
which
are
thread-safe,
support
both
IPv4
and
IPv6,
and
provide
more
robust
error
reporting.
Some
platforms
offer
gethostbyname2
to
specify
address
family,
but
getaddrinfo
remains
the
portable,
recommended
API.