Merge from vendor branch GCC:
[dragonfly.git] / contrib / bind-9.3 / 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.3.2.4 2004/08/28 06:25:24 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 #include "print_p.h"
65
66 #include "assert_p.h"
67
68 #define SUCCESS 0
69
70 static struct afd {
71         int a_af;
72         size_t a_addrlen;
73         size_t a_socklen;
74 } afdl [] = {
75         /*
76          * First entry is linked last...
77          */
78         { AF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in) },
79         { AF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6) },
80         {0, 0, 0},
81 };
82
83 #define ENI_NOSERVNAME  1
84 #define ENI_NOHOSTNAME  2
85 #define ENI_MEMORY      3
86 #define ENI_SYSTEM      4
87 #define ENI_FAMILY      5
88 #define ENI_SALEN       6
89 #define ENI_NOSOCKET    7
90
91 /*
92  * The test against 0 is there to keep the Solaris compiler
93  * from complaining about "end-of-loop code not reached".
94  */
95 #define ERR(code) \
96         do { result = (code);                   \
97                 if (result != 0) goto cleanup;  \
98         } while (0)
99
100 int
101 lwres_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
102                   size_t hostlen, char *serv, size_t servlen, int flags)
103 {
104         struct afd *afd;
105         struct servent *sp;
106         unsigned short port;
107 #ifdef LWRES_PLATFORM_HAVESALEN
108         size_t len;
109 #endif
110         int family, i;
111         const void *addr;
112         char *p;
113 #if 0
114         unsigned long v4a;
115         unsigned char pfx;
116 #endif
117         char numserv[sizeof("65000")];
118         char numaddr[sizeof("abcd:abcd:abcd:abcd:abcd:abcd:255.255.255.255")
119                     + 1 + sizeof("4294967295")];
120         const char *proto;
121         lwres_uint32_t lwf = 0;
122         lwres_context_t *lwrctx = NULL;
123         lwres_gnbaresponse_t *by = NULL;
124         int result = SUCCESS;
125         int n;
126
127         if (sa == NULL)
128                 ERR(ENI_NOSOCKET);
129
130 #ifdef LWRES_PLATFORM_HAVESALEN
131         len = sa->sa_len;
132         if (len != salen)
133                 ERR(ENI_SALEN);
134 #endif
135
136         family = sa->sa_family;
137         for (i = 0; afdl[i].a_af; i++)
138                 if (afdl[i].a_af == family) {
139                         afd = &afdl[i];
140                         goto found;
141                 }
142         ERR(ENI_FAMILY);
143
144  found:
145         if (salen != afd->a_socklen)
146                 ERR(ENI_SALEN);
147
148         switch (family) {
149         case AF_INET:
150                 port = ((const struct sockaddr_in *)sa)->sin_port;
151                 addr = &((const struct sockaddr_in *)sa)->sin_addr.s_addr;
152                 break;
153
154         case AF_INET6:
155                 port = ((const struct sockaddr_in6 *)sa)->sin6_port;
156                 addr = ((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
157                 break;
158
159         default:
160                 port = 0;
161                 addr = NULL;
162                 INSIST(0);
163         }
164         proto = (flags & NI_DGRAM) ? "udp" : "tcp";
165
166         if (serv == NULL || servlen == 0U) {
167                 /*
168                  * Caller does not want service.
169                  */
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)
174                         ERR(ENI_MEMORY);
175                 strcpy(serv, numserv);
176         } else {
177                 if ((strlen(sp->s_name) + 1) > servlen)
178                         ERR(ENI_MEMORY);
179                 strcpy(serv, sp->s_name);
180         }
181
182 #if 0
183         switch (sa->sa_family) {
184         case AF_INET:
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;
191                 break;
192
193         case AF_INET6:
194                 pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
195                 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
196                         flags |= NI_NUMERICHOST;
197                 break;
198         }
199 #endif
200
201         if (host == NULL || hostlen == 0U) {
202                 /*
203                  * What should we do?
204                  */
205         } else if (flags & NI_NUMERICHOST) {
206                 if (lwres_net_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
207                     == NULL)
208                         ERR(ENI_SYSTEM);
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;
214 #if 0
215                         if ((flags & NI_NUMERICSCOPE) == 0) {
216                                 /*
217                                  * Vendors may want to add support for
218                                  * non-numeric scope identifier.
219                                  */
220                                 stringscope = foo;
221                         }
222 #endif
223                         if (stringscope == NULL) {
224                                 snprintf(p, sizeof(numaddr) - (p - numaddr),
225                                     "%%%u",
226                                     ((const struct sockaddr_in6 *)sa)->sin6_scope_id);
227                         } else {
228                                 snprintf(p, sizeof(numaddr) - (p - numaddr),
229                                     "%%%s", stringscope);
230                         }
231                 }
232 #endif
233                 if (strlen(numaddr) + 1 > hostlen)
234                         ERR(ENI_MEMORY);
235                 strcpy(host, numaddr);
236         } else {
237                 switch (family) {
238                 case AF_INET:
239                         lwf = LWRES_ADDRTYPE_V4;
240                         break;
241                 case AF_INET6:
242                         lwf = LWRES_ADDRTYPE_V6;
243                         break;
244                 default:
245                         INSIST(0);
246                 }
247
248                 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
249                 if (n == 0)
250                         (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
251
252                 if (n == 0)
253                         n = lwres_getnamebyaddr(lwrctx, lwf,
254                                                 (lwres_uint16_t)afd->a_addrlen,
255                                                 addr, &by);
256                 if (n == 0) {
257                         if (flags & NI_NOFQDN) {
258                                 p = strchr(by->realname, '.');
259                                 if (p)
260                                         *p = '\0';
261                         }
262                         if ((strlen(by->realname) + 1) > hostlen)
263                                 ERR(ENI_MEMORY);
264                         strcpy(host, by->realname);
265                 } else {
266                         if (flags & NI_NAMEREQD)
267                                 ERR(ENI_NOHOSTNAME);
268                         if (lwres_net_ntop(afd->a_af, addr, numaddr,
269                                            sizeof(numaddr))
270                             == NULL)
271                                 ERR(ENI_NOHOSTNAME);
272                         if ((strlen(numaddr) + 1) > hostlen)
273                                 ERR(ENI_MEMORY);
274                         strcpy(host, numaddr);
275                 }
276         }
277         result = SUCCESS;
278  cleanup:
279         if (by != NULL)
280                 lwres_gnbaresponse_free(lwrctx, &by);
281         if (lwrctx != NULL) {
282                 lwres_conf_clear(lwrctx);
283                 lwres_context_destroy(&lwrctx);
284         }
285         return (result);
286 }