update the readme files for bind-9.3.4 import
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / lwres / getnameinfo.c
1 /*
2  * Portions Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright (C) 1999-2001, 2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: getnameinfo.c,v 1.30.2.4 2004/03/09 06:12:33 marka Exp $ */
19
20 /*
21  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
22  * All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
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.
35  *
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
46  * SUCH DAMAGE.
47  */
48
49 /*
50  * XXX
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().
54  */
55
56 #include <config.h>
57
58 #include <stdio.h>
59 #include <string.h>
60
61 #include <lwres/lwres.h>
62 #include <lwres/net.h>
63 #include <lwres/netdb.h>
64
65 #include "assert_p.h"
66
67 #define SUCCESS 0
68
69 static struct afd {
70         int a_af;
71         size_t a_addrlen;
72         size_t a_socklen;
73 } afdl [] = {
74         /*
75          * First entry is linked last...
76          */
77         { AF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in) },
78         { AF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6) },
79         {0, 0, 0},
80 };
81
82 #define ENI_NOSERVNAME  1
83 #define ENI_NOHOSTNAME  2
84 #define ENI_MEMORY      3
85 #define ENI_SYSTEM      4
86 #define ENI_FAMILY      5
87 #define ENI_SALEN       6
88 #define ENI_NOSOCKET    7
89
90 /*
91  * The test against 0 is there to keep the Solaris compiler
92  * from complaining about "end-of-loop code not reached".
93  */
94 #define ERR(code) \
95         do { result = (code);                   \
96                 if (result != 0) goto cleanup;  \
97         } while (0)
98
99 int
100 lwres_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
101                   size_t hostlen, char *serv, size_t servlen, int flags)
102 {
103         struct afd *afd;
104         struct servent *sp;
105         unsigned short port;
106 #ifdef LWRES_PLATFORM_HAVESALEN
107         size_t len;
108 #endif
109         int family, i;
110         const void *addr;
111         char *p;
112 #if 0
113         unsigned long v4a;
114         unsigned char pfx;
115 #endif
116         char numserv[sizeof("65000")];
117         char numaddr[sizeof("abcd:abcd:abcd:abcd:abcd:abcd:255.255.255.255")
118                     + 1 + sizeof("4294967295")];
119         const char *proto;
120         lwres_uint32_t lwf = 0;
121         lwres_context_t *lwrctx = NULL;
122         lwres_gnbaresponse_t *by = NULL;
123         int result = SUCCESS;
124         int n;
125
126         if (sa == NULL)
127                 ERR(ENI_NOSOCKET);
128
129 #ifdef LWRES_PLATFORM_HAVESALEN
130         len = sa->sa_len;
131         if (len != salen)
132                 ERR(ENI_SALEN);
133 #endif
134
135         family = sa->sa_family;
136         for (i = 0; afdl[i].a_af; i++)
137                 if (afdl[i].a_af == family) {
138                         afd = &afdl[i];
139                         goto found;
140                 }
141         ERR(ENI_FAMILY);
142
143  found:
144         if (salen != afd->a_socklen)
145                 ERR(ENI_SALEN);
146
147         switch (family) {
148         case AF_INET:
149                 port = ((const struct sockaddr_in *)sa)->sin_port;
150                 addr = &((const struct sockaddr_in *)sa)->sin_addr.s_addr;
151                 break;
152
153         case AF_INET6:
154                 port = ((const struct sockaddr_in6 *)sa)->sin6_port;
155                 addr = ((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
156                 break;
157
158         default:
159                 port = 0;
160                 addr = NULL;
161                 INSIST(0);
162         }
163         proto = (flags & NI_DGRAM) ? "udp" : "tcp";
164
165         if (serv == NULL || servlen == 0U) {
166                 /*
167                  * Caller does not want service.
168                  */
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)
173                         ERR(ENI_MEMORY);
174                 strcpy(serv, numserv);
175         } else {
176                 if ((strlen(sp->s_name) + 1) > servlen)
177                         ERR(ENI_MEMORY);
178                 strcpy(serv, sp->s_name);
179         }
180
181 #if 0
182         switch (sa->sa_family) {
183         case AF_INET:
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;
190                 break;
191
192         case AF_INET6:
193                 pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
194                 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
195                         flags |= NI_NUMERICHOST;
196                 break;
197         }
198 #endif
199
200         if (host == NULL || hostlen == 0U) {
201                 /*
202                  * What should we do?
203                  */
204         } else if (flags & NI_NUMERICHOST) {
205                 if (lwres_net_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
206                     == NULL)
207                         ERR(ENI_SYSTEM);
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;
213 #if 0
214                         if ((flags & NI_NUMERICSCOPE) == 0) {
215                                 /*
216                                  * Vendors may want to add support for
217                                  * non-numeric scope identifier.
218                                  */
219                                 stringscope = foo;
220                         }
221 #endif
222                         if (stringscope == NULL) {
223                                 snprintf(p, sizeof(numaddr) - (p - numaddr),
224                                     "%%%u",
225                                     ((const struct sockaddr_in6 *)sa)->sin6_scope_id);
226                         } else {
227                                 snprintf(p, sizeof(numaddr) - (p - numaddr),
228                                     "%%%s", stringscope);
229                         }
230                 }
231 #endif
232                 if (strlen(numaddr) + 1 > hostlen)
233                         ERR(ENI_MEMORY);
234                 strcpy(host, numaddr);
235         } else {
236                 switch (family) {
237                 case AF_INET:
238                         lwf = LWRES_ADDRTYPE_V4;
239                         break;
240                 case AF_INET6:
241                         lwf = LWRES_ADDRTYPE_V6;
242                         break;
243                 default:
244                         INSIST(0);
245                 }
246
247                 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
248                 if (n == 0)
249                         (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
250
251                 if (n == 0)
252                         n = lwres_getnamebyaddr(lwrctx, lwf,
253                                                 (lwres_uint16_t)afd->a_addrlen,
254                                                 addr, &by);
255                 if (n == 0) {
256                         if (flags & NI_NOFQDN) {
257                                 p = strchr(by->realname, '.');
258                                 if (p)
259                                         *p = '\0';
260                         }
261                         if ((strlen(by->realname) + 1) > hostlen)
262                                 ERR(ENI_MEMORY);
263                         strcpy(host, by->realname);
264                 } else {
265                         if (flags & NI_NAMEREQD)
266                                 ERR(ENI_NOHOSTNAME);
267                         if (lwres_net_ntop(afd->a_af, addr, numaddr,
268                                            sizeof(numaddr))
269                             == NULL)
270                                 ERR(ENI_NOHOSTNAME);
271                         if ((strlen(numaddr) + 1) > hostlen)
272                                 ERR(ENI_MEMORY);
273                         strcpy(host, numaddr);
274                 }
275         }
276         result = SUCCESS;
277  cleanup:
278         if (by != NULL)
279                 lwres_gnbaresponse_free(lwrctx, &by);
280         if (lwrctx != NULL) {
281                 lwres_conf_clear(lwrctx);
282                 lwres_context_destroy(&lwrctx);
283         }
284         return (result);
285 }