No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / lwres / getnameinfo.c
blob02a9682b19b729ab6ac96602618cbd392efbeef2
1 /* $NetBSD$ */
3 /*
4 * Portions Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 1999-2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: getnameinfo.c,v 1.39 2007/06/19 23:47:22 tbox Exp */
22 /*! \file */
25 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the project nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
54 * XXX
55 * Issues to be discussed:
56 * - Return values. There seems to be no standard for return value (RFC2553)
57 * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
61 /**
62 * This function is equivalent to the getnameinfo(3) function defined in
63 * RFC2133. lwres_getnameinfo() returns the hostname for the struct
64 * sockaddr sa which is salen bytes long. The hostname is of length
65 * hostlen and is returned via *host. The maximum length of the hostname
66 * is 1025 bytes: #NI_MAXHOST.
68 * The name of the service associated with the port number in sa is
69 * returned in *serv. It is servlen bytes long. The maximum length of the
70 * service name is #NI_MAXSERV - 32 bytes.
72 * The flags argument sets the following bits:
74 * \li #NI_NOFQDN:
75 * A fully qualified domain name is not required for local hosts.
76 * The local part of the fully qualified domain name is returned
77 * instead.
79 * \li #NI_NUMERICHOST
80 * Return the address in numeric form, as if calling inet_ntop(),
81 * instead of a host name.
83 * \li #NI_NAMEREQD
84 * A name is required. If the hostname cannot be found in the DNS
85 * and this flag is set, a non-zero error code is returned. If the
86 * hostname is not found and the flag is not set, the address is
87 * returned in numeric form.
89 * \li #NI_NUMERICSERV
90 * The service name is returned as a digit string representing the
91 * port number.
93 * \li #NI_DGRAM
94 * Specifies that the service being looked up is a datagram
95 * service, and causes getservbyport() to be called with a second
96 * argument of "udp" instead of its default of "tcp". This is
97 * required for the few ports (512-514) that have different
98 * services for UDP and TCP.
100 * \section getnameinfo_return Return Values
102 * lwres_getnameinfo() returns 0 on success or a non-zero error code if
103 * an error occurs.
105 * \section getname_see See Also
107 * RFC2133, getservbyport(),
108 * lwres_getnamebyaddr(). lwres_net_ntop().
110 * \section getnameinfo_bugs Bugs
112 * RFC2133 fails to define what the nonzero return values of
113 * getnameinfo() are.
116 #include <config.h>
118 #include <stdio.h>
119 #include <string.h>
121 #include <lwres/lwres.h>
122 #include <lwres/net.h>
123 #include <lwres/netdb.h>
124 #include "print_p.h"
126 #include "assert_p.h"
128 #define SUCCESS 0
130 /*% afd structure definition */
131 static struct afd {
132 int a_af;
133 size_t a_addrlen;
134 size_t a_socklen;
135 } afdl [] = {
137 * First entry is linked last...
139 { AF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in) },
140 { AF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6) },
141 {0, 0, 0},
144 #define ENI_NOSERVNAME 1
145 #define ENI_NOHOSTNAME 2
146 #define ENI_MEMORY 3
147 #define ENI_SYSTEM 4
148 #define ENI_FAMILY 5
149 #define ENI_SALEN 6
150 #define ENI_NOSOCKET 7
153 * The test against 0 is there to keep the Solaris compiler
154 * from complaining about "end-of-loop code not reached".
156 #define ERR(code) \
157 do { result = (code); \
158 if (result != 0) goto cleanup; \
159 } while (0)
161 /*% lightweight resolver socket address structure to hostname and service name */
163 lwres_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
164 size_t hostlen, char *serv, size_t servlen, int flags)
166 struct afd *afd;
167 struct servent *sp;
168 unsigned short port;
169 #ifdef LWRES_PLATFORM_HAVESALEN
170 size_t len;
171 #endif
172 int family, i;
173 const void *addr;
174 char *p;
175 #if 0
176 unsigned long v4a;
177 unsigned char pfx;
178 #endif
179 char numserv[sizeof("65000")];
180 char numaddr[sizeof("abcd:abcd:abcd:abcd:abcd:abcd:255.255.255.255")
181 + 1 + sizeof("4294967295")];
182 const char *proto;
183 lwres_uint32_t lwf = 0;
184 lwres_context_t *lwrctx = NULL;
185 lwres_gnbaresponse_t *by = NULL;
186 int result = SUCCESS;
187 int n;
189 if (sa == NULL)
190 ERR(ENI_NOSOCKET);
192 #ifdef LWRES_PLATFORM_HAVESALEN
193 len = sa->sa_len;
194 if (len != salen)
195 ERR(ENI_SALEN);
196 #endif
198 family = sa->sa_family;
199 for (i = 0; afdl[i].a_af; i++)
200 if (afdl[i].a_af == family) {
201 afd = &afdl[i];
202 goto found;
204 ERR(ENI_FAMILY);
206 found:
207 if (salen != afd->a_socklen)
208 ERR(ENI_SALEN);
210 switch (family) {
211 case AF_INET:
212 port = ((const struct sockaddr_in *)sa)->sin_port;
213 addr = &((const struct sockaddr_in *)sa)->sin_addr.s_addr;
214 break;
216 case AF_INET6:
217 port = ((const struct sockaddr_in6 *)sa)->sin6_port;
218 addr = ((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
219 break;
221 default:
222 port = 0;
223 addr = NULL;
224 INSIST(0);
226 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
228 if (serv == NULL || servlen == 0U) {
230 * Caller does not want service.
232 } else if ((flags & NI_NUMERICSERV) != 0 ||
233 (sp = getservbyport(port, proto)) == NULL) {
234 snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
235 if ((strlen(numserv) + 1) > servlen)
236 ERR(ENI_MEMORY);
237 strcpy(serv, numserv);
238 } else {
239 if ((strlen(sp->s_name) + 1) > servlen)
240 ERR(ENI_MEMORY);
241 strcpy(serv, sp->s_name);
244 #if 0
245 switch (sa->sa_family) {
246 case AF_INET:
247 v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
248 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
249 flags |= NI_NUMERICHOST;
250 v4a >>= IN_CLASSA_NSHIFT;
251 if (v4a == 0 || v4a == IN_LOOPBACKNET)
252 flags |= NI_NUMERICHOST;
253 break;
255 case AF_INET6:
256 pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
257 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
258 flags |= NI_NUMERICHOST;
259 break;
261 #endif
263 if (host == NULL || hostlen == 0U) {
265 * What should we do?
267 } else if (flags & NI_NUMERICHOST) {
268 if (lwres_net_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
269 == NULL)
270 ERR(ENI_SYSTEM);
271 #if defined(LWRES_HAVE_SIN6_SCOPE_ID)
272 if (afd->a_af == AF_INET6 &&
273 ((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
274 char *p = numaddr + strlen(numaddr);
275 const char *stringscope = NULL;
276 #if 0
277 if ((flags & NI_NUMERICSCOPE) == 0) {
279 * Vendors may want to add support for
280 * non-numeric scope identifier.
282 stringscope = foo;
284 #endif
285 if (stringscope == NULL) {
286 snprintf(p, sizeof(numaddr) - (p - numaddr),
287 "%%%u",
288 ((const struct sockaddr_in6 *)sa)->sin6_scope_id);
289 } else {
290 snprintf(p, sizeof(numaddr) - (p - numaddr),
291 "%%%s", stringscope);
294 #endif
295 if (strlen(numaddr) + 1 > hostlen)
296 ERR(ENI_MEMORY);
297 strcpy(host, numaddr);
298 } else {
299 switch (family) {
300 case AF_INET:
301 lwf = LWRES_ADDRTYPE_V4;
302 break;
303 case AF_INET6:
304 lwf = LWRES_ADDRTYPE_V6;
305 break;
306 default:
307 INSIST(0);
310 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
311 if (n == 0)
312 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
314 if (n == 0)
315 n = lwres_getnamebyaddr(lwrctx, lwf,
316 (lwres_uint16_t)afd->a_addrlen,
317 addr, &by);
318 if (n == 0) {
319 if (flags & NI_NOFQDN) {
320 p = strchr(by->realname, '.');
321 if (p)
322 *p = '\0';
324 if ((strlen(by->realname) + 1) > hostlen)
325 ERR(ENI_MEMORY);
326 strcpy(host, by->realname);
327 } else {
328 if (flags & NI_NAMEREQD)
329 ERR(ENI_NOHOSTNAME);
330 if (lwres_net_ntop(afd->a_af, addr, numaddr,
331 sizeof(numaddr))
332 == NULL)
333 ERR(ENI_NOHOSTNAME);
334 if ((strlen(numaddr) + 1) > hostlen)
335 ERR(ENI_MEMORY);
336 strcpy(host, numaddr);
339 result = SUCCESS;
340 cleanup:
341 if (by != NULL)
342 lwres_gnbaresponse_free(lwrctx, &by);
343 if (lwrctx != NULL) {
344 lwres_conf_clear(lwrctx);
345 lwres_context_destroy(&lwrctx);
347 return (result);