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