xref: /curl/lib/cfilters.h (revision c6655f70)
1 #ifndef HEADER_CURL_CFILTERS_H
2 #define HEADER_CURL_CFILTERS_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 
28 struct Curl_cfilter;
29 struct Curl_easy;
30 struct Curl_dns_entry;
31 struct connectdata;
32 
33 /* Callback to destroy resources held by this filter instance.
34  * Implementations MUST NOT chain calls to cf->next.
35  */
36 typedef void     Curl_cft_destroy_this(struct Curl_cfilter *cf,
37                                        struct Curl_easy *data);
38 
39 typedef void     Curl_cft_close(struct Curl_cfilter *cf,
40                                 struct Curl_easy *data);
41 
42 typedef CURLcode Curl_cft_connect(struct Curl_cfilter *cf,
43                                   struct Curl_easy *data,
44                                   bool blocking, bool *done);
45 
46 /* Return the hostname and port the connection goes to.
47  * This may change with the connection state of filters when tunneling
48  * is involved.
49  * @param cf     the filter to ask
50  * @param data   the easy handle currently active
51  * @param phost  on return, points to the relevant, real hostname.
52  *               this is owned by the connection.
53  * @param pdisplay_host  on return, points to the printable hostname.
54  *               this is owned by the connection.
55  * @param pport  on return, contains the port number
56  */
57 typedef void     Curl_cft_get_host(struct Curl_cfilter *cf,
58                                   struct Curl_easy *data,
59                                   const char **phost,
60                                   const char **pdisplay_host,
61                                   int *pport);
62 
63 struct easy_pollset;
64 
65 /* Passing in an easy_pollset for monitoring of sockets, let
66  * filters add or remove sockets actions (CURL_POLL_OUT, CURL_POLL_IN).
67  * This may add a socket or, in case no actions remain, remove
68  * a socket from the set.
69  *
70  * Filter implementations need to call filters "below" *after* they have
71  * made their adjustments. This allows lower filters to override "upper"
72  * actions. If a "lower" filter is unable to write, it needs to be able
73  * to disallow POLL_OUT.
74  *
75  * A filter without own restrictions/preferences should not modify
76  * the pollset. Filters, whose filter "below" is not connected, should
77  * also do no adjustments.
78  *
79  * Examples: a TLS handshake, while ongoing, might remove POLL_IN
80  * when it needs to write, or vice versa. A HTTP/2 filter might remove
81  * POLL_OUT when a stream window is exhausted and a WINDOW_UPDATE needs
82  * to be received first and add instead POLL_IN.
83  *
84  * @param cf     the filter to ask
85  * @param data   the easy handle the pollset is about
86  * @param ps     the pollset (inout) for the easy handle
87  */
88 typedef void     Curl_cft_adjust_pollset(struct Curl_cfilter *cf,
89                                           struct Curl_easy *data,
90                                           struct easy_pollset *ps);
91 
92 typedef bool     Curl_cft_data_pending(struct Curl_cfilter *cf,
93                                        const struct Curl_easy *data);
94 
95 typedef ssize_t  Curl_cft_send(struct Curl_cfilter *cf,
96                                struct Curl_easy *data, /* transfer */
97                                const void *buf,        /* data to write */
98                                size_t len,             /* amount to write */
99                                CURLcode *err);         /* error to return */
100 
101 typedef ssize_t  Curl_cft_recv(struct Curl_cfilter *cf,
102                                struct Curl_easy *data, /* transfer */
103                                char *buf,              /* store data here */
104                                size_t len,             /* amount to read */
105                                CURLcode *err);         /* error to return */
106 
107 typedef bool     Curl_cft_conn_is_alive(struct Curl_cfilter *cf,
108                                         struct Curl_easy *data,
109                                         bool *input_pending);
110 
111 typedef CURLcode Curl_cft_conn_keep_alive(struct Curl_cfilter *cf,
112                                           struct Curl_easy *data);
113 
114 /**
115  * Events/controls for connection filters, their arguments and
116  * return code handling. Filter callbacks are invoked "top down".
117  * Return code handling:
118  * "first fail" meaning that the first filter returning != CURLE_OK, will
119  *              abort further event distribution and determine the result.
120  * "ignored" meaning return values are ignored and the event is distributed
121  *           to all filters in the chain. Overall result is always CURLE_OK.
122  */
123 /*      data event                          arg1       arg2     return */
124 #define CF_CTRL_DATA_ATTACH           1  /* 0          NULL     ignored */
125 #define CF_CTRL_DATA_DETACH           2  /* 0          NULL     ignored */
126 #define CF_CTRL_DATA_SETUP            4  /* 0          NULL     first fail */
127 #define CF_CTRL_DATA_IDLE             5  /* 0          NULL     first fail */
128 #define CF_CTRL_DATA_PAUSE            6  /* on/off     NULL     first fail */
129 #define CF_CTRL_DATA_DONE             7  /* premature  NULL     ignored */
130 #define CF_CTRL_DATA_DONE_SEND        8  /* 0          NULL     ignored */
131 /* update conn info at connection and data */
132 #define CF_CTRL_CONN_INFO_UPDATE (256+0) /* 0          NULL     ignored */
133 #define CF_CTRL_FORGET_SOCKET    (256+1) /* 0          NULL     ignored */
134 
135 /**
136  * Handle event/control for the filter.
137  * Implementations MUST NOT chain calls to cf->next.
138  */
139 typedef CURLcode Curl_cft_cntrl(struct Curl_cfilter *cf,
140                                 struct Curl_easy *data,
141                                 int event, int arg1, void *arg2);
142 
143 
144 /**
145  * Queries to ask via a `Curl_cft_query *query` method on a cfilter chain.
146  * - MAX_CONCURRENT: the maximum number of parallel transfers the filter
147  *                   chain expects to handle at the same time.
148  *                   default: 1 if no filter overrides.
149  * - CONNECT_REPLY_MS: milliseconds until the first indication of a server
150  *                   response was received on a connect. For TCP, this
151  *                   reflects the time until the socket connected. On UDP
152  *                   this gives the time the first bytes from the server
153  *                   were received.
154  *                   -1 if not determined yet.
155  * - CF_QUERY_SOCKET: the socket used by the filter chain
156  */
157 /*      query                             res1       res2     */
158 #define CF_QUERY_MAX_CONCURRENT     1  /* number     -        */
159 #define CF_QUERY_CONNECT_REPLY_MS   2  /* number     -        */
160 #define CF_QUERY_SOCKET             3  /* -          curl_socket_t */
161 #define CF_QUERY_TIMER_CONNECT      4  /* -          struct curltime */
162 #define CF_QUERY_TIMER_APPCONNECT   5  /* -          struct curltime */
163 #define CF_QUERY_STREAM_ERROR       6  /* error code - */
164 
165 /**
166  * Query the cfilter for properties. Filters ignorant of a query will
167  * pass it "down" the filter chain.
168  */
169 typedef CURLcode Curl_cft_query(struct Curl_cfilter *cf,
170                                 struct Curl_easy *data,
171                                 int query, int *pres1, void *pres2);
172 
173 /**
174  * Type flags for connection filters. A filter can have none, one or
175  * many of those. Use to evaluate state/capabilities of a filter chain.
176  *
177  * CF_TYPE_IP_CONNECT: provides an IP connection or sth equivalent, like
178  *                     a CONNECT tunnel, a UNIX domain socket, a QUIC
179  *                     connection, etc.
180  * CF_TYPE_SSL:        provide SSL/TLS
181  * CF_TYPE_MULTIPLEX:  provides multiplexing of easy handles
182  * CF_TYPE_PROXY       provides proxying
183  */
184 #define CF_TYPE_IP_CONNECT  (1 << 0)
185 #define CF_TYPE_SSL         (1 << 1)
186 #define CF_TYPE_MULTIPLEX   (1 << 2)
187 #define CF_TYPE_PROXY       (1 << 3)
188 
189 /* A connection filter type, e.g. specific implementation. */
190 struct Curl_cftype {
191   const char *name;                       /* name of the filter type */
192   int flags;                              /* flags of filter type */
193   int log_level;                          /* log level for such filters */
194   Curl_cft_destroy_this *destroy;         /* destroy resources of this cf */
195   Curl_cft_connect *do_connect;           /* establish connection */
196   Curl_cft_close *do_close;               /* close conn */
197   Curl_cft_get_host *get_host;            /* host filter talks to */
198   Curl_cft_adjust_pollset *adjust_pollset; /* adjust transfer poll set */
199   Curl_cft_data_pending *has_data_pending;/* conn has data pending */
200   Curl_cft_send *do_send;                 /* send data */
201   Curl_cft_recv *do_recv;                 /* receive data */
202   Curl_cft_cntrl *cntrl;                  /* events/control */
203   Curl_cft_conn_is_alive *is_alive;       /* FALSE if conn is dead, Jim! */
204   Curl_cft_conn_keep_alive *keep_alive;   /* try to keep it alive */
205   Curl_cft_query *query;                  /* query filter chain */
206 };
207 
208 /* A connection filter instance, e.g. registered at a connection */
209 struct Curl_cfilter {
210   const struct Curl_cftype *cft; /* the type providing implementation */
211   struct Curl_cfilter *next;     /* next filter in chain */
212   void *ctx;                     /* filter type specific settings */
213   struct connectdata *conn;      /* the connection this filter belongs to */
214   int sockindex;                 /* the index the filter is installed at */
215   BIT(connected);                /* != 0 iff this filter is connected */
216 };
217 
218 /* Default implementations for the type functions, implementing nop. */
219 void Curl_cf_def_destroy_this(struct Curl_cfilter *cf,
220                               struct Curl_easy *data);
221 
222 /* Default implementations for the type functions, implementing pass-through
223  * the filter chain. */
224 void     Curl_cf_def_get_host(struct Curl_cfilter *cf, struct Curl_easy *data,
225                               const char **phost, const char **pdisplay_host,
226                               int *pport);
227 void     Curl_cf_def_adjust_pollset(struct Curl_cfilter *cf,
228                                      struct Curl_easy *data,
229                                      struct easy_pollset *ps);
230 bool     Curl_cf_def_data_pending(struct Curl_cfilter *cf,
231                                   const struct Curl_easy *data);
232 ssize_t  Curl_cf_def_send(struct Curl_cfilter *cf, struct Curl_easy *data,
233                           const void *buf, size_t len, CURLcode *err);
234 ssize_t  Curl_cf_def_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
235                           char *buf, size_t len, CURLcode *err);
236 CURLcode Curl_cf_def_cntrl(struct Curl_cfilter *cf,
237                                 struct Curl_easy *data,
238                                 int event, int arg1, void *arg2);
239 bool     Curl_cf_def_conn_is_alive(struct Curl_cfilter *cf,
240                                    struct Curl_easy *data,
241                                    bool *input_pending);
242 CURLcode Curl_cf_def_conn_keep_alive(struct Curl_cfilter *cf,
243                                      struct Curl_easy *data);
244 CURLcode Curl_cf_def_query(struct Curl_cfilter *cf,
245                            struct Curl_easy *data,
246                            int query, int *pres1, void *pres2);
247 
248 /**
249  * Create a new filter instance, unattached to the filter chain.
250  * Use Curl_conn_cf_add() to add it to the chain.
251  * @param pcf  on success holds the created instance
252  * @param cft   the filter type
253  * @param ctx  the type specific context to use
254  */
255 CURLcode Curl_cf_create(struct Curl_cfilter **pcf,
256                         const struct Curl_cftype *cft,
257                         void *ctx);
258 
259 /**
260  * Add a filter instance to the `sockindex` filter chain at connection
261  * `conn`. The filter must not already be attached. It is inserted at
262  * the start of the chain (top).
263  */
264 void Curl_conn_cf_add(struct Curl_easy *data,
265                       struct connectdata *conn,
266                       int sockindex,
267                       struct Curl_cfilter *cf);
268 
269 /**
270  * Insert a filter (chain) after `cf_at`.
271  * `cf_new` must not already be attached.
272  */
273 void Curl_conn_cf_insert_after(struct Curl_cfilter *cf_at,
274                                struct Curl_cfilter *cf_new);
275 
276 /**
277  * Discard, e.g. remove and destroy `discard` iff
278  * it still is in the filter chain below `cf`. If `discard`
279  * is no longer found beneath `cf` return FALSE.
280  * if `destroy_always` is TRUE, will call `discard`s destroy
281  * function and free it even if not found in the subchain.
282  */
283 bool Curl_conn_cf_discard_sub(struct Curl_cfilter *cf,
284                               struct Curl_cfilter *discard,
285                               struct Curl_easy *data,
286                               bool destroy_always);
287 
288 /**
289  * Discard all cfilters starting with `*pcf` and clearing it afterwards.
290  */
291 void Curl_conn_cf_discard_chain(struct Curl_cfilter **pcf,
292                                 struct Curl_easy *data);
293 
294 /**
295  * Remove and destroy all filters at chain `sockindex` on connection `conn`.
296  */
297 void Curl_conn_cf_discard_all(struct Curl_easy *data,
298                               struct connectdata *conn,
299                               int sockindex);
300 
301 
302 CURLcode Curl_conn_cf_connect(struct Curl_cfilter *cf,
303                               struct Curl_easy *data,
304                               bool blocking, bool *done);
305 void Curl_conn_cf_close(struct Curl_cfilter *cf, struct Curl_easy *data);
306 ssize_t Curl_conn_cf_send(struct Curl_cfilter *cf, struct Curl_easy *data,
307                           const void *buf, size_t len, CURLcode *err);
308 ssize_t Curl_conn_cf_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
309                           char *buf, size_t len, CURLcode *err);
310 CURLcode Curl_conn_cf_cntrl(struct Curl_cfilter *cf,
311                             struct Curl_easy *data,
312                             bool ignore_result,
313                             int event, int arg1, void *arg2);
314 
315 /**
316  * Determine if the connection filter chain is using SSL to the remote host
317  * (or will be once connected).
318  */
319 bool Curl_conn_cf_is_ssl(struct Curl_cfilter *cf);
320 
321 /**
322  * Get the socket used by the filter chain starting at `cf`.
323  * Returns CURL_SOCKET_BAD if not available.
324  */
325 curl_socket_t Curl_conn_cf_get_socket(struct Curl_cfilter *cf,
326                                       struct Curl_easy *data);
327 
328 
329 #define CURL_CF_SSL_DEFAULT  -1
330 #define CURL_CF_SSL_DISABLE  0
331 #define CURL_CF_SSL_ENABLE   1
332 
333 /**
334  * Bring the filter chain at `sockindex` for connection `data->conn` into
335  * connected state. Which will set `*done` to TRUE.
336  * This can be called on an already connected chain with no side effects.
337  * When not `blocking`, calls may return without error and `*done != TRUE`,
338  * while the individual filters negotiated the connection.
339  */
340 CURLcode Curl_conn_connect(struct Curl_easy *data, int sockindex,
341                            bool blocking, bool *done);
342 
343 /**
344  * Check if the filter chain at `sockindex` for connection `conn` is
345  * completely connected.
346  */
347 bool Curl_conn_is_connected(struct connectdata *conn, int sockindex);
348 
349 /**
350  * Determine if we have reached the remote host on IP level, e.g.
351  * have a TCP connection. This turns TRUE before a possible SSL
352  * handshake has been started/done.
353  */
354 bool Curl_conn_is_ip_connected(struct Curl_easy *data, int sockindex);
355 
356 /**
357  * Determine if the connection is using SSL to the remote host
358  * (or will be once connected). This will return FALSE, if SSL
359  * is only used in proxying and not for the tunnel itself.
360  */
361 bool Curl_conn_is_ssl(struct connectdata *conn, int sockindex);
362 
363 /**
364  * Connection provides multiplexing of easy handles at `socketindex`.
365  */
366 bool Curl_conn_is_multiplex(struct connectdata *conn, int sockindex);
367 
368 /**
369  * Close the filter chain at `sockindex` for connection `data->conn`.
370   * Filters remain in place and may be connected again afterwards.
371  */
372 void Curl_conn_close(struct Curl_easy *data, int sockindex);
373 
374 /**
375  * Return if data is pending in some connection filter at chain
376  * `sockindex` for connection `data->conn`.
377  */
378 bool Curl_conn_data_pending(struct Curl_easy *data,
379                             int sockindex);
380 
381 /**
382  * Return the socket used on data's connection for the index.
383  * Returns CURL_SOCKET_BAD if not available.
384  */
385 curl_socket_t Curl_conn_get_socket(struct Curl_easy *data, int sockindex);
386 
387 /**
388  * Tell filters to forget about the socket at sockindex.
389  */
390 void Curl_conn_forget_socket(struct Curl_easy *data, int sockindex);
391 
392 /**
393  * Adjust the pollset for the filter chain startgin at `cf`.
394  */
395 void Curl_conn_cf_adjust_pollset(struct Curl_cfilter *cf,
396                                  struct Curl_easy *data,
397                                  struct easy_pollset *ps);
398 
399 /**
400  * Adjust pollset from filters installed at transfer's connection.
401  */
402 void Curl_conn_adjust_pollset(struct Curl_easy *data,
403                                struct easy_pollset *ps);
404 
405 /**
406  * Receive data through the filter chain at `sockindex` for connection
407  * `data->conn`. Copy at most `len` bytes into `buf`. Return the
408  * actual number of bytes copied or a negative value on error.
409  * The error code is placed into `*code`.
410  */
411 ssize_t Curl_cf_recv(struct Curl_easy *data, int sockindex, char *buf,
412                      size_t len, CURLcode *code);
413 
414 /**
415  * Send `len` bytes of data from `buf` through the filter chain `sockindex`
416  * at connection `data->conn`. Return the actual number of bytes written
417  * or a negative value on error.
418  * The error code is placed into `*code`.
419  */
420 ssize_t Curl_cf_send(struct Curl_easy *data, int sockindex,
421                      const void *buf, size_t len, CURLcode *code);
422 
423 /**
424  * The easy handle `data` is being attached to `conn`. This does
425  * not mean that data will actually do a transfer. Attachment is
426  * also used for temporary actions on the connection.
427  */
428 void Curl_conn_ev_data_attach(struct connectdata *conn,
429                               struct Curl_easy *data);
430 
431 /**
432  * The easy handle `data` is being detached (no longer served)
433  * by connection `conn`. All filters are informed to release any resources
434  * related to `data`.
435  * Note: there may be several `data` attached to a connection at the same
436  * time.
437  */
438 void Curl_conn_ev_data_detach(struct connectdata *conn,
439                               struct Curl_easy *data);
440 
441 /**
442  * Notify connection filters that they need to setup data for
443  * a transfer.
444  */
445 CURLcode Curl_conn_ev_data_setup(struct Curl_easy *data);
446 
447 /**
448  * Notify connection filters that now would be a good time to
449  * perform any idle, e.g. time related, actions.
450  */
451 CURLcode Curl_conn_ev_data_idle(struct Curl_easy *data);
452 
453 /**
454  * Notify connection filters that the transfer represented by `data`
455  * is done with sending data (e.g. has uploaded everything).
456  */
457 void Curl_conn_ev_data_done_send(struct Curl_easy *data);
458 
459 /**
460  * Notify connection filters that the transfer represented by `data`
461  * is finished - eventually premature, e.g. before being complete.
462  */
463 void Curl_conn_ev_data_done(struct Curl_easy *data, bool premature);
464 
465 /**
466  * Notify connection filters that the transfer of data is paused/unpaused.
467  */
468 CURLcode Curl_conn_ev_data_pause(struct Curl_easy *data, bool do_pause);
469 
470 /**
471  * Inform connection filters to update their info in `conn`.
472  */
473 void Curl_conn_ev_update_info(struct Curl_easy *data,
474                               struct connectdata *conn);
475 
476 /**
477  * Check if FIRSTSOCKET's cfilter chain deems connection alive.
478  */
479 bool Curl_conn_is_alive(struct Curl_easy *data, struct connectdata *conn,
480                         bool *input_pending);
481 
482 /**
483  * Try to upkeep the connection filters at sockindex.
484  */
485 CURLcode Curl_conn_keep_alive(struct Curl_easy *data,
486                               struct connectdata *conn,
487                               int sockindex);
488 
489 void Curl_cf_def_close(struct Curl_cfilter *cf, struct Curl_easy *data);
490 void Curl_conn_get_host(struct Curl_easy *data, int sockindex,
491                         const char **phost, const char **pdisplay_host,
492                         int *pport);
493 
494 /**
495  * Get the maximum number of parallel transfers the connection
496  * expects to be able to handle at `sockindex`.
497  */
498 size_t Curl_conn_get_max_concurrent(struct Curl_easy *data,
499                                     struct connectdata *conn,
500                                     int sockindex);
501 
502 /**
503  * Get the underlying error code for a transfer stream or 0 if not known.
504  */
505 int Curl_conn_get_stream_error(struct Curl_easy *data,
506                                struct connectdata *conn,
507                                int sockindex);
508 
509 /**
510  * Get the index of the given socket in the connection's sockets.
511  * Useful in calling `Curl_conn_send()/Curl_conn_recv()` with the
512  * correct socket index.
513  */
514 int Curl_conn_sockindex(struct Curl_easy *data, curl_socket_t sockfd);
515 
516 /*
517  * Receive data on the connection, using FIRSTSOCKET/SECONDARYSOCKET.
518  * Will return CURLE_AGAIN iff blocked on receiving.
519  */
520 CURLcode Curl_conn_recv(struct Curl_easy *data, int sockindex,
521                         char *buf, size_t buffersize,
522                         ssize_t *pnread);
523 
524 /*
525  * Send data on the connection, using FIRSTSOCKET/SECONDARYSOCKET.
526  * Will return CURLE_AGAIN iff blocked on sending.
527  */
528 CURLcode Curl_conn_send(struct Curl_easy *data, int sockindex,
529                         const void *buf, size_t blen,
530                         size_t *pnwritten);
531 
532 
533 void Curl_pollset_reset(struct Curl_easy *data,
534                         struct easy_pollset *ps);
535 
536 /* Change the poll flags (CURL_POLL_IN/CURL_POLL_OUT) to the poll set for
537  * socket `sock`. If the socket is not already part of the poll set, it
538  * will be added.
539  * If the socket is present and all poll flags are cleared, it will be removed.
540  */
541 void Curl_pollset_change(struct Curl_easy *data,
542                          struct easy_pollset *ps, curl_socket_t sock,
543                          int add_flags, int remove_flags);
544 
545 void Curl_pollset_set(struct Curl_easy *data,
546                       struct easy_pollset *ps, curl_socket_t sock,
547                       bool do_in, bool do_out);
548 
549 #define Curl_pollset_add_in(data, ps, sock) \
550           Curl_pollset_change((data), (ps), (sock), CURL_POLL_IN, 0)
551 #define Curl_pollset_add_out(data, ps, sock) \
552           Curl_pollset_change((data), (ps), (sock), CURL_POLL_OUT, 0)
553 #define Curl_pollset_add_inout(data, ps, sock) \
554           Curl_pollset_change((data), (ps), (sock), \
555                                CURL_POLL_IN|CURL_POLL_OUT, 0)
556 #define Curl_pollset_set_in_only(data, ps, sock) \
557           Curl_pollset_change((data), (ps), (sock), \
558                                CURL_POLL_IN, CURL_POLL_OUT)
559 #define Curl_pollset_set_out_only(data, ps, sock) \
560           Curl_pollset_change((data), (ps), (sock), \
561                                CURL_POLL_OUT, CURL_POLL_IN)
562 
563 void Curl_pollset_add_socks(struct Curl_easy *data,
564                             struct easy_pollset *ps,
565                             int (*get_socks_cb)(struct Curl_easy *data,
566                                                 curl_socket_t *socks));
567 
568 /**
569  * Check if the pollset, as is, wants to read and/or write regarding
570  * the given socket.
571  */
572 void Curl_pollset_check(struct Curl_easy *data,
573                         struct easy_pollset *ps, curl_socket_t sock,
574                         bool *pwant_read, bool *pwant_write);
575 
576 /**
577  * Types and macros used to keep the current easy handle in filter calls,
578  * allowing for nested invocations. See #10336.
579  *
580  * `cf_call_data` is intended to be a member of the cfilter's `ctx` type.
581  * A filter defines the macro `CF_CTX_CALL_DATA` to give access to that.
582  *
583  * With all values 0, the default, this indicates that there is no cfilter
584  * call with `data` ongoing.
585  * Macro `CF_DATA_SAVE` preserves the current `cf_call_data` in a local
586  * variable and sets the `data` given, incrementing the `depth` counter.
587  *
588  * Macro `CF_DATA_RESTORE` restores the old values from the local variable,
589  * while checking that `depth` values are as expected (debug build), catching
590  * cases where a "lower" RESTORE was not called.
591  *
592  * Finally, macro `CF_DATA_CURRENT` gives the easy handle of the current
593  * invocation.
594  */
595 struct cf_call_data {
596   struct Curl_easy *data;
597 #ifdef DEBUGBUILD
598   int depth;
599 #endif
600 };
601 
602 /**
603  * define to access the `struct cf_call_data for a cfilter. Normally
604  * a member in the cfilter's `ctx`.
605  *
606  * #define CF_CTX_CALL_DATA(cf)   -> struct cf_call_data instance
607 */
608 
609 #ifdef DEBUGBUILD
610 
611 #define CF_DATA_SAVE(save, cf, data) \
612   do { \
613     (save) = CF_CTX_CALL_DATA(cf); \
614     DEBUGASSERT((save).data == NULL || (save).depth > 0); \
615     CF_CTX_CALL_DATA(cf).depth++;  \
616     CF_CTX_CALL_DATA(cf).data = (struct Curl_easy *)data; \
617   } while(0)
618 
619 #define CF_DATA_RESTORE(cf, save) \
620   do { \
621     DEBUGASSERT(CF_CTX_CALL_DATA(cf).depth == (save).depth + 1); \
622     DEBUGASSERT((save).data == NULL || (save).depth > 0); \
623     CF_CTX_CALL_DATA(cf) = (save); \
624   } while(0)
625 
626 #else /* DEBUGBUILD */
627 
628 #define CF_DATA_SAVE(save, cf, data) \
629   do { \
630     (save) = CF_CTX_CALL_DATA(cf); \
631     CF_CTX_CALL_DATA(cf).data = (struct Curl_easy *)data; \
632   } while(0)
633 
634 #define CF_DATA_RESTORE(cf, save) \
635   do { \
636     CF_CTX_CALL_DATA(cf) = (save); \
637   } while(0)
638 
639 #endif /* !DEBUGBUILD */
640 
641 #define CF_DATA_CURRENT(cf) \
642   ((cf)? (CF_CTX_CALL_DATA(cf).data) : NULL)
643 
644 #endif /* HEADER_CURL_CFILTERS_H */
645