cfilters.h 27 KB

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