Blame man3/getaddrinfo.3

Packit 7cfc04
.\" Copyright (c) 2007, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" and Copyright (c) 2006 Ulrich Drepper <drepper@redhat.com>
Packit 7cfc04
.\" A few pieces of an earlier version remain:
Packit 7cfc04
.\" Copyright 2000, Sam Varshavchik <mrsam@courier-mta.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" References: RFC 2553
Packit 7cfc04
.\"
Packit 7cfc04
.\" 2005-08-09, mtk, added AI_ALL, AI_ADDRCONFIG, AI_V4MAPPED,
Packit 7cfc04
.\"			and AI_NUMERICSERV.
Packit 7cfc04
.\" 2006-11-25, Ulrich Drepper <drepper@redhat.com>
Packit 7cfc04
.\"     Add text describing Internationalized Domain Name extensions.
Packit 7cfc04
.\" 2007-06-08, mtk: added example programs
Packit 7cfc04
.\" 2008-02-26, mtk; clarify discussion of NULL 'hints' argument; other
Packit 7cfc04
.\"     minor rewrites.
Packit 7cfc04
.\" 2008-06-18, mtk: many parts rewritten
Packit 7cfc04
.\" 2008-12-04, Petr Baudis <pasky@suse.cz>
Packit 7cfc04
.\"	Describe results ordering and reference /etc/gai.conf.
Packit 7cfc04
.\"
Packit 7cfc04
.\" FIXME . glibc's 2.9 NEWS file documents DCCP and UDP-lite support
Packit 7cfc04
.\"           and is SCTP support now also there?
Packit 7cfc04
.\"
Packit 7cfc04
.TH GETADDRINFO 3 2017-09-15 "GNU" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
getaddrinfo, freeaddrinfo, gai_strerror \- network address and
Packit 7cfc04
service translation
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <sys/types.h>
Packit 7cfc04
.B #include <sys/socket.h>
Packit 7cfc04
.B #include <netdb.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int getaddrinfo(const char *" "node" ", const char *" "service" ,
Packit 7cfc04
.BI "                const struct addrinfo *" "hints" ,
Packit 7cfc04
.BI "                struct addrinfo **" "res" );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "void freeaddrinfo(struct addrinfo *" "res" );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "const char *gai_strerror(int " "errcode" );
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.ad l
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR getaddrinfo (),
Packit 7cfc04
.BR freeaddrinfo (),
Packit 7cfc04
.BR gai_strerror ():
Packit 7cfc04
    Since glibc 2.22: _POSIX_C_SOURCE >= 200112L
Packit 7cfc04
    Glibc 2.21 and earlier: _POSIX_C_SOURCE
