1 .\" This page is in the public domain.
2 .\" Almost all details are from RFC 2553.
4 .\" 2004-12-14, mtk, Added EAI_OVERFLOW error
5 .\" 2004-12-14 Fixed description of error return
7 .TH getnameinfo 3 2000-12-11 "Linux Man Page" "UNIX Programmer's Manual"
9 getnameinfo \- address-to-name translation in protocol-independent manner
12 .B #include <sys/socket.h>
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" );
22 function is defined for protocol-independent address-to-nodename translation.
23 It combines the functionality of
31 argument is a pointer to a generic socket address structure
38 that holds the input IP address and port number.
43 are pointers to buffers (of size
47 respectively) to hold the return values.
49 The caller can specify that no hostname (or no service name)
50 is required by providing a NULL
58 parameter. However, at least one of hostname or service name
63 argument modifies the behaviour of
68 If set, return only the hostname part of the FQDN for local hosts.
71 If set, then the numeric form of the hostname is returned.
72 .\" For example, by calling
75 .\" .IR gethostbyaddr() .
76 (When not set, this will still happen in case the node's name
80 If set, then a error is returned if the hostname cannot be looked up.
83 If set, then the service address is returned in numeric form,
84 for example by its port number.
87 If set, then the service is datagram (UDP) based rather than
88 stream (TCP) based. This is required for the few ports (512-514)
89 that have different services for UDP and TCP.
91 On success 0 is returned, and node and service names, if requested,
92 are filled with NUL-terminated strings, possibly truncated to fit
93 the specified buffer lengths.
94 On error one of the following non-zero error codes is returned:
97 The name could not be resolved at this time. Try again later.
102 parameter has an invalid value.
105 A non-recoverable error occurred.
108 The address family was not recognized,
109 or the address length was invalid for the specified family.
115 The name does not resolve for the supplied parameters.
116 NI_NAMEREQD is set and the host's name cannot be located,
117 or neither hostname nor service name were requested.
120 The buffer pointed to by
127 A system error occurred. The error code can be found in
137 function translates these error codes to a human readable string,
138 suitable for error reporting.
140 In order to assist the programmer in choosing reasonable sizes
141 for the supplied buffers,
143 defines the constants
146 # define NI_MAXHOST 1025
148 # define NI_MAXSERV 32
151 The former is the constant MAXDNAME in recent versions of BIND's
153 header file. The latter is a guess based on the services listed
154 in the current Assigned Numbers RFC.
156 The following code tries to get the numeric hostname and service name, for
157 a given socket address. Note that there is no hardcoded reference to
158 a particular address family.
162 struct sockaddr *sa; /* input */
163 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
165 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
166 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
167 printf("host=%s, serv=%s\en", hbuf, sbuf);
171 The following version checks if the socket address has a
172 reverse address mapping.
176 struct sockaddr *sa; /* input */
177 char hbuf[NI_MAXHOST];
179 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf),
180 NULL, 0, NI_NAMEREQD))
181 printf("could not resolve hostname");
183 printf("host=%s\en", hbuf);
187 RFC 2553. (See also XNS, issue 5.2.)
190 .BR gethostbyaddr (3),
191 .BR getservbyname (3),
192 .BR getservbyport (3),
200 R. Gilligan, S. Thomson, J. Bound and W. Stevens,
201 .IR "Basic Socket Interface Extensions for IPv6" ,
202 RFC 2553, March 1999.
204 Tatsuya Jinmei and Atsushi Onoe,
205 .IR "An Extension of Format for IPv6 Scoped Addresses" ,
206 internet draft, work in progress.
207 ftp://ftp.ietf.org/internet-drafts/draft-ietf-ipngwg-scopedaddr-format-02.txt
210 .IR "Protocol Independence Using the Sockets API" ,
211 Proceedings of the freenix track:
212 2000 USENIX annual technical conference, June 2000.
213 http://www.usenix.org/publications/library/proceedings/usenix2000/freenix/metzprotocol.html