1.\" $FreeBSD$ 2.\" $KAME: getnameinfo.3,v 1.17 2000/08/09 21:16:17 itojun Exp $ 3.\" 4.\" Copyright (c) 1983, 1987, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed by the University of 18.\" California, Berkeley and its contributors. 19.\" 4. Neither the name of the University nor the names of its contributors 20.\" may be used to endorse or promote products derived from this software 21.\" without specific prior written permission. 22.\" 23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" SUCH DAMAGE. 34.\" 35.\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95 36.\" 37.Dd May 25, 1995 38.Dt GETNAMEINFO 3 39.Os 40.\" 41.Sh NAME 42.Nm getnameinfo 43.Nd address-to-nodename translation in protocol-independent manner 44.\" 45.Sh LIBRARY 46.Lb libc 47.Sh SYNOPSIS 48.Fd #include <sys/types.h> 49.Fd #include <sys/socket.h> 50.Fd #include <netdb.h> 51.Ft int 52.Fn getnameinfo "const struct sockaddr *sa" "socklen_t salen" \ 53"char *host" "size_t hostlen" "char *serv" "size_t servlen" "int flags" 54.\" 55.Sh DESCRIPTION 56The 57.Fn getnameinfo 58function is defined for protocol-independent address-to-nodename translation. 59Its functionality is a reverse conversion of 60.Xr getaddrinfo 3 , 61and implements similar functionality with 62.Xr gethostbyaddr 3 63and 64.Xr getservbyport 3 65in more sophisticated manner. 66.Pp 67This function looks up an IP address and port number provided by the 68caller in the DNS and system-specific database, and returns text 69strings for both in buffers provided by the caller. 70The function indicates successful completion by a zero return value; 71a non-zero return value indicates failure. 72.Pp 73The first argument, 74.Fa sa , 75points to either a 76.Li sockaddr_in 77structure (for IPv4) or a 78.Li sockaddr_in6 79structure (for IPv6) that holds the IP address and port number. 80The 81.Fa salen 82argument gives the length of the 83.Li sockaddr_in 84or 85.Li sockaddr_in6 86structure. 87.Pp 88The function returns the nodename associated with the IP address in 89the buffer pointed to by the 90.Fa host 91argument. 92The caller provides the size of this buffer via the 93.Fa hostlen 94argument. 95The service name associated with the port number is returned in the buffer 96pointed to by 97.Fa serv , 98and the 99.Fa servlen 100argument gives the length of this buffer. 101The caller specifies not to return either string by providing a zero 102value for the 103.Fa hostlen 104or 105.Fa servlen 106arguments. 107Otherwise, the caller must provide buffers large enough to hold the 108nodename and the service name, including the terminating null characters. 109.Pp 110Unfortunately most systems do not provide constants that specify the 111maximum size of either a fully-qualified domain name or a service name. 112Therefore to aid the application in allocating buffers for these two 113returned strings the following constants are defined in 114.Aq Pa netdb.h : 115.Bd -literal -offset 116#define NI_MAXHOST 1025 117#define NI_MAXSERV 32 118.Ed 119.Pp 120The first value is actually defined as the constant 121.Dv MAXDNAME 122in recent versions of BIND's 123.Aq Pa arpa/nameser.h 124header 125.Po 126older versions of BIND define this constant to be 256 127.Pc 128and the second is a guess based on the services listed in the current 129Assigned Numbers RFC. 130.Pp 131The final argument is a 132.Fa flag 133that changes the default actions of this function. 134By default the fully-qualified domain name (FQDN) for the host is 135looked up in the DNS and returned. 136If the flag bit 137.Dv NI_NOFQDN 138is set, only the nodename portion of the FQDN is returned for local hosts. 139.Pp 140If the 141.Fa flag 142bit 143.Dv NI_NUMERICHOST 144is set, or if the host's name cannot be located in the DNS, 145the numeric form of the host's address is returned instead of its name 146.Po 147e.g., by calling 148.Fn inet_ntop 149instead of 150.Fn getnodebyaddr 151.Pc . 152If the 153.Fa flag 154bit 155.Dv NI_NAMEREQD 156is set, an error is returned if the host's name cannot be located in the DNS. 157.Pp 158If the flag bit 159.Dv NI_NUMERICSERV 160is set, the numeric form of the service address is returned 161.Pq e.g., its port number 162instead of its name. 163The two 164.Dv NI_NUMERICxxx 165flags are required to support the 166.Fl n 167flag that many commands provide. 168.Pp 169A fifth flag bit, 170.Dv NI_DGRAM , 171specifies that the service is a datagram service, and causes 172.Fn getservbyport 173to be called with a second argument of 174.Dq udp 175instead of its default of 176.Dq tcp . 177This is required for the few ports (512-514) 178that have different services for UDP and TCP. 179.Pp 180These 181.Dv NI_xxx 182flags are defined in 183.Aq Pa netdb.h . 184.\" 185.Sh EXTENSION 186The implementation allows experimental numeric IPv6 address notation with 187scope identifier. 188IPv6 link-local address will appear as string like 189.Dq Li fe80::1%ne0 , 190if 191.Dv NI_WITHSCOPEID 192bit is enabled in 193.Ar flags 194argument. 195Refer to 196.Xr getaddrinfo 3 197for the notation. 198.\" 199.Sh EXAMPLES 200The following code tries to get numeric hostname, and service name, 201for given socket address. 202Observe that there is no hardcoded reference to particular address family. 203.Bd -literal -offset indent 204struct sockaddr *sa; /* input */ 205char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; 206 207if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf, 208 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) { 209 errx(1, "could not get numeric hostname"); 210 /*NOTREACHED*/ 211} 212printf("host=%s, serv=%s\\n", hbuf, sbuf); 213.Ed 214.Pp 215The following version checks if the socket address has reverse address mapping. 216.Bd -literal -offset indent 217struct sockaddr *sa; /* input */ 218char hbuf[NI_MAXHOST]; 219 220if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0, 221 NI_NAMEREQD)) { 222 errx(1, "could not resolve hostname"); 223 /*NOTREACHED*/ 224} 225printf("host=%s\\n", hbuf); 226.Ed 227.\" 228.Sh FILES 229.Bl -tag -width /etc/nsswitch.conf -compact 230.It Pa /etc/hosts 231.It Pa /etc/nsswitch.conf 232.It Pa /etc/resolv.conf 233.El 234.\" 235.Sh DIAGNOSTICS 236The function indicates successful completion by a zero return value; 237a non-zero return value indicates failure. 238Error codes are as below: 239.Bl -tag -width Er 240.It Bq Er EAI_AGAIN 241The name could not be resolved at this time. 242Future attempts may succeed. 243.It Bq Er EAI_BADFLAGS 244The flags had an invalid value. 245.It Bq Er EAI_FAIL 246A non-recoverable error occurred. 247.It Bq Er EAI_FAMILY 248The address family was not recognized or the address length was invalid 249for the specified family. 250.It Bq Er EAI_MEMORY 251There was a memory allocation failure. 252.It Bq Er EAI_NONAME 253The name does not resolve for the supplied parameters. 254.Dv NI_NAMEREQD 255is set and the host's name cannot be located, 256or both nodename and servname were null. 257.It Bq Er EAI_SYSTEM 258A system error occurred. 259The error code can be found in errno. 260.El 261.\" 262.Sh SEE ALSO 263.Xr getaddrinfo 3 , 264.Xr gethostbyaddr 3 , 265.Xr getservbyport 3 , 266.Xr hosts 5 , 267.Xr services 5 , 268.Xr hostname 7 , 269.Xr named 8 270.Pp 271.Rs 272.%A R. Gilligan 273.%A S. Thomson 274.%A J. Bound 275.%A W. Stevens 276.%T Basic Socket Interface Extensions for IPv6 277.%R RFC2553 278.%D March 1999 279.Re 280.Rs 281.%A Tatsuya Jinmei 282.%A Atsushi Onoe 283.%T "An Extension of Format for IPv6 Scoped Addresses" 284.%R internet draft 285.%N draft-ietf-ipngwg-scopedaddr-format-02.txt 286.%O work in progress material 287.Re 288.Rs 289.%A Craig Metz 290.%T Protocol Independence Using the Sockets API 291.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference" 292.%D June 2000 293.Re 294.\" 295.Sh HISTORY 296The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit. 297.\" 298.Sh STANDARDS 299The 300.Fn getaddrinfo 301function is defined in 302.St -p1003.1g-2000 , 303and documented in 304.Dq Basic Socket Interface Extensions for IPv6 305.Pq RFC2553 . 306.\" 307.Sh BUGS 308The current implementation is not thread-safe. 309.Pp 310The text was shamelessly copied from RFC2553. 311.Pp 312The type of the 2nd argument should be 313.Li socklen_t 314for RFC2553 conformance. 315The current code is based on pre-RFC2553 specification. 316