hostip4.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 
23 #include "curl_setup.h"
24 
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
27 #endif
28 #ifdef HAVE_NETDB_H
29 #include <netdb.h>
30 #endif
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
33 #endif
34 #ifdef __VMS
35 #include <in.h>
36 #include <inet.h>
37 #endif
38 
39 #ifdef HAVE_PROCESS_H
40 #include <process.h>
41 #endif
42 
43 #include "urldata.h"
44 #include "sendf.h"
45 #include "hostip.h"
46 #include "hash.h"
47 #include "share.h"
48 #include "strerror.h"
49 #include "url.h"
50 #include "inet_pton.h"
51 /* The last 3 #include files should be in this order */
52 #include "curl_printf.h"
53 #include "curl_memory.h"
54 #include "memdebug.h"
55 
56 /***********************************************************************
57  * Only for plain IPv4 builds
58  **********************************************************************/
59 #ifdef CURLRES_IPV4 /* plain IPv4 code coming up */
60 /*
61  * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
62  * been set and returns TRUE if they are OK.
63  */
64 bool Curl_ipvalid(struct connectdata *conn)
65 {
66  if(conn->ip_version == CURL_IPRESOLVE_V6)
67  /* An IPv6 address was requested and we can't get/use one */
68  return FALSE;
69 
70  return TRUE; /* OK, proceed */
71 }
72 
73 #ifdef CURLRES_SYNCH
74 
75 /*
76  * Curl_getaddrinfo() - the IPv4 synchronous version.
77  *
78  * The original code to this function was from the Dancer source code, written
79  * by Bjorn Reese, it has since been patched and modified considerably.
80  *
81  * gethostbyname_r() is the thread-safe version of the gethostbyname()
82  * function. When we build for plain IPv4, we attempt to use this
83  * function. There are _three_ different gethostbyname_r() versions, and we
84  * detect which one this platform supports in the configure script and set up
85  * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or
86  * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME
87  * has the corresponding rules. This is primarily on *nix. Note that some unix
88  * flavours have thread-safe versions of the plain gethostbyname() etc.
89  *
90  */
92  const char *hostname,
93  int port,
94  int *waitp)
95 {
96  Curl_addrinfo *ai = NULL;
97 
98 #ifdef CURL_DISABLE_VERBOSE_STRINGS
99  (void)conn;
100 #endif
101 
102  *waitp = 0; /* synchronous response only */
103 
104  ai = Curl_ipv4_resolve_r(hostname, port);
105  if(!ai)
106  infof(conn->data, "Curl_ipv4_resolve_r failed for %s\n", hostname);
107 
108  return ai;
109 }
110 #endif /* CURLRES_SYNCH */
111 #endif /* CURLRES_IPV4 */
112 
113 #if defined(CURLRES_IPV4) && !defined(CURLRES_ARES)
114 
115 /*
116  * Curl_ipv4_resolve_r() - ipv4 threadsafe resolver function.
117  *
118  * This is used for both synchronous and asynchronous resolver builds,
119  * implying that only threadsafe code and function calls may be used.
120  *
121  */
123  int port)
124 {
125 #if !defined(HAVE_GETADDRINFO_THREADSAFE) && defined(HAVE_GETHOSTBYNAME_R_3)
126  int res;
127 #endif
128  Curl_addrinfo *ai = NULL;
129  struct hostent *h = NULL;
130  struct in_addr in;
131  struct hostent *buf = NULL;
132 
133  if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
134  /* This is a dotted IP address 123.123.123.123-style */
135  return Curl_ip2addr(AF_INET, &in, hostname, port);
136 
137 #if defined(HAVE_GETADDRINFO_THREADSAFE)
138  else {
139  struct addrinfo hints;
140  char sbuf[12];
141  char *sbufptr = NULL;
142 
143  memset(&hints, 0, sizeof(hints));
144  hints.ai_family = PF_INET;
145  hints.ai_socktype = SOCK_STREAM;
146  if(port) {
147  snprintf(sbuf, sizeof(sbuf), "%d", port);
148  sbufptr = sbuf;
149  }
150 
151  (void)Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &ai);
152 
153 #elif defined(HAVE_GETHOSTBYNAME_R)
154  /*
155  * gethostbyname_r() is the preferred resolve function for many platforms.
156  * Since there are three different versions of it, the following code is
157  * somewhat #ifdef-ridden.
158  */
159  else {
160  int h_errnop;
161 
162  buf = calloc(1, CURL_HOSTENT_SIZE);
163  if(!buf)
164  return NULL; /* major failure */
165  /*
166  * The clearing of the buffer is a workaround for a gethostbyname_r bug in
167  * qnx nto and it is also _required_ for some of these functions on some
168  * platforms.
169  */
170 
171 #if defined(HAVE_GETHOSTBYNAME_R_5)
172  /* Solaris, IRIX and more */
173  h = gethostbyname_r(hostname,
174  (struct hostent *)buf,
175  (char *)buf + sizeof(struct hostent),
176  CURL_HOSTENT_SIZE - sizeof(struct hostent),
177  &h_errnop);
178 
179  /* If the buffer is too small, it returns NULL and sets errno to
180  * ERANGE. The errno is thread safe if this is compiled with
181  * -D_REENTRANT as then the 'errno' variable is a macro defined to get
182  * used properly for threads.
183  */
184 
185  if(h) {
186  ;
187  }
188  else
189 #elif defined(HAVE_GETHOSTBYNAME_R_6)
190  /* Linux */
191 
192  (void)gethostbyname_r(hostname,
193  (struct hostent *)buf,
194  (char *)buf + sizeof(struct hostent),
195  CURL_HOSTENT_SIZE - sizeof(struct hostent),
196  &h, /* DIFFERENCE */
197  &h_errnop);
198  /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a
199  * sudden this function returns EAGAIN if the given buffer size is too
200  * small. Previous versions are known to return ERANGE for the same
201  * problem.
202  *
203  * This wouldn't be such a big problem if older versions wouldn't
204  * sometimes return EAGAIN on a common failure case. Alas, we can't
205  * assume that EAGAIN *or* ERANGE means ERANGE for any given version of
206  * glibc.
207  *
208  * For now, we do that and thus we may call the function repeatedly and
209  * fail for older glibc versions that return EAGAIN, until we run out of
210  * buffer size (step_size grows beyond CURL_HOSTENT_SIZE).
211  *
212  * If anyone has a better fix, please tell us!
213  *
214  * -------------------------------------------------------------------
215  *
216  * On October 23rd 2003, Dan C dug up more details on the mysteries of
217  * gethostbyname_r() in glibc:
218  *
219  * In glibc 2.2.5 the interface is different (this has also been
220  * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't
221  * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32
222  * (shipped/upgraded by Redhat 7.2) don't show this behavior!
223  *
224  * In this "buggy" version, the return code is -1 on error and 'errno'
225  * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a
226  * thread-safe variable.
227  */
228 
229  if(!h) /* failure */
230 #elif defined(HAVE_GETHOSTBYNAME_R_3)
231  /* AIX, Digital Unix/Tru64, HPUX 10, more? */
232 
233  /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of
234  * the plain fact that it does not return unique full buffers on each
235  * call, but instead several of the pointers in the hostent structs will
236  * point to the same actual data! This have the unfortunate down-side that
237  * our caching system breaks down horribly. Luckily for us though, AIX 4.3
238  * and more recent versions have a "completely thread-safe"[*] libc where
239  * all the data is stored in thread-specific memory areas making calls to
240  * the plain old gethostbyname() work fine even for multi-threaded
241  * programs.
242  *
243  * This AIX 4.3 or later detection is all made in the configure script.
244  *
245  * Troels Walsted Hansen helped us work this out on March 3rd, 2003.
246  *
247  * [*] = much later we've found out that it isn't at all "completely
248  * thread-safe", but at least the gethostbyname() function is.
249  */
250 
251  if(CURL_HOSTENT_SIZE >=
252  (sizeof(struct hostent) + sizeof(struct hostent_data))) {
253 
254  /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
255  * that should work! September 20: Richard Prescott worked on the buffer
256  * size dilemma.
257  */
258 
259  res = gethostbyname_r(hostname,
260  (struct hostent *)buf,
261  (struct hostent_data *)((char *)buf +
262  sizeof(struct hostent)));
263  h_errnop = SOCKERRNO; /* we don't deal with this, but set it anyway */
264  }
265  else
266  res = -1; /* failure, too smallish buffer size */
267 
268  if(!res) { /* success */
269 
270  h = buf; /* result expected in h */
271 
272  /* This is the worst kind of the different gethostbyname_r() interfaces.
273  * Since we don't know how big buffer this particular lookup required,
274  * we can't realloc down the huge alloc without doing closer analysis of
275  * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every
276  * name lookup. Fixing this would require an extra malloc() and then
277  * calling Curl_addrinfo_copy() that subsequent realloc()s down the new
278  * memory area to the actually used amount.
279  */
280  }
281  else
282 #endif /* HAVE_...BYNAME_R_5 || HAVE_...BYNAME_R_6 || HAVE_...BYNAME_R_3 */
283  {
284  h = NULL; /* set return code to NULL */
285  free(buf);
286  }
287 #else /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */
288  /*
289  * Here is code for platforms that don't have a thread safe
290  * getaddrinfo() nor gethostbyname_r() function or for which
291  * gethostbyname() is the preferred one.
292  */
293  else {
294  h = gethostbyname((void *)hostname);
295 #endif /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */
296  }
297 
298  if(h) {
299  ai = Curl_he2ai(h, port);
300 
301  if(buf) /* used a *_r() function */
302  free(buf);
303  }
304 
305  return ai;
306 }
307 #endif /* defined(CURLRES_IPV4) && !defined(CURLRES_ARES) */
#define free(ptr)
Definition: curl_memory.h:130
#define CURL_IPRESOLVE_V6
Definition: curl.h:1856
Curl_addrinfo * Curl_getaddrinfo(struct connectdata *conn, const char *hostname, int port, int *waitp)
Definition: hostip4.c:91
#define SOCKERRNO
#define CURL_HOSTENT_SIZE
Definition: hostip.h:44
Curl_addrinfo * Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
static int res
#define FALSE
Curl_addrinfo * Curl_ipv4_resolve_r(const char *hostname, int port)
Definition: hostip4.c:122
int Curl_inet_pton(int af, const char *src, void *dst)
Definition: inet_pton.c:66
static unsigned short port
Definition: sockfilt.c:137
bool Curl_ipvalid(struct connectdata *conn)
Definition: hostip4.c:64
long ip_version
Definition: urldata.h:907
char buf[3]
Definition: unit1398.c:32
#define infof
Definition: sendf.h:44
#define snprintf
Definition: curl_printf.h:42
Curl_addrinfo * Curl_he2ai(const struct hostent *he, int port)
#define TRUE
#define calloc(nbelem, size)
Definition: curl_memory.h:126
struct Curl_easy * data
Definition: urldata.h:791


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15