getnameinfo.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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  * 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 == NULL)
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 == 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:
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 == 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 {
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 #if 0 // getnameinfo.c:161:9: error: variable 'h_error' set but not used
269 #if HAVE_H_ERRNO
270  h_error = h_errno;
271 #else
272  h_error = EINVAL;
273 #endif
274 #endif /* 0 */
275 #endif
276 
277  if (hp) {
278 #if 0
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  }
297 #endif
298  if (strlen(hp->h_name) + 1 > hostlen) {
299 #if USE_GETIPNODEBY
300  freehostent(hp);
301 #endif
302  return EAI_OVERFLOW;
303  }
304  strncpy(host, hp->h_name, hostlen);
305 #if USE_GETIPNODEBY
306  freehostent(hp);
307 #endif
308  } else {
309  if (flags & NI_NAMEREQD)
310  return EAI_NONAME;
311 
312 numeric:
313  switch (afd->a_af) {
314 #if INET6
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  }
324 #endif
325  default:
326  if (inet_ntop(afd->a_af, addr, host,
327  hostlen) == NULL)
328  return EAI_SYSTEM;
329  break;
330  }
331  }
332  }
333  return(0);
334 }
335 
336 #if INET6
337 static int
338 ip6_parsenumeric(sa, addr, host, hostlen, flags)
339 const struct sockaddr *sa;
340 const char *addr;
341 char *host;
342 size_t hostlen;
343 int flags;
344 {
345  int numaddrlen;
346  char numaddr[512];
347 
348  if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
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;
376 }
377 
378 /* ARGSUSED */
379 static int
380 ip6_sa2str(sa6, buf, bufsiz, flags)
381 const struct sockaddr_in6 *sa6;
382 char *buf;
383 size_t bufsiz;
384 int flags;
385 {
386  unsigned int ifindex;
387  const struct in6_addr *a6;
388  int n;
389 
390  ifindex = (unsigned int)sa6->sin6_scope_id;
391  a6 = &sa6->sin6_addr;
392 
393 #if NI_NUMERICSCOPE
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  }
401 #endif
402 
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;
417 }
418 #endif /* INET6 */
419 #endif /* HAVE_DECL_GETNAMEINFO */
420 
int socklen_t
Definition: types.h:158
#define EAI_SYSTEM
Definition: getaddrinfo.h:88
struct hostent * gethostbyaddr()
#define EAI_FAIL
void error(char *format,...)
int i
Definition: membanger.c:49
int a_af
Definition: getnameinfo.cc:128
int a_portoff
Definition: getnameinfo.cc:132
char * p
Definition: membanger.c:43
int a_socklen
Definition: getnameinfo.cc:130
struct hostent * getipnodebyaddr()
#define IN_EXPERIMENTAL(a)
Definition: getaddrinfo.h:96
#define NI_NUMERICSERV
#define NI_NOFQDN
#define NI_NAMEREQD
#define inet_ntop
Definition: inet_ntop.h:25
#define SCOPE_DELIMITER
void * addr
Definition: membanger.c:46
static int port
Definition: ldap_backend.cc:69
#define NI_DGRAM
#define NI_NUMERICHOST
bool SIGHDLR int STUB void int
Definition: stub_tools.cc:68
int a_off
Definition: getnameinfo.cc:131
struct _sp sp
#define EAI_NONAME
int a_addrlen
Definition: getnameinfo.cc:129
void freehostent()
#define SQUIDHOSTNAMELEN
Definition: rfc2181.h:30
static const struct afd afdl[]
#define EAI_OVERFLOW
Definition: getaddrinfo.h:92
#define EAI_FAMILY
struct servent * getservbyport()
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
int h_errno
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors