1 .\" $FreeBSD: src/lib/libc/net/getnameinfo.3,v 1.2.2.8 2001/12/14 18:33:55 ru Exp $
2 .\" $DragonFly: src/lib/libcr/net/Attic/getnameinfo.3,v 1.2 2003/06/17 04:26:44 dillon Exp $
3 .\" $KAME: getnameinfo.3,v 1.17 2000/08/09 21:16:17 itojun Exp $
5 .\" Copyright (c) 1983, 1987, 1991, 1993
6 .\" The Regents of the University of California. All rights reserved.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95
44 .Nd address-to-nodename translation in protocol-independent manner
53 .Fn getnameinfo "const struct sockaddr *sa" "socklen_t salen" \
54 "char *host" "size_t hostlen" "char *serv" "size_t servlen" "int flags"
59 function is defined for protocol-independent address-to-nodename translation.
60 Its functionality is a reverse conversion of
62 and implements similar functionality with
66 in more sophisticated manner.
68 This function looks up an IP address and port number provided by the
69 caller in the DNS and system-specific database, and returns text
70 strings for both in buffers provided by the caller.
71 The function indicates successful completion by a zero return value;
72 a non-zero return value indicates failure.
78 structure (for IPv4) or a
80 structure (for IPv6) that holds the IP address and port number.
83 argument gives the length of the
89 The function returns the nodename associated with the IP address in
90 the buffer pointed to by the
93 The caller provides the size of this buffer via the
96 The service name associated with the port number is returned in the buffer
101 argument gives the length of this buffer.
102 The caller specifies not to return either string by providing a zero
108 Otherwise, the caller must provide buffers large enough to hold the
109 nodename and the service name, including the terminating null characters.
111 Unfortunately most systems do not provide constants that specify the
112 maximum size of either a fully-qualified domain name or a service name.
113 Therefore to aid the application in allocating buffers for these two
114 returned strings the following constants are defined in
117 #define NI_MAXHOST 1025
118 #define NI_MAXSERV 32
121 The first value is actually defined as the constant
123 in recent versions of BIND's
124 .Aq Pa arpa/nameser.h
126 (older versions of BIND define this constant to be 256)
127 and the second is a guess based on the services listed in the current
128 Assigned Numbers RFC.
130 The final argument is a
132 that changes the default actions of this function.
133 By default the fully-qualified domain name (FQDN) for the host is
134 looked up in the DNS and returned.
137 is set, only the nodename portion of the FQDN is returned for local hosts.
143 is set, or if the host's name cannot be located in the DNS,
144 the numeric form of the host's address is returned instead of its name
148 .Fn getnodebyaddr ) .
153 is set, an error is returned if the host's name cannot be located in the DNS.
157 is set, the numeric form of the service address is returned
158 (e.g., its port number)
162 flags are required to support the
164 flag that many commands provide.
168 specifies that the service is a datagram service, and causes
170 to be called with a second argument of
172 instead of its default of
174 This is required for the few ports (512-514)
175 that have different services for UDP and TCP.
183 The implementation allows experimental numeric IPv6 address notation with
185 IPv6 link-local address will appear as string like
197 The following code tries to get numeric hostname, and service name,
198 for given socket address.
199 Observe that there is no hardcoded reference to particular address family.
200 .Bd -literal -offset indent
201 struct sockaddr *sa; /* input */
202 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
204 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
205 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
206 errx(1, "could not get numeric hostname");
209 printf("host=%s, serv=%s\\n", hbuf, sbuf);
212 The following version checks if the socket address has reverse address mapping.
213 .Bd -literal -offset indent
214 struct sockaddr *sa; /* input */
215 char hbuf[NI_MAXHOST];
217 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
219 errx(1, "could not resolve hostname");
222 printf("host=%s\\n", hbuf);
226 .Bl -tag -width /etc/resolv.conf -compact
228 .It Pa /etc/host.conf
229 .It Pa /etc/resolv.conf
233 The function indicates successful completion by a zero return value;
234 a non-zero return value indicates failure.
235 Error codes are as below:
238 The name could not be resolved at this time.
239 Future attempts may succeed.
240 .It Bq Er EAI_BADFLAGS
241 The flags had an invalid value.
243 A non-recoverable error occurred.
245 The address family was not recognized or the address length was invalid
246 for the specified family.
248 There was a memory allocation failure.
250 The name does not resolve for the supplied parameters.
252 is set and the host's name cannot be located,
253 or both nodename and servname were null.
255 A system error occurred.
256 The error code can be found in errno.
261 .Xr gethostbyaddr 3 ,
262 .Xr getservbyport 3 ,
273 .%T Basic Socket Interface Extensions for IPv6
280 .%T "An Extension of Format for IPv6 Scoped Addresses"
282 .%N draft-ietf-ipngwg-scopedaddr-format-02.txt
283 .%O work in progress material
287 .%T Protocol Independence Using the Sockets API
288 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
293 The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
298 function is defined in
301 .Dq Basic Socket Interface Extensions for IPv6
305 The current implementation is not thread-safe.
307 The text was shamelessly copied from RFC2553.
309 The type of the 2nd argument should be
311 for RFC2553 conformance.
312 The current code is based on pre-RFC2553 specification.