Packit 7cfc04
.ad b
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
Given
Packit 7cfc04
.I node
Packit 7cfc04
and
Packit 7cfc04
.IR service ,
Packit 7cfc04
which identify an Internet host and a service,
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
returns one or more
Packit 7cfc04
.I addrinfo
Packit 7cfc04
structures, each of which contains an Internet address
Packit 7cfc04
that can be specified in a call to
Packit 7cfc04
.BR bind (2)
Packit 7cfc04
or
Packit 7cfc04
.BR connect (2).
Packit 7cfc04
The
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
function combines the functionality provided by the
Packit 7cfc04
.\" .BR getipnodebyname (3),
Packit 7cfc04
.\" .BR getipnodebyaddr (3),
Packit 7cfc04
.BR gethostbyname (3)
Packit 7cfc04
and
Packit 7cfc04
.BR getservbyname (3)
Packit 7cfc04
functions into a single interface, but unlike the latter functions,
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I addrinfo
Packit 7cfc04
structure used by
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
contains the following fields:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct addrinfo {
Packit 7cfc04
    int              ai_flags;
Packit 7cfc04
    int              ai_family;
Packit 7cfc04
    int              ai_socktype;
Packit 7cfc04
    int              ai_protocol;
Packit 7cfc04
    socklen_t        ai_addrlen;
Packit 7cfc04
    struct sockaddr *ai_addr;
Packit 7cfc04
    char            *ai_canonname;
Packit 7cfc04
    struct addrinfo *ai_next;
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I hints
Packit 7cfc04
argument points to an
Packit 7cfc04
.I addrinfo
Packit 7cfc04
structure that specifies criteria for selecting the socket address
Packit 7cfc04
structures returned in the list pointed to by
Packit 7cfc04
.IR res .
Packit 7cfc04
If
Packit 7cfc04
.I hints
Packit 7cfc04
is not NULL it points to an
Packit 7cfc04
.I addrinfo
Packit 7cfc04
structure whose
Packit 7cfc04
.IR ai_family ,
Packit 7cfc04
.IR ai_socktype ,
Packit 7cfc04
and
Packit 7cfc04
.I ai_protocol
Packit 7cfc04
specify criteria that limit the set of socket addresses returned by
Packit 7cfc04
.BR getaddrinfo (),
Packit 7cfc04
as follows:
Packit 7cfc04
.TP 12
Packit 7cfc04
.I ai_family
Packit 7cfc04
This field specifies the desired address family for the returned addresses.
Packit 7cfc04
Valid values for this field include
Packit 7cfc04
.BR AF_INET
Packit 7cfc04
and
Packit 7cfc04
.BR AF_INET6 .
Packit 7cfc04
The value
Packit 7cfc04
.B AF_UNSPEC
Packit 7cfc04
indicates that
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
should return socket addresses for any address family
Packit 7cfc04
(either IPv4 or IPv6, for example) that can be used with
Packit 7cfc04
.I node
Packit 7cfc04
and
Packit 7cfc04
.IR service .
Packit 7cfc04
.TP
Packit 7cfc04
.I ai_socktype
Packit 7cfc04
This field specifies the preferred socket type, for example
Packit 7cfc04
.BR SOCK_STREAM
Packit 7cfc04
or
Packit 7cfc04
.BR SOCK_DGRAM .
Packit 7cfc04
Specifying 0 in this field indicates that socket addresses of any type
Packit 7cfc04
can be returned by
Packit 7cfc04
.BR getaddrinfo ().
Packit 7cfc04
.TP
Packit 7cfc04
.I ai_protocol
Packit 7cfc04
This field specifies the protocol for the returned socket addresses.
Packit 7cfc04
Specifying 0 in this field indicates that socket addresses with
Packit 7cfc04
any protocol can be returned by
Packit 7cfc04
.BR getaddrinfo ().
Packit 7cfc04
.TP
Packit 7cfc04
.I ai_flags
Packit 7cfc04
This field specifies additional options, described below.
Packit 7cfc04
Multiple flags are specified by bitwise OR-ing them together.
Packit 7cfc04
.PP
Packit 7cfc04
All the other fields in the structure pointed to by
Packit 7cfc04
.I hints
Packit 7cfc04
must contain either 0 or a null pointer, as appropriate.
Packit 7cfc04
.PP
Packit 7cfc04
Specifying
Packit 7cfc04
.I hints
Packit 7cfc04
as NULL is equivalent to setting
Packit 7cfc04
.I ai_socktype
Packit 7cfc04
and
Packit 7cfc04
.I ai_protocol
Packit 7cfc04
to 0;
Packit 7cfc04
.I ai_family
Packit 7cfc04
to
Packit 7cfc04
.BR AF_UNSPEC ;
Packit 7cfc04
and
Packit 7cfc04
.I ai_flags
Packit 7cfc04
to
Packit 7cfc04
.BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)" .
Packit 7cfc04
(POSIX specifies different defaults for
Packit 7cfc04
.IR ai_flags ;
Packit 7cfc04
see NOTES.)
Packit 7cfc04
.I node
Packit 7cfc04
specifies either a numerical network address
Packit 7cfc04
(for IPv4, numbers-and-dots notation as supported by
Packit 7cfc04
.BR inet_aton (3);
Packit 7cfc04
for IPv6, hexadecimal string format as supported by
Packit 7cfc04
.BR inet_pton (3)),
Packit 7cfc04
or a network hostname, whose network addresses are looked up and resolved.
Packit 7cfc04
If
Packit 7cfc04
.I hints.ai_flags
Packit 7cfc04
contains the
Packit 7cfc04
.B AI_NUMERICHOST
Packit 7cfc04
flag, then
Packit 7cfc04
.I node
Packit 7cfc04
must be a numerical network address.
Packit 7cfc04
The
Packit 7cfc04
.B AI_NUMERICHOST
Packit 7cfc04
flag suppresses any potentially lengthy network host address lookups.
Packit 7cfc04
.PP
Packit 7cfc04
If the
Packit 7cfc04
.B AI_PASSIVE
Packit 7cfc04
flag is specified in
Packit 7cfc04
.IR hints.ai_flags ,
Packit 7cfc04
and
Packit 7cfc04
.I node
Packit 7cfc04
is NULL,
Packit 7cfc04
then the returned socket addresses will be suitable for
Packit 7cfc04
.BR bind (2)ing
Packit 7cfc04
a socket that will
Packit 7cfc04
.BR accept (2)
Packit 7cfc04
connections.
Packit 7cfc04
The returned socket address will contain the "wildcard address"
Packit 7cfc04
.RB ( INADDR_ANY
Packit 7cfc04
for IPv4 addresses,
Packit 7cfc04
.BR IN6ADDR_ANY_INIT
Packit 7cfc04
for IPv6 address).
Packit 7cfc04
The wildcard address is used by applications (typically servers)
Packit 7cfc04
that intend to accept connections on any of the host's network addresses.
Packit 7cfc04
If
Packit 7cfc04
.I node
Packit 7cfc04
is not NULL, then the
Packit 7cfc04
.B AI_PASSIVE
Packit 7cfc04
flag is ignored.
Packit 7cfc04
.PP
Packit 7cfc04
If the
Packit 7cfc04
.B AI_PASSIVE
Packit 7cfc04
flag is not set in
Packit 7cfc04
.IR hints.ai_flags ,
Packit 7cfc04
then the returned socket addresses will be suitable for use with
Packit 7cfc04
.BR connect (2),
Packit 7cfc04
.BR sendto (2),
Packit 7cfc04
or
Packit 7cfc04
.BR sendmsg (2).
Packit 7cfc04
If
Packit 7cfc04
.I node
Packit 7cfc04
is NULL,
Packit 7cfc04
then the network address will be set to the loopback interface address
Packit 7cfc04
.RB ( INADDR_LOOPBACK
Packit 7cfc04
for IPv4 addresses,
Packit 7cfc04
.BR IN6ADDR_LOOPBACK_INIT
Packit 7cfc04
for IPv6 address);
Packit 7cfc04
this is used by applications that intend to communicate
Packit 7cfc04
with peers running on the same host.
Packit 7cfc04
.PP
Packit 7cfc04
.I service
Packit 7cfc04
sets the port in each returned address structure.
Packit 7cfc04
If this argument is a service name (see
Packit 7cfc04
.BR services (5)),
Packit 7cfc04
it is translated to the corresponding port number.
Packit 7cfc04
This argument can also be specified as a decimal number,
Packit 7cfc04
which is simply converted to binary.
Packit 7cfc04
If
Packit 7cfc04
.I service
Packit 7cfc04
is NULL, then the port number of the returned socket addresses
Packit 7cfc04
will be left uninitialized.
Packit 7cfc04
If
Packit 7cfc04
.B AI_NUMERICSERV
Packit 7cfc04
is specified in
Packit 7cfc04
.I hints.ai_flags
Packit 7cfc04
and
Packit 7cfc04
.I service
Packit 7cfc04
is not NULL, then
Packit 7cfc04
.I service
Packit 7cfc04
must point to a string containing a numeric port number.
Packit 7cfc04
This flag is used to inhibit the invocation of a name resolution service
Packit 7cfc04
in cases where it is known not to be required.
Packit 7cfc04
.PP
Packit 7cfc04
Either
Packit 7cfc04
.I node
Packit 7cfc04
or
Packit 7cfc04
.IR service ,
Packit 7cfc04
but not both, may be NULL.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
function allocates and initializes a linked list of
Packit 7cfc04
.I addrinfo
Packit 7cfc04
structures, one for each network address that matches
Packit 7cfc04
.I node
Packit 7cfc04
and
Packit 7cfc04
.IR service ,
Packit 7cfc04
subject to any restrictions imposed by
Packit 7cfc04
.IR hints ,
Packit 7cfc04
and returns a pointer to the start of the list in
Packit 7cfc04
.IR res .
Packit 7cfc04
The items in the linked list are linked by the
Packit 7cfc04
.I ai_next
Packit 7cfc04
field.
Packit 7cfc04
.PP
Packit 7cfc04
There are several reasons why
Packit 7cfc04
the linked list may have more than one
Packit 7cfc04
.I addrinfo
Packit 7cfc04
structure, including: the network host is multihomed, accessible
Packit 7cfc04
over multiple protocols (e.g., both
Packit 7cfc04
.BR AF_INET
Packit 7cfc04
and
Packit 7cfc04
.BR AF_INET6 );
Packit 7cfc04
or the same service is available from multiple socket types (one
Packit 7cfc04
.B SOCK_STREAM
Packit 7cfc04
address and another
Packit 7cfc04
.B SOCK_DGRAM
Packit 7cfc04
address, for example).
Packit 7cfc04
Normally, the application should try
Packit 7cfc04
using the addresses in the order in which they are returned.
Packit 7cfc04
The sorting function used within
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
is defined in RFC\ 3484; the order can be tweaked for a particular
Packit 7cfc04
system by editing
Packit 7cfc04
.IR /etc/gai.conf
Packit 7cfc04
(available since glibc 2.5).
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I hints.ai_flags
Packit 7cfc04
includes the
Packit 7cfc04
.B AI_CANONNAME
Packit 7cfc04
flag, then the
Packit 7cfc04
.I ai_canonname
Packit 7cfc04
field of the first of the
Packit 7cfc04
.I addrinfo
Packit 7cfc04
structures in the returned list is set to point to the
Packit 7cfc04
official name of the host.
Packit 7cfc04
.\" In glibc prior to 2.3.4, the ai_canonname of each addrinfo
Packit 7cfc04
.\" structure was set pointing to the canonical name; that was
Packit 7cfc04
.\" more than POSIX.1-2001 specified, or other implementations provided.
Packit 7cfc04
.\" MTK, Aug 05
Packit 7cfc04
.PP
Packit 7cfc04
The remaining fields of each returned
Packit 7cfc04
.I addrinfo
Packit 7cfc04
structure are initialized as follows:
Packit 7cfc04
.IP * 2
Packit 7cfc04
The
Packit 7cfc04
.IR ai_family ,
Packit 7cfc04
.IR ai_socktype ,
Packit 7cfc04
and
Packit 7cfc04
.I ai_protocol
Packit 7cfc04
fields return the socket creation parameters (i.e., these fields have
Packit 7cfc04
the same meaning as the corresponding arguments of
Packit 7cfc04
.BR socket (2)).
Packit 7cfc04
For example,
Packit 7cfc04
.I ai_family
Packit 7cfc04
might return
Packit 7cfc04
.B AF_INET
Packit 7cfc04
or
Packit 7cfc04
.BR AF_INET6 ;
Packit 7cfc04
.I ai_socktype
Packit 7cfc04
might return
Packit 7cfc04
.B SOCK_DGRAM
Packit 7cfc04
or
Packit 7cfc04
.BR SOCK_STREAM ;
Packit 7cfc04
and
Packit 7cfc04
.I ai_protocol
Packit 7cfc04
returns the protocol for the socket.
Packit 7cfc04
.IP *
Packit 7cfc04
A pointer to the socket address is placed in the
Packit 7cfc04
.I ai_addr
Packit 7cfc04
field, and the length of the socket address, in bytes,
Packit 7cfc04
is placed in the
Packit 7cfc04
.I ai_addrlen
Packit 7cfc04
field.
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I hints.ai_flags
Packit 7cfc04
includes the
Packit 7cfc04
.B AI_ADDRCONFIG
Packit 7cfc04
flag, then IPv4 addresses are returned in the list pointed to by
Packit 7cfc04
.I res
Packit 7cfc04
only if the local system has at least one
Packit 7cfc04
IPv4 address configured, and IPv6 addresses are returned
Packit 7cfc04
only if the local system has at least one IPv6 address configured.
Packit 7cfc04
The loopback address is not considered for this case as valid
Packit 7cfc04
as a configured address.
Packit 7cfc04
This flag is useful on, for example,
Packit 7cfc04
IPv4-only systems, to ensure that
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
does not return IPv6 socket addresses that would always fail in
Packit 7cfc04
.BR connect (2)
Packit 7cfc04
or
Packit 7cfc04
.BR bind (2).
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I hints.ai_flags
Packit 7cfc04
specifies the
Packit 7cfc04
.B AI_V4MAPPED
Packit 7cfc04
flag, and
Packit 7cfc04
.I hints.ai_family
Packit 7cfc04
was specified as
Packit 7cfc04
.BR AF_INET6 ,
Packit 7cfc04
and no matching IPv6 addresses could be found,
Packit 7cfc04
then return IPv4-mapped IPv6 addresses in the list pointed to by
Packit 7cfc04
.IR res .
Packit 7cfc04
If both
Packit 7cfc04
.B AI_V4MAPPED
Packit 7cfc04
and
Packit 7cfc04
.B AI_ALL
Packit 7cfc04
are specified in
Packit 7cfc04
.IR hints.ai_flags ,
Packit 7cfc04
then return both IPv6 and IPv4-mapped IPv6 addresses
Packit 7cfc04
in the list pointed to by
Packit 7cfc04
.IR res .
Packit 7cfc04
.B AI_ALL
Packit 7cfc04
is ignored if
Packit 7cfc04
.B AI_V4MAPPED
Packit 7cfc04
is not also specified.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR freeaddrinfo ()
Packit 7cfc04
function frees the memory that was allocated
Packit 7cfc04
for the dynamically allocated linked list
Packit 7cfc04
.IR res .
Packit 7cfc04
.SS Extensions to getaddrinfo() for Internationalized Domain Names
Packit 7cfc04
.PP
Packit 7cfc04
Starting with glibc 2.3.4,
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
has been extended to selectively allow the incoming and outgoing
Packit 7cfc04
hostnames to be transparently converted to and from the
Packit 7cfc04
Internationalized Domain Name (IDN) format (see RFC 3490,
Packit 7cfc04
.IR "Internationalizing Domain Names in Applications (IDNA)" ).
Packit 7cfc04
Four new flags are defined:
Packit 7cfc04
.TP
Packit 7cfc04
.B AI_IDN
Packit 7cfc04
If this flag is specified, then the node name given in
Packit 7cfc04
.I node
Packit 7cfc04
is converted to IDN format if necessary.
Packit 7cfc04
The source encoding is that of the current locale.
Packit 7cfc04
.IP
Packit 7cfc04
If the input name contains non-ASCII characters, then the IDN encoding
Packit 7cfc04
is used.
Packit 7cfc04
Those parts of the node name (delimited by dots) that contain
Packit 7cfc04
non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
Packit 7cfc04
before being passed to the name resolution functions.
Packit 7cfc04
.\" Implementation Detail:
Packit 7cfc04
.\" To minimize effects on system performance the implementation might
Packit 7cfc04
.\" want to check whether the input string contains any non-ASCII
Packit 7cfc04
.\" characters.  If there are none the IDN step can be skipped completely.
Packit 7cfc04
.\" On systems which allow not-ASCII safe encodings for a locale this
Packit 7cfc04
.\" might be a problem.
Packit 7cfc04
.TP
Packit 7cfc04
.B AI_CANONIDN
Packit 7cfc04
After a successful name lookup, and if the
Packit 7cfc04
.B AI_CANONNAME
Packit 7cfc04
flag was specified,
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
will return the canonical name of the
Packit 7cfc04
node corresponding to the
Packit 7cfc04
.I addrinfo
Packit 7cfc04
structure value passed back.
Packit 7cfc04
The return value is an exact copy of the value returned by the name
Packit 7cfc04
resolution function.
Packit 7cfc04
.IP
Packit 7cfc04
If the name is encoded using ACE, then it will contain the
Packit 7cfc04
.I xn\-\-
Packit 7cfc04
prefix for one or more components of the name.
Packit 7cfc04
To convert these components into a readable form the
Packit 7cfc04
.B AI_CANONIDN
Packit 7cfc04
flag can be passed in addition to
Packit 7cfc04
.BR AI_CANONNAME .
Packit 7cfc04
The resulting string is encoded using the current locale's encoding.
Packit 7cfc04
.\"
Packit 7cfc04
.\"Implementation Detail:
Packit 7cfc04
.\"If no component of the returned name starts with xn\-\- the IDN
Packit 7cfc04
.\"step can be skipped, therefore avoiding unnecessary slowdowns.
Packit 7cfc04
.TP
Packit 7cfc04
.BR AI_IDN_ALLOW_UNASSIGNED ", " AI_IDN_USE_STD3_ASCII_RULES
Packit 7cfc04
Setting these flags will enable the
Packit 7cfc04
IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
Packit 7cfc04
IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
Packit 7cfc04
conforming hostname)
Packit 7cfc04
flags respectively to be used in the IDNA handling.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
.\" FIXME glibc defines the following additional errors, some which
Packit 7cfc04
.\" can probably be returned by getaddrinfo(); they need to
Packit 7cfc04
.\" be documented.
Packit 7cfc04
.\"    #ifdef __USE_GNU
Packit 7cfc04
.\"    #define EAI_INPROGRESS  -100  /* Processing request in progress.  */
Packit 7cfc04
.\"    #define EAI_CANCELED    -101  /* Request canceled.  */
Packit 7cfc04
.\"    #define EAI_NOTCANCELED -102  /* Request not canceled.  */
Packit 7cfc04
.\"    #define EAI_ALLDONE     -103  /* All requests done.  */
Packit 7cfc04
.\"    #define EAI_INTR        -104  /* Interrupted by a signal.  */
Packit 7cfc04
.\"    #define EAI_IDN_ENCODE  -105  /* IDN encoding failed.  */
Packit 7cfc04
.\"    #endif
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
returns 0 if it succeeds, or one of the following nonzero error codes:
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_ADDRFAMILY
Packit 7cfc04
.\" Not in SUSv3
Packit 7cfc04
The specified network host does not have any network addresses in the
Packit 7cfc04
requested address family.
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_AGAIN
Packit 7cfc04
The name server returned a temporary failure indication.
Packit 7cfc04
Try again later.
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_BADFLAGS
Packit 7cfc04
.I hints.ai_flags
Packit 7cfc04
contains invalid flags; or,
Packit 7cfc04
.I hints.ai_flags
Packit 7cfc04
included
Packit 7cfc04
.B AI_CANONNAME
Packit 7cfc04
and
Packit 7cfc04
.I name
Packit 7cfc04
was NULL.
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_FAIL
Packit 7cfc04
The name server returned a permanent failure indication.
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_FAMILY
Packit 7cfc04
The requested address family is not supported.
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_MEMORY
Packit 7cfc04
Out of memory.
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_NODATA
Packit 7cfc04
.\" Not in SUSv3
Packit 7cfc04
The specified network host exists, but does not have any
Packit 7cfc04
network addresses defined.
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_NONAME
Packit 7cfc04
The
Packit 7cfc04
.I node
Packit 7cfc04
or
Packit 7cfc04
.I service
Packit 7cfc04
is not known; or both
Packit 7cfc04
.I node
Packit 7cfc04
and
Packit 7cfc04
.I service
Packit 7cfc04
are NULL; or
Packit 7cfc04
.B AI_NUMERICSERV
Packit 7cfc04
was specified in
Packit 7cfc04
.I hints.ai_flags
Packit 7cfc04
and
Packit 7cfc04
.I service
Packit 7cfc04
was not a numeric port-number string.
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_SERVICE
Packit 7cfc04
The requested service is not available for the requested socket type.
Packit 7cfc04
It may be available through another socket type.
Packit 7cfc04
For example, this error could occur if
Packit 7cfc04
.I service
Packit 7cfc04
was "shell" (a service available only on stream sockets), and either
Packit 7cfc04
.I hints.ai_protocol
Packit 7cfc04
was
Packit 7cfc04
.BR IPPROTO_UDP ,
Packit 7cfc04
or
Packit 7cfc04
.I hints.ai_socktype
Packit 7cfc04
was
Packit 7cfc04
.BR SOCK_DGRAM ;
Packit 7cfc04
or the error could occur if
Packit 7cfc04
.I service
Packit 7cfc04
was not NULL, and
Packit 7cfc04
.I hints.ai_socktype
Packit 7cfc04
was
Packit 7cfc04
.BR SOCK_RAW
Packit 7cfc04
(a socket type that does not support the concept of services).
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_SOCKTYPE
Packit 7cfc04
The requested socket type is not supported.
Packit 7cfc04
This could occur, for example, if
Packit 7cfc04
.I hints.ai_socktype
Packit 7cfc04
and
Packit 7cfc04
.I hints.ai_protocol
Packit 7cfc04
are inconsistent (e.g.,
Packit 7cfc04
.BR SOCK_DGRAM
Packit 7cfc04
and
Packit 7cfc04
.BR IPPROTO_TCP ,
Packit 7cfc04
respectively).
Packit 7cfc04
.TP
Packit 7cfc04
.B EAI_SYSTEM
Packit 7cfc04
Other system error, check
Packit 7cfc04
.I errno
Packit 7cfc04
for details.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR gai_strerror ()
Packit 7cfc04
function translates these error codes to a human readable string,
Packit 7cfc04
suitable for error reporting.
Packit 7cfc04
.SH FILES
Packit 7cfc04
.I /etc/gai.conf
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lbw15 lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
T}	Thread safety	MT-Safe env locale
Packit 7cfc04
T{
Packit 7cfc04
.BR freeaddrinfo (),
Packit 7cfc04
.BR gai_strerror ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.sp 1
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008.
Packit 7cfc04
The
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
function is documented in RFC\ 2553.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
.BR getaddrinfo ()
Packit 7cfc04
supports the
Packit 7cfc04
.IB address % scope-id
Packit 7cfc04
notation for specifying the IPv6 scope-ID.
Packit 7cfc04
.PP
Packit 7cfc04
.BR AI_ADDRCONFIG ,
Packit 7cfc04
.BR AI_ALL ,
Packit 7cfc04
and
Packit 7cfc04
.B AI_V4MAPPED
Packit 7cfc04
are available since glibc 2.3.3.
Packit 7cfc04
.B AI_NUMERICSERV
Packit 7cfc04
is available since glibc 2.3.4.
Packit 7cfc04
.PP
Packit 7cfc04
According to POSIX.1, specifying
Packit 7cfc04
.\" POSIX.1-2001, POSIX.1-2008
Packit 7cfc04
.I hints
Packit 7cfc04
as NULL should cause
Packit 7cfc04
.I ai_flags
Packit 7cfc04
to be assumed as 0.
Packit 7cfc04
The GNU C library instead assumes a value of
Packit 7cfc04
.BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
Packit 7cfc04
for this case,
Packit 7cfc04
since this value is considered an improvement on the specification.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
.\" getnameinfo.3 refers to this example
Packit 7cfc04
.\" socket.2 refers to this example
Packit 7cfc04
.\" bind.2 refers to this example
Packit 7cfc04
.\" connect.2 refers to this example
Packit 7cfc04
.\" recvfrom.2 refers to this example
Packit 7cfc04
.\" sendto.2 refers to this example
Packit 7cfc04
The following programs demonstrate the use of
Packit 7cfc04
.BR getaddrinfo (),
Packit 7cfc04
.BR gai_strerror (),
Packit 7cfc04
.BR freeaddrinfo (),
Packit 7cfc04
and
Packit 7cfc04
.BR getnameinfo (3).
Packit 7cfc04
The programs are an echo server and client for UDP datagrams.
Packit 7cfc04
.SS Server program
Packit 7cfc04
\&
Packit 7cfc04
.EX
Packit 7cfc04
#include <sys/types.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
#include <string.h>
Packit 7cfc04
#include <sys/socket.h>
Packit 7cfc04
#include <netdb.h>
Packit 7cfc04
Packit 7cfc04
#define BUF_SIZE 500
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    struct addrinfo hints;
Packit 7cfc04
    struct addrinfo *result, *rp;
Packit 7cfc04
    int sfd, s;
Packit 7cfc04
    struct sockaddr_storage peer_addr;
Packit 7cfc04
    socklen_t peer_addr_len;
Packit 7cfc04
    ssize_t nread;
Packit 7cfc04
    char buf[BUF_SIZE];
Packit 7cfc04
Packit 7cfc04
    if (argc != 2) {
Packit 7cfc04
        fprintf(stderr, "Usage: %s port\\n", argv[0]);
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    memset(&hints, 0, sizeof(struct addrinfo));
Packit 7cfc04
    hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
Packit 7cfc04
    hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
Packit 7cfc04
    hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
Packit 7cfc04
    hints.ai_protocol = 0;          /* Any protocol */
Packit 7cfc04
    hints.ai_canonname = NULL;
Packit 7cfc04
    hints.ai_addr = NULL;
Packit 7cfc04
    hints.ai_next = NULL;
Packit 7cfc04
Packit 7cfc04
    s = getaddrinfo(NULL, argv[1], &hints, &result);
Packit 7cfc04
    if (s != 0) {
Packit 7cfc04
        fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* getaddrinfo() returns a list of address structures.
Packit 7cfc04
       Try each address until we successfully bind(2).
Packit 7cfc04
       If socket(2) (or bind(2)) fails, we (close the socket
Packit 7cfc04
       and) try the next address. */
Packit 7cfc04
Packit 7cfc04
    for (rp = result; rp != NULL; rp = rp\->ai_next) {
Packit 7cfc04
        sfd = socket(rp\->ai_family, rp\->ai_socktype,
Packit 7cfc04
                rp\->ai_protocol);
Packit 7cfc04
        if (sfd == \-1)
Packit 7cfc04
            continue;
Packit 7cfc04
Packit 7cfc04
        if (bind(sfd, rp\->ai_addr, rp\->ai_addrlen) == 0)
Packit 7cfc04
            break;                  /* Success */
Packit 7cfc04
Packit 7cfc04
        close(sfd);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    if (rp == NULL) {               /* No address succeeded */
Packit 7cfc04
        fprintf(stderr, "Could not bind\\n");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    freeaddrinfo(result);           /* No longer needed */
Packit 7cfc04
Packit 7cfc04
    /* Read datagrams and echo them back to sender */
Packit 7cfc04
Packit 7cfc04
    for (;;) {
Packit 7cfc04
        peer_addr_len = sizeof(struct sockaddr_storage);
Packit 7cfc04
        nread = recvfrom(sfd, buf, BUF_SIZE, 0,
Packit 7cfc04
                (struct sockaddr *) &peer_addr, &peer_addr_len);
Packit 7cfc04
        if (nread == \-1)
Packit 7cfc04
            continue;               /* Ignore failed request */
Packit 7cfc04
Packit 7cfc04
        char host[NI_MAXHOST], service[NI_MAXSERV];
Packit 7cfc04
Packit 7cfc04
        s = getnameinfo((struct sockaddr *) &peer_addr,
Packit 7cfc04
                        peer_addr_len, host, NI_MAXHOST,
Packit 7cfc04
                        service, NI_MAXSERV, NI_NUMERICSERV);
Packit 7cfc04
        if (s == 0)
Packit 7cfc04
            printf("Received %zd bytes from %s:%s\\n",
Packit 7cfc04
                    nread, host, service);
Packit 7cfc04
        else
Packit 7cfc04
            fprintf(stderr, "getnameinfo: %s\\n", gai_strerror(s));
Packit 7cfc04
Packit 7cfc04
        if (sendto(sfd, buf, nread, 0,
Packit 7cfc04
                    (struct sockaddr *) &peer_addr,
Packit 7cfc04
                    peer_addr_len) != nread)
Packit 7cfc04
            fprintf(stderr, "Error sending response\\n");
Packit 7cfc04
    }
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SS Client program
Packit 7cfc04
\&
Packit 7cfc04
.EX
Packit 7cfc04
#include <sys/types.h>
Packit 7cfc04
#include <sys/socket.h>
Packit 7cfc04
#include <netdb.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
#include <string.h>
Packit 7cfc04
Packit 7cfc04
#define BUF_SIZE 500
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    struct addrinfo hints;
Packit 7cfc04
    struct addrinfo *result, *rp;
Packit 7cfc04
    int sfd, s, j;
Packit 7cfc04
    size_t len;
Packit 7cfc04
    ssize_t nread;
Packit 7cfc04
    char buf[BUF_SIZE];
Packit 7cfc04
Packit 7cfc04
    if (argc < 3) {
Packit 7cfc04
        fprintf(stderr, "Usage: %s host port msg...\\n", argv[0]);
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* Obtain address(es) matching host/port */
Packit 7cfc04
Packit 7cfc04
    memset(&hints, 0, sizeof(struct addrinfo));
Packit 7cfc04
    hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
Packit 7cfc04
    hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
Packit 7cfc04
    hints.ai_flags = 0;
Packit 7cfc04
    hints.ai_protocol = 0;          /* Any protocol */
Packit 7cfc04
Packit 7cfc04
    s = getaddrinfo(argv[1], argv[2], &hints, &result);
Packit 7cfc04
    if (s != 0) {
Packit 7cfc04
        fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* getaddrinfo() returns a list of address structures.
Packit 7cfc04
       Try each address until we successfully connect(2).
Packit 7cfc04
       If socket(2) (or connect(2)) fails, we (close the socket
Packit 7cfc04
       and) try the next address. */
Packit 7cfc04
Packit 7cfc04
    for (rp = result; rp != NULL; rp = rp\->ai_next) {
Packit 7cfc04
        sfd = socket(rp\->ai_family, rp\->ai_socktype,
Packit 7cfc04
                     rp\->ai_protocol);
Packit 7cfc04
        if (sfd == \-1)
Packit 7cfc04
            continue;
Packit 7cfc04
Packit 7cfc04
        if (connect(sfd, rp\->ai_addr, rp\->ai_addrlen) != \-1)
Packit 7cfc04
            break;                  /* Success */
Packit 7cfc04
Packit 7cfc04
        close(sfd);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    if (rp == NULL) {               /* No address succeeded */
Packit 7cfc04
        fprintf(stderr, "Could not connect\\n");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    freeaddrinfo(result);           /* No longer needed */
Packit 7cfc04
Packit 7cfc04
    /* Send remaining command\-line arguments as separate
Packit 7cfc04
       datagrams, and read responses from server */
Packit 7cfc04
Packit 7cfc04
    for (j = 3; j < argc; j++) {
Packit 7cfc04
        len = strlen(argv[j]) + 1;
Packit 7cfc04
                /* +1 for terminating null byte */
Packit 7cfc04
Packit 7cfc04
        if (len + 1 > BUF_SIZE) {
Packit 7cfc04
            fprintf(stderr,
Packit 7cfc04
                    "Ignoring long message in argument %d\\n", j);
Packit 7cfc04
            continue;
Packit 7cfc04
        }
Packit 7cfc04
Packit 7cfc04
        if (write(sfd, argv[j], len) != len) {
Packit 7cfc04
            fprintf(stderr, "partial/failed write\\n");
Packit 7cfc04
            exit(EXIT_FAILURE);
Packit 7cfc04
        }
Packit 7cfc04
Packit 7cfc04
        nread = read(sfd, buf, BUF_SIZE);
Packit 7cfc04
        if (nread == \-1) {
Packit 7cfc04
            perror("read");
Packit 7cfc04
            exit(EXIT_FAILURE);
Packit 7cfc04
        }
Packit 7cfc04
Packit 7cfc04
        printf("Received %zd bytes: %s\\n", nread, buf);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.\" .BR getipnodebyaddr (3),
Packit 7cfc04
.\" .BR getipnodebyname (3),
Packit 7cfc04
.BR getaddrinfo_a (3),
Packit 7cfc04
.BR gethostbyname (3),
Packit 7cfc04
.BR getnameinfo (3),
Packit 7cfc04
.BR inet (3),
Packit 7cfc04
.BR gai.conf (5),
Packit 7cfc04
.BR hostname (7),
Packit 7cfc04
.BR ip (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.