blob: 701f83ab3bff4c59951eaed3a728fd3e1b2adaec [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Elliott Hughes0128fe42018-02-27 14:57:55 -08008 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +01009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymod15eaac2016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Kristian Monsen5ab50182010-05-14 18:53:44 +010013 *
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
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070023#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010024
Kristian Monsen5ab50182010-05-14 18:53:44 +010025#ifdef HAVE_NETINET_IN_H
26#include <netinet/in.h>
27#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +010028#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 HAVE_NET_IF_H
35#include <net/if.h>
36#endif
37#ifdef HAVE_SYS_IOCTL_H
38#include <sys/ioctl.h>
39#endif
40
41#ifdef HAVE_SYS_PARAM_H
42#include <sys/param.h>
43#endif
44
45#ifdef __VMS
46#include <in.h>
47#include <inet.h>
48#endif
49
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070050#ifdef HAVE_SYS_UN_H
51#include <sys/un.h>
52#endif
53
Kristian Monsen5ab50182010-05-14 18:53:44 +010054#ifndef HAVE_SOCKET
55#error "We can't compile without socket() support!"
56#endif
57
Kristian Monsen5ab50182010-05-14 18:53:44 +010058#include <limits.h>
Kristian Monsen5ab50182010-05-14 18:53:44 +010059
Elliott Hughescee03382017-06-23 12:17:18 -070060#ifdef USE_LIBIDN2
61#include <idn2.h>
62
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070063#elif defined(USE_WIN32_IDN)
64/* prototype for curl_win32_idn_to_ascii() */
Alex Deymod15eaac2016-06-28 14:49:26 -070065bool curl_win32_idn_to_ascii(const char *in, char **out);
Elliott Hughescee03382017-06-23 12:17:18 -070066#endif /* USE_LIBIDN2 */
Kristian Monsen5ab50182010-05-14 18:53:44 +010067
68#include "urldata.h"
69#include "netrc.h"
70
71#include "formdata.h"
Alex Deymo486467e2017-12-19 19:04:07 +010072#include "mime.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070073#include "vtls/vtls.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010074#include "hostip.h"
75#include "transfer.h"
76#include "sendf.h"
77#include "progress.h"
78#include "cookie.h"
Elliott Hughescee03382017-06-23 12:17:18 -070079#include "strcase.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010080#include "strerror.h"
81#include "escape.h"
82#include "strtok.h"
83#include "share.h"
84#include "content_encoding.h"
85#include "http_digest.h"
86#include "http_negotiate.h"
87#include "select.h"
88#include "multiif.h"
89#include "easyif.h"
90#include "speedcheck.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010091#include "warnless.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070092#include "non-ascii.h"
93#include "inet_pton.h"
Elliott Hughescee03382017-06-23 12:17:18 -070094#include "getinfo.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010095
96/* And now for the protocols */
97#include "ftp.h"
98#include "dict.h"
99#include "telnet.h"
100#include "tftp.h"
101#include "http.h"
Alex Deymod15eaac2016-06-28 14:49:26 -0700102#include "http2.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +0100103#include "file.h"
104#include "curl_ldap.h"
105#include "ssh.h"
106#include "imap.h"
107#include "url.h"
108#include "connect.h"
109#include "inet_ntop.h"
Alex Deymod15eaac2016-06-28 14:49:26 -0700110#include "http_ntlm.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700111#include "curl_ntlm_wb.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +0100112#include "socks.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700113#include "curl_rtmp.h"
114#include "gopher.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700115#include "http_proxy.h"
116#include "conncache.h"
117#include "multihandle.h"
118#include "pipeline.h"
119#include "dotdot.h"
120#include "strdup.h"
Alex Deymo486467e2017-12-19 19:04:07 +0100121#include "setopt.h"
122
Alex Deymod15eaac2016-06-28 14:49:26 -0700123/* The last 3 #include files should be in this order */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700124#include "curl_printf.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +0100125#include "curl_memory.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +0100126#include "memdebug.h"
127
Kristian Monsen5ab50182010-05-14 18:53:44 +0100128static void conn_free(struct connectdata *conn);
Alex Deymod15eaac2016-06-28 14:49:26 -0700129static void free_fixed_hostname(struct hostname *host);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100130static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke);
Alex Deymoe3149cc2016-10-05 11:18:42 -0700131static CURLcode parse_url_login(struct Curl_easy *data,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700132 struct connectdata *conn,
133 char **userptr, char **passwdptr,
134 char **optionsptr);
Alex Deymod15eaac2016-06-28 14:49:26 -0700135static unsigned int get_protocol_family(unsigned int protocol);
136
Elliott Hughes82be86d2017-09-20 17:00:17 -0700137/* Some parts of the code (e.g. chunked encoding) assume this buffer has at
138 * more than just a few bytes to play with. Don't let it become too small or
139 * bad things will happen.
140 */
141#if READBUFFER_SIZE < READBUFFER_MIN
142# error READBUFFER_SIZE is too small
143#endif
144
145
Kristian Monsen5ab50182010-05-14 18:53:44 +0100146/*
147 * Protocol table.
148 */
149
150static const struct Curl_handler * const protocols[] = {
151
152#ifndef CURL_DISABLE_HTTP
153 &Curl_handler_http,
154#endif
155
156#if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
157 &Curl_handler_https,
158#endif
159
160#ifndef CURL_DISABLE_FTP
161 &Curl_handler_ftp,
162#endif
163
164#if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
165 &Curl_handler_ftps,
166#endif
167
168#ifndef CURL_DISABLE_TELNET
169 &Curl_handler_telnet,
170#endif
171
172#ifndef CURL_DISABLE_DICT
173 &Curl_handler_dict,
174#endif
175
176#ifndef CURL_DISABLE_LDAP
177 &Curl_handler_ldap,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700178#if !defined(CURL_DISABLE_LDAPS) && \
179 ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
180 (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
Kristian Monsen5ab50182010-05-14 18:53:44 +0100181 &Curl_handler_ldaps,
182#endif
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700183#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100184
185#ifndef CURL_DISABLE_FILE
186 &Curl_handler_file,
187#endif
188
189#ifndef CURL_DISABLE_TFTP
190 &Curl_handler_tftp,
191#endif
192
Elliott Hughes0128fe42018-02-27 14:57:55 -0800193#if defined(USE_LIBSSH2) || defined(USE_LIBSSH)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100194 &Curl_handler_scp,
Elliott Hughes0128fe42018-02-27 14:57:55 -0800195#endif
196
197#if defined(USE_LIBSSH2) || defined(USE_LIBSSH)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100198 &Curl_handler_sftp,
199#endif
200
201#ifndef CURL_DISABLE_IMAP
202 &Curl_handler_imap,
203#ifdef USE_SSL
204 &Curl_handler_imaps,
205#endif
206#endif
207
208#ifndef CURL_DISABLE_POP3
209 &Curl_handler_pop3,
210#ifdef USE_SSL
211 &Curl_handler_pop3s,
212#endif
213#endif
214
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700215#if !defined(CURL_DISABLE_SMB) && defined(USE_NTLM) && \
216 (CURL_SIZEOF_CURL_OFF_T > 4) && \
217 (!defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO))
218 &Curl_handler_smb,
219#ifdef USE_SSL
220 &Curl_handler_smbs,
221#endif
222#endif
223
Kristian Monsen5ab50182010-05-14 18:53:44 +0100224#ifndef CURL_DISABLE_SMTP
225 &Curl_handler_smtp,
226#ifdef USE_SSL
227 &Curl_handler_smtps,
228#endif
229#endif
230
231#ifndef CURL_DISABLE_RTSP
232 &Curl_handler_rtsp,
233#endif
234
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700235#ifndef CURL_DISABLE_GOPHER
236 &Curl_handler_gopher,
237#endif
238
239#ifdef USE_LIBRTMP
240 &Curl_handler_rtmp,
241 &Curl_handler_rtmpt,
242 &Curl_handler_rtmpe,
243 &Curl_handler_rtmpte,
244 &Curl_handler_rtmps,
245 &Curl_handler_rtmpts,
246#endif
247
Kristian Monsen5ab50182010-05-14 18:53:44 +0100248 (struct Curl_handler *) NULL
249};
250
251/*
252 * Dummy handler for undefined protocol schemes.
253 */
254
255static const struct Curl_handler Curl_handler_dummy = {
256 "<no protocol>", /* scheme */
257 ZERO_NULL, /* setup_connection */
258 ZERO_NULL, /* do_it */
259 ZERO_NULL, /* done */
260 ZERO_NULL, /* do_more */
261 ZERO_NULL, /* connect_it */
262 ZERO_NULL, /* connecting */
263 ZERO_NULL, /* doing */
264 ZERO_NULL, /* proto_getsock */
265 ZERO_NULL, /* doing_getsock */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700266 ZERO_NULL, /* domore_getsock */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100267 ZERO_NULL, /* perform_getsock */
268 ZERO_NULL, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700269 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700270 ZERO_NULL, /* connection_check */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100271 0, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700272 0, /* protocol */
273 PROTOPT_NONE /* flags */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100274};
275
Alex Deymoe3149cc2016-10-05 11:18:42 -0700276void Curl_freeset(struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100277{
278 /* Free all dynamic strings stored in the data->set substructure. */
279 enum dupstring i;
Alex Deymo486467e2017-12-19 19:04:07 +0100280 for(i = (enum dupstring)0; i < STRING_LAST; i++) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100281 Curl_safefree(data->set.str[i]);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700282 }
283
284 if(data->change.referer_alloc) {
285 Curl_safefree(data->change.referer);
286 data->change.referer_alloc = FALSE;
287 }
288 data->change.referer = NULL;
289 if(data->change.url_alloc) {
290 Curl_safefree(data->change.url);
291 data->change.url_alloc = FALSE;
292 }
293 data->change.url = NULL;
Elliott Hughes0128fe42018-02-27 14:57:55 -0800294
295 Curl_mime_cleanpart(&data->set.mimepost);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100296}
297
Kristian Monsen5ab50182010-05-14 18:53:44 +0100298/*
299 * This is the internal function curl_easy_cleanup() calls. This should
300 * cleanup and free all resources associated with this sessionhandle.
301 *
302 * NOTE: if we ever add something that attempts to write to a socket or
303 * similar here, we must ignore SIGPIPE first. It is currently only done
304 * when curl_easy_perform() is invoked.
305 */
306
Alex Deymoe3149cc2016-10-05 11:18:42 -0700307CURLcode Curl_close(struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100308{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700309 struct Curl_multi *m;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100310
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700311 if(!data)
312 return CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100313
Elliott Hughescee03382017-06-23 12:17:18 -0700314 Curl_expire_clear(data); /* shut off timers */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700315
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700316 m = data->multi;
317
Kristian Monsen5ab50182010-05-14 18:53:44 +0100318 if(m)
319 /* This handle is still part of a multi handle, take care of this first
320 and detach this handle from there. */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700321 curl_multi_remove_handle(data->multi, data);
322
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700323 if(data->multi_easy)
324 /* when curl_easy_perform() is used, it creates its own multi handle to
325 use and this is the one */
326 curl_multi_cleanup(data->multi_easy);
327
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700328 /* Destroy the timeout list that is held in the easy handle. It is
329 /normally/ done by curl_multi_remove_handle() but this is "just in
330 case" */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700331 Curl_llist_destroy(&data->state.timeoutlist, NULL);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100332
333 data->magic = 0; /* force a clear AFTER the possibly enforced removal from
334 the multi handle, since that function uses the magic
335 field! */
336
Kristian Monsen5ab50182010-05-14 18:53:44 +0100337 if(data->state.rangestringalloc)
338 free(data->state.range);
339
340 /* Free the pathbuffer */
341 Curl_safefree(data->state.pathbuffer);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700342 data->state.path = NULL;
343
344 /* freed here just in case DONE wasn't called */
345 Curl_free_request_state(data);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100346
347 /* Close down all open SSL info and sessions */
348 Curl_ssl_close_all(data);
349 Curl_safefree(data->state.first_host);
350 Curl_safefree(data->state.scratch);
351 Curl_ssl_free_certinfo(data);
352
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700353 /* Cleanup possible redirect junk */
354 free(data->req.newurl);
355 data->req.newurl = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100356
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700357 if(data->change.referer_alloc) {
358 Curl_safefree(data->change.referer);
359 data->change.referer_alloc = FALSE;
360 }
361 data->change.referer = NULL;
362
363 if(data->change.url_alloc) {
364 Curl_safefree(data->change.url);
365 data->change.url_alloc = FALSE;
366 }
367 data->change.url = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100368
Elliott Hughes82be86d2017-09-20 17:00:17 -0700369 Curl_safefree(data->state.buffer);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100370 Curl_safefree(data->state.headerbuff);
371
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700372 Curl_flush_cookies(data, 1);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100373
374 Curl_digest_cleanup(data);
375
376 Curl_safefree(data->info.contenttype);
377 Curl_safefree(data->info.wouldredirect);
378
379 /* this destroys the channel and we cannot use it anymore after this */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700380 Curl_resolver_cleanup(data->state.resolver);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100381
Elliott Hughes82be86d2017-09-20 17:00:17 -0700382 Curl_http2_cleanup_dependencies(data);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700383 Curl_convert_close(data);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100384
385 /* No longer a dirty share, if it exists */
386 if(data->share) {
387 Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
388 data->share->dirty--;
389 Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
390 }
391
Alex Deymo486467e2017-12-19 19:04:07 +0100392 /* destruct wildcard structures if it is needed */
393 Curl_wildcard_dtor(&data->wildcard);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100394 Curl_freeset(data);
395 free(data);
396 return CURLE_OK;
397}
398
Kristian Monsen5ab50182010-05-14 18:53:44 +0100399/*
Alex Deymoe3149cc2016-10-05 11:18:42 -0700400 * Initialize the UserDefined fields within a Curl_easy.
401 * This may be safely called on a new or existing Curl_easy.
Kristian Monsen5ab50182010-05-14 18:53:44 +0100402 */
Elliott Hughes0128fe42018-02-27 14:57:55 -0800403CURLcode Curl_init_userdefined(struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100404{
Elliott Hughes0128fe42018-02-27 14:57:55 -0800405 struct UserDefined *set = &data->set;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700406 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100407
408 set->out = stdout; /* default output to stdout */
Alex Deymod15eaac2016-06-28 14:49:26 -0700409 set->in_set = stdin; /* default input from stdin */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100410 set->err = stderr; /* default stderr to stderr */
411
412 /* use fwrite as default function to store output */
413 set->fwrite_func = (curl_write_callback)fwrite;
414
415 /* use fread as default function to read input */
Alex Deymod15eaac2016-06-28 14:49:26 -0700416 set->fread_func_set = (curl_read_callback)fread;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700417 set->is_fread_set = 0;
418 set->is_fwrite_set = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100419
420 set->seek_func = ZERO_NULL;
421 set->seek_client = ZERO_NULL;
422
423 /* conversion callbacks for non-ASCII hosts */
424 set->convfromnetwork = ZERO_NULL;
425 set->convtonetwork = ZERO_NULL;
426 set->convfromutf8 = ZERO_NULL;
427
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700428 set->filesize = -1; /* we don't know the size */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100429 set->postfieldsize = -1; /* unknown size */
430 set->maxredirs = -1; /* allow any amount by default */
431
432 set->httpreq = HTTPREQ_GET; /* Default HTTP request */
433 set->rtspreq = RTSPREQ_OPTIONS; /* Default RTSP request */
434 set->ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
435 set->ftp_use_eprt = TRUE; /* FTP defaults to EPRT operations */
436 set->ftp_use_pret = FALSE; /* mainly useful for drftpd servers */
437 set->ftp_filemethod = FTPFILE_MULTICWD;
438
439 set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
440
441 /* Set the default size of the SSL session ID cache */
Elliott Hughescee03382017-06-23 12:17:18 -0700442 set->general_ssl.max_ssl_sessions = 5;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100443
Elliott Hughes82be86d2017-09-20 17:00:17 -0700444 set->proxyport = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100445 set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
446 set->httpauth = CURLAUTH_BASIC; /* defaults to basic */
447 set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
448
Elliott Hughes82be86d2017-09-20 17:00:17 -0700449 /* SOCKS5 proxy auth defaults to username/password + GSS-API */
450 set->socks5auth = CURLAUTH_BASIC | CURLAUTH_GSSAPI;
451
Kristian Monsen5ab50182010-05-14 18:53:44 +0100452 /* make libcurl quiet by default: */
453 set->hide_progress = TRUE; /* CURLOPT_NOPROGRESS changes these */
454
Elliott Hughes0128fe42018-02-27 14:57:55 -0800455 Curl_mime_initpart(&set->mimepost, data);
456
Kristian Monsen5ab50182010-05-14 18:53:44 +0100457 /*
458 * libcurl 7.10 introduced SSL verification *by default*! This needs to be
459 * switched off unless wanted.
460 */
Elliott Hughescee03382017-06-23 12:17:18 -0700461 set->ssl.primary.verifypeer = TRUE;
462 set->ssl.primary.verifyhost = TRUE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700463#ifdef USE_TLS_SRP
464 set->ssl.authtype = CURL_TLSAUTH_NONE;
465#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100466 set->ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
467 type */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700468 set->ssl.primary.sessionid = TRUE; /* session ID caching enabled by
Elliott Hughescee03382017-06-23 12:17:18 -0700469 default */
470 set->proxy_ssl = set->ssl;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100471
472 set->new_file_perms = 0644; /* Default permissions */
473 set->new_directory_perms = 0755; /* Default permissions */
474
475 /* for the *protocols fields we don't use the CURLPROTO_ALL convenience
476 define since we internally only use the lower 16 bits for the passed
477 in bitmask to not conflict with the private bits */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700478 set->allowed_protocols = CURLPROTO_ALL;
479 set->redir_protocols = CURLPROTO_ALL & /* All except FILE, SCP and SMB */
480 ~(CURLPROTO_FILE | CURLPROTO_SCP | CURLPROTO_SMB |
481 CURLPROTO_SMBS);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100482
483#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
484 /*
485 * disallow unprotected protection negotiation NEC reference implementation
486 * seem not to follow rfc1961 section 4.3/4.4
487 */
488 set->socks5_gssapi_nec = FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100489#endif
490
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700491 /* Set the default CA cert bundle/path detected/specified at build time.
492 *
493 * If Schannel (WinSSL) is the selected SSL backend then these locations
494 * are ignored. We allow setting CA location for schannel only when
495 * explicitly specified by the user via CURLOPT_CAINFO / --cacert.
496 */
497 if(Curl_ssl_backend() != CURLSSLBACKEND_SCHANNEL) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100498#if defined(CURL_CA_BUNDLE)
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700499 result = Curl_setstropt(&set->str[STRING_SSL_CAFILE_ORIG], CURL_CA_BUNDLE);
500 if(result)
501 return result;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700502
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700503 result = Curl_setstropt(&set->str[STRING_SSL_CAFILE_PROXY],
504 CURL_CA_BUNDLE);
505 if(result)
506 return result;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700507#endif
508#if defined(CURL_CA_PATH)
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700509 result = Curl_setstropt(&set->str[STRING_SSL_CAPATH_ORIG], CURL_CA_PATH);
510 if(result)
511 return result;
Elliott Hughescee03382017-06-23 12:17:18 -0700512
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700513 result = Curl_setstropt(&set->str[STRING_SSL_CAPATH_PROXY], CURL_CA_PATH);
514 if(result)
515 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100516#endif
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700517 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100518
Alex Deymo486467e2017-12-19 19:04:07 +0100519 set->wildcard_enabled = FALSE;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700520 set->chunk_bgn = ZERO_NULL;
521 set->chunk_end = ZERO_NULL;
522
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700523 /* tcp keepalives are disabled by default, but provide reasonable values for
524 * the interval and idle times.
525 */
526 set->tcp_keepalive = FALSE;
527 set->tcp_keepintvl = 60;
528 set->tcp_keepidle = 60;
Alex Deymod15eaac2016-06-28 14:49:26 -0700529 set->tcp_fastopen = FALSE;
Elliott Hughescee03382017-06-23 12:17:18 -0700530 set->tcp_nodelay = TRUE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700531
532 set->ssl_enable_npn = TRUE;
533 set->ssl_enable_alpn = TRUE;
534
535 set->expect_100_timeout = 1000L; /* Wait for a second by default. */
536 set->sep_headers = TRUE; /* separated header lists by default */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700537 set->buffer_size = READBUFFER_SIZE;
Elliott Hughescac39802018-04-27 16:19:43 -0700538 set->happy_eyeballs_timeout = CURL_HET_DEFAULT;
Alex Deymod15eaac2016-06-28 14:49:26 -0700539
540 Curl_http2_init_userset(set);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700541 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100542}
543
544/**
545 * Curl_open()
546 *
547 * @param curl is a pointer to a sessionhandle pointer that gets set by this
548 * function.
549 * @return CURLcode
550 */
551
Alex Deymoe3149cc2016-10-05 11:18:42 -0700552CURLcode Curl_open(struct Curl_easy **curl)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100553{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700554 CURLcode result;
Alex Deymoe3149cc2016-10-05 11:18:42 -0700555 struct Curl_easy *data;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100556
557 /* Very simple start-up: alloc the struct, init it with zeroes and return */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700558 data = calloc(1, sizeof(struct Curl_easy));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100559 if(!data) {
560 /* this is a very serious error */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700561 DEBUGF(fprintf(stderr, "Error: calloc of Curl_easy failed\n"));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100562 return CURLE_OUT_OF_MEMORY;
563 }
564
565 data->magic = CURLEASY_MAGIC_NUMBER;
566
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700567 result = Curl_resolver_init(&data->state.resolver);
568 if(result) {
569 DEBUGF(fprintf(stderr, "Error: resolver_init failed\n"));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100570 free(data);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700571 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100572 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100573
574 /* We do some initial setup here, all those fields that can't be just 0 */
575
Elliott Hughes82be86d2017-09-20 17:00:17 -0700576 data->state.buffer = malloc(READBUFFER_SIZE + 1);
577 if(!data->state.buffer) {
578 DEBUGF(fprintf(stderr, "Error: malloc of buffer failed\n"));
579 result = CURLE_OUT_OF_MEMORY;
580 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100581 else {
Alex Deymo486467e2017-12-19 19:04:07 +0100582 data->state.headerbuff = malloc(HEADERSIZE);
583 if(!data->state.headerbuff) {
584 DEBUGF(fprintf(stderr, "Error: malloc of headerbuff failed\n"));
585 result = CURLE_OUT_OF_MEMORY;
586 }
587 else {
Elliott Hughes0128fe42018-02-27 14:57:55 -0800588 result = Curl_init_userdefined(data);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100589
Alex Deymo486467e2017-12-19 19:04:07 +0100590 data->state.headersize = HEADERSIZE;
591 Curl_convert_init(data);
592 Curl_initinfo(data);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100593
Alex Deymo486467e2017-12-19 19:04:07 +0100594 /* most recent connection is not yet defined */
595 data->state.lastconnect = NULL;
Elliott Hughescee03382017-06-23 12:17:18 -0700596
Alex Deymo486467e2017-12-19 19:04:07 +0100597 data->progress.flags |= PGRS_HIDE;
598 data->state.current_speed = -1; /* init to negative == impossible */
599 data->set.fnmatch = ZERO_NULL;
600 data->set.maxconnects = DEFAULT_CONNCACHE_SIZE; /* for easy handles */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100601
Alex Deymo486467e2017-12-19 19:04:07 +0100602 Curl_http2_init_state(&data->state);
603 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100604 }
605
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700606 if(result) {
607 Curl_resolver_cleanup(data->state.resolver);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700608 free(data->state.buffer);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700609 free(data->state.headerbuff);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100610 Curl_freeset(data);
611 free(data);
612 data = NULL;
613 }
614 else
615 *curl = data;
616
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700617 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100618}
619
Alex Deymod15eaac2016-06-28 14:49:26 -0700620#ifdef USE_RECV_BEFORE_SEND_WORKAROUND
621static void conn_reset_postponed_data(struct connectdata *conn, int num)
622{
623 struct postponed_data * const psnd = &(conn->postponed[num]);
624 if(psnd->buffer) {
625 DEBUGASSERT(psnd->allocated_size > 0);
626 DEBUGASSERT(psnd->recv_size <= psnd->allocated_size);
627 DEBUGASSERT(psnd->recv_size ?
628 (psnd->recv_processed < psnd->recv_size) :
629 (psnd->recv_processed == 0));
630 DEBUGASSERT(psnd->bindsock != CURL_SOCKET_BAD);
631 free(psnd->buffer);
632 psnd->buffer = NULL;
633 psnd->allocated_size = 0;
634 psnd->recv_size = 0;
635 psnd->recv_processed = 0;
636#ifdef DEBUGBUILD
637 psnd->bindsock = CURL_SOCKET_BAD; /* used only for DEBUGASSERT */
638#endif /* DEBUGBUILD */
639 }
640 else {
Elliott Hughes82be86d2017-09-20 17:00:17 -0700641 DEBUGASSERT(psnd->allocated_size == 0);
642 DEBUGASSERT(psnd->recv_size == 0);
643 DEBUGASSERT(psnd->recv_processed == 0);
644 DEBUGASSERT(psnd->bindsock == CURL_SOCKET_BAD);
Alex Deymod15eaac2016-06-28 14:49:26 -0700645 }
646}
647
648static void conn_reset_all_postponed_data(struct connectdata *conn)
649{
650 conn_reset_postponed_data(conn, 0);
651 conn_reset_postponed_data(conn, 1);
652}
653#else /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700654/* Use "do-nothing" macro instead of function when workaround not used */
Alex Deymod15eaac2016-06-28 14:49:26 -0700655#define conn_reset_all_postponed_data(c) do {} WHILE_FALSE
656#endif /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
657
Kristian Monsen5ab50182010-05-14 18:53:44 +0100658static void conn_free(struct connectdata *conn)
659{
660 if(!conn)
661 return;
662
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700663 /* possible left-overs from the async name resolvers */
664 Curl_resolver_cancel(conn);
665
Kristian Monsen5ab50182010-05-14 18:53:44 +0100666 /* close the SSL stuff before we close any sockets since they will/may
667 write to the sockets */
668 Curl_ssl_close(conn, FIRSTSOCKET);
669 Curl_ssl_close(conn, SECONDARYSOCKET);
670
671 /* close possibly still open sockets */
672 if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700673 Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100674 if(CURL_SOCKET_BAD != conn->sock[FIRSTSOCKET])
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700675 Curl_closesocket(conn, conn->sock[FIRSTSOCKET]);
676 if(CURL_SOCKET_BAD != conn->tempsock[0])
677 Curl_closesocket(conn, conn->tempsock[0]);
678 if(CURL_SOCKET_BAD != conn->tempsock[1])
679 Curl_closesocket(conn, conn->tempsock[1]);
680
681#if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
682 defined(NTLM_WB_ENABLED)
683 Curl_ntlm_wb_cleanup(conn);
684#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100685
686 Curl_safefree(conn->user);
687 Curl_safefree(conn->passwd);
Alex Deymod15eaac2016-06-28 14:49:26 -0700688 Curl_safefree(conn->oauth_bearer);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700689 Curl_safefree(conn->options);
Elliott Hughescee03382017-06-23 12:17:18 -0700690 Curl_safefree(conn->http_proxy.user);
691 Curl_safefree(conn->socks_proxy.user);
692 Curl_safefree(conn->http_proxy.passwd);
693 Curl_safefree(conn->socks_proxy.passwd);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100694 Curl_safefree(conn->allocptr.proxyuserpwd);
695 Curl_safefree(conn->allocptr.uagent);
696 Curl_safefree(conn->allocptr.userpwd);
697 Curl_safefree(conn->allocptr.accept_encoding);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700698 Curl_safefree(conn->allocptr.te);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100699 Curl_safefree(conn->allocptr.rangeline);
700 Curl_safefree(conn->allocptr.ref);
701 Curl_safefree(conn->allocptr.host);
702 Curl_safefree(conn->allocptr.cookiehost);
703 Curl_safefree(conn->allocptr.rtsp_transport);
704 Curl_safefree(conn->trailer);
705 Curl_safefree(conn->host.rawalloc); /* host name buffer */
Alex Deymod15eaac2016-06-28 14:49:26 -0700706 Curl_safefree(conn->conn_to_host.rawalloc); /* host name buffer */
Elliott Hughescee03382017-06-23 12:17:18 -0700707 Curl_safefree(conn->secondaryhostname);
708 Curl_safefree(conn->http_proxy.host.rawalloc); /* http proxy name buffer */
709 Curl_safefree(conn->socks_proxy.host.rawalloc); /* socks proxy name buffer */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100710 Curl_safefree(conn->master_buffer);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700711 Curl_safefree(conn->connect_state);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100712
Alex Deymod15eaac2016-06-28 14:49:26 -0700713 conn_reset_all_postponed_data(conn);
714
Elliott Hughes82be86d2017-09-20 17:00:17 -0700715 Curl_llist_destroy(&conn->send_pipe, NULL);
716 Curl_llist_destroy(&conn->recv_pipe, NULL);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100717
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700718 Curl_safefree(conn->localdev);
Elliott Hughescee03382017-06-23 12:17:18 -0700719 Curl_free_primary_ssl_config(&conn->ssl_config);
720 Curl_free_primary_ssl_config(&conn->proxy_ssl_config);
721
722#ifdef USE_UNIX_SOCKETS
723 Curl_safefree(conn->unix_domain_socket);
724#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100725
Elliott Hughes0128fe42018-02-27 14:57:55 -0800726#ifdef USE_SSL
727 Curl_safefree(conn->ssl_extra);
728#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100729 free(conn); /* free all the connection oriented data */
730}
731
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700732/*
733 * Disconnects the given connection. Note the connection may not be the
734 * primary connection, like when freeing room in the connection cache or
735 * killing of a dead old connection.
736 *
Alex Deymoe3149cc2016-10-05 11:18:42 -0700737 * This function MUST NOT reset state in the Curl_easy struct if that
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700738 * isn't strictly bound to the life-time of *this* particular connection.
739 *
740 */
741
742CURLcode Curl_disconnect(struct connectdata *conn, bool dead_connection)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100743{
Alex Deymoe3149cc2016-10-05 11:18:42 -0700744 struct Curl_easy *data;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100745 if(!conn)
746 return CURLE_OK; /* this is closed and fine already */
747 data = conn->data;
748
749 if(!data) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700750 DEBUGF(fprintf(stderr, "DISCONNECT without easy handle, ignoring\n"));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100751 return CURLE_OK;
752 }
753
Elliott Hughescee03382017-06-23 12:17:18 -0700754 /*
755 * If this connection isn't marked to force-close, leave it open if there
756 * are other users of it
757 */
758 if(!conn->bits.close &&
Elliott Hughes82be86d2017-09-20 17:00:17 -0700759 (conn->send_pipe.size + conn->recv_pipe.size)) {
Elliott Hughescee03382017-06-23 12:17:18 -0700760 DEBUGF(infof(data, "Curl_disconnect, usecounter: %d\n",
Elliott Hughes82be86d2017-09-20 17:00:17 -0700761 conn->send_pipe.size + conn->recv_pipe.size));
Elliott Hughescee03382017-06-23 12:17:18 -0700762 return CURLE_OK;
763 }
764
Kristian Monsen5ab50182010-05-14 18:53:44 +0100765 if(conn->dns_entry != NULL) {
766 Curl_resolv_unlock(data, conn->dns_entry);
767 conn->dns_entry = NULL;
768 }
769
Kristian Monsen5ab50182010-05-14 18:53:44 +0100770 Curl_hostcache_prune(data); /* kill old DNS cache entries */
771
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700772#if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
773 /* Cleanup NTLM connection-related data */
774 Curl_http_ntlm_cleanup(conn);
775#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100776
777 if(conn->handler->disconnect)
778 /* This is set if protocol-specific cleanups should be made */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700779 conn->handler->disconnect(conn, dead_connection);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100780
Kristian Monsen5ab50182010-05-14 18:53:44 +0100781 /* unlink ourselves! */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700782 infof(data, "Closing connection %ld\n", conn->connection_id);
Elliott Hughes0128fe42018-02-27 14:57:55 -0800783 Curl_conncache_remove_conn(conn, TRUE);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100784
Alex Deymod15eaac2016-06-28 14:49:26 -0700785 free_fixed_hostname(&conn->host);
786 free_fixed_hostname(&conn->conn_to_host);
Elliott Hughescee03382017-06-23 12:17:18 -0700787 free_fixed_hostname(&conn->http_proxy.host);
788 free_fixed_hostname(&conn->socks_proxy.host);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100789
790 Curl_ssl_close(conn, FIRSTSOCKET);
791
792 /* Indicate to all handles on the pipe that we're dead */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700793 if(Curl_pipeline_wanted(data->multi, CURLPIPE_ANY)) {
Elliott Hughes82be86d2017-09-20 17:00:17 -0700794 signalPipeClose(&conn->send_pipe, TRUE);
795 signalPipeClose(&conn->recv_pipe, TRUE);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100796 }
797
798 conn_free(conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100799
800 return CURLE_OK;
801}
802
803/*
804 * This function should return TRUE if the socket is to be assumed to
805 * be dead. Most commonly this happens when the server has closed the
806 * connection due to inactivity.
807 */
808static bool SocketIsDead(curl_socket_t sock)
809{
810 int sval;
811 bool ret_val = TRUE;
812
Elliott Hughescee03382017-06-23 12:17:18 -0700813 sval = SOCKET_READABLE(sock, 0);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100814 if(sval == 0)
815 /* timeout */
816 ret_val = FALSE;
817
818 return ret_val;
819}
820
Kristian Monsen5ab50182010-05-14 18:53:44 +0100821/*
Elliott Hughes82be86d2017-09-20 17:00:17 -0700822 * IsPipeliningPossible()
823 *
824 * Return a bitmask with the available pipelining and multiplexing options for
825 * the given requested connection.
Kristian Monsen5ab50182010-05-14 18:53:44 +0100826 */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700827static int IsPipeliningPossible(const struct Curl_easy *handle,
828 const struct connectdata *conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100829{
Elliott Hughes82be86d2017-09-20 17:00:17 -0700830 int avail = 0;
831
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700832 /* If a HTTP protocol and pipelining is enabled */
Elliott Hughescee03382017-06-23 12:17:18 -0700833 if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
834 (!conn->bits.protoconnstart || !conn->bits.close)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100835
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700836 if(Curl_pipeline_wanted(handle->multi, CURLPIPE_HTTP1) &&
837 (handle->set.httpversion != CURL_HTTP_VERSION_1_0) &&
838 (handle->set.httpreq == HTTPREQ_GET ||
839 handle->set.httpreq == HTTPREQ_HEAD))
840 /* didn't ask for HTTP/1.0 and a GET or HEAD */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700841 avail |= CURLPIPE_HTTP1;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700842
843 if(Curl_pipeline_wanted(handle->multi, CURLPIPE_MULTIPLEX) &&
Alex Deymod15eaac2016-06-28 14:49:26 -0700844 (handle->set.httpversion >= CURL_HTTP_VERSION_2))
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700845 /* allows HTTP/2 */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700846 avail |= CURLPIPE_MULTIPLEX;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100847 }
Elliott Hughes82be86d2017-09-20 17:00:17 -0700848 return avail;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100849}
850
Alex Deymoe3149cc2016-10-05 11:18:42 -0700851int Curl_removeHandleFromPipeline(struct Curl_easy *handle,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100852 struct curl_llist *pipeline)
853{
Alex Deymod15eaac2016-06-28 14:49:26 -0700854 if(pipeline) {
855 struct curl_llist_element *curr;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100856
Alex Deymod15eaac2016-06-28 14:49:26 -0700857 curr = pipeline->head;
858 while(curr) {
859 if(curr->ptr == handle) {
860 Curl_llist_remove(pipeline, curr, NULL);
861 return 1; /* we removed a handle */
862 }
863 curr = curr->next;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100864 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100865 }
866
867 return 0;
868}
869
870#if 0 /* this code is saved here as it is useful for debugging purposes */
871static void Curl_printPipeline(struct curl_llist *pipeline)
872{
873 struct curl_llist_element *curr;
874
875 curr = pipeline->head;
876 while(curr) {
Alex Deymoe3149cc2016-10-05 11:18:42 -0700877 struct Curl_easy *data = (struct Curl_easy *) curr->ptr;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100878 infof(data, "Handle in pipeline: %s\n", data->state.path);
879 curr = curr->next;
880 }
881}
882#endif
883
Alex Deymoe3149cc2016-10-05 11:18:42 -0700884static struct Curl_easy* gethandleathead(struct curl_llist *pipeline)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100885{
886 struct curl_llist_element *curr = pipeline->head;
887 if(curr) {
Alex Deymoe3149cc2016-10-05 11:18:42 -0700888 return (struct Curl_easy *) curr->ptr;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100889 }
890
891 return NULL;
892}
893
894/* remove the specified connection from all (possible) pipelines and related
895 queues */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700896void Curl_getoff_all_pipelines(struct Curl_easy *data,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100897 struct connectdata *conn)
898{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700899 bool recv_head = (conn->readchannel_inuse &&
900 Curl_recvpipe_head(data, conn));
901 bool send_head = (conn->writechannel_inuse &&
902 Curl_sendpipe_head(data, conn));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100903
Elliott Hughes82be86d2017-09-20 17:00:17 -0700904 if(Curl_removeHandleFromPipeline(data, &conn->recv_pipe) && recv_head)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700905 Curl_pipeline_leave_read(conn);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700906 if(Curl_removeHandleFromPipeline(data, &conn->send_pipe) && send_head)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700907 Curl_pipeline_leave_write(conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100908}
909
910static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke)
911{
912 struct curl_llist_element *curr;
913
914 if(!pipeline)
915 return;
916
917 curr = pipeline->head;
918 while(curr) {
919 struct curl_llist_element *next = curr->next;
Alex Deymoe3149cc2016-10-05 11:18:42 -0700920 struct Curl_easy *data = (struct Curl_easy *) curr->ptr;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100921
922#ifdef DEBUGBUILD /* debug-only code */
923 if(data->magic != CURLEASY_MAGIC_NUMBER) {
924 /* MAJOR BADNESS */
925 infof(data, "signalPipeClose() found BAAD easy handle\n");
926 }
927#endif
928
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700929 if(pipe_broke)
930 data->state.pipe_broke = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100931 Curl_multi_handlePipeBreak(data);
932 Curl_llist_remove(pipeline, curr, NULL);
933 curr = next;
934 }
935}
936
Elliott Hughescee03382017-06-23 12:17:18 -0700937static bool
938proxy_info_matches(const struct proxy_info* data,
939 const struct proxy_info* needle)
940{
941 if((data->proxytype == needle->proxytype) &&
942 (data->port == needle->port) &&
Elliott Hughes82be86d2017-09-20 17:00:17 -0700943 Curl_safe_strcasecompare(data->host.name, needle->host.name))
Elliott Hughescee03382017-06-23 12:17:18 -0700944 return TRUE;
945
946 return FALSE;
947}
948
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700949/*
Elliott Hughes0128fe42018-02-27 14:57:55 -0800950 * This function checks if the given connection is dead and extracts it from
951 * the connection cache if so.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700952 *
Elliott Hughes0128fe42018-02-27 14:57:55 -0800953 * When this is called as a Curl_conncache_foreach() callback, the connection
954 * cache lock is held!
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700955 *
Elliott Hughes0128fe42018-02-27 14:57:55 -0800956 * Returns TRUE if the connection was dead and extracted.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700957 */
Elliott Hughes0128fe42018-02-27 14:57:55 -0800958static bool extract_if_dead(struct connectdata *conn,
959 struct Curl_easy *data)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700960{
Elliott Hughes82be86d2017-09-20 17:00:17 -0700961 size_t pipeLen = conn->send_pipe.size + conn->recv_pipe.size;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700962 if(!pipeLen && !conn->inuse) {
963 /* The check for a dead socket makes sense only if there are no
964 handles in pipeline and the connection isn't already marked in
965 use */
966 bool dead;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700967
968 if(conn->handler->connection_check) {
969 /* The protocol has a special method for checking the state of the
970 connection. Use it to check if the connection is dead. */
971 unsigned int state;
972
973 state = conn->handler->connection_check(conn, CONNCHECK_ISDEAD);
974 dead = (state & CONNRESULT_DEAD);
975 }
976 else {
977 /* Use the general method for determining the death of a connection */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700978 dead = SocketIsDead(conn->sock[FIRSTSOCKET]);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700979 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700980
981 if(dead) {
982 conn->data = data;
983 infof(data, "Connection %ld seems to be dead!\n", conn->connection_id);
Elliott Hughes0128fe42018-02-27 14:57:55 -0800984 Curl_conncache_remove_conn(conn, FALSE);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700985 return TRUE;
986 }
987 }
988 return FALSE;
989}
990
Elliott Hughes0128fe42018-02-27 14:57:55 -0800991struct prunedead {
992 struct Curl_easy *data;
993 struct connectdata *extracted;
994};
995
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700996/*
Elliott Hughes0128fe42018-02-27 14:57:55 -0800997 * Wrapper to use extract_if_dead() function in Curl_conncache_foreach()
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700998 *
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700999 */
Elliott Hughes0128fe42018-02-27 14:57:55 -08001000static int call_extract_if_dead(struct connectdata *conn, void *param)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001001{
Elliott Hughes0128fe42018-02-27 14:57:55 -08001002 struct prunedead *p = (struct prunedead *)param;
1003 if(extract_if_dead(conn, p->data)) {
1004 /* stop the iteration here, pass back the connection that was extracted */
1005 p->extracted = conn;
1006 return 1;
1007 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001008 return 0; /* continue iteration */
1009}
1010
1011/*
1012 * This function scans the connection cache for half-open/dead connections,
1013 * closes and removes them.
1014 * The cleanup is done at most once per second.
1015 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07001016static void prune_dead_connections(struct Curl_easy *data)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001017{
Alex Deymo486467e2017-12-19 19:04:07 +01001018 struct curltime now = Curl_now();
1019 time_t elapsed = Curl_timediff(now, data->state.conn_cache->last_cleanup);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001020
1021 if(elapsed >= 1000L) {
Elliott Hughes0128fe42018-02-27 14:57:55 -08001022 struct prunedead prune;
1023 prune.data = data;
1024 prune.extracted = NULL;
1025 while(Curl_conncache_foreach(data, data->state.conn_cache, &prune,
1026 call_extract_if_dead)) {
1027 /* disconnect it */
1028 (void)Curl_disconnect(prune.extracted, /* dead_connection */TRUE);
1029 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001030 data->state.conn_cache->last_cleanup = now;
1031 }
1032}
1033
1034
1035static size_t max_pipeline_length(struct Curl_multi *multi)
1036{
1037 return multi ? multi->max_pipeline_length : 0;
1038}
1039
Kristian Monsen5ab50182010-05-14 18:53:44 +01001040
1041/*
1042 * Given one filled in connection struct (named needle), this function should
1043 * detect if there already is one that has all the significant details
1044 * exactly the same and thus should be used instead.
1045 *
1046 * If there is a match, this function returns TRUE - and has marked the
1047 * connection as 'in-use'. It must later be called with ConnectionDone() to
1048 * return back to 'idle' (unused) state.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001049 *
1050 * The force_reuse flag is set if the connection must be used, even if
1051 * the pipelining strategy wants to open a new connection instead of reusing.
Kristian Monsen5ab50182010-05-14 18:53:44 +01001052 */
1053static bool
Alex Deymoe3149cc2016-10-05 11:18:42 -07001054ConnectionExists(struct Curl_easy *data,
Kristian Monsen5ab50182010-05-14 18:53:44 +01001055 struct connectdata *needle,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001056 struct connectdata **usethis,
1057 bool *force_reuse,
1058 bool *waitpipe)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001059{
Kristian Monsen5ab50182010-05-14 18:53:44 +01001060 struct connectdata *check;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001061 struct connectdata *chosen = 0;
Alex Deymod15eaac2016-06-28 14:49:26 -07001062 bool foundPendingCandidate = FALSE;
Elliott Hughes82be86d2017-09-20 17:00:17 -07001063 int canpipe = IsPipeliningPossible(data, needle);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001064 struct connectbundle *bundle;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001065
Alex Deymod15eaac2016-06-28 14:49:26 -07001066#ifdef USE_NTLM
1067 bool wantNTLMhttp = ((data->state.authhost.want &
1068 (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
1069 (needle->handler->protocol & PROTO_FAMILY_HTTP));
1070 bool wantProxyNTLMhttp = (needle->bits.proxy_user_passwd &&
1071 ((data->state.authproxy.want &
1072 (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
1073 (needle->handler->protocol & PROTO_FAMILY_HTTP)));
1074#endif
1075
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001076 *force_reuse = FALSE;
1077 *waitpipe = FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001078
Elliott Hughes82be86d2017-09-20 17:00:17 -07001079 /* We can't pipeline if the site is blacklisted */
1080 if((canpipe & CURLPIPE_HTTP1) &&
1081 Curl_pipeline_site_blacklisted(data, needle))
1082 canpipe &= ~ CURLPIPE_HTTP1;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001083
Elliott Hughes0128fe42018-02-27 14:57:55 -08001084 /* Look up the bundle with all the connections to this particular host.
1085 Locks the connection cache, beware of early returns! */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001086 bundle = Curl_conncache_find_bundle(needle, data->state.conn_cache);
1087 if(bundle) {
1088 /* Max pipe length is zero (unlimited) for multiplexed connections */
1089 size_t max_pipe_len = (bundle->multiuse != BUNDLE_MULTIPLEX)?
1090 max_pipeline_length(data->multi):0;
1091 size_t best_pipe_len = max_pipe_len;
1092 struct curl_llist_element *curr;
Alex Deymod15eaac2016-06-28 14:49:26 -07001093
1094 infof(data, "Found bundle for host %s: %p [%s]\n",
Elliott Hughes82be86d2017-09-20 17:00:17 -07001095 (needle->bits.conn_to_host ? needle->conn_to_host.name :
1096 needle->host.name), (void *)bundle,
1097 (bundle->multiuse == BUNDLE_PIPELINING ?
1098 "can pipeline" :
1099 (bundle->multiuse == BUNDLE_MULTIPLEX ?
1100 "can multiplex" : "serially")));
Kristian Monsen5ab50182010-05-14 18:53:44 +01001101
Elliott Hughes82be86d2017-09-20 17:00:17 -07001102 /* We can't pipeline if we don't know anything about the server */
1103 if(canpipe) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001104 if(bundle->multiuse <= BUNDLE_UNKNOWN) {
1105 if((bundle->multiuse == BUNDLE_UNKNOWN) && data->set.pipewait) {
1106 infof(data, "Server doesn't support multi-use yet, wait\n");
1107 *waitpipe = TRUE;
Elliott Hughes0128fe42018-02-27 14:57:55 -08001108 Curl_conncache_unlock(needle);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001109 return FALSE; /* no re-use */
1110 }
1111
1112 infof(data, "Server doesn't support multi-use (yet)\n");
Elliott Hughes82be86d2017-09-20 17:00:17 -07001113 canpipe = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001114 }
Alex Deymod15eaac2016-06-28 14:49:26 -07001115 if((bundle->multiuse == BUNDLE_PIPELINING) &&
1116 !Curl_pipeline_wanted(data->multi, CURLPIPE_HTTP1)) {
1117 /* not asked for, switch off */
1118 infof(data, "Could pipeline, but not asked to!\n");
Elliott Hughes82be86d2017-09-20 17:00:17 -07001119 canpipe = 0;
Alex Deymod15eaac2016-06-28 14:49:26 -07001120 }
1121 else if((bundle->multiuse == BUNDLE_MULTIPLEX) &&
1122 !Curl_pipeline_wanted(data->multi, CURLPIPE_MULTIPLEX)) {
1123 infof(data, "Could multiplex, but not asked to!\n");
Elliott Hughes82be86d2017-09-20 17:00:17 -07001124 canpipe = 0;
Alex Deymod15eaac2016-06-28 14:49:26 -07001125 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001126 }
1127
Elliott Hughes82be86d2017-09-20 17:00:17 -07001128 curr = bundle->conn_list.head;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001129 while(curr) {
1130 bool match = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001131 size_t pipeLen;
1132
1133 /*
Alex Deymod15eaac2016-06-28 14:49:26 -07001134 * Note that if we use a HTTP proxy in normal mode (no tunneling), we
1135 * check connections to that proxy and not to the actual remote server.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001136 */
1137 check = curr->ptr;
1138 curr = curr->next;
1139
Elliott Hughes0128fe42018-02-27 14:57:55 -08001140 if(extract_if_dead(check, data)) {
1141 /* disconnect it */
1142 (void)Curl_disconnect(check, /* dead_connection */TRUE);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001143 continue;
Elliott Hughes0128fe42018-02-27 14:57:55 -08001144 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001145
Elliott Hughes82be86d2017-09-20 17:00:17 -07001146 pipeLen = check->send_pipe.size + check->recv_pipe.size;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001147
Elliott Hughes82be86d2017-09-20 17:00:17 -07001148 if(canpipe) {
Elliott Hughescee03382017-06-23 12:17:18 -07001149 if(check->bits.protoconnstart && check->bits.close)
1150 continue;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001151
1152 if(!check->bits.multiplex) {
Elliott Hughes82be86d2017-09-20 17:00:17 -07001153 /* If not multiplexing, make sure the connection is fine for HTTP/1
1154 pipelining */
1155 struct Curl_easy* sh = gethandleathead(&check->send_pipe);
1156 struct Curl_easy* rh = gethandleathead(&check->recv_pipe);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001157 if(sh) {
Elliott Hughes82be86d2017-09-20 17:00:17 -07001158 if(!(IsPipeliningPossible(sh, check) & CURLPIPE_HTTP1))
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001159 continue;
1160 }
1161 else if(rh) {
Elliott Hughes82be86d2017-09-20 17:00:17 -07001162 if(!(IsPipeliningPossible(rh, check) & CURLPIPE_HTTP1))
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001163 continue;
1164 }
1165 }
1166 }
1167 else {
1168 if(pipeLen > 0) {
1169 /* can only happen within multi handles, and means that another easy
1170 handle is using this connection */
1171 continue;
1172 }
1173
1174 if(Curl_resolver_asynch()) {
1175 /* ip_addr_str[0] is NUL only if the resolving of the name hasn't
1176 completed yet and until then we don't re-use this connection */
1177 if(!check->ip_addr_str[0]) {
1178 infof(data,
1179 "Connection #%ld is still name resolving, can't reuse\n",
1180 check->connection_id);
1181 continue;
1182 }
1183 }
1184
1185 if((check->sock[FIRSTSOCKET] == CURL_SOCKET_BAD) ||
1186 check->bits.close) {
Alex Deymod15eaac2016-06-28 14:49:26 -07001187 if(!check->bits.close)
1188 foundPendingCandidate = TRUE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001189 /* Don't pick a connection that hasn't connected yet or that is going
1190 to get closed. */
1191 infof(data, "Connection #%ld isn't open enough, can't reuse\n",
1192 check->connection_id);
1193#ifdef DEBUGBUILD
Elliott Hughes82be86d2017-09-20 17:00:17 -07001194 if(check->recv_pipe.size > 0) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001195 infof(data,
1196 "BAD! Unconnected #%ld has a non-empty recv pipeline!\n",
1197 check->connection_id);
1198 }
1199#endif
1200 continue;
1201 }
1202 }
1203
Elliott Hughescee03382017-06-23 12:17:18 -07001204#ifdef USE_UNIX_SOCKETS
1205 if(needle->unix_domain_socket) {
1206 if(!check->unix_domain_socket)
1207 continue;
1208 if(strcmp(needle->unix_domain_socket, check->unix_domain_socket))
1209 continue;
Elliott Hughes82be86d2017-09-20 17:00:17 -07001210 if(needle->abstract_unix_socket != check->abstract_unix_socket)
1211 continue;
Elliott Hughescee03382017-06-23 12:17:18 -07001212 }
1213 else if(check->unix_domain_socket)
1214 continue;
1215#endif
1216
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001217 if((needle->handler->flags&PROTOPT_SSL) !=
1218 (check->handler->flags&PROTOPT_SSL))
1219 /* don't do mixed SSL and non-SSL connections */
Alex Deymod15eaac2016-06-28 14:49:26 -07001220 if(get_protocol_family(check->handler->protocol) !=
1221 needle->handler->protocol || !check->tls_upgraded)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001222 /* except protocols that have been upgraded via TLS */
1223 continue;
1224
Elliott Hughescee03382017-06-23 12:17:18 -07001225 if(needle->bits.httpproxy != check->bits.httpproxy ||
1226 needle->bits.socksproxy != check->bits.socksproxy)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001227 continue;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001228
Elliott Hughescee03382017-06-23 12:17:18 -07001229 if(needle->bits.socksproxy && !proxy_info_matches(&needle->socks_proxy,
1230 &check->socks_proxy))
Alex Deymod15eaac2016-06-28 14:49:26 -07001231 continue;
1232
1233 if(needle->bits.conn_to_host != check->bits.conn_to_host)
1234 /* don't mix connections that use the "connect to host" feature and
1235 * connections that don't use this feature */
1236 continue;
1237
1238 if(needle->bits.conn_to_port != check->bits.conn_to_port)
1239 /* don't mix connections that use the "connect to port" feature and
1240 * connections that don't use this feature */
1241 continue;
1242
Elliott Hughescee03382017-06-23 12:17:18 -07001243 if(needle->bits.httpproxy) {
1244 if(!proxy_info_matches(&needle->http_proxy, &check->http_proxy))
1245 continue;
1246
1247 if(needle->bits.tunnel_proxy != check->bits.tunnel_proxy)
1248 continue;
1249
1250 if(needle->http_proxy.proxytype == CURLPROXY_HTTPS) {
1251 /* use https proxy */
1252 if(needle->handler->flags&PROTOPT_SSL) {
1253 /* use double layer ssl */
1254 if(!Curl_ssl_config_matches(&needle->proxy_ssl_config,
1255 &check->proxy_ssl_config))
1256 continue;
1257 if(check->proxy_ssl[FIRSTSOCKET].state != ssl_connection_complete)
1258 continue;
1259 }
1260 else {
1261 if(!Curl_ssl_config_matches(&needle->ssl_config,
1262 &check->ssl_config))
1263 continue;
1264 if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete)
1265 continue;
1266 }
1267 }
1268 }
1269
Elliott Hughes82be86d2017-09-20 17:00:17 -07001270 if(!canpipe && check->inuse)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001271 /* this request can't be pipelined but the checked connection is
1272 already in use so we skip it */
1273 continue;
1274
Elliott Hughes0128fe42018-02-27 14:57:55 -08001275 if((check->inuse) && (check->data->multi != needle->data->multi))
1276 /* this could be subject for pipeline/multiplex use, but only
1277 if they belong to the same multi handle */
1278 continue;
1279
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001280 if(needle->localdev || needle->localport) {
1281 /* If we are bound to a specific local end (IP+port), we must not
1282 re-use a random other one, although if we didn't ask for a
1283 particular one we can reuse one that was bound.
1284
1285 This comparison is a bit rough and too strict. Since the input
1286 parameters can be specified in numerous ways and still end up the
1287 same it would take a lot of processing to make it really accurate.
1288 Instead, this matching will assume that re-uses of bound connections
1289 will most likely also re-use the exact same binding parameters and
1290 missing out a few edge cases shouldn't hurt anyone very much.
1291 */
1292 if((check->localport != needle->localport) ||
1293 (check->localportrange != needle->localportrange) ||
Elliott Hughescee03382017-06-23 12:17:18 -07001294 (needle->localdev &&
1295 (!check->localdev || strcmp(check->localdev, needle->localdev))))
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001296 continue;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001297 }
1298
Alex Deymod15eaac2016-06-28 14:49:26 -07001299 if(!(needle->handler->flags & PROTOPT_CREDSPERREQUEST)) {
1300 /* This protocol requires credentials per connection,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001301 so verify that we're using the same name and password as well */
Elliott Hughescee03382017-06-23 12:17:18 -07001302 if(strcmp(needle->user, check->user) ||
1303 strcmp(needle->passwd, check->passwd)) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001304 /* one of them was different */
1305 continue;
1306 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001307 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001308
Alex Deymod15eaac2016-06-28 14:49:26 -07001309 if(!needle->bits.httpproxy || (needle->handler->flags&PROTOPT_SSL) ||
Elliott Hughescee03382017-06-23 12:17:18 -07001310 needle->bits.tunnel_proxy) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001311 /* The requested connection does not use a HTTP proxy or it uses SSL or
Elliott Hughescee03382017-06-23 12:17:18 -07001312 it is a non-SSL protocol tunneled or it is a non-SSL protocol which
1313 is allowed to be upgraded via TLS */
1314
1315 if((strcasecompare(needle->handler->scheme, check->handler->scheme) ||
Alex Deymod15eaac2016-06-28 14:49:26 -07001316 (get_protocol_family(check->handler->protocol) ==
1317 needle->handler->protocol && check->tls_upgraded)) &&
Elliott Hughescee03382017-06-23 12:17:18 -07001318 (!needle->bits.conn_to_host || strcasecompare(
Alex Deymod15eaac2016-06-28 14:49:26 -07001319 needle->conn_to_host.name, check->conn_to_host.name)) &&
1320 (!needle->bits.conn_to_port ||
1321 needle->conn_to_port == check->conn_to_port) &&
Elliott Hughescee03382017-06-23 12:17:18 -07001322 strcasecompare(needle->host.name, check->host.name) &&
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001323 needle->remote_port == check->remote_port) {
Alex Deymod15eaac2016-06-28 14:49:26 -07001324 /* The schemes match or the the protocol family is the same and the
1325 previous connection was TLS upgraded, and the hostname and host
1326 port match */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001327 if(needle->handler->flags & PROTOPT_SSL) {
1328 /* This is a SSL connection so verify that we're using the same
1329 SSL options as well */
1330 if(!Curl_ssl_config_matches(&needle->ssl_config,
1331 &check->ssl_config)) {
1332 DEBUGF(infof(data,
1333 "Connection #%ld has different SSL parameters, "
1334 "can't reuse\n",
1335 check->connection_id));
1336 continue;
1337 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07001338 if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete) {
Alex Deymod15eaac2016-06-28 14:49:26 -07001339 foundPendingCandidate = TRUE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001340 DEBUGF(infof(data,
1341 "Connection #%ld has not started SSL connect, "
1342 "can't reuse\n",
1343 check->connection_id));
1344 continue;
1345 }
1346 }
1347 match = TRUE;
1348 }
1349 }
Alex Deymod15eaac2016-06-28 14:49:26 -07001350 else {
1351 /* The requested connection is using the same HTTP proxy in normal
1352 mode (no tunneling) */
1353 match = TRUE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001354 }
1355
1356 if(match) {
1357#if defined(USE_NTLM)
1358 /* If we are looking for an HTTP+NTLM connection, check if this is
1359 already authenticating with the right credentials. If not, keep
1360 looking so that we can reuse NTLM connections if
1361 possible. (Especially we must not reuse the same connection if
1362 partway through a handshake!) */
1363 if(wantNTLMhttp) {
Elliott Hughescee03382017-06-23 12:17:18 -07001364 if(strcmp(needle->user, check->user) ||
1365 strcmp(needle->passwd, check->passwd))
Alex Deymod15eaac2016-06-28 14:49:26 -07001366 continue;
1367 }
1368 else if(check->ntlm.state != NTLMSTATE_NONE) {
1369 /* Connection is using NTLM auth but we don't want NTLM */
1370 continue;
1371 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001372
Alex Deymod15eaac2016-06-28 14:49:26 -07001373 /* Same for Proxy NTLM authentication */
1374 if(wantProxyNTLMhttp) {
Elliott Hughescee03382017-06-23 12:17:18 -07001375 /* Both check->http_proxy.user and check->http_proxy.passwd can be
1376 * NULL */
1377 if(!check->http_proxy.user || !check->http_proxy.passwd)
Alex Deymod15eaac2016-06-28 14:49:26 -07001378 continue;
1379
Elliott Hughescee03382017-06-23 12:17:18 -07001380 if(strcmp(needle->http_proxy.user, check->http_proxy.user) ||
1381 strcmp(needle->http_proxy.passwd, check->http_proxy.passwd))
Alex Deymod15eaac2016-06-28 14:49:26 -07001382 continue;
1383 }
1384 else if(check->proxyntlm.state != NTLMSTATE_NONE) {
1385 /* Proxy connection is using NTLM auth but we don't want NTLM */
1386 continue;
1387 }
1388
1389 if(wantNTLMhttp || wantProxyNTLMhttp) {
1390 /* Credentials are already checked, we can use this connection */
1391 chosen = check;
1392
1393 if((wantNTLMhttp &&
1394 (check->ntlm.state != NTLMSTATE_NONE)) ||
1395 (wantProxyNTLMhttp &&
1396 (check->proxyntlm.state != NTLMSTATE_NONE))) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001397 /* We must use this connection, no other */
1398 *force_reuse = TRUE;
1399 break;
1400 }
Alex Deymod15eaac2016-06-28 14:49:26 -07001401
1402 /* Continue look up for a better connection */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001403 continue;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001404 }
1405#endif
Elliott Hughes82be86d2017-09-20 17:00:17 -07001406 if(canpipe) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001407 /* We can pipeline if we want to. Let's continue looking for
1408 the optimal connection to use, i.e the shortest pipe that is not
1409 blacklisted. */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001410
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001411 if(pipeLen == 0) {
1412 /* We have the optimal connection. Let's stop looking. */
1413 chosen = check;
1414 break;
1415 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001416
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001417 /* We can't use the connection if the pipe is full */
1418 if(max_pipe_len && (pipeLen >= max_pipe_len)) {
1419 infof(data, "Pipe is full, skip (%zu)\n", pipeLen);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001420 continue;
1421 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001422#ifdef USE_NGHTTP2
1423 /* If multiplexed, make sure we don't go over concurrency limit */
1424 if(check->bits.multiplex) {
1425 /* Multiplexed connections can only be HTTP/2 for now */
1426 struct http_conn *httpc = &check->proto.httpc;
1427 if(pipeLen >= httpc->settings.max_concurrent_streams) {
1428 infof(data, "MAX_CONCURRENT_STREAMS reached, skip (%zu)\n",
1429 pipeLen);
1430 continue;
1431 }
1432 }
1433#endif
1434 /* We can't use the connection if the pipe is penalized */
1435 if(Curl_pipeline_penalized(data, check)) {
1436 infof(data, "Penalized, skip\n");
Kristian Monsen5ab50182010-05-14 18:53:44 +01001437 continue;
1438 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001439
1440 if(max_pipe_len) {
1441 if(pipeLen < best_pipe_len) {
1442 /* This connection has a shorter pipe so far. We'll pick this
1443 and continue searching */
1444 chosen = check;
1445 best_pipe_len = pipeLen;
1446 continue;
1447 }
1448 }
1449 else {
1450 /* When not pipelining (== multiplexed), we have a match here! */
1451 chosen = check;
1452 infof(data, "Multiplexed connection found!\n");
1453 break;
1454 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001455 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001456 else {
1457 /* We have found a connection. Let's stop searching. */
1458 chosen = check;
1459 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001460 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001461 }
1462 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001463 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001464
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001465 if(chosen) {
Elliott Hughes0128fe42018-02-27 14:57:55 -08001466 /* mark it as used before releasing the lock */
1467 chosen->inuse = TRUE;
1468 Curl_conncache_unlock(needle);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001469 *usethis = chosen;
1470 return TRUE; /* yes, we found one to use! */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001471 }
Elliott Hughes0128fe42018-02-27 14:57:55 -08001472 Curl_conncache_unlock(needle);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001473
Alex Deymod15eaac2016-06-28 14:49:26 -07001474 if(foundPendingCandidate && data->set.pipewait) {
1475 infof(data,
1476 "Found pending candidate for reuse and CURLOPT_PIPEWAIT is set\n");
1477 *waitpipe = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001478 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001479
Alex Deymod15eaac2016-06-28 14:49:26 -07001480 return FALSE; /* no matching connecting exists */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001481}
1482
1483/* after a TCP connection to the proxy has been verified, this function does
1484 the next magic step.
1485
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001486 Note: this function's sub-functions call failf()
Kristian Monsen5ab50182010-05-14 18:53:44 +01001487
1488*/
Elliott Hughescee03382017-06-23 12:17:18 -07001489CURLcode Curl_connected_proxy(struct connectdata *conn, int sockindex)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001490{
Elliott Hughescee03382017-06-23 12:17:18 -07001491 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001492
Elliott Hughescee03382017-06-23 12:17:18 -07001493 if(conn->bits.socksproxy) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001494#ifndef CURL_DISABLE_PROXY
Elliott Hughes82be86d2017-09-20 17:00:17 -07001495 /* for the secondary socket (FTP), use the "connect to host"
1496 * but ignore the "connect to port" (use the secondary port)
1497 */
1498 const char * const host = conn->bits.httpproxy ?
Elliott Hughescee03382017-06-23 12:17:18 -07001499 conn->http_proxy.host.name :
Elliott Hughes82be86d2017-09-20 17:00:17 -07001500 conn->bits.conn_to_host ?
1501 conn->conn_to_host.name :
Elliott Hughescee03382017-06-23 12:17:18 -07001502 sockindex == SECONDARYSOCKET ?
1503 conn->secondaryhostname : conn->host.name;
Elliott Hughes82be86d2017-09-20 17:00:17 -07001504 const int port = conn->bits.httpproxy ? (int)conn->http_proxy.port :
1505 sockindex == SECONDARYSOCKET ? conn->secondary_port :
1506 conn->bits.conn_to_port ? conn->conn_to_port :
1507 conn->remote_port;
Elliott Hughescee03382017-06-23 12:17:18 -07001508 conn->bits.socksproxy_connecting = TRUE;
1509 switch(conn->socks_proxy.proxytype) {
1510 case CURLPROXY_SOCKS5:
1511 case CURLPROXY_SOCKS5_HOSTNAME:
1512 result = Curl_SOCKS5(conn->socks_proxy.user, conn->socks_proxy.passwd,
1513 host, port, sockindex, conn);
1514 break;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001515
Elliott Hughescee03382017-06-23 12:17:18 -07001516 case CURLPROXY_SOCKS4:
1517 case CURLPROXY_SOCKS4A:
1518 result = Curl_SOCKS4(conn->socks_proxy.user, host, port, sockindex,
1519 conn);
1520 break;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001521
Elliott Hughescee03382017-06-23 12:17:18 -07001522 default:
1523 failf(conn->data, "unknown proxytype option given");
1524 result = CURLE_COULDNT_CONNECT;
1525 } /* switch proxytype */
1526 conn->bits.socksproxy_connecting = FALSE;
1527#else
1528 (void)sockindex;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001529#endif /* CURL_DISABLE_PROXY */
Elliott Hughescee03382017-06-23 12:17:18 -07001530 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001531
Elliott Hughescee03382017-06-23 12:17:18 -07001532 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001533}
1534
1535/*
1536 * verboseconnect() displays verbose information after a connect
1537 */
1538#ifndef CURL_DISABLE_VERBOSE_STRINGS
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001539void Curl_verboseconnect(struct connectdata *conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001540{
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001541 if(conn->data->set.verbose)
1542 infof(conn->data, "Connected to %s (%s) port %ld (#%ld)\n",
Elliott Hughescee03382017-06-23 12:17:18 -07001543 conn->bits.socksproxy ? conn->socks_proxy.host.dispname :
1544 conn->bits.httpproxy ? conn->http_proxy.host.dispname :
Elliott Hughes82be86d2017-09-20 17:00:17 -07001545 conn->bits.conn_to_host ? conn->conn_to_host.dispname :
1546 conn->host.dispname,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001547 conn->ip_addr_str, conn->port, conn->connection_id);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001548}
1549#endif
1550
1551int Curl_protocol_getsock(struct connectdata *conn,
1552 curl_socket_t *socks,
1553 int numsocks)
1554{
1555 if(conn->handler->proto_getsock)
1556 return conn->handler->proto_getsock(conn, socks, numsocks);
1557 return GETSOCK_BLANK;
1558}
1559
1560int Curl_doing_getsock(struct connectdata *conn,
1561 curl_socket_t *socks,
1562 int numsocks)
1563{
1564 if(conn && conn->handler->doing_getsock)
1565 return conn->handler->doing_getsock(conn, socks, numsocks);
1566 return GETSOCK_BLANK;
1567}
1568
1569/*
1570 * We are doing protocol-specific connecting and this is being called over and
1571 * over from the multi interface until the connection phase is done on
1572 * protocol layer.
1573 */
1574
1575CURLcode Curl_protocol_connecting(struct connectdata *conn,
1576 bool *done)
1577{
Alex Deymo486467e2017-12-19 19:04:07 +01001578 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001579
1580 if(conn && conn->handler->connecting) {
1581 *done = FALSE;
1582 result = conn->handler->connecting(conn, done);
1583 }
1584 else
1585 *done = TRUE;
1586
1587 return result;
1588}
1589
1590/*
1591 * We are DOING this is being called over and over from the multi interface
1592 * until the DOING phase is done on protocol layer.
1593 */
1594
1595CURLcode Curl_protocol_doing(struct connectdata *conn, bool *done)
1596{
Alex Deymo486467e2017-12-19 19:04:07 +01001597 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001598
1599 if(conn && conn->handler->doing) {
1600 *done = FALSE;
1601 result = conn->handler->doing(conn, done);
1602 }
1603 else
1604 *done = TRUE;
1605
1606 return result;
1607}
1608
1609/*
1610 * We have discovered that the TCP connection has been successful, we can now
1611 * proceed with some action.
1612 *
1613 */
1614CURLcode Curl_protocol_connect(struct connectdata *conn,
1615 bool *protocol_done)
1616{
Alex Deymo486467e2017-12-19 19:04:07 +01001617 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001618
1619 *protocol_done = FALSE;
1620
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001621 if(conn->bits.tcpconnect[FIRSTSOCKET] && conn->bits.protoconnstart) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001622 /* We already are connected, get back. This may happen when the connect
1623 worked fine in the first call, like when we connect to a local server
1624 or proxy. Note that we don't know if the protocol is actually done.
1625
1626 Unless this protocol doesn't have any protocol-connect callback, as
1627 then we know we're done. */
1628 if(!conn->handler->connecting)
1629 *protocol_done = TRUE;
1630
1631 return CURLE_OK;
1632 }
1633
Kristian Monsen5ab50182010-05-14 18:53:44 +01001634 if(!conn->bits.protoconnstart) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001635
Elliott Hughescee03382017-06-23 12:17:18 -07001636 result = Curl_proxy_connect(conn, FIRSTSOCKET);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001637 if(result)
1638 return result;
1639
Elliott Hughescee03382017-06-23 12:17:18 -07001640 if(CONNECT_FIRSTSOCKET_PROXY_SSL())
1641 /* wait for HTTPS proxy SSL initialization to complete */
1642 return CURLE_OK;
1643
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001644 if(conn->bits.tunnel_proxy && conn->bits.httpproxy &&
Elliott Hughes82be86d2017-09-20 17:00:17 -07001645 Curl_connect_ongoing(conn))
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001646 /* when using an HTTP tunnel proxy, await complete tunnel establishment
1647 before proceeding further. Return CURLE_OK so we'll be called again */
1648 return CURLE_OK;
1649
Kristian Monsen5ab50182010-05-14 18:53:44 +01001650 if(conn->handler->connect_it) {
1651 /* is there a protocol-specific connect() procedure? */
1652
Kristian Monsen5ab50182010-05-14 18:53:44 +01001653 /* Call the protocol-specific connect function */
1654 result = conn->handler->connect_it(conn, protocol_done);
1655 }
1656 else
1657 *protocol_done = TRUE;
1658
1659 /* it has started, possibly even completed but that knowledge isn't stored
1660 in this bit! */
1661 if(!result)
1662 conn->bits.protoconnstart = TRUE;
1663 }
1664
1665 return result; /* pass back status */
1666}
1667
1668/*
Elliott Hughes82be86d2017-09-20 17:00:17 -07001669 * Helpers for IDNA conversions.
Kristian Monsen5ab50182010-05-14 18:53:44 +01001670 */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001671static bool is_ASCII_name(const char *hostname)
1672{
Elliott Hughescee03382017-06-23 12:17:18 -07001673 const unsigned char *ch = (const unsigned char *)hostname;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001674
1675 while(*ch) {
1676 if(*ch++ & 0x80)
1677 return FALSE;
1678 }
1679 return TRUE;
1680}
1681
Kristian Monsen5ab50182010-05-14 18:53:44 +01001682/*
1683 * Perform any necessary IDN conversion of hostname
1684 */
Alex Deymo486467e2017-12-19 19:04:07 +01001685static CURLcode fix_hostname(struct connectdata *conn, struct hostname *host)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001686{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001687 size_t len;
Elliott Hughescee03382017-06-23 12:17:18 -07001688 struct Curl_easy *data = conn->data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001689
Elliott Hughescee03382017-06-23 12:17:18 -07001690#ifndef USE_LIBIDN2
Kristian Monsen5ab50182010-05-14 18:53:44 +01001691 (void)data;
1692 (void)conn;
1693#elif defined(CURL_DISABLE_VERBOSE_STRINGS)
1694 (void)conn;
1695#endif
1696
1697 /* set the name we use to display the host name */
1698 host->dispname = host->name;
1699
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001700 len = strlen(host->name);
1701 if(len && (host->name[len-1] == '.'))
1702 /* strip off a single trailing dot if present, primarily for SNI but
1703 there's no use for it */
Alex Deymo486467e2017-12-19 19:04:07 +01001704 host->name[len-1] = 0;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001705
Alex Deymod15eaac2016-06-28 14:49:26 -07001706 /* Check name for non-ASCII and convert hostname to ACE form if we can */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001707 if(!is_ASCII_name(host->name)) {
Elliott Hughescee03382017-06-23 12:17:18 -07001708#ifdef USE_LIBIDN2
1709 if(idn2_check_version(IDN2_VERSION)) {
Alex Deymod15eaac2016-06-28 14:49:26 -07001710 char *ace_hostname = NULL;
Elliott Hughes82be86d2017-09-20 17:00:17 -07001711#if IDN2_VERSION_NUMBER >= 0x00140000
1712 /* IDN2_NFC_INPUT: Normalize input string using normalization form C.
1713 IDN2_NONTRANSITIONAL: Perform Unicode TR46 non-transitional
1714 processing. */
1715 int flags = IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL;
1716#else
1717 int flags = IDN2_NFC_INPUT;
1718#endif
1719 int rc = idn2_lookup_ul((const char *)host->name, &ace_hostname, flags);
Elliott Hughescee03382017-06-23 12:17:18 -07001720 if(rc == IDN2_OK) {
1721 host->encalloc = (char *)ace_hostname;
Alex Deymod15eaac2016-06-28 14:49:26 -07001722 /* change the name pointer to point to the encoded hostname */
1723 host->name = host->encalloc;
1724 }
Alex Deymo486467e2017-12-19 19:04:07 +01001725 else {
1726 failf(data, "Failed to convert %s to ACE; %s\n", host->name,
Elliott Hughescee03382017-06-23 12:17:18 -07001727 idn2_strerror(rc));
Alex Deymo486467e2017-12-19 19:04:07 +01001728 return CURLE_URL_MALFORMAT;
1729 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001730 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001731#elif defined(USE_WIN32_IDN)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001732 char *ace_hostname = NULL;
Alex Deymod15eaac2016-06-28 14:49:26 -07001733
1734 if(curl_win32_idn_to_ascii(host->name, &ace_hostname)) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001735 host->encalloc = ace_hostname;
1736 /* change the name pointer to point to the encoded hostname */
1737 host->name = host->encalloc;
1738 }
Alex Deymo486467e2017-12-19 19:04:07 +01001739 else {
1740 failf(data, "Failed to convert %s to ACE;\n", host->name);
1741 return CURLE_URL_MALFORMAT;
1742 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001743#else
1744 infof(data, "IDN support not present, can't parse Unicode domains\n");
Kristian Monsen5ab50182010-05-14 18:53:44 +01001745#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001746 }
Alex Deymo486467e2017-12-19 19:04:07 +01001747 {
1748 char *hostp;
1749 for(hostp = host->name; *hostp; hostp++) {
1750 if(*hostp <= 32) {
1751 failf(data, "Host name '%s' contains bad letter", host->name);
1752 return CURLE_URL_MALFORMAT;
1753 }
1754 }
1755 }
1756 return CURLE_OK;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001757}
1758
Alex Deymod15eaac2016-06-28 14:49:26 -07001759/*
1760 * Frees data allocated by fix_hostname()
1761 */
1762static void free_fixed_hostname(struct hostname *host)
1763{
Elliott Hughescee03382017-06-23 12:17:18 -07001764#if defined(USE_LIBIDN2)
Alex Deymod15eaac2016-06-28 14:49:26 -07001765 if(host->encalloc) {
Elliott Hughescee03382017-06-23 12:17:18 -07001766 idn2_free(host->encalloc); /* must be freed with idn2_free() since this was
Alex Deymod15eaac2016-06-28 14:49:26 -07001767 allocated by libidn */
1768 host->encalloc = NULL;
1769 }
1770#elif defined(USE_WIN32_IDN)
Alex Deymo486467e2017-12-19 19:04:07 +01001771 free(host->encalloc); /* must be freed with free() since this was
Alex Deymod15eaac2016-06-28 14:49:26 -07001772 allocated by curl_win32_idn_to_ascii */
1773 host->encalloc = NULL;
1774#else
1775 (void)host;
1776#endif
1777}
1778
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001779static void llist_dtor(void *user, void *element)
1780{
1781 (void)user;
1782 (void)element;
1783 /* Do nothing */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001784}
1785
1786/*
1787 * Allocate and initialize a new connectdata object.
1788 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07001789static struct connectdata *allocate_conn(struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001790{
Elliott Hughes0128fe42018-02-27 14:57:55 -08001791 struct connectdata *conn = calloc(1, sizeof(struct connectdata));
Kristian Monsen5ab50182010-05-14 18:53:44 +01001792 if(!conn)
1793 return NULL;
1794
Alex Deymo486467e2017-12-19 19:04:07 +01001795#ifdef USE_SSL
Elliott Hughes0128fe42018-02-27 14:57:55 -08001796 /* The SSL backend-specific data (ssl_backend_data) objects are allocated as
1797 a separate array to ensure suitable alignment.
Alex Deymo486467e2017-12-19 19:04:07 +01001798 Note that these backend pointers can be swapped by vtls (eg ssl backend
1799 data becomes proxy backend data). */
1800 {
Elliott Hughes0128fe42018-02-27 14:57:55 -08001801 size_t sslsize = Curl_ssl->sizeof_ssl_backend_data;
1802 char *ssl = calloc(4, sslsize);
1803 if(!ssl) {
1804 free(conn);
1805 return NULL;
1806 }
1807 conn->ssl_extra = ssl;
1808 conn->ssl[0].backend = (void *)ssl;
1809 conn->ssl[1].backend = (void *)(ssl + sslsize);
1810 conn->proxy_ssl[0].backend = (void *)(ssl + 2 * sslsize);
1811 conn->proxy_ssl[1].backend = (void *)(ssl + 3 * sslsize);
Alex Deymo486467e2017-12-19 19:04:07 +01001812 }
1813#endif
1814
Kristian Monsen5ab50182010-05-14 18:53:44 +01001815 conn->handler = &Curl_handler_dummy; /* Be sure we have a handler defined
1816 already from start to avoid NULL
1817 situations and checks */
1818
1819 /* and we setup a few fields in case we end up actually using this struct */
1820
1821 conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
1822 conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001823 conn->tempsock[0] = CURL_SOCKET_BAD; /* no file descriptor */
1824 conn->tempsock[1] = CURL_SOCKET_BAD; /* no file descriptor */
1825 conn->connection_id = -1; /* no ID */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001826 conn->port = -1; /* unknown at this point */
Elliott Hughes82be86d2017-09-20 17:00:17 -07001827 conn->remote_port = -1; /* unknown at this point */
Alex Deymod15eaac2016-06-28 14:49:26 -07001828#if defined(USE_RECV_BEFORE_SEND_WORKAROUND) && defined(DEBUGBUILD)
1829 conn->postponed[0].bindsock = CURL_SOCKET_BAD; /* no file descriptor */
1830 conn->postponed[1].bindsock = CURL_SOCKET_BAD; /* no file descriptor */
1831#endif /* USE_RECV_BEFORE_SEND_WORKAROUND && DEBUGBUILD */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001832
1833 /* Default protocol-independent behavior doesn't support persistent
1834 connections, so we set this to force-close. Protocols that support
1835 this need to set this to FALSE in their "curl_do" functions. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001836 connclose(conn, "Default to force-close");
Kristian Monsen5ab50182010-05-14 18:53:44 +01001837
1838 /* Store creation time to help future close decision making */
Alex Deymo486467e2017-12-19 19:04:07 +01001839 conn->created = Curl_now();
Kristian Monsen5ab50182010-05-14 18:53:44 +01001840
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001841 conn->data = data; /* Setup the association between this connection
Alex Deymoe3149cc2016-10-05 11:18:42 -07001842 and the Curl_easy */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001843
Elliott Hughescee03382017-06-23 12:17:18 -07001844 conn->http_proxy.proxytype = data->set.proxytype;
Elliott Hughes82be86d2017-09-20 17:00:17 -07001845 conn->socks_proxy.proxytype = CURLPROXY_SOCKS4;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001846
1847#ifdef CURL_DISABLE_PROXY
1848
1849 conn->bits.proxy = FALSE;
1850 conn->bits.httpproxy = FALSE;
Elliott Hughescee03382017-06-23 12:17:18 -07001851 conn->bits.socksproxy = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001852 conn->bits.proxy_user_passwd = FALSE;
1853 conn->bits.tunnel_proxy = FALSE;
1854
1855#else /* CURL_DISABLE_PROXY */
1856
1857 /* note that these two proxy bits are now just on what looks to be
1858 requested, they may be altered down the road */
1859 conn->bits.proxy = (data->set.str[STRING_PROXY] &&
Alex Deymod15eaac2016-06-28 14:49:26 -07001860 *data->set.str[STRING_PROXY]) ? TRUE : FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001861 conn->bits.httpproxy = (conn->bits.proxy &&
Elliott Hughescee03382017-06-23 12:17:18 -07001862 (conn->http_proxy.proxytype == CURLPROXY_HTTP ||
1863 conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0 ||
1864 conn->http_proxy.proxytype == CURLPROXY_HTTPS)) ?
1865 TRUE : FALSE;
1866 conn->bits.socksproxy = (conn->bits.proxy &&
1867 !conn->bits.httpproxy) ? TRUE : FALSE;
1868
Elliott Hughes82be86d2017-09-20 17:00:17 -07001869 if(data->set.str[STRING_PRE_PROXY] && *data->set.str[STRING_PRE_PROXY]) {
Elliott Hughescee03382017-06-23 12:17:18 -07001870 conn->bits.proxy = TRUE;
1871 conn->bits.socksproxy = TRUE;
1872 }
1873
1874 conn->bits.proxy_user_passwd =
1875 (data->set.str[STRING_PROXYUSERNAME]) ? TRUE : FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001876 conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
1877
1878#endif /* CURL_DISABLE_PROXY */
1879
Alex Deymod15eaac2016-06-28 14:49:26 -07001880 conn->bits.user_passwd = (data->set.str[STRING_USERNAME]) ? TRUE : FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001881 conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
1882 conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
1883
Elliott Hughes82be86d2017-09-20 17:00:17 -07001884 conn->ssl_config.verifystatus = data->set.ssl.primary.verifystatus;
Elliott Hughescee03382017-06-23 12:17:18 -07001885 conn->ssl_config.verifypeer = data->set.ssl.primary.verifypeer;
1886 conn->ssl_config.verifyhost = data->set.ssl.primary.verifyhost;
Elliott Hughes82be86d2017-09-20 17:00:17 -07001887 conn->proxy_ssl_config.verifystatus =
1888 data->set.proxy_ssl.primary.verifystatus;
Elliott Hughescee03382017-06-23 12:17:18 -07001889 conn->proxy_ssl_config.verifypeer = data->set.proxy_ssl.primary.verifypeer;
1890 conn->proxy_ssl_config.verifyhost = data->set.proxy_ssl.primary.verifyhost;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001891
1892 conn->ip_version = data->set.ipver;
1893
1894#if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
1895 defined(NTLM_WB_ENABLED)
1896 conn->ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
1897 conn->ntlm_auth_hlpr_pid = 0;
1898 conn->challenge_header = NULL;
1899 conn->response_header = NULL;
1900#endif
1901
1902 if(Curl_pipeline_wanted(data->multi, CURLPIPE_HTTP1) &&
1903 !conn->master_buffer) {
1904 /* Allocate master_buffer to be used for HTTP/1 pipelining */
Elliott Hughes82be86d2017-09-20 17:00:17 -07001905 conn->master_buffer = calloc(MASTERBUF_SIZE, sizeof(char));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001906 if(!conn->master_buffer)
1907 goto error;
1908 }
1909
1910 /* Initialize the pipeline lists */
Elliott Hughes82be86d2017-09-20 17:00:17 -07001911 Curl_llist_init(&conn->send_pipe, (curl_llist_dtor) llist_dtor);
1912 Curl_llist_init(&conn->recv_pipe, (curl_llist_dtor) llist_dtor);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001913
1914#ifdef HAVE_GSSAPI
1915 conn->data_prot = PROT_CLEAR;
1916#endif
1917
1918 /* Store the local bind parameters that will be used for this connection */
1919 if(data->set.str[STRING_DEVICE]) {
1920 conn->localdev = strdup(data->set.str[STRING_DEVICE]);
1921 if(!conn->localdev)
1922 goto error;
1923 }
1924 conn->localportrange = data->set.localportrange;
1925 conn->localport = data->set.localport;
1926
1927 /* the close socket stuff needs to be copied to the connection struct as
Alex Deymoe3149cc2016-10-05 11:18:42 -07001928 it may live on without (this specific) Curl_easy */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001929 conn->fclosesocket = data->set.fclosesocket;
1930 conn->closesocket_client = data->set.closesocket_client;
1931
Kristian Monsen5ab50182010-05-14 18:53:44 +01001932 return conn;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001933 error:
1934
Elliott Hughes82be86d2017-09-20 17:00:17 -07001935 Curl_llist_destroy(&conn->send_pipe, NULL);
1936 Curl_llist_destroy(&conn->recv_pipe, NULL);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001937
1938 free(conn->master_buffer);
1939 free(conn->localdev);
Elliott Hughes0128fe42018-02-27 14:57:55 -08001940#ifdef USE_SSL
1941 free(conn->ssl_extra);
1942#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001943 free(conn);
1944 return NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001945}
1946
Alex Deymoe3149cc2016-10-05 11:18:42 -07001947static CURLcode findprotocol(struct Curl_easy *data,
Kristian Monsen5ab50182010-05-14 18:53:44 +01001948 struct connectdata *conn,
1949 const char *protostr)
1950{
1951 const struct Curl_handler * const *pp;
1952 const struct Curl_handler *p;
1953
1954 /* Scan protocol handler table and match against 'protostr' to set a few
1955 variables based on the URL. Now that the handler may be changed later
1956 when the protocol specific setup function is called. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001957 for(pp = protocols; (p = *pp) != NULL; pp++) {
Elliott Hughescee03382017-06-23 12:17:18 -07001958 if(strcasecompare(p->scheme, protostr)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001959 /* Protocol found in table. Check if allowed */
1960 if(!(data->set.allowed_protocols & p->protocol))
1961 /* nope, get out */
1962 break;
1963
1964 /* it is allowed for "normal" request, now do an extra check if this is
1965 the result of a redirect */
1966 if(data->state.this_is_a_follow &&
1967 !(data->set.redir_protocols & p->protocol))
1968 /* nope, get out */
1969 break;
1970
1971 /* Perform setup complement if some. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001972 conn->handler = conn->given = p;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001973
1974 /* 'port' and 'remote_port' are set in setup_connection_internals() */
1975 return CURLE_OK;
1976 }
1977 }
1978
1979
1980 /* The protocol was not found in the table, but we don't have to assign it
1981 to anything since it is already assigned to a dummy-struct in the
1982 create_conn() function when the connectdata struct is allocated. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001983 failf(data, "Protocol \"%s\" not supported or disabled in " LIBCURL_NAME,
Kristian Monsen5ab50182010-05-14 18:53:44 +01001984 protostr);
1985
1986 return CURLE_UNSUPPORTED_PROTOCOL;
1987}
1988
1989/*
1990 * Parse URL and fill in the relevant members of the connection struct.
1991 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07001992static CURLcode parseurlandfillconn(struct Curl_easy *data,
Kristian Monsen5ab50182010-05-14 18:53:44 +01001993 struct connectdata *conn,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001994 bool *prot_missing,
1995 char **userp, char **passwdp,
1996 char **optionsp)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001997{
1998 char *at;
1999 char *fragment;
2000 char *path = data->state.path;
2001 char *query;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002002 int i;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002003 int rc;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002004 const char *protop = "";
2005 CURLcode result;
2006 bool rebuild_url = FALSE;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002007 bool url_has_scheme = FALSE;
2008 char protobuf[16];
Kristian Monsen5ab50182010-05-14 18:53:44 +01002009
2010 *prot_missing = FALSE;
2011
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002012 /* We might pass the entire URL into the request so we need to make sure
2013 * there are no bad characters in there.*/
2014 if(strpbrk(data->change.url, "\r\n")) {
2015 failf(data, "Illegal characters found in URL");
2016 return CURLE_URL_MALFORMAT;
2017 }
2018
Kristian Monsen5ab50182010-05-14 18:53:44 +01002019 /*************************************************************
2020 * Parse the URL.
2021 *
2022 * We need to parse the url even when using the proxy, because we will need
2023 * the hostname and port in case we are trying to SSL connect through the
2024 * proxy -- and we don't know if we will need to use SSL until we parse the
2025 * url ...
2026 ************************************************************/
Elliott Hughes82be86d2017-09-20 17:00:17 -07002027 if(data->change.url[0] == ':') {
2028 failf(data, "Bad URL, colon is first character");
2029 return CURLE_URL_MALFORMAT;
2030 }
2031
2032 /* MSDOS/Windows style drive prefix, eg c: in c:foo */
2033#define STARTS_WITH_DRIVE_PREFIX(str) \
2034 ((('a' <= str[0] && str[0] <= 'z') || \
2035 ('A' <= str[0] && str[0] <= 'Z')) && \
2036 (str[1] == ':'))
2037
Alex Deymo486467e2017-12-19 19:04:07 +01002038 /* MSDOS/Windows style drive prefix, optionally with
2039 * a '|' instead of ':', followed by a slash or NUL */
2040#define STARTS_WITH_URL_DRIVE_PREFIX(str) \
2041 ((('a' <= (str)[0] && (str)[0] <= 'z') || \
2042 ('A' <= (str)[0] && (str)[0] <= 'Z')) && \
2043 ((str)[1] == ':' || (str)[1] == '|') && \
Elliott Hughes0128fe42018-02-27 14:57:55 -08002044 ((str)[2] == '/' || (str)[2] == '\\' || (str)[2] == 0))
Alex Deymo486467e2017-12-19 19:04:07 +01002045
Elliott Hughes82be86d2017-09-20 17:00:17 -07002046 /* Don't mistake a drive letter for a scheme if the default protocol is file.
2047 curld --proto-default file c:/foo/bar.txt */
2048 if(STARTS_WITH_DRIVE_PREFIX(data->change.url) &&
2049 data->set.str[STRING_DEFAULT_PROTOCOL] &&
2050 strcasecompare(data->set.str[STRING_DEFAULT_PROTOCOL], "file")) {
2051 ; /* do nothing */
2052 }
2053 else { /* check for a scheme */
2054 for(i = 0; i < 16 && data->change.url[i]; ++i) {
2055 if(data->change.url[i] == '/')
2056 break;
2057 if(data->change.url[i] == ':') {
2058 url_has_scheme = TRUE;
2059 break;
2060 }
2061 }
2062 }
2063
2064 /* handle the file: scheme */
2065 if((url_has_scheme && strncasecompare(data->change.url, "file:", 5)) ||
2066 (!url_has_scheme && data->set.str[STRING_DEFAULT_PROTOCOL] &&
2067 strcasecompare(data->set.str[STRING_DEFAULT_PROTOCOL], "file"))) {
2068 if(url_has_scheme)
2069 rc = sscanf(data->change.url, "%*15[^\n/:]:%[^\n]", path);
2070 else
2071 rc = sscanf(data->change.url, "%[^\n]", path);
2072
2073 if(rc != 1) {
2074 failf(data, "Bad URL");
2075 return CURLE_URL_MALFORMAT;
2076 }
2077
Alex Deymo486467e2017-12-19 19:04:07 +01002078 /* Extra handling URLs with an authority component (i.e. that start with
2079 * "file://")
2080 *
2081 * We allow omitted hostname (e.g. file:/<path>) -- valid according to
2082 * RFC 8089, but not the (current) WHAT-WG URL spec.
Kristian Monsen5ab50182010-05-14 18:53:44 +01002083 */
Alex Deymo486467e2017-12-19 19:04:07 +01002084 if(url_has_scheme && path[0] == '/' && path[1] == '/') {
2085 /* swallow the two slashes */
2086 char *ptr = &path[2];
2087
2088 /*
2089 * According to RFC 8089, a file: URL can be reliably dereferenced if:
2090 *
2091 * o it has no/blank hostname, or
2092 *
2093 * o the hostname matches "localhost" (case-insensitively), or
2094 *
2095 * o the hostname is a FQDN that resolves to this machine.
2096 *
2097 * For brevity, we only consider URLs with empty, "localhost", or
2098 * "127.0.0.1" hostnames as local.
2099 *
2100 * Additionally, there is an exception for URLs with a Windows drive
2101 * letter in the authority (which was accidentally omitted from RFC 8089
2102 * Appendix E, but believe me, it was meant to be there. --MK)
2103 */
2104 if(ptr[0] != '/' && !STARTS_WITH_URL_DRIVE_PREFIX(ptr)) {
2105 /* the URL includes a host name, it must match "localhost" or
2106 "127.0.0.1" to be valid */
2107 if(!checkprefix("localhost/", ptr) &&
2108 !checkprefix("127.0.0.1/", ptr)) {
2109 failf(data, "Invalid file://hostname/, "
2110 "expected localhost or 127.0.0.1 or none");
2111 return CURLE_URL_MALFORMAT;
2112 }
2113 ptr += 9; /* now points to the slash after the host */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002114 }
Elliott Hughescee03382017-06-23 12:17:18 -07002115
Alex Deymo486467e2017-12-19 19:04:07 +01002116 /* This cannot be done with strcpy, as the memory chunks overlap! */
2117 memmove(path, ptr, strlen(ptr) + 1);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002118 }
2119
Elliott Hughes82be86d2017-09-20 17:00:17 -07002120#if !defined(MSDOS) && !defined(WIN32) && !defined(__CYGWIN__)
Alex Deymo486467e2017-12-19 19:04:07 +01002121 /* Don't allow Windows drive letters when not in Windows.
2122 * This catches both "file:/c:" and "file:c:" */
2123 if(('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) ||
2124 STARTS_WITH_URL_DRIVE_PREFIX(path)) {
Elliott Hughes82be86d2017-09-20 17:00:17 -07002125 failf(data, "File drive letters are only accepted in MSDOS/Windows.");
2126 return CURLE_URL_MALFORMAT;
2127 }
Alex Deymo486467e2017-12-19 19:04:07 +01002128#else
2129 /* If the path starts with a slash and a drive letter, ditch the slash */
2130 if('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) {
2131 /* This cannot be done with strcpy, as the memory chunks overlap! */
2132 memmove(path, &path[1], strlen(&path[1]) + 1);
2133 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07002134#endif
2135
Kristian Monsen5ab50182010-05-14 18:53:44 +01002136 protop = "file"; /* protocol string */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002137 *prot_missing = !url_has_scheme;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002138 }
2139 else {
2140 /* clear path */
Alex Deymoe3149cc2016-10-05 11:18:42 -07002141 char slashbuf[4];
Alex Deymo486467e2017-12-19 19:04:07 +01002142 path[0] = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002143
Alex Deymoe3149cc2016-10-05 11:18:42 -07002144 rc = sscanf(data->change.url,
Elliott Hughes82be86d2017-09-20 17:00:17 -07002145 "%15[^\n/:]:%3[/]%[^\n/?#]%[^\n]",
Alex Deymoe3149cc2016-10-05 11:18:42 -07002146 protobuf, slashbuf, conn->host.name, path);
2147 if(2 == rc) {
2148 failf(data, "Bad URL");
2149 return CURLE_URL_MALFORMAT;
2150 }
2151 if(3 > rc) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002152
2153 /*
2154 * The URL was badly formatted, let's try the browser-style _without_
2155 * protocol specified like 'http://'.
2156 */
Elliott Hughescee03382017-06-23 12:17:18 -07002157 rc = sscanf(data->change.url, "%[^\n/?#]%[^\n]", conn->host.name, path);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002158 if(1 > rc) {
2159 /*
2160 * We couldn't even get this format.
2161 * djgpp 2.04 has a sscanf() bug where 'conn->host.name' is
2162 * assigned, but the return value is EOF!
2163 */
2164#if defined(__DJGPP__) && (DJGPP_MINOR == 4)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002165 if(!(rc == -1 && *conn->host.name))
Kristian Monsen5ab50182010-05-14 18:53:44 +01002166#endif
2167 {
2168 failf(data, "<url> malformed");
2169 return CURLE_URL_MALFORMAT;
2170 }
2171 }
2172
2173 /*
Alex Deymod15eaac2016-06-28 14:49:26 -07002174 * Since there was no protocol part specified in the URL use the
2175 * user-specified default protocol. If we weren't given a default make a
2176 * guess by matching some protocols against the host's outermost
2177 * sub-domain name. Finally if there was no match use HTTP.
Kristian Monsen5ab50182010-05-14 18:53:44 +01002178 */
2179
Alex Deymod15eaac2016-06-28 14:49:26 -07002180 protop = data->set.str[STRING_DEFAULT_PROTOCOL];
2181 if(!protop) {
2182 /* Note: if you add a new protocol, please update the list in
2183 * lib/version.c too! */
2184 if(checkprefix("FTP.", conn->host.name))
2185 protop = "ftp";
2186 else if(checkprefix("DICT.", conn->host.name))
2187 protop = "DICT";
2188 else if(checkprefix("LDAP.", conn->host.name))
2189 protop = "LDAP";
2190 else if(checkprefix("IMAP.", conn->host.name))
2191 protop = "IMAP";
2192 else if(checkprefix("SMTP.", conn->host.name))
2193 protop = "smtp";
2194 else if(checkprefix("POP3.", conn->host.name))
2195 protop = "pop3";
2196 else
2197 protop = "http";
Kristian Monsen5ab50182010-05-14 18:53:44 +01002198 }
2199
2200 *prot_missing = TRUE; /* not given in URL */
2201 }
Alex Deymoe3149cc2016-10-05 11:18:42 -07002202 else {
2203 size_t s = strlen(slashbuf);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002204 protop = protobuf;
Alex Deymoe3149cc2016-10-05 11:18:42 -07002205 if(s != 2) {
2206 infof(data, "Unwillingly accepted illegal URL using %d slash%s!\n",
2207 s, s>1?"es":"");
2208
2209 if(data->change.url_alloc)
2210 free(data->change.url);
2211 /* repair the URL to use two slashes */
2212 data->change.url = aprintf("%s://%s%s",
2213 protobuf, conn->host.name, path);
2214 if(!data->change.url)
2215 return CURLE_OUT_OF_MEMORY;
2216 data->change.url_alloc = TRUE;
2217 }
2218 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002219 }
2220
2221 /* We search for '?' in the host name (but only on the right side of a
2222 * @-letter to allow ?-letters in username and password) to handle things
2223 * like http://example.com?param= (notice the missing '/').
2224 */
2225 at = strchr(conn->host.name, '@');
2226 if(at)
Alex Deymo486467e2017-12-19 19:04:07 +01002227 query = strchr(at + 1, '?');
Kristian Monsen5ab50182010-05-14 18:53:44 +01002228 else
2229 query = strchr(conn->host.name, '?');
2230
2231 if(query) {
2232 /* We must insert a slash before the '?'-letter in the URL. If the URL had
2233 a slash after the '?', that is where the path currently begins and the
2234 '?string' is still part of the host name.
2235
2236 We must move the trailing part from the host name and put it first in
2237 the path. And have it all prefixed with a slash.
2238 */
2239
2240 size_t hostlen = strlen(query);
2241 size_t pathlen = strlen(path);
2242
2243 /* move the existing path plus the zero byte forward, to make room for
2244 the host-name part */
Alex Deymo486467e2017-12-19 19:04:07 +01002245 memmove(path + hostlen + 1, path, pathlen + 1);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002246
2247 /* now copy the trailing host part in front of the existing path */
Alex Deymo486467e2017-12-19 19:04:07 +01002248 memcpy(path + 1, query, hostlen);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002249
2250 path[0]='/'; /* prepend the missing slash */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002251 rebuild_url = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002252
Alex Deymo486467e2017-12-19 19:04:07 +01002253 *query = 0; /* now cut off the hostname at the ? */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002254 }
2255 else if(!path[0]) {
2256 /* if there's no path set, use a single slash */
2257 strcpy(path, "/");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002258 rebuild_url = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002259 }
2260
2261 /* If the URL is malformatted (missing a '/' after hostname before path) we
Elliott Hughescee03382017-06-23 12:17:18 -07002262 * insert a slash here. The only letters except '/' that can start a path is
2263 * '?' and '#' - as controlled by the two sscanf() patterns above.
Kristian Monsen5ab50182010-05-14 18:53:44 +01002264 */
Elliott Hughescee03382017-06-23 12:17:18 -07002265 if(path[0] != '/') {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002266 /* We need this function to deal with overlapping memory areas. We know
2267 that the memory area 'path' points to is 'urllen' bytes big and that
2268 is bigger than the path. Use +1 to move the zero byte too. */
Alex Deymo486467e2017-12-19 19:04:07 +01002269 memmove(&path[1], path, strlen(path) + 1);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002270 path[0] = '/';
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002271 rebuild_url = TRUE;
2272 }
2273 else if(!data->set.path_as_is) {
2274 /* sanitise paths and remove ../ and ./ sequences according to RFC3986 */
2275 char *newp = Curl_dedotdotify(path);
2276 if(!newp)
2277 return CURLE_OUT_OF_MEMORY;
2278
2279 if(strcmp(newp, path)) {
2280 rebuild_url = TRUE;
2281 free(data->state.pathbuffer);
2282 data->state.pathbuffer = newp;
2283 data->state.path = newp;
2284 path = newp;
2285 }
2286 else
2287 free(newp);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002288 }
2289
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002290 /*
2291 * "rebuild_url" means that one or more URL components have been modified so
2292 * we need to generate an updated full version. We need the corrected URL
2293 * when communicating over HTTP proxy and we don't know at this point if
2294 * we're using a proxy or not.
2295 */
2296 if(rebuild_url) {
2297 char *reurl;
2298
2299 size_t plen = strlen(path); /* new path, should be 1 byte longer than
2300 the original */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002301 size_t prefixlen = strlen(conn->host.name);
2302
Elliott Hughes82be86d2017-09-20 17:00:17 -07002303 if(!*prot_missing) {
2304 size_t protolen = strlen(protop);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002305
Elliott Hughes82be86d2017-09-20 17:00:17 -07002306 if(curl_strnequal(protop, data->change.url, protolen))
2307 prefixlen += protolen;
2308 else {
2309 failf(data, "<url> malformed");
2310 return CURLE_URL_MALFORMAT;
2311 }
2312
2313 if(curl_strnequal("://", &data->change.url[protolen], 3))
2314 prefixlen += 3;
2315 /* only file: is allowed to omit one or both slashes */
2316 else if(curl_strnequal("file:", data->change.url, 5))
2317 prefixlen += 1 + (data->change.url[5] == '/');
2318 else {
2319 failf(data, "<url> malformed");
2320 return CURLE_URL_MALFORMAT;
2321 }
2322 }
2323
2324 reurl = malloc(prefixlen + plen + 1);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002325 if(!reurl)
2326 return CURLE_OUT_OF_MEMORY;
2327
2328 /* copy the prefix */
2329 memcpy(reurl, data->change.url, prefixlen);
2330
2331 /* append the trailing piece + zerobyte */
2332 memcpy(&reurl[prefixlen], path, plen + 1);
2333
2334 /* possible free the old one */
2335 if(data->change.url_alloc) {
2336 Curl_safefree(data->change.url);
2337 data->change.url_alloc = FALSE;
2338 }
2339
2340 infof(data, "Rebuilt URL to: %s\n", reurl);
2341
2342 data->change.url = reurl;
2343 data->change.url_alloc = TRUE; /* free this later */
2344 }
2345
Elliott Hughes82be86d2017-09-20 17:00:17 -07002346 result = findprotocol(data, conn, protop);
2347 if(result)
2348 return result;
2349
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002350 /*
2351 * Parse the login details from the URL and strip them out of
2352 * the host name
2353 */
2354 result = parse_url_login(data, conn, userp, passwdp, optionsp);
2355 if(result)
2356 return result;
2357
2358 if(conn->host.name[0] == '[') {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002359 /* This looks like an IPv6 address literal. See if there is an address
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002360 scope if there is no location header */
2361 char *percent = strchr(conn->host.name, '%');
2362 if(percent) {
2363 unsigned int identifier_offset = 3;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002364 char *endp;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002365 unsigned long scope;
2366 if(strncmp("%25", percent, 3) != 0) {
2367 infof(data,
2368 "Please URL encode %% as %%25, see RFC 6874.\n");
2369 identifier_offset = 1;
2370 }
2371 scope = strtoul(percent + identifier_offset, &endp, 10);
2372 if(*endp == ']') {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002373 /* The address scope was well formed. Knock it out of the
2374 hostname. */
Alex Deymo486467e2017-12-19 19:04:07 +01002375 memmove(percent, endp, strlen(endp) + 1);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002376 conn->scope_id = (unsigned int)scope;
2377 }
2378 else {
2379 /* Zone identifier is not numeric */
2380#if defined(HAVE_NET_IF_H) && defined(IFNAMSIZ) && defined(HAVE_IF_NAMETOINDEX)
2381 char ifname[IFNAMSIZ + 2];
2382 char *square_bracket;
2383 unsigned int scopeidx = 0;
2384 strncpy(ifname, percent + identifier_offset, IFNAMSIZ + 2);
2385 /* Ensure nullbyte termination */
2386 ifname[IFNAMSIZ + 1] = '\0';
2387 square_bracket = strchr(ifname, ']');
2388 if(square_bracket) {
2389 /* Remove ']' */
2390 *square_bracket = '\0';
2391 scopeidx = if_nametoindex(ifname);
2392 if(scopeidx == 0) {
2393 infof(data, "Invalid network interface: %s; %s\n", ifname,
2394 strerror(errno));
2395 }
2396 }
2397 if(scopeidx > 0) {
2398 char *p = percent + identifier_offset + strlen(ifname);
2399
2400 /* Remove zone identifier from hostname */
2401 memmove(percent, p, strlen(p) + 1);
2402 conn->scope_id = scopeidx;
2403 }
2404 else
2405#endif /* HAVE_NET_IF_H && IFNAMSIZ */
2406 infof(data, "Invalid IPv6 address format\n");
2407 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002408 }
2409 }
2410
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002411 if(data->set.scope_id)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002412 /* Override any scope that was set above. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002413 conn->scope_id = data->set.scope_id;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002414
2415 /* Remove the fragment part of the path. Per RFC 2396, this is always the
2416 last part of the URI. We are looking for the first '#' so that we deal
2417 gracefully with non conformant URI such as http://example.com#foo#bar. */
2418 fragment = strchr(path, '#');
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002419 if(fragment) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002420 *fragment = 0;
2421
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002422 /* we know the path part ended with a fragment, so we know the full URL
2423 string does too and we need to cut it off from there so it isn't used
2424 over proxy */
2425 fragment = strchr(data->change.url, '#');
2426 if(fragment)
2427 *fragment = 0;
2428 }
2429
Kristian Monsen5ab50182010-05-14 18:53:44 +01002430 /*
2431 * So if the URL was A://B/C#D,
2432 * protop is A
2433 * conn->host.name is B
2434 * data->state.path is /C
2435 */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002436 return CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002437}
2438
Kristian Monsen5ab50182010-05-14 18:53:44 +01002439/*
2440 * If we're doing a resumed transfer, we need to setup our stuff
2441 * properly.
2442 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07002443static CURLcode setup_range(struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002444{
2445 struct UrlState *s = &data->state;
2446 s->resume_from = data->set.set_resume_from;
2447 if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
2448 if(s->rangestringalloc)
2449 free(s->range);
2450
2451 if(s->resume_from)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002452 s->range = aprintf("%" CURL_FORMAT_CURL_OFF_TU "-", s->resume_from);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002453 else
2454 s->range = strdup(data->set.str[STRING_SET_RANGE]);
2455
Alex Deymod15eaac2016-06-28 14:49:26 -07002456 s->rangestringalloc = (s->range) ? TRUE : FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002457
2458 if(!s->range)
2459 return CURLE_OUT_OF_MEMORY;
2460
2461 /* tell ourselves to fetch this range */
2462 s->use_range = TRUE; /* enable range download */
2463 }
2464 else
2465 s->use_range = FALSE; /* disable range download */
2466
2467 return CURLE_OK;
2468}
2469
2470
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002471/*
2472 * setup_connection_internals() -
2473 *
2474 * Setup connection internals specific to the requested protocol in the
Alex Deymoe3149cc2016-10-05 11:18:42 -07002475 * Curl_easy. This is inited and setup before the connection is made but
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002476 * is about the particular protocol that is to be used.
2477 *
2478 * This MUST get called after proxy magic has been figured out.
2479 */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002480static CURLcode setup_connection_internals(struct connectdata *conn)
2481{
2482 const struct Curl_handler * p;
2483 CURLcode result;
Alex Deymoe3149cc2016-10-05 11:18:42 -07002484 struct Curl_easy *data = conn->data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002485
2486 /* in some case in the multi state-machine, we go back to the CONNECT state
2487 and then a second (or third or...) call to this function will be made
2488 without doing a DISCONNECT or DONE in between (since the connection is
2489 yet in place) and therefore this function needs to first make sure
2490 there's no lingering previous data allocated. */
2491 Curl_free_request_state(data);
2492
2493 memset(&data->req, 0, sizeof(struct SingleRequest));
2494 data->req.maxdownload = -1;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002495
2496 conn->socktype = SOCK_STREAM; /* most of them are TCP streams */
2497
Kristian Monsen5ab50182010-05-14 18:53:44 +01002498 /* Perform setup complement if some. */
2499 p = conn->handler;
2500
2501 if(p->setup_connection) {
2502 result = (*p->setup_connection)(conn);
2503
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002504 if(result)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002505 return result;
2506
2507 p = conn->handler; /* May have changed. */
2508 }
2509
2510 if(conn->port < 0)
2511 /* we check for -1 here since if proxy was detected already, this
2512 was very likely already set to the proxy port */
2513 conn->port = p->defport;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002514
Kristian Monsen5ab50182010-05-14 18:53:44 +01002515 return CURLE_OK;
2516}
2517
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002518/*
2519 * Curl_free_request_state() should free temp data that was allocated in the
Alex Deymoe3149cc2016-10-05 11:18:42 -07002520 * Curl_easy for this single request.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002521 */
2522
Alex Deymoe3149cc2016-10-05 11:18:42 -07002523void Curl_free_request_state(struct Curl_easy *data)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002524{
2525 Curl_safefree(data->req.protop);
2526 Curl_safefree(data->req.newurl);
2527}
2528
2529
Kristian Monsen5ab50182010-05-14 18:53:44 +01002530#ifndef CURL_DISABLE_PROXY
2531/****************************************************************
2532* Checks if the host is in the noproxy list. returns true if it matches
2533* and therefore the proxy should NOT be used.
2534****************************************************************/
Elliott Hughescee03382017-06-23 12:17:18 -07002535static bool check_noproxy(const char *name, const char *no_proxy)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002536{
2537 /* no_proxy=domain1.dom,host.domain2.dom
2538 * (a comma-separated list of hosts which should
2539 * not be proxied, or an asterisk to override
2540 * all proxy variables)
2541 */
2542 size_t tok_start;
2543 size_t tok_end;
Elliott Hughescee03382017-06-23 12:17:18 -07002544 const char *separator = ", ";
Kristian Monsen5ab50182010-05-14 18:53:44 +01002545 size_t no_proxy_len;
2546 size_t namelen;
2547 char *endptr;
2548
2549 if(no_proxy && no_proxy[0]) {
Elliott Hughescee03382017-06-23 12:17:18 -07002550 if(strcasecompare("*", no_proxy)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002551 return TRUE;
2552 }
2553
2554 /* NO_PROXY was specified and it wasn't just an asterisk */
2555
2556 no_proxy_len = strlen(no_proxy);
Elliott Hughescac39802018-04-27 16:19:43 -07002557 if(name[0] == '[') {
2558 /* IPv6 numerical address */
2559 endptr = strchr(name, ']');
2560 if(!endptr)
2561 return FALSE;
2562 name++;
2563 }
2564 else
2565 endptr = strchr(name, ':');
Kristian Monsen5ab50182010-05-14 18:53:44 +01002566 if(endptr)
2567 namelen = endptr - name;
2568 else
2569 namelen = strlen(name);
2570
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002571 for(tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
2572 while(tok_start < no_proxy_len &&
2573 strchr(separator, no_proxy[tok_start]) != NULL) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002574 /* Look for the beginning of the token. */
2575 ++tok_start;
2576 }
2577
2578 if(tok_start == no_proxy_len)
2579 break; /* It was all trailing separator chars, no more tokens. */
2580
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002581 for(tok_end = tok_start; tok_end < no_proxy_len &&
2582 strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002583 /* Look for the end of the token. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002584 ;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002585
2586 /* To match previous behaviour, where it was necessary to specify
2587 * ".local.com" to prevent matching "notlocal.com", we will leave
2588 * the '.' off.
2589 */
2590 if(no_proxy[tok_start] == '.')
2591 ++tok_start;
2592
2593 if((tok_end - tok_start) <= namelen) {
2594 /* Match the last part of the name to the domain we are checking. */
2595 const char *checkn = name + namelen - (tok_end - tok_start);
Elliott Hughescee03382017-06-23 12:17:18 -07002596 if(strncasecompare(no_proxy + tok_start, checkn,
Kristian Monsen5ab50182010-05-14 18:53:44 +01002597 tok_end - tok_start)) {
2598 if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
2599 /* We either have an exact match, or the previous character is a .
2600 * so it is within the same domain, so no proxy for this host.
2601 */
2602 return TRUE;
2603 }
2604 }
2605 } /* if((tok_end - tok_start) <= namelen) */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002606 } /* for(tok_start = 0; tok_start < no_proxy_len;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002607 tok_start = tok_end + 1) */
2608 } /* NO_PROXY was specified and it wasn't just an asterisk */
2609
2610 return FALSE;
2611}
2612
Elliott Hughes82be86d2017-09-20 17:00:17 -07002613#ifndef CURL_DISABLE_HTTP
Kristian Monsen5ab50182010-05-14 18:53:44 +01002614/****************************************************************
2615* Detect what (if any) proxy to use. Remember that this selects a host
2616* name and is not limited to HTTP proxies only.
2617* The returned pointer must be freed by the caller (unless NULL)
2618****************************************************************/
2619static char *detect_proxy(struct connectdata *conn)
2620{
2621 char *proxy = NULL;
2622
Kristian Monsen5ab50182010-05-14 18:53:44 +01002623 /* If proxy was not specified, we check for default proxy environment
2624 * variables, to enable i.e Lynx compliance:
2625 *
2626 * http_proxy=http://some.server.dom:port/
2627 * https_proxy=http://some.server.dom:port/
2628 * ftp_proxy=http://some.server.dom:port/
2629 * no_proxy=domain1.dom,host.domain2.dom
2630 * (a comma-separated list of hosts which should
2631 * not be proxied, or an asterisk to override
2632 * all proxy variables)
2633 * all_proxy=http://some.server.dom:port/
2634 * (seems to exist for the CERN www lib. Probably
2635 * the first to check for.)
2636 *
2637 * For compatibility, the all-uppercase versions of these variables are
2638 * checked if the lowercase versions don't exist.
2639 */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002640 char proxy_env[128];
Elliott Hughes82be86d2017-09-20 17:00:17 -07002641 const char *protop = conn->handler->scheme;
2642 char *envp = proxy_env;
2643 char *prox;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002644
Elliott Hughes82be86d2017-09-20 17:00:17 -07002645 /* Now, build <protocol>_proxy and check for such a one to use */
2646 while(*protop)
2647 *envp++ = (char)tolower((int)*protop++);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002648
Elliott Hughes82be86d2017-09-20 17:00:17 -07002649 /* append _proxy */
2650 strcpy(envp, "_proxy");
Kristian Monsen5ab50182010-05-14 18:53:44 +01002651
Elliott Hughes82be86d2017-09-20 17:00:17 -07002652 /* read the protocol proxy: */
Alex Deymo486467e2017-12-19 19:04:07 +01002653 prox = curl_getenv(proxy_env);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002654
Elliott Hughes82be86d2017-09-20 17:00:17 -07002655 /*
2656 * We don't try the uppercase version of HTTP_PROXY because of
2657 * security reasons:
2658 *
2659 * When curl is used in a webserver application
2660 * environment (cgi or php), this environment variable can
2661 * be controlled by the web server user by setting the
2662 * http header 'Proxy:' to some value.
2663 *
2664 * This can cause 'internal' http/ftp requests to be
2665 * arbitrarily redirected by any external attacker.
2666 */
2667 if(!prox && !strcasecompare("http_proxy", proxy_env)) {
2668 /* There was no lowercase variable, try the uppercase version: */
2669 Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
Alex Deymo486467e2017-12-19 19:04:07 +01002670 prox = curl_getenv(proxy_env);
Elliott Hughes82be86d2017-09-20 17:00:17 -07002671 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002672
Elliott Hughes1ef06ba2018-05-30 15:43:58 -07002673 envp = proxy_env;
2674 if(prox) {
Elliott Hughes82be86d2017-09-20 17:00:17 -07002675 proxy = prox; /* use this */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002676 }
Elliott Hughes1ef06ba2018-05-30 15:43:58 -07002677 else {
2678 envp = (char *)"all_proxy";
2679 proxy = curl_getenv(envp); /* default proxy to use */
2680 if(!proxy) {
2681 envp = (char *)"ALL_PROXY";
2682 proxy = curl_getenv(envp);
2683 }
2684 }
2685 if(proxy)
2686 infof(conn->data, "Uses proxy env variable %s == '%s'\n", envp, proxy);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002687
2688 return proxy;
2689}
Elliott Hughes82be86d2017-09-20 17:00:17 -07002690#endif /* CURL_DISABLE_HTTP */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002691
2692/*
2693 * If this is supposed to use a proxy, we need to figure out the proxy
2694 * host name, so that we can re-use an existing connection
2695 * that may exist registered to the same proxy host.
Kristian Monsen5ab50182010-05-14 18:53:44 +01002696 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07002697static CURLcode parse_proxy(struct Curl_easy *data,
Elliott Hughescee03382017-06-23 12:17:18 -07002698 struct connectdata *conn, char *proxy,
2699 curl_proxytype proxytype)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002700{
2701 char *prox_portno;
2702 char *endofprot;
2703
2704 /* We use 'proxyptr' to point to the proxy name from now on... */
2705 char *proxyptr;
2706 char *portptr;
2707 char *atsign;
Elliott Hughescee03382017-06-23 12:17:18 -07002708 long port = -1;
2709 char *proxyuser = NULL;
2710 char *proxypasswd = NULL;
2711 bool sockstype;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002712
2713 /* We do the proxy host string parsing here. We want the host name and the
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002714 * port name. Accept a protocol:// prefix
Kristian Monsen5ab50182010-05-14 18:53:44 +01002715 */
2716
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002717 /* Parse the protocol part if present */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002718 endofprot = strstr(proxy, "://");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002719 if(endofprot) {
Alex Deymo486467e2017-12-19 19:04:07 +01002720 proxyptr = endofprot + 3;
Elliott Hughescee03382017-06-23 12:17:18 -07002721 if(checkprefix("https", proxy))
2722 proxytype = CURLPROXY_HTTPS;
2723 else if(checkprefix("socks5h", proxy))
2724 proxytype = CURLPROXY_SOCKS5_HOSTNAME;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002725 else if(checkprefix("socks5", proxy))
Elliott Hughescee03382017-06-23 12:17:18 -07002726 proxytype = CURLPROXY_SOCKS5;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002727 else if(checkprefix("socks4a", proxy))
Elliott Hughescee03382017-06-23 12:17:18 -07002728 proxytype = CURLPROXY_SOCKS4A;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002729 else if(checkprefix("socks4", proxy) || checkprefix("socks", proxy))
Elliott Hughescee03382017-06-23 12:17:18 -07002730 proxytype = CURLPROXY_SOCKS4;
2731 else if(checkprefix("http:", proxy))
2732 ; /* leave it as HTTP or HTTP/1.0 */
2733 else {
2734 /* Any other xxx:// reject! */
2735 failf(data, "Unsupported proxy scheme for \'%s\'", proxy);
2736 return CURLE_COULDNT_CONNECT;
2737 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002738 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002739 else
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002740 proxyptr = proxy; /* No xxx:// head: It's a HTTP proxy */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002741
Alex Deymo486467e2017-12-19 19:04:07 +01002742#ifdef USE_SSL
Elliott Hughes1ef06ba2018-05-30 15:43:58 -07002743 if(!(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY))
Elliott Hughes82be86d2017-09-20 17:00:17 -07002744#endif
Alex Deymo486467e2017-12-19 19:04:07 +01002745 if(proxytype == CURLPROXY_HTTPS) {
2746 failf(data, "Unsupported proxy \'%s\', libcurl is built without the "
2747 "HTTPS-proxy support.", proxy);
2748 return CURLE_NOT_BUILT_IN;
2749 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07002750
Elliott Hughescee03382017-06-23 12:17:18 -07002751 sockstype = proxytype == CURLPROXY_SOCKS5_HOSTNAME ||
2752 proxytype == CURLPROXY_SOCKS5 ||
2753 proxytype == CURLPROXY_SOCKS4A ||
2754 proxytype == CURLPROXY_SOCKS4;
2755
Kristian Monsen5ab50182010-05-14 18:53:44 +01002756 /* Is there a username and password given in this proxy url? */
2757 atsign = strchr(proxyptr, '@');
2758 if(atsign) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002759 CURLcode result =
Alex Deymo486467e2017-12-19 19:04:07 +01002760 Curl_parse_login_details(proxyptr, atsign - proxyptr,
2761 &proxyuser, &proxypasswd, NULL);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002762 if(result)
2763 return result;
Elliott Hughescee03382017-06-23 12:17:18 -07002764 proxyptr = atsign + 1;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002765 }
2766
2767 /* start scanning for port number at this point */
2768 portptr = proxyptr;
2769
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002770 /* detect and extract RFC6874-style IPv6-addresses */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002771 if(*proxyptr == '[') {
2772 char *ptr = ++proxyptr; /* advance beyond the initial bracket */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002773 while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '.')))
Kristian Monsen5ab50182010-05-14 18:53:44 +01002774 ptr++;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002775 if(*ptr == '%') {
2776 /* There might be a zone identifier */
2777 if(strncmp("%25", ptr, 3))
2778 infof(data, "Please URL encode %% as %%25, see RFC 6874.\n");
2779 ptr++;
Alex Deymod15eaac2016-06-28 14:49:26 -07002780 /* Allow unreserved characters as defined in RFC 3986 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002781 while(*ptr && (ISALPHA(*ptr) || ISXDIGIT(*ptr) || (*ptr == '-') ||
2782 (*ptr == '.') || (*ptr == '_') || (*ptr == '~')))
2783 ptr++;
2784 }
2785 if(*ptr == ']')
Kristian Monsen5ab50182010-05-14 18:53:44 +01002786 /* yeps, it ended nicely with a bracket as well */
2787 *ptr++ = 0;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002788 else
Kristian Monsen5ab50182010-05-14 18:53:44 +01002789 infof(data, "Invalid IPv6 address format\n");
2790 portptr = ptr;
2791 /* Note that if this didn't end with a bracket, we still advanced the
2792 * proxyptr first, but I can't see anything wrong with that as no host
2793 * name nor a numeric can legally start with a bracket.
2794 */
2795 }
2796
2797 /* Get port number off proxy.server.com:1080 */
2798 prox_portno = strchr(portptr, ':');
2799 if(prox_portno) {
Alex Deymod15eaac2016-06-28 14:49:26 -07002800 char *endp = NULL;
Elliott Hughescee03382017-06-23 12:17:18 -07002801
Kristian Monsen5ab50182010-05-14 18:53:44 +01002802 *prox_portno = 0x0; /* cut off number from host name */
2803 prox_portno ++;
2804 /* now set the local port number */
Alex Deymod15eaac2016-06-28 14:49:26 -07002805 port = strtol(prox_portno, &endp, 10);
2806 if((endp && *endp && (*endp != '/') && (*endp != ' ')) ||
2807 (port < 0) || (port > 65535)) {
2808 /* meant to detect for example invalid IPv6 numerical addresses without
2809 brackets: "2a00:fac0:a000::7:13". Accept a trailing slash only
2810 because we then allow "URL style" with the number followed by a
2811 slash, used in curl test cases already. Space is also an acceptable
2812 terminating symbol. */
2813 infof(data, "No valid port number in proxy string (%s)\n",
2814 prox_portno);
2815 }
2816 else
2817 conn->port = port;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002818 }
2819 else {
Elliott Hughes82be86d2017-09-20 17:00:17 -07002820 if(proxyptr[0]=='/') {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002821 /* If the first character in the proxy string is a slash, fail
2822 immediately. The following code will otherwise clear the string which
2823 will lead to code running as if no proxy was set! */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002824 Curl_safefree(proxyuser);
2825 Curl_safefree(proxypasswd);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002826 return CURLE_COULDNT_RESOLVE_PROXY;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002827 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002828
Kristian Monsen5ab50182010-05-14 18:53:44 +01002829 /* without a port number after the host name, some people seem to use
2830 a slash so we strip everything from the first slash */
2831 atsign = strchr(proxyptr, '/');
2832 if(atsign)
Alex Deymod15eaac2016-06-28 14:49:26 -07002833 *atsign = '\0'; /* cut off path part from host name */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002834
2835 if(data->set.proxyport)
2836 /* None given in the proxy string, then get the default one if it is
2837 given */
Elliott Hughescee03382017-06-23 12:17:18 -07002838 port = data->set.proxyport;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002839 else {
2840 if(proxytype == CURLPROXY_HTTPS)
2841 port = CURL_DEFAULT_HTTPS_PROXY_PORT;
2842 else
2843 port = CURL_DEFAULT_PROXY_PORT;
2844 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002845 }
2846
Elliott Hughescee03382017-06-23 12:17:18 -07002847 if(*proxyptr) {
2848 struct proxy_info *proxyinfo =
2849 sockstype ? &conn->socks_proxy : &conn->http_proxy;
2850 proxyinfo->proxytype = proxytype;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002851
Elliott Hughescee03382017-06-23 12:17:18 -07002852 if(proxyuser) {
2853 /* found user and password, rip them out. note that we are unescaping
2854 them, as there is otherwise no way to have a username or password
2855 with reserved characters like ':' in them. */
2856 Curl_safefree(proxyinfo->user);
2857 proxyinfo->user = curl_easy_unescape(data, proxyuser, 0, NULL);
Elliott Hughes82be86d2017-09-20 17:00:17 -07002858 Curl_safefree(proxyuser);
Elliott Hughescee03382017-06-23 12:17:18 -07002859
Elliott Hughes82be86d2017-09-20 17:00:17 -07002860 if(!proxyinfo->user) {
2861 Curl_safefree(proxypasswd);
Elliott Hughescee03382017-06-23 12:17:18 -07002862 return CURLE_OUT_OF_MEMORY;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002863 }
Elliott Hughescee03382017-06-23 12:17:18 -07002864
2865 Curl_safefree(proxyinfo->passwd);
2866 if(proxypasswd && strlen(proxypasswd) < MAX_CURL_PASSWORD_LENGTH)
2867 proxyinfo->passwd = curl_easy_unescape(data, proxypasswd, 0, NULL);
2868 else
2869 proxyinfo->passwd = strdup("");
Elliott Hughes82be86d2017-09-20 17:00:17 -07002870 Curl_safefree(proxypasswd);
Elliott Hughescee03382017-06-23 12:17:18 -07002871
2872 if(!proxyinfo->passwd)
2873 return CURLE_OUT_OF_MEMORY;
2874
2875 conn->bits.proxy_user_passwd = TRUE; /* enable it */
2876 }
2877
2878 if(port >= 0) {
2879 proxyinfo->port = port;
2880 if(conn->port < 0 || sockstype || !conn->socks_proxy.host.rawalloc)
2881 conn->port = port;
2882 }
2883
2884 /* now, clone the cleaned proxy host name */
2885 Curl_safefree(proxyinfo->host.rawalloc);
2886 proxyinfo->host.rawalloc = strdup(proxyptr);
2887 proxyinfo->host.name = proxyinfo->host.rawalloc;
2888
2889 if(!proxyinfo->host.rawalloc)
2890 return CURLE_OUT_OF_MEMORY;
2891 }
2892
2893 Curl_safefree(proxyuser);
2894 Curl_safefree(proxypasswd);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002895
2896 return CURLE_OK;
2897}
2898
2899/*
2900 * Extract the user and password from the authentication string
2901 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07002902static CURLcode parse_proxy_auth(struct Curl_easy *data,
Kristian Monsen5ab50182010-05-14 18:53:44 +01002903 struct connectdata *conn)
2904{
2905 char proxyuser[MAX_CURL_USER_LENGTH]="";
2906 char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
Elliott Hughescee03382017-06-23 12:17:18 -07002907 CURLcode result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002908
2909 if(data->set.str[STRING_PROXYUSERNAME] != NULL) {
2910 strncpy(proxyuser, data->set.str[STRING_PROXYUSERNAME],
2911 MAX_CURL_USER_LENGTH);
2912 proxyuser[MAX_CURL_USER_LENGTH-1] = '\0'; /*To be on safe side*/
2913 }
2914 if(data->set.str[STRING_PROXYPASSWORD] != NULL) {
2915 strncpy(proxypasswd, data->set.str[STRING_PROXYPASSWORD],
2916 MAX_CURL_PASSWORD_LENGTH);
2917 proxypasswd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
2918 }
2919
Elliott Hughescee03382017-06-23 12:17:18 -07002920 result = Curl_urldecode(data, proxyuser, 0, &conn->http_proxy.user, NULL,
2921 FALSE);
2922 if(!result)
2923 result = Curl_urldecode(data, proxypasswd, 0, &conn->http_proxy.passwd,
2924 NULL, FALSE);
2925 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002926}
Elliott Hughes82be86d2017-09-20 17:00:17 -07002927
2928/* create_conn helper to parse and init proxy values. to be called after unix
2929 socket init but before any proxy vars are evaluated. */
2930static CURLcode create_conn_helper_init_proxy(struct connectdata *conn)
2931{
2932 char *proxy = NULL;
2933 char *socksproxy = NULL;
2934 char *no_proxy = NULL;
2935 CURLcode result = CURLE_OK;
2936 struct Curl_easy *data = conn->data;
2937
2938 /*************************************************************
2939 * Extract the user and password from the authentication string
2940 *************************************************************/
2941 if(conn->bits.proxy_user_passwd) {
2942 result = parse_proxy_auth(data, conn);
2943 if(result)
2944 goto out;
2945 }
2946
2947 /*************************************************************
2948 * Detect what (if any) proxy to use
2949 *************************************************************/
2950 if(data->set.str[STRING_PROXY]) {
2951 proxy = strdup(data->set.str[STRING_PROXY]);
2952 /* if global proxy is set, this is it */
2953 if(NULL == proxy) {
2954 failf(data, "memory shortage");
2955 result = CURLE_OUT_OF_MEMORY;
2956 goto out;
2957 }
2958 }
2959
2960 if(data->set.str[STRING_PRE_PROXY]) {
2961 socksproxy = strdup(data->set.str[STRING_PRE_PROXY]);
2962 /* if global socks proxy is set, this is it */
2963 if(NULL == socksproxy) {
2964 failf(data, "memory shortage");
2965 result = CURLE_OUT_OF_MEMORY;
2966 goto out;
2967 }
2968 }
2969
Alex Deymo486467e2017-12-19 19:04:07 +01002970 if(!data->set.str[STRING_NOPROXY]) {
Elliott Hughes1ef06ba2018-05-30 15:43:58 -07002971 const char *p = "no_proxy";
2972 no_proxy = curl_getenv(p);
2973 if(!no_proxy) {
2974 p = "NO_PROXY";
2975 no_proxy = curl_getenv(p);
2976 }
2977 if(no_proxy) {
2978 infof(conn->data, "Uses proxy env variable %s == '%s'\n", p, no_proxy);
2979 }
Alex Deymo486467e2017-12-19 19:04:07 +01002980 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07002981
Alex Deymo486467e2017-12-19 19:04:07 +01002982 if(check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY] ?
2983 data->set.str[STRING_NOPROXY] : no_proxy)) {
Elliott Hughes82be86d2017-09-20 17:00:17 -07002984 Curl_safefree(proxy);
2985 Curl_safefree(socksproxy);
2986 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07002987#ifndef CURL_DISABLE_HTTP
Alex Deymo486467e2017-12-19 19:04:07 +01002988 else if(!proxy && !socksproxy)
Elliott Hughes82be86d2017-09-20 17:00:17 -07002989 /* if the host is not in the noproxy list, detect proxy. */
2990 proxy = detect_proxy(conn);
Elliott Hughes82be86d2017-09-20 17:00:17 -07002991#endif /* CURL_DISABLE_HTTP */
2992
2993 Curl_safefree(no_proxy);
2994
2995#ifdef USE_UNIX_SOCKETS
2996 /* For the time being do not mix proxy and unix domain sockets. See #1274 */
2997 if(proxy && conn->unix_domain_socket) {
2998 free(proxy);
2999 proxy = NULL;
3000 }
3001#endif
3002
3003 if(proxy && (!*proxy || (conn->handler->flags & PROTOPT_NONETWORK))) {
3004 free(proxy); /* Don't bother with an empty proxy string or if the
3005 protocol doesn't work with network */
3006 proxy = NULL;
3007 }
3008 if(socksproxy && (!*socksproxy ||
3009 (conn->handler->flags & PROTOPT_NONETWORK))) {
3010 free(socksproxy); /* Don't bother with an empty socks proxy string or if
3011 the protocol doesn't work with network */
3012 socksproxy = NULL;
3013 }
3014
3015 /***********************************************************************
3016 * If this is supposed to use a proxy, we need to figure out the proxy host
3017 * name, proxy type and port number, so that we can re-use an existing
3018 * connection that may exist registered to the same proxy host.
3019 ***********************************************************************/
3020 if(proxy || socksproxy) {
3021 if(proxy) {
3022 result = parse_proxy(data, conn, proxy, conn->http_proxy.proxytype);
3023 Curl_safefree(proxy); /* parse_proxy copies the proxy string */
3024 if(result)
3025 goto out;
3026 }
3027
3028 if(socksproxy) {
3029 result = parse_proxy(data, conn, socksproxy,
3030 conn->socks_proxy.proxytype);
3031 /* parse_proxy copies the socks proxy string */
3032 Curl_safefree(socksproxy);
3033 if(result)
3034 goto out;
3035 }
3036
3037 if(conn->http_proxy.host.rawalloc) {
3038#ifdef CURL_DISABLE_HTTP
3039 /* asking for a HTTP proxy is a bit funny when HTTP is disabled... */
3040 result = CURLE_UNSUPPORTED_PROTOCOL;
3041 goto out;
3042#else
3043 /* force this connection's protocol to become HTTP if compatible */
3044 if(!(conn->handler->protocol & PROTO_FAMILY_HTTP)) {
3045 if((conn->handler->flags & PROTOPT_PROXY_AS_HTTP) &&
3046 !conn->bits.tunnel_proxy)
3047 conn->handler = &Curl_handler_http;
3048 else
3049 /* if not converting to HTTP over the proxy, enforce tunneling */
3050 conn->bits.tunnel_proxy = TRUE;
3051 }
3052 conn->bits.httpproxy = TRUE;
3053#endif
3054 }
3055 else {
3056 conn->bits.httpproxy = FALSE; /* not a HTTP proxy */
3057 conn->bits.tunnel_proxy = FALSE; /* no tunneling if not HTTP */
3058 }
3059
3060 if(conn->socks_proxy.host.rawalloc) {
3061 if(!conn->http_proxy.host.rawalloc) {
3062 /* once a socks proxy */
3063 if(!conn->socks_proxy.user) {
3064 conn->socks_proxy.user = conn->http_proxy.user;
3065 conn->http_proxy.user = NULL;
3066 Curl_safefree(conn->socks_proxy.passwd);
3067 conn->socks_proxy.passwd = conn->http_proxy.passwd;
3068 conn->http_proxy.passwd = NULL;
3069 }
3070 }
3071 conn->bits.socksproxy = TRUE;
3072 }
3073 else
3074 conn->bits.socksproxy = FALSE; /* not a socks proxy */
3075 }
3076 else {
3077 conn->bits.socksproxy = FALSE;
3078 conn->bits.httpproxy = FALSE;
3079 }
3080 conn->bits.proxy = conn->bits.httpproxy || conn->bits.socksproxy;
3081
3082 if(!conn->bits.proxy) {
3083 /* we aren't using the proxy after all... */
3084 conn->bits.proxy = FALSE;
3085 conn->bits.httpproxy = FALSE;
3086 conn->bits.socksproxy = FALSE;
3087 conn->bits.proxy_user_passwd = FALSE;
3088 conn->bits.tunnel_proxy = FALSE;
3089 }
3090
3091out:
3092
3093 free(socksproxy);
3094 free(proxy);
3095 return result;
3096}
Kristian Monsen5ab50182010-05-14 18:53:44 +01003097#endif /* CURL_DISABLE_PROXY */
3098
3099/*
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003100 * parse_url_login()
Kristian Monsen5ab50182010-05-14 18:53:44 +01003101 *
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003102 * Parse the login details (user name, password and options) from the URL and
3103 * strip them out of the host name
Kristian Monsen5ab50182010-05-14 18:53:44 +01003104 *
3105 * Inputs: data->set.use_netrc (CURLOPT_NETRC)
3106 * conn->host.name
3107 *
3108 * Outputs: (almost :- all currently undefined)
3109 * conn->bits.user_passwd - non-zero if non-default passwords exist
3110 * user - non-zero length if defined
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003111 * passwd - non-zero length if defined
3112 * options - non-zero length if defined
Kristian Monsen5ab50182010-05-14 18:53:44 +01003113 * conn->host.name - remove user name and password
3114 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07003115static CURLcode parse_url_login(struct Curl_easy *data,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003116 struct connectdata *conn,
3117 char **user, char **passwd, char **options)
Kristian Monsen5ab50182010-05-14 18:53:44 +01003118{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003119 CURLcode result = CURLE_OK;
3120 char *userp = NULL;
3121 char *passwdp = NULL;
3122 char *optionsp = NULL;
3123
Kristian Monsen5ab50182010-05-14 18:53:44 +01003124 /* At this point, we're hoping all the other special cases have
3125 * been taken care of, so conn->host.name is at most
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003126 * [user[:password][;options]]@]hostname
Kristian Monsen5ab50182010-05-14 18:53:44 +01003127 *
3128 * We need somewhere to put the embedded details, so do that first.
3129 */
3130
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003131 char *ptr = strchr(conn->host.name, '@');
3132 char *login = conn->host.name;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003133
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003134 DEBUGASSERT(!**user);
3135 DEBUGASSERT(!**passwd);
3136 DEBUGASSERT(!**options);
Elliott Hughes82be86d2017-09-20 17:00:17 -07003137 DEBUGASSERT(conn->handler);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003138
3139 if(!ptr)
3140 goto out;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003141
3142 /* We will now try to extract the
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003143 * possible login information in a string like:
Kristian Monsen5ab50182010-05-14 18:53:44 +01003144 * ftp://user:password@ftp.my.site:8021/README */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003145 conn->host.name = ++ptr;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003146
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003147 /* So the hostname is sane. Only bother interpreting the
3148 * results if we could care. It could still be wasted
3149 * work because it might be overtaken by the programmatically
3150 * set user/passwd, but doing that first adds more cases here :-(
3151 */
Kristian Monsen5ab50182010-05-14 18:53:44 +01003152
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003153 if(data->set.use_netrc == CURL_NETRC_REQUIRED)
3154 goto out;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003155
Elliott Hughes82be86d2017-09-20 17:00:17 -07003156 /* We could use the login information in the URL so extract it. Only parse
3157 options if the handler says we should. */
Alex Deymo486467e2017-12-19 19:04:07 +01003158 result =
3159 Curl_parse_login_details(login, ptr - login - 1,
3160 &userp, &passwdp,
3161 (conn->handler->flags & PROTOPT_URLOPTIONS)?
3162 &optionsp:NULL);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003163 if(result)
3164 goto out;
3165
3166 if(userp) {
3167 char *newname;
3168
3169 /* We have a user in the URL */
Lucas Eckels9bd90e62012-08-06 15:07:02 -07003170 conn->bits.userpwd_in_url = TRUE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003171 conn->bits.user_passwd = TRUE; /* enable user+password */
Kristian Monsen5ab50182010-05-14 18:53:44 +01003172
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003173 /* Decode the user */
Elliott Hughescee03382017-06-23 12:17:18 -07003174 result = Curl_urldecode(data, userp, 0, &newname, NULL, FALSE);
3175 if(result) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003176 goto out;
3177 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003178
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003179 free(*user);
3180 *user = newname;
3181 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003182
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003183 if(passwdp) {
3184 /* We have a password in the URL so decode it */
Elliott Hughescee03382017-06-23 12:17:18 -07003185 char *newpasswd;
3186 result = Curl_urldecode(data, passwdp, 0, &newpasswd, NULL, FALSE);
3187 if(result) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003188 goto out;
3189 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003190
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003191 free(*passwd);
3192 *passwd = newpasswd;
3193 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003194
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003195 if(optionsp) {
3196 /* We have an options list in the URL so decode it */
Elliott Hughescee03382017-06-23 12:17:18 -07003197 char *newoptions;
3198 result = Curl_urldecode(data, optionsp, 0, &newoptions, NULL, FALSE);
3199 if(result) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003200 goto out;
3201 }
3202
3203 free(*options);
3204 *options = newoptions;
3205 }
3206
3207
3208 out:
3209
3210 free(userp);
3211 free(passwdp);
3212 free(optionsp);
3213
3214 return result;
3215}
3216
3217/*
Alex Deymo486467e2017-12-19 19:04:07 +01003218 * Curl_parse_login_details()
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003219 *
3220 * This is used to parse a login string for user name, password and options in
3221 * the following formats:
3222 *
3223 * user
3224 * user:password
3225 * user:password;options
3226 * user;options
3227 * user;options:password
3228 * :password
3229 * :password;options
3230 * ;options
3231 * ;options:password
3232 *
3233 * Parameters:
3234 *
3235 * login [in] - The login string.
3236 * len [in] - The length of the login string.
3237 * userp [in/out] - The address where a pointer to newly allocated memory
3238 * holding the user will be stored upon completion.
3239 * passdwp [in/out] - The address where a pointer to newly allocated memory
3240 * holding the password will be stored upon completion.
3241 * optionsp [in/out] - The address where a pointer to newly allocated memory
3242 * holding the options will be stored upon completion.
3243 *
3244 * Returns CURLE_OK on success.
3245 */
Alex Deymo486467e2017-12-19 19:04:07 +01003246CURLcode Curl_parse_login_details(const char *login, const size_t len,
3247 char **userp, char **passwdp,
3248 char **optionsp)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003249{
3250 CURLcode result = CURLE_OK;
3251 char *ubuf = NULL;
3252 char *pbuf = NULL;
3253 char *obuf = NULL;
3254 const char *psep = NULL;
3255 const char *osep = NULL;
3256 size_t ulen;
3257 size_t plen;
3258 size_t olen;
3259
3260 /* Attempt to find the password separator */
3261 if(passwdp) {
3262 psep = strchr(login, ':');
3263
3264 /* Within the constraint of the login string */
3265 if(psep >= login + len)
3266 psep = NULL;
3267 }
3268
3269 /* Attempt to find the options separator */
3270 if(optionsp) {
3271 osep = strchr(login, ';');
3272
3273 /* Within the constraint of the login string */
3274 if(osep >= login + len)
3275 osep = NULL;
3276 }
3277
3278 /* Calculate the portion lengths */
3279 ulen = (psep ?
3280 (size_t)(osep && psep > osep ? osep - login : psep - login) :
3281 (osep ? (size_t)(osep - login) : len));
3282 plen = (psep ?
3283 (osep && osep > psep ? (size_t)(osep - psep) :
3284 (size_t)(login + len - psep)) - 1 : 0);
3285 olen = (osep ?
3286 (psep && psep > osep ? (size_t)(psep - osep) :
3287 (size_t)(login + len - osep)) - 1 : 0);
3288
3289 /* Allocate the user portion buffer */
3290 if(userp && ulen) {
3291 ubuf = malloc(ulen + 1);
3292 if(!ubuf)
3293 result = CURLE_OUT_OF_MEMORY;
3294 }
3295
3296 /* Allocate the password portion buffer */
3297 if(!result && passwdp && plen) {
3298 pbuf = malloc(plen + 1);
3299 if(!pbuf) {
3300 free(ubuf);
3301 result = CURLE_OUT_OF_MEMORY;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003302 }
3303 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003304
3305 /* Allocate the options portion buffer */
3306 if(!result && optionsp && olen) {
3307 obuf = malloc(olen + 1);
3308 if(!obuf) {
3309 free(pbuf);
3310 free(ubuf);
3311 result = CURLE_OUT_OF_MEMORY;
3312 }
3313 }
3314
3315 if(!result) {
3316 /* Store the user portion if necessary */
3317 if(ubuf) {
3318 memcpy(ubuf, login, ulen);
3319 ubuf[ulen] = '\0';
3320 Curl_safefree(*userp);
3321 *userp = ubuf;
3322 }
3323
3324 /* Store the password portion if necessary */
3325 if(pbuf) {
3326 memcpy(pbuf, psep + 1, plen);
3327 pbuf[plen] = '\0';
3328 Curl_safefree(*passwdp);
3329 *passwdp = pbuf;
3330 }
3331
3332 /* Store the options portion if necessary */
3333 if(obuf) {
3334 memcpy(obuf, osep + 1, olen);
3335 obuf[olen] = '\0';
3336 Curl_safefree(*optionsp);
3337 *optionsp = obuf;
3338 }
3339 }
3340
3341 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003342}
3343
3344/*************************************************************
3345 * Figure out the remote port number and fix it in the URL
3346 *
3347 * No matter if we use a proxy or not, we have to figure out the remote
3348 * port number of various reasons.
3349 *
3350 * To be able to detect port number flawlessly, we must not confuse them
3351 * IPv6-specified addresses in the [0::1] style. (RFC2732)
3352 *
3353 * The conn->host.name is currently [user:passwd@]host[:port] where host
3354 * could be a hostname, IPv4 address or IPv6 address.
3355 *
3356 * The port number embedded in the URL is replaced, if necessary.
3357 *************************************************************/
Alex Deymoe3149cc2016-10-05 11:18:42 -07003358static CURLcode parse_remote_port(struct Curl_easy *data,
Kristian Monsen5ab50182010-05-14 18:53:44 +01003359 struct connectdata *conn)
3360{
3361 char *portptr;
3362 char endbracket;
3363
3364 /* Note that at this point, the IPv6 address cannot contain any scope
3365 suffix as that has already been removed in the parseurlandfillconn()
3366 function */
3367 if((1 == sscanf(conn->host.name, "[%*45[0123456789abcdefABCDEF:.]%c",
3368 &endbracket)) &&
3369 (']' == endbracket)) {
3370 /* this is a RFC2732-style specified IP-address */
3371 conn->bits.ipv6_ip = TRUE;
3372
3373 conn->host.name++; /* skip over the starting bracket */
3374 portptr = strchr(conn->host.name, ']');
3375 if(portptr) {
3376 *portptr++ = '\0'; /* zero terminate, killing the bracket */
Alex Deymo486467e2017-12-19 19:04:07 +01003377 if(*portptr) {
3378 if (*portptr != ':') {
3379 failf(data, "IPv6 closing bracket followed by '%c'", *portptr);
3380 return CURLE_URL_MALFORMAT;
3381 }
3382 }
3383 else
Kristian Monsen5ab50182010-05-14 18:53:44 +01003384 portptr = NULL; /* no port number available */
3385 }
3386 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003387 else {
3388#ifdef ENABLE_IPV6
3389 struct in6_addr in6;
3390 if(Curl_inet_pton(AF_INET6, conn->host.name, &in6) > 0) {
3391 /* This is a numerical IPv6 address, meaning this is a wrongly formatted
3392 URL */
3393 failf(data, "IPv6 numerical address used in URL without brackets");
3394 return CURLE_URL_MALFORMAT;
3395 }
3396#endif
3397
Elliott Hughes82be86d2017-09-20 17:00:17 -07003398 portptr = strchr(conn->host.name, ':');
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003399 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003400
3401 if(data->set.use_port && data->state.allow_port) {
3402 /* if set, we use this and ignore the port possibly given in the URL */
3403 conn->remote_port = (unsigned short)data->set.use_port;
3404 if(portptr)
3405 *portptr = '\0'; /* cut off the name there anyway - if there was a port
3406 number - since the port number is to be ignored! */
3407 if(conn->bits.httpproxy) {
3408 /* we need to create new URL with the new port number */
3409 char *url;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07003410 char type[12]="";
3411
3412 if(conn->bits.type_set)
3413 snprintf(type, sizeof(type), ";type=%c",
3414 data->set.prefer_ascii?'A':
3415 (data->set.ftp_list_only?'D':'I'));
Kristian Monsen5ab50182010-05-14 18:53:44 +01003416
3417 /*
Lucas Eckels9bd90e62012-08-06 15:07:02 -07003418 * This synthesized URL isn't always right--suffixes like ;type=A are
3419 * stripped off. It would be better to work directly from the original
3420 * URL and simply replace the port part of it.
Kristian Monsen5ab50182010-05-14 18:53:44 +01003421 */
Elliott Hughes1ef06ba2018-05-30 15:43:58 -07003422 url = aprintf("%s://%s%s%s:%d%s%s%s", conn->given->scheme,
Kristian Monsen5ab50182010-05-14 18:53:44 +01003423 conn->bits.ipv6_ip?"[":"", conn->host.name,
3424 conn->bits.ipv6_ip?"]":"", conn->remote_port,
Lucas Eckels9bd90e62012-08-06 15:07:02 -07003425 data->state.slash_removed?"/":"", data->state.path,
3426 type);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003427 if(!url)
3428 return CURLE_OUT_OF_MEMORY;
3429
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003430 if(data->change.url_alloc) {
3431 Curl_safefree(data->change.url);
3432 data->change.url_alloc = FALSE;
3433 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003434
3435 data->change.url = url;
3436 data->change.url_alloc = TRUE;
3437 }
3438 }
3439 else if(portptr) {
3440 /* no CURLOPT_PORT given, extract the one from the URL */
3441
3442 char *rest;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003443 long port;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003444
Alex Deymo486467e2017-12-19 19:04:07 +01003445 port = strtol(portptr + 1, &rest, 10); /* Port number must be decimal */
Kristian Monsen5ab50182010-05-14 18:53:44 +01003446
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003447 if((port < 0) || (port > 0xffff)) {
3448 /* Single unix standard says port numbers are 16 bits long */
3449 failf(data, "Port number out of range");
3450 return CURLE_URL_MALFORMAT;
3451 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003452
Elliott Hughes82be86d2017-09-20 17:00:17 -07003453 if(rest[0]) {
3454 failf(data, "Port number ended with '%c'", rest[0]);
3455 return CURLE_URL_MALFORMAT;
3456 }
3457
3458 if(rest != &portptr[1]) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01003459 *portptr = '\0'; /* cut off the name there */
3460 conn->remote_port = curlx_ultous(port);
3461 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07003462 else {
Lucas Eckels9bd90e62012-08-06 15:07:02 -07003463 /* Browser behavior adaptation. If there's a colon with no digits after,
3464 just cut off the name there which makes us ignore the colon and just
3465 use the default port. Firefox and Chrome both do that. */
3466 *portptr = '\0';
Elliott Hughes82be86d2017-09-20 17:00:17 -07003467 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003468 }
Alex Deymod15eaac2016-06-28 14:49:26 -07003469
3470 /* only if remote_port was not already parsed off the URL we use the
3471 default port number */
3472 if(conn->remote_port < 0)
3473 conn->remote_port = (unsigned short)conn->given->defport;
3474
Kristian Monsen5ab50182010-05-14 18:53:44 +01003475 return CURLE_OK;
3476}
3477
3478/*
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003479 * Override the login details from the URL with that in the CURLOPT_USERPWD
3480 * option or a .netrc file, if applicable.
Kristian Monsen5ab50182010-05-14 18:53:44 +01003481 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07003482static CURLcode override_login(struct Curl_easy *data,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003483 struct connectdata *conn,
3484 char **userp, char **passwdp, char **optionsp)
Kristian Monsen5ab50182010-05-14 18:53:44 +01003485{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003486 if(data->set.str[STRING_USERNAME]) {
3487 free(*userp);
3488 *userp = strdup(data->set.str[STRING_USERNAME]);
3489 if(!*userp)
3490 return CURLE_OUT_OF_MEMORY;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003491 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003492
3493 if(data->set.str[STRING_PASSWORD]) {
3494 free(*passwdp);
3495 *passwdp = strdup(data->set.str[STRING_PASSWORD]);
3496 if(!*passwdp)
3497 return CURLE_OUT_OF_MEMORY;
3498 }
3499
3500 if(data->set.str[STRING_OPTIONS]) {
3501 free(*optionsp);
3502 *optionsp = strdup(data->set.str[STRING_OPTIONS]);
3503 if(!*optionsp)
3504 return CURLE_OUT_OF_MEMORY;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003505 }
3506
3507 conn->bits.netrc = FALSE;
3508 if(data->set.use_netrc != CURL_NETRC_IGNORED) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003509 int ret = Curl_parsenetrc(conn->host.name,
3510 userp, passwdp,
3511 data->set.str[STRING_NETRC_FILE]);
3512 if(ret > 0) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01003513 infof(data, "Couldn't find host %s in the "
3514 DOT_CHAR "netrc file; using defaults\n",
3515 conn->host.name);
3516 }
Alex Deymod15eaac2016-06-28 14:49:26 -07003517 else if(ret < 0) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003518 return CURLE_OUT_OF_MEMORY;
3519 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003520 else {
3521 /* set bits.netrc TRUE to remember that we got the name from a .netrc
3522 file, so that it is safe to use even if we followed a Location: to a
3523 different host or similar. */
3524 conn->bits.netrc = TRUE;
3525
3526 conn->bits.user_passwd = TRUE; /* enable user+password */
3527 }
3528 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003529
3530 return CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003531}
3532
3533/*
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003534 * Set the login details so they're available in the connection
Kristian Monsen5ab50182010-05-14 18:53:44 +01003535 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003536static CURLcode set_login(struct connectdata *conn,
3537 const char *user, const char *passwd,
3538 const char *options)
Kristian Monsen5ab50182010-05-14 18:53:44 +01003539{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003540 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003541
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003542 /* If our protocol needs a password and we have none, use the defaults */
3543 if((conn->handler->flags & PROTOPT_NEEDSPWD) && !conn->bits.user_passwd) {
3544 /* Store the default user */
Kristian Monsen5ab50182010-05-14 18:53:44 +01003545 conn->user = strdup(CURL_DEFAULT_USER);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003546
3547 /* Store the default password */
Kristian Monsen5ab50182010-05-14 18:53:44 +01003548 if(conn->user)
3549 conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
3550 else
3551 conn->passwd = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003552
Kristian Monsen5ab50182010-05-14 18:53:44 +01003553 /* This is the default password, so DON'T set conn->bits.user_passwd */
3554 }
3555 else {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003556 /* Store the user, zero-length if not set */
Kristian Monsen5ab50182010-05-14 18:53:44 +01003557 conn->user = strdup(user);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003558
3559 /* Store the password (only if user is present), zero-length if not set */
Kristian Monsen5ab50182010-05-14 18:53:44 +01003560 if(conn->user)
3561 conn->passwd = strdup(passwd);
3562 else
3563 conn->passwd = NULL;
3564 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003565
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003566 if(!conn->user || !conn->passwd)
3567 result = CURLE_OUT_OF_MEMORY;
3568
3569 /* Store the options, null if not set */
3570 if(!result && options[0]) {
3571 conn->options = strdup(options);
3572
3573 if(!conn->options)
3574 result = CURLE_OUT_OF_MEMORY;
3575 }
3576
3577 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003578}
3579
Alex Deymod15eaac2016-06-28 14:49:26 -07003580/*
3581 * Parses a "host:port" string to connect to.
3582 * The hostname and the port may be empty; in this case, NULL is returned for
3583 * the hostname and -1 for the port.
3584 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07003585static CURLcode parse_connect_to_host_port(struct Curl_easy *data,
Alex Deymod15eaac2016-06-28 14:49:26 -07003586 const char *host,
3587 char **hostname_result,
3588 int *port_result)
3589{
3590 char *host_dup;
3591 char *hostptr;
3592 char *host_portno;
3593 char *portptr;
3594 int port = -1;
3595
Elliott Hughes82be86d2017-09-20 17:00:17 -07003596#if defined(CURL_DISABLE_VERBOSE_STRINGS)
3597 (void) data;
3598#endif
3599
Alex Deymod15eaac2016-06-28 14:49:26 -07003600 *hostname_result = NULL;
3601 *port_result = -1;
3602
3603 if(!host || !*host)
3604 return CURLE_OK;
3605
3606 host_dup = strdup(host);
3607 if(!host_dup)
3608 return CURLE_OUT_OF_MEMORY;
3609
3610 hostptr = host_dup;
3611
3612 /* start scanning for port number at this point */
3613 portptr = hostptr;
3614
3615 /* detect and extract RFC6874-style IPv6-addresses */
3616 if(*hostptr == '[') {
Elliott Hughes0128fe42018-02-27 14:57:55 -08003617#ifdef ENABLE_IPV6
Alex Deymod15eaac2016-06-28 14:49:26 -07003618 char *ptr = ++hostptr; /* advance beyond the initial bracket */
3619 while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '.')))
3620 ptr++;
3621 if(*ptr == '%') {
3622 /* There might be a zone identifier */
3623 if(strncmp("%25", ptr, 3))
3624 infof(data, "Please URL encode %% as %%25, see RFC 6874.\n");
3625 ptr++;
3626 /* Allow unreserved characters as defined in RFC 3986 */
3627 while(*ptr && (ISALPHA(*ptr) || ISXDIGIT(*ptr) || (*ptr == '-') ||
3628 (*ptr == '.') || (*ptr == '_') || (*ptr == '~')))
3629 ptr++;
3630 }
3631 if(*ptr == ']')
3632 /* yeps, it ended nicely with a bracket as well */
3633 *ptr++ = '\0';
3634 else
3635 infof(data, "Invalid IPv6 address format\n");
3636 portptr = ptr;
3637 /* Note that if this didn't end with a bracket, we still advanced the
3638 * hostptr first, but I can't see anything wrong with that as no host
3639 * name nor a numeric can legally start with a bracket.
3640 */
Elliott Hughes0128fe42018-02-27 14:57:55 -08003641#else
3642 failf(data, "Use of IPv6 in *_CONNECT_TO without IPv6 support built-in!");
3643 free(host_dup);
3644 return CURLE_NOT_BUILT_IN;
3645#endif
Alex Deymod15eaac2016-06-28 14:49:26 -07003646 }
3647
3648 /* Get port number off server.com:1080 */
3649 host_portno = strchr(portptr, ':');
3650 if(host_portno) {
3651 char *endp = NULL;
3652 *host_portno = '\0'; /* cut off number from host name */
3653 host_portno++;
3654 if(*host_portno) {
3655 long portparse = strtol(host_portno, &endp, 10);
3656 if((endp && *endp) || (portparse < 0) || (portparse > 65535)) {
3657 infof(data, "No valid port number in connect to host string (%s)\n",
3658 host_portno);
3659 hostptr = NULL;
3660 port = -1;
3661 }
3662 else
3663 port = (int)portparse; /* we know it will fit */
3664 }
3665 }
3666
3667 /* now, clone the cleaned host name */
3668 if(hostptr) {
3669 *hostname_result = strdup(hostptr);
3670 if(!*hostname_result) {
3671 free(host_dup);
3672 return CURLE_OUT_OF_MEMORY;
3673 }
3674 }
3675
3676 *port_result = port;
3677
3678 free(host_dup);
3679 return CURLE_OK;
3680}
3681
3682/*
3683 * Parses one "connect to" string in the form:
3684 * "HOST:PORT:CONNECT-TO-HOST:CONNECT-TO-PORT".
3685 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07003686static CURLcode parse_connect_to_string(struct Curl_easy *data,
Alex Deymod15eaac2016-06-28 14:49:26 -07003687 struct connectdata *conn,
3688 const char *conn_to_host,
3689 char **host_result,
3690 int *port_result)
3691{
3692 CURLcode result = CURLE_OK;
3693 const char *ptr = conn_to_host;
3694 int host_match = FALSE;
3695 int port_match = FALSE;
3696
Elliott Hughes82be86d2017-09-20 17:00:17 -07003697 *host_result = NULL;
3698 *port_result = -1;
3699
Alex Deymod15eaac2016-06-28 14:49:26 -07003700 if(*ptr == ':') {
3701 /* an empty hostname always matches */
3702 host_match = TRUE;
3703 ptr++;
3704 }
3705 else {
3706 /* check whether the URL's hostname matches */
3707 size_t hostname_to_match_len;
3708 char *hostname_to_match = aprintf("%s%s%s",
3709 conn->bits.ipv6_ip ? "[" : "",
3710 conn->host.name,
3711 conn->bits.ipv6_ip ? "]" : "");
3712 if(!hostname_to_match)
3713 return CURLE_OUT_OF_MEMORY;
3714 hostname_to_match_len = strlen(hostname_to_match);
Elliott Hughescee03382017-06-23 12:17:18 -07003715 host_match = strncasecompare(ptr, hostname_to_match,
3716 hostname_to_match_len);
Alex Deymod15eaac2016-06-28 14:49:26 -07003717 free(hostname_to_match);
3718 ptr += hostname_to_match_len;
3719
3720 host_match = host_match && *ptr == ':';
3721 ptr++;
3722 }
3723
3724 if(host_match) {
3725 if(*ptr == ':') {
3726 /* an empty port always matches */
3727 port_match = TRUE;
3728 ptr++;
3729 }
3730 else {
3731 /* check whether the URL's port matches */
3732 char *ptr_next = strchr(ptr, ':');
3733 if(ptr_next) {
3734 char *endp = NULL;
3735 long port_to_match = strtol(ptr, &endp, 10);
3736 if((endp == ptr_next) && (port_to_match == conn->remote_port)) {
3737 port_match = TRUE;
3738 ptr = ptr_next + 1;
3739 }
3740 }
3741 }
3742 }
3743
3744 if(host_match && port_match) {
3745 /* parse the hostname and port to connect to */
3746 result = parse_connect_to_host_port(data, ptr, host_result, port_result);
3747 }
3748
3749 return result;
3750}
3751
3752/*
3753 * Processes all strings in the "connect to" slist, and uses the "connect
3754 * to host" and "connect to port" of the first string that matches.
3755 */
Alex Deymoe3149cc2016-10-05 11:18:42 -07003756static CURLcode parse_connect_to_slist(struct Curl_easy *data,
Alex Deymod15eaac2016-06-28 14:49:26 -07003757 struct connectdata *conn,
3758 struct curl_slist *conn_to_host)
3759{
3760 CURLcode result = CURLE_OK;
3761 char *host = NULL;
Elliott Hughes82be86d2017-09-20 17:00:17 -07003762 int port = -1;
Alex Deymod15eaac2016-06-28 14:49:26 -07003763
Elliott Hughes82be86d2017-09-20 17:00:17 -07003764 while(conn_to_host && !host && port == -1) {
Alex Deymod15eaac2016-06-28 14:49:26 -07003765 result = parse_connect_to_string(data, conn, conn_to_host->data,
3766 &host, &port);
3767 if(result)
3768 return result;
3769
3770 if(host && *host) {
Alex Deymod15eaac2016-06-28 14:49:26 -07003771 conn->conn_to_host.rawalloc = host;
3772 conn->conn_to_host.name = host;
3773 conn->bits.conn_to_host = TRUE;
3774
Elliott Hughes82be86d2017-09-20 17:00:17 -07003775 infof(data, "Connecting to hostname: %s\n", host);
Alex Deymod15eaac2016-06-28 14:49:26 -07003776 }
3777 else {
3778 /* no "connect to host" */
3779 conn->bits.conn_to_host = FALSE;
Elliott Hughes82be86d2017-09-20 17:00:17 -07003780 Curl_safefree(host);
Alex Deymod15eaac2016-06-28 14:49:26 -07003781 }
3782
3783 if(port >= 0) {
3784 conn->conn_to_port = port;
3785 conn->bits.conn_to_port = TRUE;
3786 infof(data, "Connecting to port: %d\n", port);
3787 }
3788 else {
3789 /* no "connect to port" */
3790 conn->bits.conn_to_port = FALSE;
Elliott Hughes82be86d2017-09-20 17:00:17 -07003791 port = -1;
Alex Deymod15eaac2016-06-28 14:49:26 -07003792 }
3793
3794 conn_to_host = conn_to_host->next;
3795 }
3796
3797 return result;
3798}
3799
Kristian Monsen5ab50182010-05-14 18:53:44 +01003800/*************************************************************
3801 * Resolve the address of the server or proxy
3802 *************************************************************/
Alex Deymoe3149cc2016-10-05 11:18:42 -07003803static CURLcode resolve_server(struct Curl_easy *data,
Kristian Monsen5ab50182010-05-14 18:53:44 +01003804 struct connectdata *conn,
3805 bool *async)
3806{
Alex Deymo486467e2017-12-19 19:04:07 +01003807 CURLcode result = CURLE_OK;
3808 timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003809
3810 /*************************************************************
3811 * Resolve the name of the server or proxy
3812 *************************************************************/
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003813 if(conn->bits.reuse)
3814 /* We're reusing the connection - no need to resolve anything, and
3815 fix_hostname() was called already in create_conn() for the re-use
3816 case. */
Kristian Monsen5ab50182010-05-14 18:53:44 +01003817 *async = FALSE;
3818
Kristian Monsen5ab50182010-05-14 18:53:44 +01003819 else {
3820 /* this is a fresh connect */
3821 int rc;
3822 struct Curl_dns_entry *hostaddr;
3823
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003824#ifdef USE_UNIX_SOCKETS
Elliott Hughescee03382017-06-23 12:17:18 -07003825 if(conn->unix_domain_socket) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003826 /* Unix domain sockets are local. The host gets ignored, just use the
3827 * specified domain socket address. Do not cache "DNS entries". There is
3828 * no DNS involved and we already have the filesystem path available */
Elliott Hughescee03382017-06-23 12:17:18 -07003829 const char *path = conn->unix_domain_socket;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003830
3831 hostaddr = calloc(1, sizeof(struct Curl_dns_entry));
3832 if(!hostaddr)
3833 result = CURLE_OUT_OF_MEMORY;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003834 else {
Elliott Hughes82be86d2017-09-20 17:00:17 -07003835 bool longpath = FALSE;
3836 hostaddr->addr = Curl_unix2addr(path, &longpath,
3837 conn->abstract_unix_socket);
3838 if(hostaddr->addr)
3839 hostaddr->inuse++;
3840 else {
3841 /* Long paths are not supported for now */
3842 if(longpath) {
3843 failf(data, "Unix socket path too long: '%s'", path);
3844 result = CURLE_COULDNT_RESOLVE_HOST;
3845 }
3846 else
3847 result = CURLE_OUT_OF_MEMORY;
3848 free(hostaddr);
3849 hostaddr = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003850 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003851 }
3852 }
3853 else
3854#endif
Elliott Hughescee03382017-06-23 12:17:18 -07003855 if(!conn->bits.proxy) {
Alex Deymod15eaac2016-06-28 14:49:26 -07003856 struct hostname *connhost;
3857 if(conn->bits.conn_to_host)
3858 connhost = &conn->conn_to_host;
3859 else
3860 connhost = &conn->host;
3861
Kristian Monsen5ab50182010-05-14 18:53:44 +01003862 /* If not connecting via a proxy, extract the port from the URL, if it is
3863 * there, thus overriding any defaults that might have been set above. */
Alex Deymod15eaac2016-06-28 14:49:26 -07003864 if(conn->bits.conn_to_port)
3865 conn->port = conn->conn_to_port;
3866 else
Elliott Hughes82be86d2017-09-20 17:00:17 -07003867 conn->port = conn->remote_port;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003868
3869 /* Resolve target host right on */
Alex Deymod15eaac2016-06-28 14:49:26 -07003870 rc = Curl_resolv_timeout(conn, connhost->name, (int)conn->port,
Lucas Eckels9bd90e62012-08-06 15:07:02 -07003871 &hostaddr, timeout_ms);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003872 if(rc == CURLRESOLV_PENDING)
3873 *async = TRUE;
3874
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003875 else if(rc == CURLRESOLV_TIMEDOUT)
Kristian Monsen5ab50182010-05-14 18:53:44 +01003876 result = CURLE_OPERATION_TIMEDOUT;
3877
3878 else if(!hostaddr) {
Alex Deymod15eaac2016-06-28 14:49:26 -07003879 failf(data, "Couldn't resolve host '%s'", connhost->dispname);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003880 result = CURLE_COULDNT_RESOLVE_HOST;
3881 /* don't return yet, we need to clean up the timeout first */
3882 }
3883 }
3884 else {
3885 /* This is a proxy that hasn't been resolved yet. */
3886
Elliott Hughescee03382017-06-23 12:17:18 -07003887 struct hostname * const host = conn->bits.socksproxy ?
3888 &conn->socks_proxy.host : &conn->http_proxy.host;
3889
Kristian Monsen5ab50182010-05-14 18:53:44 +01003890 /* resolve proxy */
Elliott Hughescee03382017-06-23 12:17:18 -07003891 rc = Curl_resolv_timeout(conn, host->name, (int)conn->port,
Lucas Eckels9bd90e62012-08-06 15:07:02 -07003892 &hostaddr, timeout_ms);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003893
3894 if(rc == CURLRESOLV_PENDING)
3895 *async = TRUE;
3896
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003897 else if(rc == CURLRESOLV_TIMEDOUT)
Kristian Monsen5ab50182010-05-14 18:53:44 +01003898 result = CURLE_OPERATION_TIMEDOUT;
3899
3900 else if(!hostaddr) {
Elliott Hughescee03382017-06-23 12:17:18 -07003901 failf(data, "Couldn't resolve proxy '%s'", host->dispname);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003902 result = CURLE_COULDNT_RESOLVE_PROXY;
3903 /* don't return yet, we need to clean up the timeout first */
3904 }
3905 }
3906 DEBUGASSERT(conn->dns_entry == NULL);
3907 conn->dns_entry = hostaddr;
3908 }
3909
3910 return result;
3911}
3912
3913/*
3914 * Cleanup the connection just allocated before we can move along and use the
3915 * previously existing one. All relevant data is copied over and old_conn is
3916 * ready for freeing once this function returns.
3917 */
3918static void reuse_conn(struct connectdata *old_conn,
3919 struct connectdata *conn)
3920{
Elliott Hughescee03382017-06-23 12:17:18 -07003921 free_fixed_hostname(&old_conn->http_proxy.host);
3922 free_fixed_hostname(&old_conn->socks_proxy.host);
Elliott Hughescee03382017-06-23 12:17:18 -07003923
3924 free(old_conn->http_proxy.host.rawalloc);
3925 free(old_conn->socks_proxy.host.rawalloc);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003926
3927 /* free the SSL config struct from this connection struct as this was
3928 allocated in vain and is targeted for destruction */
Elliott Hughescee03382017-06-23 12:17:18 -07003929 Curl_free_primary_ssl_config(&old_conn->ssl_config);
3930 Curl_free_primary_ssl_config(&old_conn->proxy_ssl_config);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003931
3932 conn->data = old_conn->data;
3933
3934 /* get the user+password information from the old_conn struct since it may
3935 * be new for this request even when we re-use an existing connection */
3936 conn->bits.user_passwd = old_conn->bits.user_passwd;
3937 if(conn->bits.user_passwd) {
3938 /* use the new user name and password though */
3939 Curl_safefree(conn->user);
3940 Curl_safefree(conn->passwd);
3941 conn->user = old_conn->user;
3942 conn->passwd = old_conn->passwd;
3943 old_conn->user = NULL;
3944 old_conn->passwd = NULL;
3945 }
3946
3947 conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
3948 if(conn->bits.proxy_user_passwd) {
3949 /* use the new proxy user name and proxy password though */
Elliott Hughescee03382017-06-23 12:17:18 -07003950 Curl_safefree(conn->http_proxy.user);
3951 Curl_safefree(conn->socks_proxy.user);
3952 Curl_safefree(conn->http_proxy.passwd);
3953 Curl_safefree(conn->socks_proxy.passwd);
3954 conn->http_proxy.user = old_conn->http_proxy.user;
3955 conn->socks_proxy.user = old_conn->socks_proxy.user;
3956 conn->http_proxy.passwd = old_conn->http_proxy.passwd;
3957 conn->socks_proxy.passwd = old_conn->socks_proxy.passwd;
3958 old_conn->http_proxy.user = NULL;
3959 old_conn->socks_proxy.user = NULL;
3960 old_conn->http_proxy.passwd = NULL;
3961 old_conn->socks_proxy.passwd = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003962 }
3963
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003964 /* host can change, when doing keepalive with a proxy or if the case is
3965 different this time etc */
Alex Deymod15eaac2016-06-28 14:49:26 -07003966 free_fixed_hostname(&conn->host);
3967 free_fixed_hostname(&conn->conn_to_host);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003968 Curl_safefree(conn->host.rawalloc);
Alex Deymod15eaac2016-06-28 14:49:26 -07003969 Curl_safefree(conn->conn_to_host.rawalloc);
Alex Deymo486467e2017-12-19 19:04:07 +01003970 conn->host = old_conn->host;
Alex Deymod15eaac2016-06-28 14:49:26 -07003971 conn->conn_to_host = old_conn->conn_to_host;
Alex Deymod15eaac2016-06-28 14:49:26 -07003972 conn->conn_to_port = old_conn->conn_to_port;
Alex Deymo486467e2017-12-19 19:04:07 +01003973 conn->remote_port = old_conn->remote_port;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003974
3975 /* persist connection info in session handle */
3976 Curl_persistconninfo(conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003977
Alex Deymod15eaac2016-06-28 14:49:26 -07003978 conn_reset_all_postponed_data(old_conn); /* free buffers */
Alex Deymod15eaac2016-06-28 14:49:26 -07003979
Kristian Monsen5ab50182010-05-14 18:53:44 +01003980 /* re-use init */
3981 conn->bits.reuse = TRUE; /* yes, we're re-using here */
3982
3983 Curl_safefree(old_conn->user);
3984 Curl_safefree(old_conn->passwd);
Elliott Hughescee03382017-06-23 12:17:18 -07003985 Curl_safefree(old_conn->http_proxy.user);
3986 Curl_safefree(old_conn->socks_proxy.user);
3987 Curl_safefree(old_conn->http_proxy.passwd);
3988 Curl_safefree(old_conn->socks_proxy.passwd);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003989 Curl_safefree(old_conn->localdev);
3990
Elliott Hughes82be86d2017-09-20 17:00:17 -07003991 Curl_llist_destroy(&old_conn->send_pipe, NULL);
3992 Curl_llist_destroy(&old_conn->recv_pipe, NULL);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003993
Kristian Monsen5ab50182010-05-14 18:53:44 +01003994 Curl_safefree(old_conn->master_buffer);
Elliott Hughescee03382017-06-23 12:17:18 -07003995
3996#ifdef USE_UNIX_SOCKETS
3997 Curl_safefree(old_conn->unix_domain_socket);
3998#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +01003999}
4000
4001/**
4002 * create_conn() sets up a new connectdata struct, or re-uses an already
4003 * existing one, and resolves host name.
4004 *
4005 * if this function returns CURLE_OK and *async is set to TRUE, the resolve
4006 * response will be coming asynchronously. If *async is FALSE, the name is
4007 * already resolved.
4008 *
4009 * @param data The sessionhandle pointer
4010 * @param in_connect is set to the next connection data pointer
4011 * @param async is set TRUE when an async DNS resolution is pending
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004012 * @see Curl_setup_conn()
Kristian Monsen5ab50182010-05-14 18:53:44 +01004013 *
4014 * *NOTE* this function assigns the conn->data pointer!
4015 */
4016
Alex Deymoe3149cc2016-10-05 11:18:42 -07004017static CURLcode create_conn(struct Curl_easy *data,
Kristian Monsen5ab50182010-05-14 18:53:44 +01004018 struct connectdata **in_connect,
4019 bool *async)
4020{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004021 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004022 struct connectdata *conn;
4023 struct connectdata *conn_temp = NULL;
4024 size_t urllen;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004025 char *user = NULL;
4026 char *passwd = NULL;
4027 char *options = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004028 bool reuse;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004029 bool prot_missing = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004030 bool connections_available = TRUE;
4031 bool force_reuse = FALSE;
4032 bool waitpipe = FALSE;
4033 size_t max_host_connections = Curl_multi_max_host_connections(data->multi);
4034 size_t max_total_connections = Curl_multi_max_total_connections(data->multi);
Kristian Monsen5ab50182010-05-14 18:53:44 +01004035
4036 *async = FALSE;
4037
4038 /*************************************************************
4039 * Check input data
4040 *************************************************************/
4041
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004042 if(!data->change.url) {
4043 result = CURLE_URL_MALFORMAT;
4044 goto out;
4045 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01004046
4047 /* First, split up the current URL in parts so that we can use the
4048 parts for checking against the already present connections. In order
4049 to not have to modify everything at once, we allocate a temporary
4050 connection data struct and fill in for comparison purposes. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004051 conn = allocate_conn(data);
Kristian Monsen5ab50182010-05-14 18:53:44 +01004052
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004053 if(!conn) {
4054 result = CURLE_OUT_OF_MEMORY;
4055 goto out;
4056 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01004057
4058 /* We must set the return variable as soon as possible, so that our
4059 parent can cleanup any possible allocs we may have done before
4060 any failure */
4061 *in_connect = conn;
4062
Kristian Monsen5ab50182010-05-14 18:53:44 +01004063 /* This initing continues below, see the comment "Continue connectdata
4064 * initialization here" */
4065
4066 /***********************************************************
4067 * We need to allocate memory to store the path in. We get the size of the
4068 * full URL to be sure, and we need to make it at least 256 bytes since
4069 * other parts of the code will rely on this fact
4070 ***********************************************************/
4071#define LEAST_PATH_ALLOC 256
Alex Deymo486467e2017-12-19 19:04:07 +01004072 urllen = strlen(data->change.url);
Kristian Monsen5ab50182010-05-14 18:53:44 +01004073 if(urllen < LEAST_PATH_ALLOC)
Alex Deymo486467e2017-12-19 19:04:07 +01004074 urllen = LEAST_PATH_ALLOC;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004075
4076 /*
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004077 * We malloc() the buffers below urllen+2 to make room for 2 possibilities:
Kristian Monsen5ab50182010-05-14 18:53:44 +01004078 * 1 - an extra terminating zero
4079 * 2 - an extra slash (in case a syntax like "www.host.com?moo" is used)
4080 */
4081
4082 Curl_safefree(data->state.pathbuffer);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004083 data->state.path = NULL;
4084
Alex Deymo486467e2017-12-19 19:04:07 +01004085 data->state.pathbuffer = malloc(urllen + 2);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004086 if(NULL == data->state.pathbuffer) {
4087 result = CURLE_OUT_OF_MEMORY; /* really bad error */
4088 goto out;
4089 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01004090 data->state.path = data->state.pathbuffer;
4091
Alex Deymo486467e2017-12-19 19:04:07 +01004092 conn->host.rawalloc = malloc(urllen + 2);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004093 if(NULL == conn->host.rawalloc) {
4094 Curl_safefree(data->state.pathbuffer);
4095 data->state.path = NULL;
4096 result = CURLE_OUT_OF_MEMORY;
4097 goto out;
4098 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01004099
4100 conn->host.name = conn->host.rawalloc;
4101 conn->host.name[0] = 0;
4102
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004103 user = strdup("");
4104 passwd = strdup("");
4105 options = strdup("");
4106 if(!user || !passwd || !options) {
4107 result = CURLE_OUT_OF_MEMORY;
4108 goto out;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004109 }
4110
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004111 result = parseurlandfillconn(data, conn, &prot_missing, &user, &passwd,
4112 &options);
4113 if(result)
4114 goto out;
4115
Kristian Monsen5ab50182010-05-14 18:53:44 +01004116 /*************************************************************
4117 * No protocol part in URL was used, add it!
4118 *************************************************************/
4119 if(prot_missing) {
4120 /* We're guessing prefixes here and if we're told to use a proxy or if
Elliott Hughescac39802018-04-27 16:19:43 -07004121 we're going to follow a Location: later or... then we need the protocol
Kristian Monsen5ab50182010-05-14 18:53:44 +01004122 part added so that we have a valid URL. */
4123 char *reurl;
Alex Deymod15eaac2016-06-28 14:49:26 -07004124 char *ch_lower;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004125
4126 reurl = aprintf("%s://%s", conn->handler->scheme, data->change.url);
4127
4128 if(!reurl) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004129 result = CURLE_OUT_OF_MEMORY;
4130 goto out;
4131 }
4132
Alex Deymod15eaac2016-06-28 14:49:26 -07004133 /* Change protocol prefix to lower-case */
4134 for(ch_lower = reurl; *ch_lower != ':'; ch_lower++)
4135 *ch_lower = (char)TOLOWER(*ch_lower);
4136
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004137 if(data->change.url_alloc) {
4138 Curl_safefree(data->change.url);
4139 data->change.url_alloc = FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004140 }
4141
4142 data->change.url = reurl;
4143 data->change.url_alloc = TRUE; /* free this later */
4144 }
4145
4146 /*************************************************************
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004147 * If the protocol can't handle url query strings, then cut
4148 * off the unhandable part
Kristian Monsen5ab50182010-05-14 18:53:44 +01004149 *************************************************************/
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004150 if((conn->given->flags&PROTOPT_NOURLQUERY)) {
4151 char *path_q_sep = strchr(conn->data->state.path, '?');
4152 if(path_q_sep) {
4153 /* according to rfc3986, allow the query (?foo=bar)
4154 also on protocols that can't handle it.
4155
4156 cut the string-part after '?'
4157 */
4158
4159 /* terminate the string */
4160 path_q_sep[0] = 0;
4161 }
4162 }
4163
4164 if(data->set.str[STRING_BEARER]) {
Alex Deymod15eaac2016-06-28 14:49:26 -07004165 conn->oauth_bearer = strdup(data->set.str[STRING_BEARER]);
4166 if(!conn->oauth_bearer) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004167 result = CURLE_OUT_OF_MEMORY;
4168 goto out;
4169 }
4170 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01004171
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004172#ifdef USE_UNIX_SOCKETS
Elliott Hughescee03382017-06-23 12:17:18 -07004173 if(data->set.str[STRING_UNIX_SOCKET_PATH]) {
Elliott Hughescee03382017-06-23 12:17:18 -07004174 conn->unix_domain_socket = strdup(data->set.str[STRING_UNIX_SOCKET_PATH]);
4175 if(conn->unix_domain_socket == NULL) {
4176 result = CURLE_OUT_OF_MEMORY;
4177 goto out;
4178 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07004179 conn->abstract_unix_socket = data->set.abstract_unix_socket;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004180 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004181#endif
4182
Elliott Hughes82be86d2017-09-20 17:00:17 -07004183 /* After the unix socket init but before the proxy vars are used, parse and
4184 initialize the proxy vars */
4185#ifndef CURL_DISABLE_PROXY
4186 result = create_conn_helper_init_proxy(conn);
4187 if(result)
4188 goto out;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004189#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +01004190
4191 /*************************************************************
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004192 * If the protocol is using SSL and HTTP proxy is used, we set
4193 * the tunnel_proxy bit.
4194 *************************************************************/
4195 if((conn->given->flags&PROTOPT_SSL) && conn->bits.httpproxy)
4196 conn->bits.tunnel_proxy = TRUE;
4197
4198 /*************************************************************
4199 * Figure out the remote port number and fix it in the URL
4200 *************************************************************/
4201 result = parse_remote_port(data, conn);
4202 if(result)
4203 goto out;
4204
4205 /* Check for overridden login details and set them accordingly so they
4206 they are known when protocol->setup_connection is called! */
4207 result = override_login(data, conn, &user, &passwd, &options);
4208 if(result)
4209 goto out;
4210 result = set_login(conn, user, passwd, options);
4211 if(result)
4212 goto out;
4213
4214 /*************************************************************
Alex Deymod15eaac2016-06-28 14:49:26 -07004215 * Process the "connect to" linked list of hostname/port mappings.
4216 * Do this after the remote port number has been fixed in the URL.
4217 *************************************************************/
4218 result = parse_connect_to_slist(data, conn, data->set.connect_to);
4219 if(result)
4220 goto out;
4221
4222 /*************************************************************
4223 * IDN-fix the hostnames
4224 *************************************************************/
Alex Deymo486467e2017-12-19 19:04:07 +01004225 result = fix_hostname(conn, &conn->host);
4226 if(result)
4227 goto out;
4228 if(conn->bits.conn_to_host) {
4229 result = fix_hostname(conn, &conn->conn_to_host);
4230 if(result)
4231 goto out;
4232 }
4233 if(conn->bits.httpproxy) {
4234 result = fix_hostname(conn, &conn->http_proxy.host);
4235 if(result)
4236 goto out;
4237 }
4238 if(conn->bits.socksproxy) {
4239 result = fix_hostname(conn, &conn->socks_proxy.host);
4240 if(result)
4241 goto out;
4242 }
Alex Deymod15eaac2016-06-28 14:49:26 -07004243
4244 /*************************************************************
4245 * Check whether the host and the "connect to host" are equal.
Elliott Hughes82be86d2017-09-20 17:00:17 -07004246 * Do this after the hostnames have been IDN-fixed.
Alex Deymod15eaac2016-06-28 14:49:26 -07004247 *************************************************************/
4248 if(conn->bits.conn_to_host &&
Elliott Hughescee03382017-06-23 12:17:18 -07004249 strcasecompare(conn->conn_to_host.name, conn->host.name)) {
Alex Deymod15eaac2016-06-28 14:49:26 -07004250 conn->bits.conn_to_host = FALSE;
4251 }
4252
4253 /*************************************************************
4254 * Check whether the port and the "connect to port" are equal.
4255 * Do this after the remote port number has been fixed in the URL.
4256 *************************************************************/
4257 if(conn->bits.conn_to_port && conn->conn_to_port == conn->remote_port) {
4258 conn->bits.conn_to_port = FALSE;
4259 }
4260
4261 /*************************************************************
4262 * If the "connect to" feature is used with an HTTP proxy,
4263 * we set the tunnel_proxy bit.
4264 *************************************************************/
4265 if((conn->bits.conn_to_host || conn->bits.conn_to_port) &&
4266 conn->bits.httpproxy)
4267 conn->bits.tunnel_proxy = TRUE;
4268
4269 /*************************************************************
Kristian Monsen5ab50182010-05-14 18:53:44 +01004270 * Setup internals depending on protocol. Needs to be done after
4271 * we figured out what/if proxy to use.
4272 *************************************************************/
4273 result = setup_connection_internals(conn);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004274 if(result)
4275 goto out;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004276
Lucas Eckels9bd90e62012-08-06 15:07:02 -07004277 conn->recv[FIRSTSOCKET] = Curl_recv_plain;
4278 conn->send[FIRSTSOCKET] = Curl_send_plain;
4279 conn->recv[SECONDARYSOCKET] = Curl_recv_plain;
4280 conn->send[SECONDARYSOCKET] = Curl_send_plain;
4281
Alex Deymod15eaac2016-06-28 14:49:26 -07004282 conn->bits.tcp_fastopen = data->set.tcp_fastopen;
4283
Kristian Monsen5ab50182010-05-14 18:53:44 +01004284 /***********************************************************************
4285 * file: is a special case in that it doesn't need a network connection
4286 ***********************************************************************/
4287#ifndef CURL_DISABLE_FILE
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004288 if(conn->handler->flags & PROTOPT_NONETWORK) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01004289 bool done;
4290 /* this is supposed to be the connect function so we better at least check
4291 that the file is present here! */
4292 DEBUGASSERT(conn->handler->connect_it);
4293 result = conn->handler->connect_it(conn, &done);
4294
4295 /* Setup a "faked" transfer that'll do nothing */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004296 if(!result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01004297 conn->data = data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004298 conn->bits.tcpconnect[FIRSTSOCKET] = TRUE; /* we are "connected */
Kristian Monsen5ab50182010-05-14 18:53:44 +01004299
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004300 Curl_conncache_add_conn(data->state.conn_cache, conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +01004301
4302 /*
4303 * Setup whatever necessary for a resumed transfer
4304 */
4305 result = setup_range(data);
4306 if(result) {
4307 DEBUGASSERT(conn->handler->done);
4308 /* we ignore the return code for the protocol-specific DONE */
4309 (void)conn->handler->done(conn, result, FALSE);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004310 goto out;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004311 }
4312
Lucas Eckels9bd90e62012-08-06 15:07:02 -07004313 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, /* no download */
4314 -1, NULL); /* no upload */
Kristian Monsen5ab50182010-05-14 18:53:44 +01004315 }
4316
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004317 /* since we skip do_init() */
Alex Deymod15eaac2016-06-28 14:49:26 -07004318 Curl_init_do(data, conn);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004319
4320 goto out;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004321 }
4322#endif
4323
Kristian Monsen5ab50182010-05-14 18:53:44 +01004324 /* Get a cloned copy of the SSL config situation stored in the
4325 connection struct. But to get this going nicely, we must first make
4326 sure that the strings in the master copy are pointing to the correct
4327 strings in the session handle strings array!
4328
4329 Keep in mind that the pointers in the master copy are pointing to strings
Alex Deymoe3149cc2016-10-05 11:18:42 -07004330 that will be freed as part of the Curl_easy struct, but all cloned
Kristian Monsen5ab50182010-05-14 18:53:44 +01004331 copies will be separately allocated.
4332 */
Elliott Hughescee03382017-06-23 12:17:18 -07004333 data->set.ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_ORIG];
4334 data->set.proxy_ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_PROXY];
4335 data->set.ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_ORIG];
4336 data->set.proxy_ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_PROXY];
4337 data->set.ssl.primary.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
4338 data->set.proxy_ssl.primary.random_file =
4339 data->set.str[STRING_SSL_RANDOM_FILE];
4340 data->set.ssl.primary.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
4341 data->set.proxy_ssl.primary.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
4342 data->set.ssl.primary.cipher_list =
4343 data->set.str[STRING_SSL_CIPHER_LIST_ORIG];
4344 data->set.proxy_ssl.primary.cipher_list =
4345 data->set.str[STRING_SSL_CIPHER_LIST_PROXY];
4346
4347 data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE_ORIG];
4348 data->set.proxy_ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE_PROXY];
4349 data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT_ORIG];
4350 data->set.proxy_ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT_PROXY];
4351 data->set.ssl.cert = data->set.str[STRING_CERT_ORIG];
4352 data->set.proxy_ssl.cert = data->set.str[STRING_CERT_PROXY];
4353 data->set.ssl.cert_type = data->set.str[STRING_CERT_TYPE_ORIG];
4354 data->set.proxy_ssl.cert_type = data->set.str[STRING_CERT_TYPE_PROXY];
4355 data->set.ssl.key = data->set.str[STRING_KEY_ORIG];
4356 data->set.proxy_ssl.key = data->set.str[STRING_KEY_PROXY];
4357 data->set.ssl.key_type = data->set.str[STRING_KEY_TYPE_ORIG];
4358 data->set.proxy_ssl.key_type = data->set.str[STRING_KEY_TYPE_PROXY];
4359 data->set.ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_ORIG];
4360 data->set.proxy_ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_PROXY];
4361 data->set.ssl.primary.clientcert = data->set.str[STRING_CERT_ORIG];
4362 data->set.proxy_ssl.primary.clientcert = data->set.str[STRING_CERT_PROXY];
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004363#ifdef USE_TLS_SRP
Elliott Hughescee03382017-06-23 12:17:18 -07004364 data->set.ssl.username = data->set.str[STRING_TLSAUTH_USERNAME_ORIG];
4365 data->set.proxy_ssl.username = data->set.str[STRING_TLSAUTH_USERNAME_PROXY];
4366 data->set.ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD_ORIG];
4367 data->set.proxy_ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD_PROXY];
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004368#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +01004369
Elliott Hughescee03382017-06-23 12:17:18 -07004370 if(!Curl_clone_primary_ssl_config(&data->set.ssl.primary,
4371 &conn->ssl_config)) {
4372 result = CURLE_OUT_OF_MEMORY;
4373 goto out;
4374 }
4375
4376 if(!Curl_clone_primary_ssl_config(&data->set.proxy_ssl.primary,
4377 &conn->proxy_ssl_config)) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004378 result = CURLE_OUT_OF_MEMORY;
4379 goto out;
4380 }
4381
4382 prune_dead_connections(data);
Kristian Monsen5ab50182010-05-14 18:53:44 +01004383
4384 /*************************************************************
4385 * Check the current list of connections to see if we can
4386 * re-use an already existing one or if we have to create a
4387 * new one.
4388 *************************************************************/
4389
4390 /* reuse_fresh is TRUE if we are told to use a new connection by force, but
4391 we only acknowledge this option if this is not a re-used connection
4392 already (which happens due to follow-location or during a HTTP
4393 authentication phase). */
4394 if(data->set.reuse_fresh && !data->state.this_is_a_follow)
4395 reuse = FALSE;
4396 else
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004397 reuse = ConnectionExists(data, conn, &conn_temp, &force_reuse, &waitpipe);
4398
Elliott Hughes0128fe42018-02-27 14:57:55 -08004399 /* If we found a reusable connection that is now marked as in use, we may
4400 still want to open a new connection if we are pipelining. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004401 if(reuse && !force_reuse && IsPipeliningPossible(data, conn_temp)) {
Elliott Hughes82be86d2017-09-20 17:00:17 -07004402 size_t pipelen = conn_temp->send_pipe.size + conn_temp->recv_pipe.size;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004403 if(pipelen > 0) {
4404 infof(data, "Found connection %ld, with requests in the pipe (%zu)\n",
4405 conn_temp->connection_id, pipelen);
4406
Elliott Hughes0128fe42018-02-27 14:57:55 -08004407 if(Curl_conncache_bundle_size(conn_temp) < max_host_connections &&
4408 Curl_conncache_size(data) < max_total_connections) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004409 /* We want a new connection anyway */
4410 reuse = FALSE;
4411
4412 infof(data, "We can reuse, but we want a new connection anyway\n");
Elliott Hughes0128fe42018-02-27 14:57:55 -08004413 Curl_conncache_return_conn(conn_temp);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004414 }
4415 }
4416 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01004417
4418 if(reuse) {
4419 /*
4420 * We already have a connection for this, we got the former connection
4421 * in the conn_temp variable and thus we need to cleanup the one we
4422 * just allocated before we can move along and use the previously
4423 * existing one.
4424 */
4425 reuse_conn(conn, conn_temp);
Elliott Hughes0128fe42018-02-27 14:57:55 -08004426#ifdef USE_SSL
4427 free(conn->ssl_extra);
4428#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +01004429 free(conn); /* we don't need this anymore */
4430 conn = conn_temp;
4431 *in_connect = conn;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004432
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004433 infof(data, "Re-using existing connection! (#%ld) with %s %s\n",
4434 conn->connection_id,
4435 conn->bits.proxy?"proxy":"host",
Elliott Hughescee03382017-06-23 12:17:18 -07004436 conn->socks_proxy.host.name ? conn->socks_proxy.host.dispname :
4437 conn->http_proxy.host.name ? conn->http_proxy.host.dispname :
4438 conn->host.dispname);
Kristian Monsen5ab50182010-05-14 18:53:44 +01004439 }
4440 else {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004441 /* We have decided that we want a new connection. However, we may not
4442 be able to do that if we have reached the limit of how many
4443 connections we are allowed to open. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004444
Alex Deymod15eaac2016-06-28 14:49:26 -07004445 if(conn->handler->flags & PROTOPT_ALPN_NPN) {
4446 /* The protocol wants it, so set the bits if enabled in the easy handle
4447 (default) */
4448 if(data->set.ssl_enable_alpn)
4449 conn->bits.tls_enable_alpn = TRUE;
4450 if(data->set.ssl_enable_npn)
4451 conn->bits.tls_enable_npn = TRUE;
4452 }
4453
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004454 if(waitpipe)
4455 /* There is a connection that *might* become usable for pipelining
4456 "soon", and we wait for that */
4457 connections_available = FALSE;
Elliott Hughes0128fe42018-02-27 14:57:55 -08004458 else {
4459 /* this gets a lock on the conncache */
4460 struct connectbundle *bundle =
4461 Curl_conncache_find_bundle(conn, data->state.conn_cache);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004462
Elliott Hughes0128fe42018-02-27 14:57:55 -08004463 if(max_host_connections > 0 && bundle &&
4464 (bundle->num_connections >= max_host_connections)) {
4465 struct connectdata *conn_candidate;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004466
Elliott Hughes0128fe42018-02-27 14:57:55 -08004467 /* The bundle is full. Extract the oldest connection. */
4468 conn_candidate = Curl_conncache_extract_bundle(data, bundle);
4469 Curl_conncache_unlock(conn);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004470
Elliott Hughes0128fe42018-02-27 14:57:55 -08004471 if(conn_candidate) {
4472 /* Set the connection's owner correctly, then kill it */
4473 conn_candidate->data = data;
4474 (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
4475 }
4476 else {
4477 infof(data, "No more connections allowed to host: %d\n",
4478 max_host_connections);
4479 connections_available = FALSE;
4480 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004481 }
Elliott Hughes0128fe42018-02-27 14:57:55 -08004482 else
4483 Curl_conncache_unlock(conn);
4484
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004485 }
4486
4487 if(connections_available &&
4488 (max_total_connections > 0) &&
Elliott Hughes0128fe42018-02-27 14:57:55 -08004489 (Curl_conncache_size(data) >= max_total_connections)) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004490 struct connectdata *conn_candidate;
4491
4492 /* The cache is full. Let's see if we can kill a connection. */
Elliott Hughes0128fe42018-02-27 14:57:55 -08004493 conn_candidate = Curl_conncache_extract_oldest(data);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004494
4495 if(conn_candidate) {
4496 /* Set the connection's owner correctly, then kill it */
4497 conn_candidate->data = data;
4498 (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
4499 }
4500 else {
4501 infof(data, "No connections available in cache\n");
4502 connections_available = FALSE;
4503 }
4504 }
4505
4506 if(!connections_available) {
4507 infof(data, "No connections available.\n");
4508
4509 conn_free(conn);
4510 *in_connect = NULL;
4511
4512 result = CURLE_NO_CONNECTION_AVAILABLE;
4513 goto out;
4514 }
4515 else {
Elliott Hughes0128fe42018-02-27 14:57:55 -08004516 /* Mark the connection as used, before we add it */
4517 conn->inuse = TRUE;
4518
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004519 /*
4520 * This is a brand new connection, so let's store it in the connection
4521 * cache of ours!
4522 */
4523 Curl_conncache_add_conn(data->state.conn_cache, conn);
4524 }
4525
4526#if defined(USE_NTLM)
4527 /* If NTLM is requested in a part of this connection, make sure we don't
4528 assume the state is fine as this is a fresh connection and NTLM is
4529 connection based. */
4530 if((data->state.authhost.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
4531 data->state.authhost.done) {
4532 infof(data, "NTLM picked AND auth done set, clear picked!\n");
4533 data->state.authhost.picked = CURLAUTH_NONE;
Alex Deymod15eaac2016-06-28 14:49:26 -07004534 data->state.authhost.done = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004535 }
4536
4537 if((data->state.authproxy.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
4538 data->state.authproxy.done) {
4539 infof(data, "NTLM-proxy picked AND auth done set, clear picked!\n");
4540 data->state.authproxy.picked = CURLAUTH_NONE;
Alex Deymod15eaac2016-06-28 14:49:26 -07004541 data->state.authproxy.done = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004542 }
4543#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +01004544 }
4545
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004546 /* Setup and init stuff before DO starts, in preparing for the transfer. */
Alex Deymod15eaac2016-06-28 14:49:26 -07004547 Curl_init_do(data, conn);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004548
Kristian Monsen5ab50182010-05-14 18:53:44 +01004549 /*
4550 * Setup whatever necessary for a resumed transfer
4551 */
4552 result = setup_range(data);
4553 if(result)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004554 goto out;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004555
4556 /* Continue connectdata initialization here. */
4557
4558 /*
4559 * Inherit the proper values from the urldata struct AFTER we have arranged
4560 * the persistent connection stuff
4561 */
Kristian Monsen5ab50182010-05-14 18:53:44 +01004562 conn->seek_func = data->set.seek_func;
4563 conn->seek_client = data->set.seek_client;
4564
4565 /*************************************************************
4566 * Resolve the address of the server or proxy
4567 *************************************************************/
4568 result = resolve_server(data, conn, async);
4569
Elliott Hughes82be86d2017-09-20 17:00:17 -07004570out:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004571
4572 free(options);
4573 free(passwd);
4574 free(user);
Kristian Monsen5ab50182010-05-14 18:53:44 +01004575 return result;
4576}
4577
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004578/* Curl_setup_conn() is called after the name resolve initiated in
Kristian Monsen5ab50182010-05-14 18:53:44 +01004579 * create_conn() is all done.
4580 *
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004581 * Curl_setup_conn() also handles reused connections
Kristian Monsen5ab50182010-05-14 18:53:44 +01004582 *
4583 * conn->data MUST already have been setup fine (in create_conn)
4584 */
4585
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004586CURLcode Curl_setup_conn(struct connectdata *conn,
4587 bool *protocol_done)
Kristian Monsen5ab50182010-05-14 18:53:44 +01004588{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004589 CURLcode result = CURLE_OK;
Alex Deymoe3149cc2016-10-05 11:18:42 -07004590 struct Curl_easy *data = conn->data;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004591
4592 Curl_pgrsTime(data, TIMER_NAMELOOKUP);
4593
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004594 if(conn->handler->flags & PROTOPT_NONETWORK) {
4595 /* nothing to setup when not using a network */
Kristian Monsen5ab50182010-05-14 18:53:44 +01004596 *protocol_done = TRUE;
4597 return result;
4598 }
4599 *protocol_done = FALSE; /* default to not done */
4600
4601 /* set proxy_connect_closed to false unconditionally already here since it
4602 is used strictly to provide extra information to a parent function in the
4603 case of proxy CONNECT failures and we must make sure we don't have it
4604 lingering set from a previous invoke */
4605 conn->bits.proxy_connect_closed = FALSE;
4606
4607 /*
4608 * Set user-agent. Used for HTTP, but since we can attempt to tunnel
4609 * basically anything through a http proxy we can't limit this based on
4610 * protocol.
4611 */
4612 if(data->set.str[STRING_USERAGENT]) {
4613 Curl_safefree(conn->allocptr.uagent);
4614 conn->allocptr.uagent =
4615 aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
4616 if(!conn->allocptr.uagent)
4617 return CURLE_OUT_OF_MEMORY;
4618 }
4619
4620 data->req.headerbytecount = 0;
4621
4622#ifdef CURL_DO_LINEEND_CONV
4623 data->state.crlf_conversions = 0; /* reset CRLF conversion counter */
4624#endif /* CURL_DO_LINEEND_CONV */
4625
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004626 /* set start time here for timeout purposes in the connect procedure, it
4627 is later set again for the progress meter purpose */
Alex Deymo486467e2017-12-19 19:04:07 +01004628 conn->now = Curl_now();
Kristian Monsen5ab50182010-05-14 18:53:44 +01004629
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004630 if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
4631 conn->bits.tcpconnect[FIRSTSOCKET] = FALSE;
4632 result = Curl_connecthost(conn, conn->dns_entry);
4633 if(result)
4634 return result;
4635 }
4636 else {
4637 Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected already */
4638 Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
4639 conn->bits.tcpconnect[FIRSTSOCKET] = TRUE;
4640 *protocol_done = TRUE;
4641 Curl_updateconninfo(conn, conn->sock[FIRSTSOCKET]);
4642 Curl_verboseconnect(conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +01004643 }
4644
Elliott Hughes0128fe42018-02-27 14:57:55 -08004645 conn->now = Curl_now(); /* time this *after* the connect is done, we set
4646 this here perhaps a second time */
Kristian Monsen5ab50182010-05-14 18:53:44 +01004647 return result;
4648}
4649
Alex Deymoe3149cc2016-10-05 11:18:42 -07004650CURLcode Curl_connect(struct Curl_easy *data,
Kristian Monsen5ab50182010-05-14 18:53:44 +01004651 struct connectdata **in_connect,
4652 bool *asyncp,
4653 bool *protocol_done)
4654{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004655 CURLcode result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004656
4657 *asyncp = FALSE; /* assume synchronous resolves by default */
4658
4659 /* call the stuff that needs to be called */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004660 result = create_conn(data, in_connect, asyncp);
Kristian Monsen5ab50182010-05-14 18:53:44 +01004661
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004662 if(!result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01004663 /* no error */
Elliott Hughes82be86d2017-09-20 17:00:17 -07004664 if((*in_connect)->send_pipe.size || (*in_connect)->recv_pipe.size)
Kristian Monsen5ab50182010-05-14 18:53:44 +01004665 /* pipelining */
4666 *protocol_done = TRUE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004667 else if(!*asyncp) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01004668 /* DNS resolution is done: that's either because this is a reused
4669 connection, in which case DNS was unnecessary, or because DNS
4670 really did finish already (synch resolver/fast async resolve) */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004671 result = Curl_setup_conn(*in_connect, protocol_done);
Kristian Monsen5ab50182010-05-14 18:53:44 +01004672 }
4673 }
4674
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004675 if(result == CURLE_NO_CONNECTION_AVAILABLE) {
4676 *in_connect = NULL;
4677 return result;
4678 }
4679
4680 if(result && *in_connect) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01004681 /* We're not allowed to return failure with memory left allocated
4682 in the connectdata struct, free those here */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004683 Curl_disconnect(*in_connect, FALSE); /* close the connection */
Kristian Monsen5ab50182010-05-14 18:53:44 +01004684 *in_connect = NULL; /* return a NULL */
4685 }
4686
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07004687 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004688}
4689
Kristian Monsen5ab50182010-05-14 18:53:44 +01004690/*
Alex Deymod15eaac2016-06-28 14:49:26 -07004691 * Curl_init_do() inits the readwrite session. This is inited each time (in
4692 * the DO function before the protocol-specific DO functions are invoked) for
Alex Deymoe3149cc2016-10-05 11:18:42 -07004693 * a transfer, sometimes multiple times on the same Curl_easy. Make sure
Kristian Monsen5ab50182010-05-14 18:53:44 +01004694 * nothing in here depends on stuff that are setup dynamically for the
4695 * transfer.
Alex Deymod15eaac2016-06-28 14:49:26 -07004696 *
4697 * Allow this function to get called with 'conn' set to NULL.
Kristian Monsen5ab50182010-05-14 18:53:44 +01004698 */
4699
Alex Deymoe3149cc2016-10-05 11:18:42 -07004700CURLcode Curl_init_do(struct Curl_easy *data, struct connectdata *conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +01004701{
Kristian Monsen5ab50182010-05-14 18:53:44 +01004702 struct SingleRequest *k = &data->req;
4703
Alex Deymo486467e2017-12-19 19:04:07 +01004704 conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to
4705 use */
Alex Deymod15eaac2016-06-28 14:49:26 -07004706
4707 data->state.done = FALSE; /* *_done() is not called yet */
Kristian Monsen5ab50182010-05-14 18:53:44 +01004708 data->state.expect100header = FALSE;
4709
Alex Deymo486467e2017-12-19 19:04:07 +01004710 /* if the protocol used doesn't support wildcards, switch it off */
4711 if(data->state.wildcardmatch &&
4712 !(conn->handler->flags & PROTOPT_WILDCARD))
4713 data->state.wildcardmatch = FALSE;
4714
Kristian Monsen5ab50182010-05-14 18:53:44 +01004715 if(data->set.opt_no_body)
4716 /* in HTTP lingo, no body means using the HEAD request... */
4717 data->set.httpreq = HTTPREQ_HEAD;
4718 else if(HTTPREQ_HEAD == data->set.httpreq)
4719 /* ... but if unset there really is no perfect method that is the
4720 "opposite" of HEAD but in reality most people probably think GET
4721 then. The important thing is that we can't let it remain HEAD if the
4722 opt_no_body is set FALSE since then we'll behave wrong when getting
4723 HTTP. */
4724 data->set.httpreq = HTTPREQ_GET;
4725
Alex Deymo486467e2017-12-19 19:04:07 +01004726 k->start = Curl_now(); /* start time */
Kristian Monsen5ab50182010-05-14 18:53:44 +01004727 k->now = k->start; /* current time is now */
4728 k->header = TRUE; /* assume header */
4729
4730 k->bytecount = 0;
4731
4732 k->buf = data->state.buffer;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004733 k->hbufp = data->state.headerbuff;
Alex Deymo486467e2017-12-19 19:04:07 +01004734 k->ignorebody = FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004735
Kristian Monsen5ab50182010-05-14 18:53:44 +01004736 Curl_speedinit(data);
4737
4738 Curl_pgrsSetUploadCounter(data, 0);
4739 Curl_pgrsSetDownloadCounter(data, 0);
4740
4741 return CURLE_OK;
4742}
4743
4744/*
Alex Deymod15eaac2016-06-28 14:49:26 -07004745* get_protocol_family()
4746*
4747* This is used to return the protocol family for a given protocol.
4748*
4749* Parameters:
4750*
4751* protocol [in] - A single bit protocol identifier such as HTTP or HTTPS.
4752*
4753* Returns the family as a single bit protocol identifier.
4754*/
4755
Elliott Hughes82be86d2017-09-20 17:00:17 -07004756static unsigned int get_protocol_family(unsigned int protocol)
Kristian Monsen5ab50182010-05-14 18:53:44 +01004757{
Alex Deymod15eaac2016-06-28 14:49:26 -07004758 unsigned int family;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004759
Alex Deymod15eaac2016-06-28 14:49:26 -07004760 switch(protocol) {
4761 case CURLPROTO_HTTP:
4762 case CURLPROTO_HTTPS:
4763 family = CURLPROTO_HTTP;
4764 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004765
Alex Deymod15eaac2016-06-28 14:49:26 -07004766 case CURLPROTO_FTP:
4767 case CURLPROTO_FTPS:
4768 family = CURLPROTO_FTP;
4769 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004770
Alex Deymod15eaac2016-06-28 14:49:26 -07004771 case CURLPROTO_SCP:
4772 family = CURLPROTO_SCP;
4773 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004774
Alex Deymod15eaac2016-06-28 14:49:26 -07004775 case CURLPROTO_SFTP:
4776 family = CURLPROTO_SFTP;
4777 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004778
Alex Deymod15eaac2016-06-28 14:49:26 -07004779 case CURLPROTO_TELNET:
4780 family = CURLPROTO_TELNET;
4781 break;
4782
4783 case CURLPROTO_LDAP:
4784 case CURLPROTO_LDAPS:
4785 family = CURLPROTO_LDAP;
4786 break;
4787
4788 case CURLPROTO_DICT:
4789 family = CURLPROTO_DICT;
4790 break;
4791
4792 case CURLPROTO_FILE:
4793 family = CURLPROTO_FILE;
4794 break;
4795
4796 case CURLPROTO_TFTP:
4797 family = CURLPROTO_TFTP;
4798 break;
4799
4800 case CURLPROTO_IMAP:
4801 case CURLPROTO_IMAPS:
4802 family = CURLPROTO_IMAP;
4803 break;
4804
4805 case CURLPROTO_POP3:
4806 case CURLPROTO_POP3S:
4807 family = CURLPROTO_POP3;
4808 break;
4809
4810 case CURLPROTO_SMTP:
4811 case CURLPROTO_SMTPS:
4812 family = CURLPROTO_SMTP;
4813 break;
4814
4815 case CURLPROTO_RTSP:
4816 family = CURLPROTO_RTSP;
4817 break;
4818
4819 case CURLPROTO_RTMP:
4820 case CURLPROTO_RTMPS:
4821 family = CURLPROTO_RTMP;
4822 break;
4823
4824 case CURLPROTO_RTMPT:
4825 case CURLPROTO_RTMPTS:
4826 family = CURLPROTO_RTMPT;
4827 break;
4828
4829 case CURLPROTO_RTMPE:
4830 family = CURLPROTO_RTMPE;
4831 break;
4832
4833 case CURLPROTO_RTMPTE:
4834 family = CURLPROTO_RTMPTE;
4835 break;
4836
4837 case CURLPROTO_GOPHER:
4838 family = CURLPROTO_GOPHER;
4839 break;
4840
4841 case CURLPROTO_SMB:
4842 case CURLPROTO_SMBS:
4843 family = CURLPROTO_SMB;
4844 break;
4845
4846 default:
4847 family = 0;
4848 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004849 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01004850
Alex Deymod15eaac2016-06-28 14:49:26 -07004851 return family;
Kristian Monsen5ab50182010-05-14 18:53:44 +01004852}