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