500 lines
11 KiB
HTML
500 lines
11 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of GETNAMEINFO</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>GETNAMEINFO</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>
|
|
|
|
getnameinfo - address-to-name translation in protocol-independent manner
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<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 getnameinfo(const struct sockaddr *</B><I>addr</I><B>, socklen_t </B><I>addrlen</I><B>,</B>
|
|
<B> char *</B><I>host</I><B>, socklen_t </B><I>hostlen</I><B>,</B>
|
|
<B> char *</B><I>serv</I><B>, socklen_t </B><I>servlen</I><B>, int </B><I>flags</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>getnameinfo</B>():
|
|
|
|
<BR> Since glibc 2.22: _POSIX_C_SOURCE >= 201112L
|
|
<BR> Glibc 2.21 and earlier: _POSIX_C_SOURCE
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>getnameinfo</B>()
|
|
|
|
function is the inverse of
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3):
|
|
|
|
it converts a socket address to a corresponding host and service,
|
|
in a protocol-independent manner.
|
|
It combines the functionality of
|
|
<B><A HREF="/cgi-bin/man/man2html?3+gethostbyaddr">gethostbyaddr</A></B>(3)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getservbyport">getservbyport</A></B>(3),
|
|
|
|
but unlike those functions,
|
|
<B>getnameinfo</B>()
|
|
|
|
is reentrant and allows programs to eliminate
|
|
IPv4-versus-IPv6 dependencies.
|
|
<P>
|
|
|
|
The
|
|
<I>addr</I>
|
|
|
|
argument is a pointer to a generic socket address structure
|
|
(of type
|
|
<I>sockaddr_in</I>
|
|
|
|
or
|
|
<I>sockaddr_in6</I>)
|
|
|
|
of size
|
|
<I>addrlen</I>
|
|
|
|
that holds the input IP address and port number.
|
|
The arguments
|
|
<I>host</I>
|
|
|
|
and
|
|
<I>serv</I>
|
|
|
|
are pointers to caller-allocated buffers (of size
|
|
<I>hostlen</I>
|
|
|
|
and
|
|
<I>servlen</I>
|
|
|
|
respectively) into which
|
|
<B>getnameinfo</B>()
|
|
|
|
places null-terminated strings containing the host and
|
|
service names respectively.
|
|
<P>
|
|
|
|
The caller can specify that no hostname (or no service name)
|
|
is required by providing a NULL
|
|
<I>host</I>
|
|
|
|
(or
|
|
<I>serv</I>)
|
|
|
|
argument or a zero
|
|
<I>hostlen</I>
|
|
|
|
(or
|
|
<I>servlen</I>)
|
|
|
|
argument.
|
|
However, at least one of hostname or service name
|
|
must be requested.
|
|
<P>
|
|
|
|
The
|
|
<I>flags</I>
|
|
|
|
argument modifies the behavior of
|
|
<B>getnameinfo</B>()
|
|
|
|
as follows:
|
|
<DL COMPACT>
|
|
<DT id="1"><B>NI_NAMEREQD</B>
|
|
|
|
<DD>
|
|
If set, then an error is returned if the hostname cannot be determined.
|
|
<DT id="2"><B>NI_DGRAM</B>
|
|
|
|
<DD>
|
|
If set, then the service is datagram (UDP) based rather than
|
|
stream (TCP) based.
|
|
This is required for the few ports (512-514)
|
|
that have different services for UDP and TCP.
|
|
<DT id="3"><B>NI_NOFQDN</B>
|
|
|
|
<DD>
|
|
If set, return only the hostname part of the fully qualified domain name
|
|
for local hosts.
|
|
<DT id="4"><B>NI_NUMERICHOST</B>
|
|
|
|
<DD>
|
|
If set, then the numeric form of the hostname is returned.
|
|
|
|
|
|
|
|
|
|
(When not set, this will still happen in case the node's name
|
|
cannot be determined.)
|
|
|
|
<DT id="5"><B>NI_NUMERICSERV</B>
|
|
|
|
<DD>
|
|
If set, then the numeric form of the service address is returned.
|
|
(When not set, this will still happen in case the service's name
|
|
cannot be determined.)
|
|
</DL>
|
|
<A NAME="lbAE"> </A>
|
|
<H3>Extensions to getnameinfo() for Internationalized Domain Names</H3>
|
|
|
|
<P>
|
|
|
|
Starting with glibc 2.3.4,
|
|
<B>getnameinfo</B>()
|
|
|
|
has been extended to selectively allow
|
|
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>).
|
|
|
|
Three new flags are defined:
|
|
<DL COMPACT>
|
|
<DT id="6"><B>NI_IDN</B>
|
|
|
|
<DD>
|
|
If this flag is used, then the name found in the lookup process is
|
|
converted from IDN format to the locale's encoding if necessary.
|
|
ASCII-only names are not affected by the conversion, which
|
|
makes this flag usable in existing programs and environments.
|
|
<DT id="7"><B>NI_IDN_ALLOW_UNASSIGNED</B>, <B>NI_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>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
On success, 0 is returned, and node and service names, if requested,
|
|
are filled with null-terminated strings, possibly truncated to fit
|
|
the specified buffer lengths.
|
|
On error, one of the following nonzero error codes is returned:
|
|
<DL COMPACT>
|
|
<DT id="8"><B>EAI_AGAIN</B>
|
|
|
|
<DD>
|
|
The name could not be resolved at this time.
|
|
Try again later.
|
|
<DT id="9"><B>EAI_BADFLAGS</B>
|
|
|
|
<DD>
|
|
The
|
|
<I>flags</I>
|
|
|
|
argument has an invalid value.
|
|
<DT id="10"><B>EAI_FAIL</B>
|
|
|
|
<DD>
|
|
A nonrecoverable error occurred.
|
|
<DT id="11"><B>EAI_FAMILY</B>
|
|
|
|
<DD>
|
|
The address family was not recognized,
|
|
or the address length was invalid for the specified family.
|
|
<DT id="12"><B>EAI_MEMORY</B>
|
|
|
|
<DD>
|
|
Out of memory.
|
|
<DT id="13"><B>EAI_NONAME</B>
|
|
|
|
<DD>
|
|
The name does not resolve for the supplied arguments.
|
|
<B>NI_NAMEREQD</B>
|
|
|
|
is set and the host's name cannot be located,
|
|
or neither hostname nor service name were requested.
|
|
<DT id="14"><B>EAI_OVERFLOW</B>
|
|
|
|
<DD>
|
|
The buffer pointed to by
|
|
<I>host</I>
|
|
|
|
or
|
|
<I>serv</I>
|
|
|
|
was too small.
|
|
<DT id="15"><B>EAI_SYSTEM</B>
|
|
|
|
<DD>
|
|
A system error occurred.
|
|
The error code can be found in
|
|
<I>errno</I>.
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
The
|
|
<B><A HREF="/cgi-bin/man/man2html?3+gai_strerror">gai_strerror</A></B>(3)
|
|
|
|
function translates these error codes to a human readable string,
|
|
suitable for error reporting.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>FILES</H2>
|
|
|
|
<I>/etc/hosts</I>
|
|
|
|
<BR>
|
|
|
|
<I>/etc/nsswitch.conf</I>
|
|
|
|
<BR>
|
|
|
|
<I>/etc/resolv.conf</I>
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>getnameinfo</B>()
|
|
|
|
is provided in glibc since version 2.1.
|
|
<A NAME="lbAI"> </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>getnameinfo</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe env locale<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<P>
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, RFC 2553.
|
|
<A NAME="lbAK"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
In order to assist the programmer in choosing reasonable sizes
|
|
for the supplied buffers,
|
|
<I><<A HREF="file:///usr/include/netdb.h">netdb.h</A>></I>
|
|
|
|
defines the constants
|
|
<P>
|
|
|
|
|
|
|
|
#define NI_MAXHOST 1025
|
|
#define NI_MAXSERV 32
|
|
|
|
|
|
<P>
|
|
|
|
Since glibc 2.8,
|
|
these definitions are exposed only if suitable
|
|
feature test macros are defined, namely:
|
|
<B>_GNU_SOURCE</B>,
|
|
|
|
<B>_DEFAULT_SOURCE</B>
|
|
|
|
(since glibc 2.19),
|
|
or (in glibc versions up to and including 2.19)
|
|
<B>_BSD_SOURCE</B>
|
|
|
|
or
|
|
<B>_SVID_SOURCE</B>.
|
|
|
|
<P>
|
|
|
|
The former is the constant
|
|
<B>MAXDNAME</B>
|
|
|
|
in recent versions of BIND's
|
|
<I><<A HREF="file:///usr/include/arpa/nameser.h">arpa/nameser.h</A>></I>
|
|
|
|
header file.
|
|
The latter is a guess based on the services listed
|
|
in the current Assigned Numbers RFC.
|
|
<P>
|
|
|
|
Before glibc version 2.2, the
|
|
<I>hostlen</I>
|
|
|
|
and
|
|
<I>servlen</I>
|
|
|
|
arguments were typed as
|
|
<I>size_t</I>.
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The following code tries to get the numeric hostname and service name,
|
|
for a given socket address.
|
|
Note that there is no hardcoded reference to
|
|
a particular address family.
|
|
<P>
|
|
|
|
|
|
|
|
struct sockaddr *addr; /* input */
|
|
socklen_t addrlen; /* input */
|
|
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
|
<P>
|
|
if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), sbuf,
|
|
<BR> sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
|
|
<BR> printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
|
|
<P>
|
|
|
|
The following version checks if the socket address has a
|
|
reverse address mapping.
|
|
<P>
|
|
|
|
|
|
|
|
struct sockaddr *addr; /* input */
|
|
socklen_t addrlen; /* input */
|
|
char hbuf[NI_MAXHOST];
|
|
<P>
|
|
if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf),
|
|
<BR> NULL, 0, NI_NAMEREQD))
|
|
<BR> printf("could not resolve hostname");
|
|
else
|
|
<BR> printf("host=%s\n", hbuf);
|
|
|
|
|
|
<P>
|
|
|
|
An example program using
|
|
<B>getnameinfo</B>()
|
|
|
|
can be found in
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3).
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getpeername">getpeername</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getsockname">getsockname</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+recvfrom">recvfrom</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+gethostbyaddr">gethostbyaddr</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getservbyname">getservbyname</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getservbyport">getservbyport</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet_ntop">inet_ntop</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?5+hosts">hosts</A></B>(5),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?5+services">services</A></B>(5),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+hostname">hostname</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?8+named">named</A></B>(8)
|
|
|
|
<P>
|
|
|
|
R. Gilligan, S. Thomson, J. Bound and W. Stevens,
|
|
<I>Basic Socket Interface Extensions for IPv6</I>,
|
|
|
|
RFC 2553, March 1999.
|
|
<P>
|
|
|
|
Tatsuya Jinmei and Atsushi Onoe,
|
|
<I>An Extension of Format for IPv6 Scoped Addresses</I>,
|
|
|
|
internet draft, work in progress
|
|
|
|
|
|
<P>
|
|
|
|
Craig Metz,
|
|
<I>Protocol Independence Using the Sockets API</I>,
|
|
|
|
Proceedings of the freenix track:
|
|
2000 USENIX annual technical conference, June 2000
|
|
|
|
|
|
|
|
<A NAME="lbAN"> </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="16"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="17"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="18"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="19"><A HREF="#lbAE">Extensions to getnameinfo() for Internationalized Domain Names</A><DD>
|
|
</DL>
|
|
<DT id="20"><A HREF="#lbAF">RETURN VALUE</A><DD>
|
|
<DT id="21"><A HREF="#lbAG">FILES</A><DD>
|
|
<DT id="22"><A HREF="#lbAH">VERSIONS</A><DD>
|
|
<DT id="23"><A HREF="#lbAI">ATTRIBUTES</A><DD>
|
|
<DT id="24"><A HREF="#lbAJ">CONFORMING TO</A><DD>
|
|
<DT id="25"><A HREF="#lbAK">NOTES</A><DD>
|
|
<DT id="26"><A HREF="#lbAL">EXAMPLE</A><DD>
|
|
<DT id="27"><A HREF="#lbAM">SEE ALSO</A><DD>
|
|
<DT id="28"><A HREF="#lbAN">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>
|