1 #ifndef __CURL_MULTI_H
2 #define __CURL_MULTI_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2007, 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  * licensed versions 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 #include "cafe/curl/cafeport.h"
26 
27 /*
28   This is an "external" header file. do not give away any internals here!
29 
30   GOALS
31 
32   o Enable a "pull" interface. The application that uses libcurl decides where
33     and when to ask libcurl to get/send data.
34 
35   o Enable multiple simultaneous transfers in the same thread without making it
36     complicated for the application.
37 
38   o Enable the application to select() on its own file descriptors and curl's
39     file descriptors simultaneous easily.
40 
41 */
42 
43 /*
44  * This header file should not really include "curl.h" since curl.h
45  * itself includes this file and we expect user applications to do #include
46  * <curl/curl.h> without the need for especially including multi.h.
47  *
48  * For some reason we added this include here at one point, and rather than to
49  * break existing (wrongly written) libcurl applications, we leave it as is
50  * but with this warning attached.
51  */
52 #include "cafe/curl/curl.h"
53 
54 #ifdef  __cplusplus
55 extern "C" {
56 #endif
57 
58 typedef void CURLM;
59 
60 typedef enum {
61   CURLM_CALL_MULTI_PERFORM = -1, /* call curl_multi_perform() or
62                                     curl_multi_socket*() soon */
63   CURLM_OK,
64   CURLM_BAD_HANDLE,      /* the passed-in handle is not a valid CURLM handle */
65   CURLM_BAD_EASY_HANDLE, /* an easy handle was not good/valid */
66   CURLM_OUT_OF_MEMORY,   /* out of memory */
67   CURLM_INTERNAL_ERROR,  /* this is a libcurl bug */
68   CURLM_BAD_SOCKET,      /* the passed in socket argument did not match */
69   CURLM_UNKNOWN_OPTION,  /* curl_multi_setopt() with unsupported option */
70   CURLM_LAST
71 } CURLMcode;
72 
73 /* just to make code nicer when using curl_multi_socket() you can now check
74    for CURLM_CALL_MULTI_SOCKET too in the same style it works for
75    curl_multi_perform() and CURLM_CALL_MULTI_PERFORM */
76 #define CURLM_CALL_MULTI_SOCKET CURLM_CALL_MULTI_PERFORM
77 
78 typedef enum {
79   CURLMSG_NONE, /* first, not used */
80   CURLMSG_DONE, /* This easy handle has completed. 'result' contains
81                    the CURLcode of the transfer */
82   CURLMSG_LAST /* last, not used */
83 } CURLMSG;
84 
85 struct CURLMsg {
86   CURLMSG msg;       /* what this message means */
87   CURL *easy_handle; /* the handle it concerns */
88   union {
89     void *whatever;    /* message-specific data */
90     CURLcode result;   /* return code for transfer */
91   } data;
92 };
93 typedef struct CURLMsg CURLMsg;
94 
95 /*
96  * Name:    curl_multi_init()
97  *
98  * Desc:    inititalize multi-style curl usage
99  *
100  * Returns: a new CURLM handle to use in all 'curl_multi' functions.
101  */
102 CURL_EXTERN CURLM *curl_multi_init(void);
103 
104 /*
105  * Name:    curl_multi_add_handle()
106  *
107  * Desc:    add a standard curl handle to the multi stack
108  *
109  * Returns: CURLMcode type, general multi error code.
110  */
111 CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle,
112                                             CURL *curl_handle);
113 
114  /*
115   * Name:    curl_multi_remove_handle()
116   *
117   * Desc:    removes a curl handle from the multi stack again
118   *
119   * Returns: CURLMcode type, general multi error code.
120   */
121 CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
122                                                CURL *curl_handle);
123 
124  /*
125   * Name:    curl_multi_fdset()
126   *
127   * Desc:    Ask curl for its fd_set sets. The app can use these to select() or
128   *          poll() on. We want curl_multi_perform() called as soon as one of
129   *          them are ready.
130   *
131   * Returns: CURLMcode type, general multi error code.
132   */
133 CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,
134                                        fd_set *read_fd_set,
135                                        fd_set *write_fd_set,
136                                        fd_set *exc_fd_set,
137                                        int *max_fd);
138 
139  /*
140   * Name:    curl_multi_perform()
141   *
142   * Desc:    When the app thinks there is data available for curl it calls this
143   *          function to read/write whatever there is right now. This returns
144   *          as soon as the reads and writes are done. This function does not
145   *          require that there actually is data available for reading or that
146   *          data can be written, it can be called just in case. It returns
147   *          the number of handles that still transfer data in the second
148   *          argument's integer-pointer.
149   *
150   * Returns: CURLMcode type, general multi error code. *NOTE* that this only
151   *          returns errors for the whole multi stack. There might
152   *          still have occurred problems on invidual transfers even when this
153   *          returns OK.
154   */
155 CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle,
156                                          int *running_handles);
157 
158  /*
159   * Name:    curl_multi_cleanup()
160   *
161   * Desc:    Cleans up and removes a whole multi stack. It does not free or
162   *          touch any individual easy handles in any way. We must define
163   *          in what state those handles will be if this function is called
164   *          in the middle of a transfer.
165   *
166   * Returns: CURLMcode type, general multi error code.
167   */
168 CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle);
169 
170 /*
171  * Name:    curl_multi_info_read()
172  *
173  * Desc:    Ask the multi handle if there is any messages/informationals from
174  *          the individual transfers. Messages include informationals such as
175  *          error code from the transfer or just the fact that a transfer is
176  *          completed. More details on these should be written down as well.
177  *
178  *          Repeated calls to this function will return a new struct each
179  *          time, until a special "end of msgs" struct is returned as a signal
180  *          that there is no more to get at this point.
181  *
182  *          The data the returned pointer points to will not survive calling
183  *          curl_multi_cleanup().
184  *
185  *          The 'CURLMsg' struct is meant to be very simple and only contain
186  *          very basic informations. If more involved information is wanted,
187  *          we will provide the particular "transfer handle" in that struct
188  *          and that should/could/would be used in subsequent
189  *          curl_easy_getinfo() calls (or similar). The point being that we
190  *          must never expose complex structs to applications because then we will
191  *          undoubtably get backward compatibility problems in the future.
192  *
193  * Returns: A pointer to a filled-in struct, or NULL if it failed or ran out
194  *          of structs. It also writes the number of messages left in the
195  *          queue (after this read) in the integer the second argument points
196  *          to.
197  */
198 CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle,
199                                           int *msgs_in_queue);
200 
201 /*
202  * Name:    curl_multi_strerror()
203  *
204  * Desc:    The curl_multi_strerror function may be used to turn a CURLMcode
205  *          value into the equivalent human readable error string.  This is
206  *          useful for printing meaningful error messages.
207  *
208  * Returns: A pointer to a zero-terminated error message.
209  */
210 CURL_EXTERN const char *curl_multi_strerror(CURLMcode);
211 
212 /*
213  * Name:    curl_multi_socket() and
214  *          curl_multi_socket_all()
215  *
216  * Desc:    An alternative version of curl_multi_perform() that allows the
217  *          application to pass in one of the file descriptors that have been
218  *          detected to have "action" on them and let libcurl perform.
219  *          See man page for details.
220  */
221 #define CURL_POLL_NONE   0
222 #define CURL_POLL_IN     1
223 #define CURL_POLL_OUT    2
224 #define CURL_POLL_INOUT  3
225 #define CURL_POLL_REMOVE 4
226 
227 #define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD
228 
229 #define CURL_CSELECT_IN   0x01
230 #define CURL_CSELECT_OUT  0x02
231 #define CURL_CSELECT_ERR  0x04
232 
233 typedef int (*curl_socket_callback)(CURL *easy,      /* easy handle */
234                                     curl_socket_t s, /* socket */
235                                     int what,        /* see above */
236                                     void *userp,     /* private callback
237                                                         pointer */
238                                     void *socketp);  /* private socket
239                                                         pointer */
240 /*
241  * Name:    curl_multi_timer_callback
242  *
243  * Desc:    Called by libcurl whenever the library detects a change in the
244  *          maximum number of milliseconds the app is allowed to wait before
245  *          curl_multi_socket() or curl_multi_perform() must be called
246  *          (to allow libcurl's timed events to take place).
247  *
248  * Returns: The callback should return zero.
249  */
250 typedef int (*curl_multi_timer_callback)(CURLM *multi,    /* multi handle */
251                                          long timeout_ms, /* see above */
252                                          void *userp);    /* private callback
253                                                              pointer */
254 
255 CURL_EXTERN CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s,
256                                         int *running_handles);
257 
258 CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle,
259                                                curl_socket_t s,
260                                                int ev_bitmask,
261                                                int *running_handles);
262 
263 CURL_EXTERN CURLMcode curl_multi_socket_all(CURLM *multi_handle,
264                                             int *running_handles);
265 
266 #ifndef CURL_ALLOW_OLD_MULTI_SOCKET
267 /* This macro below was added in 7.16.3 to push users who recompile to use
268    the new curl_multi_socket_action() instead of the old curl_multi_socket()
269 */
270 #define curl_multi_socket(x,y,z) curl_multi_socket_action(x,y,0,z)
271 #endif
272 
273 /*
274  * Name:    curl_multi_timeout()
275  *
276  * Desc:    Returns the maximum number of milliseconds the app is allowed to
277  *          wait before curl_multi_socket() or curl_multi_perform() must be
278  *          called (to allow libcurl's timed events to take place).
279  *
280  * Returns: CURLM error code.
281  */
282 CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle,
283                                          long *milliseconds);
284 
285 #undef CINIT /* re-using the same name as in curl.h */
286 
287 #ifdef CURL_ISOCPP
288 #define CINIT(name,type,num) CURLMOPT_ ## name = CURLOPTTYPE_ ## type + num
289 #else
290 /* The macro "##" is ISO C, we assume pre-ISO C does not support it. */
291 #define LONG          CURLOPTTYPE_LONG
292 #define OBJECTPOINT   CURLOPTTYPE_OBJECTPOINT
293 #define FUNCTIONPOINT CURLOPTTYPE_FUNCTIONPOINT
294 #define OFF_T         CURLOPTTYPE_OFF_T
295 #define CINIT(name,type,number) CURLMOPT_/**/name = type + number
296 #endif
297 
298 typedef enum {
299   /* This is the socket callback function pointer */
300   CINIT(SOCKETFUNCTION, FUNCTIONPOINT, 1),
301 
302   /* This is the argument passed to the socket callback */
303   CINIT(SOCKETDATA, OBJECTPOINT, 2),
304 
305     /* set to 1 to enable pipelining for this multi handle */
306   CINIT(PIPELINING, LONG, 3),
307 
308    /* This is the timer callback function pointer */
309   CINIT(TIMERFUNCTION, FUNCTIONPOINT, 4),
310 
311   /* This is the argument passed to the timer callback */
312   CINIT(TIMERDATA, OBJECTPOINT, 5),
313 
314   /* maximum number of entries in the connection cache */
315   CINIT(MAXCONNECTS, LONG, 6),
316 
317   CURLMOPT_LASTENTRY /* the last unused */
318 } CURLMoption;
319 
320 
321 /*
322  * Name:    curl_multi_setopt()
323  *
324  * Desc:    Sets options for the multi handle.
325  *
326  * Returns: CURLM error code.
327  */
328 CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle,
329                                         CURLMoption option, ...);
330 
331 
332 /*
333  * Name:    curl_multi_assign()
334  *
335  * Desc:    This function sets an association in the multi handle between the
336  *          given socket and a private pointer of the application. This is
337  *          (only) useful for curl_multi_socket uses.
338  *
339  * Returns: CURLM error code.
340  */
341 CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle,
342                                         curl_socket_t sockfd, void *sockp);
343 
344 #ifdef __cplusplus
345 } /* end of extern "C" */
346 #endif
347 
348 #endif
349