]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/getnameinfo.cc
f70fa7d8e3e22719932ec58aca6a812f32361f95
2 * Copyright (C) 1996-2016 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 * 13-Jan-2015 : Various fixed for C++ and MinGW native build
27 * Original License and code follows.
31 /* KAME: getnameinfo.c,v 1.72 2005/01/13 04:12:03 itojun Exp */
34 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the project nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * Issues to be discussed:
64 * - RFC2553 says that we should raise error on short buffer. X/Open says
65 * we need to truncate the result. We obey RFC2553 (and X/Open should be
66 * modified). ipngwg rough consensus seems to follow RFC2553. RFC3493 says
67 * nothing about it, but defines a new error code EAI_OVERFLOW which seems
68 * to be intended the code for this case.
69 * - What is "local" in NI_NOFQDN? (see comments in the code)
70 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
71 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
72 * sin6_scope_id is filled - standardization status?
73 * - what should we do if we should do getservbyport("sctp")?
77 * Considerations about thread-safeness
78 * The code in this file is thread-safe, and so the thread-safeness of
79 * getnameinfo() depends on the property of backend functions.
80 * - getservbyport() is not thread safe for most systems we are targeting.
81 * - getipnodebyaddr() is thread safe. However, many resolver libraries
82 * used in the function are not thread safe.
83 * - gethostbyaddr() is usually not thread safe.
86 #if !HAVE_DECL_GETNAMEINFO
89 #include <sys/socket.h>
95 #include <netinet/in.h>
98 #include <arpa/inet.h>
100 #if HAVE_ARPA_NAMESER_H
101 #include <arpa/nameser.h>
119 #include <inttypes.h>
124 #include <ws2tcpip.h>
127 static const struct afd
{
135 { PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
136 offsetof(struct sockaddr_in6
, sin6_addr
),
137 offsetof(struct sockaddr_in6
, sin6_port
)
140 { PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
141 offsetof(struct sockaddr_in
, sin_addr
),
142 offsetof(struct sockaddr_in
, sin_port
)
148 static int ip6_parsenumeric
__P((const struct sockaddr
*, const char *, char *,
150 static int ip6_sa2str
__P((const struct sockaddr_in6
*, char *, size_t, int));
154 xgetnameinfo(const struct sockaddr
*sa
, socklen_t salen
, char *host
, size_t hostlen
, char *serv
, size_t servlen
, int flags
)
156 const struct afd
*afd
;
168 #if HAVE_SA_LEN /*XXX*/
169 if (sa
->sa_len
!= salen
)
173 family
= sa
->sa_family
;
174 for (i
= 0; afdl
[i
].a_af
; i
++)
175 if (afdl
[i
].a_af
== family
) {
182 if (salen
!= afd
->a_socklen
)
185 /* network byte order */
186 memcpy(&port
, (const char *)sa
+ afd
->a_portoff
, sizeof(port
));
187 addr
= (const char *)sa
+ afd
->a_off
;
189 if (serv
== NULL
|| servlen
== 0) {
191 * do nothing in this case.
192 * in case you are wondering if "&&" is more correct than
193 * "||" here: RFC3493 says that serv == NULL OR servlen == 0
194 * means that the caller does not want the result.
197 if (flags
& NI_NUMERICSERV
)
200 sp
= getservbyport(port
,
201 (flags
& NI_DGRAM
) ? "udp" : "tcp");
204 if (strlen(sp
->s_name
) + 1 > servlen
)
206 strncpy(serv
, sp
->s_name
, servlen
);
208 snprintf(numserv
, sizeof(numserv
), "%u", ntohs(port
));
209 if (strlen(numserv
) + 1 > servlen
)
211 strncpy(serv
, numserv
, servlen
);
215 switch (sa
->sa_family
) {
218 ntohl(((const struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
219 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
220 flags
|= NI_NUMERICHOST
;
221 v4a
>>= IN_CLASSA_NSHIFT
;
223 flags
|= NI_NUMERICHOST
;
227 const struct sockaddr_in6
*sin6
;
228 sin6
= (const struct sockaddr_in6
*)sa
;
229 switch (sin6
->sin6_addr
.s6_addr
[0]) {
231 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
233 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
236 flags
|= NI_NUMERICHOST
;
239 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
240 flags
|= NI_NUMERICHOST
;
241 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
242 flags
|= NI_NUMERICHOST
;
249 if (host
== NULL
|| hostlen
== 0) {
251 * do nothing in this case.
252 * in case you are wondering if "&&" is more correct than
253 * "||" here: RFC3493 says that host == NULL or hostlen == 0
254 * means that the caller does not want the result.
256 } else if (flags
& NI_NUMERICHOST
) {
257 /* NUMERICHOST and NAMEREQD conflicts with each other */
258 if (flags
& NI_NAMEREQD
)
265 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
267 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
268 #if 0 // getnameinfo.c:161:9: error: variable 'h_error' set but not used
279 if (flags
& NI_NOFQDN
) {
281 * According to RFC3493 section 6.2, NI_NOFQDN
282 * means "node name portion of the FQDN shall
283 * be returned for local hosts." The following
284 * code tries to implement it by returning the
285 * first label (the part before the first
286 * period) of the FQDN. However, it is not
287 * clear if this always makes sense, since the
288 * given address may be outside of "local
289 * hosts." Due to the unclear description, we
290 * disable the code in this implementation.
293 p
= strchr(hp
->h_name
, '.');
298 if (strlen(hp
->h_name
) + 1 > hostlen
) {
304 strncpy(host
, hp
->h_name
, hostlen
);
309 if (flags
& NI_NAMEREQD
)
318 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
326 if (inet_ntop(afd
->a_af
, addr
, host
,
338 ip6_parsenumeric(sa
, addr
, host
, hostlen
, flags
)
339 const struct sockaddr
*sa
;
348 if (inet_ntop(AF_INET6
, addr
, numaddr
, sizeof(numaddr
)) == NULL
)
351 numaddrlen
= strlen(numaddr
);
352 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
354 strncpy(host
, numaddr
, hostlen
);
356 if (((const struct sockaddr_in6
*)sa
)->sin6_scope_id
) {
357 char zonebuf
[SQUIDHOSTNAMELEN
];
360 zonelen
= ip6_sa2str(
361 (const struct sockaddr_in6
*)(const void *)sa
,
362 zonebuf
, sizeof(zonebuf
), flags
);
365 if (zonelen
+ 1 + numaddrlen
+ 1 > hostlen
)
368 /* construct <numeric-addr><delim><zoneid> */
369 memcpy(host
+ numaddrlen
+ 1, zonebuf
,
371 host
[numaddrlen
] = SCOPE_DELIMITER
;
372 host
[numaddrlen
+ 1 + zonelen
] = '\0';
380 ip6_sa2str(sa6
, buf
, bufsiz
, flags
)
381 const struct sockaddr_in6
*sa6
;
386 unsigned int ifindex
;
387 const struct in6_addr
*a6
;
390 ifindex
= (unsigned int)sa6
->sin6_scope_id
;
391 a6
= &sa6
->sin6_addr
;
394 if ((flags
& NI_NUMERICSCOPE
) != 0) {
395 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
396 if (n
< 0 || n
>= bufsiz
)
403 /* if_indextoname() does not take buffer size. not a good api... */
404 if ((IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
) ||
405 IN6_IS_ADDR_MC_NODELOCAL(a6
)) && bufsiz
>= IF_NAMESIZE
) {
406 char *p
= if_indextoname(ifindex
, buf
);
412 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
413 if (n
< 0 || n
>= bufsiz
)
419 #endif /* HAVE_DECL_GETNAMEINFO */