cfilters.h 28 KB

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