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.3.2.4 2004/08/28 06:25:24 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>
76 * First entry is linked last...
78 { AF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in) },
79 { AF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6) },
83 #define ENI_NOSERVNAME 1
84 #define ENI_NOHOSTNAME 2
89 #define ENI_NOSOCKET 7
92 * The test against 0 is there to keep the Solaris compiler
93 * from complaining about "end-of-loop code not reached".
96 do { result = (code); \
97 if (result != 0) goto cleanup; \
101 lwres_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
102 size_t hostlen, char *serv, size_t servlen, int flags)
107 #ifdef LWRES_PLATFORM_HAVESALEN
117 char numserv[sizeof("65000")];
118 char numaddr[sizeof("abcd:abcd:abcd:abcd:abcd:abcd:255.255.255.255")
119 + 1 + sizeof("4294967295")];
121 lwres_uint32_t lwf = 0;
122 lwres_context_t *lwrctx = NULL;
123 lwres_gnbaresponse_t *by = NULL;
124 int result = SUCCESS;
130 #ifdef LWRES_PLATFORM_HAVESALEN
136 family = sa->sa_family;
137 for (i = 0; afdl[i].a_af; i++)
138 if (afdl[i].a_af == family) {
145 if (salen != afd->a_socklen)
150 port = ((const struct sockaddr_in *)sa)->sin_port;
151 addr = &((const struct sockaddr_in *)sa)->sin_addr.s_addr;
155 port = ((const struct sockaddr_in6 *)sa)->sin6_port;
156 addr = ((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
164 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
166 if (serv == NULL || servlen == 0U) {
168 * Caller does not want service.
170 } else if ((flags & NI_NUMERICSERV) != 0 ||
171 (sp = getservbyport(port, proto)) == NULL) {
172 snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
173 if ((strlen(numserv) + 1) > servlen)
175 strcpy(serv, numserv);
177 if ((strlen(sp->s_name) + 1) > servlen)
179 strcpy(serv, sp->s_name);
183 switch (sa->sa_family) {
185 v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
186 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
187 flags |= NI_NUMERICHOST;
188 v4a >>= IN_CLASSA_NSHIFT;
189 if (v4a == 0 || v4a == IN_LOOPBACKNET)
190 flags |= NI_NUMERICHOST;
194 pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
195 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
196 flags |= NI_NUMERICHOST;
201 if (host == NULL || hostlen == 0U) {
205 } else if (flags & NI_NUMERICHOST) {
206 if (lwres_net_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
209 #if defined(LWRES_HAVE_SIN6_SCOPE_ID)
210 if (afd->a_af == AF_INET6 &&
211 ((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
212 char *p = numaddr + strlen(numaddr);
213 const char *stringscope = NULL;
215 if ((flags & NI_NUMERICSCOPE) == 0) {
217 * Vendors may want to add support for
218 * non-numeric scope identifier.
223 if (stringscope == NULL) {
224 snprintf(p, sizeof(numaddr) - (p - numaddr),
226 ((const struct sockaddr_in6 *)sa)->sin6_scope_id);
228 snprintf(p, sizeof(numaddr) - (p - numaddr),
229 "%%%s", stringscope);
233 if (strlen(numaddr) + 1 > hostlen)
235 strcpy(host, numaddr);
239 lwf = LWRES_ADDRTYPE_V4;
242 lwf = LWRES_ADDRTYPE_V6;
248 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
250 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
253 n = lwres_getnamebyaddr(lwrctx, lwf,
254 (lwres_uint16_t)afd->a_addrlen,
257 if (flags & NI_NOFQDN) {
258 p = strchr(by->realname, '.');
262 if ((strlen(by->realname) + 1) > hostlen)
264 strcpy(host, by->realname);
266 if (flags & NI_NAMEREQD)
268 if (lwres_net_ntop(afd->a_af, addr, numaddr,
272 if ((strlen(numaddr) + 1) > hostlen)
274 strcpy(host, numaddr);
280 lwres_gnbaresponse_free(lwrctx, &by);
281 if (lwrctx != NULL) {
282 lwres_conf_clear(lwrctx);
283 lwres_context_destroy(&lwrctx);