]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getnameinfo.3
getauxval.3: wfix
[thirdparty/man-pages.git] / man3 / getnameinfo.3
CommitLineData
2d6c6dd1 1.\" %%%LICENSE_START(PUBLIC_DOMAIN)
fea681da 2.\" This page is in the public domain.
2d6c6dd1 3.\" %%%LICENSE_END
9f9c6de1 4.\"
fea681da
MK
5.\" Almost all details are from RFC 2553.
6.\"
3d4d9116
MK
7.\" 2004-12-14, mtk, Added EAI_OVERFLOW error
8.\" 2004-12-14 Fixed description of error return
9.\"
5722c835 10.TH GETNAMEINFO 3 2015-07-23 "GNU" "Linux Programmer's Manual"
fea681da
MK
11.SH NAME
12getnameinfo \- address-to-name translation in protocol-independent manner
13.SH SYNOPSIS
14.nf
15.B #include <sys/socket.h>
16.B #include <netdb.h>
17.sp
18.BI "int getnameinfo(const struct sockaddr *" "sa" ", socklen_t " "salen" ,
7c1e49d2
RV
19.BI " char *" "host" ", socklen_t " "hostlen" ,
20.BI " char *" "serv" ", socklen_t " "servlen" ", int " "flags" );
fea681da 21.fi
0f200f07
MK
22.sp
23.in -4n
24Feature Test Macro Requirements for glibc (see
25.BR feature_test_macros (7)):
26.ad l
27.in
28.sp
29.BR getnameinfo ():
30_POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
31.ad b
fea681da
MK
32.SH DESCRIPTION
33The
2777b1ca 34.BR getnameinfo ()
c81e8a42
MK
35function is the inverse of
36.BR getaddrinfo (3):
37it converts a socket address to a corresponding host and service,
38in a protocol-independent manner.
fea681da
MK
39It combines the functionality of
40.BR gethostbyaddr (3)
41and
c81e8a42
MK
42.BR getservbyport (3),
43but unlike those functions,
27a6247b 44.BR getnameinfo ()
c81e8a42
MK
45is reentrant and allows programs to eliminate
46IPv4-versus-IPv6 dependencies.
47
fea681da
MK
48The
49.I sa
50argument is a pointer to a generic socket address structure
51(of type
52.I sockaddr_in
53or
54.IR sockaddr_in6 )
55of size
0daa9e92 56.I salen
fea681da
MK
57that holds the input IP address and port number.
58The arguments
59.I host
60and
61.I serv
c81e8a42 62are pointers to caller-allocated buffers (of size
fea681da
MK
63.I hostlen
64and
65.I servlen
c81e8a42
MK
66respectively) into which
67.BR getnameinfo ()
68places null-terminated strings containing the host and
69service names respectively.
fea681da
MK
70
71The caller can specify that no hostname (or no service name)
72is required by providing a NULL
73.I host
74(or
75.IR serv )
76argument or a zero
77.I hostlen
78(or
79.IR servlen )
c81e8a42 80argument.
c13182ef 81However, at least one of hostname or service name
fea681da
MK
82must be requested.
83
84The
85.I flags
d9bfdb9c 86argument modifies the behavior of
2777b1ca 87.BR getnameinfo ()
fea681da
MK
88as follows:
89.TP
c81e8a42
MK
90.B NI_NAMEREQD
91If set, then an error is returned if the hostname cannot be determined.
92.TP
93.B NI_DGRAM
94If set, then the service is datagram (UDP) based rather than
95stream (TCP) based.
96This is required for the few ports (512-514)
97that have different services for UDP and TCP.
98.TP
fea681da 99.B NI_NOFQDN
c81e8a42
MK
100If set, return only the hostname part of the fully qualified domain name
101for local hosts.
fea681da
MK
102.TP
103.B NI_NUMERICHOST
104If set, then the numeric form of the hostname is returned.
105.\" For example, by calling
ef1ee33c 106.\" .BR inet_ntop ()
fea681da 107.\" instead of
ef1ee33c 108.\" .BR gethostbyaddr ().
fea681da 109(When not set, this will still happen in case the node's name
c81e8a42
MK
110cannot be determined.)
111.\" POSIX.1-2003 has NI_NUMERICSCOPE, but glibc doesn't have it.
fea681da
MK
112.TP
113.B NI_NUMERICSERV
c81e8a42
MK
114If set, then the numeric form of the service address is returned.
115(When not set, this will still happen in case the service's name
116cannot be determined.)
73d8cece 117.SS Extensions to getnameinfo() for Internationalized Domain Names
22135cad
MK
118.PP
119Starting with glibc 2.3.4,
c13182ef 120.BR getnameinfo ()
22135cad 121has been extended to selectively allow
ddaec46d 122hostnames to be transparently converted to and from the
22135cad
MK
123Internationalized Domain Name (IDN) format (see RFC 3490,
124.IR "Internationalizing Domain Names in Applications (IDNA)" ).
125Three new flags are defined:
126.TP
127.B NI_IDN
128If this flag is used, then the name found in the lookup process is
c13182ef 129converted from IDN format to the locale's encoding if necessary.
22135cad
MK
130ASCII-only names are not affected by the conversion, which
131makes this flag usable in existing programs and environments.
132.TP
133.BR NI_IDN_ALLOW_UNASSIGNED ", " NI_IDN_USE_STD3_ASCII_RULES
c13182ef 134Setting these flags will enable the
22135cad 135IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
c13182ef 136IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
ddaec46d 137conforming hostname)
22135cad 138flags respectively to be used in the IDNA handling.
47297adb 139.SH RETURN VALUE
420dad7a 140.\" FIXME glibc defines the following additional errors, some which
c13182ef 141.\" can probably be returned by getnameinfo(); they need to
420dad7a 142.\" be documented.
3d32fee8
MK
143.\" #ifdef __USE_GNU
144.\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
145.\" #define EAI_CANCELED -101 /* Request canceled. */
146.\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
147.\" #define EAI_ALLDONE -103 /* All requests done. */
148.\" #define EAI_INTR -104 /* Interrupted by a signal. */
149.\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
150.\" #endif
60569afd 151On success, 0 is returned, and node and service names, if requested,
28d88c17 152are filled with null-terminated strings, possibly truncated to fit
fea681da 153the specified buffer lengths.
dec985f9 154On error, one of the following nonzero error codes is returned:
fea681da
MK
155.TP
156.B EAI_AGAIN
c13182ef
MK
157The name could not be resolved at this time.
158Try again later.
fea681da
MK
159.TP
160.B EAI_BADFLAGS
161The
162.I flags
c81e8a42 163argument has an invalid value.
fea681da
MK
164.TP
165.B EAI_FAIL
b28f6e56 166A nonrecoverable error occurred.
fea681da
MK
167.TP
168.B EAI_FAMILY
169The address family was not recognized,
170or the address length was invalid for the specified family.
171.TP
172.B EAI_MEMORY
173Out of memory.
174.TP
175.B EAI_NONAME
c81e8a42 176The name does not resolve for the supplied arguments.
2f0af33b
MK
177.B NI_NAMEREQD
178is set and the host's name cannot be located,
fea681da
MK
179or neither hostname nor service name were requested.
180.TP
3d4d9116
MK
181.B EAI_OVERFLOW
182The buffer pointed to by
183.I host
184or
185.I serv
186was too small.
187.TP
fea681da 188.B EAI_SYSTEM
c13182ef
MK
189A system error occurred.
190The error code can be found in
fea681da 191.IR errno .
6a7f0c48 192.PP
d2cf9489
MK
193The
194.BR gai_strerror (3)
195function translates these error codes to a human readable string,
196suitable for error reporting.
fea681da
MK
197.SH FILES
198/etc/hosts
199.br
200/etc/nsswitch.conf
201.br
202/etc/resolv.conf
c343e74c
MK
203.SH VERSIONS
204.BR getnameinfo ()
205is provided in glibc since version 2.1.
a9e30c8c
ZL
206.SH ATTRIBUTES
207For an explanation of the terms used in this section, see
208.BR attributes (7).
209.TS
210allbox;
211lb lb lb
212l l l.
213Interface Attribute Value
214T{
215.BR getnameinfo ()
216T} Thread safety MT-Safe env locale
217.TE
218
47297adb 219.SH CONFORMING TO
0281851d 220POSIX.1-2001, POSIX.1-2008, RFC\ 2553.
19c98696 221.SH NOTES
fea681da
MK
222In order to assist the programmer in choosing reasonable sizes
223for the supplied buffers,
224.I <netdb.h>
225defines the constants
088a639b 226.in +4n
fea681da 227.nf
6602f61c 228
3d32fee8
MK
229#define NI_MAXHOST 1025
230#define NI_MAXSERV 32
fea681da 231.fi
6602f61c 232.in
a10ab4c6 233
0d4b699d
MK
234Since glibc 2.8,
235these definitions are exposed only if one of the feature test macros
236.BR _BSD_SOURCE ,
237.BR _SVID_SOURCE ,
238or
239.BR _GNU_SOURCE
240is defined.
6602f61c 241.PP
2f0af33b
MK
242The former is the constant
243.B MAXDNAME
244in recent versions of BIND's
fea681da 245.I <arpa/nameser.h>
c13182ef
MK
246header file.
247The latter is a guess based on the services listed
fea681da 248in the current Assigned Numbers RFC.
c15f85d8
MK
249
250Before glibc version 2.2, the
251.I hostlen
252and
253.I servlen
254arguments were typed as
255.IR size_t .
9b336505 256.SH EXAMPLE
6602f61c
MK
257The following code tries to get the numeric hostname and service name,
258for a given socket address.
c13182ef 259Note that there is no hardcoded reference to
fea681da
MK
260a particular address family.
261
088a639b 262.in +4n
fea681da 263.nf
6602f61c 264struct sockaddr *sa; /* input */
9d05bd16 265socklen_t len; /* input */
6602f61c 266char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
fea681da 267
481c58ca 268if (getnameinfo(sa, len, hbuf, sizeof(hbuf), sbuf,
6602f61c 269 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
31a6818e 270 printf("host=%s, serv=%s\en", hbuf, sbuf);
fea681da 271.fi
6602f61c 272.in
fea681da
MK
273
274The following version checks if the socket address has a
275reverse address mapping.
276
088a639b 277.in +4n
6602f61c
MK
278.nf
279struct sockaddr *sa; /* input */
9d05bd16 280socklen_t len; /* input */
6602f61c 281char hbuf[NI_MAXHOST];
fea681da 282
481c58ca 283if (getnameinfo(sa, len, hbuf, sizeof(hbuf),
6602f61c
MK
284 NULL, 0, NI_NAMEREQD))
285 printf("could not resolve hostname");
286else
31a6818e 287 printf("host=%s\en", hbuf);
fea681da 288.fi
6602f61c 289.in
77dbc341 290.PP
6602f61c 291An example program using
77dbc341
MK
292.BR getnameinfo ()
293can be found in
294.BR getaddrinfo (3).
47297adb 295.SH SEE ALSO
c81e8a42
MK
296.BR accept (2),
297.BR getpeername (2),
298.BR getsockname (2),
299.BR recvfrom (2),
2f1f4221 300.BR socket (2),
fea681da
MK
301.BR getaddrinfo (3),
302.BR gethostbyaddr (3),
303.BR getservbyname (3),
304.BR getservbyport (3),
305.BR inet_ntop (3),
fea681da
MK
306.BR hosts (5),
307.BR services (5),
308.BR hostname (7),
309.BR named (8)
173fe7e7 310
fea681da
MK
311R. Gilligan, S. Thomson, J. Bound and W. Stevens,
312.IR "Basic Socket Interface Extensions for IPv6" ,
331da7c3 313RFC\ 2553, March 1999.
173fe7e7 314
fea681da
MK
315Tatsuya Jinmei and Atsushi Onoe,
316.IR "An Extension of Format for IPv6 Scoped Addresses" ,
173fe7e7 317internet draft, work in progress
608bf950 318.UR ftp://ftp.ietf.org\:/internet\-drafts\:/draft\-ietf\-ipngwg\-scopedaddr\-format\-02.txt
173fe7e7
DP
319.UE .
320
fea681da
MK
321Craig Metz,
322.IR "Protocol Independence Using the Sockets API" ,
323Proceedings of the freenix track:
173fe7e7 3242000 USENIX annual technical conference, June 2000
d0f1fd33 325.ad l
608bf950 326.UR http://www.usenix.org\:/publications\:/library\:/proceedings\:/usenix2000\:/freenix\:/metzprotocol.html
173fe7e7 327.UE .