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