multi_done: if multiplexed, make conn->data point to another transfer
[platform/upstream/curl.git] / lib / url.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
27 #endif
28 #ifdef HAVE_NETDB_H
29 #include <netdb.h>
30 #endif
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
33 #endif
34 #ifdef HAVE_NET_IF_H
35 #include <net/if.h>
36 #endif
37 #ifdef HAVE_IPHLPAPI_H
38 #include <Iphlpapi.h>
39 #endif
40 #ifdef HAVE_SYS_IOCTL_H
41 #include <sys/ioctl.h>
42 #endif
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
45 #endif
46
47 #ifdef __VMS
48 #include <in.h>
49 #include <inet.h>
50 #endif
51
52 #ifdef HAVE_SYS_UN_H
53 #include <sys/un.h>
54 #endif
55
56 #ifndef HAVE_SOCKET
57 #error "We can't compile without socket() support!"
58 #endif
59
60 #include <limits.h>
61
62 #ifdef USE_LIBIDN2
63 #include <idn2.h>
64
65 #elif defined(USE_WIN32_IDN)
66 /* prototype for curl_win32_idn_to_ascii() */
67 bool curl_win32_idn_to_ascii(const char *in, char **out);
68
69 #elif defined(USE_ICU_IDNA)
70 #include <unicode/uidna.h>
71 #endif  /* USE_LIBIDN2 */
72
73 #include "urldata.h"
74 #include "netrc.h"
75
76 #include "formdata.h"
77 #include "mime.h"
78 #include "vtls/vtls.h"
79 #include "hostip.h"
80 #include "transfer.h"
81 #include "sendf.h"
82 #include "progress.h"
83 #include "cookie.h"
84 #include "strcase.h"
85 #include "strerror.h"
86 #include "escape.h"
87 #include "strtok.h"
88 #include "share.h"
89 #include "content_encoding.h"
90 #include "http_digest.h"
91 #include "http_negotiate.h"
92 #include "select.h"
93 #include "multiif.h"
94 #include "easyif.h"
95 #include "speedcheck.h"
96 #include "warnless.h"
97 #include "non-ascii.h"
98 #include "inet_pton.h"
99 #include "getinfo.h"
100 #include "urlapi-int.h"
101 #include "system_win32.h"
102
103 /* And now for the protocols */
104 #include "ftp.h"
105 #include "dict.h"
106 #include "telnet.h"
107 #include "tftp.h"
108 #include "http.h"
109 #include "http2.h"
110 #include "file.h"
111 #include "curl_ldap.h"
112 #include "vssh/ssh.h"
113 #include "imap.h"
114 #include "url.h"
115 #include "connect.h"
116 #include "inet_ntop.h"
117 #include "http_ntlm.h"
118 #include "curl_rtmp.h"
119 #include "gopher.h"
120 #include "http_proxy.h"
121 #include "conncache.h"
122 #include "multihandle.h"
123 #include "dotdot.h"
124 #include "strdup.h"
125 #include "setopt.h"
126 #include "altsvc.h"
127
128 /* The last 3 #include files should be in this order */
129 #include "curl_printf.h"
130 #include "curl_memory.h"
131 #include "memdebug.h"
132
133 #if defined(USE_ICU_IDNA)
134 #define MAX_DOMAIN_NAME_LEN 256
135 #endif  /* USE_ICU_IDNA */
136
137 static void conn_free(struct connectdata *conn);
138 static void free_idnconverted_hostname(struct hostname *host);
139 static unsigned int get_protocol_family(unsigned int protocol);
140
141 /* Some parts of the code (e.g. chunked encoding) assume this buffer has at
142  * more than just a few bytes to play with. Don't let it become too small or
143  * bad things will happen.
144  */
145 #if READBUFFER_SIZE < READBUFFER_MIN
146 # error READBUFFER_SIZE is too small
147 #endif
148
149
150 /*
151  * Protocol table.
152  */
153
154 static const struct Curl_handler * const protocols[] = {
155
156 #ifndef CURL_DISABLE_HTTP
157   &Curl_handler_http,
158 #endif
159
160 #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
161   &Curl_handler_https,
162 #endif
163
164 #ifndef CURL_DISABLE_FTP
165   &Curl_handler_ftp,
166 #endif
167
168 #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
169   &Curl_handler_ftps,
170 #endif
171
172 #ifndef CURL_DISABLE_TELNET
173   &Curl_handler_telnet,
174 #endif
175
176 #ifndef CURL_DISABLE_DICT
177   &Curl_handler_dict,
178 #endif
179
180 #ifndef CURL_DISABLE_LDAP
181   &Curl_handler_ldap,
182 #if !defined(CURL_DISABLE_LDAPS) && \
183     ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
184      (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
185   &Curl_handler_ldaps,
186 #endif
187 #endif
188
189 #ifndef CURL_DISABLE_FILE
190   &Curl_handler_file,
191 #endif
192
193 #ifndef CURL_DISABLE_TFTP
194   &Curl_handler_tftp,
195 #endif
196
197 #if defined(USE_SSH)
198   &Curl_handler_scp,
199 #endif
200
201 #if defined(USE_SSH)
202   &Curl_handler_sftp,
203 #endif
204
205 #ifndef CURL_DISABLE_IMAP
206   &Curl_handler_imap,
207 #ifdef USE_SSL
208   &Curl_handler_imaps,
209 #endif
210 #endif
211
212 #ifndef CURL_DISABLE_POP3
213   &Curl_handler_pop3,
214 #ifdef USE_SSL
215   &Curl_handler_pop3s,
216 #endif
217 #endif
218
219 #if !defined(CURL_DISABLE_SMB) && defined(USE_NTLM) && \
220    (CURL_SIZEOF_CURL_OFF_T > 4) && \
221    (!defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO))
222   &Curl_handler_smb,
223 #ifdef USE_SSL
224   &Curl_handler_smbs,
225 #endif
226 #endif
227
228 #ifndef CURL_DISABLE_SMTP
229   &Curl_handler_smtp,
230 #ifdef USE_SSL
231   &Curl_handler_smtps,
232 #endif
233 #endif
234
235 #ifndef CURL_DISABLE_RTSP
236   &Curl_handler_rtsp,
237 #endif
238
239 #ifndef CURL_DISABLE_GOPHER
240   &Curl_handler_gopher,
241 #endif
242
243 #ifdef USE_LIBRTMP
244   &Curl_handler_rtmp,
245   &Curl_handler_rtmpt,
246   &Curl_handler_rtmpe,
247   &Curl_handler_rtmpte,
248   &Curl_handler_rtmps,
249   &Curl_handler_rtmpts,
250 #endif
251
252   (struct Curl_handler *) NULL
253 };
254
255 /*
256  * Dummy handler for undefined protocol schemes.
257  */
258
259 static const struct Curl_handler Curl_handler_dummy = {
260   "<no protocol>",                      /* scheme */
261   ZERO_NULL,                            /* setup_connection */
262   ZERO_NULL,                            /* do_it */
263   ZERO_NULL,                            /* done */
264   ZERO_NULL,                            /* do_more */
265   ZERO_NULL,                            /* connect_it */
266   ZERO_NULL,                            /* connecting */
267   ZERO_NULL,                            /* doing */
268   ZERO_NULL,                            /* proto_getsock */
269   ZERO_NULL,                            /* doing_getsock */
270   ZERO_NULL,                            /* domore_getsock */
271   ZERO_NULL,                            /* perform_getsock */
272   ZERO_NULL,                            /* disconnect */
273   ZERO_NULL,                            /* readwrite */
274   ZERO_NULL,                            /* connection_check */
275   0,                                    /* defport */
276   0,                                    /* protocol */
277   PROTOPT_NONE                          /* flags */
278 };
279
280 void Curl_freeset(struct Curl_easy *data)
281 {
282   /* Free all dynamic strings stored in the data->set substructure. */
283   enum dupstring i;
284   for(i = (enum dupstring)0; i < STRING_LAST; i++) {
285     Curl_safefree(data->set.str[i]);
286   }
287
288   if(data->change.referer_alloc) {
289     Curl_safefree(data->change.referer);
290     data->change.referer_alloc = FALSE;
291   }
292   data->change.referer = NULL;
293   if(data->change.url_alloc) {
294     Curl_safefree(data->change.url);
295     data->change.url_alloc = FALSE;
296   }
297   data->change.url = NULL;
298
299   Curl_mime_cleanpart(&data->set.mimepost);
300 }
301
302 /* free the URL pieces */
303 static void up_free(struct Curl_easy *data)
304 {
305   struct urlpieces *up = &data->state.up;
306   Curl_safefree(up->scheme);
307   Curl_safefree(up->hostname);
308   Curl_safefree(up->port);
309   Curl_safefree(up->user);
310   Curl_safefree(up->password);
311   Curl_safefree(up->options);
312   Curl_safefree(up->path);
313   Curl_safefree(up->query);
314   curl_url_cleanup(data->state.uh);
315   data->state.uh = NULL;
316 }
317
318 /*
319  * This is the internal function curl_easy_cleanup() calls. This should
320  * cleanup and free all resources associated with this sessionhandle.
321  *
322  * NOTE: if we ever add something that attempts to write to a socket or
323  * similar here, we must ignore SIGPIPE first. It is currently only done
324  * when curl_easy_perform() is invoked.
325  */
326
327 CURLcode Curl_close(struct Curl_easy **datap)
328 {
329   struct Curl_multi *m;
330   struct Curl_easy *data;
331
332   if(!datap || !*datap)
333     return CURLE_OK;
334
335   data = *datap;
336   *datap = NULL;
337
338   Curl_expire_clear(data); /* shut off timers */
339
340   m = data->multi;
341   if(m)
342     /* This handle is still part of a multi handle, take care of this first
343        and detach this handle from there. */
344     curl_multi_remove_handle(data->multi, data);
345
346   if(data->multi_easy) {
347     /* when curl_easy_perform() is used, it creates its own multi handle to
348        use and this is the one */
349     curl_multi_cleanup(data->multi_easy);
350     data->multi_easy = NULL;
351   }
352
353   /* Destroy the timeout list that is held in the easy handle. It is
354      /normally/ done by curl_multi_remove_handle() but this is "just in
355      case" */
356   Curl_llist_destroy(&data->state.timeoutlist, NULL);
357
358   data->magic = 0; /* force a clear AFTER the possibly enforced removal from
359                       the multi handle, since that function uses the magic
360                       field! */
361
362   if(data->state.rangestringalloc)
363     free(data->state.range);
364
365   /* freed here just in case DONE wasn't called */
366   Curl_free_request_state(data);
367
368   /* Close down all open SSL info and sessions */
369   Curl_ssl_close_all(data);
370   Curl_safefree(data->state.first_host);
371   Curl_safefree(data->state.scratch);
372   Curl_ssl_free_certinfo(data);
373
374   /* Cleanup possible redirect junk */
375   free(data->req.newurl);
376   data->req.newurl = NULL;
377
378   if(data->change.referer_alloc) {
379     Curl_safefree(data->change.referer);
380     data->change.referer_alloc = FALSE;
381   }
382   data->change.referer = NULL;
383
384   up_free(data);
385   Curl_safefree(data->state.buffer);
386   Curl_safefree(data->state.headerbuff);
387   Curl_safefree(data->state.ulbuf);
388   Curl_flush_cookies(data, TRUE);
389 #ifdef USE_ALTSVC
390   Curl_altsvc_save(data->asi, data->set.str[STRING_ALTSVC]);
391   Curl_altsvc_cleanup(data->asi);
392   data->asi = NULL;
393 #endif
394 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
395   Curl_http_auth_cleanup_digest(data);
396 #endif
397   Curl_safefree(data->info.contenttype);
398   Curl_safefree(data->info.wouldredirect);
399
400   /* this destroys the channel and we cannot use it anymore after this */
401   Curl_resolver_cleanup(data->state.resolver);
402
403   Curl_http2_cleanup_dependencies(data);
404   Curl_convert_close(data);
405
406   /* No longer a dirty share, if it exists */
407   if(data->share) {
408     Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
409     data->share->dirty--;
410     Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
411   }
412
413 #ifndef CURL_DISABLE_DOH
414   free(data->req.doh.probe[0].serverdoh.memory);
415   free(data->req.doh.probe[1].serverdoh.memory);
416   curl_slist_free_all(data->req.doh.headers);
417 #endif
418
419   /* destruct wildcard structures if it is needed */
420   Curl_wildcard_dtor(&data->wildcard);
421   Curl_freeset(data);
422   free(data);
423   return CURLE_OK;
424 }
425
426 /*
427  * Initialize the UserDefined fields within a Curl_easy.
428  * This may be safely called on a new or existing Curl_easy.
429  */
430 CURLcode Curl_init_userdefined(struct Curl_easy *data)
431 {
432   struct UserDefined *set = &data->set;
433   CURLcode result = CURLE_OK;
434
435   set->out = stdout; /* default output to stdout */
436   set->in_set = stdin;  /* default input from stdin */
437   set->err  = stderr;  /* default stderr to stderr */
438
439   /* use fwrite as default function to store output */
440   set->fwrite_func = (curl_write_callback)fwrite;
441
442   /* use fread as default function to read input */
443   set->fread_func_set = (curl_read_callback)fread;
444   set->is_fread_set = 0;
445   set->is_fwrite_set = 0;
446
447   set->seek_func = ZERO_NULL;
448   set->seek_client = ZERO_NULL;
449
450   /* conversion callbacks for non-ASCII hosts */
451   set->convfromnetwork = ZERO_NULL;
452   set->convtonetwork   = ZERO_NULL;
453   set->convfromutf8    = ZERO_NULL;
454
455   set->filesize = -1;        /* we don't know the size */
456   set->postfieldsize = -1;   /* unknown size */
457   set->maxredirs = -1;       /* allow any amount by default */
458
459   set->httpreq = HTTPREQ_GET; /* Default HTTP request */
460   set->rtspreq = RTSPREQ_OPTIONS; /* Default RTSP request */
461 #ifndef CURL_DISABLE_FTP
462   set->ftp_use_epsv = TRUE;   /* FTP defaults to EPSV operations */
463   set->ftp_use_eprt = TRUE;   /* FTP defaults to EPRT operations */
464   set->ftp_use_pret = FALSE;  /* mainly useful for drftpd servers */
465   set->ftp_filemethod = FTPFILE_MULTICWD;
466 #endif
467   set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
468
469   /* Set the default size of the SSL session ID cache */
470   set->general_ssl.max_ssl_sessions = 5;
471
472   set->proxyport = 0;
473   set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
474   set->httpauth = CURLAUTH_BASIC;  /* defaults to basic */
475   set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
476
477   /* SOCKS5 proxy auth defaults to username/password + GSS-API */
478   set->socks5auth = CURLAUTH_BASIC | CURLAUTH_GSSAPI;
479
480   /* make libcurl quiet by default: */
481   set->hide_progress = TRUE;  /* CURLOPT_NOPROGRESS changes these */
482
483   Curl_mime_initpart(&set->mimepost, data);
484
485   /*
486    * libcurl 7.10 introduced SSL verification *by default*! This needs to be
487    * switched off unless wanted.
488    */
489   set->ssl.primary.verifypeer = TRUE;
490   set->ssl.primary.verifyhost = TRUE;
491 #ifdef USE_TLS_SRP
492   set->ssl.authtype = CURL_TLSAUTH_NONE;
493 #endif
494   set->ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
495                                                       type */
496   set->ssl.primary.sessionid = TRUE; /* session ID caching enabled by
497                                         default */
498   set->proxy_ssl = set->ssl;
499
500   set->new_file_perms = 0644;    /* Default permissions */
501   set->new_directory_perms = 0755; /* Default permissions */
502
503   /* for the *protocols fields we don't use the CURLPROTO_ALL convenience
504      define since we internally only use the lower 16 bits for the passed
505      in bitmask to not conflict with the private bits */
506   set->allowed_protocols = CURLPROTO_ALL;
507   set->redir_protocols = CURLPROTO_HTTP | CURLPROTO_HTTPS | CURLPROTO_FTP |
508                          CURLPROTO_FTPS;
509
510 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
511   /*
512    * disallow unprotected protection negotiation NEC reference implementation
513    * seem not to follow rfc1961 section 4.3/4.4
514    */
515   set->socks5_gssapi_nec = FALSE;
516 #endif
517
518   /* Set the default CA cert bundle/path detected/specified at build time.
519    *
520    * If Schannel is the selected SSL backend then these locations are
521    * ignored. We allow setting CA location for schannel only when explicitly
522    * specified by the user via CURLOPT_CAINFO / --cacert.
523    */
524   if(Curl_ssl_backend() != CURLSSLBACKEND_SCHANNEL) {
525 #if defined(CURL_CA_BUNDLE)
526     result = Curl_setstropt(&set->str[STRING_SSL_CAFILE_ORIG], CURL_CA_BUNDLE);
527     if(result)
528       return result;
529
530     result = Curl_setstropt(&set->str[STRING_SSL_CAFILE_PROXY],
531                             CURL_CA_BUNDLE);
532     if(result)
533       return result;
534 #endif
535 #if defined(CURL_CA_PATH)
536     result = Curl_setstropt(&set->str[STRING_SSL_CAPATH_ORIG], CURL_CA_PATH);
537     if(result)
538       return result;
539
540     result = Curl_setstropt(&set->str[STRING_SSL_CAPATH_PROXY], CURL_CA_PATH);
541     if(result)
542       return result;
543 #endif
544   }
545
546   set->wildcard_enabled = FALSE;
547   set->chunk_bgn      = ZERO_NULL;
548   set->chunk_end      = ZERO_NULL;
549   set->tcp_keepalive = FALSE;
550   set->tcp_keepintvl = 60;
551   set->tcp_keepidle = 60;
552   set->tcp_fastopen = FALSE;
553   set->tcp_nodelay = TRUE;
554   set->ssl_enable_npn = TRUE;
555   set->ssl_enable_alpn = TRUE;
556   set->expect_100_timeout = 1000L; /* Wait for a second by default. */
557   set->sep_headers = TRUE; /* separated header lists by default */
558   set->buffer_size = READBUFFER_SIZE;
559   set->upload_buffer_size = UPLOADBUFFER_DEFAULT;
560   set->happy_eyeballs_timeout = CURL_HET_DEFAULT;
561   set->fnmatch = ZERO_NULL;
562   set->upkeep_interval_ms = CURL_UPKEEP_INTERVAL_DEFAULT;
563   set->maxconnects = DEFAULT_CONNCACHE_SIZE; /* for easy handles */
564   set->maxage_conn = 118;
565   set->http09_allowed = FALSE;
566   set->httpversion =
567 #ifdef USE_NGHTTP2
568     CURL_HTTP_VERSION_2TLS
569 #else
570     CURL_HTTP_VERSION_1_1
571 #endif
572     ;
573   Curl_http2_init_userset(set);
574   return result;
575 }
576
577 /**
578  * Curl_open()
579  *
580  * @param curl is a pointer to a sessionhandle pointer that gets set by this
581  * function.
582  * @return CURLcode
583  */
584
585 CURLcode Curl_open(struct Curl_easy **curl)
586 {
587   CURLcode result;
588   struct Curl_easy *data;
589
590   /* Very simple start-up: alloc the struct, init it with zeroes and return */
591   data = calloc(1, sizeof(struct Curl_easy));
592   if(!data) {
593     /* this is a very serious error */
594     DEBUGF(fprintf(stderr, "Error: calloc of Curl_easy failed\n"));
595     return CURLE_OUT_OF_MEMORY;
596   }
597
598   data->magic = CURLEASY_MAGIC_NUMBER;
599
600   result = Curl_resolver_init(data, &data->state.resolver);
601   if(result) {
602     DEBUGF(fprintf(stderr, "Error: resolver_init failed\n"));
603     free(data);
604     return result;
605   }
606
607   /* We do some initial setup here, all those fields that can't be just 0 */
608
609   data->state.buffer = malloc(READBUFFER_SIZE + 1);
610   if(!data->state.buffer) {
611     DEBUGF(fprintf(stderr, "Error: malloc of buffer failed\n"));
612     result = CURLE_OUT_OF_MEMORY;
613   }
614   else {
615     data->state.headerbuff = malloc(HEADERSIZE);
616     if(!data->state.headerbuff) {
617       DEBUGF(fprintf(stderr, "Error: malloc of headerbuff failed\n"));
618       result = CURLE_OUT_OF_MEMORY;
619     }
620     else {
621       result = Curl_init_userdefined(data);
622
623       data->state.headersize = HEADERSIZE;
624       Curl_convert_init(data);
625       Curl_initinfo(data);
626
627       /* most recent connection is not yet defined */
628       data->state.lastconnect = NULL;
629
630       data->progress.flags |= PGRS_HIDE;
631       data->state.current_speed = -1; /* init to negative == impossible */
632     }
633   }
634
635   if(result) {
636     Curl_resolver_cleanup(data->state.resolver);
637     free(data->state.buffer);
638     free(data->state.headerbuff);
639     Curl_freeset(data);
640     free(data);
641     data = NULL;
642   }
643   else
644     *curl = data;
645
646   return result;
647 }
648
649 #ifdef USE_RECV_BEFORE_SEND_WORKAROUND
650 static void conn_reset_postponed_data(struct connectdata *conn, int num)
651 {
652   struct postponed_data * const psnd = &(conn->postponed[num]);
653   if(psnd->buffer) {
654     DEBUGASSERT(psnd->allocated_size > 0);
655     DEBUGASSERT(psnd->recv_size <= psnd->allocated_size);
656     DEBUGASSERT(psnd->recv_size ?
657                 (psnd->recv_processed < psnd->recv_size) :
658                 (psnd->recv_processed == 0));
659     DEBUGASSERT(psnd->bindsock != CURL_SOCKET_BAD);
660     free(psnd->buffer);
661     psnd->buffer = NULL;
662     psnd->allocated_size = 0;
663     psnd->recv_size = 0;
664     psnd->recv_processed = 0;
665 #ifdef DEBUGBUILD
666     psnd->bindsock = CURL_SOCKET_BAD; /* used only for DEBUGASSERT */
667 #endif /* DEBUGBUILD */
668   }
669   else {
670     DEBUGASSERT(psnd->allocated_size == 0);
671     DEBUGASSERT(psnd->recv_size == 0);
672     DEBUGASSERT(psnd->recv_processed == 0);
673     DEBUGASSERT(psnd->bindsock == CURL_SOCKET_BAD);
674   }
675 }
676
677 static void conn_reset_all_postponed_data(struct connectdata *conn)
678 {
679   conn_reset_postponed_data(conn, 0);
680   conn_reset_postponed_data(conn, 1);
681 }
682 #else  /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
683 /* Use "do-nothing" macro instead of function when workaround not used */
684 #define conn_reset_all_postponed_data(c) do {} while(0)
685 #endif /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
686
687
688 static void conn_shutdown(struct connectdata *conn)
689 {
690   if(!conn)
691     return;
692
693   infof(conn->data, "Closing connection %ld\n", conn->connection_id);
694   DEBUGASSERT(conn->data);
695
696   /* possible left-overs from the async name resolvers */
697   Curl_resolver_cancel(conn);
698
699   /* close the SSL stuff before we close any sockets since they will/may
700      write to the sockets */
701   Curl_ssl_close(conn, FIRSTSOCKET);
702   Curl_ssl_close(conn, SECONDARYSOCKET);
703
704   /* close possibly still open sockets */
705   if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
706     Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
707   if(CURL_SOCKET_BAD != conn->sock[FIRSTSOCKET])
708     Curl_closesocket(conn, conn->sock[FIRSTSOCKET]);
709   if(CURL_SOCKET_BAD != conn->tempsock[0])
710     Curl_closesocket(conn, conn->tempsock[0]);
711   if(CURL_SOCKET_BAD != conn->tempsock[1])
712     Curl_closesocket(conn, conn->tempsock[1]);
713
714   /* unlink ourselves. this should be called last since other shutdown
715      procedures need a valid conn->data and this may clear it. */
716   Curl_conncache_remove_conn(conn->data, conn, TRUE);
717 }
718
719 static void conn_free(struct connectdata *conn)
720 {
721   if(!conn)
722     return;
723
724   free_idnconverted_hostname(&conn->host);
725   free_idnconverted_hostname(&conn->conn_to_host);
726   free_idnconverted_hostname(&conn->http_proxy.host);
727   free_idnconverted_hostname(&conn->socks_proxy.host);
728
729   Curl_safefree(conn->user);
730   Curl_safefree(conn->passwd);
731   Curl_safefree(conn->oauth_bearer);
732   Curl_safefree(conn->sasl_authzid);
733   Curl_safefree(conn->options);
734   Curl_safefree(conn->http_proxy.user);
735   Curl_safefree(conn->socks_proxy.user);
736   Curl_safefree(conn->http_proxy.passwd);
737   Curl_safefree(conn->socks_proxy.passwd);
738   Curl_safefree(conn->allocptr.proxyuserpwd);
739   Curl_safefree(conn->allocptr.uagent);
740   Curl_safefree(conn->allocptr.userpwd);
741   Curl_safefree(conn->allocptr.accept_encoding);
742   Curl_safefree(conn->allocptr.te);
743   Curl_safefree(conn->allocptr.rangeline);
744   Curl_safefree(conn->allocptr.ref);
745   Curl_safefree(conn->allocptr.host);
746   Curl_safefree(conn->allocptr.cookiehost);
747   Curl_safefree(conn->allocptr.rtsp_transport);
748   Curl_safefree(conn->trailer);
749   Curl_safefree(conn->host.rawalloc); /* host name buffer */
750   Curl_safefree(conn->conn_to_host.rawalloc); /* host name buffer */
751   Curl_safefree(conn->hostname_resolve);
752   Curl_safefree(conn->secondaryhostname);
753   Curl_safefree(conn->http_proxy.host.rawalloc); /* http proxy name buffer */
754   Curl_safefree(conn->socks_proxy.host.rawalloc); /* socks proxy name buffer */
755   Curl_safefree(conn->connect_state);
756
757   conn_reset_all_postponed_data(conn);
758   Curl_llist_destroy(&conn->easyq, NULL);
759   Curl_safefree(conn->localdev);
760   Curl_free_primary_ssl_config(&conn->ssl_config);
761   Curl_free_primary_ssl_config(&conn->proxy_ssl_config);
762
763 #ifdef USE_UNIX_SOCKETS
764   Curl_safefree(conn->unix_domain_socket);
765 #endif
766
767 #ifdef USE_SSL
768   Curl_safefree(conn->ssl_extra);
769 #endif
770   free(conn); /* free all the connection oriented data */
771 }
772
773 /*
774  * Disconnects the given connection. Note the connection may not be the
775  * primary connection, like when freeing room in the connection cache or
776  * killing of a dead old connection.
777  *
778  * A connection needs an easy handle when closing down. We support this passed
779  * in separately since the connection to get closed here is often already
780  * disassociated from an easy handle.
781  *
782  * This function MUST NOT reset state in the Curl_easy struct if that
783  * isn't strictly bound to the life-time of *this* particular connection.
784  *
785  */
786
787 CURLcode Curl_disconnect(struct Curl_easy *data,
788                          struct connectdata *conn, bool dead_connection)
789 {
790   if(!conn)
791     return CURLE_OK; /* this is closed and fine already */
792
793   if(!data) {
794     DEBUGF(infof(data, "DISCONNECT without easy handle, ignoring\n"));
795     return CURLE_OK;
796   }
797
798   /*
799    * If this connection isn't marked to force-close, leave it open if there
800    * are other users of it
801    */
802   if(CONN_INUSE(conn) && !dead_connection) {
803     DEBUGF(infof(data, "Curl_disconnect when inuse: %zu\n", CONN_INUSE(conn)));
804     return CURLE_OK;
805   }
806
807   if(conn->dns_entry != NULL) {
808     Curl_resolv_unlock(data, conn->dns_entry);
809     conn->dns_entry = NULL;
810   }
811
812   Curl_hostcache_prune(data); /* kill old DNS cache entries */
813
814 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
815   /* Cleanup NTLM connection-related data */
816   Curl_http_auth_cleanup_ntlm(conn);
817 #endif
818 #if !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
819   /* Cleanup NEGOTIATE connection-related data */
820   Curl_http_auth_cleanup_negotiate(conn);
821 #endif
822
823   /* the protocol specific disconnect handler and conn_shutdown need a transfer
824      for the connection! */
825   conn->data = data;
826
827   if(conn->bits.connect_only)
828     /* treat the connection as dead in CONNECT_ONLY situations */
829     dead_connection = TRUE;
830
831   if(conn->handler->disconnect)
832     /* This is set if protocol-specific cleanups should be made */
833     conn->handler->disconnect(conn, dead_connection);
834
835   conn_shutdown(conn);
836   conn_free(conn);
837   return CURLE_OK;
838 }
839
840 /*
841  * This function should return TRUE if the socket is to be assumed to
842  * be dead. Most commonly this happens when the server has closed the
843  * connection due to inactivity.
844  */
845 static bool SocketIsDead(curl_socket_t sock)
846 {
847   int sval;
848   bool ret_val = TRUE;
849
850   sval = SOCKET_READABLE(sock, 0);
851   if(sval == 0)
852     /* timeout */
853     ret_val = FALSE;
854
855   return ret_val;
856 }
857
858 /*
859  * IsMultiplexingPossible()
860  *
861  * Return a bitmask with the available multiplexing options for the given
862  * requested connection.
863  */
864 static int IsMultiplexingPossible(const struct Curl_easy *handle,
865                                   const struct connectdata *conn)
866 {
867   int avail = 0;
868
869   /* If a HTTP protocol and multiplexing is enabled */
870   if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
871      (!conn->bits.protoconnstart || !conn->bits.close)) {
872
873     if(Curl_multiplex_wanted(handle->multi) &&
874        (handle->set.httpversion >= CURL_HTTP_VERSION_2))
875       /* allows HTTP/2 */
876       avail |= CURLPIPE_MULTIPLEX;
877   }
878   return avail;
879 }
880
881 #ifndef CURL_DISABLE_PROXY
882 static bool
883 proxy_info_matches(const struct proxy_info* data,
884                    const struct proxy_info* needle)
885 {
886   if((data->proxytype == needle->proxytype) &&
887      (data->port == needle->port) &&
888      Curl_safe_strcasecompare(data->host.name, needle->host.name))
889     return TRUE;
890
891   return FALSE;
892 }
893 #else
894 /* disabled, won't get called */
895 #define proxy_info_matches(x,y) FALSE
896 #endif
897
898 /* A connection has to have been idle for a shorter time than 'maxage_conn' to
899    be subject for reuse. The success rate is just too low after this. */
900
901 static bool conn_maxage(struct Curl_easy *data,
902                         struct connectdata *conn,
903                         struct curltime now)
904 {
905   if(!conn->data) {
906     timediff_t idletime = Curl_timediff(now, conn->lastused);
907     idletime /= 1000; /* integer seconds is fine */
908
909     if(idletime > data->set.maxage_conn) {
910       infof(data, "Too old connection (%ld seconds), disconnect it\n",
911             idletime);
912       return TRUE;
913     }
914   }
915   return FALSE;
916 }
917
918 /*
919  * This function checks if the given connection is dead and extracts it from
920  * the connection cache if so.
921  *
922  * When this is called as a Curl_conncache_foreach() callback, the connection
923  * cache lock is held!
924  *
925  * Returns TRUE if the connection was dead and extracted.
926  */
927 static bool extract_if_dead(struct connectdata *conn,
928                             struct Curl_easy *data)
929 {
930   if(!CONN_INUSE(conn) && !conn->data) {
931     /* The check for a dead socket makes sense only if the connection isn't in
932        use */
933     bool dead;
934     struct curltime now = Curl_now();
935     if(conn_maxage(data, conn, now)) {
936       dead = TRUE;
937     }
938     else if(conn->handler->connection_check) {
939       /* The protocol has a special method for checking the state of the
940          connection. Use it to check if the connection is dead. */
941       unsigned int state;
942       struct Curl_easy *olddata = conn->data;
943       conn->data = data; /* use this transfer for now */
944       state = conn->handler->connection_check(conn, CONNCHECK_ISDEAD);
945       conn->data = olddata;
946       dead = (state & CONNRESULT_DEAD);
947     }
948     else {
949       /* Use the general method for determining the death of a connection */
950       dead = SocketIsDead(conn->sock[FIRSTSOCKET]);
951     }
952
953     if(dead) {
954       infof(data, "Connection %ld seems to be dead!\n", conn->connection_id);
955       Curl_conncache_remove_conn(data, conn, FALSE);
956       return TRUE;
957     }
958   }
959   return FALSE;
960 }
961
962 struct prunedead {
963   struct Curl_easy *data;
964   struct connectdata *extracted;
965 };
966
967 /*
968  * Wrapper to use extract_if_dead() function in Curl_conncache_foreach()
969  *
970  */
971 static int call_extract_if_dead(struct connectdata *conn, void *param)
972 {
973   struct prunedead *p = (struct prunedead *)param;
974   if(extract_if_dead(conn, p->data)) {
975     /* stop the iteration here, pass back the connection that was extracted */
976     p->extracted = conn;
977     return 1;
978   }
979   return 0; /* continue iteration */
980 }
981
982 /*
983  * This function scans the connection cache for half-open/dead connections,
984  * closes and removes them.
985  * The cleanup is done at most once per second.
986  */
987 static void prune_dead_connections(struct Curl_easy *data)
988 {
989   struct curltime now = Curl_now();
990   timediff_t elapsed =
991     Curl_timediff(now, data->state.conn_cache->last_cleanup);
992
993   if(elapsed >= 1000L) {
994     struct prunedead prune;
995     prune.data = data;
996     prune.extracted = NULL;
997     while(Curl_conncache_foreach(data, data->state.conn_cache, &prune,
998                                  call_extract_if_dead)) {
999       /* disconnect it */
1000       (void)Curl_disconnect(data, prune.extracted, /* dead_connection */TRUE);
1001     }
1002     data->state.conn_cache->last_cleanup = now;
1003   }
1004 }
1005
1006 /*
1007  * Given one filled in connection struct (named needle), this function should
1008  * detect if there already is one that has all the significant details
1009  * exactly the same and thus should be used instead.
1010  *
1011  * If there is a match, this function returns TRUE - and has marked the
1012  * connection as 'in-use'. It must later be called with ConnectionDone() to
1013  * return back to 'idle' (unused) state.
1014  *
1015  * The force_reuse flag is set if the connection must be used.
1016  */
1017 static bool
1018 ConnectionExists(struct Curl_easy *data,
1019                  struct connectdata *needle,
1020                  struct connectdata **usethis,
1021                  bool *force_reuse,
1022                  bool *waitpipe)
1023 {
1024   struct connectdata *check;
1025   struct connectdata *chosen = 0;
1026   bool foundPendingCandidate = FALSE;
1027   bool canmultiplex = IsMultiplexingPossible(data, needle);
1028   struct connectbundle *bundle;
1029   const char *hostbundle;
1030
1031 #ifdef USE_NTLM
1032   bool wantNTLMhttp = ((data->state.authhost.want &
1033                       (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
1034                       (needle->handler->protocol & PROTO_FAMILY_HTTP));
1035   bool wantProxyNTLMhttp = (needle->bits.proxy_user_passwd &&
1036                            ((data->state.authproxy.want &
1037                            (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
1038                            (needle->handler->protocol & PROTO_FAMILY_HTTP)));
1039 #endif
1040
1041   *force_reuse = FALSE;
1042   *waitpipe = FALSE;
1043
1044   /* Look up the bundle with all the connections to this particular host.
1045      Locks the connection cache, beware of early returns! */
1046   bundle = Curl_conncache_find_bundle(needle, data->state.conn_cache,
1047                                       &hostbundle);
1048   if(bundle) {
1049     /* Max pipe length is zero (unlimited) for multiplexed connections */
1050     struct curl_llist_element *curr;
1051
1052     infof(data, "Found bundle for host %s: %p [%s]\n",
1053           hostbundle, (void *)bundle, (bundle->multiuse == BUNDLE_MULTIPLEX ?
1054                                        "can multiplex" : "serially"));
1055
1056     /* We can't multiplex if we don't know anything about the server */
1057     if(canmultiplex) {
1058       if(bundle->multiuse == BUNDLE_UNKNOWN) {
1059         if(data->set.pipewait) {
1060           infof(data, "Server doesn't support multiplex yet, wait\n");
1061           *waitpipe = TRUE;
1062           Curl_conncache_unlock(data);
1063           return FALSE; /* no re-use */
1064         }
1065
1066         infof(data, "Server doesn't support multiplex (yet)\n");
1067         canmultiplex = FALSE;
1068       }
1069       if((bundle->multiuse == BUNDLE_MULTIPLEX) &&
1070          !Curl_multiplex_wanted(data->multi)) {
1071         infof(data, "Could multiplex, but not asked to!\n");
1072         canmultiplex = FALSE;
1073       }
1074       if(bundle->multiuse == BUNDLE_NO_MULTIUSE) {
1075         infof(data, "Can not multiplex, even if we wanted to!\n");
1076         canmultiplex = FALSE;
1077       }
1078     }
1079
1080     curr = bundle->conn_list.head;
1081     while(curr) {
1082       bool match = FALSE;
1083       size_t multiplexed;
1084
1085       /*
1086        * Note that if we use a HTTP proxy in normal mode (no tunneling), we
1087        * check connections to that proxy and not to the actual remote server.
1088        */
1089       check = curr->ptr;
1090       curr = curr->next;
1091
1092       if(check->bits.connect_only || check->bits.close)
1093         /* connect-only or to-be-closed connections will not be reused */
1094         continue;
1095
1096       multiplexed = CONN_INUSE(check) &&
1097         (bundle->multiuse == BUNDLE_MULTIPLEX);
1098
1099       if(canmultiplex) {
1100         ;
1101       }
1102       else {
1103         if(multiplexed) {
1104           /* can only happen within multi handles, and means that another easy
1105              handle is using this connection */
1106           continue;
1107         }
1108
1109         if(Curl_resolver_asynch()) {
1110           /* ip_addr_str[0] is NUL only if the resolving of the name hasn't
1111              completed yet and until then we don't re-use this connection */
1112           if(!check->ip_addr_str[0]) {
1113             infof(data,
1114                   "Connection #%ld is still name resolving, can't reuse\n",
1115                   check->connection_id);
1116             continue;
1117           }
1118         }
1119
1120         if(check->sock[FIRSTSOCKET] == CURL_SOCKET_BAD) {
1121           foundPendingCandidate = TRUE;
1122           /* Don't pick a connection that hasn't connected yet */
1123           infof(data, "Connection #%ld isn't open enough, can't reuse\n",
1124                 check->connection_id);
1125           continue;
1126         }
1127       }
1128
1129 #ifdef USE_UNIX_SOCKETS
1130       if(needle->unix_domain_socket) {
1131         if(!check->unix_domain_socket)
1132           continue;
1133         if(strcmp(needle->unix_domain_socket, check->unix_domain_socket))
1134           continue;
1135         if(needle->abstract_unix_socket != check->abstract_unix_socket)
1136           continue;
1137       }
1138       else if(check->unix_domain_socket)
1139         continue;
1140 #endif
1141
1142       if((needle->handler->flags&PROTOPT_SSL) !=
1143          (check->handler->flags&PROTOPT_SSL))
1144         /* don't do mixed SSL and non-SSL connections */
1145         if(get_protocol_family(check->handler->protocol) !=
1146            needle->handler->protocol || !check->tls_upgraded)
1147           /* except protocols that have been upgraded via TLS */
1148           continue;
1149
1150       if(needle->bits.httpproxy != check->bits.httpproxy ||
1151          needle->bits.socksproxy != check->bits.socksproxy)
1152         continue;
1153
1154       if(needle->bits.socksproxy && !proxy_info_matches(&needle->socks_proxy,
1155                                                         &check->socks_proxy))
1156         continue;
1157
1158       if(needle->bits.conn_to_host != check->bits.conn_to_host)
1159         /* don't mix connections that use the "connect to host" feature and
1160          * connections that don't use this feature */
1161         continue;
1162
1163       if(needle->bits.conn_to_port != check->bits.conn_to_port)
1164         /* don't mix connections that use the "connect to port" feature and
1165          * connections that don't use this feature */
1166         continue;
1167
1168       if(needle->bits.httpproxy) {
1169         if(!proxy_info_matches(&needle->http_proxy, &check->http_proxy))
1170           continue;
1171
1172         if(needle->bits.tunnel_proxy != check->bits.tunnel_proxy)
1173           continue;
1174
1175         if(needle->http_proxy.proxytype == CURLPROXY_HTTPS) {
1176           /* use https proxy */
1177           if(needle->handler->flags&PROTOPT_SSL) {
1178             /* use double layer ssl */
1179             if(!Curl_ssl_config_matches(&needle->proxy_ssl_config,
1180                                         &check->proxy_ssl_config))
1181               continue;
1182             if(check->proxy_ssl[FIRSTSOCKET].state != ssl_connection_complete)
1183               continue;
1184           }
1185           else {
1186             if(!Curl_ssl_config_matches(&needle->ssl_config,
1187                                         &check->ssl_config))
1188               continue;
1189             if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete)
1190               continue;
1191           }
1192         }
1193       }
1194
1195       DEBUGASSERT(!check->data || GOOD_EASY_HANDLE(check->data));
1196
1197       if(!canmultiplex && check->data)
1198         /* this request can't be multiplexed but the checked connection is
1199            already in use so we skip it */
1200         continue;
1201
1202       if(check->data && (check->data->multi != needle->data->multi))
1203         /* this could be subject for multiplex use, but only if they belong to
1204          * the same multi handle */
1205         continue;
1206
1207       if(needle->localdev || needle->localport) {
1208         /* If we are bound to a specific local end (IP+port), we must not
1209            re-use a random other one, although if we didn't ask for a
1210            particular one we can reuse one that was bound.
1211
1212            This comparison is a bit rough and too strict. Since the input
1213            parameters can be specified in numerous ways and still end up the
1214            same it would take a lot of processing to make it really accurate.
1215            Instead, this matching will assume that re-uses of bound connections
1216            will most likely also re-use the exact same binding parameters and
1217            missing out a few edge cases shouldn't hurt anyone very much.
1218         */
1219         if((check->localport != needle->localport) ||
1220            (check->localportrange != needle->localportrange) ||
1221            (needle->localdev &&
1222             (!check->localdev || strcmp(check->localdev, needle->localdev))))
1223           continue;
1224       }
1225
1226       if(!(needle->handler->flags & PROTOPT_CREDSPERREQUEST)) {
1227         /* This protocol requires credentials per connection,
1228            so verify that we're using the same name and password as well */
1229         if(strcmp(needle->user, check->user) ||
1230            strcmp(needle->passwd, check->passwd)) {
1231           /* one of them was different */
1232           continue;
1233         }
1234       }
1235
1236       if(!needle->bits.httpproxy || (needle->handler->flags&PROTOPT_SSL) ||
1237          needle->bits.tunnel_proxy) {
1238         /* The requested connection does not use a HTTP proxy or it uses SSL or
1239            it is a non-SSL protocol tunneled or it is a non-SSL protocol which
1240            is allowed to be upgraded via TLS */
1241
1242         if((strcasecompare(needle->handler->scheme, check->handler->scheme) ||
1243             (get_protocol_family(check->handler->protocol) ==
1244              needle->handler->protocol && check->tls_upgraded)) &&
1245            (!needle->bits.conn_to_host || strcasecompare(
1246             needle->conn_to_host.name, check->conn_to_host.name)) &&
1247            (!needle->bits.conn_to_port ||
1248              needle->conn_to_port == check->conn_to_port) &&
1249            strcasecompare(needle->host.name, check->host.name) &&
1250            needle->remote_port == check->remote_port) {
1251           /* The schemes match or the the protocol family is the same and the
1252              previous connection was TLS upgraded, and the hostname and host
1253              port match */
1254           if(needle->handler->flags & PROTOPT_SSL) {
1255             /* This is a SSL connection so verify that we're using the same
1256                SSL options as well */
1257             if(!Curl_ssl_config_matches(&needle->ssl_config,
1258                                         &check->ssl_config)) {
1259               DEBUGF(infof(data,
1260                            "Connection #%ld has different SSL parameters, "
1261                            "can't reuse\n",
1262                            check->connection_id));
1263               continue;
1264             }
1265             if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete) {
1266               foundPendingCandidate = TRUE;
1267               DEBUGF(infof(data,
1268                            "Connection #%ld has not started SSL connect, "
1269                            "can't reuse\n",
1270                            check->connection_id));
1271               continue;
1272             }
1273           }
1274           match = TRUE;
1275         }
1276       }
1277       else {
1278         /* The requested connection is using the same HTTP proxy in normal
1279            mode (no tunneling) */
1280         match = TRUE;
1281       }
1282
1283       if(match) {
1284 #if defined(USE_NTLM)
1285         /* If we are looking for an HTTP+NTLM connection, check if this is
1286            already authenticating with the right credentials. If not, keep
1287            looking so that we can reuse NTLM connections if
1288            possible. (Especially we must not reuse the same connection if
1289            partway through a handshake!) */
1290         if(wantNTLMhttp) {
1291           if(strcmp(needle->user, check->user) ||
1292              strcmp(needle->passwd, check->passwd)) {
1293
1294             /* we prefer a credential match, but this is at least a connection
1295                that can be reused and "upgraded" to NTLM */
1296             if(check->http_ntlm_state == NTLMSTATE_NONE)
1297               chosen = check;
1298             continue;
1299           }
1300         }
1301         else if(check->http_ntlm_state != NTLMSTATE_NONE) {
1302           /* Connection is using NTLM auth but we don't want NTLM */
1303           continue;
1304         }
1305
1306         /* Same for Proxy NTLM authentication */
1307         if(wantProxyNTLMhttp) {
1308           /* Both check->http_proxy.user and check->http_proxy.passwd can be
1309            * NULL */
1310           if(!check->http_proxy.user || !check->http_proxy.passwd)
1311             continue;
1312
1313           if(strcmp(needle->http_proxy.user, check->http_proxy.user) ||
1314              strcmp(needle->http_proxy.passwd, check->http_proxy.passwd))
1315             continue;
1316         }
1317         else if(check->proxy_ntlm_state != NTLMSTATE_NONE) {
1318           /* Proxy connection is using NTLM auth but we don't want NTLM */
1319           continue;
1320         }
1321
1322         if(wantNTLMhttp || wantProxyNTLMhttp) {
1323           /* Credentials are already checked, we can use this connection */
1324           chosen = check;
1325
1326           if((wantNTLMhttp &&
1327              (check->http_ntlm_state != NTLMSTATE_NONE)) ||
1328               (wantProxyNTLMhttp &&
1329                (check->proxy_ntlm_state != NTLMSTATE_NONE))) {
1330             /* We must use this connection, no other */
1331             *force_reuse = TRUE;
1332             break;
1333           }
1334
1335           /* Continue look up for a better connection */
1336           continue;
1337         }
1338 #endif
1339         if(canmultiplex) {
1340           /* We can multiplex if we want to. Let's continue looking for
1341              the optimal connection to use. */
1342
1343           if(!multiplexed) {
1344             /* We have the optimal connection. Let's stop looking. */
1345             chosen = check;
1346             break;
1347           }
1348
1349 #ifdef USE_NGHTTP2
1350           /* If multiplexed, make sure we don't go over concurrency limit */
1351           if(check->bits.multiplex) {
1352             /* Multiplexed connections can only be HTTP/2 for now */
1353             struct http_conn *httpc = &check->proto.httpc;
1354             if(multiplexed >= httpc->settings.max_concurrent_streams) {
1355               infof(data, "MAX_CONCURRENT_STREAMS reached, skip (%zu)\n",
1356                     multiplexed);
1357               continue;
1358             }
1359           }
1360 #endif
1361           /* When not multiplexed, we have a match here! */
1362           chosen = check;
1363           infof(data, "Multiplexed connection found!\n");
1364           break;
1365         }
1366         else {
1367           /* We have found a connection. Let's stop searching. */
1368           chosen = check;
1369           break;
1370         }
1371       }
1372     }
1373   }
1374
1375   if(chosen) {
1376     /* mark it as used before releasing the lock */
1377     chosen->data = data; /* own it! */
1378     Curl_conncache_unlock(data);
1379     *usethis = chosen;
1380     return TRUE; /* yes, we found one to use! */
1381   }
1382   Curl_conncache_unlock(data);
1383
1384   if(foundPendingCandidate && data->set.pipewait) {
1385     infof(data,
1386           "Found pending candidate for reuse and CURLOPT_PIPEWAIT is set\n");
1387     *waitpipe = TRUE;
1388   }
1389
1390   return FALSE; /* no matching connecting exists */
1391 }
1392
1393 /*
1394  * verboseconnect() displays verbose information after a connect
1395  */
1396 #ifndef CURL_DISABLE_VERBOSE_STRINGS
1397 void Curl_verboseconnect(struct connectdata *conn)
1398 {
1399   if(conn->data->set.verbose)
1400     infof(conn->data, "Connected to %s (%s) port %ld (#%ld)\n",
1401           conn->bits.socksproxy ? conn->socks_proxy.host.dispname :
1402           conn->bits.httpproxy ? conn->http_proxy.host.dispname :
1403           conn->bits.conn_to_host ? conn->conn_to_host.dispname :
1404           conn->host.dispname,
1405           conn->ip_addr_str, conn->port, conn->connection_id);
1406 }
1407 #endif
1408
1409 /*
1410  * Helpers for IDNA conversions.
1411  */
1412 static bool is_ASCII_name(const char *hostname)
1413 {
1414   const unsigned char *ch = (const unsigned char *)hostname;
1415
1416   while(*ch) {
1417     if(*ch++ & 0x80)
1418       return FALSE;
1419   }
1420   return TRUE;
1421 }
1422
1423 /*
1424  * Strip single trailing dot in the hostname,
1425  * primarily for SNI and http host header.
1426  */
1427 static void strip_trailing_dot(struct hostname *host)
1428 {
1429   size_t len;
1430   if(!host || !host->name)
1431     return;
1432   len = strlen(host->name);
1433   if(len && (host->name[len-1] == '.'))
1434     host->name[len-1] = 0;
1435 }
1436
1437 /*
1438  * Perform any necessary IDN conversion of hostname
1439  */
1440 static CURLcode idnconvert_hostname(struct connectdata *conn,
1441                                     struct hostname *host)
1442 {
1443   struct Curl_easy *data = conn->data;
1444
1445 #ifndef USE_LIBIDN2
1446   (void)data;
1447   (void)conn;
1448 #elif defined(CURL_DISABLE_VERBOSE_STRINGS)
1449   (void)conn;
1450 #endif
1451
1452   /* set the name we use to display the host name */
1453   host->dispname = host->name;
1454
1455   /* Check name for non-ASCII and convert hostname to ACE form if we can */
1456   if(!is_ASCII_name(host->name)) {
1457 #ifdef USE_LIBIDN2
1458     if(idn2_check_version(IDN2_VERSION)) {
1459       char *ace_hostname = NULL;
1460 #if IDN2_VERSION_NUMBER >= 0x00140000
1461       /* IDN2_NFC_INPUT: Normalize input string using normalization form C.
1462          IDN2_NONTRANSITIONAL: Perform Unicode TR46 non-transitional
1463          processing. */
1464       int flags = IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL;
1465 #else
1466       int flags = IDN2_NFC_INPUT;
1467 #endif
1468       int rc = idn2_lookup_ul((const char *)host->name, &ace_hostname, flags);
1469       if(rc == IDN2_OK) {
1470         host->encalloc = (char *)ace_hostname;
1471         /* change the name pointer to point to the encoded hostname */
1472         host->name = host->encalloc;
1473       }
1474       else {
1475         failf(data, "Failed to convert %s to ACE; %s\n", host->name,
1476               idn2_strerror(rc));
1477         return CURLE_URL_MALFORMAT;
1478       }
1479     }
1480 #elif defined(USE_WIN32_IDN)
1481     char *ace_hostname = NULL;
1482
1483     if(curl_win32_idn_to_ascii(host->name, &ace_hostname)) {
1484       host->encalloc = ace_hostname;
1485       /* change the name pointer to point to the encoded hostname */
1486       host->name = host->encalloc;
1487     }
1488     else {
1489       failf(data, "Failed to convert %s to ACE;\n", host->name);
1490       return CURLE_URL_MALFORMAT;
1491     }
1492 #elif defined(USE_ICU_IDNA)
1493     char *ace_hostname = malloc(MAX_DOMAIN_NAME_LEN * sizeof(char));
1494     UErrorCode errorCode = U_ZERO_ERROR;
1495     UIDNAInfo info = UIDNA_INFO_INITIALIZER;
1496     UIDNA *uts46 =
1497       uidna_openUTS46(UIDNA_USE_STD3_RULES|UIDNA_NONTRANSITIONAL_TO_UNICODE,
1498                       &errorCode);
1499     int32_t length = uidna_nameToASCII_UTF8(uts46,
1500       host->name, strlen(host->name),
1501       ace_hostname, MAX_DOMAIN_NAME_LEN, &info, &errorCode);
1502     uidna_close(uts46);
1503
1504     if(errorCode != U_ZERO_ERROR || info.errors || length < 1) {
1505       infof(data, "Failed to convert %s to ACE;\n", host->name);
1506       return CURLE_URL_MALFORMAT;
1507     }
1508     else {
1509       host->encalloc = ace_hostname;
1510       host->name = host->encalloc;
1511     }
1512 #else
1513     infof(data, "IDN support not present, can't parse Unicode domains\n");
1514 #endif
1515   }
1516   return CURLE_OK;
1517 }
1518
1519 /*
1520  * Frees data allocated by idnconvert_hostname()
1521  */
1522 static void free_idnconverted_hostname(struct hostname *host)
1523 {
1524 #if defined(USE_LIBIDN2)
1525   if(host->encalloc) {
1526     idn2_free(host->encalloc); /* must be freed with idn2_free() since this was
1527                                  allocated by libidn */
1528     host->encalloc = NULL;
1529   }
1530 #elif defined(USE_WIN32_IDN) || defined(USE_ICU_IDNA)
1531   free(host->encalloc); /* must be freed with free() since this was
1532                            allocated by curl_win32_idn_to_ascii */
1533   host->encalloc = NULL;
1534 #else
1535   (void)host;
1536 #endif
1537 }
1538
1539 /*
1540  * Allocate and initialize a new connectdata object.
1541  */
1542 static struct connectdata *allocate_conn(struct Curl_easy *data)
1543 {
1544   struct connectdata *conn = calloc(1, sizeof(struct connectdata));
1545   if(!conn)
1546     return NULL;
1547
1548 #ifdef USE_SSL
1549   /* The SSL backend-specific data (ssl_backend_data) objects are allocated as
1550      a separate array to ensure suitable alignment.
1551      Note that these backend pointers can be swapped by vtls (eg ssl backend
1552      data becomes proxy backend data). */
1553   {
1554     size_t sslsize = Curl_ssl->sizeof_ssl_backend_data;
1555     char *ssl = calloc(4, sslsize);
1556     if(!ssl) {
1557       free(conn);
1558       return NULL;
1559     }
1560     conn->ssl_extra = ssl;
1561     conn->ssl[0].backend = (void *)ssl;
1562     conn->ssl[1].backend = (void *)(ssl + sslsize);
1563     conn->proxy_ssl[0].backend = (void *)(ssl + 2 * sslsize);
1564     conn->proxy_ssl[1].backend = (void *)(ssl + 3 * sslsize);
1565   }
1566 #endif
1567
1568   conn->handler = &Curl_handler_dummy;  /* Be sure we have a handler defined
1569                                            already from start to avoid NULL
1570                                            situations and checks */
1571
1572   /* and we setup a few fields in case we end up actually using this struct */
1573
1574   conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD;     /* no file descriptor */
1575   conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
1576   conn->tempsock[0] = CURL_SOCKET_BAD; /* no file descriptor */
1577   conn->tempsock[1] = CURL_SOCKET_BAD; /* no file descriptor */
1578   conn->connection_id = -1;    /* no ID */
1579   conn->port = -1; /* unknown at this point */
1580   conn->remote_port = -1; /* unknown at this point */
1581 #if defined(USE_RECV_BEFORE_SEND_WORKAROUND) && defined(DEBUGBUILD)
1582   conn->postponed[0].bindsock = CURL_SOCKET_BAD; /* no file descriptor */
1583   conn->postponed[1].bindsock = CURL_SOCKET_BAD; /* no file descriptor */
1584 #endif /* USE_RECV_BEFORE_SEND_WORKAROUND && DEBUGBUILD */
1585
1586   /* Default protocol-independent behavior doesn't support persistent
1587      connections, so we set this to force-close. Protocols that support
1588      this need to set this to FALSE in their "curl_do" functions. */
1589   connclose(conn, "Default to force-close");
1590
1591   /* Store creation time to help future close decision making */
1592   conn->created = Curl_now();
1593
1594   /* Store current time to give a baseline to keepalive connection times. */
1595   conn->keepalive = Curl_now();
1596
1597   /* Store off the configured connection upkeep time. */
1598   conn->upkeep_interval_ms = data->set.upkeep_interval_ms;
1599
1600   conn->data = data; /* Setup the association between this connection
1601                         and the Curl_easy */
1602
1603   conn->http_proxy.proxytype = data->set.proxytype;
1604   conn->socks_proxy.proxytype = CURLPROXY_SOCKS4;
1605
1606 #if !defined(CURL_DISABLE_PROXY)
1607   /* note that these two proxy bits are now just on what looks to be
1608      requested, they may be altered down the road */
1609   conn->bits.proxy = (data->set.str[STRING_PROXY] &&
1610                       *data->set.str[STRING_PROXY]) ? TRUE : FALSE;
1611   conn->bits.httpproxy = (conn->bits.proxy &&
1612                           (conn->http_proxy.proxytype == CURLPROXY_HTTP ||
1613                            conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0 ||
1614                            conn->http_proxy.proxytype == CURLPROXY_HTTPS)) ?
1615                            TRUE : FALSE;
1616   conn->bits.socksproxy = (conn->bits.proxy &&
1617                            !conn->bits.httpproxy) ? TRUE : FALSE;
1618
1619   if(data->set.str[STRING_PRE_PROXY] && *data->set.str[STRING_PRE_PROXY]) {
1620     conn->bits.proxy = TRUE;
1621     conn->bits.socksproxy = TRUE;
1622   }
1623
1624   conn->bits.proxy_user_passwd =
1625     (data->set.str[STRING_PROXYUSERNAME]) ? TRUE : FALSE;
1626   conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
1627 #endif /* CURL_DISABLE_PROXY */
1628
1629   conn->bits.user_passwd = (data->set.str[STRING_USERNAME]) ? TRUE : FALSE;
1630 #ifndef CURL_DISABLE_FTP
1631   conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
1632   conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
1633 #endif
1634   conn->ssl_config.verifystatus = data->set.ssl.primary.verifystatus;
1635   conn->ssl_config.verifypeer = data->set.ssl.primary.verifypeer;
1636   conn->ssl_config.verifyhost = data->set.ssl.primary.verifyhost;
1637   conn->proxy_ssl_config.verifystatus =
1638     data->set.proxy_ssl.primary.verifystatus;
1639   conn->proxy_ssl_config.verifypeer = data->set.proxy_ssl.primary.verifypeer;
1640   conn->proxy_ssl_config.verifyhost = data->set.proxy_ssl.primary.verifyhost;
1641   conn->ip_version = data->set.ipver;
1642   conn->bits.connect_only = data->set.connect_only;
1643   conn->transport = TRNSPRT_TCP; /* most of them are TCP streams */
1644
1645 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
1646     defined(NTLM_WB_ENABLED)
1647   conn->ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
1648 #endif
1649
1650   /* Initialize the easy handle list */
1651   Curl_llist_init(&conn->easyq, NULL);
1652
1653 #ifdef HAVE_GSSAPI
1654   conn->data_prot = PROT_CLEAR;
1655 #endif
1656
1657   /* Store the local bind parameters that will be used for this connection */
1658   if(data->set.str[STRING_DEVICE]) {
1659     conn->localdev = strdup(data->set.str[STRING_DEVICE]);
1660     if(!conn->localdev)
1661       goto error;
1662   }
1663   conn->localportrange = data->set.localportrange;
1664   conn->localport = data->set.localport;
1665
1666   /* the close socket stuff needs to be copied to the connection struct as
1667      it may live on without (this specific) Curl_easy */
1668   conn->fclosesocket = data->set.fclosesocket;
1669   conn->closesocket_client = data->set.closesocket_client;
1670   conn->lastused = Curl_now(); /* used now */
1671
1672   return conn;
1673   error:
1674
1675   Curl_llist_destroy(&conn->easyq, NULL);
1676   free(conn->localdev);
1677 #ifdef USE_SSL
1678   free(conn->ssl_extra);
1679 #endif
1680   free(conn);
1681   return NULL;
1682 }
1683
1684 /* returns the handler if the given scheme is built-in */
1685 const struct Curl_handler *Curl_builtin_scheme(const char *scheme)
1686 {
1687   const struct Curl_handler * const *pp;
1688   const struct Curl_handler *p;
1689   /* Scan protocol handler table and match against 'scheme'. The handler may
1690      be changed later when the protocol specific setup function is called. */
1691   for(pp = protocols; (p = *pp) != NULL; pp++)
1692     if(strcasecompare(p->scheme, scheme))
1693       /* Protocol found in table. Check if allowed */
1694       return p;
1695   return NULL; /* not found */
1696 }
1697
1698
1699 static CURLcode findprotocol(struct Curl_easy *data,
1700                              struct connectdata *conn,
1701                              const char *protostr)
1702 {
1703   const struct Curl_handler *p = Curl_builtin_scheme(protostr);
1704
1705   if(p && /* Protocol found in table. Check if allowed */
1706      (data->set.allowed_protocols & p->protocol)) {
1707
1708     /* it is allowed for "normal" request, now do an extra check if this is
1709        the result of a redirect */
1710     if(data->state.this_is_a_follow &&
1711        !(data->set.redir_protocols & p->protocol))
1712       /* nope, get out */
1713       ;
1714     else {
1715       /* Perform setup complement if some. */
1716       conn->handler = conn->given = p;
1717
1718       /* 'port' and 'remote_port' are set in setup_connection_internals() */
1719       return CURLE_OK;
1720     }
1721   }
1722
1723   /* The protocol was not found in the table, but we don't have to assign it
1724      to anything since it is already assigned to a dummy-struct in the
1725      create_conn() function when the connectdata struct is allocated. */
1726   failf(data, "Protocol \"%s\" not supported or disabled in " LIBCURL_NAME,
1727         protostr);
1728
1729   return CURLE_UNSUPPORTED_PROTOCOL;
1730 }
1731
1732
1733 CURLcode Curl_uc_to_curlcode(CURLUcode uc)
1734 {
1735   switch(uc) {
1736   default:
1737     return CURLE_URL_MALFORMAT;
1738   case CURLUE_UNSUPPORTED_SCHEME:
1739     return CURLE_UNSUPPORTED_PROTOCOL;
1740   case CURLUE_OUT_OF_MEMORY:
1741     return CURLE_OUT_OF_MEMORY;
1742   case CURLUE_USER_NOT_ALLOWED:
1743     return CURLE_LOGIN_DENIED;
1744   }
1745 }
1746
1747 /*
1748  * If the URL was set with an IPv6 numerical address with a zone id part, set
1749  * the scope_id based on that!
1750  */
1751
1752 static void zonefrom_url(CURLU *uh, struct connectdata *conn)
1753 {
1754   char *zoneid;
1755   CURLUcode uc;
1756
1757   uc = curl_url_get(uh, CURLUPART_ZONEID, &zoneid, 0);
1758
1759   if(!uc && zoneid) {
1760     char *endp;
1761     unsigned long scope = strtoul(zoneid, &endp, 10);
1762     if(!*endp && (scope < UINT_MAX))
1763       /* A plain number, use it directly as a scope id. */
1764       conn->scope_id = (unsigned int)scope;
1765 #if defined(HAVE_IF_NAMETOINDEX)
1766     else {
1767 #elif defined(WIN32)
1768     else if(Curl_if_nametoindex) {
1769 #endif
1770
1771 #if defined(HAVE_IF_NAMETOINDEX) || defined(WIN32)
1772       /* Zone identifier is not numeric */
1773       unsigned int scopeidx = 0;
1774 #if defined(WIN32)
1775       scopeidx = Curl_if_nametoindex(zoneid);
1776 #else
1777       scopeidx = if_nametoindex(zoneid);
1778 #endif
1779       if(!scopeidx)
1780         infof(conn->data, "Invalid zoneid: %s; %s\n", zoneid,
1781               strerror(errno));
1782       else
1783         conn->scope_id = scopeidx;
1784     }
1785 #endif /* HAVE_IF_NAMETOINDEX || WIN32 */
1786
1787     free(zoneid);
1788   }
1789 }
1790
1791 /*
1792  * Parse URL and fill in the relevant members of the connection struct.
1793  */
1794 static CURLcode parseurlandfillconn(struct Curl_easy *data,
1795                                     struct connectdata *conn)
1796 {
1797   CURLcode result;
1798   CURLU *uh;
1799   CURLUcode uc;
1800   char *hostname;
1801
1802   up_free(data); /* cleanup previous leftovers first */
1803
1804   /* parse the URL */
1805   if(data->set.uh) {
1806     uh = data->state.uh = curl_url_dup(data->set.uh);
1807   }
1808   else {
1809     uh = data->state.uh = curl_url();
1810   }
1811
1812   if(!uh)
1813     return CURLE_OUT_OF_MEMORY;
1814
1815   if(data->set.str[STRING_DEFAULT_PROTOCOL] &&
1816      !Curl_is_absolute_url(data->change.url, NULL, MAX_SCHEME_LEN)) {
1817     char *url;
1818     if(data->change.url_alloc)
1819       free(data->change.url);
1820     url = aprintf("%s://%s", data->set.str[STRING_DEFAULT_PROTOCOL],
1821                   data->change.url);
1822     if(!url)
1823       return CURLE_OUT_OF_MEMORY;
1824     data->change.url = url;
1825     data->change.url_alloc = TRUE;
1826   }
1827
1828   if(!data->set.uh) {
1829     char *newurl;
1830     uc = curl_url_set(uh, CURLUPART_URL, data->change.url,
1831                     CURLU_GUESS_SCHEME |
1832                     CURLU_NON_SUPPORT_SCHEME |
1833                     (data->set.disallow_username_in_url ?
1834                      CURLU_DISALLOW_USER : 0) |
1835                     (data->set.path_as_is ? CURLU_PATH_AS_IS : 0));
1836     if(uc) {
1837       DEBUGF(infof(data, "curl_url_set rejected %s\n", data->change.url));
1838       return Curl_uc_to_curlcode(uc);
1839     }
1840
1841     /* after it was parsed, get the generated normalized version */
1842     uc = curl_url_get(uh, CURLUPART_URL, &newurl, 0);
1843     if(uc)
1844       return Curl_uc_to_curlcode(uc);
1845     if(data->change.url_alloc)
1846       free(data->change.url);
1847     data->change.url = newurl;
1848     data->change.url_alloc = TRUE;
1849   }
1850
1851   uc = curl_url_get(uh, CURLUPART_SCHEME, &data->state.up.scheme, 0);
1852   if(uc)
1853     return Curl_uc_to_curlcode(uc);
1854
1855   result = findprotocol(data, conn, data->state.up.scheme);
1856   if(result)
1857     return result;
1858
1859   uc = curl_url_get(uh, CURLUPART_USER, &data->state.up.user,
1860                     CURLU_URLDECODE);
1861   if(!uc) {
1862     conn->user = strdup(data->state.up.user);
1863     if(!conn->user)
1864       return CURLE_OUT_OF_MEMORY;
1865     conn->bits.user_passwd = TRUE;
1866   }
1867   else if(uc != CURLUE_NO_USER)
1868     return Curl_uc_to_curlcode(uc);
1869
1870   uc = curl_url_get(uh, CURLUPART_PASSWORD, &data->state.up.password,
1871                     CURLU_URLDECODE);
1872   if(!uc) {
1873     conn->passwd = strdup(data->state.up.password);
1874     if(!conn->passwd)
1875       return CURLE_OUT_OF_MEMORY;
1876     conn->bits.user_passwd = TRUE;
1877   }
1878   else if(uc != CURLUE_NO_PASSWORD)
1879     return Curl_uc_to_curlcode(uc);
1880
1881   uc = curl_url_get(uh, CURLUPART_OPTIONS, &data->state.up.options,
1882                     CURLU_URLDECODE);
1883   if(!uc) {
1884     conn->options = strdup(data->state.up.options);
1885     if(!conn->options)
1886       return CURLE_OUT_OF_MEMORY;
1887   }
1888   else if(uc != CURLUE_NO_OPTIONS)
1889     return Curl_uc_to_curlcode(uc);
1890
1891   uc = curl_url_get(uh, CURLUPART_HOST, &data->state.up.hostname, 0);
1892   if(uc) {
1893     if(!strcasecompare("file", data->state.up.scheme))
1894       return CURLE_OUT_OF_MEMORY;
1895   }
1896
1897   uc = curl_url_get(uh, CURLUPART_PATH, &data->state.up.path, 0);
1898   if(uc)
1899     return Curl_uc_to_curlcode(uc);
1900
1901   uc = curl_url_get(uh, CURLUPART_PORT, &data->state.up.port,
1902                     CURLU_DEFAULT_PORT);
1903   if(uc) {
1904     if(!strcasecompare("file", data->state.up.scheme))
1905       return CURLE_OUT_OF_MEMORY;
1906   }
1907   else {
1908     unsigned long port = strtoul(data->state.up.port, NULL, 10);
1909     conn->port = conn->remote_port = curlx_ultous(port);
1910   }
1911
1912   (void)curl_url_get(uh, CURLUPART_QUERY, &data->state.up.query, 0);
1913
1914   hostname = data->state.up.hostname;
1915   if(hostname && hostname[0] == '[') {
1916     /* This looks like an IPv6 address literal. See if there is an address
1917        scope. */
1918     size_t hlen;
1919     conn->bits.ipv6_ip = TRUE;
1920     /* cut off the brackets! */
1921     hostname++;
1922     hlen = strlen(hostname);
1923     hostname[hlen - 1] = 0;
1924
1925     zonefrom_url(uh, conn);
1926   }
1927
1928   /* make sure the connect struct gets its own copy of the host name */
1929   conn->host.rawalloc = strdup(hostname ? hostname : "");
1930   if(!conn->host.rawalloc)
1931     return CURLE_OUT_OF_MEMORY;
1932   conn->host.name = conn->host.rawalloc;
1933
1934   if(data->set.scope_id)
1935     /* Override any scope that was set above.  */
1936     conn->scope_id = data->set.scope_id;
1937
1938   return CURLE_OK;
1939 }
1940
1941
1942 /*
1943  * If we're doing a resumed transfer, we need to setup our stuff
1944  * properly.
1945  */
1946 static CURLcode setup_range(struct Curl_easy *data)
1947 {
1948   struct UrlState *s = &data->state;
1949   s->resume_from = data->set.set_resume_from;
1950   if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
1951     if(s->rangestringalloc)
1952       free(s->range);
1953
1954     if(s->resume_from)
1955       s->range = aprintf("%" CURL_FORMAT_CURL_OFF_T "-", s->resume_from);
1956     else
1957       s->range = strdup(data->set.str[STRING_SET_RANGE]);
1958
1959     s->rangestringalloc = (s->range) ? TRUE : FALSE;
1960
1961     if(!s->range)
1962       return CURLE_OUT_OF_MEMORY;
1963
1964     /* tell ourselves to fetch this range */
1965     s->use_range = TRUE;        /* enable range download */
1966   }
1967   else
1968     s->use_range = FALSE; /* disable range download */
1969
1970   return CURLE_OK;
1971 }
1972
1973
1974 /*
1975  * setup_connection_internals() -
1976  *
1977  * Setup connection internals specific to the requested protocol in the
1978  * Curl_easy. This is inited and setup before the connection is made but
1979  * is about the particular protocol that is to be used.
1980  *
1981  * This MUST get called after proxy magic has been figured out.
1982  */
1983 static CURLcode setup_connection_internals(struct connectdata *conn)
1984 {
1985   const struct Curl_handler * p;
1986   CURLcode result;
1987
1988   /* Perform setup complement if some. */
1989   p = conn->handler;
1990
1991   if(p->setup_connection) {
1992     result = (*p->setup_connection)(conn);
1993
1994     if(result)
1995       return result;
1996
1997     p = conn->handler;              /* May have changed. */
1998   }
1999
2000   if(conn->port < 0)
2001     /* we check for -1 here since if proxy was detected already, this
2002        was very likely already set to the proxy port */
2003     conn->port = p->defport;
2004
2005   return CURLE_OK;
2006 }
2007
2008 /*
2009  * Curl_free_request_state() should free temp data that was allocated in the
2010  * Curl_easy for this single request.
2011  */
2012
2013 void Curl_free_request_state(struct Curl_easy *data)
2014 {
2015   Curl_safefree(data->req.protop);
2016   Curl_safefree(data->req.newurl);
2017
2018 #ifndef CURL_DISABLE_DOH
2019   Curl_close(&data->req.doh.probe[0].easy);
2020   Curl_close(&data->req.doh.probe[1].easy);
2021 #endif
2022 }
2023
2024
2025 #ifndef CURL_DISABLE_PROXY
2026 /****************************************************************
2027 * Checks if the host is in the noproxy list. returns true if it matches
2028 * and therefore the proxy should NOT be used.
2029 ****************************************************************/
2030 static bool check_noproxy(const char *name, const char *no_proxy)
2031 {
2032   /* no_proxy=domain1.dom,host.domain2.dom
2033    *   (a comma-separated list of hosts which should
2034    *   not be proxied, or an asterisk to override
2035    *   all proxy variables)
2036    */
2037   if(no_proxy && no_proxy[0]) {
2038     size_t tok_start;
2039     size_t tok_end;
2040     const char *separator = ", ";
2041     size_t no_proxy_len;
2042     size_t namelen;
2043     char *endptr;
2044     if(strcasecompare("*", no_proxy)) {
2045       return TRUE;
2046     }
2047
2048     /* NO_PROXY was specified and it wasn't just an asterisk */
2049
2050     no_proxy_len = strlen(no_proxy);
2051     if(name[0] == '[') {
2052       /* IPv6 numerical address */
2053       endptr = strchr(name, ']');
2054       if(!endptr)
2055         return FALSE;
2056       name++;
2057       namelen = endptr - name;
2058     }
2059     else
2060       namelen = strlen(name);
2061
2062     for(tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
2063       while(tok_start < no_proxy_len &&
2064             strchr(separator, no_proxy[tok_start]) != NULL) {
2065         /* Look for the beginning of the token. */
2066         ++tok_start;
2067       }
2068
2069       if(tok_start == no_proxy_len)
2070         break; /* It was all trailing separator chars, no more tokens. */
2071
2072       for(tok_end = tok_start; tok_end < no_proxy_len &&
2073             strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end)
2074         /* Look for the end of the token. */
2075         ;
2076
2077       /* To match previous behaviour, where it was necessary to specify
2078        * ".local.com" to prevent matching "notlocal.com", we will leave
2079        * the '.' off.
2080        */
2081       if(no_proxy[tok_start] == '.')
2082         ++tok_start;
2083
2084       if((tok_end - tok_start) <= namelen) {
2085         /* Match the last part of the name to the domain we are checking. */
2086         const char *checkn = name + namelen - (tok_end - tok_start);
2087         if(strncasecompare(no_proxy + tok_start, checkn,
2088                            tok_end - tok_start)) {
2089           if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
2090             /* We either have an exact match, or the previous character is a .
2091              * so it is within the same domain, so no proxy for this host.
2092              */
2093             return TRUE;
2094           }
2095         }
2096       } /* if((tok_end - tok_start) <= namelen) */
2097     } /* for(tok_start = 0; tok_start < no_proxy_len;
2098          tok_start = tok_end + 1) */
2099   } /* NO_PROXY was specified and it wasn't just an asterisk */
2100
2101   return FALSE;
2102 }
2103
2104 #ifndef CURL_DISABLE_HTTP
2105 /****************************************************************
2106 * Detect what (if any) proxy to use. Remember that this selects a host
2107 * name and is not limited to HTTP proxies only.
2108 * The returned pointer must be freed by the caller (unless NULL)
2109 ****************************************************************/
2110 static char *detect_proxy(struct connectdata *conn)
2111 {
2112   char *proxy = NULL;
2113
2114   /* If proxy was not specified, we check for default proxy environment
2115    * variables, to enable i.e Lynx compliance:
2116    *
2117    * http_proxy=http://some.server.dom:port/
2118    * https_proxy=http://some.server.dom:port/
2119    * ftp_proxy=http://some.server.dom:port/
2120    * no_proxy=domain1.dom,host.domain2.dom
2121    *   (a comma-separated list of hosts which should
2122    *   not be proxied, or an asterisk to override
2123    *   all proxy variables)
2124    * all_proxy=http://some.server.dom:port/
2125    *   (seems to exist for the CERN www lib. Probably
2126    *   the first to check for.)
2127    *
2128    * For compatibility, the all-uppercase versions of these variables are
2129    * checked if the lowercase versions don't exist.
2130    */
2131   char proxy_env[128];
2132   const char *protop = conn->handler->scheme;
2133   char *envp = proxy_env;
2134   char *prox;
2135
2136   /* Now, build <protocol>_proxy and check for such a one to use */
2137   while(*protop)
2138     *envp++ = (char)tolower((int)*protop++);
2139
2140   /* append _proxy */
2141   strcpy(envp, "_proxy");
2142
2143   /* read the protocol proxy: */
2144   prox = curl_getenv(proxy_env);
2145
2146   /*
2147    * We don't try the uppercase version of HTTP_PROXY because of
2148    * security reasons:
2149    *
2150    * When curl is used in a webserver application
2151    * environment (cgi or php), this environment variable can
2152    * be controlled by the web server user by setting the
2153    * http header 'Proxy:' to some value.
2154    *
2155    * This can cause 'internal' http/ftp requests to be
2156    * arbitrarily redirected by any external attacker.
2157    */
2158   if(!prox && !strcasecompare("http_proxy", proxy_env)) {
2159     /* There was no lowercase variable, try the uppercase version: */
2160     Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
2161     prox = curl_getenv(proxy_env);
2162   }
2163
2164   envp = proxy_env;
2165   if(prox) {
2166     proxy = prox; /* use this */
2167   }
2168   else {
2169     envp = (char *)"all_proxy";
2170     proxy = curl_getenv(envp); /* default proxy to use */
2171     if(!proxy) {
2172       envp = (char *)"ALL_PROXY";
2173       proxy = curl_getenv(envp);
2174     }
2175   }
2176   if(proxy)
2177     infof(conn->data, "Uses proxy env variable %s == '%s'\n", envp, proxy);
2178
2179   return proxy;
2180 }
2181 #endif /* CURL_DISABLE_HTTP */
2182
2183 /*
2184  * If this is supposed to use a proxy, we need to figure out the proxy
2185  * host name, so that we can re-use an existing connection
2186  * that may exist registered to the same proxy host.
2187  */
2188 static CURLcode parse_proxy(struct Curl_easy *data,
2189                             struct connectdata *conn, char *proxy,
2190                             curl_proxytype proxytype)
2191 {
2192   char *portptr = NULL;
2193   long port = -1;
2194   char *proxyuser = NULL;
2195   char *proxypasswd = NULL;
2196   char *host;
2197   bool sockstype;
2198   CURLUcode uc;
2199   struct proxy_info *proxyinfo;
2200   CURLU *uhp = curl_url();
2201   CURLcode result = CURLE_OK;
2202   char *scheme = NULL;
2203
2204   /* When parsing the proxy, allowing non-supported schemes since we have
2205      these made up ones for proxies. Guess scheme for URLs without it. */
2206   uc = curl_url_set(uhp, CURLUPART_URL, proxy,
2207                     CURLU_NON_SUPPORT_SCHEME|CURLU_GUESS_SCHEME);
2208   if(!uc) {
2209     /* parsed okay as a URL */
2210     uc = curl_url_get(uhp, CURLUPART_SCHEME, &scheme, 0);
2211     if(uc) {
2212       result = CURLE_OUT_OF_MEMORY;
2213       goto error;
2214     }
2215
2216     if(strcasecompare("https", scheme))
2217       proxytype = CURLPROXY_HTTPS;
2218     else if(strcasecompare("socks5h", scheme))
2219       proxytype = CURLPROXY_SOCKS5_HOSTNAME;
2220     else if(strcasecompare("socks5", scheme))
2221       proxytype = CURLPROXY_SOCKS5;
2222     else if(strcasecompare("socks4a", scheme))
2223       proxytype = CURLPROXY_SOCKS4A;
2224     else if(strcasecompare("socks4", scheme) ||
2225             strcasecompare("socks", scheme))
2226       proxytype = CURLPROXY_SOCKS4;
2227     else if(strcasecompare("http", scheme))
2228       ; /* leave it as HTTP or HTTP/1.0 */
2229     else {
2230       /* Any other xxx:// reject! */
2231       failf(data, "Unsupported proxy scheme for \'%s\'", proxy);
2232       result = CURLE_COULDNT_CONNECT;
2233       goto error;
2234     }
2235   }
2236   else {
2237     failf(data, "Unsupported proxy syntax in \'%s\'", proxy);
2238     result = CURLE_COULDNT_RESOLVE_PROXY;
2239     goto error;
2240   }
2241
2242 #ifdef USE_SSL
2243   if(!(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY))
2244 #endif
2245     if(proxytype == CURLPROXY_HTTPS) {
2246       failf(data, "Unsupported proxy \'%s\', libcurl is built without the "
2247                   "HTTPS-proxy support.", proxy);
2248       result = CURLE_NOT_BUILT_IN;
2249       goto error;
2250     }
2251
2252   sockstype =
2253     proxytype == CURLPROXY_SOCKS5_HOSTNAME ||
2254     proxytype == CURLPROXY_SOCKS5 ||
2255     proxytype == CURLPROXY_SOCKS4A ||
2256     proxytype == CURLPROXY_SOCKS4;
2257
2258   proxyinfo = sockstype ? &conn->socks_proxy : &conn->http_proxy;
2259   proxyinfo->proxytype = proxytype;
2260
2261   /* Is there a username and password given in this proxy url? */
2262   curl_url_get(uhp, CURLUPART_USER, &proxyuser, CURLU_URLDECODE);
2263   curl_url_get(uhp, CURLUPART_PASSWORD, &proxypasswd, CURLU_URLDECODE);
2264   if(proxyuser || proxypasswd) {
2265     Curl_safefree(proxyinfo->user);
2266     proxyinfo->user = proxyuser;
2267     Curl_safefree(proxyinfo->passwd);
2268     if(!proxypasswd) {
2269       proxypasswd = strdup("");
2270       if(!proxypasswd) {
2271         result = CURLE_OUT_OF_MEMORY;
2272         goto error;
2273       }
2274     }
2275     proxyinfo->passwd = proxypasswd;
2276     conn->bits.proxy_user_passwd = TRUE; /* enable it */
2277   }
2278
2279   curl_url_get(uhp, CURLUPART_PORT, &portptr, 0);
2280
2281   if(portptr) {
2282     port = strtol(portptr, NULL, 10);
2283     free(portptr);
2284   }
2285   else {
2286     if(data->set.proxyport)
2287       /* None given in the proxy string, then get the default one if it is
2288          given */
2289       port = data->set.proxyport;
2290     else {
2291       if(proxytype == CURLPROXY_HTTPS)
2292         port = CURL_DEFAULT_HTTPS_PROXY_PORT;
2293       else
2294         port = CURL_DEFAULT_PROXY_PORT;
2295     }
2296   }
2297   if(port >= 0) {
2298     proxyinfo->port = port;
2299     if(conn->port < 0 || sockstype || !conn->socks_proxy.host.rawalloc)
2300       conn->port = port;
2301   }
2302
2303   /* now, clone the proxy host name */
2304   uc = curl_url_get(uhp, CURLUPART_HOST, &host, CURLU_URLDECODE);
2305   if(uc) {
2306     result = CURLE_OUT_OF_MEMORY;
2307     goto error;
2308   }
2309   Curl_safefree(proxyinfo->host.rawalloc);
2310   proxyinfo->host.rawalloc = host;
2311   if(host[0] == '[') {
2312     /* this is a numerical IPv6, strip off the brackets */
2313     size_t len = strlen(host);
2314     host[len-1] = 0; /* clear the trailing bracket */
2315     host++;
2316     zonefrom_url(uhp, conn);
2317   }
2318   proxyinfo->host.name = host;
2319
2320   error:
2321   free(scheme);
2322   curl_url_cleanup(uhp);
2323   return result;
2324 }
2325
2326 /*
2327  * Extract the user and password from the authentication string
2328  */
2329 static CURLcode parse_proxy_auth(struct Curl_easy *data,
2330                                  struct connectdata *conn)
2331 {
2332   char proxyuser[MAX_CURL_USER_LENGTH]="";
2333   char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
2334   CURLcode result;
2335
2336   if(data->set.str[STRING_PROXYUSERNAME] != NULL) {
2337     strncpy(proxyuser, data->set.str[STRING_PROXYUSERNAME],
2338             MAX_CURL_USER_LENGTH);
2339     proxyuser[MAX_CURL_USER_LENGTH-1] = '\0';   /*To be on safe side*/
2340   }
2341   if(data->set.str[STRING_PROXYPASSWORD] != NULL) {
2342     strncpy(proxypasswd, data->set.str[STRING_PROXYPASSWORD],
2343             MAX_CURL_PASSWORD_LENGTH);
2344     proxypasswd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
2345   }
2346
2347   result = Curl_urldecode(data, proxyuser, 0, &conn->http_proxy.user, NULL,
2348                           FALSE);
2349   if(!result)
2350     result = Curl_urldecode(data, proxypasswd, 0, &conn->http_proxy.passwd,
2351                             NULL, FALSE);
2352   return result;
2353 }
2354
2355 /* create_conn helper to parse and init proxy values. to be called after unix
2356    socket init but before any proxy vars are evaluated. */
2357 static CURLcode create_conn_helper_init_proxy(struct connectdata *conn)
2358 {
2359   char *proxy = NULL;
2360   char *socksproxy = NULL;
2361   char *no_proxy = NULL;
2362   CURLcode result = CURLE_OK;
2363   struct Curl_easy *data = conn->data;
2364
2365   /*************************************************************
2366    * Extract the user and password from the authentication string
2367    *************************************************************/
2368   if(conn->bits.proxy_user_passwd) {
2369     result = parse_proxy_auth(data, conn);
2370     if(result)
2371       goto out;
2372   }
2373
2374   /*************************************************************
2375    * Detect what (if any) proxy to use
2376    *************************************************************/
2377   if(data->set.str[STRING_PROXY]) {
2378     proxy = strdup(data->set.str[STRING_PROXY]);
2379     /* if global proxy is set, this is it */
2380     if(NULL == proxy) {
2381       failf(data, "memory shortage");
2382       result = CURLE_OUT_OF_MEMORY;
2383       goto out;
2384     }
2385   }
2386
2387   if(data->set.str[STRING_PRE_PROXY]) {
2388     socksproxy = strdup(data->set.str[STRING_PRE_PROXY]);
2389     /* if global socks proxy is set, this is it */
2390     if(NULL == socksproxy) {
2391       failf(data, "memory shortage");
2392       result = CURLE_OUT_OF_MEMORY;
2393       goto out;
2394     }
2395   }
2396
2397   if(!data->set.str[STRING_NOPROXY]) {
2398     const char *p = "no_proxy";
2399     no_proxy = curl_getenv(p);
2400     if(!no_proxy) {
2401       p = "NO_PROXY";
2402       no_proxy = curl_getenv(p);
2403     }
2404     if(no_proxy) {
2405       infof(conn->data, "Uses proxy env variable %s == '%s'\n", p, no_proxy);
2406     }
2407   }
2408
2409   if(check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY] ?
2410       data->set.str[STRING_NOPROXY] : no_proxy)) {
2411     Curl_safefree(proxy);
2412     Curl_safefree(socksproxy);
2413   }
2414 #ifndef CURL_DISABLE_HTTP
2415   else if(!proxy && !socksproxy)
2416     /* if the host is not in the noproxy list, detect proxy. */
2417     proxy = detect_proxy(conn);
2418 #endif /* CURL_DISABLE_HTTP */
2419
2420   Curl_safefree(no_proxy);
2421
2422 #ifdef USE_UNIX_SOCKETS
2423   /* For the time being do not mix proxy and unix domain sockets. See #1274 */
2424   if(proxy && conn->unix_domain_socket) {
2425     free(proxy);
2426     proxy = NULL;
2427   }
2428 #endif
2429
2430   if(proxy && (!*proxy || (conn->handler->flags & PROTOPT_NONETWORK))) {
2431     free(proxy);  /* Don't bother with an empty proxy string or if the
2432                      protocol doesn't work with network */
2433     proxy = NULL;
2434   }
2435   if(socksproxy && (!*socksproxy ||
2436                     (conn->handler->flags & PROTOPT_NONETWORK))) {
2437     free(socksproxy);  /* Don't bother with an empty socks proxy string or if
2438                           the protocol doesn't work with network */
2439     socksproxy = NULL;
2440   }
2441
2442   /***********************************************************************
2443    * If this is supposed to use a proxy, we need to figure out the proxy host
2444    * name, proxy type and port number, so that we can re-use an existing
2445    * connection that may exist registered to the same proxy host.
2446    ***********************************************************************/
2447   if(proxy || socksproxy) {
2448     if(proxy) {
2449       result = parse_proxy(data, conn, proxy, conn->http_proxy.proxytype);
2450       Curl_safefree(proxy); /* parse_proxy copies the proxy string */
2451       if(result)
2452         goto out;
2453     }
2454
2455     if(socksproxy) {
2456       result = parse_proxy(data, conn, socksproxy,
2457                            conn->socks_proxy.proxytype);
2458       /* parse_proxy copies the socks proxy string */
2459       Curl_safefree(socksproxy);
2460       if(result)
2461         goto out;
2462     }
2463
2464     if(conn->http_proxy.host.rawalloc) {
2465 #ifdef CURL_DISABLE_HTTP
2466       /* asking for a HTTP proxy is a bit funny when HTTP is disabled... */
2467       result = CURLE_UNSUPPORTED_PROTOCOL;
2468       goto out;
2469 #else
2470       /* force this connection's protocol to become HTTP if compatible */
2471       if(!(conn->handler->protocol & PROTO_FAMILY_HTTP)) {
2472         if((conn->handler->flags & PROTOPT_PROXY_AS_HTTP) &&
2473            !conn->bits.tunnel_proxy)
2474           conn->handler = &Curl_handler_http;
2475         else
2476           /* if not converting to HTTP over the proxy, enforce tunneling */
2477           conn->bits.tunnel_proxy = TRUE;
2478       }
2479       conn->bits.httpproxy = TRUE;
2480 #endif
2481     }
2482     else {
2483       conn->bits.httpproxy = FALSE; /* not a HTTP proxy */
2484       conn->bits.tunnel_proxy = FALSE; /* no tunneling if not HTTP */
2485     }
2486
2487     if(conn->socks_proxy.host.rawalloc) {
2488       if(!conn->http_proxy.host.rawalloc) {
2489         /* once a socks proxy */
2490         if(!conn->socks_proxy.user) {
2491           conn->socks_proxy.user = conn->http_proxy.user;
2492           conn->http_proxy.user = NULL;
2493           Curl_safefree(conn->socks_proxy.passwd);
2494           conn->socks_proxy.passwd = conn->http_proxy.passwd;
2495           conn->http_proxy.passwd = NULL;
2496         }
2497       }
2498       conn->bits.socksproxy = TRUE;
2499     }
2500     else
2501       conn->bits.socksproxy = FALSE; /* not a socks proxy */
2502   }
2503   else {
2504     conn->bits.socksproxy = FALSE;
2505     conn->bits.httpproxy = FALSE;
2506   }
2507   conn->bits.proxy = conn->bits.httpproxy || conn->bits.socksproxy;
2508
2509   if(!conn->bits.proxy) {
2510     /* we aren't using the proxy after all... */
2511     conn->bits.proxy = FALSE;
2512     conn->bits.httpproxy = FALSE;
2513     conn->bits.socksproxy = FALSE;
2514     conn->bits.proxy_user_passwd = FALSE;
2515     conn->bits.tunnel_proxy = FALSE;
2516   }
2517
2518 out:
2519
2520   free(socksproxy);
2521   free(proxy);
2522   return result;
2523 }
2524 #endif /* CURL_DISABLE_PROXY */
2525
2526 /*
2527  * Curl_parse_login_details()
2528  *
2529  * This is used to parse a login string for user name, password and options in
2530  * the following formats:
2531  *
2532  *   user
2533  *   user:password
2534  *   user:password;options
2535  *   user;options
2536  *   user;options:password
2537  *   :password
2538  *   :password;options
2539  *   ;options
2540  *   ;options:password
2541  *
2542  * Parameters:
2543  *
2544  * login    [in]     - The login string.
2545  * len      [in]     - The length of the login string.
2546  * userp    [in/out] - The address where a pointer to newly allocated memory
2547  *                     holding the user will be stored upon completion.
2548  * passwdp  [in/out] - The address where a pointer to newly allocated memory
2549  *                     holding the password will be stored upon completion.
2550  * optionsp [in/out] - The address where a pointer to newly allocated memory
2551  *                     holding the options will be stored upon completion.
2552  *
2553  * Returns CURLE_OK on success.
2554  */
2555 CURLcode Curl_parse_login_details(const char *login, const size_t len,
2556                                   char **userp, char **passwdp,
2557                                   char **optionsp)
2558 {
2559   CURLcode result = CURLE_OK;
2560   char *ubuf = NULL;
2561   char *pbuf = NULL;
2562   char *obuf = NULL;
2563   const char *psep = NULL;
2564   const char *osep = NULL;
2565   size_t ulen;
2566   size_t plen;
2567   size_t olen;
2568
2569   /* Attempt to find the password separator */
2570   if(passwdp) {
2571     psep = strchr(login, ':');
2572
2573     /* Within the constraint of the login string */
2574     if(psep >= login + len)
2575       psep = NULL;
2576   }
2577
2578   /* Attempt to find the options separator */
2579   if(optionsp) {
2580     osep = strchr(login, ';');
2581
2582     /* Within the constraint of the login string */
2583     if(osep >= login + len)
2584       osep = NULL;
2585   }
2586
2587   /* Calculate the portion lengths */
2588   ulen = (psep ?
2589           (size_t)(osep && psep > osep ? osep - login : psep - login) :
2590           (osep ? (size_t)(osep - login) : len));
2591   plen = (psep ?
2592           (osep && osep > psep ? (size_t)(osep - psep) :
2593                                  (size_t)(login + len - psep)) - 1 : 0);
2594   olen = (osep ?
2595           (psep && psep > osep ? (size_t)(psep - osep) :
2596                                  (size_t)(login + len - osep)) - 1 : 0);
2597
2598   /* Allocate the user portion buffer */
2599   if(userp && ulen) {
2600     ubuf = malloc(ulen + 1);
2601     if(!ubuf)
2602       result = CURLE_OUT_OF_MEMORY;
2603   }
2604
2605   /* Allocate the password portion buffer */
2606   if(!result && passwdp && plen) {
2607     pbuf = malloc(plen + 1);
2608     if(!pbuf) {
2609       free(ubuf);
2610       result = CURLE_OUT_OF_MEMORY;
2611     }
2612   }
2613
2614   /* Allocate the options portion buffer */
2615   if(!result && optionsp && olen) {
2616     obuf = malloc(olen + 1);
2617     if(!obuf) {
2618       free(pbuf);
2619       free(ubuf);
2620       result = CURLE_OUT_OF_MEMORY;
2621     }
2622   }
2623
2624   if(!result) {
2625     /* Store the user portion if necessary */
2626     if(ubuf) {
2627       memcpy(ubuf, login, ulen);
2628       ubuf[ulen] = '\0';
2629       Curl_safefree(*userp);
2630       *userp = ubuf;
2631     }
2632
2633     /* Store the password portion if necessary */
2634     if(pbuf) {
2635       memcpy(pbuf, psep + 1, plen);
2636       pbuf[plen] = '\0';
2637       Curl_safefree(*passwdp);
2638       *passwdp = pbuf;
2639     }
2640
2641     /* Store the options portion if necessary */
2642     if(obuf) {
2643       memcpy(obuf, osep + 1, olen);
2644       obuf[olen] = '\0';
2645       Curl_safefree(*optionsp);
2646       *optionsp = obuf;
2647     }
2648   }
2649
2650   return result;
2651 }
2652
2653 /*************************************************************
2654  * Figure out the remote port number and fix it in the URL
2655  *
2656  * No matter if we use a proxy or not, we have to figure out the remote
2657  * port number of various reasons.
2658  *
2659  * The port number embedded in the URL is replaced, if necessary.
2660  *************************************************************/
2661 static CURLcode parse_remote_port(struct Curl_easy *data,
2662                                   struct connectdata *conn)
2663 {
2664
2665   if(data->set.use_port && data->state.allow_port) {
2666     /* if set, we use this instead of the port possibly given in the URL */
2667     char portbuf[16];
2668     CURLUcode uc;
2669     conn->remote_port = (unsigned short)data->set.use_port;
2670     msnprintf(portbuf, sizeof(portbuf), "%d", conn->remote_port);
2671     uc = curl_url_set(data->state.uh, CURLUPART_PORT, portbuf, 0);
2672     if(uc)
2673       return CURLE_OUT_OF_MEMORY;
2674   }
2675
2676   return CURLE_OK;
2677 }
2678
2679 /*
2680  * Override the login details from the URL with that in the CURLOPT_USERPWD
2681  * option or a .netrc file, if applicable.
2682  */
2683 static CURLcode override_login(struct Curl_easy *data,
2684                                struct connectdata *conn,
2685                                char **userp, char **passwdp, char **optionsp)
2686 {
2687   bool user_changed = FALSE;
2688   bool passwd_changed = FALSE;
2689   CURLUcode uc;
2690
2691   if(data->set.use_netrc == CURL_NETRC_REQUIRED && conn->bits.user_passwd) {
2692     /* ignore user+password in the URL */
2693     if(*userp) {
2694       Curl_safefree(*userp);
2695       user_changed = TRUE;
2696     }
2697     if(*passwdp) {
2698       Curl_safefree(*passwdp);
2699       passwd_changed = TRUE;
2700     }
2701     conn->bits.user_passwd = FALSE; /* disable user+password */
2702   }
2703
2704   if(data->set.str[STRING_USERNAME]) {
2705     free(*userp);
2706     *userp = strdup(data->set.str[STRING_USERNAME]);
2707     if(!*userp)
2708       return CURLE_OUT_OF_MEMORY;
2709     conn->bits.user_passwd = TRUE; /* enable user+password */
2710     user_changed = TRUE;
2711   }
2712
2713   if(data->set.str[STRING_PASSWORD]) {
2714     free(*passwdp);
2715     *passwdp = strdup(data->set.str[STRING_PASSWORD]);
2716     if(!*passwdp)
2717       return CURLE_OUT_OF_MEMORY;
2718     conn->bits.user_passwd = TRUE; /* enable user+password */
2719     passwd_changed = TRUE;
2720   }
2721
2722   if(data->set.str[STRING_OPTIONS]) {
2723     free(*optionsp);
2724     *optionsp = strdup(data->set.str[STRING_OPTIONS]);
2725     if(!*optionsp)
2726       return CURLE_OUT_OF_MEMORY;
2727   }
2728
2729   conn->bits.netrc = FALSE;
2730   if(data->set.use_netrc != CURL_NETRC_IGNORED &&
2731       (!*userp || !**userp || !*passwdp || !**passwdp)) {
2732     bool netrc_user_changed = FALSE;
2733     bool netrc_passwd_changed = FALSE;
2734     int ret;
2735
2736     ret = Curl_parsenetrc(conn->host.name,
2737                           userp, passwdp,
2738                           &netrc_user_changed, &netrc_passwd_changed,
2739                           data->set.str[STRING_NETRC_FILE]);
2740     if(ret > 0) {
2741       infof(data, "Couldn't find host %s in the .netrc file; using defaults\n",
2742             conn->host.name);
2743     }
2744     else if(ret < 0) {
2745       return CURLE_OUT_OF_MEMORY;
2746     }
2747     else {
2748       /* set bits.netrc TRUE to remember that we got the name from a .netrc
2749          file, so that it is safe to use even if we followed a Location: to a
2750          different host or similar. */
2751       conn->bits.netrc = TRUE;
2752       conn->bits.user_passwd = TRUE; /* enable user+password */
2753
2754       if(netrc_user_changed) {
2755         user_changed = TRUE;
2756       }
2757       if(netrc_passwd_changed) {
2758         passwd_changed = TRUE;
2759       }
2760     }
2761   }
2762
2763   /* for updated strings, we update them in the URL */
2764   if(user_changed) {
2765     uc = curl_url_set(data->state.uh, CURLUPART_USER, *userp, 0);
2766     if(uc)
2767       return Curl_uc_to_curlcode(uc);
2768   }
2769   if(passwd_changed) {
2770     uc = curl_url_set(data->state.uh, CURLUPART_PASSWORD, *passwdp, 0);
2771     if(uc)
2772       return Curl_uc_to_curlcode(uc);
2773   }
2774   return CURLE_OK;
2775 }
2776
2777 /*
2778  * Set the login details so they're available in the connection
2779  */
2780 static CURLcode set_login(struct connectdata *conn)
2781 {
2782   CURLcode result = CURLE_OK;
2783   const char *setuser = CURL_DEFAULT_USER;
2784   const char *setpasswd = CURL_DEFAULT_PASSWORD;
2785
2786   /* If our protocol needs a password and we have none, use the defaults */
2787   if((conn->handler->flags & PROTOPT_NEEDSPWD) && !conn->bits.user_passwd)
2788     ;
2789   else {
2790     setuser = "";
2791     setpasswd = "";
2792   }
2793   /* Store the default user */
2794   if(!conn->user) {
2795     conn->user = strdup(setuser);
2796     if(!conn->user)
2797       return CURLE_OUT_OF_MEMORY;
2798   }
2799
2800   /* Store the default password */
2801   if(!conn->passwd) {
2802     conn->passwd = strdup(setpasswd);
2803     if(!conn->passwd)
2804       result = CURLE_OUT_OF_MEMORY;
2805   }
2806
2807   return result;
2808 }
2809
2810 /*
2811  * Parses a "host:port" string to connect to.
2812  * The hostname and the port may be empty; in this case, NULL is returned for
2813  * the hostname and -1 for the port.
2814  */
2815 static CURLcode parse_connect_to_host_port(struct Curl_easy *data,
2816                                            const char *host,
2817                                            char **hostname_result,
2818                                            int *port_result)
2819 {
2820   char *host_dup;
2821   char *hostptr;
2822   char *host_portno;
2823   char *portptr;
2824   int port = -1;
2825
2826 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
2827   (void) data;
2828 #endif
2829
2830   *hostname_result = NULL;
2831   *port_result = -1;
2832
2833   if(!host || !*host)
2834     return CURLE_OK;
2835
2836   host_dup = strdup(host);
2837   if(!host_dup)
2838     return CURLE_OUT_OF_MEMORY;
2839
2840   hostptr = host_dup;
2841
2842   /* start scanning for port number at this point */
2843   portptr = hostptr;
2844
2845   /* detect and extract RFC6874-style IPv6-addresses */
2846   if(*hostptr == '[') {
2847 #ifdef ENABLE_IPV6
2848     char *ptr = ++hostptr; /* advance beyond the initial bracket */
2849     while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '.')))
2850       ptr++;
2851     if(*ptr == '%') {
2852       /* There might be a zone identifier */
2853       if(strncmp("%25", ptr, 3))
2854         infof(data, "Please URL encode %% as %%25, see RFC 6874.\n");
2855       ptr++;
2856       /* Allow unreserved characters as defined in RFC 3986 */
2857       while(*ptr && (ISALPHA(*ptr) || ISXDIGIT(*ptr) || (*ptr == '-') ||
2858                      (*ptr == '.') || (*ptr == '_') || (*ptr == '~')))
2859         ptr++;
2860     }
2861     if(*ptr == ']')
2862       /* yeps, it ended nicely with a bracket as well */
2863       *ptr++ = '\0';
2864     else
2865       infof(data, "Invalid IPv6 address format\n");
2866     portptr = ptr;
2867     /* Note that if this didn't end with a bracket, we still advanced the
2868      * hostptr first, but I can't see anything wrong with that as no host
2869      * name nor a numeric can legally start with a bracket.
2870      */
2871 #else
2872     failf(data, "Use of IPv6 in *_CONNECT_TO without IPv6 support built-in!");
2873     free(host_dup);
2874     return CURLE_NOT_BUILT_IN;
2875 #endif
2876   }
2877
2878   /* Get port number off server.com:1080 */
2879   host_portno = strchr(portptr, ':');
2880   if(host_portno) {
2881     char *endp = NULL;
2882     *host_portno = '\0'; /* cut off number from host name */
2883     host_portno++;
2884     if(*host_portno) {
2885       long portparse = strtol(host_portno, &endp, 10);
2886       if((endp && *endp) || (portparse < 0) || (portparse > 65535)) {
2887         infof(data, "No valid port number in connect to host string (%s)\n",
2888               host_portno);
2889         hostptr = NULL;
2890         port = -1;
2891       }
2892       else
2893         port = (int)portparse; /* we know it will fit */
2894     }
2895   }
2896
2897   /* now, clone the cleaned host name */
2898   if(hostptr) {
2899     *hostname_result = strdup(hostptr);
2900     if(!*hostname_result) {
2901       free(host_dup);
2902       return CURLE_OUT_OF_MEMORY;
2903     }
2904   }
2905
2906   *port_result = port;
2907
2908   free(host_dup);
2909   return CURLE_OK;
2910 }
2911
2912 /*
2913  * Parses one "connect to" string in the form:
2914  * "HOST:PORT:CONNECT-TO-HOST:CONNECT-TO-PORT".
2915  */
2916 static CURLcode parse_connect_to_string(struct Curl_easy *data,
2917                                         struct connectdata *conn,
2918                                         const char *conn_to_host,
2919                                         char **host_result,
2920                                         int *port_result)
2921 {
2922   CURLcode result = CURLE_OK;
2923   const char *ptr = conn_to_host;
2924   int host_match = FALSE;
2925   int port_match = FALSE;
2926
2927   *host_result = NULL;
2928   *port_result = -1;
2929
2930   if(*ptr == ':') {
2931     /* an empty hostname always matches */
2932     host_match = TRUE;
2933     ptr++;
2934   }
2935   else {
2936     /* check whether the URL's hostname matches */
2937     size_t hostname_to_match_len;
2938     char *hostname_to_match = aprintf("%s%s%s",
2939                                       conn->bits.ipv6_ip ? "[" : "",
2940                                       conn->host.name,
2941                                       conn->bits.ipv6_ip ? "]" : "");
2942     if(!hostname_to_match)
2943       return CURLE_OUT_OF_MEMORY;
2944     hostname_to_match_len = strlen(hostname_to_match);
2945     host_match = strncasecompare(ptr, hostname_to_match,
2946                                  hostname_to_match_len);
2947     free(hostname_to_match);
2948     ptr += hostname_to_match_len;
2949
2950     host_match = host_match && *ptr == ':';
2951     ptr++;
2952   }
2953
2954   if(host_match) {
2955     if(*ptr == ':') {
2956       /* an empty port always matches */
2957       port_match = TRUE;
2958       ptr++;
2959     }
2960     else {
2961       /* check whether the URL's port matches */
2962       char *ptr_next = strchr(ptr, ':');
2963       if(ptr_next) {
2964         char *endp = NULL;
2965         long port_to_match = strtol(ptr, &endp, 10);
2966         if((endp == ptr_next) && (port_to_match == conn->remote_port)) {
2967           port_match = TRUE;
2968           ptr = ptr_next + 1;
2969         }
2970       }
2971     }
2972   }
2973
2974   if(host_match && port_match) {
2975     /* parse the hostname and port to connect to */
2976     result = parse_connect_to_host_port(data, ptr, host_result, port_result);
2977   }
2978
2979   return result;
2980 }
2981
2982 /*
2983  * Processes all strings in the "connect to" slist, and uses the "connect
2984  * to host" and "connect to port" of the first string that matches.
2985  */
2986 static CURLcode parse_connect_to_slist(struct Curl_easy *data,
2987                                        struct connectdata *conn,
2988                                        struct curl_slist *conn_to_host)
2989 {
2990   CURLcode result = CURLE_OK;
2991   char *host = NULL;
2992   int port = -1;
2993
2994   while(conn_to_host && !host && port == -1) {
2995     result = parse_connect_to_string(data, conn, conn_to_host->data,
2996                                      &host, &port);
2997     if(result)
2998       return result;
2999
3000     if(host && *host) {
3001       conn->conn_to_host.rawalloc = host;
3002       conn->conn_to_host.name = host;
3003       conn->bits.conn_to_host = TRUE;
3004
3005       infof(data, "Connecting to hostname: %s\n", host);
3006     }
3007     else {
3008       /* no "connect to host" */
3009       conn->bits.conn_to_host = FALSE;
3010       Curl_safefree(host);
3011     }
3012
3013     if(port >= 0) {
3014       conn->conn_to_port = port;
3015       conn->bits.conn_to_port = TRUE;
3016       infof(data, "Connecting to port: %d\n", port);
3017     }
3018     else {
3019       /* no "connect to port" */
3020       conn->bits.conn_to_port = FALSE;
3021       port = -1;
3022     }
3023
3024     conn_to_host = conn_to_host->next;
3025   }
3026
3027 #ifdef USE_ALTSVC
3028   if(data->asi && !host && (port == -1) &&
3029      (conn->handler->protocol == CURLPROTO_HTTPS)) {
3030     /* no connect_to match, try alt-svc! */
3031     enum alpnid srcalpnid;
3032     bool hit;
3033     struct altsvc *as;
3034     const int allowed_versions = ( ALPN_h1
3035 #ifdef USE_NGHTTP2
3036       | ALPN_h2
3037 #endif
3038 #ifdef ENABLE_QUIC
3039       | ALPN_h3
3040 #endif
3041       ) & data->asi->flags;
3042
3043     host = conn->host.rawalloc;
3044 #ifdef USE_NGHTTP2
3045     /* with h2 support, check that first */
3046     srcalpnid = ALPN_h2;
3047     hit = Curl_altsvc_lookup(data->asi,
3048                              srcalpnid, host, conn->remote_port, /* from */
3049                              &as /* to */,
3050                              allowed_versions);
3051     if(!hit)
3052 #endif
3053     {
3054       srcalpnid = ALPN_h1;
3055       hit = Curl_altsvc_lookup(data->asi,
3056                                srcalpnid, host, conn->remote_port, /* from */
3057                                &as /* to */,
3058                                allowed_versions);
3059     }
3060     if(hit) {
3061       char *hostd = strdup((char *)as->dst.host);
3062       if(!hostd)
3063         return CURLE_OUT_OF_MEMORY;
3064       conn->conn_to_host.rawalloc = hostd;
3065       conn->conn_to_host.name = hostd;
3066       conn->bits.conn_to_host = TRUE;
3067       conn->conn_to_port = as->dst.port;
3068       conn->bits.conn_to_port = TRUE;
3069       conn->bits.altused = TRUE;
3070       infof(data, "Alt-svc connecting from [%s]%s:%d to [%s]%s:%d\n",
3071             Curl_alpnid2str(srcalpnid), host, conn->remote_port,
3072             Curl_alpnid2str(as->dst.alpnid), hostd, as->dst.port);
3073       if(srcalpnid != as->dst.alpnid) {
3074         /* protocol version switch */
3075         switch(as->dst.alpnid) {
3076         case ALPN_h1:
3077           conn->httpversion = 11;
3078           break;
3079         case ALPN_h2:
3080           conn->httpversion = 20;
3081           break;
3082         case ALPN_h3:
3083           conn->transport = TRNSPRT_QUIC;
3084           conn->httpversion = 30;
3085           break;
3086         default: /* shouldn't be possible */
3087           break;
3088         }
3089       }
3090     }
3091   }
3092 #endif
3093
3094   return result;
3095 }
3096
3097 /*************************************************************
3098  * Resolve the address of the server or proxy
3099  *************************************************************/
3100 static CURLcode resolve_server(struct Curl_easy *data,
3101                                struct connectdata *conn,
3102                                bool *async)
3103 {
3104   CURLcode result = CURLE_OK;
3105   timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
3106
3107   DEBUGASSERT(conn);
3108   DEBUGASSERT(data);
3109   /*************************************************************
3110    * Resolve the name of the server or proxy
3111    *************************************************************/
3112   if(conn->bits.reuse)
3113     /* We're reusing the connection - no need to resolve anything, and
3114        idnconvert_hostname() was called already in create_conn() for the re-use
3115        case. */
3116     *async = FALSE;
3117
3118   else {
3119     /* this is a fresh connect */
3120     int rc;
3121     struct Curl_dns_entry *hostaddr;
3122
3123 #ifdef USE_UNIX_SOCKETS
3124     if(conn->unix_domain_socket) {
3125       /* Unix domain sockets are local. The host gets ignored, just use the
3126        * specified domain socket address. Do not cache "DNS entries". There is
3127        * no DNS involved and we already have the filesystem path available */
3128       const char *path = conn->unix_domain_socket;
3129
3130       hostaddr = calloc(1, sizeof(struct Curl_dns_entry));
3131       if(!hostaddr)
3132         result = CURLE_OUT_OF_MEMORY;
3133       else {
3134         bool longpath = FALSE;
3135         hostaddr->addr = Curl_unix2addr(path, &longpath,
3136                                         conn->abstract_unix_socket);
3137         if(hostaddr->addr)
3138           hostaddr->inuse++;
3139         else {
3140           /* Long paths are not supported for now */
3141           if(longpath) {
3142             failf(data, "Unix socket path too long: '%s'", path);
3143             result = CURLE_COULDNT_RESOLVE_HOST;
3144           }
3145           else
3146             result = CURLE_OUT_OF_MEMORY;
3147           free(hostaddr);
3148           hostaddr = NULL;
3149         }
3150       }
3151     }
3152     else
3153 #endif
3154     if(!conn->bits.proxy) {
3155       struct hostname *connhost;
3156       if(conn->bits.conn_to_host)
3157         connhost = &conn->conn_to_host;
3158       else
3159         connhost = &conn->host;
3160
3161       /* If not connecting via a proxy, extract the port from the URL, if it is
3162        * there, thus overriding any defaults that might have been set above. */
3163       if(conn->bits.conn_to_port)
3164         conn->port = conn->conn_to_port;
3165       else
3166         conn->port = conn->remote_port;
3167
3168       /* Resolve target host right on */
3169       conn->hostname_resolve = strdup(connhost->name);
3170       if(!conn->hostname_resolve)
3171         return CURLE_OUT_OF_MEMORY;
3172       rc = Curl_resolv_timeout(conn, conn->hostname_resolve, (int)conn->port,
3173                                &hostaddr, timeout_ms);
3174       if(rc == CURLRESOLV_PENDING)
3175         *async = TRUE;
3176
3177       else if(rc == CURLRESOLV_TIMEDOUT)
3178         result = CURLE_OPERATION_TIMEDOUT;
3179
3180       else if(!hostaddr) {
3181         failf(data, "Couldn't resolve host '%s'", connhost->dispname);
3182         result =  CURLE_COULDNT_RESOLVE_HOST;
3183         /* don't return yet, we need to clean up the timeout first */
3184       }
3185     }
3186     else {
3187       /* This is a proxy that hasn't been resolved yet. */
3188
3189       struct hostname * const host = conn->bits.socksproxy ?
3190         &conn->socks_proxy.host : &conn->http_proxy.host;
3191
3192       /* resolve proxy */
3193       conn->hostname_resolve = strdup(host->name);
3194       if(!conn->hostname_resolve)
3195         return CURLE_OUT_OF_MEMORY;
3196       rc = Curl_resolv_timeout(conn, conn->hostname_resolve, (int)conn->port,
3197                                &hostaddr, timeout_ms);
3198
3199       if(rc == CURLRESOLV_PENDING)
3200         *async = TRUE;
3201
3202       else if(rc == CURLRESOLV_TIMEDOUT)
3203         result = CURLE_OPERATION_TIMEDOUT;
3204
3205       else if(!hostaddr) {
3206         failf(data, "Couldn't resolve proxy '%s'", host->dispname);
3207         result = CURLE_COULDNT_RESOLVE_PROXY;
3208         /* don't return yet, we need to clean up the timeout first */
3209       }
3210     }
3211     DEBUGASSERT(conn->dns_entry == NULL);
3212     conn->dns_entry = hostaddr;
3213   }
3214
3215   return result;
3216 }
3217
3218 /*
3219  * Cleanup the connection just allocated before we can move along and use the
3220  * previously existing one.  All relevant data is copied over and old_conn is
3221  * ready for freeing once this function returns.
3222  */
3223 static void reuse_conn(struct connectdata *old_conn,
3224                        struct connectdata *conn)
3225 {
3226   free_idnconverted_hostname(&old_conn->http_proxy.host);
3227   free_idnconverted_hostname(&old_conn->socks_proxy.host);
3228
3229   free(old_conn->http_proxy.host.rawalloc);
3230   free(old_conn->socks_proxy.host.rawalloc);
3231
3232   /* free the SSL config struct from this connection struct as this was
3233      allocated in vain and is targeted for destruction */
3234   Curl_free_primary_ssl_config(&old_conn->ssl_config);
3235   Curl_free_primary_ssl_config(&old_conn->proxy_ssl_config);
3236
3237   conn->data = old_conn->data;
3238
3239   /* get the user+password information from the old_conn struct since it may
3240    * be new for this request even when we re-use an existing connection */
3241   conn->bits.user_passwd = old_conn->bits.user_passwd;
3242   if(conn->bits.user_passwd) {
3243     /* use the new user name and password though */
3244     Curl_safefree(conn->user);
3245     Curl_safefree(conn->passwd);
3246     conn->user = old_conn->user;
3247     conn->passwd = old_conn->passwd;
3248     old_conn->user = NULL;
3249     old_conn->passwd = NULL;
3250   }
3251
3252   conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
3253   if(conn->bits.proxy_user_passwd) {
3254     /* use the new proxy user name and proxy password though */
3255     Curl_safefree(conn->http_proxy.user);
3256     Curl_safefree(conn->socks_proxy.user);
3257     Curl_safefree(conn->http_proxy.passwd);
3258     Curl_safefree(conn->socks_proxy.passwd);
3259     conn->http_proxy.user = old_conn->http_proxy.user;
3260     conn->socks_proxy.user = old_conn->socks_proxy.user;
3261     conn->http_proxy.passwd = old_conn->http_proxy.passwd;
3262     conn->socks_proxy.passwd = old_conn->socks_proxy.passwd;
3263     old_conn->http_proxy.user = NULL;
3264     old_conn->socks_proxy.user = NULL;
3265     old_conn->http_proxy.passwd = NULL;
3266     old_conn->socks_proxy.passwd = NULL;
3267   }
3268
3269   /* host can change, when doing keepalive with a proxy or if the case is
3270      different this time etc */
3271   free_idnconverted_hostname(&conn->host);
3272   free_idnconverted_hostname(&conn->conn_to_host);
3273   Curl_safefree(conn->host.rawalloc);
3274   Curl_safefree(conn->conn_to_host.rawalloc);
3275   conn->host = old_conn->host;
3276   conn->conn_to_host = old_conn->conn_to_host;
3277   conn->conn_to_port = old_conn->conn_to_port;
3278   conn->remote_port = old_conn->remote_port;
3279   Curl_safefree(conn->hostname_resolve);
3280
3281   conn->hostname_resolve = old_conn->hostname_resolve;
3282   old_conn->hostname_resolve = NULL;
3283
3284   /* persist connection info in session handle */
3285   Curl_persistconninfo(conn);
3286
3287   conn_reset_all_postponed_data(old_conn); /* free buffers */
3288
3289   /* re-use init */
3290   conn->bits.reuse = TRUE; /* yes, we're re-using here */
3291
3292   Curl_safefree(old_conn->user);
3293   Curl_safefree(old_conn->passwd);
3294   Curl_safefree(old_conn->options);
3295   Curl_safefree(old_conn->http_proxy.user);
3296   Curl_safefree(old_conn->socks_proxy.user);
3297   Curl_safefree(old_conn->http_proxy.passwd);
3298   Curl_safefree(old_conn->socks_proxy.passwd);
3299   Curl_safefree(old_conn->localdev);
3300   Curl_llist_destroy(&old_conn->easyq, NULL);
3301
3302 #ifdef USE_UNIX_SOCKETS
3303   Curl_safefree(old_conn->unix_domain_socket);
3304 #endif
3305 }
3306
3307 /**
3308  * create_conn() sets up a new connectdata struct, or re-uses an already
3309  * existing one, and resolves host name.
3310  *
3311  * if this function returns CURLE_OK and *async is set to TRUE, the resolve
3312  * response will be coming asynchronously. If *async is FALSE, the name is
3313  * already resolved.
3314  *
3315  * @param data The sessionhandle pointer
3316  * @param in_connect is set to the next connection data pointer
3317  * @param async is set TRUE when an async DNS resolution is pending
3318  * @see Curl_setup_conn()
3319  *
3320  * *NOTE* this function assigns the conn->data pointer!
3321  */
3322
3323 static CURLcode create_conn(struct Curl_easy *data,
3324                             struct connectdata **in_connect,
3325                             bool *async)
3326 {
3327   CURLcode result = CURLE_OK;
3328   struct connectdata *conn;
3329   struct connectdata *conn_temp = NULL;
3330   bool reuse;
3331   bool connections_available = TRUE;
3332   bool force_reuse = FALSE;
3333   bool waitpipe = FALSE;
3334   size_t max_host_connections = Curl_multi_max_host_connections(data->multi);
3335   size_t max_total_connections = Curl_multi_max_total_connections(data->multi);
3336
3337   *async = FALSE;
3338   *in_connect = NULL;
3339
3340   /*************************************************************
3341    * Check input data
3342    *************************************************************/
3343   if(!data->change.url) {
3344     result = CURLE_URL_MALFORMAT;
3345     goto out;
3346   }
3347
3348   /* First, split up the current URL in parts so that we can use the
3349      parts for checking against the already present connections. In order
3350      to not have to modify everything at once, we allocate a temporary
3351      connection data struct and fill in for comparison purposes. */
3352   conn = allocate_conn(data);
3353
3354   if(!conn) {
3355     result = CURLE_OUT_OF_MEMORY;
3356     goto out;
3357   }
3358
3359   /* We must set the return variable as soon as possible, so that our
3360      parent can cleanup any possible allocs we may have done before
3361      any failure */
3362   *in_connect = conn;
3363
3364   result = parseurlandfillconn(data, conn);
3365   if(result)
3366     goto out;
3367
3368   if(data->set.str[STRING_BEARER]) {
3369     conn->oauth_bearer = strdup(data->set.str[STRING_BEARER]);
3370     if(!conn->oauth_bearer) {
3371       result = CURLE_OUT_OF_MEMORY;
3372       goto out;
3373     }
3374   }
3375
3376   if(data->set.str[STRING_SASL_AUTHZID]) {
3377     conn->sasl_authzid = strdup(data->set.str[STRING_SASL_AUTHZID]);
3378     if(!conn->sasl_authzid) {
3379       result = CURLE_OUT_OF_MEMORY;
3380       goto out;
3381     }
3382   }
3383
3384 #ifdef USE_UNIX_SOCKETS
3385   if(data->set.str[STRING_UNIX_SOCKET_PATH]) {
3386     conn->unix_domain_socket = strdup(data->set.str[STRING_UNIX_SOCKET_PATH]);
3387     if(conn->unix_domain_socket == NULL) {
3388       result = CURLE_OUT_OF_MEMORY;
3389       goto out;
3390     }
3391     conn->abstract_unix_socket = data->set.abstract_unix_socket;
3392   }
3393 #endif
3394
3395   /* After the unix socket init but before the proxy vars are used, parse and
3396      initialize the proxy vars */
3397 #ifndef CURL_DISABLE_PROXY
3398   result = create_conn_helper_init_proxy(conn);
3399   if(result)
3400     goto out;
3401 #endif
3402
3403   /*************************************************************
3404    * If the protocol is using SSL and HTTP proxy is used, we set
3405    * the tunnel_proxy bit.
3406    *************************************************************/
3407   if((conn->given->flags&PROTOPT_SSL) && conn->bits.httpproxy)
3408     conn->bits.tunnel_proxy = TRUE;
3409
3410   /*************************************************************
3411    * Figure out the remote port number and fix it in the URL
3412    *************************************************************/
3413   result = parse_remote_port(data, conn);
3414   if(result)
3415     goto out;
3416
3417   /* Check for overridden login details and set them accordingly so they
3418      they are known when protocol->setup_connection is called! */
3419   result = override_login(data, conn, &conn->user, &conn->passwd,
3420                           &conn->options);
3421   if(result)
3422     goto out;
3423
3424   result = set_login(conn); /* default credentials */
3425   if(result)
3426     goto out;
3427
3428   /*************************************************************
3429    * Process the "connect to" linked list of hostname/port mappings.
3430    * Do this after the remote port number has been fixed in the URL.
3431    *************************************************************/
3432   result = parse_connect_to_slist(data, conn, data->set.connect_to);
3433   if(result)
3434     goto out;
3435
3436   /*************************************************************
3437    * IDN-convert the hostnames
3438    *************************************************************/
3439   result = idnconvert_hostname(conn, &conn->host);
3440   if(result)
3441     goto out;
3442   if(conn->bits.conn_to_host) {
3443     result = idnconvert_hostname(conn, &conn->conn_to_host);
3444     if(result)
3445       goto out;
3446   }
3447   if(conn->bits.httpproxy) {
3448     result = idnconvert_hostname(conn, &conn->http_proxy.host);
3449     if(result)
3450       goto out;
3451   }
3452   if(conn->bits.socksproxy) {
3453     result = idnconvert_hostname(conn, &conn->socks_proxy.host);
3454     if(result)
3455       goto out;
3456   }
3457
3458   /*************************************************************
3459    * Check whether the host and the "connect to host" are equal.
3460    * Do this after the hostnames have been IDN-converted.
3461    *************************************************************/
3462   if(conn->bits.conn_to_host &&
3463      strcasecompare(conn->conn_to_host.name, conn->host.name)) {
3464     conn->bits.conn_to_host = FALSE;
3465   }
3466
3467   /*************************************************************
3468    * Check whether the port and the "connect to port" are equal.
3469    * Do this after the remote port number has been fixed in the URL.
3470    *************************************************************/
3471   if(conn->bits.conn_to_port && conn->conn_to_port == conn->remote_port) {
3472     conn->bits.conn_to_port = FALSE;
3473   }
3474
3475   /*************************************************************
3476    * If the "connect to" feature is used with an HTTP proxy,
3477    * we set the tunnel_proxy bit.
3478    *************************************************************/
3479   if((conn->bits.conn_to_host || conn->bits.conn_to_port) &&
3480       conn->bits.httpproxy)
3481     conn->bits.tunnel_proxy = TRUE;
3482
3483   /*************************************************************
3484    * Setup internals depending on protocol. Needs to be done after
3485    * we figured out what/if proxy to use.
3486    *************************************************************/
3487   result = setup_connection_internals(conn);
3488   if(result)
3489     goto out;
3490
3491   conn->recv[FIRSTSOCKET] = Curl_recv_plain;
3492   conn->send[FIRSTSOCKET] = Curl_send_plain;
3493   conn->recv[SECONDARYSOCKET] = Curl_recv_plain;
3494   conn->send[SECONDARYSOCKET] = Curl_send_plain;
3495
3496   conn->bits.tcp_fastopen = data->set.tcp_fastopen;
3497
3498   /***********************************************************************
3499    * file: is a special case in that it doesn't need a network connection
3500    ***********************************************************************/
3501 #ifndef CURL_DISABLE_FILE
3502   if(conn->handler->flags & PROTOPT_NONETWORK) {
3503     bool done;
3504     /* this is supposed to be the connect function so we better at least check
3505        that the file is present here! */
3506     DEBUGASSERT(conn->handler->connect_it);
3507     Curl_persistconninfo(conn);
3508     result = conn->handler->connect_it(conn, &done);
3509
3510     /* Setup a "faked" transfer that'll do nothing */
3511     if(!result) {
3512       conn->bits.tcpconnect[FIRSTSOCKET] = TRUE; /* we are "connected */
3513
3514       result = Curl_conncache_add_conn(data->state.conn_cache, conn);
3515       if(result)
3516         goto out;
3517
3518       /*
3519        * Setup whatever necessary for a resumed transfer
3520        */
3521       result = setup_range(data);
3522       if(result) {
3523         DEBUGASSERT(conn->handler->done);
3524         /* we ignore the return code for the protocol-specific DONE */
3525         (void)conn->handler->done(conn, result, FALSE);
3526         goto out;
3527       }
3528       Curl_attach_connnection(data, conn);
3529       Curl_setup_transfer(data, -1, -1, FALSE, -1);
3530     }
3531
3532     /* since we skip do_init() */
3533     Curl_init_do(data, conn);
3534
3535     goto out;
3536   }
3537 #endif
3538
3539   /* Get a cloned copy of the SSL config situation stored in the
3540      connection struct. But to get this going nicely, we must first make
3541      sure that the strings in the master copy are pointing to the correct
3542      strings in the session handle strings array!
3543
3544      Keep in mind that the pointers in the master copy are pointing to strings
3545      that will be freed as part of the Curl_easy struct, but all cloned
3546      copies will be separately allocated.
3547   */
3548   data->set.ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_ORIG];
3549   data->set.proxy_ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_PROXY];
3550   data->set.ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_ORIG];
3551   data->set.proxy_ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_PROXY];
3552   data->set.ssl.primary.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
3553   data->set.proxy_ssl.primary.random_file =
3554     data->set.str[STRING_SSL_RANDOM_FILE];
3555   data->set.ssl.primary.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
3556   data->set.proxy_ssl.primary.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
3557   data->set.ssl.primary.cipher_list =
3558     data->set.str[STRING_SSL_CIPHER_LIST_ORIG];
3559   data->set.proxy_ssl.primary.cipher_list =
3560     data->set.str[STRING_SSL_CIPHER_LIST_PROXY];
3561   data->set.ssl.primary.cipher_list13 =
3562     data->set.str[STRING_SSL_CIPHER13_LIST_ORIG];
3563   data->set.proxy_ssl.primary.cipher_list13 =
3564     data->set.str[STRING_SSL_CIPHER13_LIST_PROXY];
3565   data->set.ssl.primary.pinned_key =
3566     data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
3567   data->set.proxy_ssl.primary.pinned_key =
3568     data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY];
3569
3570   data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE_ORIG];
3571   data->set.proxy_ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE_PROXY];
3572   data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT_ORIG];
3573   data->set.proxy_ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT_PROXY];
3574   data->set.ssl.cert = data->set.str[STRING_CERT_ORIG];
3575   data->set.proxy_ssl.cert = data->set.str[STRING_CERT_PROXY];
3576   data->set.ssl.cert_type = data->set.str[STRING_CERT_TYPE_ORIG];
3577   data->set.proxy_ssl.cert_type = data->set.str[STRING_CERT_TYPE_PROXY];
3578   data->set.ssl.key = data->set.str[STRING_KEY_ORIG];
3579   data->set.proxy_ssl.key = data->set.str[STRING_KEY_PROXY];
3580   data->set.ssl.key_type = data->set.str[STRING_KEY_TYPE_ORIG];
3581   data->set.proxy_ssl.key_type = data->set.str[STRING_KEY_TYPE_PROXY];
3582   data->set.ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_ORIG];
3583   data->set.proxy_ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_PROXY];
3584   data->set.ssl.primary.clientcert = data->set.str[STRING_CERT_ORIG];
3585   data->set.proxy_ssl.primary.clientcert = data->set.str[STRING_CERT_PROXY];
3586 #ifdef USE_TLS_SRP
3587   data->set.ssl.username = data->set.str[STRING_TLSAUTH_USERNAME_ORIG];
3588   data->set.proxy_ssl.username = data->set.str[STRING_TLSAUTH_USERNAME_PROXY];
3589   data->set.ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD_ORIG];
3590   data->set.proxy_ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD_PROXY];
3591 #endif
3592
3593   if(!Curl_clone_primary_ssl_config(&data->set.ssl.primary,
3594      &conn->ssl_config)) {
3595     result = CURLE_OUT_OF_MEMORY;
3596     goto out;
3597   }
3598
3599   if(!Curl_clone_primary_ssl_config(&data->set.proxy_ssl.primary,
3600                                     &conn->proxy_ssl_config)) {
3601     result = CURLE_OUT_OF_MEMORY;
3602     goto out;
3603   }
3604
3605   prune_dead_connections(data);
3606
3607   /*************************************************************
3608    * Check the current list of connections to see if we can
3609    * re-use an already existing one or if we have to create a
3610    * new one.
3611    *************************************************************/
3612
3613   DEBUGASSERT(conn->user);
3614   DEBUGASSERT(conn->passwd);
3615
3616   /* reuse_fresh is TRUE if we are told to use a new connection by force, but
3617      we only acknowledge this option if this is not a re-used connection
3618      already (which happens due to follow-location or during a HTTP
3619      authentication phase). CONNECT_ONLY transfers also refuse reuse. */
3620   if((data->set.reuse_fresh && !data->state.this_is_a_follow) ||
3621      data->set.connect_only)
3622     reuse = FALSE;
3623   else
3624     reuse = ConnectionExists(data, conn, &conn_temp, &force_reuse, &waitpipe);
3625
3626   if(reuse) {
3627     /*
3628      * We already have a connection for this, we got the former connection
3629      * in the conn_temp variable and thus we need to cleanup the one we
3630      * just allocated before we can move along and use the previously
3631      * existing one.
3632      */
3633     reuse_conn(conn, conn_temp);
3634 #ifdef USE_SSL
3635     free(conn->ssl_extra);
3636 #endif
3637     free(conn);          /* we don't need this anymore */
3638     conn = conn_temp;
3639     *in_connect = conn;
3640
3641     infof(data, "Re-using existing connection! (#%ld) with %s %s\n",
3642           conn->connection_id,
3643           conn->bits.proxy?"proxy":"host",
3644           conn->socks_proxy.host.name ? conn->socks_proxy.host.dispname :
3645           conn->http_proxy.host.name ? conn->http_proxy.host.dispname :
3646                                        conn->host.dispname);
3647   }
3648   else {
3649     /* We have decided that we want a new connection. However, we may not
3650        be able to do that if we have reached the limit of how many
3651        connections we are allowed to open. */
3652
3653     if(conn->handler->flags & PROTOPT_ALPN_NPN) {
3654       /* The protocol wants it, so set the bits if enabled in the easy handle
3655          (default) */
3656       if(data->set.ssl_enable_alpn)
3657         conn->bits.tls_enable_alpn = TRUE;
3658       if(data->set.ssl_enable_npn)
3659         conn->bits.tls_enable_npn = TRUE;
3660     }
3661
3662     if(waitpipe)
3663       /* There is a connection that *might* become usable for multiplexing
3664          "soon", and we wait for that */
3665       connections_available = FALSE;
3666     else {
3667       /* this gets a lock on the conncache */
3668       const char *bundlehost;
3669       struct connectbundle *bundle =
3670         Curl_conncache_find_bundle(conn, data->state.conn_cache, &bundlehost);
3671
3672       if(max_host_connections > 0 && bundle &&
3673          (bundle->num_connections >= max_host_connections)) {
3674         struct connectdata *conn_candidate;
3675
3676         /* The bundle is full. Extract the oldest connection. */
3677         conn_candidate = Curl_conncache_extract_bundle(data, bundle);
3678         Curl_conncache_unlock(data);
3679
3680         if(conn_candidate)
3681           (void)Curl_disconnect(data, conn_candidate,
3682                                 /* dead_connection */ FALSE);
3683         else {
3684           infof(data, "No more connections allowed to host %s: %zu\n",
3685                 bundlehost, max_host_connections);
3686           connections_available = FALSE;
3687         }
3688       }
3689       else
3690         Curl_conncache_unlock(data);
3691
3692     }
3693
3694     if(connections_available &&
3695        (max_total_connections > 0) &&
3696        (Curl_conncache_size(data) >= max_total_connections)) {
3697       struct connectdata *conn_candidate;
3698
3699       /* The cache is full. Let's see if we can kill a connection. */
3700       conn_candidate = Curl_conncache_extract_oldest(data);
3701       if(conn_candidate)
3702         (void)Curl_disconnect(data, conn_candidate,
3703                               /* dead_connection */ FALSE);
3704       else {
3705         infof(data, "No connections available in cache\n");
3706         connections_available = FALSE;
3707       }
3708     }
3709
3710     if(!connections_available) {
3711       infof(data, "No connections available.\n");
3712
3713       conn_free(conn);
3714       *in_connect = NULL;
3715
3716       result = CURLE_NO_CONNECTION_AVAILABLE;
3717       goto out;
3718     }
3719     else {
3720       /*
3721        * This is a brand new connection, so let's store it in the connection
3722        * cache of ours!
3723        */
3724       result = Curl_conncache_add_conn(data->state.conn_cache, conn);
3725       if(result)
3726         goto out;
3727     }
3728
3729 #if defined(USE_NTLM)
3730     /* If NTLM is requested in a part of this connection, make sure we don't
3731        assume the state is fine as this is a fresh connection and NTLM is
3732        connection based. */
3733     if((data->state.authhost.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
3734        data->state.authhost.done) {
3735       infof(data, "NTLM picked AND auth done set, clear picked!\n");
3736       data->state.authhost.picked = CURLAUTH_NONE;
3737       data->state.authhost.done = FALSE;
3738     }
3739
3740     if((data->state.authproxy.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
3741        data->state.authproxy.done) {
3742       infof(data, "NTLM-proxy picked AND auth done set, clear picked!\n");
3743       data->state.authproxy.picked = CURLAUTH_NONE;
3744       data->state.authproxy.done = FALSE;
3745     }
3746 #endif
3747   }
3748
3749   /* Setup and init stuff before DO starts, in preparing for the transfer. */
3750   Curl_init_do(data, conn);
3751
3752   /*
3753    * Setup whatever necessary for a resumed transfer
3754    */
3755   result = setup_range(data);
3756   if(result)
3757     goto out;
3758
3759   /* Continue connectdata initialization here. */
3760
3761   /*
3762    * Inherit the proper values from the urldata struct AFTER we have arranged
3763    * the persistent connection stuff
3764    */
3765   conn->seek_func = data->set.seek_func;
3766   conn->seek_client = data->set.seek_client;
3767
3768   /*************************************************************
3769    * Resolve the address of the server or proxy
3770    *************************************************************/
3771   result = resolve_server(data, conn, async);
3772
3773   /* Strip trailing dots. resolve_server copied the name. */
3774   strip_trailing_dot(&conn->host);
3775   if(conn->bits.httpproxy)
3776     strip_trailing_dot(&conn->http_proxy.host);
3777   if(conn->bits.socksproxy)
3778     strip_trailing_dot(&conn->socks_proxy.host);
3779   if(conn->bits.conn_to_host)
3780     strip_trailing_dot(&conn->conn_to_host);
3781
3782 out:
3783   return result;
3784 }
3785
3786 /* Curl_setup_conn() is called after the name resolve initiated in
3787  * create_conn() is all done.
3788  *
3789  * Curl_setup_conn() also handles reused connections
3790  *
3791  * conn->data MUST already have been setup fine (in create_conn)
3792  */
3793
3794 CURLcode Curl_setup_conn(struct connectdata *conn,
3795                          bool *protocol_done)
3796 {
3797   CURLcode result = CURLE_OK;
3798   struct Curl_easy *data = conn->data;
3799
3800   Curl_pgrsTime(data, TIMER_NAMELOOKUP);
3801
3802   if(conn->handler->flags & PROTOPT_NONETWORK) {
3803     /* nothing to setup when not using a network */
3804     *protocol_done = TRUE;
3805     return result;
3806   }
3807   *protocol_done = FALSE; /* default to not done */
3808
3809   /* set proxy_connect_closed to false unconditionally already here since it
3810      is used strictly to provide extra information to a parent function in the
3811      case of proxy CONNECT failures and we must make sure we don't have it
3812      lingering set from a previous invoke */
3813   conn->bits.proxy_connect_closed = FALSE;
3814
3815   /*
3816    * Set user-agent. Used for HTTP, but since we can attempt to tunnel
3817    * basically anything through a http proxy we can't limit this based on
3818    * protocol.
3819    */
3820   if(data->set.str[STRING_USERAGENT]) {
3821     Curl_safefree(conn->allocptr.uagent);
3822     conn->allocptr.uagent =
3823       aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
3824     if(!conn->allocptr.uagent)
3825       return CURLE_OUT_OF_MEMORY;
3826   }
3827
3828   data->req.headerbytecount = 0;
3829
3830 #ifdef CURL_DO_LINEEND_CONV
3831   data->state.crlf_conversions = 0; /* reset CRLF conversion counter */
3832 #endif /* CURL_DO_LINEEND_CONV */
3833
3834   /* set start time here for timeout purposes in the connect procedure, it
3835      is later set again for the progress meter purpose */
3836   conn->now = Curl_now();
3837
3838   if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
3839     conn->bits.tcpconnect[FIRSTSOCKET] = FALSE;
3840     result = Curl_connecthost(conn, conn->dns_entry);
3841     if(result)
3842       return result;
3843   }
3844   else {
3845     Curl_pgrsTime(data, TIMER_CONNECT);    /* we're connected already */
3846     if(conn->ssl[FIRSTSOCKET].use ||
3847        (conn->handler->protocol & PROTO_FAMILY_SSH))
3848       Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
3849     conn->bits.tcpconnect[FIRSTSOCKET] = TRUE;
3850     *protocol_done = TRUE;
3851     Curl_updateconninfo(conn, conn->sock[FIRSTSOCKET]);
3852     Curl_verboseconnect(conn);
3853   }
3854
3855   conn->now = Curl_now(); /* time this *after* the connect is done, we set
3856                              this here perhaps a second time */
3857   return result;
3858 }
3859
3860 CURLcode Curl_connect(struct Curl_easy *data,
3861                       bool *asyncp,
3862                       bool *protocol_done)
3863 {
3864   CURLcode result;
3865   struct connectdata *conn;
3866
3867   *asyncp = FALSE; /* assume synchronous resolves by default */
3868
3869   /* init the single-transfer specific data */
3870   Curl_free_request_state(data);
3871   memset(&data->req, 0, sizeof(struct SingleRequest));
3872   data->req.maxdownload = -1;
3873
3874   /* call the stuff that needs to be called */
3875   result = create_conn(data, &conn, asyncp);
3876
3877   if(!result) {
3878     if(CONN_INUSE(conn))
3879       /* multiplexed */
3880       *protocol_done = TRUE;
3881     else if(!*asyncp) {
3882       /* DNS resolution is done: that's either because this is a reused
3883          connection, in which case DNS was unnecessary, or because DNS
3884          really did finish already (synch resolver/fast async resolve) */
3885       result = Curl_setup_conn(conn, protocol_done);
3886     }
3887   }
3888
3889   if(result == CURLE_NO_CONNECTION_AVAILABLE) {
3890     return result;
3891   }
3892   else if(result && conn) {
3893     /* We're not allowed to return failure with memory left allocated in the
3894        connectdata struct, free those here */
3895     Curl_disconnect(data, conn, TRUE);
3896   }
3897   else if(!result && !data->conn)
3898     /* FILE: transfers already have the connection attached */
3899     Curl_attach_connnection(data, conn);
3900
3901   return result;
3902 }
3903
3904 /*
3905  * Curl_init_do() inits the readwrite session. This is inited each time (in
3906  * the DO function before the protocol-specific DO functions are invoked) for
3907  * a transfer, sometimes multiple times on the same Curl_easy. Make sure
3908  * nothing in here depends on stuff that are setup dynamically for the
3909  * transfer.
3910  *
3911  * Allow this function to get called with 'conn' set to NULL.
3912  */
3913
3914 CURLcode Curl_init_do(struct Curl_easy *data, struct connectdata *conn)
3915 {
3916   struct SingleRequest *k = &data->req;
3917
3918   if(conn) {
3919     conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to
3920                                    use */
3921     /* if the protocol used doesn't support wildcards, switch it off */
3922     if(data->state.wildcardmatch &&
3923        !(conn->handler->flags & PROTOPT_WILDCARD))
3924       data->state.wildcardmatch = FALSE;
3925   }
3926
3927   data->state.done = FALSE; /* *_done() is not called yet */
3928   data->state.expect100header = FALSE;
3929
3930
3931   if(data->set.opt_no_body)
3932     /* in HTTP lingo, no body means using the HEAD request... */
3933     data->set.httpreq = HTTPREQ_HEAD;
3934   else if(HTTPREQ_HEAD == data->set.httpreq)
3935     /* ... but if unset there really is no perfect method that is the
3936        "opposite" of HEAD but in reality most people probably think GET
3937        then. The important thing is that we can't let it remain HEAD if the
3938        opt_no_body is set FALSE since then we'll behave wrong when getting
3939        HTTP. */
3940     data->set.httpreq = HTTPREQ_GET;
3941
3942   k->start = Curl_now(); /* start time */
3943   k->now = k->start;   /* current time is now */
3944   k->header = TRUE; /* assume header */
3945
3946   k->bytecount = 0;
3947
3948   k->buf = data->state.buffer;
3949   k->hbufp = data->state.headerbuff;
3950   k->ignorebody = FALSE;
3951
3952   Curl_speedinit(data);
3953
3954   Curl_pgrsSetUploadCounter(data, 0);
3955   Curl_pgrsSetDownloadCounter(data, 0);
3956
3957   return CURLE_OK;
3958 }
3959
3960 /*
3961 * get_protocol_family()
3962 *
3963 * This is used to return the protocol family for a given protocol.
3964 *
3965 * Parameters:
3966 *
3967 * protocol  [in]  - A single bit protocol identifier such as HTTP or HTTPS.
3968 *
3969 * Returns the family as a single bit protocol identifier.
3970 */
3971
3972 static unsigned int get_protocol_family(unsigned int protocol)
3973 {
3974   unsigned int family;
3975
3976   switch(protocol) {
3977   case CURLPROTO_HTTP:
3978   case CURLPROTO_HTTPS:
3979     family = CURLPROTO_HTTP;
3980     break;
3981
3982   case CURLPROTO_FTP:
3983   case CURLPROTO_FTPS:
3984     family = CURLPROTO_FTP;
3985     break;
3986
3987   case CURLPROTO_SCP:
3988     family = CURLPROTO_SCP;
3989     break;
3990
3991   case CURLPROTO_SFTP:
3992     family = CURLPROTO_SFTP;
3993     break;
3994
3995   case CURLPROTO_TELNET:
3996     family = CURLPROTO_TELNET;
3997     break;
3998
3999   case CURLPROTO_LDAP:
4000   case CURLPROTO_LDAPS:
4001     family = CURLPROTO_LDAP;
4002     break;
4003
4004   case CURLPROTO_DICT:
4005     family = CURLPROTO_DICT;
4006     break;
4007
4008   case CURLPROTO_FILE:
4009     family = CURLPROTO_FILE;
4010     break;
4011
4012   case CURLPROTO_TFTP:
4013     family = CURLPROTO_TFTP;
4014     break;
4015
4016   case CURLPROTO_IMAP:
4017   case CURLPROTO_IMAPS:
4018     family = CURLPROTO_IMAP;
4019     break;
4020
4021   case CURLPROTO_POP3:
4022   case CURLPROTO_POP3S:
4023     family = CURLPROTO_POP3;
4024     break;
4025
4026   case CURLPROTO_SMTP:
4027   case CURLPROTO_SMTPS:
4028       family = CURLPROTO_SMTP;
4029       break;
4030
4031   case CURLPROTO_RTSP:
4032     family = CURLPROTO_RTSP;
4033     break;
4034
4035   case CURLPROTO_RTMP:
4036   case CURLPROTO_RTMPS:
4037     family = CURLPROTO_RTMP;
4038     break;
4039
4040   case CURLPROTO_RTMPT:
4041   case CURLPROTO_RTMPTS:
4042     family = CURLPROTO_RTMPT;
4043     break;
4044
4045   case CURLPROTO_RTMPE:
4046     family = CURLPROTO_RTMPE;
4047     break;
4048
4049   case CURLPROTO_RTMPTE:
4050     family = CURLPROTO_RTMPTE;
4051     break;
4052
4053   case CURLPROTO_GOPHER:
4054     family = CURLPROTO_GOPHER;
4055     break;
4056
4057   case CURLPROTO_SMB:
4058   case CURLPROTO_SMBS:
4059     family = CURLPROTO_SMB;
4060     break;
4061
4062   default:
4063       family = 0;
4064       break;
4065   }
4066
4067   return family;
4068 }