]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/getnameinfo.cc
SourceFormat Enforcement
[thirdparty/squid.git] / compat / getnameinfo.cc
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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
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 *
25 * Original License and code follows.
26 */
27 #include "squid.h"
28
29 /* KAME: getnameinfo.c,v 1.72 2005/01/13 04:12:03 itojun Exp */
30
31 /*
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
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.
46 *
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
57 * SUCH DAMAGE.
58 */
59
60 /*
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")?
72 */
73
74 /*
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.
82 */
83
84 #if !HAVE_DECL_GETNAMEINFO
85
86 #if HAVE_SYS_SOCKET_H
87 #include <sys/socket.h>
88 #endif
89 #if HAVE_NET_IF_H
90 #include <net/if.h>
91 #endif
92 #if HAVE_NETINET_IN_H
93 #include <netinet/in.h>
94 #endif
95 #if HAVE_ARPA_INET_H
96 #include <arpa/inet.h>
97 #endif
98 #if HAVE_ARPA_NAMESER_H
99 #include <arpa/nameser.h>
100 #endif
101 #if HAVE_NETDB_H
102 #include <netdb.h>
103 #endif
104 #if HAVE_RESOLV_H
105 #include <resolv.h>
106 #endif
107 #if HAVE_STRING_H
108 #include <string.h>
109 #endif
110 #if HAVE_STDDEF_H
111 #include <stddef.h>
112 #endif
113 #if HAVE_ERRNO_H
114 #include <errno.h>
115 #endif
116 #if HAVE_INTTYPES_H
117 #include <inttypes.h>
118 #endif
119
120 #if _SQUID_WINDOWS_
121 #undef IN_ADDR
122 #include <ws2tcpip.h>
123 #endif
124
125 static const struct afd {
126 int a_af;
127 int a_addrlen;
128 int a_socklen;
129 int a_off;
130 int a_portoff;
131 } afdl [] = {
132 #if INET6
133 { PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
134 offsetof(struct sockaddr_in6, sin6_addr),
135 offsetof(struct sockaddr_in6, sin6_port)
136 },
137 #endif
138 { PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
139 offsetof(struct sockaddr_in, sin_addr),
140 offsetof(struct sockaddr_in, sin_port)
141 },
142 {0, 0, 0, 0, 0},
143 };
144
145 #if INET6
146 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
147 size_t, int));
148 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
149 #endif
150
151 int
152 xgetnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
153 const struct sockaddr *sa;
154 socklen_t salen;
155 char *host;
156 size_t hostlen;
157 char *serv;
158 size_t servlen;
159 int flags;
160 {
161 const struct afd *afd;
162 struct servent *sp;
163 struct hostent *hp;
164 unsigned short port;
165 int family, i;
166 const char *addr;
167 uint32_t v4a;
168 char numserv[512];
169
170 if (sa == NULL)
171 return EAI_FAIL;
172
173 #if HAVE_SA_LEN /*XXX*/
174 if (sa->sa_len != salen)
175 return EAI_FAIL;
176 #endif
177
178 family = sa->sa_family;
179 for (i = 0; afdl[i].a_af; i++)
180 if (afdl[i].a_af == family) {
181 afd = &afdl[i];
182 goto found;
183 }
184 return EAI_FAMILY;
185
186 found:
187 if (salen != afd->a_socklen)
188 return EAI_FAIL;
189
190 /* network byte order */
191 memcpy(&port, (const char *)sa + afd->a_portoff, sizeof(port));
192 addr = (const char *)sa + afd->a_off;
193
194 if (serv == NULL || servlen == 0) {
195 /*
196 * do nothing in this case.
197 * in case you are wondering if "&&" is more correct than
198 * "||" here: RFC3493 says that serv == NULL OR servlen == 0
199 * means that the caller does not want the result.
200 */
201 } else {
202 if (flags & NI_NUMERICSERV)
203 sp = NULL;
204 else {
205 sp = getservbyport(port,
206 (flags & NI_DGRAM) ? "udp" : "tcp");
207 }
208 if (sp) {
209 if (strlen(sp->s_name) + 1 > servlen)
210 return EAI_OVERFLOW;
211 strncpy(serv, sp->s_name, servlen);
212 } else {
213 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
214 if (strlen(numserv) + 1 > servlen)
215 return EAI_OVERFLOW;
216 strncpy(serv, numserv, servlen);
217 }
218 }
219
220 switch (sa->sa_family) {
221 case AF_INET:
222 v4a = (uint32_t)
223 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
224 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
225 flags |= NI_NUMERICHOST;
226 v4a >>= IN_CLASSA_NSHIFT;
227 if (v4a == 0)
228 flags |= NI_NUMERICHOST;
229 break;
230 #if INET6
231 case AF_INET6: {
232 const struct sockaddr_in6 *sin6;
233 sin6 = (const struct sockaddr_in6 *)sa;
234 switch (sin6->sin6_addr.s6_addr[0]) {
235 case 0x00:
236 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
237 ;
238 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
239 ;
240 else
241 flags |= NI_NUMERICHOST;
242 break;
243 default:
244 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
245 flags |= NI_NUMERICHOST;
246 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
247 flags |= NI_NUMERICHOST;
248 break;
249 }
250 }
251 break;
252 #endif
253 }
254 if (host == NULL || hostlen == 0) {
255 /*
256 * do nothing in this case.
257 * in case you are wondering if "&&" is more correct than
258 * "||" here: RFC3493 says that host == NULL or hostlen == 0
259 * means that the caller does not want the result.
260 */
261 } else if (flags & NI_NUMERICHOST) {
262 /* NUMERICHOST and NAMEREQD conflicts with each other */
263 if (flags & NI_NAMEREQD)
264 return EAI_NONAME;
265
266 goto numeric;
267 } else {
268 #if USE_GETIPNODEBY
269 int h_error = 0;
270 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
271 #else
272 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
273 #if 0 // getnameinfo.c:161:9: error: variable 'h_error' set but not used
274 #if HAVE_H_ERRNO
275 h_error = h_errno;
276 #else
277 h_error = EINVAL;
278 #endif
279 #endif /* 0 */
280 #endif
281
282 if (hp) {
283 #if 0
284 if (flags & NI_NOFQDN) {
285 /*
286 * According to RFC3493 section 6.2, NI_NOFQDN
287 * means "node name portion of the FQDN shall
288 * be returned for local hosts." The following
289 * code tries to implement it by returning the
290 * first label (the part before the first
291 * period) of the FQDN. However, it is not
292 * clear if this always makes sense, since the
293 * given address may be outside of "local
294 * hosts." Due to the unclear description, we
295 * disable the code in this implementation.
296 */
297 char *p;
298 p = strchr(hp->h_name, '.');
299 if (p)
300 *p = '\0';
301 }
302 #endif
303 if (strlen(hp->h_name) + 1 > hostlen) {
304 #if USE_GETIPNODEBY
305 freehostent(hp);
306 #endif
307 return EAI_OVERFLOW;
308 }
309 strncpy(host, hp->h_name, hostlen);
310 #if USE_GETIPNODEBY
311 freehostent(hp);
312 #endif
313 } else {
314 if (flags & NI_NAMEREQD)
315 return EAI_NONAME;
316
317 numeric:
318 switch (afd->a_af) {
319 #if INET6
320 case AF_INET6: {
321 int error;
322
323 if ((error = ip6_parsenumeric(sa, addr, host,
324 hostlen,
325 flags)) != 0)
326 return(error);
327 break;
328 }
329 #endif
330 default:
331 if (inet_ntop(afd->a_af, addr, host,
332 hostlen) == NULL)
333 return EAI_SYSTEM;
334 break;
335 }
336 }
337 }
338 return(0);
339 }
340
341 #if INET6
342 static int
343 ip6_parsenumeric(sa, addr, host, hostlen, flags)
344 const struct sockaddr *sa;
345 const char *addr;
346 char *host;
347 size_t hostlen;
348 int flags;
349 {
350 int numaddrlen;
351 char numaddr[512];
352
353 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
354 return EAI_SYSTEM;
355
356 numaddrlen = strlen(numaddr);
357 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
358 return EAI_OVERFLOW;
359 strncpy(host, numaddr, hostlen);
360
361 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
362 char zonebuf[SQUIDHOSTNAMELEN];
363 int zonelen;
364
365 zonelen = ip6_sa2str(
366 (const struct sockaddr_in6 *)(const void *)sa,
367 zonebuf, sizeof(zonebuf), flags);
368 if (zonelen < 0)
369 return EAI_OVERFLOW;
370 if (zonelen + 1 + numaddrlen + 1 > hostlen)
371 return EAI_OVERFLOW;
372
373 /* construct <numeric-addr><delim><zoneid> */
374 memcpy(host + numaddrlen + 1, zonebuf,
375 (size_t)zonelen);
376 host[numaddrlen] = SCOPE_DELIMITER;
377 host[numaddrlen + 1 + zonelen] = '\0';
378 }
379
380 return 0;
381 }
382
383 /* ARGSUSED */
384 static int
385 ip6_sa2str(sa6, buf, bufsiz, flags)
386 const struct sockaddr_in6 *sa6;
387 char *buf;
388 size_t bufsiz;
389 int flags;
390 {
391 unsigned int ifindex;
392 const struct in6_addr *a6;
393 int n;
394
395 ifindex = (unsigned int)sa6->sin6_scope_id;
396 a6 = &sa6->sin6_addr;
397
398 #if NI_NUMERICSCOPE
399 if ((flags & NI_NUMERICSCOPE) != 0) {
400 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
401 if (n < 0 || n >= bufsiz)
402 return -1;
403 else
404 return n;
405 }
406 #endif
407
408 /* if_indextoname() does not take buffer size. not a good api... */
409 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
410 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
411 char *p = if_indextoname(ifindex, buf);
412 if (p)
413 return (strlen(p));
414 }
415
416 /* last resort */
417 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
418 if (n < 0 || n >= bufsiz)
419 return -1;
420 else
421 return n;
422 }
423 #endif /* INET6 */
424 #endif /* HAVE_DECL_GETNAMEINFO */
425