Libinfo-129.4.tar.gz
[apple/libinfo.git] / gen.subproj / getnameinfo.c
1 /* $FreeBSD: src/lib/libc/net/getnameinfo.c,v 1.5 2000/07/05 05:09:17 itojun Exp $ */
2 /* $KAME: getnameinfo.c,v 1.43 2000/06/12 04:27:03 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 * - Return values. There seems to be no standard for return value (RFC2553)
37 * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
38 * - RFC2553 says that we should raise error on short buffer. X/Open says
39 * we need to truncate the result. We obey RFC2553 (and X/Open should be
40 * modified).
41 * - What is "local" in NI_FQDN?
42 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
43 * - (KAME extension) NI_WITHSCOPEID when called with global address,
44 * and sin6_scope_id filled
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 #define SUCCESS 0
60 #define ANY 0
61 #define YES 1
62 #define NO 0
63
64 static struct afd {
65 int a_af;
66 int a_addrlen;
67 int a_socklen;
68 int a_off;
69 } afdl [] = {
70 #ifdef INET6
71 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
72 offsetof(struct sockaddr_in6, sin6_addr)},
73 #endif
74 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
75 offsetof(struct sockaddr_in, sin_addr)},
76 {0, 0, 0},
77 };
78
79 struct sockinet {
80 u_char si_len;
81 u_char si_family;
82 u_short si_port;
83 };
84
85
86 #ifdef INET6
87 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
88 size_t, int));
89 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
90 #endif
91
92 #define ENI_NOSOCKET EAI_FAIL /*XXX*/
93 #define ENI_NOSERVNAME EAI_NONAME
94 #define ENI_NOHOSTNAME EAI_NONAME
95 #define ENI_MEMORY EAI_MEMORY
96 #define ENI_SYSTEM EAI_SYSTEM
97 #define ENI_FAMILY EAI_FAMILY
98 #define ENI_SALEN EAI_FAMILY
99
100 int
101 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
102 const struct sockaddr *sa;
103 size_t salen;
104 char *host;
105 size_t hostlen;
106 char *serv;
107 size_t servlen;
108 int flags;
109 {
110 struct afd *afd;
111 struct servent *sp;
112 struct hostent *hp;
113 u_short port;
114 int family, i;
115 const char *addr;
116 u_int32_t v4a;
117 char numserv[512];
118 char numaddr[512];
119
120 if (sa == NULL)
121 return ENI_NOSOCKET;
122
123 #ifdef NOTDEF
124 if (sa->sa_len != salen)
125 return ENI_SALEN;
126 #endif
127
128 family = sa->sa_family;
129 for (i = 0; afdl[i].a_af; i++)
130 if (afdl[i].a_af == family) {
131 afd = &afdl[i];
132 goto found;
133 }
134 return ENI_FAMILY;
135
136 found:
137 if (salen != afd->a_socklen)
138 return ENI_SALEN;
139
140 /* network byte order */
141 port = ((const struct sockinet *)sa)->si_port;
142 addr = (const char *)sa + afd->a_off;
143
144 if (serv == NULL || servlen == 0) {
145 /*
146 * do nothing in this case.
147 * in case you are wondering if "&&" is more correct than
148 * "||" here: RFC2553 says that serv == NULL OR servlen == 0
149 * means that the caller does not want the result.
150 */
151 } else {
152 if (flags & NI_NUMERICSERV)
153 sp = NULL;
154 else {
155 sp = getservbyport(port,
156 (flags & NI_DGRAM) ? "udp" : "tcp");
157 }
158 if (sp) {
159 if (strlen(sp->s_name) > servlen)
160 return ENI_MEMORY;
161 strcpy(serv, sp->s_name);
162 } else {
163 snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
164 if (strlen(numserv) > servlen)
165 return ENI_MEMORY;
166 strcpy(serv, numserv);
167 }
168 }
169
170 switch (sa->sa_family) {
171 case AF_INET:
172 v4a = (u_int32_t)
173 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
174 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
175 flags |= NI_NUMERICHOST;
176 v4a >>= IN_CLASSA_NSHIFT;
177 if (v4a == 0)
178 flags |= NI_NUMERICHOST;
179 break;
180 #ifdef INET6
181 case AF_INET6:
182 {
183 const struct sockaddr_in6 *sin6;
184 sin6 = (const struct sockaddr_in6 *)sa;
185 switch (sin6->sin6_addr.s6_addr[0]) {
186 case 0x00:
187 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
188 ;
189 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
190 ;
191 else
192 flags |= NI_NUMERICHOST;
193 break;
194 default:
195 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
196 flags |= NI_NUMERICHOST;
197 }
198 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
199 flags |= NI_NUMERICHOST;
200 break;
201 }
202 }
203 break;
204 #endif
205 }
206 if (host == NULL || hostlen == 0) {
207 /*
208 * do nothing in this case.
209 * in case you are wondering if "&&" is more correct than
210 * "||" here: RFC2553 says that host == NULL OR hostlen == 0
211 * means that the caller does not want the result.
212 */
213 } else if (flags & NI_NUMERICHOST) {
214 int numaddrlen;
215
216 /* NUMERICHOST and NAMEREQD conflicts with each other */
217 if (flags & NI_NAMEREQD)
218 return ENI_NOHOSTNAME;
219
220 switch(afd->a_af) {
221 #ifdef INET6
222 case AF_INET6:
223 {
224 int error;
225
226 if ((error = ip6_parsenumeric(sa, addr, host,
227 hostlen, flags)) != 0)
228 return(error);
229 break;
230 }
231 #endif
232 default:
233 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
234 == NULL)
235 return ENI_SYSTEM;
236 numaddrlen = strlen(numaddr);
237 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
238 return ENI_MEMORY;
239 strcpy(host, numaddr);
240 break;
241 }
242 } else {
243 #if 0
244 int h_error;
245 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
246 #else
247 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
248 #endif
249
250 if (hp) {
251 #if 0
252 /*
253 * commented out, since "for local host" is not
254 * implemented here - see RFC2553 p30
255 */
256 if (flags & NI_NOFQDN) {
257 char *p;
258 p = strchr(hp->h_name, '.');
259 if (p)
260 *p = '\0';
261 }
262 #endif
263 if (strlen(hp->h_name) > hostlen) {
264 #if 0
265 freehostent(hp);
266 #endif
267 return ENI_MEMORY;
268 }
269 strcpy(host, hp->h_name);
270 #if 0
271 freehostent(hp);
272 #endif
273 } else {
274 if (flags & NI_NAMEREQD)
275 return ENI_NOHOSTNAME;
276 switch(afd->a_af) {
277 #ifdef INET6
278 case AF_INET6:
279 {
280 int error;
281
282 if ((error = ip6_parsenumeric(sa, addr, host,
283 hostlen,
284 flags)) != 0)
285 return(error);
286 break;
287 }
288 #endif
289 default:
290 if (inet_ntop(afd->a_af, addr, host,
291 hostlen) == NULL)
292 return ENI_SYSTEM;
293 break;
294 }
295 }
296 }
297 return SUCCESS;
298 }
299
300 #ifdef INET6
301 static int
302 ip6_parsenumeric(sa, addr, host, hostlen, flags)
303 const struct sockaddr *sa;
304 const char *addr;
305 char *host;
306 size_t hostlen;
307 int flags;
308 {
309 int numaddrlen;
310 char numaddr[512];
311
312 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
313 == NULL)
314 return ENI_SYSTEM;
315
316 numaddrlen = strlen(numaddr);
317 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
318 return ENI_MEMORY;
319 strcpy(host, numaddr);
320
321 #ifdef NI_WITHSCOPEID
322 if (
323 #ifdef DONT_OPAQUE_SCOPEID
324 (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr) ||
325 IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) &&
326 #endif
327 ((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
328 #ifndef ALWAYS_WITHSCOPE
329 if (flags & NI_WITHSCOPEID)
330 #endif /* !ALWAYS_WITHSCOPE */
331 {
332 char scopebuf[MAXHOSTNAMELEN];
333 int scopelen;
334
335 /* ip6_sa2str never fails */
336 scopelen = ip6_sa2str((const struct sockaddr_in6 *)sa,
337 scopebuf, sizeof(scopebuf),
338 flags);
339 if (scopelen + 1 + numaddrlen + 1 > hostlen)
340 return ENI_MEMORY;
341 /*
342 * construct <numeric-addr><delim><scopeid>
343 */
344 memcpy(host + numaddrlen + 1, scopebuf,
345 scopelen);
346 host[numaddrlen] = SCOPE_DELIMITER;
347 host[numaddrlen + 1 + scopelen] = '\0';
348 }
349 }
350 #endif /* NI_WITHSCOPEID */
351
352 return 0;
353 }
354
355 /* ARGSUSED */
356 static int
357 ip6_sa2str(sa6, buf, bufsiz, flags)
358 const struct sockaddr_in6 *sa6;
359 char *buf;
360 size_t bufsiz;
361 int flags;
362 {
363 unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
364 const struct in6_addr *a6 = &sa6->sin6_addr;
365
366 #ifdef NI_NUMERICSCOPE
367 if (flags & NI_NUMERICSCOPE) {
368 return(snprintf(buf, bufsiz, "%d", sa6->sin6_scope_id));
369 }
370 #endif
371
372 /* if_indextoname() does not take buffer size. not a good api... */
373 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
374 bufsiz >= IF_NAMESIZE) {
375 char *p = if_indextoname(ifindex, buf);
376 if (p) {
377 return(strlen(p));
378 }
379 }
380
381 /* last resort */
382 return(snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id));
383 }
384 #endif /* INET6 */