]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getnameinfo.3
ffix
[thirdparty/man-pages.git] / man3 / getnameinfo.3
CommitLineData
fea681da
MK
1.\" This page is in the public domain.
2.\" Almost all details are from RFC 2553.
3.\"
3d4d9116
MK
4.\" 2004-12-14, mtk, Added EAI_OVERFLOW error
5.\" 2004-12-14 Fixed description of error return
6.\"
cb5f1a35 7.TH GETNAMEINFO 3 2007-06-08 "GNU" "Linux Programmer's Manual"
fea681da
MK
8.SH NAME
9getnameinfo \- address-to-name translation in protocol-independent manner
10.SH SYNOPSIS
11.nf
12.B #include <sys/socket.h>
13.B #include <netdb.h>
14.sp
15.BI "int getnameinfo(const struct sockaddr *" "sa" ", socklen_t " "salen" ,
16.BI " char *" "host" ", size_t " "hostlen" ,
17.BI " char *" "serv" ", size_t " "servlen" ", int " "flags" );
18.fi
19.SH DESCRIPTION
20The
21.BR getnameinfo (3)
22function is defined for protocol-independent address-to-nodename translation.
23It combines the functionality of
24.BR gethostbyaddr (3)
25and
26.BR getservbyport (3)
27and is the inverse of
28.BR getaddrinfo (3).
29The
30.I sa
31argument is a pointer to a generic socket address structure
32(of type
33.I sockaddr_in
34or
35.IR sockaddr_in6 )
36of size
37.IR salen
38that holds the input IP address and port number.
39The arguments
40.I host
41and
42.I serv
43are pointers to buffers (of size
44.I hostlen
45and
46.I servlen
47respectively) to hold the return values.
48
49The caller can specify that no hostname (or no service name)
50is required by providing a NULL
51.I host
52(or
53.IR serv )
54argument or a zero
55.I hostlen
56(or
57.IR servlen )
c13182ef
MK
58parameter.
59However, at least one of hostname or service name
fea681da
MK
60must be requested.
61
62The
63.I flags
d9bfdb9c 64argument modifies the behavior of
fea681da
MK
65.BR getnameinfo (3)
66as follows:
67.TP
68.B NI_NOFQDN
69If set, return only the hostname part of the FQDN for local hosts.
70.TP
71.B NI_NUMERICHOST
72If set, then the numeric form of the hostname is returned.
73.\" For example, by calling
74.\" .I inet_ntop()
75.\" instead of
76.\" .IR gethostbyaddr() .
77(When not set, this will still happen in case the node's name
78cannot be looked up.)
79.TP
80.B NI_NAMEREQD
81If set, then a error is returned if the hostname cannot be looked up.
82.TP
83.B NI_NUMERICSERV
84If set, then the service address is returned in numeric form,
85for example by its port number.
86.TP
87.B NI_DGRAM
88If set, then the service is datagram (UDP) based rather than
c13182ef
MK
89stream (TCP) based.
90This is required for the few ports (512-514)
fea681da 91that have different services for UDP and TCP.
22135cad
MK
92.SS "Extensions to getaddrinfo() for Internationalized Domain Names"
93.PP
94Starting with glibc 2.3.4,
c13182ef 95.BR getnameinfo ()
22135cad 96has been extended to selectively allow
c13182ef 97host names to be transparently converted to and from the
22135cad
MK
98Internationalized Domain Name (IDN) format (see RFC 3490,
99.IR "Internationalizing Domain Names in Applications (IDNA)" ).
100Three new flags are defined:
101.TP
102.B NI_IDN
103If this flag is used, then the name found in the lookup process is
c13182ef 104converted from IDN format to the locale's encoding if necessary.
22135cad
MK
105ASCII-only names are not affected by the conversion, which
106makes this flag usable in existing programs and environments.
107.TP
108.BR NI_IDN_ALLOW_UNASSIGNED ", " NI_IDN_USE_STD3_ASCII_RULES
c13182ef 109Setting these flags will enable the
22135cad 110IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
c13182ef
MK
111IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
112conforming host name)
22135cad 113flags respectively to be used in the IDNA handling.
fea681da 114.SH "RETURN VALUE"
420dad7a 115.\" FIXME glibc defines the following additional errors, some which
c13182ef 116.\" can probably be returned by getnameinfo(); they need to
420dad7a
MK
117.\" be documented.
118.\" # ifdef __USE_GNU
119.\" # define EAI_INPROGRESS -100 /* Processing request in progress. */
120.\" # define EAI_CANCELED -101 /* Request canceled. */
121.\" # define EAI_NOTCANCELED -102 /* Request not canceled. */
122.\" # define EAI_ALLDONE -103 /* All requests done. */
123.\" # define EAI_INTR -104 /* Interrupted by a signal. */
124.\" # define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
125.\" # endif
fea681da 126On success 0 is returned, and node and service names, if requested,
28d88c17 127are filled with null-terminated strings, possibly truncated to fit
fea681da 128the specified buffer lengths.
3d4d9116 129On error one of the following non-zero error codes is returned:
fea681da
MK
130.TP
131.B EAI_AGAIN
c13182ef
MK
132The name could not be resolved at this time.
133Try again later.
fea681da
MK
134.TP
135.B EAI_BADFLAGS
136The
137.I flags
138parameter has an invalid value.
139.TP
140.B EAI_FAIL
141A non-recoverable error occurred.
142.TP
143.B EAI_FAMILY
144The address family was not recognized,
145or the address length was invalid for the specified family.
146.TP
147.B EAI_MEMORY
148Out of memory.
149.TP
150.B EAI_NONAME
151The name does not resolve for the supplied parameters.
152NI_NAMEREQD is set and the host's name cannot be located,
153or neither hostname nor service name were requested.
154.TP
3d4d9116
MK
155.B EAI_OVERFLOW
156The buffer pointed to by
157.I host
158or
159.I serv
160was too small.
161.TP
fea681da 162.B EAI_SYSTEM
c13182ef
MK
163A system error occurred.
164The error code can be found in
fea681da 165.IR errno .
6a7f0c48 166.PP
d2cf9489
MK
167The
168.BR gai_strerror (3)
169function translates these error codes to a human readable string,
170suitable for error reporting.
fea681da
MK
171.SH FILES
172/etc/hosts
173.br
174/etc/nsswitch.conf
175.br
176/etc/resolv.conf
2b2581ee
MK
177.SH "CONFORMING TO"
178RFC\ 2553, POSIX.1-2001.
19c98696 179.SH NOTES
fea681da
MK
180In order to assist the programmer in choosing reasonable sizes
181for the supplied buffers,
182.I <netdb.h>
183defines the constants
6602f61c 184.in +0.5i
fea681da 185.nf
6602f61c 186
fea681da 187# define NI_MAXHOST 1025
fea681da
MK
188# define NI_MAXSERV 32
189.fi
6602f61c
MK
190.in
191.PP
fea681da
MK
192The former is the constant MAXDNAME in recent versions of BIND's
193.I <arpa/nameser.h>
c13182ef
MK
194header file.
195The latter is a guess based on the services listed
fea681da 196in the current Assigned Numbers RFC.
9b336505 197.SH EXAMPLE
6602f61c
MK
198The following code tries to get the numeric hostname and service name,
199for a given socket address.
c13182ef 200Note that there is no hardcoded reference to
fea681da
MK
201a particular address family.
202
6602f61c 203.in +0.5i
fea681da 204.nf
6602f61c
MK
205struct sockaddr *sa; /* input */
206char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
fea681da 207
29059a65 208if (getnameinfo(sa, sa\->sa_len, hbuf, sizeof(hbuf), sbuf,
6602f61c
MK
209 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
210 printf("host=%s, serv=%s\en", hbuf, sbuf);
fea681da 211.fi
6602f61c 212.in
fea681da
MK
213
214The following version checks if the socket address has a
215reverse address mapping.
216
6602f61c
MK
217.in +0.5i
218.nf
219struct sockaddr *sa; /* input */
220char hbuf[NI_MAXHOST];
fea681da 221
29059a65 222if (getnameinfo(sa, sa\->sa_len, hbuf, sizeof(hbuf),
6602f61c
MK
223 NULL, 0, NI_NAMEREQD))
224 printf("could not resolve hostname");
225else
226 printf("host=%s\en", hbuf);
fea681da 227.fi
6602f61c 228.in
77dbc341 229.PP
6602f61c 230An example program using
77dbc341
MK
231.BR getnameinfo ()
232can be found in
233.BR getaddrinfo (3).
fea681da 234.SH "SEE ALSO"
2f1f4221 235.BR socket (2),
fea681da
MK
236.BR getaddrinfo (3),
237.BR gethostbyaddr (3),
238.BR getservbyname (3),
239.BR getservbyport (3),
240.BR inet_ntop (3),
fea681da
MK
241.BR hosts (5),
242.BR services (5),
243.BR hostname (7),
244.BR named (8)
245.LP
246R. Gilligan, S. Thomson, J. Bound and W. Stevens,
247.IR "Basic Socket Interface Extensions for IPv6" ,
331da7c3 248RFC\ 2553, March 1999.
fea681da
MK
249.LP
250Tatsuya Jinmei and Atsushi Onoe,
251.IR "An Extension of Format for IPv6 Scoped Addresses" ,
252internet draft, work in progress.
29059a65 253ftp://ftp.ietf.org/internet\-drafts/draft\-ietf\-ipngwg\-scopedaddr\-format\-02.txt
fea681da
MK
254.LP
255Craig Metz,
256.IR "Protocol Independence Using the Sockets API" ,
257Proceedings of the freenix track:
2582000 USENIX annual technical conference, June 2000.
259http://www.usenix.org/publications/library/proceedings/usenix2000/freenix/metzprotocol.html