2013-01-17 19:59:23 +08:00
|
|
|
#ifndef HEADER_CURL_MULTIHANDLE_H
|
|
|
|
#define HEADER_CURL_MULTIHANDLE_H
|
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2013-01-17 19:59:23 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2013-01-17 19:59:23 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2013-01-17 19:59:23 +08:00
|
|
|
***************************************************************************/
|
|
|
|
|
2020-12-11 16:05:47 +08:00
|
|
|
#include "llist.h"
|
|
|
|
#include "hash.h"
|
2015-05-12 15:15:02 +08:00
|
|
|
#include "conncache.h"
|
2018-05-29 02:29:15 +08:00
|
|
|
#include "psl.h"
|
2019-11-17 22:12:15 +08:00
|
|
|
#include "socketpair.h"
|
2015-05-12 15:15:02 +08:00
|
|
|
|
2020-12-11 16:05:47 +08:00
|
|
|
struct connectdata;
|
|
|
|
|
2013-01-17 19:59:23 +08:00
|
|
|
struct Curl_message {
|
2020-09-02 18:06:20 +08:00
|
|
|
struct Curl_llist_element list;
|
2013-01-17 19:59:23 +08:00
|
|
|
/* the 'CURLMsg' is the part that is visible to the external user */
|
|
|
|
struct CURLMsg extmsg;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* NOTE: if you add a state here, add the name to the statename[] array as
|
|
|
|
well!
|
|
|
|
*/
|
|
|
|
typedef enum {
|
2021-02-16 15:19:24 +08:00
|
|
|
MSTATE_INIT, /* 0 - start in this state */
|
|
|
|
MSTATE_PENDING, /* 1 - no connections, waiting for one */
|
2024-04-05 19:07:16 +08:00
|
|
|
MSTATE_SETUP, /* 2 - start a new transfer */
|
|
|
|
MSTATE_CONNECT, /* 3 - resolve/connect has been sent off */
|
|
|
|
MSTATE_RESOLVING, /* 4 - awaiting the resolve to finalize */
|
|
|
|
MSTATE_CONNECTING, /* 5 - awaiting the TCP connect to finalize */
|
|
|
|
MSTATE_TUNNELING, /* 6 - awaiting HTTPS proxy SSL initialization to
|
2021-02-16 15:19:24 +08:00
|
|
|
complete and/or proxy CONNECT to finalize */
|
2024-04-05 19:07:16 +08:00
|
|
|
MSTATE_PROTOCONNECT, /* 7 - initiate protocol connect procedure */
|
|
|
|
MSTATE_PROTOCONNECTING, /* 8 - completing the protocol-specific connect
|
2021-02-16 15:19:24 +08:00
|
|
|
phase */
|
2024-04-05 19:07:16 +08:00
|
|
|
MSTATE_DO, /* 9 - start send off the request (part 1) */
|
|
|
|
MSTATE_DOING, /* 10 - sending off the request (part 1) */
|
|
|
|
MSTATE_DOING_MORE, /* 11 - send off the request (part 2) */
|
|
|
|
MSTATE_DID, /* 12 - done sending off request */
|
|
|
|
MSTATE_PERFORMING, /* 13 - transfer data */
|
|
|
|
MSTATE_RATELIMITING, /* 14 - wait because limit-rate exceeded */
|
|
|
|
MSTATE_DONE, /* 15 - post data transfer operation */
|
|
|
|
MSTATE_COMPLETED, /* 16 - operation complete */
|
|
|
|
MSTATE_MSGSENT, /* 17 - the operation complete message is sent */
|
|
|
|
MSTATE_LAST /* 18 - not a true state, never use this */
|
2013-01-17 19:59:23 +08:00
|
|
|
} CURLMstate;
|
|
|
|
|
|
|
|
/* we support N sockets per easy handle. Set the corresponding bit to what
|
|
|
|
action we should wait for */
|
|
|
|
#define MAX_SOCKSPEREASYHANDLE 5
|
|
|
|
#define GETSOCK_READABLE (0x00ff)
|
|
|
|
#define GETSOCK_WRITABLE (0xff00)
|
|
|
|
|
2019-04-05 22:38:36 +08:00
|
|
|
#define CURLPIPE_ANY (CURLPIPE_MULTIPLEX)
|
2015-05-08 16:43:36 +08:00
|
|
|
|
2021-02-25 19:45:21 +08:00
|
|
|
#if !defined(CURL_DISABLE_SOCKETPAIR)
|
2019-11-17 22:12:15 +08:00
|
|
|
#define ENABLE_WAKEUP
|
|
|
|
#endif
|
|
|
|
|
2020-01-06 20:13:33 +08:00
|
|
|
/* value for MAXIMUM CONCURRENT STREAMS upper limit */
|
|
|
|
#define INITIAL_MAX_CONCURRENT_STREAMS ((1U << 31) - 1)
|
|
|
|
|
2013-01-17 19:59:23 +08:00
|
|
|
/* This is the struct known as CURLM on the outside */
|
|
|
|
struct Curl_multi {
|
|
|
|
/* First a simple identifier to easier detect if a user mix up
|
|
|
|
this multi handle with an easy handle. Set this to CURL_MULTI_HANDLE. */
|
2021-01-19 15:23:52 +08:00
|
|
|
unsigned int magic;
|
2013-01-17 19:59:23 +08:00
|
|
|
|
2020-07-28 07:24:54 +08:00
|
|
|
/* We have a doubly-linked list with easy handles */
|
2016-06-21 21:47:12 +08:00
|
|
|
struct Curl_easy *easyp;
|
|
|
|
struct Curl_easy *easylp; /* last node */
|
2013-01-17 19:59:23 +08:00
|
|
|
|
2023-11-22 01:18:27 +08:00
|
|
|
unsigned int num_easy; /* amount of entries in the linked list above. */
|
|
|
|
unsigned int num_alive; /* amount of easy handles that are added but have
|
|
|
|
not yet reached COMPLETE state */
|
2013-01-17 19:59:23 +08:00
|
|
|
|
2020-09-02 18:06:20 +08:00
|
|
|
struct Curl_llist msglist; /* a list of messages from completed transfers */
|
2013-01-17 19:59:23 +08:00
|
|
|
|
2020-09-02 18:06:20 +08:00
|
|
|
struct Curl_llist pending; /* Curl_easys that are in the
|
2021-02-16 15:19:24 +08:00
|
|
|
MSTATE_PENDING state */
|
2023-03-26 23:43:28 +08:00
|
|
|
struct Curl_llist msgsent; /* Curl_easys that are in the
|
|
|
|
MSTATE_MSGSENT state */
|
2014-09-02 15:29:50 +08:00
|
|
|
|
2013-01-17 19:59:23 +08:00
|
|
|
/* callback function and user data pointer for the *socket() API */
|
|
|
|
curl_socket_callback socket_cb;
|
|
|
|
void *socket_userp;
|
|
|
|
|
2015-06-01 17:45:52 +08:00
|
|
|
/* callback function and user data pointer for server push */
|
|
|
|
curl_push_callback push_cb;
|
|
|
|
void *push_userp;
|
|
|
|
|
2013-01-17 19:59:23 +08:00
|
|
|
/* Hostname cache */
|
2020-09-02 18:07:44 +08:00
|
|
|
struct Curl_hash hostcache;
|
2013-01-17 19:59:23 +08:00
|
|
|
|
2018-05-29 02:29:15 +08:00
|
|
|
#ifdef USE_LIBPSL
|
|
|
|
/* PSL cache. */
|
|
|
|
struct PslCache psl;
|
|
|
|
#endif
|
|
|
|
|
2013-01-17 19:59:23 +08:00
|
|
|
/* timetree points to the splay-tree of time nodes to figure out expire
|
|
|
|
times of all currently set timers */
|
|
|
|
struct Curl_tree *timetree;
|
|
|
|
|
2024-01-26 19:05:08 +08:00
|
|
|
/* buffer used for transfer data, lazy initialized */
|
|
|
|
char *xfer_buf; /* the actual buffer */
|
|
|
|
size_t xfer_buf_len; /* the allocated length */
|
2024-02-28 21:51:53 +08:00
|
|
|
/* buffer used for upload data, lazy initialized */
|
|
|
|
char *xfer_ulbuf; /* the actual buffer */
|
|
|
|
size_t xfer_ulbuf_len; /* the allocated length */
|
2024-01-26 19:05:08 +08:00
|
|
|
|
2013-01-17 19:59:23 +08:00
|
|
|
/* 'sockhash' is the lookup hash for socket descriptor => easy handles (note
|
|
|
|
the pluralis form, there can be more than one easy handle waiting on the
|
|
|
|
same actual socket) */
|
2020-09-02 18:07:44 +08:00
|
|
|
struct Curl_hash sockhash;
|
2024-04-11 18:34:40 +08:00
|
|
|
/* `proto_hash` is a general key-value store for protocol implementations
|
|
|
|
* with the lifetime of the multi handle. The number of elements kept here
|
|
|
|
* should be in the order of supported protocols (and sub-protocols like
|
|
|
|
* TLS), *not* in the order of connections or current transfers!
|
|
|
|
* Elements need to be added with their own destructor to be invoked when
|
|
|
|
* the multi handle is cleaned up (see Curl_hash_add2()).*/
|
|
|
|
struct Curl_hash proto_hash;
|
2013-01-17 19:59:23 +08:00
|
|
|
|
|
|
|
/* Shared connection cache (bundles)*/
|
2015-05-12 15:15:02 +08:00
|
|
|
struct conncache conn_cache;
|
2013-01-17 19:59:23 +08:00
|
|
|
|
2013-02-15 18:50:45 +08:00
|
|
|
long max_host_connections; /* if >0, a fixed limit of the maximum number
|
|
|
|
of connections per host */
|
|
|
|
|
|
|
|
long max_total_connections; /* if >0, a fixed limit of the maximum number
|
|
|
|
of connections in total */
|
lib: graceful connection shutdown
When libcurl discards a connection there are two phases this may go
through: "shutdown" and "closing". If a connection is aborted, the
shutdown phase is skipped and it is closed right away.
The connection filters attached to the connection implement the phases
in their `do_shutdown()` and `do_close()` callbacks. Filters carry now a
`shutdown` flags next to `connected` to keep track of the shutdown
operation.
Filters are shut down from top to bottom. If a filter is not connected,
its shutdown is skipped. Notable filters that *do* something during
shutdown are HTTP/2 and TLS. HTTP/2 sends the GOAWAY frame. TLS sends
its close notify and expects to receive a close notify from the server.
As sends and receives may EAGAIN on the network, a shutdown is often not
successful right away and needs to poll the connection's socket(s). To
facilitate this, such connections are placed on a new shutdown list
inside the connection cache.
Since managing this list requires the cooperation of a multi handle,
only the connection cache belonging to a multi handle is used. If a
connection was in another cache when being discarded, it is removed
there and added to the multi's cache. If no multi handle is available at
that time, the connection is shutdown and closed in a one-time,
best-effort attempt.
When a multi handle is destroyed, all connection still on the shutdown
list are discarded with a final shutdown attempt and close. In curl
debug builds, the environment variable `CURL_GRACEFUL_SHUTDOWN` can be
set to make this graceful with a timeout in milliseconds given by the
variable.
The shutdown list is limited to the max number of connections configured
for a multi cache. Set via CURLMOPT_MAX_TOTAL_CONNECTIONS. When the
limit is reached, the oldest connection on the shutdown list is
discarded.
- In multi_wait() and multi_waitfds(), collect all connection caches
involved (each transfer might carry its own) into a temporary list.
Let each connection cache on the list contribute sockets and
POLLIN/OUT events it's connections are waiting for.
- in multi_perform() collect the connection caches the same way and let
them peform their maintenance. This will make another non-blocking
attempt to shutdown all connections on its shutdown list.
- for event based multis (multi->socket_cb set), add the sockets and
their poll events via the callback. When `multi_socket()` is invoked
for a socket not known by an active transfer, forward this to the
multi's cache for processing. On closing a connection, remove its
socket(s) via the callback.
TLS connection filters MUST NOT send close nofity messages in their
`do_close()` implementation. The reason is that a TLS close notify
signals a success. When a connection is aborted and skips its shutdown
phase, the server needs to see a missing close notify to detect
something has gone wrong.
A graceful shutdown of FTP's data connection is performed implicitly
before regarding the upload/download as complete and continuing on the
control connection. For FTP without TLS, there is just the socket close
happening. But with TLS, the sent/received close notify signals that the
transfer is complete and healthy. Servers like `vsftpd` verify that and
reject uploads without a TLS close notify.
- added test_19_* for shutdown related tests
- test_19_01 and test_19_02 test for TCP RST packets
which happen without a graceful shutdown and should
no longer appear otherwise.
- add test_19_03 for handling shutdowns by the server
- add test_19_04 for handling shutdowns by curl
- add test_19_05 for event based shutdowny by server
- add test_30_06/07 and test_31_06/07 for shutdown checks
on FTP up- and downloads.
Closes #13976
2024-06-19 18:40:06 +08:00
|
|
|
long max_shutdown_connections; /* if >0, a fixed limit of the maximum number
|
|
|
|
of connections in shutdown handling */
|
2013-02-15 18:50:45 +08:00
|
|
|
|
2013-01-17 19:59:23 +08:00
|
|
|
/* timer callback and user data pointer for the *socket() API */
|
|
|
|
curl_multi_timer_callback timer_cb;
|
|
|
|
void *timer_userp;
|
2017-07-28 21:49:36 +08:00
|
|
|
struct curltime timer_lastcall; /* the fixed time for the timeout for the
|
2013-01-17 19:59:23 +08:00
|
|
|
previous callback */
|
2020-12-09 14:25:15 +08:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
WSAEVENT wsa_event; /* winsock event used for waits */
|
|
|
|
#else
|
2019-11-17 22:12:15 +08:00
|
|
|
#ifdef ENABLE_WAKEUP
|
2024-06-02 02:26:11 +08:00
|
|
|
curl_socket_t wakeup_pair[2]; /* eventfd()/pipe()/socketpair() used for
|
|
|
|
wakeup 0 is used for read, 1 is used
|
|
|
|
for write */
|
2020-12-09 14:25:15 +08:00
|
|
|
#endif
|
2019-11-17 22:12:15 +08:00
|
|
|
#endif
|
2023-11-22 01:18:27 +08:00
|
|
|
unsigned int max_concurrent_streams;
|
|
|
|
unsigned int maxconnects; /* if >0, a fixed limit of the maximum number of
|
2024-07-01 22:47:21 +08:00
|
|
|
entries we are allowed to grow the connection
|
2023-11-22 01:18:27 +08:00
|
|
|
cache to */
|
2022-09-21 14:52:57 +08:00
|
|
|
#define IPV6_UNKNOWN 0
|
|
|
|
#define IPV6_DEAD 1
|
|
|
|
#define IPV6_WORKS 2
|
|
|
|
unsigned char ipv6_up; /* IPV6_* defined */
|
2022-12-30 00:43:36 +08:00
|
|
|
BIT(multiplexing); /* multiplexing wanted */
|
|
|
|
BIT(recheckstate); /* see Curl_multi_connchanged */
|
|
|
|
BIT(in_callback); /* true while executing a callback */
|
2021-06-28 22:41:17 +08:00
|
|
|
#ifdef USE_OPENSSL
|
2022-12-30 00:43:36 +08:00
|
|
|
BIT(ssl_seeded);
|
2021-06-28 22:41:17 +08:00
|
|
|
#endif
|
2022-12-30 00:43:36 +08:00
|
|
|
BIT(dead); /* a callback returned error, everything needs to crash and
|
2021-12-02 20:57:38 +08:00
|
|
|
burn */
|
2024-01-26 19:05:08 +08:00
|
|
|
BIT(xfer_buf_borrowed); /* xfer_buf is currently being borrowed */
|
2024-04-10 17:51:08 +08:00
|
|
|
BIT(xfer_ulbuf_borrowed); /* xfer_ulbuf is currently being borrowed */
|
2023-01-11 21:15:26 +08:00
|
|
|
#ifdef DEBUGBUILD
|
|
|
|
BIT(warned); /* true after user warned of DEBUGBUILD */
|
|
|
|
#endif
|
2013-01-17 19:59:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* HEADER_CURL_MULTIHANDLE_H */
|