cfilters.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. struct Curl_cfilter;
  27. struct Curl_easy;
  28. struct Curl_dns_entry;
  29. struct connectdata;
  30. /* Callback to destroy resources held by this filter instance.
  31. * Implementations MUST NOT chain calls to cf->next.
  32. */
  33. typedef void Curl_cft_destroy_this(struct Curl_cfilter *cf,
  34. struct Curl_easy *data);
  35. typedef void Curl_cft_close(struct Curl_cfilter *cf,
  36. struct Curl_easy *data);
  37. typedef CURLcode Curl_cft_connect(struct Curl_cfilter *cf,
  38. struct Curl_easy *data,
  39. bool blocking, bool *done);
  40. /* Return the hostname and port the connection goes to.
  41. * This may change with the connection state of filters when tunneling
  42. * is involved.
  43. * @param cf the filter to ask
  44. * @param data the easy handle currently active
  45. * @param phost on return, points to the relevant, real hostname.
  46. * this is owned by the connection.
  47. * @param pdisplay_host on return, points to the printable hostname.
  48. * this is owned by the connection.
  49. * @param pport on return, contains the port number
  50. */
  51. typedef void Curl_cft_get_host(struct Curl_cfilter *cf,
  52. struct Curl_easy *data,
  53. const char **phost,
  54. const char **pdisplay_host,
  55. int *pport);
  56. /* Filters may return sockets and fdset flags they are waiting for.
  57. * The passes array has room for up to MAX_SOCKSPEREASYHANDLE sockets.
  58. * @return read/write fdset for index in socks
  59. * or GETSOCK_BLANK when nothing to wait on
  60. */
  61. typedef int Curl_cft_get_select_socks(struct Curl_cfilter *cf,
  62. struct Curl_easy *data,
  63. curl_socket_t *socks);
  64. typedef bool Curl_cft_data_pending(struct Curl_cfilter *cf,
  65. const struct Curl_easy *data);
  66. typedef ssize_t Curl_cft_send(struct Curl_cfilter *cf,
  67. struct Curl_easy *data, /* transfer */
  68. const void *buf, /* data to write */
  69. size_t len, /* amount to write */
  70. CURLcode *err); /* error to return */
  71. typedef ssize_t Curl_cft_recv(struct Curl_cfilter *cf,
  72. struct Curl_easy *data, /* transfer */
  73. char *buf, /* store data here */
  74. size_t len, /* amount to read */
  75. CURLcode *err); /* error to return */
  76. typedef bool Curl_cft_conn_is_alive(struct Curl_cfilter *cf,
  77. struct Curl_easy *data);
  78. typedef CURLcode Curl_cft_conn_keep_alive(struct Curl_cfilter *cf,
  79. struct Curl_easy *data);
  80. /**
  81. * Events/controls for connection filters, their arguments and
  82. * return code handling. Filter callbacks are invoked "top down".
  83. * Return code handling:
  84. * "first fail" meaning that the first filter returning != CURLE_OK, will
  85. * abort further event distribution and determine the result.
  86. * "ignored" meaning return values are ignored and the event is distributed
  87. * to all filters in the chain. Overall result is always CURLE_OK.
  88. */
  89. /* data event arg1 arg2 return */
  90. #define CF_CTRL_DATA_ATTACH 1 /* 0 NULL ignored */
  91. #define CF_CTRL_DATA_DETACH 2 /* 0 NULL ignored */
  92. #define CF_CTRL_DATA_SETUP 4 /* 0 NULL first fail */
  93. #define CF_CTRL_DATA_IDLE 5 /* 0 NULL first fail */
  94. #define CF_CTRL_DATA_PAUSE 6 /* on/off NULL first fail */
  95. #define CF_CTRL_DATA_DONE 7 /* premature NULL ignored */
  96. #define CF_CTRL_DATA_DONE_SEND 8 /* 0 NULL ignored */
  97. /* update conn info at connection and data */
  98. #define CF_CTRL_CONN_INFO_UPDATE (256+0) /* 0 NULL ignored */
  99. /* report conn statistics (timers) for connection and data */
  100. #define CF_CTRL_CONN_REPORT_STATS (256+1) /* 0 NULL ignored */
  101. /**
  102. * Handle event/control for the filter.
  103. * Implementations MUST NOT chain calls to cf->next.
  104. */
  105. typedef CURLcode Curl_cft_cntrl(struct Curl_cfilter *cf,
  106. struct Curl_easy *data,
  107. int event, int arg1, void *arg2);
  108. /**
  109. * Queries to ask via a `Curl_cft_query *query` method on a cfilter chain.
  110. * - MAX_CONCURRENT: the maximum number of parallel transfers the filter
  111. * chain expects to handle at the same time.
  112. * default: 1 if no filter overrides.
  113. * - CONNECT_REPLY_MS: milliseconds until the first indication of a server
  114. * response was received on a connect. For TCP, this
  115. * reflects the time until the socket connected. On UDP
  116. * this gives the time the first bytes from the server
  117. * were received.
  118. * -1 if not determined yet.
  119. * - CF_QUERY_SOCKET: the socket used by the filter chain
  120. */
  121. /* query res1 res2 */
  122. #define CF_QUERY_MAX_CONCURRENT 1 /* number - */
  123. #define CF_QUERY_CONNECT_REPLY_MS 2 /* number - */
  124. #define CF_QUERY_SOCKET 3 /* - curl_socket_t */
  125. /**
  126. * Query the cfilter for properties. Filters ignorant of a query will
  127. * pass it "down" the filter chain.
  128. */
  129. typedef CURLcode Curl_cft_query(struct Curl_cfilter *cf,
  130. struct Curl_easy *data,
  131. int query, int *pres1, void *pres2);
  132. /**
  133. * Type flags for connection filters. A filter can have none, one or
  134. * many of those. Use to evaluate state/capabilities of a filter chain.
  135. *
  136. * CF_TYPE_IP_CONNECT: provides an IP connection or sth equivalent, like
  137. * a CONNECT tunnel, a UNIX domain socket, a QUIC
  138. * connection, etc.
  139. * CF_TYPE_SSL: provide SSL/TLS
  140. * CF_TYPE_MULTIPLEX: provides multiplexing of easy handles
  141. */
  142. #define CF_TYPE_IP_CONNECT (1 << 0)
  143. #define CF_TYPE_SSL (1 << 1)
  144. #define CF_TYPE_MULTIPLEX (1 << 2)
  145. /* A connection filter type, e.g. specific implementation. */
  146. struct Curl_cftype {
  147. const char *name; /* name of the filter type */
  148. int flags; /* flags of filter type */
  149. int log_level; /* log level for such filters */
  150. Curl_cft_destroy_this *destroy; /* destroy resources of this cf */
  151. Curl_cft_connect *connect; /* establish connection */
  152. Curl_cft_close *close; /* close conn */
  153. Curl_cft_get_host *get_host; /* host filter talks to */
  154. Curl_cft_get_select_socks *get_select_socks;/* sockets to select on */
  155. Curl_cft_data_pending *has_data_pending;/* conn has data pending */
  156. Curl_cft_send *do_send; /* send data */
  157. Curl_cft_recv *do_recv; /* receive data */
  158. Curl_cft_cntrl *cntrl; /* events/control */
  159. Curl_cft_conn_is_alive *is_alive; /* FALSE if conn is dead, Jim! */
  160. Curl_cft_conn_keep_alive *keep_alive; /* try to keep it alive */
  161. Curl_cft_query *query; /* query filter chain */
  162. };
  163. /* A connection filter instance, e.g. registered at a connection */
  164. struct Curl_cfilter {
  165. const struct Curl_cftype *cft; /* the type providing implementation */
  166. struct Curl_cfilter *next; /* next filter in chain */
  167. void *ctx; /* filter type specific settings */
  168. struct connectdata *conn; /* the connection this filter belongs to */
  169. int sockindex; /* the index the filter is installed at */
  170. BIT(connected); /* != 0 iff this filter is connected */
  171. };
  172. /* Default implementations for the type functions, implementing nop. */
  173. void Curl_cf_def_destroy_this(struct Curl_cfilter *cf,
  174. struct Curl_easy *data);
  175. /* Default implementations for the type functions, implementing pass-through
  176. * the filter chain. */
  177. void Curl_cf_def_close(struct Curl_cfilter *cf, struct Curl_easy *data);
  178. CURLcode Curl_cf_def_connect(struct Curl_cfilter *cf,
  179. struct Curl_easy *data,
  180. bool blocking, bool *done);
  181. void Curl_cf_def_get_host(struct Curl_cfilter *cf, struct Curl_easy *data,
  182. const char **phost, const char **pdisplay_host,
  183. int *pport);
  184. int Curl_cf_def_get_select_socks(struct Curl_cfilter *cf,
  185. struct Curl_easy *data,
  186. curl_socket_t *socks);
  187. bool Curl_cf_def_data_pending(struct Curl_cfilter *cf,
  188. const struct Curl_easy *data);
  189. ssize_t Curl_cf_def_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  190. const void *buf, size_t len, CURLcode *err);
  191. ssize_t Curl_cf_def_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  192. char *buf, size_t len, CURLcode *err);
  193. CURLcode Curl_cf_def_cntrl(struct Curl_cfilter *cf,
  194. struct Curl_easy *data,
  195. int event, int arg1, void *arg2);
  196. bool Curl_cf_def_conn_is_alive(struct Curl_cfilter *cf,
  197. struct Curl_easy *data);
  198. CURLcode Curl_cf_def_conn_keep_alive(struct Curl_cfilter *cf,
  199. struct Curl_easy *data);
  200. CURLcode Curl_cf_def_query(struct Curl_cfilter *cf,
  201. struct Curl_easy *data,
  202. int query, int *pres1, void *pres2);
  203. /**
  204. * Create a new filter instance, unattached to the filter chain.
  205. * Use Curl_conn_cf_add() to add it to the chain.
  206. * @param pcf on success holds the created instance
  207. * @parm cft the filter type
  208. * @param ctx the type specific context to use
  209. */
  210. CURLcode Curl_cf_create(struct Curl_cfilter **pcf,
  211. const struct Curl_cftype *cft,
  212. void *ctx);
  213. /**
  214. * Add a filter instance to the `sockindex` filter chain at connection
  215. * `conn`. The filter must not already be attached. It is inserted at
  216. * the start of the chain (top).
  217. */
  218. void Curl_conn_cf_add(struct Curl_easy *data,
  219. struct connectdata *conn,
  220. int sockindex,
  221. struct Curl_cfilter *cf);
  222. /**
  223. * Insert a filter (chain) after `cf_at`.
  224. * `cf_new` must not already be attached.
  225. */
  226. void Curl_conn_cf_insert_after(struct Curl_cfilter *cf_at,
  227. struct Curl_cfilter *cf_new);
  228. /**
  229. * Discard, e.g. remove and destroy a specific filter instance.
  230. * If the filter is attached to a connection, it will be removed before
  231. * it is destroyed.
  232. */
  233. void Curl_conn_cf_discard(struct Curl_cfilter *cf, struct Curl_easy *data);
  234. /**
  235. * Discard all cfilters starting with `*pcf` and clearing it afterwards.
  236. */
  237. void Curl_conn_cf_discard_chain(struct Curl_cfilter **pcf,
  238. struct Curl_easy *data);
  239. /**
  240. * Remove and destroy all filters at chain `sockindex` on connection `conn`.
  241. */
  242. void Curl_conn_cf_discard_all(struct Curl_easy *data,
  243. struct connectdata *conn,
  244. int sockindex);
  245. CURLcode Curl_conn_cf_connect(struct Curl_cfilter *cf,
  246. struct Curl_easy *data,
  247. bool blocking, bool *done);
  248. void Curl_conn_cf_close(struct Curl_cfilter *cf, struct Curl_easy *data);
  249. int Curl_conn_cf_get_select_socks(struct Curl_cfilter *cf,
  250. struct Curl_easy *data,
  251. curl_socket_t *socks);
  252. bool Curl_conn_cf_data_pending(struct Curl_cfilter *cf,
  253. const struct Curl_easy *data);
  254. ssize_t Curl_conn_cf_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  255. const void *buf, size_t len, CURLcode *err);
  256. ssize_t Curl_conn_cf_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  257. char *buf, size_t len, CURLcode *err);
  258. CURLcode Curl_conn_cf_cntrl(struct Curl_cfilter *cf,
  259. struct Curl_easy *data,
  260. bool ignore_result,
  261. int event, int arg1, void *arg2);
  262. /**
  263. * Get the socket used by the filter chain starting at `cf`.
  264. * Returns CURL_SOCKET_BAD if not available.
  265. */
  266. curl_socket_t Curl_conn_cf_get_socket(struct Curl_cfilter *cf,
  267. struct Curl_easy *data);
  268. #define CURL_CF_SSL_DEFAULT -1
  269. #define CURL_CF_SSL_DISABLE 0
  270. #define CURL_CF_SSL_ENABLE 1
  271. /**
  272. * Bring the filter chain at `sockindex` for connection `data->conn` into
  273. * connected state. Which will set `*done` to TRUE.
  274. * This can be called on an already connected chain with no side effects.
  275. * When not `blocking`, calls may return without error and `*done != TRUE`,
  276. * while the individual filters negotiated the connection.
  277. */
  278. CURLcode Curl_conn_connect(struct Curl_easy *data, int sockindex,
  279. bool blocking, bool *done);
  280. /**
  281. * Check if the filter chain at `sockindex` for connection `conn` is
  282. * completely connected.
  283. */
  284. bool Curl_conn_is_connected(struct connectdata *conn, int sockindex);
  285. /**
  286. * Determine if we have reached the remote host on IP level, e.g.
  287. * have a TCP connection. This turns TRUE before a possible SSL
  288. * handshake has been started/done.
  289. */
  290. bool Curl_conn_is_ip_connected(struct Curl_easy *data, int sockindex);
  291. /**
  292. * Determine if the connection is using SSL to the remote host
  293. * (or will be once connected). This will return FALSE, if SSL
  294. * is only used in proxying and not for the tunnel itself.
  295. */
  296. bool Curl_conn_is_ssl(struct connectdata *conn, int sockindex);
  297. /**
  298. * Connection provides multiplexing of easy handles at `socketindex`.
  299. */
  300. bool Curl_conn_is_multiplex(struct connectdata *conn, int sockindex);
  301. /**
  302. * Close the filter chain at `sockindex` for connection `data->conn`.
  303. * Filters remain in place and may be connected again afterwards.
  304. */
  305. void Curl_conn_close(struct Curl_easy *data, int sockindex);
  306. /**
  307. * Return if data is pending in some connection filter at chain
  308. * `sockindex` for connection `data->conn`.
  309. */
  310. bool Curl_conn_data_pending(struct Curl_easy *data,
  311. int sockindex);
  312. /**
  313. * Return the socket used on data's connection for the index.
  314. * Returns CURL_SOCKET_BAD if not available.
  315. */
  316. curl_socket_t Curl_conn_get_socket(struct Curl_easy *data, int sockindex);
  317. /**
  318. * Get any select fd flags and the socket filters at chain `sockindex`
  319. * at connection `conn` might be waiting for.
  320. */
  321. int Curl_conn_get_select_socks(struct Curl_easy *data, int sockindex,
  322. curl_socket_t *socks);
  323. /**
  324. * Receive data through the filter chain at `sockindex` for connection
  325. * `data->conn`. Copy at most `len` bytes into `buf`. Return the
  326. * actuel number of bytes copied or a negative value on error.
  327. * The error code is placed into `*code`.
  328. */
  329. ssize_t Curl_conn_recv(struct Curl_easy *data, int sockindex, char *buf,
  330. size_t len, CURLcode *code);
  331. /**
  332. * Send `len` bytes of data from `buf` through the filter chain `sockindex`
  333. * at connection `data->conn`. Return the actual number of bytes written
  334. * or a negative value on error.
  335. * The error code is placed into `*code`.
  336. */
  337. ssize_t Curl_conn_send(struct Curl_easy *data, int sockindex,
  338. const void *buf, size_t len, CURLcode *code);
  339. /**
  340. * The easy handle `data` is being attached to `conn`. This does
  341. * not mean that data will actually do a transfer. Attachment is
  342. * also used for temporary actions on the connection.
  343. */
  344. void Curl_conn_ev_data_attach(struct connectdata *conn,
  345. struct Curl_easy *data);
  346. /**
  347. * The easy handle `data` is being detached (no longer served)
  348. * by connection `conn`. All filters are informed to release any resources
  349. * related to `data`.
  350. * Note: there may be several `data` attached to a connection at the same
  351. * time.
  352. */
  353. void Curl_conn_ev_data_detach(struct connectdata *conn,
  354. struct Curl_easy *data);
  355. /**
  356. * Notify connection filters that they need to setup data for
  357. * a transfer.
  358. */
  359. CURLcode Curl_conn_ev_data_setup(struct Curl_easy *data);
  360. /**
  361. * Notify connection filters that now would be a good time to
  362. * perform any idle, e.g. time related, actions.
  363. */
  364. CURLcode Curl_conn_ev_data_idle(struct Curl_easy *data);
  365. /**
  366. * Notify connection filters that the transfer represented by `data`
  367. * is donw with sending data (e.g. has uploaded everything).
  368. */
  369. void Curl_conn_ev_data_done_send(struct Curl_easy *data);
  370. /**
  371. * Notify connection filters that the transfer represented by `data`
  372. * is finished - eventually premature, e.g. before being complete.
  373. */
  374. void Curl_conn_ev_data_done(struct Curl_easy *data, bool premature);
  375. /**
  376. * Notify connection filters that the transfer of data is paused/unpaused.
  377. */
  378. CURLcode Curl_conn_ev_data_pause(struct Curl_easy *data, bool do_pause);
  379. /**
  380. * Inform connection filters to update their info in `conn`.
  381. */
  382. void Curl_conn_ev_update_info(struct Curl_easy *data,
  383. struct connectdata *conn);
  384. /**
  385. * Inform connection filters to report statistics.
  386. */
  387. void Curl_conn_ev_report_stats(struct Curl_easy *data,
  388. struct connectdata *conn);
  389. /**
  390. * Check if FIRSTSOCKET's cfilter chain deems connection alive.
  391. */
  392. bool Curl_conn_is_alive(struct Curl_easy *data, struct connectdata *conn);
  393. /**
  394. * Try to upkeep the connection filters at sockindex.
  395. */
  396. CURLcode Curl_conn_keep_alive(struct Curl_easy *data,
  397. struct connectdata *conn,
  398. int sockindex);
  399. void Curl_conn_get_host(struct Curl_easy *data, int sockindex,
  400. const char **phost, const char **pdisplay_host,
  401. int *pport);
  402. /**
  403. * Get the maximum number of parallel transfers the connection
  404. * expects to be able to handle at `sockindex`.
  405. */
  406. size_t Curl_conn_get_max_concurrent(struct Curl_easy *data,
  407. struct connectdata *conn,
  408. int sockindex);
  409. /**
  410. * Types and macros used to keep the current easy handle in filter calls,
  411. * allowing for nested invocations. See #10336.
  412. *
  413. * `cf_call_data` is intended to be a member of the cfilter's `ctx` type.
  414. * A filter defines the macro `CF_CTX_CALL_DATA` to give access to that.
  415. *
  416. * With all values 0, the default, this indicates that there is no cfilter
  417. * call with `data` ongoing.
  418. * Macro `CF_DATA_SAVE` preserves the current `cf_call_data` in a local
  419. * variable and sets the `data` given, incrementing the `depth` counter.
  420. *
  421. * Macro `CF_DATA_RESTORE` restores the old values from the local variable,
  422. * while checking that `depth` values are as expected (debug build), catching
  423. * cases where a "lower" RESTORE was not called.
  424. *
  425. * Finally, macro `CF_DATA_CURRENT` gives the easy handle of the current
  426. * invocation.
  427. */
  428. struct cf_call_data {
  429. struct Curl_easy *data;
  430. #ifdef DEBUGBUILD
  431. int depth;
  432. #endif
  433. };
  434. /**
  435. * define to access the `struct cf_call_data for a cfilter. Normally
  436. * a member in the cfilter's `ctx`.
  437. *
  438. * #define CF_CTX_CALL_DATA(cf) -> struct cf_call_data instance
  439. */
  440. #ifdef DEBUGBUILD
  441. #define CF_DATA_SAVE(save, cf, data) \
  442. do { \
  443. (save) = CF_CTX_CALL_DATA(cf); \
  444. DEBUGASSERT((save).data == NULL || (save).depth > 0); \
  445. CF_CTX_CALL_DATA(cf).depth++; \
  446. CF_CTX_CALL_DATA(cf).data = (struct Curl_easy *)data; \
  447. } while(0)
  448. #define CF_DATA_RESTORE(cf, save) \
  449. do { \
  450. DEBUGASSERT(CF_CTX_CALL_DATA(cf).depth == (save).depth + 1); \
  451. DEBUGASSERT((save).data == NULL || (save).depth > 0); \
  452. CF_CTX_CALL_DATA(cf) = (save); \
  453. } while(0)
  454. #else /* DEBUGBUILD */
  455. #define CF_DATA_SAVE(save, cf, data) \
  456. do { \
  457. (save) = CF_CTX_CALL_DATA(cf); \
  458. CF_CTX_CALL_DATA(cf).data = (struct Curl_easy *)data; \
  459. } while(0)
  460. #define CF_DATA_RESTORE(cf, save) \
  461. do { \
  462. CF_CTX_CALL_DATA(cf) = (save); \
  463. } while(0)
  464. #endif /* !DEBUGBUILD */
  465. #define CF_DATA_CURRENT(cf) \
  466. ((cf)? (CF_CTX_CALL_DATA(cf).data) : NULL)
  467. #endif /* HEADER_CURL_CFILTERS_H */