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