blob: 63a347e5827aae66e6b1e5b4b7295baf911966ae [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 <netinet/in.h>
58#include <arpa/inet.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059#include <assert.h>
60#include <limits.h>
61#include <netdb.h>
Calin Juravle569fb982014-03-04 15:01:29 +000062#include <arpa/nameser.h>
Szymon Jakubczakea9bf672014-02-14 17:07:23 -050063#include "resolv_netid.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064#include "resolv_private.h"
Mattias Falk149f7df2011-02-15 08:45:26 +010065#include <sys/system_properties.h>
66#include <stdlib.h>
67#include <unistd.h>
Mattias Falk149f7df2011-02-15 08:45:26 +010068#include <errno.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069#include <stddef.h>
70#include <string.h>
71
72static const struct afd {
73 int a_af;
74 socklen_t a_addrlen;
75 socklen_t a_socklen;
76 int a_off;
77} afdl [] = {
78#ifdef INET6
79 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
80 offsetof(struct sockaddr_in6, sin6_addr)},
81#endif
82 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
83 offsetof(struct sockaddr_in, sin_addr)},
84 {0, 0, 0, 0},
85};
86
87struct sockinet {
88 u_char si_len;
89 u_char si_family;
90 u_short si_port;
91};
92
Elliott Hughesd8213bb2013-02-13 09:49:33 -080093static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
Szymon Jakubczakea9bf672014-02-14 17:07:23 -050094 socklen_t, char *, socklen_t, int, unsigned, unsigned);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095#ifdef INET6
Elliott Hughesd8213bb2013-02-13 09:49:33 -080096static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
97 socklen_t, int);
98static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099#endif
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800100static int getnameinfo_local(const struct sockaddr *, socklen_t, char *,
101 socklen_t, char *, socklen_t, int);
Selim Gurun06e18312012-02-27 15:58:54 -0800102
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103/*
104 * Top-level getnameinfo() code. Look at the address family, and pick an
105 * appropriate function to call.
106 */
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500107int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen,
108 char* serv, size_t servlen, int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109{
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500110 return android_getnameinfofornet(sa, salen, host, hostlen, serv, servlen, flags,
111 NETID_UNSET, MARK_UNSET);
Mattias Falkc63e5902011-08-23 14:34:14 +0200112}
113
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500114int android_getnameinfofornet(const struct sockaddr* sa, socklen_t salen, char* host,
115 size_t hostlen, char* serv, size_t servlen, int flags, unsigned netid,
116 unsigned mark)
Mattias Falkc63e5902011-08-23 14:34:14 +0200117{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118 switch (sa->sa_family) {
119 case AF_INET:
120 case AF_INET6:
121 return getnameinfo_inet(sa, salen, host, hostlen,
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500122 serv, servlen, flags, netid, mark);
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800123 case AF_LOCAL:
124 return getnameinfo_local(sa, salen, host, hostlen,
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000125 serv, servlen, flags);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126 default:
127 return EAI_FAMILY;
128 }
129}
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000130
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800131/*
132 * getnameinfo_local():
133 * Format an local address into a printable format.
134 */
135/* ARGSUSED */
136static int
137getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
138 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
139 int flags __attribute__((unused)))
140{
141 const struct sockaddr_un *sun =
142 (const struct sockaddr_un *)(const void *)sa;
143
144 if (salen < (socklen_t) offsetof(struct sockaddr_un, sun_path)) {
145 return EAI_FAMILY;
146 }
147
148 if (serv != NULL && servlen > 0)
149 serv[0] = '\0';
150
151 if (host && hostlen > 0)
152 strlcpy(host, sun->sun_path,
153 MIN((socklen_t) sizeof(sun->sun_path) + 1, hostlen));
154
155 return 0;
156}
157
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800158/*
159 * getnameinfo_inet():
160 * Format an IPv4 or IPv6 sockaddr into a printable string.
161 */
Mattias Falkc63e5902011-08-23 14:34:14 +0200162static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800163getnameinfo_inet(const struct sockaddr* sa, socklen_t salen,
164 char *host, socklen_t hostlen,
165 char *serv, socklen_t servlen,
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500166 int flags, unsigned netid, unsigned mark)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167{
168 const struct afd *afd;
169 struct servent *sp;
170 struct hostent *hp;
171 u_short port;
172 int family, i;
173 const char *addr;
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800174 uint32_t v4a;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175 char numserv[512];
176 char numaddr[512];
177
178 /* sa is checked below */
179 /* host may be NULL */
180 /* serv may be NULL */
181
182 if (sa == NULL)
183 return EAI_FAIL;
184
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185 family = sa->sa_family;
186 for (i = 0; afdl[i].a_af; i++)
187 if (afdl[i].a_af == family) {
188 afd = &afdl[i];
189 goto found;
190 }
191 return EAI_FAMILY;
192
193 found:
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800194 // http://b/1889275: callers should be allowed to provide too much
195 // space, but not too little.
196 if (salen < afd->a_socklen) {
197 return EAI_FAMILY;
198 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199
200 /* network byte order */
201 port = ((const struct sockinet *)(const void *)sa)->si_port;
202 addr = (const char *)(const void *)sa + afd->a_off;
203
204 if (serv == NULL || servlen == 0) {
205 /*
206 * do nothing in this case.
207 * in case you are wondering if "&&" is more correct than
208 * "||" here: rfc2553bis-03 says that serv == NULL OR
209 * servlen == 0 means that the caller does not want the result.
210 */
211 } else {
212 if (flags & NI_NUMERICSERV)
213 sp = NULL;
214 else {
215 sp = getservbyport(port,
216 (flags & NI_DGRAM) ? "udp" : "tcp");
217 }
218 if (sp) {
219 if (strlen(sp->s_name) + 1 > (size_t)servlen)
220 return EAI_MEMORY;
221 strlcpy(serv, sp->s_name, servlen);
222 } else {
223 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
224 if (strlen(numserv) + 1 > (size_t)servlen)
225 return EAI_MEMORY;
226 strlcpy(serv, numserv, servlen);
227 }
228 }
229
230 switch (sa->sa_family) {
231 case AF_INET:
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800232 v4a = (uint32_t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233 ntohl(((const struct sockaddr_in *)
234 (const void *)sa)->sin_addr.s_addr);
235 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
236 flags |= NI_NUMERICHOST;
237 v4a >>= IN_CLASSA_NSHIFT;
238 if (v4a == 0)
239 flags |= NI_NUMERICHOST;
240 break;
241#ifdef INET6
242 case AF_INET6:
243 {
244 const struct sockaddr_in6 *sin6;
245 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
246 switch (sin6->sin6_addr.s6_addr[0]) {
247 case 0x00:
248 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
249 ;
250 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
251 ;
252 else
253 flags |= NI_NUMERICHOST;
254 break;
255 default:
256 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
257 flags |= NI_NUMERICHOST;
258 }
259 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
260 flags |= NI_NUMERICHOST;
261 break;
262 }
263 }
264 break;
265#endif
266 }
267 if (host == NULL || hostlen == 0) {
268 /*
269 * do nothing in this case.
270 * in case you are wondering if "&&" is more correct than
271 * "||" here: rfc2553bis-03 says that host == NULL or
272 * hostlen == 0 means that the caller does not want the result.
273 */
274 } else if (flags & NI_NUMERICHOST) {
275 size_t numaddrlen;
276
277 /* NUMERICHOST and NAMEREQD conflicts with each other */
278 if (flags & NI_NAMEREQD)
279 return EAI_NONAME;
280
281 switch(afd->a_af) {
282#ifdef INET6
283 case AF_INET6:
284 {
285 int error;
286
287 if ((error = ip6_parsenumeric(sa, addr, host,
288 hostlen, flags)) != 0)
289 return(error);
290 break;
291 }
292#endif
293 default:
294 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
295 == NULL)
296 return EAI_SYSTEM;
297 numaddrlen = strlen(numaddr);
298 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
299 return EAI_MEMORY;
300 strlcpy(host, numaddr, hostlen);
301 break;
302 }
303 } else {
Elliott Hughes9773fa32014-12-10 14:56:46 -0800304 hp = android_gethostbyaddrfornet_proxy(addr, afd->a_addrlen, afd->a_af, netid, mark);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800305 if (hp) {
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800306#if 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800307 /*
308 * commented out, since "for local host" is not
309 * implemented here - see RFC2553 p30
310 */
311 if (flags & NI_NOFQDN) {
312 char *p;
313 p = strchr(hp->h_name, '.');
314 if (p)
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500315 TODO: Before uncommenting rewrite to avoid modifying hp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800316 *p = '\0';
317 }
318#endif
319 if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
320 return EAI_MEMORY;
321 }
322 strlcpy(host, hp->h_name, hostlen);
323 } else {
324 if (flags & NI_NAMEREQD)
325 return EAI_NONAME;
326 switch(afd->a_af) {
327#ifdef INET6
328 case AF_INET6:
329 {
330 int error;
331
332 if ((error = ip6_parsenumeric(sa, addr, host,
333 hostlen,
334 flags)) != 0)
335 return(error);
336 break;
337 }
338#endif
339 default:
340 if (inet_ntop(afd->a_af, addr, host,
341 hostlen) == NULL)
342 return EAI_SYSTEM;
343 break;
344 }
345 }
346 }
347 return(0);
348}
349
350#ifdef INET6
351static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800352ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
353 socklen_t hostlen, int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800354{
355 size_t numaddrlen;
356 char numaddr[512];
357
358 assert(sa != NULL);
359 assert(addr != NULL);
360 assert(host != NULL);
361
362 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
363 return EAI_SYSTEM;
364
365 numaddrlen = strlen(numaddr);
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700366 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800367 return EAI_OVERFLOW;
368 strlcpy(host, numaddr, hostlen);
369
370 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
371 char zonebuf[MAXHOSTNAMELEN];
372 int zonelen;
373
374 zonelen = ip6_sa2str(
375 (const struct sockaddr_in6 *)(const void *)sa,
376 zonebuf, sizeof(zonebuf), flags);
377 if (zonelen < 0)
378 return EAI_OVERFLOW;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700379 if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800380 return EAI_OVERFLOW;
381 /* construct <numeric-addr><delim><zoneid> */
382 memcpy(host + numaddrlen + 1, zonebuf,
383 (size_t)zonelen);
384 host[numaddrlen] = SCOPE_DELIMITER;
385 host[numaddrlen + 1 + zonelen] = '\0';
386 }
387
388 return 0;
389}
390
391/* ARGSUSED */
392static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800393ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800394{
395 unsigned int ifindex;
396 const struct in6_addr *a6;
397 int n;
398
399 assert(sa6 != NULL);
400 assert(buf != NULL);
401
402 ifindex = (unsigned int)sa6->sin6_scope_id;
403 a6 = &sa6->sin6_addr;
404
405#ifdef NI_NUMERICSCOPE
406 if ((flags & NI_NUMERICSCOPE) != 0) {
407 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
408 if (n < 0 || n >= bufsiz)
409 return -1;
410 else
411 return n;
412 }
413#endif
414
415 /* if_indextoname() does not take buffer size. not a good api... */
416 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
417 bufsiz >= IF_NAMESIZE) {
418 char *p = if_indextoname(ifindex, buf);
419 if (p) {
420 return(strlen(p));
421 }
422 }
423
424 /* last resort */
425 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
426 if (n < 0 || (size_t) n >= bufsiz)
427 return -1;
428 else
429 return n;
430}
431#endif /* INET6 */