blob: f9401092103e5eced3a429b82f23f2116211b0db [file] [log] [blame]
Elliott Hughesd8213bb2013-02-13 09:49:33 -08001/* $NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos Exp $ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002/* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */
3
4/*
5 * Copyright (c) 2000 Ben Harris.
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/*
35 * Issues to be discussed:
36 * - Thread safe-ness must be checked
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). ipngwg rough consensus seems to follow RFC2553.
40 * - What is "local" in NI_FQDN?
41 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
42 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
43 * sin6_scope_id is filled - standardization status?
44 * XXX breaks backward compat for code that expects no scopeid.
45 * beware on merge.
46 */
47
48#include <sys/cdefs.h>
49#if defined(LIBC_SCCS) && !defined(lint)
Elliott Hughesd8213bb2013-02-13 09:49:33 -080050__RCSID("$NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos Exp $");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051#endif /* LIBC_SCCS and not lint */
52
53#include <sys/types.h>
54#include <sys/socket.h>
Elliott Hughesd8213bb2013-02-13 09:49:33 -080055#include <sys/un.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056#include <net/if.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057#include <net/if_ieee1394.h>
58#include <net/if_types.h>
59#include <netinet/in.h>
60#include <arpa/inet.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061#include <assert.h>
62#include <limits.h>
63#include <netdb.h>
Calin Juravle569fb982014-03-04 15:01:29 +000064#include <arpa/nameser.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065#include "resolv_private.h"
Mattias Falk149f7df2011-02-15 08:45:26 +010066#include <sys/system_properties.h>
67#include <stdlib.h>
68#include <unistd.h>
Mattias Falk149f7df2011-02-15 08:45:26 +010069#include <errno.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070#include <stddef.h>
71#include <string.h>
72
73static const struct afd {
74 int a_af;
75 socklen_t a_addrlen;
76 socklen_t a_socklen;
77 int a_off;
78} afdl [] = {
79#ifdef INET6
80 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
81 offsetof(struct sockaddr_in6, sin6_addr)},
82#endif
83 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
84 offsetof(struct sockaddr_in, sin_addr)},
85 {0, 0, 0, 0},
86};
87
88struct sockinet {
89 u_char si_len;
90 u_char si_family;
91 u_short si_port;
92};
93
Elliott Hughesd8213bb2013-02-13 09:49:33 -080094static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
Chad Brubakerc39214e2013-06-20 10:36:56 -070095 socklen_t, char *, socklen_t, int, const char*, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096#ifdef INET6
Elliott Hughesd8213bb2013-02-13 09:49:33 -080097static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
98 socklen_t, int);
99static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100#endif
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800101static int getnameinfo_local(const struct sockaddr *, socklen_t, char *,
102 socklen_t, char *, socklen_t, int);
Selim Gurun06e18312012-02-27 15:58:54 -0800103
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104/*
105 * Top-level getnameinfo() code. Look at the address family, and pick an
106 * appropriate function to call.
107 */
108int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags)
109{
Chad Brubakerc39214e2013-06-20 10:36:56 -0700110 return android_getnameinfoforiface(sa, salen, host, hostlen, serv, servlen, flags, NULL, 0);
Mattias Falkc63e5902011-08-23 14:34:14 +0200111}
112
Chad Brubakerc39214e2013-06-20 10:36:56 -0700113int android_getnameinfoforiface(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags, const char* iface, int mark)
Mattias Falkc63e5902011-08-23 14:34:14 +0200114{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115 switch (sa->sa_family) {
116 case AF_INET:
117 case AF_INET6:
118 return getnameinfo_inet(sa, salen, host, hostlen,
Chad Brubakerc39214e2013-06-20 10:36:56 -0700119 serv, servlen, flags, iface, mark);
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800120 case AF_LOCAL:
121 return getnameinfo_local(sa, salen, host, hostlen,
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000122 serv, servlen, flags);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123 default:
124 return EAI_FAMILY;
125 }
126}
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000127
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800128/*
129 * getnameinfo_local():
130 * Format an local address into a printable format.
131 */
132/* ARGSUSED */
133static int
134getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
135 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
136 int flags __attribute__((unused)))
137{
138 const struct sockaddr_un *sun =
139 (const struct sockaddr_un *)(const void *)sa;
140
141 if (salen < (socklen_t) offsetof(struct sockaddr_un, sun_path)) {
142 return EAI_FAMILY;
143 }
144
145 if (serv != NULL && servlen > 0)
146 serv[0] = '\0';
147
148 if (host && hostlen > 0)
149 strlcpy(host, sun->sun_path,
150 MIN((socklen_t) sizeof(sun->sun_path) + 1, hostlen));
151
152 return 0;
153}
154
Mattias Falk149f7df2011-02-15 08:45:26 +0100155/* On success length of the host name is returned. A return
156 * value of 0 means there's no host name associated with
157 * the address. On failure -1 is returned in which case
158 * normal execution flow shall continue. */
159static int
Chad Brubakerc39214e2013-06-20 10:36:56 -0700160android_gethostbyaddr_proxy(char* nameBuf, size_t nameBufLen, const void *addr, socklen_t addrLen, int addrFamily, const char* iface, int mark)
Mattias Falkc63e5902011-08-23 14:34:14 +0200161{
162 struct hostent *hostResult =
Chad Brubakerc39214e2013-06-20 10:36:56 -0700163 android_gethostbyaddrforiface_proxy(addr, addrLen, addrFamily, iface, mark);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164
Mattias Falkc63e5902011-08-23 14:34:14 +0200165 if (hostResult == NULL) return 0;
Mattias Falk149f7df2011-02-15 08:45:26 +0100166
Mattias Falkc63e5902011-08-23 14:34:14 +0200167 int lengthResult = strlen(hostResult->h_name);
Mattias Falk149f7df2011-02-15 08:45:26 +0100168
Mattias Falkc63e5902011-08-23 14:34:14 +0200169 if (nameBuf) strncpy(nameBuf, hostResult->h_name, nameBufLen);
170 return lengthResult;
Mattias Falk149f7df2011-02-15 08:45:26 +0100171}
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800172
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173/*
174 * getnameinfo_inet():
175 * Format an IPv4 or IPv6 sockaddr into a printable string.
176 */
Mattias Falkc63e5902011-08-23 14:34:14 +0200177static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800178getnameinfo_inet(const struct sockaddr* sa, socklen_t salen,
179 char *host, socklen_t hostlen,
180 char *serv, socklen_t servlen,
Chad Brubakerc39214e2013-06-20 10:36:56 -0700181 int flags, const char* iface, int mark)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182{
183 const struct afd *afd;
184 struct servent *sp;
185 struct hostent *hp;
186 u_short port;
187 int family, i;
188 const char *addr;
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800189 uint32_t v4a;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190 char numserv[512];
191 char numaddr[512];
192
193 /* sa is checked below */
194 /* host may be NULL */
195 /* serv may be NULL */
196
197 if (sa == NULL)
198 return EAI_FAIL;
199
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200 family = sa->sa_family;
201 for (i = 0; afdl[i].a_af; i++)
202 if (afdl[i].a_af == family) {
203 afd = &afdl[i];
204 goto found;
205 }
206 return EAI_FAMILY;
207
208 found:
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800209 // http://b/1889275: callers should be allowed to provide too much
210 // space, but not too little.
211 if (salen < afd->a_socklen) {
212 return EAI_FAMILY;
213 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214
215 /* network byte order */
216 port = ((const struct sockinet *)(const void *)sa)->si_port;
217 addr = (const char *)(const void *)sa + afd->a_off;
218
219 if (serv == NULL || servlen == 0) {
220 /*
221 * do nothing in this case.
222 * in case you are wondering if "&&" is more correct than
223 * "||" here: rfc2553bis-03 says that serv == NULL OR
224 * servlen == 0 means that the caller does not want the result.
225 */
226 } else {
227 if (flags & NI_NUMERICSERV)
228 sp = NULL;
229 else {
230 sp = getservbyport(port,
231 (flags & NI_DGRAM) ? "udp" : "tcp");
232 }
233 if (sp) {
234 if (strlen(sp->s_name) + 1 > (size_t)servlen)
235 return EAI_MEMORY;
236 strlcpy(serv, sp->s_name, servlen);
237 } else {
238 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
239 if (strlen(numserv) + 1 > (size_t)servlen)
240 return EAI_MEMORY;
241 strlcpy(serv, numserv, servlen);
242 }
243 }
244
245 switch (sa->sa_family) {
246 case AF_INET:
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800247 v4a = (uint32_t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248 ntohl(((const struct sockaddr_in *)
249 (const void *)sa)->sin_addr.s_addr);
250 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
251 flags |= NI_NUMERICHOST;
252 v4a >>= IN_CLASSA_NSHIFT;
253 if (v4a == 0)
254 flags |= NI_NUMERICHOST;
255 break;
256#ifdef INET6
257 case AF_INET6:
258 {
259 const struct sockaddr_in6 *sin6;
260 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
261 switch (sin6->sin6_addr.s6_addr[0]) {
262 case 0x00:
263 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
264 ;
265 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
266 ;
267 else
268 flags |= NI_NUMERICHOST;
269 break;
270 default:
271 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
272 flags |= NI_NUMERICHOST;
273 }
274 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
275 flags |= NI_NUMERICHOST;
276 break;
277 }
278 }
279 break;
280#endif
281 }
282 if (host == NULL || hostlen == 0) {
283 /*
284 * do nothing in this case.
285 * in case you are wondering if "&&" is more correct than
286 * "||" here: rfc2553bis-03 says that host == NULL or
287 * hostlen == 0 means that the caller does not want the result.
288 */
289 } else if (flags & NI_NUMERICHOST) {
290 size_t numaddrlen;
291
292 /* NUMERICHOST and NAMEREQD conflicts with each other */
293 if (flags & NI_NAMEREQD)
294 return EAI_NONAME;
295
296 switch(afd->a_af) {
297#ifdef INET6
298 case AF_INET6:
299 {
300 int error;
301
302 if ((error = ip6_parsenumeric(sa, addr, host,
303 hostlen, flags)) != 0)
304 return(error);
305 break;
306 }
307#endif
308 default:
309 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
310 == NULL)
311 return EAI_SYSTEM;
312 numaddrlen = strlen(numaddr);
313 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
314 return EAI_MEMORY;
315 strlcpy(host, numaddr, hostlen);
316 break;
317 }
318 } else {
Mattias Falk149f7df2011-02-15 08:45:26 +0100319 struct hostent android_proxy_hostent;
320 char android_proxy_buf[MAXDNAME];
Mattias Falk149f7df2011-02-15 08:45:26 +0100321
Selim Gurun06e18312012-02-27 15:58:54 -0800322 int hostnamelen = android_gethostbyaddr_proxy(android_proxy_buf,
Chad Brubakerc39214e2013-06-20 10:36:56 -0700323 MAXDNAME, addr, afd->a_addrlen, afd->a_af, iface, mark);
Selim Gurun06e18312012-02-27 15:58:54 -0800324 if (hostnamelen > 0) {
325 hp = &android_proxy_hostent;
326 hp->h_name = android_proxy_buf;
327 } else if (!hostnamelen) {
328 hp = NULL;
Mattias Falk149f7df2011-02-15 08:45:26 +0100329 } else {
Chad Brubakerc39214e2013-06-20 10:36:56 -0700330 hp = android_gethostbyaddrforiface(addr, afd->a_addrlen, afd->a_af,
331 iface, mark);
Mattias Falk149f7df2011-02-15 08:45:26 +0100332 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333
334 if (hp) {
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800335#if 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336 /*
337 * commented out, since "for local host" is not
338 * implemented here - see RFC2553 p30
339 */
340 if (flags & NI_NOFQDN) {
341 char *p;
342 p = strchr(hp->h_name, '.');
343 if (p)
344 *p = '\0';
345 }
346#endif
347 if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
348 return EAI_MEMORY;
349 }
350 strlcpy(host, hp->h_name, hostlen);
351 } else {
352 if (flags & NI_NAMEREQD)
353 return EAI_NONAME;
354 switch(afd->a_af) {
355#ifdef INET6
356 case AF_INET6:
357 {
358 int error;
359
360 if ((error = ip6_parsenumeric(sa, addr, host,
361 hostlen,
362 flags)) != 0)
363 return(error);
364 break;
365 }
366#endif
367 default:
368 if (inet_ntop(afd->a_af, addr, host,
369 hostlen) == NULL)
370 return EAI_SYSTEM;
371 break;
372 }
373 }
374 }
375 return(0);
376}
377
378#ifdef INET6
379static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800380ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
381 socklen_t hostlen, int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800382{
383 size_t numaddrlen;
384 char numaddr[512];
385
386 assert(sa != NULL);
387 assert(addr != NULL);
388 assert(host != NULL);
389
390 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
391 return EAI_SYSTEM;
392
393 numaddrlen = strlen(numaddr);
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700394 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800395 return EAI_OVERFLOW;
396 strlcpy(host, numaddr, hostlen);
397
398 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
399 char zonebuf[MAXHOSTNAMELEN];
400 int zonelen;
401
402 zonelen = ip6_sa2str(
403 (const struct sockaddr_in6 *)(const void *)sa,
404 zonebuf, sizeof(zonebuf), flags);
405 if (zonelen < 0)
406 return EAI_OVERFLOW;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700407 if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800408 return EAI_OVERFLOW;
409 /* construct <numeric-addr><delim><zoneid> */
410 memcpy(host + numaddrlen + 1, zonebuf,
411 (size_t)zonelen);
412 host[numaddrlen] = SCOPE_DELIMITER;
413 host[numaddrlen + 1 + zonelen] = '\0';
414 }
415
416 return 0;
417}
418
419/* ARGSUSED */
420static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800421ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422{
423 unsigned int ifindex;
424 const struct in6_addr *a6;
425 int n;
426
427 assert(sa6 != NULL);
428 assert(buf != NULL);
429
430 ifindex = (unsigned int)sa6->sin6_scope_id;
431 a6 = &sa6->sin6_addr;
432
433#ifdef NI_NUMERICSCOPE
434 if ((flags & NI_NUMERICSCOPE) != 0) {
435 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
436 if (n < 0 || n >= bufsiz)
437 return -1;
438 else
439 return n;
440 }
441#endif
442
443 /* if_indextoname() does not take buffer size. not a good api... */
444 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
445 bufsiz >= IF_NAMESIZE) {
446 char *p = if_indextoname(ifindex, buf);
447 if (p) {
448 return(strlen(p));
449 }
450 }
451
452 /* last resort */
453 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
454 if (n < 0 || (size_t) n >= bufsiz)
455 return -1;
456 else
457 return n;
458}
459#endif /* INET6 */