]>
Commit | Line | Data |
---|---|---|
37be9888 | 1 | /* |
5b74111a | 2 | * Copyright (C) 1996-2018 The Squid Software Foundation and contributors |
37be9888 AJ |
3 | * |
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. | |
7 | */ | |
8 | ||
0e076fb1 | 9 | /* |
10 | * Shamelessly duplicated from the fetchmail public sources | |
11 | * for use by the Squid Project under GNU Public License. | |
12 | * | |
13 | * Update/Maintenance History: | |
14 | * | |
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. | |
19 | * | |
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 | |
24 | * | |
d4ddbe05 DF |
25 | * 13-Jan-2015 : Various fixed for C++ and MinGW native build |
26 | * | |
0e076fb1 | 27 | * Original License and code follows. |
28 | */ | |
f7f3304a | 29 | #include "squid.h" |
0e076fb1 | 30 | |
f53969cc | 31 | /* KAME: getnameinfo.c,v 1.72 2005/01/13 04:12:03 itojun Exp */ |
0e076fb1 | 32 | |
33 | /* | |
34 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
35 | * All rights reserved. | |
36 | * | |
37 | * Redistribution and use in source and binary forms, with or without | |
38 | * modification, are permitted provided that the following conditions | |
39 | * are met: | |
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. | |
48 | * | |
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 | |
59 | * SUCH DAMAGE. | |
60 | */ | |
61 | ||
62 | /* | |
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")? | |
74 | */ | |
75 | ||
76 | /* | |
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. | |
84 | */ | |
85 | ||
55d7d5e9 | 86 | #if !HAVE_DECL_GETNAMEINFO |
27bc2077 | 87 | |
0e076fb1 | 88 | #if HAVE_SYS_SOCKET_H |
89 | #include <sys/socket.h> | |
90 | #endif | |
91 | #if HAVE_NET_IF_H | |
92 | #include <net/if.h> | |
93 | #endif | |
94 | #if HAVE_NETINET_IN_H | |
95 | #include <netinet/in.h> | |
96 | #endif | |
97 | #if HAVE_ARPA_INET_H | |
98 | #include <arpa/inet.h> | |
99 | #endif | |
100 | #if HAVE_ARPA_NAMESER_H | |
101 | #include <arpa/nameser.h> | |
102 | #endif | |
103 | #if HAVE_NETDB_H | |
104 | #include <netdb.h> | |
105 | #endif | |
106 | #if HAVE_RESOLV_H | |
107 | #include <resolv.h> | |
108 | #endif | |
109 | #if HAVE_STRING_H | |
110 | #include <string.h> | |
111 | #endif | |
112 | #if HAVE_STDDEF_H | |
113 | #include <stddef.h> | |
114 | #endif | |
115 | #if HAVE_ERRNO_H | |
116 | #include <errno.h> | |
117 | #endif | |
118 | #if HAVE_INTTYPES_H | |
119 | #include <inttypes.h> | |
120 | #endif | |
121 | ||
7aa9bb3e | 122 | #if _SQUID_WINDOWS_ |
0e076fb1 | 123 | #undef IN_ADDR |
124 | #include <ws2tcpip.h> | |
125 | #endif | |
126 | ||
0e076fb1 | 127 | static const struct afd { |
26ac0430 AJ |
128 | int a_af; |
129 | int a_addrlen; | |
130 | int a_socklen; | |
131 | int a_off; | |
132 | int a_portoff; | |
0e076fb1 | 133 | } afdl [] = { |
32d002cb | 134 | #if INET6 |
f53969cc | 135 | { PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), |
26ac0430 | 136 | offsetof(struct sockaddr_in6, sin6_addr), |
f53969cc SM |
137 | offsetof(struct sockaddr_in6, sin6_port) |
138 | }, | |
0e076fb1 | 139 | #endif |
f53969cc SM |
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) | |
143 | }, | |
26ac0430 | 144 | {0, 0, 0, 0, 0}, |
0e076fb1 | 145 | }; |
146 | ||
32d002cb | 147 | #if INET6 |
0e076fb1 | 148 | static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *, |
26ac0430 | 149 | size_t, int)); |
0e076fb1 | 150 | static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int)); |
151 | #endif | |
152 | ||
153 | int | |
d4ddbe05 | 154 | xgetnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags) |
0e076fb1 | 155 | { |
26ac0430 AJ |
156 | const struct afd *afd; |
157 | struct servent *sp; | |
158 | struct hostent *hp; | |
f45dd259 | 159 | unsigned short port; |
26ac0430 AJ |
160 | int family, i; |
161 | const char *addr; | |
09aabd84 | 162 | uint32_t v4a; |
26ac0430 AJ |
163 | char numserv[512]; |
164 | ||
165 | if (sa == NULL) | |
166 | return EAI_FAIL; | |
0e076fb1 | 167 | |
f53969cc | 168 | #if HAVE_SA_LEN /*XXX*/ |
26ac0430 AJ |
169 | if (sa->sa_len != salen) |
170 | return EAI_FAIL; | |
0e076fb1 | 171 | #endif |
172 | ||
26ac0430 AJ |
173 | family = sa->sa_family; |
174 | for (i = 0; afdl[i].a_af; i++) | |
175 | if (afdl[i].a_af == family) { | |
176 | afd = &afdl[i]; | |
177 | goto found; | |
178 | } | |
179 | return EAI_FAMILY; | |
180 | ||
181 | found: | |
182 | if (salen != afd->a_socklen) | |
183 | return EAI_FAIL; | |
184 | ||
185 | /* network byte order */ | |
186 | memcpy(&port, (const char *)sa + afd->a_portoff, sizeof(port)); | |
187 | addr = (const char *)sa + afd->a_off; | |
188 | ||
189 | if (serv == NULL || servlen == 0) { | |
190 | /* | |
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. | |
195 | */ | |
196 | } else { | |
197 | if (flags & NI_NUMERICSERV) | |
198 | sp = NULL; | |
199 | else { | |
200 | sp = getservbyport(port, | |
201 | (flags & NI_DGRAM) ? "udp" : "tcp"); | |
202 | } | |
203 | if (sp) { | |
204 | if (strlen(sp->s_name) + 1 > servlen) | |
205 | return EAI_OVERFLOW; | |
206 | strncpy(serv, sp->s_name, servlen); | |
207 | } else { | |
208 | snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); | |
209 | if (strlen(numserv) + 1 > servlen) | |
210 | return EAI_OVERFLOW; | |
211 | strncpy(serv, numserv, servlen); | |
212 | } | |
213 | } | |
214 | ||
215 | switch (sa->sa_family) { | |
216 | case AF_INET: | |
09aabd84 | 217 | v4a = (uint32_t) |
26ac0430 AJ |
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; | |
222 | if (v4a == 0) | |
223 | flags |= NI_NUMERICHOST; | |
224 | break; | |
32d002cb | 225 | #if INET6 |
26ac0430 AJ |
226 | case AF_INET6: { |
227 | const struct sockaddr_in6 *sin6; | |
228 | sin6 = (const struct sockaddr_in6 *)sa; | |
229 | switch (sin6->sin6_addr.s6_addr[0]) { | |
230 | case 0x00: | |
231 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) | |
232 | ; | |
233 | else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) | |
234 | ; | |
235 | else | |
236 | flags |= NI_NUMERICHOST; | |
237 | break; | |
238 | default: | |
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; | |
243 | break; | |
244 | } | |
245 | } | |
246 | break; | |
0e076fb1 | 247 | #endif |
26ac0430 AJ |
248 | } |
249 | if (host == NULL || hostlen == 0) { | |
250 | /* | |
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. | |
255 | */ | |
256 | } else if (flags & NI_NUMERICHOST) { | |
257 | /* NUMERICHOST and NAMEREQD conflicts with each other */ | |
258 | if (flags & NI_NAMEREQD) | |
259 | return EAI_NONAME; | |
260 | ||
261 | goto numeric; | |
262 | } else { | |
32d002cb | 263 | #if USE_GETIPNODEBY |
5b10eaba | 264 | int h_error = 0; |
26ac0430 | 265 | hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error); |
0e076fb1 | 266 | #else |
26ac0430 | 267 | hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); |
5b10eaba | 268 | #if 0 // getnameinfo.c:161:9: error: variable 'h_error' set but not used |
32d002cb | 269 | #if HAVE_H_ERRNO |
26ac0430 | 270 | h_error = h_errno; |
0e076fb1 | 271 | #else |
26ac0430 | 272 | h_error = EINVAL; |
0e076fb1 | 273 | #endif |
5b10eaba | 274 | #endif /* 0 */ |
0e076fb1 | 275 | #endif |
276 | ||
26ac0430 | 277 | if (hp) { |
0e076fb1 | 278 | #if 0 |
26ac0430 AJ |
279 | if (flags & NI_NOFQDN) { |
280 | /* | |
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. | |
291 | */ | |
292 | char *p; | |
293 | p = strchr(hp->h_name, '.'); | |
294 | if (p) | |
295 | *p = '\0'; | |
296 | } | |
0e076fb1 | 297 | #endif |
26ac0430 | 298 | if (strlen(hp->h_name) + 1 > hostlen) { |
32d002cb | 299 | #if USE_GETIPNODEBY |
26ac0430 | 300 | freehostent(hp); |
0e076fb1 | 301 | #endif |
26ac0430 AJ |
302 | return EAI_OVERFLOW; |
303 | } | |
304 | strncpy(host, hp->h_name, hostlen); | |
32d002cb | 305 | #if USE_GETIPNODEBY |
26ac0430 | 306 | freehostent(hp); |
0e076fb1 | 307 | #endif |
26ac0430 AJ |
308 | } else { |
309 | if (flags & NI_NAMEREQD) | |
310 | return EAI_NONAME; | |
0e076fb1 | 311 | |
26ac0430 AJ |
312 | numeric: |
313 | switch (afd->a_af) { | |
32d002cb | 314 | #if INET6 |
26ac0430 AJ |
315 | case AF_INET6: { |
316 | int error; | |
317 | ||
318 | if ((error = ip6_parsenumeric(sa, addr, host, | |
319 | hostlen, | |
320 | flags)) != 0) | |
321 | return(error); | |
322 | break; | |
323 | } | |
0e076fb1 | 324 | #endif |
26ac0430 | 325 | default: |
27bc2077 | 326 | if (inet_ntop(afd->a_af, addr, host, |
b1c8a478 | 327 | hostlen) == NULL) |
26ac0430 AJ |
328 | return EAI_SYSTEM; |
329 | break; | |
330 | } | |
331 | } | |
332 | } | |
333 | return(0); | |
0e076fb1 | 334 | } |
335 | ||
32d002cb | 336 | #if INET6 |
0e076fb1 | 337 | static int |
338 | ip6_parsenumeric(sa, addr, host, hostlen, flags) | |
26ac0430 AJ |
339 | const struct sockaddr *sa; |
340 | const char *addr; | |
341 | char *host; | |
342 | size_t hostlen; | |
343 | int flags; | |
0e076fb1 | 344 | { |
26ac0430 AJ |
345 | int numaddrlen; |
346 | char numaddr[512]; | |
347 | ||
27bc2077 | 348 | if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) |
26ac0430 AJ |
349 | return EAI_SYSTEM; |
350 | ||
351 | numaddrlen = strlen(numaddr); | |
352 | if (numaddrlen + 1 > hostlen) /* don't forget terminator */ | |
353 | return EAI_OVERFLOW; | |
354 | strncpy(host, numaddr, hostlen); | |
355 | ||
356 | if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) { | |
357 | char zonebuf[SQUIDHOSTNAMELEN]; | |
358 | int zonelen; | |
359 | ||
360 | zonelen = ip6_sa2str( | |
361 | (const struct sockaddr_in6 *)(const void *)sa, | |
362 | zonebuf, sizeof(zonebuf), flags); | |
363 | if (zonelen < 0) | |
364 | return EAI_OVERFLOW; | |
365 | if (zonelen + 1 + numaddrlen + 1 > hostlen) | |
366 | return EAI_OVERFLOW; | |
367 | ||
368 | /* construct <numeric-addr><delim><zoneid> */ | |
369 | memcpy(host + numaddrlen + 1, zonebuf, | |
370 | (size_t)zonelen); | |
371 | host[numaddrlen] = SCOPE_DELIMITER; | |
372 | host[numaddrlen + 1 + zonelen] = '\0'; | |
373 | } | |
374 | ||
375 | return 0; | |
0e076fb1 | 376 | } |
377 | ||
378 | /* ARGSUSED */ | |
379 | static int | |
380 | ip6_sa2str(sa6, buf, bufsiz, flags) | |
26ac0430 AJ |
381 | const struct sockaddr_in6 *sa6; |
382 | char *buf; | |
383 | size_t bufsiz; | |
384 | int flags; | |
0e076fb1 | 385 | { |
26ac0430 AJ |
386 | unsigned int ifindex; |
387 | const struct in6_addr *a6; | |
388 | int n; | |
0e076fb1 | 389 | |
26ac0430 AJ |
390 | ifindex = (unsigned int)sa6->sin6_scope_id; |
391 | a6 = &sa6->sin6_addr; | |
0e076fb1 | 392 | |
32d002cb | 393 | #if NI_NUMERICSCOPE |
26ac0430 AJ |
394 | if ((flags & NI_NUMERICSCOPE) != 0) { |
395 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | |
396 | if (n < 0 || n >= bufsiz) | |
397 | return -1; | |
398 | else | |
399 | return n; | |
400 | } | |
0e076fb1 | 401 | #endif |
402 | ||
26ac0430 AJ |
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); | |
407 | if (p) | |
408 | return (strlen(p)); | |
409 | } | |
410 | ||
411 | /* last resort */ | |
412 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | |
413 | if (n < 0 || n >= bufsiz) | |
414 | return -1; | |
415 | else | |
416 | return n; | |
0e076fb1 | 417 | } |
418 | #endif /* INET6 */ | |
55d7d5e9 | 419 | #endif /* HAVE_DECL_GETNAMEINFO */ |
f53969cc | 420 |