Merge from vendor branch GCC:
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / getnameinfo.c
1 /*
2  * Issues to be discussed:
3  * - Thread safe-ness must be checked
4  */
5
6 /*
7  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8  * All rights reserved.
9  * 
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *    This product includes software developed by WIDE Project and
21  *    its contributors.
22  * 4. Neither the name of the project nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  * 
26  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38
39 #include <port_before.h>
40
41 #include <sys/types.h>
42 #include <sys/socket.h>
43
44 #include <netinet/in.h>
45 #include <arpa/nameser.h>
46 #include <arpa/inet.h>
47 #include <net/if.h>
48
49 #include <netdb.h>
50 #include <resolv.h>
51 #include <string.h>
52 #include <stddef.h>
53
54 #include <port_after.h>
55
56 /*
57  * Note that a_off will be dynamically adjusted so that to be consistent
58  * with the definition of sockaddr_in{,6}.
59  * The value presented below is just a guess.
60  */
61 static struct afd {
62         int a_af;
63         int a_addrlen;
64         size_t a_socklen;
65         int a_off;
66 } afdl [] = {
67         /* first entry is linked last... */
68         {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
69          offsetof(struct sockaddr_in, sin_addr)},
70         {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
71          offsetof(struct sockaddr_in6, sin6_addr)},
72         {0, 0, 0, 0},
73 };
74
75 struct sockinet {
76 #ifdef HAVE_SA_LEN
77         u_char  si_len;
78 #endif
79         u_char  si_family;
80         u_short si_port;
81 };
82
83 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
84                                  size_t, int));
85 #ifdef HAVE_SIN6_SCOPE_ID
86 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
87 #endif
88
89 int
90 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
91         const struct sockaddr *sa;
92         size_t salen;
93         char *host;
94         size_t hostlen;
95         char *serv;
96         size_t servlen;
97         int flags;
98 {
99         struct afd *afd;
100         struct servent *sp;
101         struct hostent *hp;
102         u_short port;
103 #ifdef HAVE_SA_LEN
104         size_t len;
105 #endif
106         int family, i;
107         const char *addr;
108         char *p;
109         char numserv[512];
110         char numaddr[512];
111         const struct sockaddr_in6 *sin6;
112
113         if (sa == NULL)
114                 return EAI_FAIL;
115
116 #ifdef HAVE_SA_LEN
117         len = sa->sa_len;
118         if (len != salen) return EAI_FAIL;
119 #endif
120
121         family = sa->sa_family;
122         for (i = 0; afdl[i].a_af; i++)
123                 if (afdl[i].a_af == family) {
124                         afd = &afdl[i];
125                         goto found;
126                 }
127         return EAI_FAMILY;
128
129  found:
130         if (salen != afd->a_socklen) return EAI_FAIL;
131
132         port = ((const struct sockinet *)sa)->si_port; /* network byte order */
133         addr = (const char *)sa + afd->a_off;
134
135         if (serv == NULL || servlen == 0U) {
136                 /*
137                  * rfc2553bis says that serv == NULL or servlen == 0 means that
138                  * the caller does not want the result.
139                  */
140         } else if (flags & NI_NUMERICSERV) {
141                 sprintf(numserv, "%d", ntohs(port));
142                 if (strlen(numserv) > servlen)
143                         return EAI_MEMORY;
144                 strcpy(serv, numserv);
145         } else {
146                 sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
147                 if (sp) {
148                         if (strlen(sp->s_name) + 1 > servlen)
149                                 return EAI_MEMORY;
150                         strcpy(serv, sp->s_name);
151                 } else
152                         return EAI_NONAME;
153         }
154
155         switch (sa->sa_family) {
156         case AF_INET:
157                 if (ntohl(*(const u_int32_t *)addr) >> IN_CLASSA_NSHIFT == 0)
158                         flags |= NI_NUMERICHOST;                        
159                 break;
160         case AF_INET6:
161                 sin6 = (const struct sockaddr_in6 *)sa;
162                 switch (sin6->sin6_addr.s6_addr[0]) {
163                 case 0x00:
164                         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
165                                 ;
166                         else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
167                                 ;
168                         else
169                                 flags |= NI_NUMERICHOST;
170                         break;
171                 default:
172                         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
173                                 flags |= NI_NUMERICHOST;
174                         else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
175                                 flags |= NI_NUMERICHOST;
176                         break;
177                 }
178                 break;
179         }
180         if (host == NULL || hostlen == 0U) {
181                 /*
182                  * rfc2553bis says that host == NULL or hostlen == 0 means that
183                  * the caller does not want the result.
184                  */
185         } else if (flags & NI_NUMERICHOST) {
186                 goto numeric;
187         } else {
188                 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
189
190                 if (hp) {
191                         if (flags & NI_NOFQDN) {
192                                 p = strchr(hp->h_name, '.');
193                                 if (p) *p = '\0';
194                         }
195                         if (strlen(hp->h_name) + 1 > hostlen)
196                                 return EAI_MEMORY;
197                         strcpy(host, hp->h_name);
198                 } else {
199                         if (flags & NI_NAMEREQD)
200                                 return EAI_NONAME;
201                   numeric:
202                         switch(afd->a_af) {
203                         case AF_INET6:
204                         {
205                                 int error;
206
207                                 if ((error = ip6_parsenumeric(sa, addr, host,
208                                                               hostlen,
209                                                               flags)) != 0)
210                                         return(error);
211                                 break;
212                         }
213
214                         default:
215                                 if (inet_ntop(afd->a_af, addr, numaddr,
216                                               sizeof(numaddr)) == NULL)
217                                         return EAI_NONAME;
218                                 if (strlen(numaddr) + 1 > hostlen)
219                                         return EAI_MEMORY;
220                                 strcpy(host, numaddr);
221                         }
222                 }
223         }
224         return(0);
225 }
226
227 static int
228 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
229                  size_t hostlen, int flags)
230 {
231         size_t numaddrlen;
232         char numaddr[512];
233
234 #ifndef HAVE_SIN6_SCOPE_ID
235         UNUSED(sa);
236         UNUSED(flags);
237 #endif
238
239         if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
240             == NULL)
241                 return EAI_SYSTEM;
242
243         numaddrlen = strlen(numaddr);
244         if (numaddrlen + 1 > hostlen) /* don't forget terminator */
245                 return EAI_MEMORY;
246         strcpy(host, numaddr);
247
248 #ifdef HAVE_SIN6_SCOPE_ID
249         if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
250                 char scopebuf[MAXHOSTNAMELEN]; /* XXX */
251                 int scopelen;
252
253                 /* ip6_sa2str never fails */
254                 scopelen = ip6_sa2str((const struct sockaddr_in6 *)sa,
255                                       scopebuf, sizeof(scopebuf), flags);
256
257                 if (scopelen + 1 + numaddrlen + 1 > hostlen)
258                         return EAI_MEMORY;
259
260                 /* construct <numeric-addr><delim><scopeid> */
261                 memcpy(host + numaddrlen + 1, scopebuf,
262                        scopelen);
263                 host[numaddrlen] = SCOPE_DELIMITER;
264                 host[numaddrlen + 1 + scopelen] = '\0';
265         }
266 #endif
267
268         return 0;
269 }
270
271 #ifdef HAVE_SIN6_SCOPE_ID
272 /* ARGSUSED */
273 static int
274 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf,
275            size_t bufsiz, int flags)
276 {
277 #ifdef USE_IFNAMELINKID
278         unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
279         const struct in6_addr *a6 = &sa6->sin6_addr;
280 #endif
281         char tmp[64];
282
283 #ifdef NI_NUMERICSCOPE
284         if (flags & NI_NUMERICSCOPE) {
285                 sprintf(tmp, "%u", sa6->sin6_scope_id);
286                 if (bufsiz != 0U) {
287                         strncpy(buf, tmp, bufsiz - 1);
288                         buf[bufsiz - 1] = '\0';
289                 }
290                 return(strlen(tmp));
291         }
292 #endif
293
294 #ifdef USE_IFNAMELINKID
295         /*
296          * For a link-local address, convert the index to an interface
297          * name, assuming a one-to-one mapping between links and interfaces.
298          * Note, however, that this assumption is stronger than the
299          * specification of the scoped address architecture;  the
300          * specficication says that more than one interfaces can belong to
301          * a single link.
302          */
303
304         /* if_indextoname() does not take buffer size.  not a good api... */
305         if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
306             bufsiz >= IF_NAMESIZE) {
307                 char *p = if_indextoname(ifindex, buf);
308                 if (p) {
309                         return(strlen(p));
310                 }
311         }
312 #endif
313
314         /* last resort */
315         sprintf(tmp, "%u", sa6->sin6_scope_id);
316         if (bufsiz != 0U) {
317                 strncpy(buf, tmp, bufsiz - 1);
318                 buf[bufsiz - 1] = '\0';
319         }
320         return(strlen(tmp));
321 }
322 #endif