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