2 * Portions Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 1999-2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: getnameinfo.c,v 1.34.18.3 2005/04/29 00:17:18 marka Exp $ */
23 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
24 * All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. Neither the name of the project nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * Issues to be discussed:
54 * - Return values. There seems to be no standard for return value (RFC2553)
55 * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
60 * This function is equivalent to the getnameinfo(3) function defined in
61 * RFC2133. lwres_getnameinfo() returns the hostname for the struct
62 * sockaddr sa which is salen bytes long. The hostname is of length
63 * hostlen and is returned via *host. The maximum length of the hostname
64 * is 1025 bytes: #NI_MAXHOST.
66 * The name of the service associated with the port number in sa is
67 * returned in *serv. It is servlen bytes long. The maximum length of the
68 * service name is #NI_MAXSERV - 32 bytes.
70 * The flags argument sets the following bits:
73 * A fully qualified domain name is not required for local hosts.
74 * The local part of the fully qualified domain name is returned
78 * Return the address in numeric form, as if calling inet_ntop(),
79 * instead of a host name.
82 * A name is required. If the hostname cannot be found in the DNS
83 * and this flag is set, a non-zero error code is returned. If the
84 * hostname is not found and the flag is not set, the address is
85 * returned in numeric form.
88 * The service name is returned as a digit string representing the
92 * Specifies that the service being looked up is a datagram
93 * service, and causes getservbyport() to be called with a second
94 * argument of "udp" instead of its default of "tcp". This is
95 * required for the few ports (512-514) that have different
96 * services for UDP and TCP.
98 * \section getnameinfo_return Return Values
100 * lwres_getnameinfo() returns 0 on success or a non-zero error code if
103 * \section getname_see See Also
105 * RFC2133, getservbyport(),
106 * lwres_getnamebyaddr(). lwres_net_ntop().
108 * \section getnameinfo_bugs Bugs
110 * RFC2133 fails to define what the nonzero return values of
119 #include <lwres/lwres.h>
120 #include <lwres/net.h>
121 #include <lwres/netdb.h>
124 #include "assert_p.h"
128 /*% afd structure definition */
135 * First entry is linked last...
137 { AF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in) },
138 { AF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6) },
142 #define ENI_NOSERVNAME 1
143 #define ENI_NOHOSTNAME 2
148 #define ENI_NOSOCKET 7
151 * The test against 0 is there to keep the Solaris compiler
152 * from complaining about "end-of-loop code not reached".
155 do { result = (code); \
156 if (result != 0) goto cleanup; \
159 /*% lightweight resolver socket address structure to hostname and service name */
161 lwres_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
162 size_t hostlen, char *serv, size_t servlen, int flags)
167 #ifdef LWRES_PLATFORM_HAVESALEN
177 char numserv[sizeof("65000")];
178 char numaddr[sizeof("abcd:abcd:abcd:abcd:abcd:abcd:255.255.255.255")
179 + 1 + sizeof("4294967295")];
181 lwres_uint32_t lwf = 0;
182 lwres_context_t *lwrctx = NULL;
183 lwres_gnbaresponse_t *by = NULL;
184 int result = SUCCESS;
190 #ifdef LWRES_PLATFORM_HAVESALEN
196 family = sa->sa_family;
197 for (i = 0; afdl[i].a_af; i++)
198 if (afdl[i].a_af == family) {
205 if (salen != afd->a_socklen)
210 port = ((const struct sockaddr_in *)sa)->sin_port;
211 addr = &((const struct sockaddr_in *)sa)->sin_addr.s_addr;
215 port = ((const struct sockaddr_in6 *)sa)->sin6_port;
216 addr = ((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
224 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
226 if (serv == NULL || servlen == 0U) {
228 * Caller does not want service.
230 } else if ((flags & NI_NUMERICSERV) != 0 ||
231 (sp = getservbyport(port, proto)) == NULL) {
232 snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
233 if ((strlen(numserv) + 1) > servlen)
235 strcpy(serv, numserv);
237 if ((strlen(sp->s_name) + 1) > servlen)
239 strcpy(serv, sp->s_name);
243 switch (sa->sa_family) {
245 v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
246 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
247 flags |= NI_NUMERICHOST;
248 v4a >>= IN_CLASSA_NSHIFT;
249 if (v4a == 0 || v4a == IN_LOOPBACKNET)
250 flags |= NI_NUMERICHOST;
254 pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
255 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
256 flags |= NI_NUMERICHOST;
261 if (host == NULL || hostlen == 0U) {
265 } else if (flags & NI_NUMERICHOST) {
266 if (lwres_net_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
269 #if defined(LWRES_HAVE_SIN6_SCOPE_ID)
270 if (afd->a_af == AF_INET6 &&
271 ((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
272 char *p = numaddr + strlen(numaddr);
273 const char *stringscope = NULL;
275 if ((flags & NI_NUMERICSCOPE) == 0) {
277 * Vendors may want to add support for
278 * non-numeric scope identifier.
283 if (stringscope == NULL) {
284 snprintf(p, sizeof(numaddr) - (p - numaddr),
286 ((const struct sockaddr_in6 *)sa)->sin6_scope_id);
288 snprintf(p, sizeof(numaddr) - (p - numaddr),
289 "%%%s", stringscope);
293 if (strlen(numaddr) + 1 > hostlen)
295 strcpy(host, numaddr);
299 lwf = LWRES_ADDRTYPE_V4;
302 lwf = LWRES_ADDRTYPE_V6;
308 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
310 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
313 n = lwres_getnamebyaddr(lwrctx, lwf,
314 (lwres_uint16_t)afd->a_addrlen,
317 if (flags & NI_NOFQDN) {
318 p = strchr(by->realname, '.');
322 if ((strlen(by->realname) + 1) > hostlen)
324 strcpy(host, by->realname);
326 if (flags & NI_NAMEREQD)
328 if (lwres_net_ntop(afd->a_af, addr, numaddr,
332 if ((strlen(numaddr) + 1) > hostlen)
334 strcpy(host, numaddr);
340 lwres_gnbaresponse_free(lwrctx, &by);
341 if (lwrctx != NULL) {
342 lwres_conf_clear(lwrctx);
343 lwres_context_destroy(&lwrctx);