Imported Upstream version 7.59.0
[platform/upstream/curl.git] / docs / examples / asiohiper.cpp
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2012 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 /* <DESC>
24  * demonstrate the use of multi socket interface with boost::asio
25  * </DESC>
26  */
27 /*
28  * This program is in c++ and uses boost::asio instead of libevent/libev.
29  * Requires boost::asio, boost::bind and boost::system
30  *
31  * This is an adaptation of libcurl's "hiperfifo.c" and "evhiperfifo.c"
32  * sample programs. This example implements a subset of the functionality from
33  * hiperfifo.c, for full functionality refer hiperfifo.c or evhiperfifo.c
34  *
35  * Written by Lijo Antony based on hiperfifo.c by Jeff Pohlmeyer
36  *
37  * When running, the program creates an easy handle for a URL and
38  * uses the curl_multi API to fetch it.
39  *
40  * Note:
41  *  For the sake of simplicity, URL is hard coded to "www.google.com"
42  *
43  * This is purely a demo app, all retrieved data is simply discarded by the
44  * write callback.
45  */
46
47
48 #include <curl/curl.h>
49 #include <boost/asio.hpp>
50 #include <boost/bind.hpp>
51 #include <iostream>
52
53 #define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
54
55 /* boost::asio related objects
56  * using global variables for simplicity
57  */
58 boost::asio::io_service io_service;
59 boost::asio::deadline_timer timer(io_service);
60 std::map<curl_socket_t, boost::asio::ip::tcp::socket *> socket_map;
61
62 /* Global information, common to all connections */
63 typedef struct _GlobalInfo
64 {
65   CURLM *multi;
66   int still_running;
67 } GlobalInfo;
68
69 /* Information associated with a specific easy handle */
70 typedef struct _ConnInfo
71 {
72   CURL *easy;
73   char *url;
74   GlobalInfo *global;
75   char error[CURL_ERROR_SIZE];
76 } ConnInfo;
77
78 static void timer_cb(const boost::system::error_code & error, GlobalInfo *g);
79
80 /* Update the event timer after curl_multi library calls */
81 static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
82 {
83   fprintf(MSG_OUT, "\nmulti_timer_cb: timeout_ms %ld", timeout_ms);
84
85   /* cancel running timer */
86   timer.cancel();
87
88   if(timeout_ms > 0) {
89     /* update timer */
90     timer.expires_from_now(boost::posix_time::millisec(timeout_ms));
91     timer.async_wait(boost::bind(&timer_cb, _1, g));
92   }
93   else if(timeout_ms == 0) {
94     /* call timeout function immediately */
95     boost::system::error_code error; /*success*/
96     timer_cb(error, g);
97   }
98
99   return 0;
100 }
101
102 /* Die if we get a bad CURLMcode somewhere */
103 static void mcode_or_die(const char *where, CURLMcode code)
104 {
105   if(CURLM_OK != code) {
106     const char *s;
107     switch(code) {
108     case CURLM_CALL_MULTI_PERFORM:
109       s = "CURLM_CALL_MULTI_PERFORM";
110       break;
111     case CURLM_BAD_HANDLE:
112       s = "CURLM_BAD_HANDLE";
113       break;
114     case CURLM_BAD_EASY_HANDLE:
115       s = "CURLM_BAD_EASY_HANDLE";
116       break;
117     case CURLM_OUT_OF_MEMORY:
118       s = "CURLM_OUT_OF_MEMORY";
119       break;
120     case CURLM_INTERNAL_ERROR:
121       s = "CURLM_INTERNAL_ERROR";
122       break;
123     case CURLM_UNKNOWN_OPTION:
124       s = "CURLM_UNKNOWN_OPTION";
125       break;
126     case CURLM_LAST:
127       s = "CURLM_LAST";
128       break;
129     default:
130       s = "CURLM_unknown";
131       break;
132     case CURLM_BAD_SOCKET:
133       s = "CURLM_BAD_SOCKET";
134       fprintf(MSG_OUT, "\nERROR: %s returns %s", where, s);
135       /* ignore this error */
136       return;
137     }
138
139     fprintf(MSG_OUT, "\nERROR: %s returns %s", where, s);
140
141     exit(code);
142   }
143 }
144
145 /* Check for completed transfers, and remove their easy handles */
146 static void check_multi_info(GlobalInfo *g)
147 {
148   char *eff_url;
149   CURLMsg *msg;
150   int msgs_left;
151   ConnInfo *conn;
152   CURL *easy;
153   CURLcode res;
154
155   fprintf(MSG_OUT, "\nREMAINING: %d", g->still_running);
156
157   while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
158     if(msg->msg == CURLMSG_DONE) {
159       easy = msg->easy_handle;
160       res = msg->data.result;
161       curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
162       curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
163       fprintf(MSG_OUT, "\nDONE: %s => (%d) %s", eff_url, res, conn->error);
164       curl_multi_remove_handle(g->multi, easy);
165       free(conn->url);
166       curl_easy_cleanup(easy);
167       free(conn);
168     }
169   }
170 }
171
172 /* Called by asio when there is an action on a socket */
173 static void event_cb(GlobalInfo *g, curl_socket_t s,
174                      int action, const boost::system::error_code & error,
175                      int *fdp)
176 {
177   fprintf(MSG_OUT, "\nevent_cb: action=%d", action);
178
179   if(socket_map.find(s) == socket_map.end()) {
180     fprintf(MSG_OUT, "\nevent_cb: socket already closed");
181     return;
182   }
183
184   /* make sure the event matches what are wanted */
185   if(*fdp == action || *fdp == CURL_POLL_INOUT) {
186     CURLMcode rc;
187     if(error)
188       action = CURL_CSELECT_ERR;
189     rc = curl_multi_socket_action(g->multi, s, action, &g->still_running);
190
191     mcode_or_die("event_cb: curl_multi_socket_action", rc);
192     check_multi_info(g);
193
194     if(g->still_running <= 0) {
195       fprintf(MSG_OUT, "\nlast transfer done, kill timeout");
196       timer.cancel();
197     }
198
199     /* keep on watching.
200      * the socket may have been closed and/or fdp may have been changed
201      * in curl_multi_socket_action(), so check them both */
202     if(!error && socket_map.find(s) != socket_map.end() &&
203        (*fdp == action || *fdp == CURL_POLL_INOUT)) {
204       boost::asio::ip::tcp::socket *tcp_socket = socket_map.find(s)->second;
205
206       if(action == CURL_POLL_IN) {
207         tcp_socket->async_read_some(boost::asio::null_buffers(),
208                                     boost::bind(&event_cb, g, s,
209                                                 action, _1, fdp));
210       }
211       if(action == CURL_POLL_OUT) {
212         tcp_socket->async_write_some(boost::asio::null_buffers(),
213                                      boost::bind(&event_cb, g, s,
214                                                  action, _1, fdp));
215       }
216     }
217   }
218 }
219
220 /* Called by asio when our timeout expires */
221 static void timer_cb(const boost::system::error_code & error, GlobalInfo *g)
222 {
223   if(!error) {
224     fprintf(MSG_OUT, "\ntimer_cb: ");
225
226     CURLMcode rc;
227     rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0,
228                                   &g->still_running);
229
230     mcode_or_die("timer_cb: curl_multi_socket_action", rc);
231     check_multi_info(g);
232   }
233 }
234
235 /* Clean up any data */
236 static void remsock(int *f, GlobalInfo *g)
237 {
238   fprintf(MSG_OUT, "\nremsock: ");
239
240   if(f) {
241     free(f);
242   }
243 }
244
245 static void setsock(int *fdp, curl_socket_t s, CURL *e, int act, int oldact,
246                     GlobalInfo *g)
247 {
248   fprintf(MSG_OUT, "\nsetsock: socket=%d, act=%d, fdp=%p", s, act, fdp);
249
250   std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it =
251     socket_map.find(s);
252
253   if(it == socket_map.end()) {
254     fprintf(MSG_OUT, "\nsocket %d is a c-ares socket, ignoring", s);
255     return;
256   }
257
258   boost::asio::ip::tcp::socket * tcp_socket = it->second;
259
260   *fdp = act;
261
262   if(act == CURL_POLL_IN) {
263     fprintf(MSG_OUT, "\nwatching for socket to become readable");
264     if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) {
265       tcp_socket->async_read_some(boost::asio::null_buffers(),
266                                   boost::bind(&event_cb, g, s,
267                                               CURL_POLL_IN, _1, fdp));
268     }
269   }
270   else if(act == CURL_POLL_OUT) {
271     fprintf(MSG_OUT, "\nwatching for socket to become writable");
272     if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) {
273       tcp_socket->async_write_some(boost::asio::null_buffers(),
274                                    boost::bind(&event_cb, g, s,
275                                                CURL_POLL_OUT, _1, fdp));
276     }
277   }
278   else if(act == CURL_POLL_INOUT) {
279     fprintf(MSG_OUT, "\nwatching for socket to become readable & writable");
280     if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) {
281       tcp_socket->async_read_some(boost::asio::null_buffers(),
282                                   boost::bind(&event_cb, g, s,
283                                               CURL_POLL_IN, _1, fdp));
284     }
285     if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) {
286       tcp_socket->async_write_some(boost::asio::null_buffers(),
287                                    boost::bind(&event_cb, g, s,
288                                                CURL_POLL_OUT, _1, fdp));
289     }
290   }
291 }
292
293 static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
294 {
295   /* fdp is used to store current action */
296   int *fdp = (int *) calloc(sizeof(int), 1);
297
298   setsock(fdp, s, easy, action, 0, g);
299   curl_multi_assign(g->multi, s, fdp);
300 }
301
302 /* CURLMOPT_SOCKETFUNCTION */
303 static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
304 {
305   fprintf(MSG_OUT, "\nsock_cb: socket=%d, what=%d, sockp=%p", s, what, sockp);
306
307   GlobalInfo *g = (GlobalInfo*) cbp;
308   int *actionp = (int *) sockp;
309   const char *whatstr[] = { "none", "IN", "OUT", "INOUT", "REMOVE"};
310
311   fprintf(MSG_OUT,
312           "\nsocket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
313
314   if(what == CURL_POLL_REMOVE) {
315     fprintf(MSG_OUT, "\n");
316     remsock(actionp, g);
317   }
318   else {
319     if(!actionp) {
320       fprintf(MSG_OUT, "\nAdding data: %s", whatstr[what]);
321       addsock(s, e, what, g);
322     }
323     else {
324       fprintf(MSG_OUT,
325               "\nChanging action from %s to %s",
326               whatstr[*actionp], whatstr[what]);
327       setsock(actionp, s, e, what, *actionp, g);
328     }
329   }
330
331   return 0;
332 }
333
334 /* CURLOPT_WRITEFUNCTION */
335 static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
336 {
337   size_t written = size * nmemb;
338   char *pBuffer = (char *)malloc(written + 1);
339
340   strncpy(pBuffer, (const char *)ptr, written);
341   pBuffer[written] = '\0';
342
343   fprintf(MSG_OUT, "%s", pBuffer);
344
345   free(pBuffer);
346
347   return written;
348 }
349
350 /* CURLOPT_PROGRESSFUNCTION */
351 static int prog_cb(void *p, double dltotal, double dlnow, double ult,
352                    double uln)
353 {
354   ConnInfo *conn = (ConnInfo *)p;
355
356   (void)ult;
357   (void)uln;
358
359   fprintf(MSG_OUT, "\nProgress: %s (%g/%g)", conn->url, dlnow, dltotal);
360   fprintf(MSG_OUT, "\nProgress: %s (%g)", conn->url, ult);
361
362   return 0;
363 }
364
365 /* CURLOPT_OPENSOCKETFUNCTION */
366 static curl_socket_t opensocket(void *clientp, curlsocktype purpose,
367                                 struct curl_sockaddr *address)
368 {
369   fprintf(MSG_OUT, "\nopensocket :");
370
371   curl_socket_t sockfd = CURL_SOCKET_BAD;
372
373   /* restrict to IPv4 */
374   if(purpose == CURLSOCKTYPE_IPCXN && address->family == AF_INET) {
375     /* create a tcp socket object */
376     boost::asio::ip::tcp::socket *tcp_socket =
377       new boost::asio::ip::tcp::socket(io_service);
378
379     /* open it and get the native handle*/
380     boost::system::error_code ec;
381     tcp_socket->open(boost::asio::ip::tcp::v4(), ec);
382
383     if(ec) {
384       /* An error occurred */
385       std::cout << std::endl << "Couldn't open socket [" << ec << "][" <<
386         ec.message() << "]";
387       fprintf(MSG_OUT, "\nERROR: Returning CURL_SOCKET_BAD to signal error");
388     }
389     else {
390       sockfd = tcp_socket->native_handle();
391       fprintf(MSG_OUT, "\nOpened socket %d", sockfd);
392
393       /* save it for monitoring */
394       socket_map.insert(std::pair<curl_socket_t,
395                         boost::asio::ip::tcp::socket *>(sockfd, tcp_socket));
396     }
397   }
398
399   return sockfd;
400 }
401
402 /* CURLOPT_CLOSESOCKETFUNCTION */
403 static int close_socket(void *clientp, curl_socket_t item)
404 {
405   fprintf(MSG_OUT, "\nclose_socket : %d", item);
406
407   std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it =
408     socket_map.find(item);
409
410   if(it != socket_map.end()) {
411     delete it->second;
412     socket_map.erase(it);
413   }
414
415   return 0;
416 }
417
418 /* Create a new easy handle, and add it to the global curl_multi */
419 static void new_conn(char *url, GlobalInfo *g)
420 {
421   ConnInfo *conn;
422   CURLMcode rc;
423
424   conn = (ConnInfo *) calloc(1, sizeof(ConnInfo));
425
426   conn->easy = curl_easy_init();
427   if(!conn->easy) {
428     fprintf(MSG_OUT, "\ncurl_easy_init() failed, exiting!");
429     exit(2);
430   }
431
432   conn->global = g;
433   conn->url = strdup(url);
434   curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url);
435   curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb);
436   curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, &conn);
437   curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L);
438   curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error);
439   curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn);
440   curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 1L);
441   curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb);
442   curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn);
443   curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_TIME, 3L);
444   curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_LIMIT, 10L);
445
446   /* call this function to get a socket */
447   curl_easy_setopt(conn->easy, CURLOPT_OPENSOCKETFUNCTION, opensocket);
448
449   /* call this function to close a socket */
450   curl_easy_setopt(conn->easy, CURLOPT_CLOSESOCKETFUNCTION, close_socket);
451
452   fprintf(MSG_OUT,
453           "\nAdding easy %p to multi %p (%s)", conn->easy, g->multi, url);
454   rc = curl_multi_add_handle(g->multi, conn->easy);
455   mcode_or_die("new_conn: curl_multi_add_handle", rc);
456
457   /* note that the add_handle() will set a time-out to trigger very soon so
458      that the necessary socket_action() call will be called by this app */
459 }
460
461 int main(int argc, char **argv)
462 {
463   GlobalInfo g;
464
465   (void)argc;
466   (void)argv;
467
468   memset(&g, 0, sizeof(GlobalInfo));
469   g.multi = curl_multi_init();
470
471   curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
472   curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
473   curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
474   curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
475
476   new_conn((char *)"www.google.com", &g);  /* add a URL */
477
478   /* enter io_service run loop */
479   io_service.run();
480
481   curl_multi_cleanup(g.multi);
482
483   fprintf(MSG_OUT, "\ndone.\n");
484
485   return 0;
486 }