xref: /curl/lib/multihandle.h (revision 23fe1a52)
1 #ifndef HEADER_CURL_MULTIHANDLE_H
2 #define HEADER_CURL_MULTIHANDLE_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  * SPDX-License-Identifier: curl
24  *
25  ***************************************************************************/
26 
27 #include "llist.h"
28 #include "hash.h"
29 #include "conncache.h"
30 #include "psl.h"
31 #include "socketpair.h"
32 
33 struct connectdata;
34 
35 struct Curl_message {
36   struct Curl_llist_element list;
37   /* the 'CURLMsg' is the part that is visible to the external user */
38   struct CURLMsg extmsg;
39 };
40 
41 /* NOTE: if you add a state here, add the name to the statename[] array as
42    well!
43 */
44 typedef enum {
45   MSTATE_INIT,         /* 0 - start in this state */
46   MSTATE_PENDING,      /* 1 - no connections, waiting for one */
47   MSTATE_SETUP,        /* 2 - start a new transfer */
48   MSTATE_CONNECT,      /* 3 - resolve/connect has been sent off */
49   MSTATE_RESOLVING,    /* 4 - awaiting the resolve to finalize */
50   MSTATE_CONNECTING,   /* 5 - awaiting the TCP connect to finalize */
51   MSTATE_TUNNELING,    /* 6 - awaiting HTTPS proxy SSL initialization to
52                           complete and/or proxy CONNECT to finalize */
53   MSTATE_PROTOCONNECT, /* 7 - initiate protocol connect procedure */
54   MSTATE_PROTOCONNECTING, /* 8 - completing the protocol-specific connect
55                              phase */
56   MSTATE_DO,           /* 9 - start send off the request (part 1) */
57   MSTATE_DOING,        /* 10 - sending off the request (part 1) */
58   MSTATE_DOING_MORE,   /* 11 - send off the request (part 2) */
59   MSTATE_DID,          /* 12 - done sending off request */
60   MSTATE_PERFORMING,   /* 13 - transfer data */
61   MSTATE_RATELIMITING, /* 14 - wait because limit-rate exceeded */
62   MSTATE_DONE,         /* 15 - post data transfer operation */
63   MSTATE_COMPLETED,    /* 16 - operation complete */
64   MSTATE_MSGSENT,      /* 17 - the operation complete message is sent */
65   MSTATE_LAST          /* 18 - not a true state, never use this */
66 } CURLMstate;
67 
68 /* we support N sockets per easy handle. Set the corresponding bit to what
69    action we should wait for */
70 #define MAX_SOCKSPEREASYHANDLE 5
71 #define GETSOCK_READABLE (0x00ff)
72 #define GETSOCK_WRITABLE (0xff00)
73 
74 #define CURLPIPE_ANY (CURLPIPE_MULTIPLEX)
75 
76 #if !defined(CURL_DISABLE_SOCKETPAIR)
77 #define ENABLE_WAKEUP
78 #endif
79 
80 /* value for MAXIMUM CONCURRENT STREAMS upper limit */
81 #define INITIAL_MAX_CONCURRENT_STREAMS ((1U << 31) - 1)
82 
83 /* This is the struct known as CURLM on the outside */
84 struct Curl_multi {
85   /* First a simple identifier to easier detect if a user mix up
86      this multi handle with an easy handle. Set this to CURL_MULTI_HANDLE. */
87   unsigned int magic;
88 
89   /* We have a doubly-linked list with easy handles */
90   struct Curl_easy *easyp;
91   struct Curl_easy *easylp; /* last node */
92 
93   unsigned int num_easy; /* amount of entries in the linked list above. */
94   unsigned int num_alive; /* amount of easy handles that are added but have
95                              not yet reached COMPLETE state */
96 
97   struct Curl_llist msglist; /* a list of messages from completed transfers */
98 
99   struct Curl_llist pending; /* Curl_easys that are in the
100                                 MSTATE_PENDING state */
101   struct Curl_llist msgsent; /* Curl_easys that are in the
102                                 MSTATE_MSGSENT state */
103 
104   /* callback function and user data pointer for the *socket() API */
105   curl_socket_callback socket_cb;
106   void *socket_userp;
107 
108   /* callback function and user data pointer for server push */
109   curl_push_callback push_cb;
110   void *push_userp;
111 
112   /* Hostname cache */
113   struct Curl_hash hostcache;
114 
115 #ifdef USE_LIBPSL
116   /* PSL cache. */
117   struct PslCache psl;
118 #endif
119 
120   /* timetree points to the splay-tree of time nodes to figure out expire
121      times of all currently set timers */
122   struct Curl_tree *timetree;
123 
124   /* buffer used for transfer data, lazy initialized */
125   char *xfer_buf; /* the actual buffer */
126   size_t xfer_buf_len;      /* the allocated length */
127   /* buffer used for upload data, lazy initialized */
128   char *xfer_ulbuf; /* the actual buffer */
129   size_t xfer_ulbuf_len;      /* the allocated length */
130 
131   /* 'sockhash' is the lookup hash for socket descriptor => easy handles (note
132      the pluralis form, there can be more than one easy handle waiting on the
133      same actual socket) */
134   struct Curl_hash sockhash;
135   /* `proto_hash` is a general key-value store for protocol implementations
136    * with the lifetime of the multi handle. The number of elements kept here
137    * should be in the order of supported protocols (and sub-protocols like
138    * TLS), *not* in the order of connections or current transfers!
139    * Elements need to be added with their own destructor to be invoked when
140    * the multi handle is cleaned up (see Curl_hash_add2()).*/
141   struct Curl_hash proto_hash;
142 
143   /* Shared connection cache (bundles)*/
144   struct conncache conn_cache;
145 
146   long max_host_connections; /* if >0, a fixed limit of the maximum number
147                                 of connections per host */
148 
149   long max_total_connections; /* if >0, a fixed limit of the maximum number
150                                  of connections in total */
151 
152   /* timer callback and user data pointer for the *socket() API */
153   curl_multi_timer_callback timer_cb;
154   void *timer_userp;
155   struct curltime timer_lastcall; /* the fixed time for the timeout for the
156                                     previous callback */
157 #ifdef USE_WINSOCK
158   WSAEVENT wsa_event; /* winsock event used for waits */
159 #else
160 #ifdef ENABLE_WAKEUP
161   curl_socket_t wakeup_pair[2]; /* eventfd()/pipe()/socketpair() used for
162                                    wakeup 0 is used for read, 1 is used
163                                    for write */
164 #endif
165 #endif
166   unsigned int max_concurrent_streams;
167   unsigned int maxconnects; /* if >0, a fixed limit of the maximum number of
168                                entries we're allowed to grow the connection
169                                cache to */
170 #define IPV6_UNKNOWN 0
171 #define IPV6_DEAD    1
172 #define IPV6_WORKS   2
173   unsigned char ipv6_up;       /* IPV6_* defined */
174   BIT(multiplexing);           /* multiplexing wanted */
175   BIT(recheckstate);           /* see Curl_multi_connchanged */
176   BIT(in_callback);            /* true while executing a callback */
177 #ifdef USE_OPENSSL
178   BIT(ssl_seeded);
179 #endif
180   BIT(dead); /* a callback returned error, everything needs to crash and
181                 burn */
182   BIT(xfer_buf_borrowed);      /* xfer_buf is currently being borrowed */
183   BIT(xfer_ulbuf_borrowed);    /* xfer_ulbuf is currently being borrowed */
184 #ifdef DEBUGBUILD
185   BIT(warned);                 /* true after user warned of DEBUGBUILD */
186 #endif
187 };
188 
189 #endif /* HEADER_CURL_MULTIHANDLE_H */
190