2 * Portions Copyright (C) 2004 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.30.2.4 2004/03/09 06:12:33 marka Exp $ */
21 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
22 * All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. Neither the name of the project nor the names of its contributors
33 * may be used to endorse or promote products derived from this software
34 * without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * Issues to be discussed:
52 * - Return values. There seems to be no standard for return value (RFC2553)
53 * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
61 #include <lwres/lwres.h>
62 #include <lwres/net.h>
63 #include <lwres/netdb.h>
75 * First entry is linked last...
77 { AF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in) },
78 { AF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6) },
82 #define ENI_NOSERVNAME 1
83 #define ENI_NOHOSTNAME 2
88 #define ENI_NOSOCKET 7
91 * The test against 0 is there to keep the Solaris compiler
92 * from complaining about "end-of-loop code not reached".
95 do { result = (code); \
96 if (result != 0) goto cleanup; \
100 lwres_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
101 size_t hostlen, char *serv, size_t servlen, int flags)
106 #ifdef LWRES_PLATFORM_HAVESALEN
116 char numserv[sizeof("65000")];
117 char numaddr[sizeof("abcd:abcd:abcd:abcd:abcd:abcd:255.255.255.255")
118 + 1 + sizeof("4294967295")];
120 lwres_uint32_t lwf = 0;
121 lwres_context_t *lwrctx = NULL;
122 lwres_gnbaresponse_t *by = NULL;
123 int result = SUCCESS;
129 #ifdef LWRES_PLATFORM_HAVESALEN
135 family = sa->sa_family;
136 for (i = 0; afdl[i].a_af; i++)
137 if (afdl[i].a_af == family) {
144 if (salen != afd->a_socklen)
149 port = ((const struct sockaddr_in *)sa)->sin_port;
150 addr = &((const struct sockaddr_in *)sa)->sin_addr.s_addr;
154 port = ((const struct sockaddr_in6 *)sa)->sin6_port;
155 addr = ((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
163 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
165 if (serv == NULL || servlen == 0U) {
167 * Caller does not want service.
169 } else if ((flags & NI_NUMERICSERV) != 0 ||
170 (sp = getservbyport(port, proto)) == NULL) {
171 sprintf(numserv, "%d", ntohs(port));
172 if ((strlen(numserv) + 1) > servlen)
174 strcpy(serv, numserv);
176 if ((strlen(sp->s_name) + 1) > servlen)
178 strcpy(serv, sp->s_name);
182 switch (sa->sa_family) {
184 v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
185 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
186 flags |= NI_NUMERICHOST;
187 v4a >>= IN_CLASSA_NSHIFT;
188 if (v4a == 0 || v4a == IN_LOOPBACKNET)
189 flags |= NI_NUMERICHOST;
193 pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
194 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
195 flags |= NI_NUMERICHOST;
200 if (host == NULL || hostlen == 0U) {
204 } else if (flags & NI_NUMERICHOST) {
205 if (lwres_net_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
208 #if defined(LWRES_HAVE_SIN6_SCOPE_ID)
209 if (afd->a_af == AF_INET6 &&
210 ((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
211 char *p = numaddr + strlen(numaddr);
212 const char *stringscope = NULL;
214 if ((flags & NI_NUMERICSCOPE) == 0) {
216 * Vendors may want to add support for
217 * non-numeric scope identifier.
222 if (stringscope == NULL) {
223 snprintf(p, sizeof(numaddr) - (p - numaddr),
225 ((const struct sockaddr_in6 *)sa)->sin6_scope_id);
227 snprintf(p, sizeof(numaddr) - (p - numaddr),
228 "%%%s", stringscope);
232 if (strlen(numaddr) + 1 > hostlen)
234 strcpy(host, numaddr);
238 lwf = LWRES_ADDRTYPE_V4;
241 lwf = LWRES_ADDRTYPE_V6;
247 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
249 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
252 n = lwres_getnamebyaddr(lwrctx, lwf,
253 (lwres_uint16_t)afd->a_addrlen,
256 if (flags & NI_NOFQDN) {
257 p = strchr(by->realname, '.');
261 if ((strlen(by->realname) + 1) > hostlen)
263 strcpy(host, by->realname);
265 if (flags & NI_NAMEREQD)
267 if (lwres_net_ntop(afd->a_af, addr, numaddr,
271 if ((strlen(numaddr) + 1) > hostlen)
273 strcpy(host, numaddr);
279 lwres_gnbaresponse_free(lwrctx, &by);
280 if (lwrctx != NULL) {
281 lwres_conf_clear(lwrctx);
282 lwres_context_destroy(&lwrctx);