]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/getnameinfo.c
SourceFormat Enforcement
[thirdparty/squid.git] / compat / getnameinfo.c
1 /*
2 * Copyright (C) 1996-2014 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_GETNAMEINFO
85
86 #include "compat/getaddrinfo.h"
87 #include "compat/inet_ntop.h"
88
89 #if HAVE_SYS_SOCKET_H
90 #include <sys/socket.h>
91 #endif
92 #if HAVE_NET_IF_H
93 #include <net/if.h>
94 #endif
95 #if HAVE_NETINET_IN_H
96 #include <netinet/in.h>
97 #endif
98 #if HAVE_ARPA_INET_H
99 #include <arpa/inet.h>
100 #endif
101 #if HAVE_ARPA_NAMESER_H
102 #include <arpa/nameser.h>
103 #endif
104 #if HAVE_NETDB_H
105 #include <netdb.h>
106 #endif
107 #if HAVE_RESOLV_H
108 #include <resolv.h>
109 #endif
110 #if HAVE_STRING_H
111 #include <string.h>
112 #endif
113 #if HAVE_STDDEF_H
114 #include <stddef.h>
115 #endif
116 #if HAVE_ERRNO_H
117 #include <errno.h>
118 #endif
119 #if HAVE_INTTYPES_H
120 #include <inttypes.h>
121 #endif
122
123 #if _SQUID_WINDOWS_
124 #undef IN_ADDR
125 #include <ws2tcpip.h>
126 #endif
127
128 static const struct afd {
129 int a_af;
130 int a_addrlen;
131 int a_socklen;
132 int a_off;
133 int a_portoff;
134 } afdl [] = {
135 #if INET6
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)
139 },
140 #endif
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)
144 },
145 {0, 0, 0, 0, 0},
146 };
147
148 #if INET6
149 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
150 size_t, int));
151 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
152 #endif
153
154 int
155 xgetnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
156 const struct sockaddr *sa;
157 socklen_t salen;
158 char *host;
159 size_t hostlen;
160 char *serv;
161 size_t servlen;
162 int flags;
163 {
164 const struct afd *afd;
165 struct servent *sp;
166 struct hostent *hp;
167 unsigned short port;
168 int family, i;
169 const char *addr;
170 uint32_t v4a;
171 char numserv[512];
172
173 if (sa == NULL)
174 return EAI_FAIL;
175
176 #if HAVE_SA_LEN /*XXX*/
177 if (sa->sa_len != salen)
178 return EAI_FAIL;
179 #endif
180
181 family = sa->sa_family;
182 for (i = 0; afdl[i].a_af; i++)
183 if (afdl[i].a_af == family) {
184 afd = &afdl[i];
185 goto found;
186 }
187 return EAI_FAMILY;
188
189 found:
190 if (salen != afd->a_socklen)
191 return EAI_FAIL;
192
193 /* network byte order */
194 memcpy(&port, (const char *)sa + afd->a_portoff, sizeof(port));
195 addr = (const char *)sa + afd->a_off;
196
197 if (serv == NULL || servlen == 0) {
198 /*
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.
203 */
204 } else {
205 if (flags & NI_NUMERICSERV)
206 sp = NULL;
207 else {
208 sp = getservbyport(port,
209 (flags & NI_DGRAM) ? "udp" : "tcp");
210 }
211 if (sp) {
212 if (strlen(sp->s_name) + 1 > servlen)
213 return EAI_OVERFLOW;
214 strncpy(serv, sp->s_name, servlen);
215 } else {
216 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
217 if (strlen(numserv) + 1 > servlen)
218 return EAI_OVERFLOW;
219 strncpy(serv, numserv, servlen);
220 }
221 }
222
223 switch (sa->sa_family) {
224 case AF_INET:
225 v4a = (uint32_t)
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;
230 if (v4a == 0)
231 flags |= NI_NUMERICHOST;
232 break;
233 #if INET6
234 case AF_INET6: {
235 const struct sockaddr_in6 *sin6;
236 sin6 = (const struct sockaddr_in6 *)sa;
237 switch (sin6->sin6_addr.s6_addr[0]) {
238 case 0x00:
239 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
240 ;
241 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
242 ;
243 else
244 flags |= NI_NUMERICHOST;
245 break;
246 default:
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;
251 break;
252 }
253 }
254 break;
255 #endif
256 }
257 if (host == NULL || hostlen == 0) {
258 /*
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.
263 */
264 } else if (flags & NI_NUMERICHOST) {
265 /* NUMERICHOST and NAMEREQD conflicts with each other */
266 if (flags & NI_NAMEREQD)
267 return EAI_NONAME;
268
269 goto numeric;
270 } else {
271 #if USE_GETIPNODEBY
272 int h_error = 0;
273 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
274 #else
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
277 #if HAVE_H_ERRNO
278 h_error = h_errno;
279 #else
280 h_error = EINVAL;
281 #endif
282 #endif /* 0 */
283 #endif
284
285 if (hp) {
286 #if 0
287 if (flags & NI_NOFQDN) {
288 /*
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.
299 */
300 char *p;
301 p = strchr(hp->h_name, '.');
302 if (p)
303 *p = '\0';
304 }
305 #endif
306 if (strlen(hp->h_name) + 1 > hostlen) {
307 #if USE_GETIPNODEBY
308 freehostent(hp);
309 #endif
310 return EAI_OVERFLOW;
311 }
312 strncpy(host, hp->h_name, hostlen);
313 #if USE_GETIPNODEBY
314 freehostent(hp);
315 #endif
316 } else {
317 if (flags & NI_NAMEREQD)
318 return EAI_NONAME;
319
320 numeric:
321 switch (afd->a_af) {
322 #if INET6
323 case AF_INET6: {
324 int error;
325
326 if ((error = ip6_parsenumeric(sa, addr, host,
327 hostlen,
328 flags)) != 0)
329 return(error);
330 break;
331 }
332 #endif
333 default:
334 if (inet_ntop(afd->a_af, addr, host,
335 hostlen) == NULL)
336 return EAI_SYSTEM;
337 break;
338 }
339 }
340 }
341 return(0);
342 }
343
344 #if INET6
345 static int
346 ip6_parsenumeric(sa, addr, host, hostlen, flags)
347 const struct sockaddr *sa;
348 const char *addr;
349 char *host;
350 size_t hostlen;
351 int flags;
352 {
353 int numaddrlen;
354 char numaddr[512];
355
356 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
357 return EAI_SYSTEM;
358
359 numaddrlen = strlen(numaddr);
360 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
361 return EAI_OVERFLOW;
362 strncpy(host, numaddr, hostlen);
363
364 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
365 char zonebuf[SQUIDHOSTNAMELEN];
366 int zonelen;
367
368 zonelen = ip6_sa2str(
369 (const struct sockaddr_in6 *)(const void *)sa,
370 zonebuf, sizeof(zonebuf), flags);
371 if (zonelen < 0)
372 return EAI_OVERFLOW;
373 if (zonelen + 1 + numaddrlen + 1 > hostlen)
374 return EAI_OVERFLOW;
375
376 /* construct <numeric-addr><delim><zoneid> */
377 memcpy(host + numaddrlen + 1, zonebuf,
378 (size_t)zonelen);
379 host[numaddrlen] = SCOPE_DELIMITER;
380 host[numaddrlen + 1 + zonelen] = '\0';
381 }
382
383 return 0;
384 }
385
386 /* ARGSUSED */
387 static int
388 ip6_sa2str(sa6, buf, bufsiz, flags)
389 const struct sockaddr_in6 *sa6;
390 char *buf;
391 size_t bufsiz;
392 int flags;
393 {
394 unsigned int ifindex;
395 const struct in6_addr *a6;
396 int n;
397
398 ifindex = (unsigned int)sa6->sin6_scope_id;
399 a6 = &sa6->sin6_addr;
400
401 #if NI_NUMERICSCOPE
402 if ((flags & NI_NUMERICSCOPE) != 0) {
403 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
404 if (n < 0 || n >= bufsiz)
405 return -1;
406 else
407 return n;
408 }
409 #endif
410
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);
415 if (p)
416 return (strlen(p));
417 }
418
419 /* last resort */
420 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
421 if (n < 0 || n >= bufsiz)
422 return -1;
423 else
424 return n;
425 }
426 #endif /* INET6 */
427 #endif
428