getnameinfo.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 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 library 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  * 13-Jan-2015 : Various fixed for C++ and MinGW native build
26  *
27  * Original License and code follows.
28  */
29 #include "squid.h"
30 
31 /* KAME: getnameinfo.c,v 1.72 2005/01/13 04:12:03 itojun Exp */
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 
86 #if !HAVE_DECL_GETNAMEINFO
87 
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 
122 #if _SQUID_WINDOWS_
123 #undef IN_ADDR
124 #include <ws2tcpip.h>
125 #endif
126 
127 static const struct afd {
128  int a_af;
131  int a_off;
133 } afdl [] = {
134 #if INET6
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)
138  },
139 #endif
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  },
144  {0, 0, 0, 0, 0},
145 };
146 
147 #if INET6
148 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
149  size_t, int));
150 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
151 #endif
152 
153 int
154 xgetnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
155 {
156  const struct afd *afd;
157  struct servent *sp;
158  struct hostent *hp;
159  unsigned short port;
160  int family, i;
161  const char *addr;
162  uint32_t v4a;
163  char numserv[512];
164 
165  if (!sa)
166  return EAI_FAIL;
167 
168 #if HAVE_SA_LEN /*XXX*/
169  if (sa->sa_len != salen)
170  return EAI_FAIL;
171 #endif
172 
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 || 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 OR servlen == 0
194  * means that the caller does not want the result.
195  */
196  } else {
197  if (flags & NI_NUMERICSERV)
198  sp = nullptr;
199  else {
201  (flags & NI_DGRAM) ? "udp" : "tcp");
202  }
203  if (sp) {
204  if (strlen(sp->s_name) + 1 > servlen)
205  return EAI_OVERFLOW;
206  xstrncpy(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  xstrncpy(serv, numserv, servlen);
212  }
213  }
214 
215  switch (sa->sa_family) {
216  case AF_INET:
217  v4a = (uint32_t)
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;
225 #if INET6
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;
247 #endif
248  }
249  if (!host || 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 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 {
263 #if USE_GETIPNODEBY
264  int h_error = 0;
265  hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
266 #else
267  hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
268 #endif
269 
270  if (hp) {
271  if (strlen(hp->h_name) + 1 > hostlen) {
272 #if USE_GETIPNODEBY
273  freehostent(hp);
274 #endif
275  return EAI_OVERFLOW;
276  }
277  xstrncpy(host, hp->h_name, hostlen);
278 #if USE_GETIPNODEBY
279  freehostent(hp);
280 #endif
281  } else {
282  if (flags & NI_NAMEREQD)
283  return EAI_NONAME;
284 
285 numeric:
286  switch (afd->a_af) {
287 #if INET6
288  case AF_INET6: {
289  int error;
290 
291  if ((error = ip6_parsenumeric(sa, addr, host,
292  hostlen,
293  flags)) != 0)
294  return(error);
295  break;
296  }
297 #endif
298  default:
299  if (!inet_ntop(afd->a_af, addr, host, hostlen))
300  return EAI_SYSTEM;
301  break;
302  }
303  }
304  }
305  return(0);
306 }
307 
308 #if INET6
309 static int
310 ip6_parsenumeric(sa, addr, host, hostlen, flags)
311 const struct sockaddr *sa;
312 const char *addr;
313 char *host;
314 size_t hostlen;
315 int flags;
316 {
317  int numaddrlen;
318  char numaddr[512];
319 
320  if (!inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)))
321  return EAI_SYSTEM;
322 
323  numaddrlen = strlen(numaddr);
324  if (numaddrlen + 1 > hostlen) /* don't forget terminator */
325  return EAI_OVERFLOW;
326  xstrncpy(host, numaddr, hostlen);
327 
328  if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
329  char zonebuf[SQUIDHOSTNAMELEN];
330  int zonelen;
331 
332  zonelen = ip6_sa2str(
333  (const struct sockaddr_in6 *)(const void *)sa,
334  zonebuf, sizeof(zonebuf), flags);
335  if (zonelen < 0)
336  return EAI_OVERFLOW;
337  if (zonelen + 1 + numaddrlen + 1 > hostlen)
338  return EAI_OVERFLOW;
339 
340  /* construct <numeric-addr><delim><zoneid> */
341  memcpy(host + numaddrlen + 1, zonebuf,
342  (size_t)zonelen);
343  host[numaddrlen] = SCOPE_DELIMITER;
344  host[numaddrlen + 1 + zonelen] = '\0';
345  }
346 
347  return 0;
348 }
349 
350 /* ARGSUSED */
351 static int
352 ip6_sa2str(sa6, buf, bufsiz, flags)
353 const struct sockaddr_in6 *sa6;
354 char *buf;
355 size_t bufsiz;
356 int flags;
357 {
358  unsigned int ifindex;
359  const struct in6_addr *a6;
360  int n;
361 
362  ifindex = (unsigned int)sa6->sin6_scope_id;
363  a6 = &sa6->sin6_addr;
364 
365 #if NI_NUMERICSCOPE
366  if ((flags & NI_NUMERICSCOPE) != 0) {
367  n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
368  if (n < 0 || n >= bufsiz)
369  return -1;
370  else
371  return n;
372  }
373 #endif
374 
375  /* if_indextoname() does not take buffer size. not a good api... */
376  if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
377  IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
378  char *p = if_indextoname(ifindex, buf);
379  if (p)
380  return (strlen(p));
381  }
382 
383  /* last resort */
384  n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
385  if (n < 0 || n >= bufsiz)
386  return -1;
387  else
388  return n;
389 }
390 #endif /* INET6 */
391 #endif /* HAVE_DECL_GETNAMEINFO */
392 
struct hostent * getipnodebyaddr()
void freehostent()
#define NI_NUMERICSERV
#define NI_DGRAM
#define EAI_SYSTEM
Definition: getaddrinfo.h:56
void error(char *format,...)
#define EAI_OVERFLOW
Definition: getaddrinfo.h:60
int a_socklen
Definition: getnameinfo.cc:130
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
#define NI_NUMERICHOST
int a_off
Definition: getnameinfo.cc:131
static int port
Definition: ldap_backend.cc:69
#define IN_EXPERIMENTAL(a)
Definition: getaddrinfo.h:64
int socklen_t
Definition: types.h:158
#define EAI_FAMILY
int a_addrlen
Definition: getnameinfo.cc:129
#define SCOPE_DELIMITER
#define EAI_NONAME
int xgetnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
Definition: getnameinfo.cc:154
struct servent * getservbyport()
int a_af
Definition: getnameinfo.cc:128
struct _sp sp
#define NI_NUMERICSCOPE
#define EAI_FAIL
#define inet_ntop
Definition: inet_ntop.h:25
int a_portoff
Definition: getnameinfo.cc:132
#define NI_NAMEREQD
static const struct afd afdl[]
struct hostent * gethostbyaddr()
#define SQUIDHOSTNAMELEN
Definition: rfc2181.h:30
int unsigned int
Definition: stub_fd.cc:19

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors