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. Do not give away any internals here!
28
29 GOALS
30
31 o Enable a "pull" interface. The application that uses libcurl decides where
32 and when to ask libcurl to get/send data.
33
34 o Enable multiple simultaneous transfers in the same thread without making it
35 complicated for the application.
36
37 o Enable the application to select() on its own file descriptors and curl's
38 file descriptors simultaneous easily.
39
40*/
41
42/*
43 * This header file should not really need to include "curl.h" since curl.h
44 * itself includes this file and we expect user applications to do #include
45 * <curl/curl.h> without the need for especially including multi.h.
46 *
47 * For some reason we added this include here at one point, and rather than to
48 * break existing (wrongly written) libcurl applications, we leave it as-is
49 * but with this warning attached.
50 */
51#include "curl.h"
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57typedef void CURLM;
58
59typedef enum {
60 CURLM_CALL_MULTI_PERFORM = -1, /* please call curl_multi_perform() or
61 curl_multi_socket*() soon */
62 CURLM_OK,
63 CURLM_BAD_HANDLE, /* the passed-in handle is not a valid CURLM handle */
64 CURLM_BAD_EASY_HANDLE, /* an easy handle was not good/valid */
65 CURLM_OUT_OF_MEMORY, /* if you ever get this, you are in deep sh*t */
66 CURLM_INTERNAL_ERROR, /* this is a libcurl bug */
67 CURLM_BAD_SOCKET, /* the passed in socket argument did not match */
68 CURLM_UNKNOWN_OPTION, /* curl_multi_setopt() with unsupported option */
69 CURLM_ADDED_ALREADY, /* an easy handle already added to a multi handle was
70 attempted to get added - again */
71 CURLM_RECURSIVE_API_CALL, /* an api function was called from inside a
72 callback */
73 CURLM_WAKEUP_FAILURE, /* wakeup is unavailable or failed */
74 CURLM_BAD_FUNCTION_ARGUMENT, /* function called with a bad parameter */
75 CURLM_ABORTED_BY_CALLBACK,
76 CURLM_UNRECOVERABLE_POLL,
77 CURLM_LAST
78} CURLMcode;
79
80/* You can check for CURLM_CALL_MULTI_SOCKET too in the same style it works
81 for curl_multi_perform() and CURLM_CALL_MULTI_PERFORM */
82#define CURLM_CALL_MULTI_SOCKET CURLM_CALL_MULTI_PERFORM
83
84/* bitmask bits for CURLMOPT_PIPELINING */
85#define CURLPIPE_NOTHING 0L
86#define CURLPIPE_HTTP1 1L
87#define CURLPIPE_MULTIPLEX 2L
88
89typedef enum {
90 CURLMSG_NONE, /* first, not used */
91 CURLMSG_DONE, /* This easy handle has completed. 'result' contains
92 the CURLcode of the transfer */
93 CURLMSG_LAST /* last, not used */
94} CURLMSG;
95
96struct CURLMsg {
97 CURLMSG msg; /* what this message means */
98 CURL *easy_handle; /* the handle it concerns */
99 union {
100 void *whatever; /* message-specific data */
101 CURLcode result; /* return code for transfer */
102 } data;
103};
104typedef struct CURLMsg CURLMsg;
105
106/* Based on poll(2) structure and values.
107 * We do not use pollfd and POLL* constants explicitly
108 * to cover platforms without poll(). */
109#define CURL_WAIT_POLLIN 0x0001
110#define CURL_WAIT_POLLPRI 0x0002
111#define CURL_WAIT_POLLOUT 0x0004
112
113struct curl_waitfd {
114 curl_socket_t fd;
115 short events;
116 short revents;
117};
118
119/*
120 * Name: curl_multi_init()
121 *
122 * Desc: initialize multi-style curl usage
123 *
124 * Returns: a new CURLM handle to use in all 'curl_multi' functions.
125 */
126CURL_EXTERN CURLM *curl_multi_init(void);
127
128/*
129 * Name: curl_multi_add_handle()
130 *
131 * Desc: add a standard curl handle to the multi stack
132 *
133 * Returns: CURLMcode type, general multi error code.
134 */
135CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle,
136 CURL *curl_handle);
137
138/*
139 * Name: curl_multi_remove_handle()
140 *
141 * Desc: removes a curl handle from the multi stack again
142 *
143 * Returns: CURLMcode type, general multi error code.
144 */
145CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
146 CURL *curl_handle);
147
148/*
149 * Name: curl_multi_fdset()
150 *
151 * Desc: Ask curl for its fd_set sets. The app can use these to select() or
152 * poll() on. We want curl_multi_perform() called as soon as one of
153 * them are ready.
154 *
155 * Returns: CURLMcode type, general multi error code.
156 */
157CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,
158 fd_set *read_fd_set,
159 fd_set *write_fd_set,
160 fd_set *exc_fd_set,
161 int *max_fd);
162
163/*
164 * Name: curl_multi_wait()
165 *
166 * Desc: Poll on all fds within a CURLM set as well as any
167 * additional fds passed to the function.
168 *
169 * Returns: CURLMcode type, general multi error code.
170 */
171CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle,
172 struct curl_waitfd extra_fds[],
173 unsigned int extra_nfds,
174 int timeout_ms,
175 int *ret);
176
177/*
178 * Name: curl_multi_poll()
179 *
180 * Desc: Poll on all fds within a CURLM set as well as any
181 * additional fds passed to the function.
182 *
183 * Returns: CURLMcode type, general multi error code.
184 */
185CURL_EXTERN CURLMcode curl_multi_poll(CURLM *multi_handle,
186 struct curl_waitfd extra_fds[],
187 unsigned int extra_nfds,
188 int timeout_ms,
189 int *ret);
190
191/*
192 * Name: curl_multi_wakeup()
193 *
194 * Desc: wakes up a sleeping curl_multi_poll call.
195 *
196 * Returns: CURLMcode type, general multi error code.
197 */
198CURL_EXTERN CURLMcode curl_multi_wakeup(CURLM *multi_handle);
199
200/*
201 * Name: curl_multi_perform()
202 *
203 * Desc: When the app thinks there is data available for curl it calls this
204 * function to read/write whatever there is right now. This returns
205 * as soon as the reads and writes are done. This function does not
206 * require that there actually is data available for reading or that
207 * data can be written, it can be called. It returns the number of
208 * handles that still transfer data in the second argument's
209 * integer-pointer.
210 *
211 * Returns: CURLMcode type, general multi error code. *NOTE* that this only
212 * returns errors etc regarding the whole multi stack. There might
213 * still have occurred problems on individual transfers even when
214 * this returns OK.
215 */
216CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle,
217 int *running_handles);
218
219/*
220 * Name: curl_multi_cleanup()
221 *
222 * Desc: Cleans up and removes a whole multi stack. It does not free or
223 * touch any individual easy handles in any way. We need to define
224 * in what state those handles will be if this function is called
225 * in the middle of a transfer.
226 *
227 * Returns: CURLMcode type, general multi error code.
228 */
229CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle);
230
231/*
232 * Name: curl_multi_info_read()
233 *
234 * Desc: Ask the multi handle if there is any messages/informationals from
235 * the individual transfers. Messages include informationals such as
236 * error code from the transfer or the fact that a transfer is
237 * completed. More details on these should be written down as well.
238 *
239 * Repeated calls to this function will return a new struct each
240 * time, until a special "end of msgs" struct is returned as a signal
241 * that there is no more to get at this point.
242 *
243 * The data the returned pointer points to will not survive calling
244 * curl_multi_cleanup().
245 *
246 * The 'CURLMsg' struct is meant to be simple and only contain basic
247 * information. If more involved information is wanted, we will
248 * provide the particular "transfer handle" in that struct and that
249 * should/could/would be used in subsequent curl_easy_getinfo() calls
250 * (or similar). The point being that we must never expose complex
251 * structs to applications, as then we will undoubtably get backwards
252 * compatibility problems in the future.
253 *
254 * Returns: A pointer to a filled-in struct, or NULL if it failed or ran out
255 * of structs. It also writes the number of messages left in the
256 * queue (after this read) in the integer the second argument points
257 * to.
258 */
259CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle,
260 int *msgs_in_queue);
261
262/*
263 * Name: curl_multi_strerror()
264 *
265 * Desc: The curl_multi_strerror function may be used to turn a CURLMcode
266 * value into the equivalent human readable error string. This is
267 * useful for printing meaningful error messages.
268 *
269 * Returns: A pointer to a null-terminated error message.
270 */
271CURL_EXTERN const char *curl_multi_strerror(CURLMcode error);
272
273/*
274 * Name: curl_multi_socket() and
275 * curl_multi_socket_all()
276 *
277 * Desc: An alternative version of curl_multi_perform() that allows the
278 * application to pass in one of the file descriptors that have been
279 * detected to have "action" on them and let libcurl perform.
280 * See man page for details.
281 */
282#define CURL_POLL_NONE 0
283#define CURL_POLL_IN 1
284#define CURL_POLL_OUT 2
285#define CURL_POLL_INOUT 3
286#define CURL_POLL_REMOVE 4
287
288#define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD
289
290#define CURL_CSELECT_IN 0x01
291#define CURL_CSELECT_OUT 0x02
292#define CURL_CSELECT_ERR 0x04
293
294typedef int (*curl_socket_callback)(CURL *easy, /* easy handle */
295 curl_socket_t s, /* socket */
296 int what, /* see above */
297 void *userp, /* private callback
298 pointer */
299 void *socketp); /* private socket
300 pointer */
301/*
302 * Name: curl_multi_timer_callback
303 *
304 * Desc: Called by libcurl whenever the library detects a change in the
305 * maximum number of milliseconds the app is allowed to wait before
306 * curl_multi_socket() or curl_multi_perform() must be called
307 * (to allow libcurl's timed events to take place).
308 *
309 * Returns: The callback should return zero.
310 */
311typedef int (*curl_multi_timer_callback)(CURLM *multi, /* multi handle */
312 long timeout_ms, /* see above */
313 void *userp); /* private callback
314 pointer */
315
316CURL_EXTERN CURLMcode CURL_DEPRECATED(7.19.5, "Use curl_multi_socket_action()")
317curl_multi_socket(CURLM *multi_handle, curl_socket_t s, int *running_handles);
318
319CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle,
320 curl_socket_t s,
321 int ev_bitmask,
322 int *running_handles);
323
324CURL_EXTERN CURLMcode CURL_DEPRECATED(7.19.5, "Use curl_multi_socket_action()")
325curl_multi_socket_all(CURLM *multi_handle, int *running_handles);
326
327#ifndef CURL_ALLOW_OLD_MULTI_SOCKET
328/* This macro below was added in 7.16.3 to push users who recompile to use
329 * the new curl_multi_socket_action() instead of the old curl_multi_socket()
330 */
331#define curl_multi_socket(x, y, z) curl_multi_socket_action(x, y, 0, z)
332#endif
333
334/*
335 * Name: curl_multi_timeout()
336 *
337 * Desc: Returns the maximum number of milliseconds the app is allowed to
338 * wait before curl_multi_socket() or curl_multi_perform() must be
339 * called (to allow libcurl's timed events to take place).
340 *
341 * Returns: CURLM error code.
342 */
343CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle,
344 long *milliseconds);
345
346typedef enum {
347 /* This is the socket callback function pointer */
348 CURLOPT(CURLMOPT_SOCKETFUNCTION, CURLOPTTYPE_FUNCTIONPOINT, 1),
349
350 /* This is the argument passed to the socket callback */
351 CURLOPT(CURLMOPT_SOCKETDATA, CURLOPTTYPE_OBJECTPOINT, 2),
352
353 /* set to 1 to enable pipelining for this multi handle */
354 CURLOPT(CURLMOPT_PIPELINING, CURLOPTTYPE_LONG, 3),
355
356 /* This is the timer callback function pointer */
357 CURLOPT(CURLMOPT_TIMERFUNCTION, CURLOPTTYPE_FUNCTIONPOINT, 4),
358
359 /* This is the argument passed to the timer callback */
360 CURLOPT(CURLMOPT_TIMERDATA, CURLOPTTYPE_OBJECTPOINT, 5),
361
362 /* maximum number of entries in the connection cache */
363 CURLOPT(CURLMOPT_MAXCONNECTS, CURLOPTTYPE_LONG, 6),
364
365 /* maximum number of (pipelining) connections to one host */
366 CURLOPT(CURLMOPT_MAX_HOST_CONNECTIONS, CURLOPTTYPE_LONG, 7),
367
368 /* maximum number of requests in a pipeline */
369 CURLOPT(CURLMOPT_MAX_PIPELINE_LENGTH, CURLOPTTYPE_LONG, 8),
370
371 /* a connection with a content-length longer than this
372 will not be considered for pipelining */
373 CURLOPT(CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, CURLOPTTYPE_OFF_T, 9),
374
375 /* a connection with a chunk length longer than this
376 will not be considered for pipelining */
377 CURLOPT(CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, CURLOPTTYPE_OFF_T, 10),
378
379 /* a list of site names(+port) that are blocked from pipelining */
380 CURLOPT(CURLMOPT_PIPELINING_SITE_BL, CURLOPTTYPE_OBJECTPOINT, 11),
381
382 /* a list of server types that are blocked from pipelining */
383 CURLOPT(CURLMOPT_PIPELINING_SERVER_BL, CURLOPTTYPE_OBJECTPOINT, 12),
384
385 /* maximum number of open connections in total */
386 CURLOPT(CURLMOPT_MAX_TOTAL_CONNECTIONS, CURLOPTTYPE_LONG, 13),
387
388 /* This is the server push callback function pointer */
389 CURLOPT(CURLMOPT_PUSHFUNCTION, CURLOPTTYPE_FUNCTIONPOINT, 14),
390
391 /* This is the argument passed to the server push callback */
392 CURLOPT(CURLMOPT_PUSHDATA, CURLOPTTYPE_OBJECTPOINT, 15),
393
394 /* maximum number of concurrent streams to support on a connection */
395 CURLOPT(CURLMOPT_MAX_CONCURRENT_STREAMS, CURLOPTTYPE_LONG, 16),
396
397 /* network has changed, adjust caches/connection reuse */
398 CURLOPT(CURLMOPT_NETWORK_CHANGED, CURLOPTTYPE_LONG, 17),
399
400 /* This is the notify callback function pointer */
401 CURLOPT(CURLMOPT_NOTIFYFUNCTION, CURLOPTTYPE_FUNCTIONPOINT, 18),
402
403 /* This is the argument passed to the notify callback */
404 CURLOPT(CURLMOPT_NOTIFYDATA, CURLOPTTYPE_OBJECTPOINT, 19),
405
406 CURLMOPT_LASTENTRY /* the last unused */
407} CURLMoption;
408
409/* Definition of bits for the CURLMOPT_NETWORK_CHANGED argument: */
410
411/* - CURLMNWC_CLEAR_CONNS tells libcurl to prevent further reuse of existing
412 connections. Connections that are idle will be closed. Ongoing transfers
413 will continue with the connection they have. */
414#define CURLMNWC_CLEAR_CONNS (1L << 0)
415
416/* - CURLMNWC_CLEAR_DNS tells libcurl to prevent further reuse of existing
417 connections. Connections that are idle will be closed. Ongoing transfers
418 will continue with the connection they have. */
419#define CURLMNWC_CLEAR_DNS (1L << 0)
420
421/*
422 * Name: curl_multi_setopt()
423 *
424 * Desc: Sets options for the multi handle.
425 *
426 * Returns: CURLM error code.
427 */
428CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle,
429 CURLMoption option, ...);
430
431/*
432 * Name: curl_multi_assign()
433 *
434 * Desc: This function sets an association in the multi handle between the
435 * given socket and a private pointer of the application. This is
436 * (only) useful for curl_multi_socket uses.
437 *
438 * Returns: CURLM error code.
439 */
440CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle,
441 curl_socket_t sockfd, void *sockp);
442
443/*
444 * Name: curl_multi_get_handles()
445 *
446 * Desc: Returns an allocated array holding all handles currently added to
447 * the multi handle. Marks the final entry with a NULL pointer. If
448 * there is no easy handle added to the multi handle, this function
449 * returns an array with the first entry as a NULL pointer.
450 *
451 * Returns: NULL on failure, otherwise a CURL **array pointer
452 */
453CURL_EXTERN CURL **curl_multi_get_handles(CURLM *multi_handle);
454
455typedef enum {
456 CURLMINFO_NONE, /* first, never use this */
457 /* The number of easy handles currently managed by the multi handle,
458 * e.g. have been added but not yet removed. */
459 CURLMINFO_XFERS_CURRENT = 1,
460 /* The number of easy handles running, e.g. not done and not queueing. */
461 CURLMINFO_XFERS_RUNNING = 2,
462 /* The number of easy handles waiting to start, e.g. for a connection
463 * to become available due to limits on parallelism, max connections
464 * or other factors. */
465 CURLMINFO_XFERS_PENDING = 3,
466 /* The number of easy handles finished, waiting for their results to
467 * be read via `curl_multi_info_read()`. */
468 CURLMINFO_XFERS_DONE = 4,
469 /* The total number of easy handles added to the multi handle, ever. */
470 CURLMINFO_XFERS_ADDED = 5,
471
472 CURLMINFO_LASTENTRY /* the last unused */
473} CURLMinfo_offt;
474
475/*
476 * Name: curl_multi_get_offt()
477 *
478 * Desc: Retrieves a numeric value for the `CURLMINFO_*` enums.
479 *
480 * Returns: CULRM_OK or error when value could not be obtained.
481 */
482CURL_EXTERN CURLMcode curl_multi_get_offt(CURLM *multi_handle,
483 CURLMinfo_offt info,
484 curl_off_t *pvalue);
485
486/*
487 * Name: curl_push_callback
488 *
489 * Desc: This callback gets called when a new stream is being pushed by the
490 * server. It approves or denies the new stream. It can also decide
491 * to completely fail the connection.
492 *
493 * Returns: CURL_PUSH_OK, CURL_PUSH_DENY or CURL_PUSH_ERROROUT
494 */
495#define CURL_PUSH_OK 0
496#define CURL_PUSH_DENY 1
497#define CURL_PUSH_ERROROUT 2 /* added in 7.72.0 */
498
499struct curl_pushheaders; /* forward declaration only */
500
501CURL_EXTERN char *curl_pushheader_bynum(struct curl_pushheaders *h,
502 size_t num);
503CURL_EXTERN char *curl_pushheader_byname(struct curl_pushheaders *h,
504 const char *name);
505
506typedef int (*curl_push_callback)(CURL *parent,
507 CURL *easy,
508 size_t num_headers,
509 struct curl_pushheaders *headers,
510 void *userp);
511
512/*
513 * Name: curl_multi_waitfds()
514 *
515 * Desc: Ask curl for fds for polling. The app can use these to poll on.
516 * We want curl_multi_perform() called as soon as one of them are
517 * ready. Passing zero size allows to get a number of fds.
518 *
519 * Returns: CURLMcode type, general multi error code.
520 */
521CURL_EXTERN CURLMcode curl_multi_waitfds(CURLM *multi,
522 struct curl_waitfd *ufds,
523 unsigned int size,
524 unsigned int *fd_count);
525
526/*
527 * Notifications dispatched by a multi handle, when enabled.
528 */
529#define CURLMNOTIFY_INFO_READ 0
530#define CURLMNOTIFY_EASY_DONE 1
531
532/*
533 * Callback to install via CURLMOPT_NOTIFYFUNCTION.
534 */
535typedef void (*curl_notify_callback)(CURLM *multi,
536 unsigned int notification,
537 CURL *easy,
538 void *user_data);
539
540CURL_EXTERN CURLMcode curl_multi_notify_disable(CURLM *multi,
541 unsigned int notification);
542
543CURL_EXTERN CURLMcode curl_multi_notify_enable(CURLM *multi,
544 unsigned int notification);
545
546#ifdef __cplusplus
547} /* end of extern "C" */
548#endif
549
550#endif
551