1124 lines
32 KiB
HTML
1124 lines
32 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of GETADDRINFO</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>GETADDRINFO</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
getaddrinfo, freeaddrinfo, gai_strerror - network address and
|
|
service translation
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/netdb.h">netdb.h</A>></B>
|
|
|
|
<B>int getaddrinfo(const char *</B><I>node</I><B>, const char *</B><I>service</I><B>,</B>
|
|
<B> const struct addrinfo *</B><I>hints</I><B>,</B>
|
|
<B> struct addrinfo **</B><I>res</I><B>);</B>
|
|
|
|
<B>void freeaddrinfo(struct addrinfo *</B><I>res</I><B>);</B>
|
|
|
|
<B>const char *gai_strerror(int </B><I>errcode</I><B>);</B>
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
|
|
<P>
|
|
|
|
<B>getaddrinfo</B>(),
|
|
|
|
<B>freeaddrinfo</B>(),
|
|
|
|
<B>gai_strerror</B>():
|
|
|
|
<BR> Since glibc 2.22: _POSIX_C_SOURCE >= 200112L
|
|
<BR> Glibc 2.21 and earlier: _POSIX_C_SOURCE
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
Given
|
|
<I>node</I>
|
|
|
|
and
|
|
<I>service</I>,
|
|
|
|
which identify an Internet host and a service,
|
|
<B>getaddrinfo</B>()
|
|
|
|
returns one or more
|
|
<I>addrinfo</I>
|
|
|
|
structures, each of which contains an Internet address
|
|
that can be specified in a call to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2).
|
|
|
|
The
|
|
<B>getaddrinfo</B>()
|
|
|
|
function combines the functionality provided by the
|
|
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+gethostbyname">gethostbyname</A></B>(3)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getservbyname">getservbyname</A></B>(3)
|
|
|
|
functions into a single interface, but unlike the latter functions,
|
|
<B>getaddrinfo</B>()
|
|
|
|
is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
|
|
<P>
|
|
|
|
The
|
|
<I>addrinfo</I>
|
|
|
|
structure used by
|
|
<B>getaddrinfo</B>()
|
|
|
|
contains the following fields:
|
|
<P>
|
|
|
|
|
|
|
|
struct addrinfo {
|
|
<BR> int ai_flags;
|
|
<BR> int ai_family;
|
|
<BR> int ai_socktype;
|
|
<BR> int ai_protocol;
|
|
<BR> socklen_t ai_addrlen;
|
|
<BR> struct sockaddr *ai_addr;
|
|
<BR> char *ai_canonname;
|
|
<BR> struct addrinfo *ai_next;
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>hints</I>
|
|
|
|
argument points to an
|
|
<I>addrinfo</I>
|
|
|
|
structure that specifies criteria for selecting the socket address
|
|
structures returned in the list pointed to by
|
|
<I>res</I>.
|
|
|
|
If
|
|
<I>hints</I>
|
|
|
|
is not NULL it points to an
|
|
<I>addrinfo</I>
|
|
|
|
structure whose
|
|
<I>ai_family</I>,
|
|
|
|
<I>ai_socktype</I>,
|
|
|
|
and
|
|
<I>ai_protocol</I>
|
|
|
|
specify criteria that limit the set of socket addresses returned by
|
|
<B>getaddrinfo</B>(),
|
|
|
|
as follows:
|
|
<DL COMPACT>
|
|
<DT id="1"><I>ai_family</I>
|
|
|
|
<DD>
|
|
This field specifies the desired address family for the returned addresses.
|
|
Valid values for this field include
|
|
<B>AF_INET</B>
|
|
|
|
and
|
|
<B>AF_INET6</B>.
|
|
|
|
The value
|
|
<B>AF_UNSPEC</B>
|
|
|
|
indicates that
|
|
<B>getaddrinfo</B>()
|
|
|
|
should return socket addresses for any address family
|
|
(either IPv4 or IPv6, for example) that can be used with
|
|
<I>node</I>
|
|
|
|
and
|
|
<I>service</I>.
|
|
|
|
<DT id="2"><I>ai_socktype</I>
|
|
|
|
<DD>
|
|
This field specifies the preferred socket type, for example
|
|
<B>SOCK_STREAM</B>
|
|
|
|
or
|
|
<B>SOCK_DGRAM</B>.
|
|
|
|
Specifying 0 in this field indicates that socket addresses of any type
|
|
can be returned by
|
|
<B>getaddrinfo</B>().
|
|
|
|
<DT id="3"><I>ai_protocol</I>
|
|
|
|
<DD>
|
|
This field specifies the protocol for the returned socket addresses.
|
|
Specifying 0 in this field indicates that socket addresses with
|
|
any protocol can be returned by
|
|
<B>getaddrinfo</B>().
|
|
|
|
<DT id="4"><I>ai_flags</I>
|
|
|
|
<DD>
|
|
This field specifies additional options, described below.
|
|
Multiple flags are specified by bitwise OR-ing them together.
|
|
</DL>
|
|
<P>
|
|
|
|
All the other fields in the structure pointed to by
|
|
<I>hints</I>
|
|
|
|
must contain either 0 or a null pointer, as appropriate.
|
|
<P>
|
|
|
|
Specifying
|
|
<I>hints</I>
|
|
|
|
as NULL is equivalent to setting
|
|
<I>ai_socktype</I>
|
|
|
|
and
|
|
<I>ai_protocol</I>
|
|
|
|
to 0;
|
|
<I>ai_family</I>
|
|
|
|
to
|
|
<B>AF_UNSPEC</B>;
|
|
|
|
and
|
|
<I>ai_flags</I>
|
|
|
|
to
|
|
<B>(AI_V4MAPPED | AI_ADDRCONFIG)</B>.
|
|
|
|
(POSIX specifies different defaults for
|
|
<I>ai_flags</I>;
|
|
|
|
see NOTES.)
|
|
<I>node</I>
|
|
|
|
specifies either a numerical network address
|
|
(for IPv4, numbers-and-dots notation as supported by
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet_aton">inet_aton</A></B>(3);
|
|
|
|
for IPv6, hexadecimal string format as supported by
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet_pton">inet_pton</A></B>(3)),
|
|
|
|
or a network hostname, whose network addresses are looked up and resolved.
|
|
If
|
|
<I>hints.ai_flags</I>
|
|
|
|
contains the
|
|
<B>AI_NUMERICHOST</B>
|
|
|
|
flag, then
|
|
<I>node</I>
|
|
|
|
must be a numerical network address.
|
|
The
|
|
<B>AI_NUMERICHOST</B>
|
|
|
|
flag suppresses any potentially lengthy network host address lookups.
|
|
<P>
|
|
|
|
If the
|
|
<B>AI_PASSIVE</B>
|
|
|
|
flag is specified in
|
|
<I>hints.ai_flags</I>,
|
|
|
|
and
|
|
<I>node</I>
|
|
|
|
is NULL,
|
|
then the returned socket addresses will be suitable for
|
|
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)ing
|
|
|
|
a socket that will
|
|
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2)
|
|
|
|
connections.
|
|
The returned socket address will contain the "wildcard address"
|
|
(<B>INADDR_ANY</B>
|
|
|
|
for IPv4 addresses,
|
|
<B>IN6ADDR_ANY_INIT</B>
|
|
|
|
for IPv6 address).
|
|
The wildcard address is used by applications (typically servers)
|
|
that intend to accept connections on any of the host's network addresses.
|
|
If
|
|
<I>node</I>
|
|
|
|
is not NULL, then the
|
|
<B>AI_PASSIVE</B>
|
|
|
|
flag is ignored.
|
|
<P>
|
|
|
|
If the
|
|
<B>AI_PASSIVE</B>
|
|
|
|
flag is not set in
|
|
<I>hints.ai_flags</I>,
|
|
|
|
then the returned socket addresses will be suitable for use with
|
|
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendto">sendto</A></B>(2),
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2).
|
|
|
|
If
|
|
<I>node</I>
|
|
|
|
is NULL,
|
|
then the network address will be set to the loopback interface address
|
|
(<B>INADDR_LOOPBACK</B>
|
|
|
|
for IPv4 addresses,
|
|
<B>IN6ADDR_LOOPBACK_INIT</B>
|
|
|
|
for IPv6 address);
|
|
this is used by applications that intend to communicate
|
|
with peers running on the same host.
|
|
<P>
|
|
|
|
<I>service</I>
|
|
|
|
sets the port in each returned address structure.
|
|
If this argument is a service name (see
|
|
<B><A HREF="/cgi-bin/man/man2html?5+services">services</A></B>(5)),
|
|
|
|
it is translated to the corresponding port number.
|
|
This argument can also be specified as a decimal number,
|
|
which is simply converted to binary.
|
|
If
|
|
<I>service</I>
|
|
|
|
is NULL, then the port number of the returned socket addresses
|
|
will be left uninitialized.
|
|
If
|
|
<B>AI_NUMERICSERV</B>
|
|
|
|
is specified in
|
|
<I>hints.ai_flags</I>
|
|
|
|
and
|
|
<I>service</I>
|
|
|
|
is not NULL, then
|
|
<I>service</I>
|
|
|
|
must point to a string containing a numeric port number.
|
|
This flag is used to inhibit the invocation of a name resolution service
|
|
in cases where it is known not to be required.
|
|
<P>
|
|
|
|
Either
|
|
<I>node</I>
|
|
|
|
or
|
|
<I>service</I>,
|
|
|
|
but not both, may be NULL.
|
|
<P>
|
|
|
|
The
|
|
<B>getaddrinfo</B>()
|
|
|
|
function allocates and initializes a linked list of
|
|
<I>addrinfo</I>
|
|
|
|
structures, one for each network address that matches
|
|
<I>node</I>
|
|
|
|
and
|
|
<I>service</I>,
|
|
|
|
subject to any restrictions imposed by
|
|
<I>hints</I>,
|
|
|
|
and returns a pointer to the start of the list in
|
|
<I>res</I>.
|
|
|
|
The items in the linked list are linked by the
|
|
<I>ai_next</I>
|
|
|
|
field.
|
|
<P>
|
|
|
|
There are several reasons why
|
|
the linked list may have more than one
|
|
<I>addrinfo</I>
|
|
|
|
structure, including: the network host is multihomed, accessible
|
|
over multiple protocols (e.g., both
|
|
<B>AF_INET</B>
|
|
|
|
and
|
|
<B>AF_INET6</B>);
|
|
|
|
or the same service is available from multiple socket types (one
|
|
<B>SOCK_STREAM</B>
|
|
|
|
address and another
|
|
<B>SOCK_DGRAM</B>
|
|
|
|
address, for example).
|
|
Normally, the application should try
|
|
using the addresses in the order in which they are returned.
|
|
The sorting function used within
|
|
<B>getaddrinfo</B>()
|
|
|
|
is defined in RFC 3484; the order can be tweaked for a particular
|
|
system by editing
|
|
<I>/etc/gai.conf</I>
|
|
|
|
(available since glibc 2.5).
|
|
<P>
|
|
|
|
If
|
|
<I>hints.ai_flags</I>
|
|
|
|
includes the
|
|
<B>AI_CANONNAME</B>
|
|
|
|
flag, then the
|
|
<I>ai_canonname</I>
|
|
|
|
field of the first of the
|
|
<I>addrinfo</I>
|
|
|
|
structures in the returned list is set to point to the
|
|
official name of the host.
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
The remaining fields of each returned
|
|
<I>addrinfo</I>
|
|
|
|
structure are initialized as follows:
|
|
<DL COMPACT>
|
|
<DT id="5">*<DD>
|
|
The
|
|
<I>ai_family</I>,
|
|
|
|
<I>ai_socktype</I>,
|
|
|
|
and
|
|
<I>ai_protocol</I>
|
|
|
|
fields return the socket creation parameters (i.e., these fields have
|
|
the same meaning as the corresponding arguments of
|
|
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2)).
|
|
|
|
For example,
|
|
<I>ai_family</I>
|
|
|
|
might return
|
|
<B>AF_INET</B>
|
|
|
|
or
|
|
<B>AF_INET6</B>;
|
|
|
|
<I>ai_socktype</I>
|
|
|
|
might return
|
|
<B>SOCK_DGRAM</B>
|
|
|
|
or
|
|
<B>SOCK_STREAM</B>;
|
|
|
|
and
|
|
<I>ai_protocol</I>
|
|
|
|
returns the protocol for the socket.
|
|
<DT id="6">*<DD>
|
|
A pointer to the socket address is placed in the
|
|
<I>ai_addr</I>
|
|
|
|
field, and the length of the socket address, in bytes,
|
|
is placed in the
|
|
<I>ai_addrlen</I>
|
|
|
|
field.
|
|
</DL>
|
|
<P>
|
|
|
|
If
|
|
<I>hints.ai_flags</I>
|
|
|
|
includes the
|
|
<B>AI_ADDRCONFIG</B>
|
|
|
|
flag, then IPv4 addresses are returned in the list pointed to by
|
|
<I>res</I>
|
|
|
|
only if the local system has at least one
|
|
IPv4 address configured, and IPv6 addresses are returned
|
|
only if the local system has at least one IPv6 address configured.
|
|
The loopback address is not considered for this case as valid
|
|
as a configured address.
|
|
This flag is useful on, for example,
|
|
IPv4-only systems, to ensure that
|
|
<B>getaddrinfo</B>()
|
|
|
|
does not return IPv6 socket addresses that would always fail in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2).
|
|
|
|
<P>
|
|
|
|
If
|
|
<I>hints.ai_flags</I>
|
|
|
|
specifies the
|
|
<B>AI_V4MAPPED</B>
|
|
|
|
flag, and
|
|
<I>hints.ai_family</I>
|
|
|
|
was specified as
|
|
<B>AF_INET6</B>,
|
|
|
|
and no matching IPv6 addresses could be found,
|
|
then return IPv4-mapped IPv6 addresses in the list pointed to by
|
|
<I>res</I>.
|
|
|
|
If both
|
|
<B>AI_V4MAPPED</B>
|
|
|
|
and
|
|
<B>AI_ALL</B>
|
|
|
|
are specified in
|
|
<I>hints.ai_flags</I>,
|
|
|
|
then return both IPv6 and IPv4-mapped IPv6 addresses
|
|
in the list pointed to by
|
|
<I>res</I>.
|
|
|
|
<B>AI_ALL</B>
|
|
|
|
is ignored if
|
|
<B>AI_V4MAPPED</B>
|
|
|
|
is not also specified.
|
|
<P>
|
|
|
|
The
|
|
<B>freeaddrinfo</B>()
|
|
|
|
function frees the memory that was allocated
|
|
for the dynamically allocated linked list
|
|
<I>res</I>.
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H3>Extensions to getaddrinfo() for Internationalized Domain Names</H3>
|
|
|
|
<P>
|
|
|
|
Starting with glibc 2.3.4,
|
|
<B>getaddrinfo</B>()
|
|
|
|
has been extended to selectively allow the incoming and outgoing
|
|
hostnames to be transparently converted to and from the
|
|
Internationalized Domain Name (IDN) format (see RFC 3490,
|
|
<I>Internationalizing Domain Names in Applications (IDNA)</I>).
|
|
|
|
Four new flags are defined:
|
|
<DL COMPACT>
|
|
<DT id="7"><B>AI_IDN</B>
|
|
|
|
<DD>
|
|
If this flag is specified, then the node name given in
|
|
<I>node</I>
|
|
|
|
is converted to IDN format if necessary.
|
|
The source encoding is that of the current locale.
|
|
<DT id="8"><DD>
|
|
If the input name contains non-ASCII characters, then the IDN encoding
|
|
is used.
|
|
Those parts of the node name (delimited by dots) that contain
|
|
non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
|
|
before being passed to the name resolution functions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<DT id="9"><B>AI_CANONIDN</B>
|
|
|
|
<DD>
|
|
After a successful name lookup, and if the
|
|
<B>AI_CANONNAME</B>
|
|
|
|
flag was specified,
|
|
<B>getaddrinfo</B>()
|
|
|
|
will return the canonical name of the
|
|
node corresponding to the
|
|
<I>addrinfo</I>
|
|
|
|
structure value passed back.
|
|
The return value is an exact copy of the value returned by the name
|
|
resolution function.
|
|
<DT id="10"><DD>
|
|
If the name is encoded using ACE, then it will contain the
|
|
<I>xn--</I>
|
|
|
|
prefix for one or more components of the name.
|
|
To convert these components into a readable form the
|
|
<B>AI_CANONIDN</B>
|
|
|
|
flag can be passed in addition to
|
|
<B>AI_CANONNAME</B>.
|
|
|
|
The resulting string is encoded using the current locale's encoding.
|
|
|
|
|
|
|
|
|
|
<DT id="11"><B>AI_IDN_ALLOW_UNASSIGNED</B>, <B>AI_IDN_USE_STD3_ASCII_RULES</B>
|
|
|
|
<DD>
|
|
Setting these flags will enable the
|
|
IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
|
|
IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
|
|
conforming hostname)
|
|
flags respectively to be used in the IDNA handling.
|
|
</DL>
|
|
<A NAME="lbAF"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<B>getaddrinfo</B>()
|
|
|
|
returns 0 if it succeeds, or one of the following nonzero error codes:
|
|
<DL COMPACT>
|
|
<DT id="12"><B>EAI_ADDRFAMILY</B>
|
|
|
|
<DD>
|
|
|
|
The specified network host does not have any network addresses in the
|
|
requested address family.
|
|
<DT id="13"><B>EAI_AGAIN</B>
|
|
|
|
<DD>
|
|
The name server returned a temporary failure indication.
|
|
Try again later.
|
|
<DT id="14"><B>EAI_BADFLAGS</B>
|
|
|
|
<DD>
|
|
<I>hints.ai_flags</I>
|
|
|
|
contains invalid flags; or,
|
|
<I>hints.ai_flags</I>
|
|
|
|
included
|
|
<B>AI_CANONNAME</B>
|
|
|
|
and
|
|
<I>name</I>
|
|
|
|
was NULL.
|
|
<DT id="15"><B>EAI_FAIL</B>
|
|
|
|
<DD>
|
|
The name server returned a permanent failure indication.
|
|
<DT id="16"><B>EAI_FAMILY</B>
|
|
|
|
<DD>
|
|
The requested address family is not supported.
|
|
<DT id="17"><B>EAI_MEMORY</B>
|
|
|
|
<DD>
|
|
Out of memory.
|
|
<DT id="18"><B>EAI_NODATA</B>
|
|
|
|
<DD>
|
|
|
|
The specified network host exists, but does not have any
|
|
network addresses defined.
|
|
<DT id="19"><B>EAI_NONAME</B>
|
|
|
|
<DD>
|
|
The
|
|
<I>node</I>
|
|
|
|
or
|
|
<I>service</I>
|
|
|
|
is not known; or both
|
|
<I>node</I>
|
|
|
|
and
|
|
<I>service</I>
|
|
|
|
are NULL; or
|
|
<B>AI_NUMERICSERV</B>
|
|
|
|
was specified in
|
|
<I>hints.ai_flags</I>
|
|
|
|
and
|
|
<I>service</I>
|
|
|
|
was not a numeric port-number string.
|
|
<DT id="20"><B>EAI_SERVICE</B>
|
|
|
|
<DD>
|
|
The requested service is not available for the requested socket type.
|
|
It may be available through another socket type.
|
|
For example, this error could occur if
|
|
<I>service</I>
|
|
|
|
was "shell" (a service available only on stream sockets), and either
|
|
<I>hints.ai_protocol</I>
|
|
|
|
was
|
|
<B>IPPROTO_UDP</B>,
|
|
|
|
or
|
|
<I>hints.ai_socktype</I>
|
|
|
|
was
|
|
<B>SOCK_DGRAM</B>;
|
|
|
|
or the error could occur if
|
|
<I>service</I>
|
|
|
|
was not NULL, and
|
|
<I>hints.ai_socktype</I>
|
|
|
|
was
|
|
<B>SOCK_RAW</B>
|
|
|
|
(a socket type that does not support the concept of services).
|
|
<DT id="21"><B>EAI_SOCKTYPE</B>
|
|
|
|
<DD>
|
|
The requested socket type is not supported.
|
|
This could occur, for example, if
|
|
<I>hints.ai_socktype</I>
|
|
|
|
and
|
|
<I>hints.ai_protocol</I>
|
|
|
|
are inconsistent (e.g.,
|
|
<B>SOCK_DGRAM</B>
|
|
|
|
and
|
|
<B>IPPROTO_TCP</B>,
|
|
|
|
respectively).
|
|
<DT id="22"><B>EAI_SYSTEM</B>
|
|
|
|
<DD>
|
|
Other system error, check
|
|
<I>errno</I>
|
|
|
|
for details.
|
|
</DL>
|
|
<P>
|
|
|
|
The
|
|
<B>gai_strerror</B>()
|
|
|
|
function translates these error codes to a human readable string,
|
|
suitable for error reporting.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>FILES</H2>
|
|
|
|
<I>/etc/gai.conf</I>
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>getaddrinfo</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe env locale<BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>freeaddrinfo</B>(),
|
|
|
|
<B>gai_strerror</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<P>
|
|
<A NAME="lbAI"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
The
|
|
<B>getaddrinfo</B>()
|
|
|
|
function is documented in RFC 2553.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
<B>getaddrinfo</B>()
|
|
|
|
supports the
|
|
<I>address</I><B>%</B><I>scope-id</I>
|
|
|
|
notation for specifying the IPv6 scope-ID.
|
|
<P>
|
|
|
|
<B>AI_ADDRCONFIG</B>,
|
|
|
|
<B>AI_ALL</B>,
|
|
|
|
and
|
|
<B>AI_V4MAPPED</B>
|
|
|
|
are available since glibc 2.3.3.
|
|
<B>AI_NUMERICSERV</B>
|
|
|
|
is available since glibc 2.3.4.
|
|
<P>
|
|
|
|
According to POSIX.1, specifying
|
|
|
|
<I>hints</I>
|
|
|
|
as NULL should cause
|
|
<I>ai_flags</I>
|
|
|
|
to be assumed as 0.
|
|
The GNU C library instead assumes a value of
|
|
<B>(AI_V4MAPPED | AI_ADDRCONFIG)</B>
|
|
|
|
for this case,
|
|
since this value is considered an improvement on the specification.
|
|
<A NAME="lbAK"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following programs demonstrate the use of
|
|
<B>getaddrinfo</B>(),
|
|
|
|
<B>gai_strerror</B>(),
|
|
|
|
<B>freeaddrinfo</B>(),
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getnameinfo">getnameinfo</A></B>(3).
|
|
|
|
The programs are an echo server and client for UDP datagrams.
|
|
<A NAME="lbAL"> </A>
|
|
<H3>Server program</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>>
|
|
#include <<A HREF="file:///usr/include/netdb.h">netdb.h</A>>
|
|
<P>
|
|
#define BUF_SIZE 500
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> struct addrinfo hints;
|
|
<BR> struct addrinfo *result, *rp;
|
|
<BR> int sfd, s;
|
|
<BR> struct sockaddr_storage peer_addr;
|
|
<BR> socklen_t peer_addr_len;
|
|
<BR> ssize_t nread;
|
|
<BR> char buf[BUF_SIZE];
|
|
<P>
|
|
<BR> if (argc != 2) {
|
|
<BR> fprintf(stderr, "Usage: %s port\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> memset(&hints, 0, sizeof(struct addrinfo));
|
|
<BR> hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
|
<BR> hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
|
|
<BR> hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
|
|
<BR> hints.ai_protocol = 0; /* Any protocol */
|
|
<BR> hints.ai_canonname = NULL;
|
|
<BR> hints.ai_addr = NULL;
|
|
<BR> hints.ai_next = NULL;
|
|
<P>
|
|
<BR> s = getaddrinfo(NULL, argv[1], &hints, &result);
|
|
<BR> if (s != 0) {
|
|
<BR> fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* getaddrinfo() returns a list of address structures.
|
|
<BR> Try each address until we successfully <A HREF="/cgi-bin/man/man2html?2+bind">bind</A>(2).
|
|
<BR> If <A HREF="/cgi-bin/man/man2html?2+socket">socket</A>(2) (or <A HREF="/cgi-bin/man/man2html?2+bind">bind</A>(2)) fails, we (close the socket
|
|
<BR> and) try the next address. */
|
|
<P>
|
|
<BR> for (rp = result; rp != NULL; rp = rp->ai_next) {
|
|
<BR> sfd = socket(rp->ai_family, rp->ai_socktype,
|
|
<BR> rp->ai_protocol);
|
|
<BR> if (sfd == -1)
|
|
<BR> continue;
|
|
<P>
|
|
<BR> if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
|
|
<BR> break; /* Success */
|
|
<P>
|
|
<BR> close(sfd);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (rp == NULL) { /* No address succeeded */
|
|
<BR> fprintf(stderr, "Could not bind\n");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> freeaddrinfo(result); /* No longer needed */
|
|
<P>
|
|
<BR> /* Read datagrams and echo them back to sender */
|
|
<P>
|
|
<BR> for (;;) {
|
|
<BR> peer_addr_len = sizeof(struct sockaddr_storage);
|
|
<BR> nread = recvfrom(sfd, buf, BUF_SIZE, 0,
|
|
<BR> (struct sockaddr *) &peer_addr, &peer_addr_len);
|
|
<BR> if (nread == -1)
|
|
<BR> continue; /* Ignore failed request */
|
|
<P>
|
|
<BR> char host[NI_MAXHOST], service[NI_MAXSERV];
|
|
<P>
|
|
<BR> s = getnameinfo((struct sockaddr *) &peer_addr,
|
|
<BR> peer_addr_len, host, NI_MAXHOST,
|
|
<BR> service, NI_MAXSERV, NI_NUMERICSERV);
|
|
<BR> if (s == 0)
|
|
<BR> printf("Received %zd bytes from %s:%s\n",
|
|
<BR> nread, host, service);
|
|
<BR> else
|
|
<BR> fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
|
|
<P>
|
|
<BR> if (sendto(sfd, buf, nread, 0,
|
|
<BR> (struct sockaddr *) &peer_addr,
|
|
<BR> peer_addr_len) != nread)
|
|
<BR> fprintf(stderr, "Error sending response\n");
|
|
<BR> }
|
|
}
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H3>Client program</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>>
|
|
#include <<A HREF="file:///usr/include/netdb.h">netdb.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
<P>
|
|
#define BUF_SIZE 500
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> struct addrinfo hints;
|
|
<BR> struct addrinfo *result, *rp;
|
|
<BR> int sfd, s, j;
|
|
<BR> size_t len;
|
|
<BR> ssize_t nread;
|
|
<BR> char buf[BUF_SIZE];
|
|
<P>
|
|
<BR> if (argc < 3) {
|
|
<BR> fprintf(stderr, "Usage: %s host port msg...\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Obtain address(es) matching host/port */
|
|
<P>
|
|
<BR> memset(&hints, 0, sizeof(struct addrinfo));
|
|
<BR> hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
|
<BR> hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
|
|
<BR> hints.ai_flags = 0;
|
|
<BR> hints.ai_protocol = 0; /* Any protocol */
|
|
<P>
|
|
<BR> s = getaddrinfo(argv[1], argv[2], &hints, &result);
|
|
<BR> if (s != 0) {
|
|
<BR> fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* getaddrinfo() returns a list of address structures.
|
|
<BR> Try each address until we successfully <A HREF="/cgi-bin/man/man2html?2+connect">connect</A>(2).
|
|
<BR> If <A HREF="/cgi-bin/man/man2html?2+socket">socket</A>(2) (or <A HREF="/cgi-bin/man/man2html?2+connect">connect</A>(2)) fails, we (close the socket
|
|
<BR> and) try the next address. */
|
|
<P>
|
|
<BR> for (rp = result; rp != NULL; rp = rp->ai_next) {
|
|
<BR> sfd = socket(rp->ai_family, rp->ai_socktype,
|
|
<BR> rp->ai_protocol);
|
|
<BR> if (sfd == -1)
|
|
<BR> continue;
|
|
<P>
|
|
<BR> if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
|
|
<BR> break; /* Success */
|
|
<P>
|
|
<BR> close(sfd);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (rp == NULL) { /* No address succeeded */
|
|
<BR> fprintf(stderr, "Could not connect\n");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> freeaddrinfo(result); /* No longer needed */
|
|
<P>
|
|
<BR> /* Send remaining command-line arguments as separate
|
|
<BR> datagrams, and read responses from server */
|
|
<P>
|
|
<BR> for (j = 3; j < argc; j++) {
|
|
<BR> len = strlen(argv[j]) + 1;
|
|
<BR> /* +1 for terminating null byte */
|
|
<P>
|
|
<BR> if (len > BUF_SIZE) {
|
|
<BR> fprintf(stderr,
|
|
<BR> "Ignoring long message in argument %d\n", j);
|
|
<BR> continue;
|
|
<BR> }
|
|
<P>
|
|
<BR> if (write(sfd, argv[j], len) != len) {
|
|
<BR> fprintf(stderr, "partial/failed write\n");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> nread = read(sfd, buf, BUF_SIZE);
|
|
<BR> if (nread == -1) {
|
|
<BR> perror("read");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("Received %zd bytes: %s\n", nread, buf);
|
|
<BR> }
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo_a">getaddrinfo_a</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+gethostbyname">gethostbyname</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getnameinfo">getnameinfo</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet">inet</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?5+gai.conf">gai.conf</A></B>(5),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+hostname">hostname</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7)
|
|
|
|
<A NAME="lbAO"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="23"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="24"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="25"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="26"><A HREF="#lbAE">Extensions to getaddrinfo() for Internationalized Domain Names</A><DD>
|
|
</DL>
|
|
<DT id="27"><A HREF="#lbAF">RETURN VALUE</A><DD>
|
|
<DT id="28"><A HREF="#lbAG">FILES</A><DD>
|
|
<DT id="29"><A HREF="#lbAH">ATTRIBUTES</A><DD>
|
|
<DT id="30"><A HREF="#lbAI">CONFORMING TO</A><DD>
|
|
<DT id="31"><A HREF="#lbAJ">NOTES</A><DD>
|
|
<DT id="32"><A HREF="#lbAK">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="33"><A HREF="#lbAL">Server program</A><DD>
|
|
<DT id="34"><A HREF="#lbAM">Client program</A><DD>
|
|
</DL>
|
|
<DT id="35"><A HREF="#lbAN">SEE ALSO</A><DD>
|
|
<DT id="36"><A HREF="#lbAO">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:44 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|