2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Issues to be discussed:
32 * - RFC2553 says that we should raise error on short buffer. X/Open says
33 * we need to truncate the result. We obey RFC2553 (and X/Open should be
34 * modified). ipngwg rough consensus seems to follow RFC2553. RFC3493 says
35 * nothing about it, but defines a new error code EAI_OVERFLOW which seems
36 * to be intended the code for this case.
37 * - What is "local" in NI_NOFQDN? (see comments in the code)
38 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
39 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
40 * sin6_scope_id is filled - standardization status?
41 * - what should we do if we should do getservbyport("sctp")?
45 * Considerations about thread-safeness
46 * The code in this file is thread-safe, and so the thread-safeness of
47 * getnameinfo() depends on the property of backend functions.
48 * - getservbyport() is not thread safe for most systems we are targeting.
49 * - getipnodebyaddr() is thread safe. However, many resolver libraries
50 * used in the function are not thread safe.
51 * - gethostbyaddr() is usually not thread safe.
56 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60 #include <arpa/nameser.h>
67 #include "getaddrinfo.h"
68 #include "getnameinfo.h"
70 static const struct afd
{
78 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
79 offsetof(struct sockaddr_in6
, sin6_addr
),
80 offsetof(struct sockaddr_in6
, sin6_port
)},
82 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
83 offsetof(struct sockaddr_in
, sin_addr
),
84 offsetof(struct sockaddr_in
, sin_port
)},
89 static int ip6_parsenumeric
__P((const struct sockaddr
*, const char *, char *,
91 static int ip6_sa2str
__P((const struct sockaddr_in6
*, char *, size_t, int));
95 getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
)
96 const struct sockaddr
*sa
;
104 const struct afd
*afd
;
116 #if HAVE_SA_LEN /*XXX*/
117 if (sa
->sa_len
!= salen
)
121 family
= sa
->sa_family
;
122 for (i
= 0; afdl
[i
].a_af
; i
++)
123 if (afdl
[i
].a_af
== family
) {
130 if (salen
!= afd
->a_socklen
)
133 /* network byte order */
134 memcpy(&port
, (const char *)sa
+ afd
->a_portoff
, sizeof(port
));
135 addr
= (const char *)sa
+ afd
->a_off
;
137 if (serv
== NULL
|| servlen
== 0) {
139 * do nothing in this case.
140 * in case you are wondering if "&&" is more correct than
141 * "||" here: RFC3493 says that serv == NULL OR servlen == 0
142 * means that the caller does not want the result.
145 if (flags
& NI_NUMERICSERV
)
148 sp
= getservbyport(port
,
149 (flags
& NI_DGRAM
) ? "udp" : "tcp");
152 if (strlen(sp
->s_name
) + 1 > servlen
)
154 strncpy(serv
, sp
->s_name
, servlen
);
156 snprintf(numserv
, sizeof(numserv
), "%u", ntohs(port
));
157 if (strlen(numserv
) + 1 > servlen
)
159 strncpy(serv
, numserv
, servlen
);
163 switch (sa
->sa_family
) {
166 ntohl(((const struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
167 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
168 flags
|= NI_NUMERICHOST
;
169 v4a
>>= IN_CLASSA_NSHIFT
;
171 flags
|= NI_NUMERICHOST
;
175 const struct sockaddr_in6
*sin6
;
176 sin6
= (const struct sockaddr_in6
*)sa
;
177 switch (sin6
->sin6_addr
.s6_addr
[0]) {
179 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
181 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
184 flags
|= NI_NUMERICHOST
;
187 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
188 flags
|= NI_NUMERICHOST
;
189 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
190 flags
|= NI_NUMERICHOST
;
197 if (host
== NULL
|| hostlen
== 0) {
199 * do nothing in this case.
200 * in case you are wondering if "&&" is more correct than
201 * "||" here: RFC3493 says that host == NULL or hostlen == 0
202 * means that the caller does not want the result.
204 } else if (flags
& NI_NUMERICHOST
) {
205 /* NUMERICHOST and NAMEREQD conflicts with each other */
206 if (flags
& NI_NAMEREQD
)
213 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
215 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
216 #if 0 // getnameinfo.c:161:9: error: variable 'h_error' set but not used
227 if (flags
& NI_NOFQDN
) {
229 * According to RFC3493 section 6.2, NI_NOFQDN
230 * means "node name portion of the FQDN shall
231 * be returned for local hosts." The following
232 * code tries to implement it by returning the
233 * first label (the part before the first
234 * period) of the FQDN. However, it is not
235 * clear if this always makes sense, since the
236 * given address may be outside of "local
237 * hosts." Due to the unclear description, we
238 * disable the code in this implementation.
241 p
= strchr(hp
->h_name
, '.');
246 if (strlen(hp
->h_name
) + 1 > hostlen
) {
252 strncpy(host
, hp
->h_name
, hostlen
);
257 if (flags
& NI_NAMEREQD
)
266 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
274 if (inet_ntop(afd
->a_af
, addr
, host
,
286 ip6_parsenumeric(sa
, addr
, host
, hostlen
, flags
)
287 const struct sockaddr
*sa
;
296 if (inet_ntop(AF_INET6
, addr
, numaddr
, sizeof(numaddr
)) == NULL
)
299 numaddrlen
= strlen(numaddr
);
300 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
302 strncpy(host
, numaddr
, hostlen
);
304 if (((const struct sockaddr_in6
*)sa
)->sin6_scope_id
) {
305 char zonebuf
[SQUIDHOSTNAMELEN
];
308 zonelen
= ip6_sa2str(
309 (const struct sockaddr_in6
*)(const void *)sa
,
310 zonebuf
, sizeof(zonebuf
), flags
);
313 if (zonelen
+ 1 + numaddrlen
+ 1 > hostlen
)
316 /* construct <numeric-addr><delim><zoneid> */
317 memcpy(host
+ numaddrlen
+ 1, zonebuf
,
319 host
[numaddrlen
] = SCOPE_DELIMITER
;
320 host
[numaddrlen
+ 1 + zonelen
] = '\0';
328 ip6_sa2str(sa6
, buf
, bufsiz
, flags
)
329 const struct sockaddr_in6
*sa6
;
334 unsigned int ifindex
;
335 const struct in6_addr
*a6
;
338 ifindex
= (unsigned int)sa6
->sin6_scope_id
;
339 a6
= &sa6
->sin6_addr
;
342 if ((flags
& NI_NUMERICSCOPE
) != 0) {
343 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
344 if (n
< 0 || n
>= bufsiz
)
351 /* if_indextoname() does not take buffer size. not a good api... */
352 if ((IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
) ||
353 IN6_IS_ADDR_MC_NODELOCAL(a6
)) && bufsiz
>= IF_NAMESIZE
) {
354 char *p
= if_indextoname(ifindex
, buf
);
360 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
361 if (n
< 0 || n
>= bufsiz
)