Debian Codegraph @

deps/curl/lib/url.c

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