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