2004-11-03 13:51:07 +00:00
|
|
|
.\" This page is in the public domain.
|
|
|
|
.\" Almost all details are from RFC 2553.
|
|
|
|
.\"
|
2004-12-15 16:10:55 +00:00
|
|
|
.\" 2004-12-14, mtk, Added EAI_OVERFLOW error
|
|
|
|
.\" 2004-12-14 Fixed description of error return
|
|
|
|
.\"
|
2009-12-03 11:23:22 +00:00
|
|
|
.TH GETNAMEINFO 3 2009-12-03 "GNU" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
getnameinfo \- address-to-name translation in protocol-independent manner
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <sys/socket.h>
|
|
|
|
.B #include <netdb.h>
|
|
|
|
.sp
|
|
|
|
.BI "int getnameinfo(const struct sockaddr *" "sa" ", socklen_t " "salen" ,
|
|
|
|
.BI " char *" "host" ", size_t " "hostlen" ,
|
|
|
|
.BI " char *" "serv" ", size_t " "servlen" ", int " "flags" );
|
|
|
|
.fi
|
2008-08-29 15:50:13 +00:00
|
|
|
.sp
|
|
|
|
.in -4n
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
|
|
.BR feature_test_macros (7)):
|
|
|
|
.ad l
|
|
|
|
.in
|
|
|
|
.sp
|
|
|
|
.BR getnameinfo ():
|
|
|
|
_POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
|
|
|
|
.ad b
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
|
|
|
The
|
2007-11-24 10:10:39 +00:00
|
|
|
.BR getnameinfo ()
|
2008-06-12 05:16:43 +00:00
|
|
|
function is the inverse of
|
|
|
|
.BR getaddrinfo (3):
|
|
|
|
it converts a socket address to a corresponding host and service,
|
|
|
|
in a protocol-independent manner.
|
2004-11-03 13:51:07 +00:00
|
|
|
It combines the functionality of
|
|
|
|
.BR gethostbyaddr (3)
|
|
|
|
and
|
2008-06-12 05:16:43 +00:00
|
|
|
.BR getservbyport (3),
|
|
|
|
but unlike those functions,
|
|
|
|
.BR getaddrinfo (3)
|
|
|
|
is reentrant and allows programs to eliminate
|
|
|
|
IPv4-versus-IPv6 dependencies.
|
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
|
|
|
.I sa
|
|
|
|
argument is a pointer to a generic socket address structure
|
|
|
|
(of type
|
|
|
|
.I sockaddr_in
|
|
|
|
or
|
|
|
|
.IR sockaddr_in6 )
|
|
|
|
of size
|
2007-09-20 16:26:31 +00:00
|
|
|
.I salen
|
2004-11-03 13:51:07 +00:00
|
|
|
that holds the input IP address and port number.
|
|
|
|
The arguments
|
|
|
|
.I host
|
|
|
|
and
|
|
|
|
.I serv
|
2008-06-12 05:16:43 +00:00
|
|
|
are pointers to caller-allocated buffers (of size
|
2004-11-03 13:51:07 +00:00
|
|
|
.I hostlen
|
|
|
|
and
|
|
|
|
.I servlen
|
2008-06-12 05:16:43 +00:00
|
|
|
respectively) into which
|
|
|
|
.BR getnameinfo ()
|
|
|
|
places null-terminated strings containing the host and
|
|
|
|
service names respectively.
|
2004-11-03 13:51:07 +00:00
|
|
|
|
|
|
|
The caller can specify that no hostname (or no service name)
|
|
|
|
is required by providing a NULL
|
|
|
|
.I host
|
|
|
|
(or
|
|
|
|
.IR serv )
|
|
|
|
argument or a zero
|
|
|
|
.I hostlen
|
|
|
|
(or
|
|
|
|
.IR servlen )
|
2008-06-12 05:16:43 +00:00
|
|
|
argument.
|
2007-04-12 22:42:49 +00:00
|
|
|
However, at least one of hostname or service name
|
2004-11-03 13:51:07 +00:00
|
|
|
must be requested.
|
|
|
|
|
|
|
|
The
|
|
|
|
.I flags
|
2007-06-08 09:56:56 +00:00
|
|
|
argument modifies the behavior of
|
2007-11-24 10:10:39 +00:00
|
|
|
.BR getnameinfo ()
|
2004-11-03 13:51:07 +00:00
|
|
|
as follows:
|
|
|
|
.TP
|
2008-06-12 05:16:43 +00:00
|
|
|
.B NI_NAMEREQD
|
|
|
|
If set, then an error is returned if the hostname cannot be determined.
|
|
|
|
.TP
|
|
|
|
.B NI_DGRAM
|
|
|
|
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.
|
|
|
|
.TP
|
2004-11-03 13:51:07 +00:00
|
|
|
.B NI_NOFQDN
|
2008-06-12 05:16:43 +00:00
|
|
|
If set, return only the hostname part of the fully qualified domain name
|
|
|
|
for local hosts.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B NI_NUMERICHOST
|
|
|
|
If set, then the numeric form of the hostname is returned.
|
|
|
|
.\" For example, by calling
|
2007-12-21 12:05:03 +00:00
|
|
|
.\" .BR inet_ntop ()
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" instead of
|
2007-12-21 12:05:03 +00:00
|
|
|
.\" .BR gethostbyaddr ().
|
2004-11-03 13:51:07 +00:00
|
|
|
(When not set, this will still happen in case the node's name
|
2008-06-12 05:16:43 +00:00
|
|
|
cannot be determined.)
|
|
|
|
.\" POSIX.1-2003 has NI_NUMERICSCOPE, but glibc doesn't have it.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B NI_NUMERICSERV
|
2008-06-12 05:16:43 +00:00
|
|
|
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.)
|
2006-11-25 05:35:02 +00:00
|
|
|
.SS "Extensions to getaddrinfo() for Internationalized Domain Names"
|
|
|
|
.PP
|
|
|
|
Starting with glibc 2.3.4,
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR getnameinfo ()
|
2006-11-25 05:35:02 +00:00
|
|
|
has been extended to selectively allow
|
2008-06-27 03:37:56 +00:00
|
|
|
hostnames to be transparently converted to and from the
|
2006-11-25 05:35:02 +00:00
|
|
|
Internationalized Domain Name (IDN) format (see RFC 3490,
|
|
|
|
.IR "Internationalizing Domain Names in Applications (IDNA)" ).
|
|
|
|
Three new flags are defined:
|
|
|
|
.TP
|
|
|
|
.B NI_IDN
|
|
|
|
If this flag is used, then the name found in the lookup process is
|
2007-04-12 22:42:49 +00:00
|
|
|
converted from IDN format to the locale's encoding if necessary.
|
2006-11-25 05:35:02 +00:00
|
|
|
ASCII-only names are not affected by the conversion, which
|
|
|
|
makes this flag usable in existing programs and environments.
|
|
|
|
.TP
|
|
|
|
.BR NI_IDN_ALLOW_UNASSIGNED ", " NI_IDN_USE_STD3_ASCII_RULES
|
2007-04-12 22:42:49 +00:00
|
|
|
Setting these flags will enable the
|
2006-11-25 05:35:02 +00:00
|
|
|
IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
|
2007-04-12 22:42:49 +00:00
|
|
|
IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
|
2008-06-27 03:37:56 +00:00
|
|
|
conforming hostname)
|
2006-11-25 05:35:02 +00:00
|
|
|
flags respectively to be used in the IDNA handling.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "RETURN VALUE"
|
2006-11-25 05:37:22 +00:00
|
|
|
.\" FIXME glibc defines the following additional errors, some which
|
2007-04-12 22:42:49 +00:00
|
|
|
.\" can probably be returned by getnameinfo(); they need to
|
2006-11-25 05:37:22 +00:00
|
|
|
.\" be documented.
|
2007-12-09 07:50:38 +00:00
|
|
|
.\" #ifdef __USE_GNU
|
|
|
|
.\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
|
|
|
|
.\" #define EAI_CANCELED -101 /* Request canceled. */
|
|
|
|
.\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
|
|
|
|
.\" #define EAI_ALLDONE -103 /* All requests done. */
|
|
|
|
.\" #define EAI_INTR -104 /* Interrupted by a signal. */
|
|
|
|
.\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
|
|
|
|
.\" #endif
|
2004-11-03 13:51:07 +00:00
|
|
|
On success 0 is returned, and node and service names, if requested,
|
2006-01-13 02:09:44 +00:00
|
|
|
are filled with null-terminated strings, possibly truncated to fit
|
2004-11-03 13:51:07 +00:00
|
|
|
the specified buffer lengths.
|
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
|
|
|
On error one of the following nonzero error codes is returned:
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B EAI_AGAIN
|
2007-04-12 22:42:49 +00:00
|
|
|
The name could not be resolved at this time.
|
|
|
|
Try again later.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B EAI_BADFLAGS
|
|
|
|
The
|
|
|
|
.I flags
|
2008-06-12 05:16:43 +00:00
|
|
|
argument has an invalid value.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B EAI_FAIL
|
2010-01-16 17:01:12 +00:00
|
|
|
A nonrecoverable error occurred.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B EAI_FAMILY
|
|
|
|
The address family was not recognized,
|
|
|
|
or the address length was invalid for the specified family.
|
|
|
|
.TP
|
|
|
|
.B EAI_MEMORY
|
|
|
|
Out of memory.
|
|
|
|
.TP
|
|
|
|
.B EAI_NONAME
|
2008-06-12 05:16:43 +00:00
|
|
|
The name does not resolve for the supplied arguments.
|
2007-06-22 19:42:52 +00:00
|
|
|
.B NI_NAMEREQD
|
|
|
|
is set and the host's name cannot be located,
|
2004-11-03 13:51:07 +00:00
|
|
|
or neither hostname nor service name were requested.
|
|
|
|
.TP
|
2004-12-15 16:10:55 +00:00
|
|
|
.B EAI_OVERFLOW
|
|
|
|
The buffer pointed to by
|
|
|
|
.I host
|
|
|
|
or
|
|
|
|
.I serv
|
|
|
|
was too small.
|
|
|
|
.TP
|
2004-11-03 13:51:07 +00:00
|
|
|
.B EAI_SYSTEM
|
2007-04-12 22:42:49 +00:00
|
|
|
A system error occurred.
|
|
|
|
The error code can be found in
|
2004-11-03 13:51:07 +00:00
|
|
|
.IR errno .
|
2005-12-06 15:13:12 +00:00
|
|
|
.PP
|
2005-07-04 07:39:01 +00:00
|
|
|
The
|
|
|
|
.BR gai_strerror (3)
|
|
|
|
function translates these error codes to a human readable string,
|
|
|
|
suitable for error reporting.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH FILES
|
|
|
|
/etc/hosts
|
|
|
|
.br
|
|
|
|
/etc/nsswitch.conf
|
|
|
|
.br
|
|
|
|
/etc/resolv.conf
|
2008-06-11 13:43:34 +00:00
|
|
|
.SH VERSIONS
|
|
|
|
.BR getnameinfo ()
|
|
|
|
is provided in glibc since version 2.1.
|
2007-05-19 04:30:20 +00:00
|
|
|
.SH "CONFORMING TO"
|
|
|
|
RFC\ 2553, POSIX.1-2001.
|
2007-05-16 02:54:18 +00:00
|
|
|
.SH NOTES
|
2004-11-03 13:51:07 +00:00
|
|
|
In order to assist the programmer in choosing reasonable sizes
|
|
|
|
for the supplied buffers,
|
|
|
|
.I <netdb.h>
|
|
|
|
defines the constants
|
2007-12-19 06:57:44 +00:00
|
|
|
.in +4n
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
2007-05-28 10:45:57 +00:00
|
|
|
|
2007-12-09 07:50:38 +00:00
|
|
|
#define NI_MAXHOST 1025
|
|
|
|
#define NI_MAXSERV 32
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
2007-05-28 10:45:57 +00:00
|
|
|
.in
|
2009-12-03 12:57:42 +00:00
|
|
|
|
2009-12-03 11:23:22 +00:00
|
|
|
Since glibc 2.8,
|
|
|
|
these definitions are exposed only if one of the feature test macros
|
|
|
|
.BR _BSD_SOURCE ,
|
|
|
|
.BR _SVID_SOURCE ,
|
|
|
|
or
|
|
|
|
.BR _GNU_SOURCE
|
|
|
|
is defined.
|
2007-05-28 10:45:57 +00:00
|
|
|
.PP
|
2007-06-22 19:42:52 +00:00
|
|
|
The former is the constant
|
|
|
|
.B MAXDNAME
|
|
|
|
in recent versions of BIND's
|
2004-11-03 13:51:07 +00:00
|
|
|
.I <arpa/nameser.h>
|
2007-04-12 22:42:49 +00:00
|
|
|
header file.
|
|
|
|
The latter is a guess based on the services listed
|
2004-11-03 13:51:07 +00:00
|
|
|
in the current Assigned Numbers RFC.
|
2006-05-22 19:44:47 +00:00
|
|
|
.SH EXAMPLE
|
2007-05-28 10:45:57 +00:00
|
|
|
The following code tries to get the numeric hostname and service name,
|
|
|
|
for a given socket address.
|
2007-04-12 22:42:49 +00:00
|
|
|
Note that there is no hardcoded reference to
|
2004-11-03 13:51:07 +00:00
|
|
|
a particular address family.
|
|
|
|
|
2007-12-19 06:57:44 +00:00
|
|
|
.in +4n
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
2007-05-28 10:45:57 +00:00
|
|
|
struct sockaddr *sa; /* input */
|
2009-02-23 01:51:28 +00:00
|
|
|
socklen_t len; /* input */
|
2007-05-28 10:45:57 +00:00
|
|
|
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
2004-11-03 13:51:07 +00:00
|
|
|
|
2008-06-13 09:54:52 +00:00
|
|
|
if (getnameinfo(sa, len, hbuf, sizeof(hbuf), sbuf,
|
2007-05-28 10:45:57 +00:00
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
|
|
|
|
printf("host=%s, serv=%s\en", hbuf, sbuf);
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
2007-05-28 10:45:57 +00:00
|
|
|
.in
|
2004-11-03 13:51:07 +00:00
|
|
|
|
|
|
|
The following version checks if the socket address has a
|
|
|
|
reverse address mapping.
|
|
|
|
|
2007-12-19 06:57:44 +00:00
|
|
|
.in +4n
|
2007-05-28 10:45:57 +00:00
|
|
|
.nf
|
|
|
|
struct sockaddr *sa; /* input */
|
2009-02-23 01:51:28 +00:00
|
|
|
socklen_t len; /* input */
|
2007-05-28 10:45:57 +00:00
|
|
|
char hbuf[NI_MAXHOST];
|
2004-11-03 13:51:07 +00:00
|
|
|
|
2008-06-13 09:54:52 +00:00
|
|
|
if (getnameinfo(sa, len, hbuf, sizeof(hbuf),
|
2007-05-28 10:45:57 +00:00
|
|
|
NULL, 0, NI_NAMEREQD))
|
|
|
|
printf("could not resolve hostname");
|
|
|
|
else
|
|
|
|
printf("host=%s\en", hbuf);
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
2007-05-28 10:45:57 +00:00
|
|
|
.in
|
2007-05-28 10:29:27 +00:00
|
|
|
.PP
|
2007-05-28 10:45:57 +00:00
|
|
|
An example program using
|
2007-05-28 10:29:27 +00:00
|
|
|
.BR getnameinfo ()
|
|
|
|
can be found in
|
|
|
|
.BR getaddrinfo (3).
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
2008-06-12 05:16:43 +00:00
|
|
|
.BR accept (2),
|
|
|
|
.BR getpeername (2),
|
|
|
|
.BR getsockname (2),
|
|
|
|
.BR recvfrom (2),
|
2007-06-01 06:29:34 +00:00
|
|
|
.BR socket (2),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR getaddrinfo (3),
|
|
|
|
.BR gethostbyaddr (3),
|
|
|
|
.BR getservbyname (3),
|
|
|
|
.BR getservbyport (3),
|
|
|
|
.BR inet_ntop (3),
|
|
|
|
.BR hosts (5),
|
|
|
|
.BR services (5),
|
|
|
|
.BR hostname (7),
|
|
|
|
.BR named (8)
|
|
|
|
.LP
|
|
|
|
R. Gilligan, S. Thomson, J. Bound and W. Stevens,
|
|
|
|
.IR "Basic Socket Interface Extensions for IPv6" ,
|
2005-07-20 07:50:45 +00:00
|
|
|
RFC\ 2553, March 1999.
|
2004-11-03 13:51:07 +00:00
|
|
|
.LP
|
|
|
|
Tatsuya Jinmei and Atsushi Onoe,
|
|
|
|
.IR "An Extension of Format for IPv6 Scoped Addresses" ,
|
|
|
|
internet draft, work in progress.
|
2007-06-20 21:39:45 +00:00
|
|
|
ftp://ftp.ietf.org/internet\-drafts/draft\-ietf\-ipngwg\-scopedaddr\-format\-02.txt
|
2004-11-03 13:51:07 +00:00
|
|
|
.LP
|
|
|
|
Craig Metz,
|
|
|
|
.IR "Protocol Independence Using the Sockets API" ,
|
|
|
|
Proceedings of the freenix track:
|
|
|
|
2000 USENIX annual technical conference, June 2000.
|
|
|
|
http://www.usenix.org/publications/library/proceedings/usenix2000/freenix/metzprotocol.html
|