Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libc / net / getnameinfo.c
1 /*      $FreeBSD: src/lib/libc/net/getnameinfo.c,v 1.4.2.5 2002/07/31 10:11:09 ume Exp $        */
2 /*      $KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $    */
3
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Issues to be discussed:
35  * - Thread safe-ness must be checked
36  * - RFC2553 says that we should raise error on short buffer.  X/Open says
37  *   we need to truncate the result.  We obey RFC2553 (and X/Open should be
38  *   modified).  ipngwg rough consensus seems to follow RFC2553.
39  * - What is "local" in NI_FQDN?
40  * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
41  * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
42  *   sin6_scope_id is filled - standardization status?
43  *   XXX breaks backward compat for code that expects no scopeid.
44  *   beware on merge.
45  */
46
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <net/if.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <arpa/nameser.h>
53 #include <netdb.h>
54 #include <resolv.h>
55 #include <string.h>
56 #include <stddef.h>
57 #include <errno.h>
58
59 static const struct afd {
60         int a_af;
61         int a_addrlen;
62         int a_socklen;
63         int a_off;
64 } afdl [] = {
65 #ifdef INET6
66         {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
67                 offsetof(struct sockaddr_in6, sin6_addr)},
68 #endif
69         {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
70                 offsetof(struct sockaddr_in, sin_addr)},
71         {0, 0, 0},
72 };
73
74 struct sockinet {
75         u_char  si_len;
76         u_char  si_family;
77         u_short si_port;
78 };
79
80 #ifdef INET6
81 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
82     size_t, int));
83 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
84 #endif
85
86 int
87 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
88         const struct sockaddr *sa;
89         socklen_t salen;
90         char *host;
91         size_t hostlen;
92         char *serv;
93         size_t servlen;
94         int flags;
95 {
96         const struct afd *afd;
97         struct servent *sp;
98         struct hostent *hp;
99         u_short port;
100         int family, i;
101         const char *addr;
102         u_int32_t v4a;
103         int h_error;
104         char numserv[512];
105         char numaddr[512];
106
107         if (sa == NULL)
108                 return EAI_FAIL;
109
110         if (sa->sa_len != salen)
111                 return EAI_FAIL;
112
113         family = sa->sa_family;
114         for (i = 0; afdl[i].a_af; i++)
115                 if (afdl[i].a_af == family) {
116                         afd = &afdl[i];
117                         goto found;
118                 }
119         return EAI_FAMILY;
120
121  found:
122         if (salen != afd->a_socklen)
123                 return EAI_FAIL;
124
125         /* network byte order */
126         port = ((const struct sockinet *)sa)->si_port;
127         addr = (const char *)sa + afd->a_off;
128
129         if (serv == NULL || servlen == 0) {
130                 /*
131                  * do nothing in this case.
132                  * in case you are wondering if "&&" is more correct than
133                  * "||" here: rfc2553bis-03 says that serv == NULL OR
134                  * servlen == 0 means that the caller does not want the result.
135                  */
136         } else {
137                 if (flags & NI_NUMERICSERV)
138                         sp = NULL;
139                 else {
140                         sp = getservbyport(port,
141                                 (flags & NI_DGRAM) ? "udp" : "tcp");
142                 }
143                 if (sp) {
144                         if (strlen(sp->s_name) + 1 > servlen)
145                                 return EAI_MEMORY;
146                         strlcpy(serv, sp->s_name, servlen);
147                 } else {
148                         snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
149                         if (strlen(numserv) + 1 > servlen)
150                                 return EAI_MEMORY;
151                         strlcpy(serv, numserv, servlen);
152                 }
153         }
154
155         switch (sa->sa_family) {
156         case AF_INET:
157                 v4a = (u_int32_t)
158                     ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
159                 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
160                         flags |= NI_NUMERICHOST;
161                 v4a >>= IN_CLASSA_NSHIFT;
162                 if (v4a == 0)
163                         flags |= NI_NUMERICHOST;                        
164                 break;
165 #ifdef INET6
166         case AF_INET6:
167             {
168                 const struct sockaddr_in6 *sin6;
169                 sin6 = (const struct sockaddr_in6 *)sa;
170                 switch (sin6->sin6_addr.s6_addr[0]) {
171                 case 0x00:
172                         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
173                                 ;
174                         else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
175                                 ;
176                         else
177                                 flags |= NI_NUMERICHOST;
178                         break;
179                 default:
180                         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
181                                 flags |= NI_NUMERICHOST;
182                         }
183                         else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
184                                 flags |= NI_NUMERICHOST;
185                         break;
186                 }
187             }
188                 break;
189 #endif
190         }
191         if (host == NULL || hostlen == 0) {
192                 /*
193                  * do nothing in this case.
194                  * in case you are wondering if "&&" is more correct than
195                  * "||" here: rfc2553bis-03 says that host == NULL or
196                  * hostlen == 0 means that the caller does not want the result.
197                  */
198         } else if (flags & NI_NUMERICHOST) {
199                 int numaddrlen;
200
201                 /* NUMERICHOST and NAMEREQD conflicts with each other */
202                 if (flags & NI_NAMEREQD)
203                         return EAI_NONAME;
204
205                 switch(afd->a_af) {
206 #ifdef INET6
207                 case AF_INET6:
208                 {
209                         int error;
210
211                         if ((error = ip6_parsenumeric(sa, addr, host,
212                                                       hostlen, flags)) != 0)
213                                 return(error);
214                         break;
215                 }
216 #endif
217                 default:
218                         if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
219                             == NULL)
220                                 return EAI_SYSTEM;
221                         numaddrlen = strlen(numaddr);
222                         if (numaddrlen + 1 > hostlen) /* don't forget terminator */
223                                 return EAI_MEMORY;
224                         strlcpy(host, numaddr, hostlen);
225                         break;
226                 }
227         } else {
228                 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
229
230                 if (hp) {
231 #if 0
232                         /*
233                          * commented out, since "for local host" is not
234                          * implemented here - see RFC2553 p30
235                          */
236                         if (flags & NI_NOFQDN) {
237                                 char *p;
238                                 p = strchr(hp->h_name, '.');
239                                 if (p)
240                                         *p = '\0';
241                         }
242 #endif
243                         if (strlen(hp->h_name) + 1 > hostlen) {
244                                 freehostent(hp);
245                                 return EAI_MEMORY;
246                         }
247                         strlcpy(host, hp->h_name, hostlen);
248                         freehostent(hp);
249                 } else {
250                         if (flags & NI_NAMEREQD)
251                                 return EAI_NONAME;
252                         switch(afd->a_af) {
253 #ifdef INET6
254                         case AF_INET6:
255                         {
256                                 int error;
257
258                                 if ((error = ip6_parsenumeric(sa, addr, host,
259                                                               hostlen,
260                                                               flags)) != 0)
261                                         return(error);
262                                 break;
263                         }
264 #endif
265                         default:
266                                 if (inet_ntop(afd->a_af, addr, host,
267                                     hostlen) == NULL)
268                                         return EAI_SYSTEM;
269                                 break;
270                         }
271                 }
272         }
273         return(0);
274 }
275
276 #ifdef INET6
277 static int
278 ip6_parsenumeric(sa, addr, host, hostlen, flags)
279         const struct sockaddr *sa;
280         const char *addr;
281         char *host;
282         size_t hostlen;
283         int flags;
284 {
285         int numaddrlen;
286         char numaddr[512];
287
288         if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
289                 return EAI_SYSTEM;
290
291         numaddrlen = strlen(numaddr);
292         if (numaddrlen + 1 > hostlen) /* don't forget terminator */
293                 return EAI_MEMORY;
294         strlcpy(host, numaddr, hostlen);
295
296         if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
297                 char zonebuf[MAXHOSTNAMELEN];
298                 int zonelen;
299
300                 zonelen = ip6_sa2str(
301                     (const struct sockaddr_in6 *)(const void *)sa,
302                     zonebuf, sizeof(zonebuf), flags);
303                 if (zonelen < 0)
304                         return EAI_MEMORY;
305                 if (zonelen + 1 + numaddrlen + 1 > hostlen)
306                         return EAI_MEMORY;
307
308                 /* construct <numeric-addr><delim><zoneid> */
309                 memcpy(host + numaddrlen + 1, zonebuf,
310                     (size_t)zonelen);
311                 host[numaddrlen] = SCOPE_DELIMITER;
312                 host[numaddrlen + 1 + zonelen] = '\0';
313         }
314
315         return 0;
316 }
317
318 /* ARGSUSED */
319 static int
320 ip6_sa2str(sa6, buf, bufsiz, flags)
321         const struct sockaddr_in6 *sa6;
322         char *buf;
323         size_t bufsiz;
324         int flags;
325 {
326         unsigned int ifindex;
327         const struct in6_addr *a6;
328         int n;
329
330         ifindex = (unsigned int)sa6->sin6_scope_id;
331         a6 = &sa6->sin6_addr;
332
333 #ifdef NI_NUMERICSCOPE
334         if ((flags & NI_NUMERICSCOPE) != 0) {
335                 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
336                 if (n < 0 || n >= bufsiz)
337                         return -1;
338                 else
339                         return n;
340         }
341 #endif
342
343         /* if_indextoname() does not take buffer size.  not a good api... */
344         if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
345              IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
346                 char *p = if_indextoname(ifindex, buf);
347                 if (p) {
348                         return(strlen(p));
349                 }
350         }
351
352         /* last resort */
353         n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
354         if (n < 0 || n >= bufsiz)
355                 return -1;
356         else
357                 return n;
358 }
359 #endif /* INET6 */