cfilters.h 27 KB

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