]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/getnameinfo.c
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * Shamelessly duplicated from the fetchmail public sources
11 * for use by the Squid Project under GNU Public License.
13 * Update/Maintenance History:
15 * 16-Aug-2007 : Copied from fetchmail 6.3.8
16 * - added protection around libray headers
17 * - added use of alternative name xgetnameinfo
18 * to split from any OS-provided.
20 * 06-Oct-2007 : Various fixes to allow the build on MinGW
21 * - use srtncpy instead of strlcpy
22 * - use xinet_ntop instead of inet_ntop
23 * - use SQUIDHOSTNAMELEN instead of MAXHOSTNAMELEN
25 * Original License and code follows.
29 /* KAME: getnameinfo.c,v 1.72 2005/01/13 04:12:03 itojun Exp */
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * Issues to be discussed:
62 * - RFC2553 says that we should raise error on short buffer. X/Open says
63 * we need to truncate the result. We obey RFC2553 (and X/Open should be
64 * modified). ipngwg rough consensus seems to follow RFC2553. RFC3493 says
65 * nothing about it, but defines a new error code EAI_OVERFLOW which seems
66 * to be intended the code for this case.
67 * - What is "local" in NI_NOFQDN? (see comments in the code)
68 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
69 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
70 * sin6_scope_id is filled - standardization status?
71 * - what should we do if we should do getservbyport("sctp")?
75 * Considerations about thread-safeness
76 * The code in this file is thread-safe, and so the thread-safeness of
77 * getnameinfo() depends on the property of backend functions.
78 * - getservbyport() is not thread safe for most systems we are targeting.
79 * - getipnodebyaddr() is thread safe. However, many resolver libraries
80 * used in the function are not thread safe.
81 * - gethostbyaddr() is usually not thread safe.
86 #include "compat/getaddrinfo.h"
87 #include "compat/inet_ntop.h"
90 #include <sys/socket.h>
96 #include <netinet/in.h>
99 #include <arpa/inet.h>
101 #if HAVE_ARPA_NAMESER_H
102 #include <arpa/nameser.h>
120 #include <inttypes.h>
125 #include <ws2tcpip.h>
128 static const struct afd
{
136 { PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
137 offsetof(struct sockaddr_in6
, sin6_addr
),
138 offsetof(struct sockaddr_in6
, sin6_port
)
141 { PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
142 offsetof(struct sockaddr_in
, sin_addr
),
143 offsetof(struct sockaddr_in
, sin_port
)
149 static int ip6_parsenumeric
__P((const struct sockaddr
*, const char *, char *,
151 static int ip6_sa2str
__P((const struct sockaddr_in6
*, char *, size_t, int));
155 xgetnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
)
156 const struct sockaddr
*sa
;
164 const struct afd
*afd
;
176 #if HAVE_SA_LEN /*XXX*/
177 if (sa
->sa_len
!= salen
)
181 family
= sa
->sa_family
;
182 for (i
= 0; afdl
[i
].a_af
; i
++)
183 if (afdl
[i
].a_af
== family
) {
190 if (salen
!= afd
->a_socklen
)
193 /* network byte order */
194 memcpy(&port
, (const char *)sa
+ afd
->a_portoff
, sizeof(port
));
195 addr
= (const char *)sa
+ afd
->a_off
;
197 if (serv
== NULL
|| servlen
== 0) {
199 * do nothing in this case.
200 * in case you are wondering if "&&" is more correct than
201 * "||" here: RFC3493 says that serv == NULL OR servlen == 0
202 * means that the caller does not want the result.
205 if (flags
& NI_NUMERICSERV
)
208 sp
= getservbyport(port
,
209 (flags
& NI_DGRAM
) ? "udp" : "tcp");
212 if (strlen(sp
->s_name
) + 1 > servlen
)
214 strncpy(serv
, sp
->s_name
, servlen
);
216 snprintf(numserv
, sizeof(numserv
), "%u", ntohs(port
));
217 if (strlen(numserv
) + 1 > servlen
)
219 strncpy(serv
, numserv
, servlen
);
223 switch (sa
->sa_family
) {
226 ntohl(((const struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
227 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
228 flags
|= NI_NUMERICHOST
;
229 v4a
>>= IN_CLASSA_NSHIFT
;
231 flags
|= NI_NUMERICHOST
;
235 const struct sockaddr_in6
*sin6
;
236 sin6
= (const struct sockaddr_in6
*)sa
;
237 switch (sin6
->sin6_addr
.s6_addr
[0]) {
239 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
241 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
244 flags
|= NI_NUMERICHOST
;
247 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
248 flags
|= NI_NUMERICHOST
;
249 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
250 flags
|= NI_NUMERICHOST
;
257 if (host
== NULL
|| hostlen
== 0) {
259 * do nothing in this case.
260 * in case you are wondering if "&&" is more correct than
261 * "||" here: RFC3493 says that host == NULL or hostlen == 0
262 * means that the caller does not want the result.
264 } else if (flags
& NI_NUMERICHOST
) {
265 /* NUMERICHOST and NAMEREQD conflicts with each other */
266 if (flags
& NI_NAMEREQD
)
273 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
275 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
276 #if 0 // getnameinfo.c:161:9: error: variable 'h_error' set but not used
287 if (flags
& NI_NOFQDN
) {
289 * According to RFC3493 section 6.2, NI_NOFQDN
290 * means "node name portion of the FQDN shall
291 * be returned for local hosts." The following
292 * code tries to implement it by returning the
293 * first label (the part before the first
294 * period) of the FQDN. However, it is not
295 * clear if this always makes sense, since the
296 * given address may be outside of "local
297 * hosts." Due to the unclear description, we
298 * disable the code in this implementation.
301 p
= strchr(hp
->h_name
, '.');
306 if (strlen(hp
->h_name
) + 1 > hostlen
) {
312 strncpy(host
, hp
->h_name
, hostlen
);
317 if (flags
& NI_NAMEREQD
)
326 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
334 if (inet_ntop(afd
->a_af
, addr
, host
,
346 ip6_parsenumeric(sa
, addr
, host
, hostlen
, flags
)
347 const struct sockaddr
*sa
;
356 if (inet_ntop(AF_INET6
, addr
, numaddr
, sizeof(numaddr
)) == NULL
)
359 numaddrlen
= strlen(numaddr
);
360 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
362 strncpy(host
, numaddr
, hostlen
);
364 if (((const struct sockaddr_in6
*)sa
)->sin6_scope_id
) {
365 char zonebuf
[SQUIDHOSTNAMELEN
];
368 zonelen
= ip6_sa2str(
369 (const struct sockaddr_in6
*)(const void *)sa
,
370 zonebuf
, sizeof(zonebuf
), flags
);
373 if (zonelen
+ 1 + numaddrlen
+ 1 > hostlen
)
376 /* construct <numeric-addr><delim><zoneid> */
377 memcpy(host
+ numaddrlen
+ 1, zonebuf
,
379 host
[numaddrlen
] = SCOPE_DELIMITER
;
380 host
[numaddrlen
+ 1 + zonelen
] = '\0';
388 ip6_sa2str(sa6
, buf
, bufsiz
, flags
)
389 const struct sockaddr_in6
*sa6
;
394 unsigned int ifindex
;
395 const struct in6_addr
*a6
;
398 ifindex
= (unsigned int)sa6
->sin6_scope_id
;
399 a6
= &sa6
->sin6_addr
;
402 if ((flags
& NI_NUMERICSCOPE
) != 0) {
403 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
404 if (n
< 0 || n
>= bufsiz
)
411 /* if_indextoname() does not take buffer size. not a good api... */
412 if ((IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
) ||
413 IN6_IS_ADDR_MC_NODELOCAL(a6
)) && bufsiz
>= IF_NAMESIZE
) {
414 char *p
= if_indextoname(ifindex
, buf
);
420 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
421 if (n
< 0 || n
>= bufsiz
)