blob: abd1a6614efa9b883ce4c4cd79402bd88214f046 [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_types.h>
58#include <netinet/in.h>
59#include <arpa/inet.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060#include <assert.h>
61#include <limits.h>
62#include <netdb.h>
Calin Juravle569fb982014-03-04 15:01:29 +000063#include <arpa/nameser.h>
Szymon Jakubczakea9bf672014-02-14 17:07:23 -050064#include "resolv_netid.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 *,
Szymon Jakubczakea9bf672014-02-14 17:07:23 -050095 socklen_t, char *, socklen_t, int, unsigned, unsigned);
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 */
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500108int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen,
109 char* serv, size_t servlen, int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110{
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500111 return android_getnameinfofornet(sa, salen, host, hostlen, serv, servlen, flags,
112 NETID_UNSET, MARK_UNSET);
Mattias Falkc63e5902011-08-23 14:34:14 +0200113}
114
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500115int android_getnameinfofornet(const struct sockaddr* sa, socklen_t salen, char* host,
116 size_t hostlen, char* serv, size_t servlen, int flags, unsigned netid,
117 unsigned mark)
Mattias Falkc63e5902011-08-23 14:34:14 +0200118{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119 switch (sa->sa_family) {
120 case AF_INET:
121 case AF_INET6:
122 return getnameinfo_inet(sa, salen, host, hostlen,
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500123 serv, servlen, flags, netid, mark);
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800124 case AF_LOCAL:
125 return getnameinfo_local(sa, salen, host, hostlen,
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000126 serv, servlen, flags);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127 default:
128 return EAI_FAMILY;
129 }
130}
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000131
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800132/*
133 * getnameinfo_local():
134 * Format an local address into a printable format.
135 */
136/* ARGSUSED */
137static int
138getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
139 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
140 int flags __attribute__((unused)))
141{
142 const struct sockaddr_un *sun =
143 (const struct sockaddr_un *)(const void *)sa;
144
145 if (salen < (socklen_t) offsetof(struct sockaddr_un, sun_path)) {
146 return EAI_FAMILY;
147 }
148
149 if (serv != NULL && servlen > 0)
150 serv[0] = '\0';
151
152 if (host && hostlen > 0)
153 strlcpy(host, sun->sun_path,
154 MIN((socklen_t) sizeof(sun->sun_path) + 1, hostlen));
155
156 return 0;
157}
158
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159/*
160 * getnameinfo_inet():
161 * Format an IPv4 or IPv6 sockaddr into a printable string.
162 */
Mattias Falkc63e5902011-08-23 14:34:14 +0200163static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800164getnameinfo_inet(const struct sockaddr* sa, socklen_t salen,
165 char *host, socklen_t hostlen,
166 char *serv, socklen_t servlen,
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500167 int flags, unsigned netid, unsigned mark)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168{
169 const struct afd *afd;
170 struct servent *sp;
171 struct hostent *hp;
172 u_short port;
173 int family, i;
174 const char *addr;
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800175 uint32_t v4a;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176 char numserv[512];
177 char numaddr[512];
178
179 /* sa is checked below */
180 /* host may be NULL */
181 /* serv may be NULL */
182
183 if (sa == NULL)
184 return EAI_FAIL;
185
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186 family = sa->sa_family;
187 for (i = 0; afdl[i].a_af; i++)
188 if (afdl[i].a_af == family) {
189 afd = &afdl[i];
190 goto found;
191 }
192 return EAI_FAMILY;
193
194 found:
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800195 // http://b/1889275: callers should be allowed to provide too much
196 // space, but not too little.
197 if (salen < afd->a_socklen) {
198 return EAI_FAMILY;
199 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
201 /* network byte order */
202 port = ((const struct sockinet *)(const void *)sa)->si_port;
203 addr = (const char *)(const void *)sa + afd->a_off;
204
205 if (serv == NULL || servlen == 0) {
206 /*
207 * do nothing in this case.
208 * in case you are wondering if "&&" is more correct than
209 * "||" here: rfc2553bis-03 says that serv == NULL OR
210 * servlen == 0 means that the caller does not want the result.
211 */
212 } else {
213 if (flags & NI_NUMERICSERV)
214 sp = NULL;
215 else {
216 sp = getservbyport(port,
217 (flags & NI_DGRAM) ? "udp" : "tcp");
218 }
219 if (sp) {
220 if (strlen(sp->s_name) + 1 > (size_t)servlen)
221 return EAI_MEMORY;
222 strlcpy(serv, sp->s_name, servlen);
223 } else {
224 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
225 if (strlen(numserv) + 1 > (size_t)servlen)
226 return EAI_MEMORY;
227 strlcpy(serv, numserv, servlen);
228 }
229 }
230
231 switch (sa->sa_family) {
232 case AF_INET:
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800233 v4a = (uint32_t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800234 ntohl(((const struct sockaddr_in *)
235 (const void *)sa)->sin_addr.s_addr);
236 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
237 flags |= NI_NUMERICHOST;
238 v4a >>= IN_CLASSA_NSHIFT;
239 if (v4a == 0)
240 flags |= NI_NUMERICHOST;
241 break;
242#ifdef INET6
243 case AF_INET6:
244 {
245 const struct sockaddr_in6 *sin6;
246 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
247 switch (sin6->sin6_addr.s6_addr[0]) {
248 case 0x00:
249 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
250 ;
251 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
252 ;
253 else
254 flags |= NI_NUMERICHOST;
255 break;
256 default:
257 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
258 flags |= NI_NUMERICHOST;
259 }
260 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
261 flags |= NI_NUMERICHOST;
262 break;
263 }
264 }
265 break;
266#endif
267 }
268 if (host == NULL || hostlen == 0) {
269 /*
270 * do nothing in this case.
271 * in case you are wondering if "&&" is more correct than
272 * "||" here: rfc2553bis-03 says that host == NULL or
273 * hostlen == 0 means that the caller does not want the result.
274 */
275 } else if (flags & NI_NUMERICHOST) {
276 size_t numaddrlen;
277
278 /* NUMERICHOST and NAMEREQD conflicts with each other */
279 if (flags & NI_NAMEREQD)
280 return EAI_NONAME;
281
282 switch(afd->a_af) {
283#ifdef INET6
284 case AF_INET6:
285 {
286 int error;
287
288 if ((error = ip6_parsenumeric(sa, addr, host,
289 hostlen, flags)) != 0)
290 return(error);
291 break;
292 }
293#endif
294 default:
295 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
296 == NULL)
297 return EAI_SYSTEM;
298 numaddrlen = strlen(numaddr);
299 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
300 return EAI_MEMORY;
301 strlcpy(host, numaddr, hostlen);
302 break;
303 }
304 } else {
Elliott Hughes9773fa32014-12-10 14:56:46 -0800305 hp = android_gethostbyaddrfornet_proxy(addr, afd->a_addrlen, afd->a_af, netid, mark);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800306 if (hp) {
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800307#if 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308 /*
309 * commented out, since "for local host" is not
310 * implemented here - see RFC2553 p30
311 */
312 if (flags & NI_NOFQDN) {
313 char *p;
314 p = strchr(hp->h_name, '.');
315 if (p)
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500316 TODO: Before uncommenting rewrite to avoid modifying hp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800317 *p = '\0';
318 }
319#endif
320 if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
321 return EAI_MEMORY;
322 }
323 strlcpy(host, hp->h_name, hostlen);
324 } else {
325 if (flags & NI_NAMEREQD)
326 return EAI_NONAME;
327 switch(afd->a_af) {
328#ifdef INET6
329 case AF_INET6:
330 {
331 int error;
332
333 if ((error = ip6_parsenumeric(sa, addr, host,
334 hostlen,
335 flags)) != 0)
336 return(error);
337 break;
338 }
339#endif
340 default:
341 if (inet_ntop(afd->a_af, addr, host,
342 hostlen) == NULL)
343 return EAI_SYSTEM;
344 break;
345 }
346 }
347 }
348 return(0);
349}
350
351#ifdef INET6
352static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800353ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
354 socklen_t hostlen, int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355{
356 size_t numaddrlen;
357 char numaddr[512];
358
359 assert(sa != NULL);
360 assert(addr != NULL);
361 assert(host != NULL);
362
363 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
364 return EAI_SYSTEM;
365
366 numaddrlen = strlen(numaddr);
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700367 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800368 return EAI_OVERFLOW;
369 strlcpy(host, numaddr, hostlen);
370
371 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
372 char zonebuf[MAXHOSTNAMELEN];
373 int zonelen;
374
375 zonelen = ip6_sa2str(
376 (const struct sockaddr_in6 *)(const void *)sa,
377 zonebuf, sizeof(zonebuf), flags);
378 if (zonelen < 0)
379 return EAI_OVERFLOW;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700380 if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800381 return EAI_OVERFLOW;
382 /* construct <numeric-addr><delim><zoneid> */
383 memcpy(host + numaddrlen + 1, zonebuf,
384 (size_t)zonelen);
385 host[numaddrlen] = SCOPE_DELIMITER;
386 host[numaddrlen + 1 + zonelen] = '\0';
387 }
388
389 return 0;
390}
391
392/* ARGSUSED */
393static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800394ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800395{
396 unsigned int ifindex;
397 const struct in6_addr *a6;
398 int n;
399
400 assert(sa6 != NULL);
401 assert(buf != NULL);
402
403 ifindex = (unsigned int)sa6->sin6_scope_id;
404 a6 = &sa6->sin6_addr;
405
406#ifdef NI_NUMERICSCOPE
407 if ((flags & NI_NUMERICSCOPE) != 0) {
408 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
409 if (n < 0 || n >= bufsiz)
410 return -1;
411 else
412 return n;
413 }
414#endif
415
416 /* if_indextoname() does not take buffer size. not a good api... */
417 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
418 bufsiz >= IF_NAMESIZE) {
419 char *p = if_indextoname(ifindex, buf);
420 if (p) {
421 return(strlen(p));
422 }
423 }
424
425 /* last resort */
426 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
427 if (n < 0 || (size_t) n >= bufsiz)
428 return -1;
429 else
430 return n;
431}
432#endif /* INET6 */