getnameinfo.c revision 1.14 1 /* $NetBSD: getnameinfo.c,v 1.14 2000/04/24 09:27:31 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 * - Return values. There seems to be no standard for return value (RFC2553)
36 * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
37 * - RFC2553 says that we should raise error on short buffer. X/Open says
38 * we need to truncate the result. We obey RFC2553 (and X/Open should be
39 * modified).
40 */
41
42 #include "namespace.h"
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <net/if.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <arpa/nameser.h>
49 #include <netdb.h>
50 #include <resolv.h>
51 #include <string.h>
52 #include <stddef.h>
53
54 #ifdef __weak_alias
55 __weak_alias(getnameinfo,_getnameinfo)
56 #endif
57
58 #define SUCCESS 0
59 #define ANY 0
60 #define YES 1
61 #define NO 0
62
63 static struct afd {
64 int a_af;
65 int a_addrlen;
66 int a_socklen;
67 int a_off;
68 } afdl [] = {
69 #ifdef INET6
70 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
71 offsetof(struct sockaddr_in6, sin6_addr)},
72 #endif
73 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
74 offsetof(struct sockaddr_in, sin_addr)},
75 {0, 0, 0},
76 };
77
78 struct sockinet {
79 u_char si_len;
80 u_char si_family;
81 u_short si_port;
82 };
83
84 #ifdef INET6
85 static char *ip6_sa2str __P((struct sockaddr_in6 *, char *, int));
86 #endif
87
88 #define ENI_NOSOCKET 0
89 #define ENI_NOSERVNAME 1
90 #define ENI_NOHOSTNAME 2
91 #define ENI_MEMORY 3
92 #define ENI_SYSTEM 4
93 #define ENI_FAMILY 5
94 #define ENI_SALEN 6
95
96 int
97 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
98 const struct sockaddr *sa;
99 size_t salen;
100 char *host;
101 size_t hostlen;
102 char *serv;
103 size_t servlen;
104 int flags;
105 {
106 struct afd *afd;
107 struct servent *sp;
108 struct hostent *hp;
109 u_short port;
110 int family, i;
111 char *addr, *p;
112 u_int32_t v4a;
113 int h_error;
114 char numserv[512];
115 char numaddr[512];
116
117 if (sa == NULL)
118 return ENI_NOSOCKET;
119
120 #ifdef BSD4_4
121 if (sa->sa_len != salen)
122 return ENI_SALEN;
123 #endif
124
125 family = sa->sa_family;
126 for (i = 0; afdl[i].a_af; i++)
127 if (afdl[i].a_af == family) {
128 afd = &afdl[i];
129 goto found;
130 }
131 return ENI_FAMILY;
132
133 found:
134 if (salen != afd->a_socklen)
135 return ENI_SALEN;
136
137 port = ((struct sockinet *)sa)->si_port; /* network byte order */
138 addr = (char *)sa + afd->a_off;
139
140 if (serv == NULL || servlen == 0) {
141 /*
142 * do nothing in this case.
143 * in case you are wondering if "&&" is more correct than
144 * "||" here: RFC2553 says that serv == NULL OR servlen == 0
145 * means that the caller does not want the result.
146 */
147 } else {
148 if (flags & NI_NUMERICSERV)
149 sp = NULL;
150 else {
151 sp = getservbyport(port,
152 (flags & NI_DGRAM) ? "udp" : "tcp");
153 }
154 if (sp) {
155 if (strlen(sp->s_name) > servlen)
156 return ENI_MEMORY;
157 strcpy(serv, sp->s_name);
158 } else {
159 snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
160 if (strlen(numserv) > servlen)
161 return ENI_MEMORY;
162 strcpy(serv, numserv);
163 }
164 }
165
166 switch (sa->sa_family) {
167 case AF_INET:
168 v4a = (u_int32_t)
169 ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
170 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
171 flags |= NI_NUMERICHOST;
172 v4a >>= IN_CLASSA_NSHIFT;
173 if (v4a == 0)
174 flags |= NI_NUMERICHOST;
175 break;
176 #ifdef INET6
177 case AF_INET6:
178 {
179 struct sockaddr_in6 *sin6;
180 sin6 = (struct sockaddr_in6 *)sa;
181 switch (sin6->sin6_addr.s6_addr[0]) {
182 case 0x00:
183 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
184 ;
185 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
186 ;
187 else
188 flags |= NI_NUMERICHOST;
189 break;
190 default:
191 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
192 flags |= NI_NUMERICHOST;
193 }
194 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
195 flags |= NI_NUMERICHOST;
196 break;
197 }
198 }
199 break;
200 #endif
201 }
202 if (host == NULL || hostlen == 0) {
203 /*
204 * do nothing in this case.
205 * in case you are wondering if "&&" is more correct than
206 * "||" here: RFC2553 says that host == NULL OR hostlen == 0
207 * means that the caller does not want the result.
208 */
209 } else if (flags & NI_NUMERICHOST) {
210 int numaddrlen;
211
212 /* NUMERICHOST and NAMEREQD conflicts with each other */
213 if (flags & NI_NAMEREQD)
214 return ENI_NOHOSTNAME;
215 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
216 == NULL)
217 return ENI_SYSTEM;
218 numaddrlen = strlen(numaddr);
219 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
220 return ENI_MEMORY;
221 strcpy(host, numaddr);
222 #if defined(INET6) && defined(NI_WITHSCOPEID)
223 if (afd->a_af == AF_INET6 &&
224 (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr) ||
225 IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) &&
226 ((struct sockaddr_in6 *)sa)->sin6_scope_id) {
227 #ifndef ALWAYS_WITHSCOPE
228 if (flags & NI_WITHSCOPEID)
229 #endif /* !ALWAYS_WITHSCOPE */
230 {
231 char scopebuf[MAXHOSTNAMELEN], *s;
232 int scopelen;
233
234 if ((s = ip6_sa2str((struct sockaddr_in6 *)sa,
235 scopebuf, 0)) == NULL)
236 /* XXX what should we do? */
237 strcpy(scopebuf, "0");
238
239 scopelen = strlen(scopebuf);
240 if (scopelen + 1 + numaddrlen + 1 > hostlen)
241 return ENI_MEMORY;
242
243 #if 0
244 /*
245 * construct <scopeid><delim><numeric-addr>
246 */
247 /*
248 * Shift the host string to allocate
249 * space for the scope ID part.
250 */
251 memmove(host + scopelen + 1, host,
252 numaddrlen);
253 /* copy the scope ID and the delimiter */
254 memcpy(host, scopebuf, scopelen);
255 host[scopelen] = SCOPE_DELIMITER;
256 host[scopelen + 1 + numaddrlen] = '\0';
257 #else
258 /*
259 * construct <numeric-addr><delim><scopeid>
260 */
261 memcpy(host + numaddrlen + 1, scopebuf,
262 scopelen);
263 host[numaddrlen] = SCOPE_DELIMITER;
264 host[numaddrlen + 1 + scopelen] = '\0';
265 #endif
266 }
267 }
268 #endif /* INET6 */
269 } else {
270 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
271 h_error = h_errno;
272
273 if (hp) {
274 if (flags & NI_NOFQDN) {
275 p = strchr(hp->h_name, '.');
276 if (p) *p = '\0';
277 }
278 if (strlen(hp->h_name) > hostlen) {
279 return ENI_MEMORY;
280 }
281 strcpy(host, hp->h_name);
282 } else {
283 if (flags & NI_NAMEREQD)
284 return ENI_NOHOSTNAME;
285 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
286 == NULL)
287 return ENI_NOHOSTNAME;
288 if (strlen(numaddr) > hostlen)
289 return ENI_MEMORY;
290 strcpy(host, numaddr);
291 }
292 }
293 return SUCCESS;
294 }
295
296 #ifdef INET6
297 /* ARGSUSED */
298 static char *
299 ip6_sa2str(sa6, buf, flags)
300 struct sockaddr_in6 *sa6;
301 char *buf;
302 int flags;
303 {
304 unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
305 struct in6_addr *a6 = &sa6->sin6_addr;
306
307 #ifdef notyet
308 if (flags & NI_NUMERICSCOPE) {
309 sprintf(buf, "%d", sa6->sin6_scope_id);
310 return(buf);
311 }
312 #endif
313
314 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6))
315 return(if_indextoname(ifindex, buf));
316
317 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
318 return(NULL); /* XXX */
319 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
320 return(NULL); /* XXX */
321
322 return(NULL); /* XXX */
323 }
324 #endif
325