getnameinfo.c revision 1.14 1 1.14 itojun /* $NetBSD: getnameinfo.c,v 1.14 2000/04/24 09:27:31 itojun Exp $ */
2 1.2 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.1 itojun * All rights reserved.
6 1.1 itojun *
7 1.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.1 itojun * modification, are permitted provided that the following conditions
9 1.1 itojun * are met:
10 1.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.1 itojun * documentation and/or other materials provided with the distribution.
15 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.1 itojun * may be used to endorse or promote products derived from this software
17 1.1 itojun * without specific prior written permission.
18 1.1 itojun *
19 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 itojun * SUCH DAMAGE.
30 1.1 itojun */
31 1.1 itojun
32 1.1 itojun /*
33 1.1 itojun * Issues to be discussed:
34 1.1 itojun * - Thread safe-ness must be checked
35 1.5 itojun * - Return values. There seems to be no standard for return value (RFC2553)
36 1.1 itojun * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
37 1.5 itojun * - RFC2553 says that we should raise error on short buffer. X/Open says
38 1.5 itojun * we need to truncate the result. We obey RFC2553 (and X/Open should be
39 1.5 itojun * modified).
40 1.1 itojun */
41 1.1 itojun
42 1.14 itojun #include "namespace.h"
43 1.1 itojun #include <sys/types.h>
44 1.1 itojun #include <sys/socket.h>
45 1.5 itojun #include <net/if.h>
46 1.1 itojun #include <netinet/in.h>
47 1.1 itojun #include <arpa/inet.h>
48 1.1 itojun #include <arpa/nameser.h>
49 1.1 itojun #include <netdb.h>
50 1.1 itojun #include <resolv.h>
51 1.1 itojun #include <string.h>
52 1.1 itojun #include <stddef.h>
53 1.14 itojun
54 1.14 itojun #ifdef __weak_alias
55 1.14 itojun __weak_alias(getnameinfo,_getnameinfo)
56 1.14 itojun #endif
57 1.1 itojun
58 1.1 itojun #define SUCCESS 0
59 1.1 itojun #define ANY 0
60 1.1 itojun #define YES 1
61 1.1 itojun #define NO 0
62 1.1 itojun
63 1.1 itojun static struct afd {
64 1.1 itojun int a_af;
65 1.1 itojun int a_addrlen;
66 1.1 itojun int a_socklen;
67 1.1 itojun int a_off;
68 1.1 itojun } afdl [] = {
69 1.1 itojun #ifdef INET6
70 1.1 itojun {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
71 1.1 itojun offsetof(struct sockaddr_in6, sin6_addr)},
72 1.1 itojun #endif
73 1.1 itojun {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
74 1.1 itojun offsetof(struct sockaddr_in, sin_addr)},
75 1.1 itojun {0, 0, 0},
76 1.1 itojun };
77 1.1 itojun
78 1.1 itojun struct sockinet {
79 1.1 itojun u_char si_len;
80 1.1 itojun u_char si_family;
81 1.1 itojun u_short si_port;
82 1.1 itojun };
83 1.1 itojun
84 1.10 itojun #ifdef INET6
85 1.10 itojun static char *ip6_sa2str __P((struct sockaddr_in6 *, char *, int));
86 1.10 itojun #endif
87 1.10 itojun
88 1.1 itojun #define ENI_NOSOCKET 0
89 1.1 itojun #define ENI_NOSERVNAME 1
90 1.1 itojun #define ENI_NOHOSTNAME 2
91 1.1 itojun #define ENI_MEMORY 3
92 1.1 itojun #define ENI_SYSTEM 4
93 1.1 itojun #define ENI_FAMILY 5
94 1.1 itojun #define ENI_SALEN 6
95 1.1 itojun
96 1.1 itojun int
97 1.1 itojun getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
98 1.1 itojun const struct sockaddr *sa;
99 1.1 itojun size_t salen;
100 1.1 itojun char *host;
101 1.1 itojun size_t hostlen;
102 1.1 itojun char *serv;
103 1.1 itojun size_t servlen;
104 1.1 itojun int flags;
105 1.1 itojun {
106 1.1 itojun struct afd *afd;
107 1.1 itojun struct servent *sp;
108 1.1 itojun struct hostent *hp;
109 1.1 itojun u_short port;
110 1.5 itojun int family, i;
111 1.10 itojun char *addr, *p;
112 1.5 itojun u_int32_t v4a;
113 1.1 itojun int h_error;
114 1.1 itojun char numserv[512];
115 1.1 itojun char numaddr[512];
116 1.1 itojun
117 1.1 itojun if (sa == NULL)
118 1.1 itojun return ENI_NOSOCKET;
119 1.1 itojun
120 1.13 christos #ifdef BSD4_4
121 1.5 itojun if (sa->sa_len != salen)
122 1.5 itojun return ENI_SALEN;
123 1.13 christos #endif
124 1.1 itojun
125 1.1 itojun family = sa->sa_family;
126 1.1 itojun for (i = 0; afdl[i].a_af; i++)
127 1.1 itojun if (afdl[i].a_af == family) {
128 1.1 itojun afd = &afdl[i];
129 1.1 itojun goto found;
130 1.1 itojun }
131 1.1 itojun return ENI_FAMILY;
132 1.1 itojun
133 1.1 itojun found:
134 1.5 itojun if (salen != afd->a_socklen)
135 1.5 itojun return ENI_SALEN;
136 1.1 itojun
137 1.10 itojun port = ((struct sockinet *)sa)->si_port; /* network byte order */
138 1.10 itojun addr = (char *)sa + afd->a_off;
139 1.1 itojun
140 1.1 itojun if (serv == NULL || servlen == 0) {
141 1.5 itojun /*
142 1.5 itojun * do nothing in this case.
143 1.5 itojun * in case you are wondering if "&&" is more correct than
144 1.5 itojun * "||" here: RFC2553 says that serv == NULL OR servlen == 0
145 1.5 itojun * means that the caller does not want the result.
146 1.5 itojun */
147 1.1 itojun } else {
148 1.5 itojun if (flags & NI_NUMERICSERV)
149 1.5 itojun sp = NULL;
150 1.5 itojun else {
151 1.5 itojun sp = getservbyport(port,
152 1.5 itojun (flags & NI_DGRAM) ? "udp" : "tcp");
153 1.5 itojun }
154 1.1 itojun if (sp) {
155 1.1 itojun if (strlen(sp->s_name) > servlen)
156 1.1 itojun return ENI_MEMORY;
157 1.1 itojun strcpy(serv, sp->s_name);
158 1.5 itojun } else {
159 1.5 itojun snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
160 1.5 itojun if (strlen(numserv) > servlen)
161 1.5 itojun return ENI_MEMORY;
162 1.5 itojun strcpy(serv, numserv);
163 1.5 itojun }
164 1.1 itojun }
165 1.1 itojun
166 1.1 itojun switch (sa->sa_family) {
167 1.1 itojun case AF_INET:
168 1.5 itojun v4a = (u_int32_t)
169 1.10 itojun ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
170 1.1 itojun if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
171 1.1 itojun flags |= NI_NUMERICHOST;
172 1.1 itojun v4a >>= IN_CLASSA_NSHIFT;
173 1.7 itojun if (v4a == 0)
174 1.1 itojun flags |= NI_NUMERICHOST;
175 1.1 itojun break;
176 1.1 itojun #ifdef INET6
177 1.1 itojun case AF_INET6:
178 1.3 itojun {
179 1.10 itojun struct sockaddr_in6 *sin6;
180 1.10 itojun sin6 = (struct sockaddr_in6 *)sa;
181 1.5 itojun switch (sin6->sin6_addr.s6_addr[0]) {
182 1.3 itojun case 0x00:
183 1.3 itojun if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
184 1.3 itojun ;
185 1.3 itojun else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
186 1.3 itojun ;
187 1.3 itojun else
188 1.3 itojun flags |= NI_NUMERICHOST;
189 1.3 itojun break;
190 1.3 itojun default:
191 1.5 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
192 1.3 itojun flags |= NI_NUMERICHOST;
193 1.5 itojun }
194 1.3 itojun else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
195 1.3 itojun flags |= NI_NUMERICHOST;
196 1.3 itojun break;
197 1.3 itojun }
198 1.3 itojun }
199 1.1 itojun break;
200 1.1 itojun #endif
201 1.1 itojun }
202 1.1 itojun if (host == NULL || hostlen == 0) {
203 1.5 itojun /*
204 1.5 itojun * do nothing in this case.
205 1.5 itojun * in case you are wondering if "&&" is more correct than
206 1.5 itojun * "||" here: RFC2553 says that host == NULL OR hostlen == 0
207 1.5 itojun * means that the caller does not want the result.
208 1.5 itojun */
209 1.1 itojun } else if (flags & NI_NUMERICHOST) {
210 1.10 itojun int numaddrlen;
211 1.10 itojun
212 1.3 itojun /* NUMERICHOST and NAMEREQD conflicts with each other */
213 1.3 itojun if (flags & NI_NAMEREQD)
214 1.3 itojun return ENI_NOHOSTNAME;
215 1.1 itojun if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
216 1.1 itojun == NULL)
217 1.1 itojun return ENI_SYSTEM;
218 1.10 itojun numaddrlen = strlen(numaddr);
219 1.10 itojun if (numaddrlen + 1 > hostlen) /* don't forget terminator */
220 1.1 itojun return ENI_MEMORY;
221 1.1 itojun strcpy(host, numaddr);
222 1.5 itojun #if defined(INET6) && defined(NI_WITHSCOPEID)
223 1.5 itojun if (afd->a_af == AF_INET6 &&
224 1.10 itojun (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr) ||
225 1.10 itojun IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) &&
226 1.10 itojun ((struct sockaddr_in6 *)sa)->sin6_scope_id) {
227 1.5 itojun #ifndef ALWAYS_WITHSCOPE
228 1.5 itojun if (flags & NI_WITHSCOPEID)
229 1.5 itojun #endif /* !ALWAYS_WITHSCOPE */
230 1.5 itojun {
231 1.10 itojun char scopebuf[MAXHOSTNAMELEN], *s;
232 1.10 itojun int scopelen;
233 1.5 itojun
234 1.10 itojun if ((s = ip6_sa2str((struct sockaddr_in6 *)sa,
235 1.10 itojun scopebuf, 0)) == NULL)
236 1.5 itojun /* XXX what should we do? */
237 1.10 itojun strcpy(scopebuf, "0");
238 1.10 itojun
239 1.10 itojun scopelen = strlen(scopebuf);
240 1.10 itojun if (scopelen + 1 + numaddrlen + 1 > hostlen)
241 1.10 itojun return ENI_MEMORY;
242 1.10 itojun
243 1.12 itojun #if 0
244 1.11 itojun /*
245 1.11 itojun * construct <scopeid><delim><numeric-addr>
246 1.11 itojun */
247 1.10 itojun /*
248 1.10 itojun * Shift the host string to allocate
249 1.10 itojun * space for the scope ID part.
250 1.10 itojun */
251 1.10 itojun memmove(host + scopelen + 1, host,
252 1.10 itojun numaddrlen);
253 1.10 itojun /* copy the scope ID and the delimiter */
254 1.10 itojun memcpy(host, scopebuf, scopelen);
255 1.10 itojun host[scopelen] = SCOPE_DELIMITER;
256 1.10 itojun host[scopelen + 1 + numaddrlen] = '\0';
257 1.11 itojun #else
258 1.11 itojun /*
259 1.11 itojun * construct <numeric-addr><delim><scopeid>
260 1.11 itojun */
261 1.11 itojun memcpy(host + numaddrlen + 1, scopebuf,
262 1.11 itojun scopelen);
263 1.11 itojun host[numaddrlen] = SCOPE_DELIMITER;
264 1.11 itojun host[numaddrlen + 1 + scopelen] = '\0';
265 1.11 itojun #endif
266 1.5 itojun }
267 1.5 itojun }
268 1.5 itojun #endif /* INET6 */
269 1.1 itojun } else {
270 1.1 itojun hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
271 1.10 itojun h_error = h_errno;
272 1.1 itojun
273 1.1 itojun if (hp) {
274 1.1 itojun if (flags & NI_NOFQDN) {
275 1.1 itojun p = strchr(hp->h_name, '.');
276 1.1 itojun if (p) *p = '\0';
277 1.1 itojun }
278 1.1 itojun if (strlen(hp->h_name) > hostlen) {
279 1.1 itojun return ENI_MEMORY;
280 1.1 itojun }
281 1.1 itojun strcpy(host, hp->h_name);
282 1.1 itojun } else {
283 1.1 itojun if (flags & NI_NAMEREQD)
284 1.1 itojun return ENI_NOHOSTNAME;
285 1.1 itojun if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
286 1.1 itojun == NULL)
287 1.1 itojun return ENI_NOHOSTNAME;
288 1.1 itojun if (strlen(numaddr) > hostlen)
289 1.1 itojun return ENI_MEMORY;
290 1.1 itojun strcpy(host, numaddr);
291 1.1 itojun }
292 1.1 itojun }
293 1.1 itojun return SUCCESS;
294 1.1 itojun }
295 1.10 itojun
296 1.10 itojun #ifdef INET6
297 1.10 itojun /* ARGSUSED */
298 1.10 itojun static char *
299 1.10 itojun ip6_sa2str(sa6, buf, flags)
300 1.10 itojun struct sockaddr_in6 *sa6;
301 1.10 itojun char *buf;
302 1.10 itojun int flags;
303 1.10 itojun {
304 1.10 itojun unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
305 1.10 itojun struct in6_addr *a6 = &sa6->sin6_addr;
306 1.10 itojun
307 1.10 itojun #ifdef notyet
308 1.10 itojun if (flags & NI_NUMERICSCOPE) {
309 1.10 itojun sprintf(buf, "%d", sa6->sin6_scope_id);
310 1.10 itojun return(buf);
311 1.10 itojun }
312 1.10 itojun #endif
313 1.10 itojun
314 1.10 itojun if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6))
315 1.10 itojun return(if_indextoname(ifindex, buf));
316 1.10 itojun
317 1.10 itojun if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
318 1.10 itojun return(NULL); /* XXX */
319 1.10 itojun if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
320 1.10 itojun return(NULL); /* XXX */
321 1.10 itojun
322 1.10 itojun return(NULL); /* XXX */
323 1.10 itojun }
324 1.10 itojun #endif
325