multi.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #ifndef CURLINC_MULTI_H
  2. #define CURLINC_MULTI_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. /*
  27. This is an "external" header file. Don't give away any internals here!
  28. GOALS
  29. o Enable a "pull" interface. The application that uses libcurl decides where
  30. and when to ask libcurl to get/send data.
  31. o Enable multiple simultaneous transfers in the same thread without making it
  32. complicated for the application.
  33. o Enable the application to select() on its own file descriptors and curl's
  34. file descriptors simultaneous easily.
  35. */
  36. /*
  37. * This header file should not really need to include "curl.h" since curl.h
  38. * itself includes this file and we expect user applications to do #include
  39. * <curl/curl.h> without the need for especially including multi.h.
  40. *
  41. * For some reason we added this include here at one point, and rather than to
  42. * break existing (wrongly written) libcurl applications, we leave it as-is
  43. * but with this warning attached.
  44. */
  45. #include "curl.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. #if defined(BUILDING_LIBCURL) || defined(CURL_STRICTER)
  50. typedef struct Curl_multi CURLM;
  51. #else
  52. typedef void CURLM;
  53. #endif
  54. typedef enum {
  55. CURLM_CALL_MULTI_PERFORM = -1, /* please call curl_multi_perform() or
  56. curl_multi_socket*() soon */
  57. CURLM_OK,
  58. CURLM_BAD_HANDLE, /* the passed-in handle is not a valid CURLM handle */
  59. CURLM_BAD_EASY_HANDLE, /* an easy handle was not good/valid */
  60. CURLM_OUT_OF_MEMORY, /* if you ever get this, you're in deep sh*t */
  61. CURLM_INTERNAL_ERROR, /* this is a libcurl bug */
  62. CURLM_BAD_SOCKET, /* the passed in socket argument did not match */
  63. CURLM_UNKNOWN_OPTION, /* curl_multi_setopt() with unsupported option */
  64. CURLM_ADDED_ALREADY, /* an easy handle already added to a multi handle was
  65. attempted to get added - again */
  66. CURLM_RECURSIVE_API_CALL, /* an api function was called from inside a
  67. callback */
  68. CURLM_WAKEUP_FAILURE, /* wakeup is unavailable or failed */
  69. CURLM_BAD_FUNCTION_ARGUMENT, /* function called with a bad parameter */
  70. CURLM_ABORTED_BY_CALLBACK,
  71. CURLM_UNRECOVERABLE_POLL,
  72. CURLM_LAST
  73. } CURLMcode;
  74. /* just to make code nicer when using curl_multi_socket() you can now check
  75. for CURLM_CALL_MULTI_SOCKET too in the same style it works for
  76. curl_multi_perform() and CURLM_CALL_MULTI_PERFORM */
  77. #define CURLM_CALL_MULTI_SOCKET CURLM_CALL_MULTI_PERFORM
  78. /* bitmask bits for CURLMOPT_PIPELINING */
  79. #define CURLPIPE_NOTHING 0L
  80. #define CURLPIPE_HTTP1 1L
  81. #define CURLPIPE_MULTIPLEX 2L
  82. typedef enum {
  83. CURLMSG_NONE, /* first, not used */
  84. CURLMSG_DONE, /* This easy handle has completed. 'result' contains
  85. the CURLcode of the transfer */
  86. CURLMSG_LAST /* last, not used */
  87. } CURLMSG;
  88. struct CURLMsg {
  89. CURLMSG msg; /* what this message means */
  90. CURL *easy_handle; /* the handle it concerns */
  91. union {
  92. void *whatever; /* message-specific data */
  93. CURLcode result; /* return code for transfer */
  94. } data;
  95. };
  96. typedef struct CURLMsg CURLMsg;
  97. /* Based on poll(2) structure and values.
  98. * We don't use pollfd and POLL* constants explicitly
  99. * to cover platforms without poll(). */
  100. #define CURL_WAIT_POLLIN 0x0001
  101. #define CURL_WAIT_POLLPRI 0x0002
  102. #define CURL_WAIT_POLLOUT 0x0004
  103. struct curl_waitfd {
  104. curl_socket_t fd;
  105. short events;
  106. short revents;
  107. };
  108. /*
  109. * Name: curl_multi_init()
  110. *
  111. * Desc: initialize multi-style curl usage
  112. *
  113. * Returns: a new CURLM handle to use in all 'curl_multi' functions.
  114. */
  115. CURL_EXTERN CURLM *curl_multi_init(void);
  116. /*
  117. * Name: curl_multi_add_handle()
  118. *
  119. * Desc: add a standard curl handle to the multi stack
  120. *
  121. * Returns: CURLMcode type, general multi error code.
  122. */
  123. CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle,
  124. CURL *curl_handle);
  125. /*
  126. * Name: curl_multi_remove_handle()
  127. *
  128. * Desc: removes a curl handle from the multi stack again
  129. *
  130. * Returns: CURLMcode type, general multi error code.
  131. */
  132. CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
  133. CURL *curl_handle);
  134. /*
  135. * Name: curl_multi_fdset()
  136. *
  137. * Desc: Ask curl for its fd_set sets. The app can use these to select() or
  138. * poll() on. We want curl_multi_perform() called as soon as one of
  139. * them are ready.
  140. *
  141. * Returns: CURLMcode type, general multi error code.
  142. */
  143. CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,
  144. fd_set *read_fd_set,
  145. fd_set *write_fd_set,
  146. fd_set *exc_fd_set,
  147. int *max_fd);
  148. /*
  149. * Name: curl_multi_wait()
  150. *
  151. * Desc: Poll on all fds within a CURLM set as well as any
  152. * additional fds passed to the function.
  153. *
  154. * Returns: CURLMcode type, general multi error code.
  155. */
  156. CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle,
  157. struct curl_waitfd extra_fds[],
  158. unsigned int extra_nfds,
  159. int timeout_ms,
  160. int *ret);
  161. /*
  162. * Name: curl_multi_poll()
  163. *
  164. * Desc: Poll on all fds within a CURLM set as well as any
  165. * additional fds passed to the function.
  166. *
  167. * Returns: CURLMcode type, general multi error code.
  168. */
  169. CURL_EXTERN CURLMcode curl_multi_poll(CURLM *multi_handle,
  170. struct curl_waitfd extra_fds[],
  171. unsigned int extra_nfds,
  172. int timeout_ms,
  173. int *ret);
  174. /*
  175. * Name: curl_multi_wakeup()
  176. *
  177. * Desc: wakes up a sleeping curl_multi_poll call.
  178. *
  179. * Returns: CURLMcode type, general multi error code.
  180. */
  181. CURL_EXTERN CURLMcode curl_multi_wakeup(CURLM *multi_handle);
  182. /*
  183. * Name: curl_multi_perform()
  184. *
  185. * Desc: When the app thinks there's data available for curl it calls this
  186. * function to read/write whatever there is right now. This returns
  187. * as soon as the reads and writes are done. This function does not
  188. * require that there actually is data available for reading or that
  189. * data can be written, it can be called just in case. It returns
  190. * the number of handles that still transfer data in the second
  191. * argument's integer-pointer.
  192. *
  193. * Returns: CURLMcode type, general multi error code. *NOTE* that this only
  194. * returns errors etc regarding the whole multi stack. There might
  195. * still have occurred problems on individual transfers even when
  196. * this returns OK.
  197. */
  198. CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle,
  199. int *running_handles);
  200. /*
  201. * Name: curl_multi_cleanup()
  202. *
  203. * Desc: Cleans up and removes a whole multi stack. It does not free or
  204. * touch any individual easy handles in any way. We need to define
  205. * in what state those handles will be if this function is called
  206. * in the middle of a transfer.
  207. *
  208. * Returns: CURLMcode type, general multi error code.
  209. */
  210. CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle);
  211. /*
  212. * Name: curl_multi_info_read()
  213. *
  214. * Desc: Ask the multi handle if there's any messages/informationals from
  215. * the individual transfers. Messages include informationals such as
  216. * error code from the transfer or just the fact that a transfer is
  217. * completed. More details on these should be written down as well.
  218. *
  219. * Repeated calls to this function will return a new struct each
  220. * time, until a special "end of msgs" struct is returned as a signal
  221. * that there is no more to get at this point.
  222. *
  223. * The data the returned pointer points to will not survive calling
  224. * curl_multi_cleanup().
  225. *
  226. * The 'CURLMsg' struct is meant to be very simple and only contain
  227. * very basic information. If more involved information is wanted,
  228. * we will provide the particular "transfer handle" in that struct
  229. * and that should/could/would be used in subsequent
  230. * curl_easy_getinfo() calls (or similar). The point being that we
  231. * must never expose complex structs to applications, as then we'll
  232. * undoubtably get backwards compatibility problems in the future.
  233. *
  234. * Returns: A pointer to a filled-in struct, or NULL if it failed or ran out
  235. * of structs. It also writes the number of messages left in the
  236. * queue (after this read) in the integer the second argument points
  237. * to.
  238. */
  239. CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle,
  240. int *msgs_in_queue);
  241. /*
  242. * Name: curl_multi_strerror()
  243. *
  244. * Desc: The curl_multi_strerror function may be used to turn a CURLMcode
  245. * value into the equivalent human readable error string. This is
  246. * useful for printing meaningful error messages.
  247. *
  248. * Returns: A pointer to a null-terminated error message.
  249. */
  250. CURL_EXTERN const char *curl_multi_strerror(CURLMcode);
  251. /*
  252. * Name: curl_multi_socket() and
  253. * curl_multi_socket_all()
  254. *
  255. * Desc: An alternative version of curl_multi_perform() that allows the
  256. * application to pass in one of the file descriptors that have been
  257. * detected to have "action" on them and let libcurl perform.
  258. * See man page for details.
  259. */
  260. #define CURL_POLL_NONE 0
  261. #define CURL_POLL_IN 1
  262. #define CURL_POLL_OUT 2
  263. #define CURL_POLL_INOUT 3
  264. #define CURL_POLL_REMOVE 4
  265. #define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD
  266. #define CURL_CSELECT_IN 0x01
  267. #define CURL_CSELECT_OUT 0x02
  268. #define CURL_CSELECT_ERR 0x04
  269. typedef int (*curl_socket_callback)(CURL *easy, /* easy handle */
  270. curl_socket_t s, /* socket */
  271. int what, /* see above */
  272. void *userp, /* private callback
  273. pointer */
  274. void *socketp); /* private socket
  275. pointer */
  276. /*
  277. * Name: curl_multi_timer_callback
  278. *
  279. * Desc: Called by libcurl whenever the library detects a change in the
  280. * maximum number of milliseconds the app is allowed to wait before
  281. * curl_multi_socket() or curl_multi_perform() must be called
  282. * (to allow libcurl's timed events to take place).
  283. *
  284. * Returns: The callback should return zero.
  285. */
  286. typedef int (*curl_multi_timer_callback)(CURLM *multi, /* multi handle */
  287. long timeout_ms, /* see above */
  288. void *userp); /* private callback
  289. pointer */
  290. CURL_EXTERN CURLMcode CURL_DEPRECATED(7.19.5, "Use curl_multi_socket_action()")
  291. curl_multi_socket(CURLM *multi_handle, curl_socket_t s, int *running_handles);
  292. CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle,
  293. curl_socket_t s,
  294. int ev_bitmask,
  295. int *running_handles);
  296. CURL_EXTERN CURLMcode CURL_DEPRECATED(7.19.5, "Use curl_multi_socket_action()")
  297. curl_multi_socket_all(CURLM *multi_handle, int *running_handles);
  298. #ifndef CURL_ALLOW_OLD_MULTI_SOCKET
  299. /* This macro below was added in 7.16.3 to push users who recompile to use
  300. the new curl_multi_socket_action() instead of the old curl_multi_socket()
  301. */
  302. #define curl_multi_socket(x,y,z) curl_multi_socket_action(x,y,0,z)
  303. #endif
  304. /*
  305. * Name: curl_multi_timeout()
  306. *
  307. * Desc: Returns the maximum number of milliseconds the app is allowed to
  308. * wait before curl_multi_socket() or curl_multi_perform() must be
  309. * called (to allow libcurl's timed events to take place).
  310. *
  311. * Returns: CURLM error code.
  312. */
  313. CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle,
  314. long *milliseconds);
  315. typedef enum {
  316. /* This is the socket callback function pointer */
  317. CURLOPT(CURLMOPT_SOCKETFUNCTION, CURLOPTTYPE_FUNCTIONPOINT, 1),
  318. /* This is the argument passed to the socket callback */
  319. CURLOPT(CURLMOPT_SOCKETDATA, CURLOPTTYPE_OBJECTPOINT, 2),
  320. /* set to 1 to enable pipelining for this multi handle */
  321. CURLOPT(CURLMOPT_PIPELINING, CURLOPTTYPE_LONG, 3),
  322. /* This is the timer callback function pointer */
  323. CURLOPT(CURLMOPT_TIMERFUNCTION, CURLOPTTYPE_FUNCTIONPOINT, 4),
  324. /* This is the argument passed to the timer callback */
  325. CURLOPT(CURLMOPT_TIMERDATA, CURLOPTTYPE_OBJECTPOINT, 5),
  326. /* maximum number of entries in the connection cache */
  327. CURLOPT(CURLMOPT_MAXCONNECTS, CURLOPTTYPE_LONG, 6),
  328. /* maximum number of (pipelining) connections to one host */
  329. CURLOPT(CURLMOPT_MAX_HOST_CONNECTIONS, CURLOPTTYPE_LONG, 7),
  330. /* maximum number of requests in a pipeline */
  331. CURLOPT(CURLMOPT_MAX_PIPELINE_LENGTH, CURLOPTTYPE_LONG, 8),
  332. /* a connection with a content-length longer than this
  333. will not be considered for pipelining */
  334. CURLOPT(CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, CURLOPTTYPE_OFF_T, 9),
  335. /* a connection with a chunk length longer than this
  336. will not be considered for pipelining */
  337. CURLOPT(CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, CURLOPTTYPE_OFF_T, 10),
  338. /* a list of site names(+port) that are blocked from pipelining */
  339. CURLOPT(CURLMOPT_PIPELINING_SITE_BL, CURLOPTTYPE_OBJECTPOINT, 11),
  340. /* a list of server types that are blocked from pipelining */
  341. CURLOPT(CURLMOPT_PIPELINING_SERVER_BL, CURLOPTTYPE_OBJECTPOINT, 12),
  342. /* maximum number of open connections in total */
  343. CURLOPT(CURLMOPT_MAX_TOTAL_CONNECTIONS, CURLOPTTYPE_LONG, 13),
  344. /* This is the server push callback function pointer */
  345. CURLOPT(CURLMOPT_PUSHFUNCTION, CURLOPTTYPE_FUNCTIONPOINT, 14),
  346. /* This is the argument passed to the server push callback */
  347. CURLOPT(CURLMOPT_PUSHDATA, CURLOPTTYPE_OBJECTPOINT, 15),
  348. /* maximum number of concurrent streams to support on a connection */
  349. CURLOPT(CURLMOPT_MAX_CONCURRENT_STREAMS, CURLOPTTYPE_LONG, 16),
  350. CURLMOPT_LASTENTRY /* the last unused */
  351. } CURLMoption;
  352. /*
  353. * Name: curl_multi_setopt()
  354. *
  355. * Desc: Sets options for the multi handle.
  356. *
  357. * Returns: CURLM error code.
  358. */
  359. CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle,
  360. CURLMoption option, ...);
  361. /*
  362. * Name: curl_multi_assign()
  363. *
  364. * Desc: This function sets an association in the multi handle between the
  365. * given socket and a private pointer of the application. This is
  366. * (only) useful for curl_multi_socket uses.
  367. *
  368. * Returns: CURLM error code.
  369. */
  370. CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle,
  371. curl_socket_t sockfd, void *sockp);
  372. /*
  373. * Name: curl_multi_get_handles()
  374. *
  375. * Desc: Returns an allocated array holding all handles currently added to
  376. * the multi handle. Marks the final entry with a NULL pointer. If
  377. * there is no easy handle added to the multi handle, this function
  378. * returns an array with the first entry as a NULL pointer.
  379. *
  380. * Returns: NULL on failure, otherwise a CURL **array pointer
  381. */
  382. CURL_EXTERN CURL **curl_multi_get_handles(CURLM *multi_handle);
  383. /*
  384. * Name: curl_push_callback
  385. *
  386. * Desc: This callback gets called when a new stream is being pushed by the
  387. * server. It approves or denies the new stream. It can also decide
  388. * to completely fail the connection.
  389. *
  390. * Returns: CURL_PUSH_OK, CURL_PUSH_DENY or CURL_PUSH_ERROROUT
  391. */
  392. #define CURL_PUSH_OK 0
  393. #define CURL_PUSH_DENY 1
  394. #define CURL_PUSH_ERROROUT 2 /* added in 7.72.0 */
  395. struct curl_pushheaders; /* forward declaration only */
  396. CURL_EXTERN char *curl_pushheader_bynum(struct curl_pushheaders *h,
  397. size_t num);
  398. CURL_EXTERN char *curl_pushheader_byname(struct curl_pushheaders *h,
  399. const char *name);
  400. typedef int (*curl_push_callback)(CURL *parent,
  401. CURL *easy,
  402. size_t num_headers,
  403. struct curl_pushheaders *headers,
  404. void *userp);
  405. #ifdef __cplusplus
  406. } /* end of extern "C" */
  407. #endif
  408. #endif