62be281abb93710a5d427d52ee4f2dbbcbda8064
[oweals/gnunet.git] / src / transport / plugin_transport_http_client.c
1 /*
2      This file is part of GNUnet
3      (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file transport/plugin_transport_http_client.c
23  * @brief http transport service plugin
24  * @author Matthias Wachs
25  */
26
27 #include "plugin_transport_http.h"
28
29 #if VERBOSE_CURL
30 /**
31  * Function to log curl debug messages with GNUNET_log
32  * @param curl handle
33  * @param type curl_infotype
34  * @param data data
35  * @param size size
36  * @param cls  closure
37  * @return 0
38  */
39 static int
40 client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
41 {
42   if (type == CURLINFO_TEXT)
43   {
44     char text[size + 2];
45
46     memcpy (text, data, size);
47     if (text[size - 1] == '\n')
48       text[size] = '\0';
49     else
50     {
51       text[size] = '\n';
52       text[size + 1] = '\0';
53     }
54 #if BUILD_HTTPS
55     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https",
56                      "Client: %X - %s", cls, text);
57 #else
58     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http",
59                      "Client: %X - %s", cls, text);
60 #endif
61   }
62   return 0;
63 }
64 #endif
65
66 /**
67  * Task performing curl operations
68  * @param cls plugin as closure
69  * @param tc gnunet scheduler task context
70  */
71 static void
72 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
73
74 /**
75  * Function setting up file descriptors and scheduling task to run
76  *
77  * @param  plugin plugin as closure
78  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
79  */
80 static int
81 client_schedule (struct Plugin *plugin, int now)
82 {
83   fd_set rs;
84   fd_set ws;
85   fd_set es;
86   int max;
87   struct GNUNET_NETWORK_FDSet *grs;
88   struct GNUNET_NETWORK_FDSet *gws;
89   long to;
90   CURLMcode mret;
91   struct GNUNET_TIME_Relative timeout;
92
93   /* Cancel previous scheduled task */
94   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
95   {
96     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
97     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
98   }
99
100   max = -1;
101   FD_ZERO (&rs);
102   FD_ZERO (&ws);
103   FD_ZERO (&es);
104   mret = curl_multi_fdset (plugin->client_mh, &rs, &ws, &es, &max);
105   if (mret != CURLM_OK)
106   {
107     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
108                 "curl_multi_fdset", __FILE__, __LINE__,
109                 curl_multi_strerror (mret));
110     return GNUNET_SYSERR;
111   }
112   mret = curl_multi_timeout (plugin->client_mh, &to);
113   if (to == -1)
114     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
115   else
116     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
117   if (now == GNUNET_YES)
118     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1);
119
120   if (mret != CURLM_OK)
121   {
122     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
123                 "curl_multi_timeout", __FILE__, __LINE__,
124                 curl_multi_strerror (mret));
125     return GNUNET_SYSERR;
126   }
127
128   grs = GNUNET_NETWORK_fdset_create ();
129   gws = GNUNET_NETWORK_fdset_create ();
130   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
131   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
132
133   plugin->client_perform_task =
134       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
135                                    GNUNET_SCHEDULER_NO_TASK, timeout, grs, gws,
136                                    &client_run, plugin);
137   GNUNET_NETWORK_fdset_destroy (gws);
138   GNUNET_NETWORK_fdset_destroy (grs);
139   return GNUNET_OK;
140 }
141
142
143 int
144 client_send (struct Session *s, struct HTTP_Message *msg)
145 {
146   GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
147
148   if ((s != NULL) && (s->client_put_paused == GNUNET_YES))
149   {
150 #if VERBOSE_CLIENT
151     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
152                      "Client: %X was suspended, unpausing\n", s->client_put);
153 #endif
154     s->client_put_paused = GNUNET_NO;
155     curl_easy_pause (s->client_put, CURLPAUSE_CONT);
156   }
157
158   client_schedule (s->plugin, GNUNET_YES);
159
160   return GNUNET_OK;
161 }
162
163
164
165 /**
166  * Task performing curl operations
167  * @param cls plugin as closure
168  * @param tc gnunet scheduler task context
169  */
170 static void
171 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
172 {
173   struct Plugin *plugin = cls;
174   int running;
175   CURLMcode mret;
176
177   GNUNET_assert (cls != NULL);
178
179   plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
180   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
181     return;
182
183   do
184   {
185     running = 0;
186     mret = curl_multi_perform (plugin->client_mh, &running);
187
188     CURLMsg *msg;
189     int msgs_left;
190
191     while ((msg = curl_multi_info_read (plugin->client_mh, &msgs_left)))
192     {
193       CURL *easy_h = msg->easy_handle;
194       struct Session *s = NULL;
195       char *d = (char *) s;
196
197
198       //GNUNET_assert (easy_h != NULL);
199       if (easy_h == NULL)
200       {
201         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
202                          "Client: connection to ended with reason %i: `%s', %i handles running\n",
203                          msg->data.result,
204                          curl_easy_strerror (msg->data.result), running);
205         continue;
206       }
207
208       GNUNET_assert (CURLE_OK ==
209                      curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
210       s = (struct Session *) d;
211       GNUNET_assert (s != NULL);
212
213       if (msg->msg == CURLMSG_DONE)
214       {
215         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
216                          "Client: %X connection to '%s'  %s ended with reason %i: `%s'\n",
217                          msg->easy_handle, GNUNET_i2s (&s->target),
218                          http_plugin_address_to_string (NULL, s->addr,
219                                                         s->addrlen),
220                          msg->data.result,
221                          curl_easy_strerror (msg->data.result));
222
223         client_disconnect (s);
224         //GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,"Notifying about ended session to peer `%s' `%s'\n", GNUNET_i2s (&s->target), http_plugin_address_to_string (plugin, s->addr, s->addrlen));
225         notify_session_end (plugin, &s->target, s);
226       }
227     }
228   }
229   while (mret == CURLM_CALL_MULTI_PERFORM);
230   client_schedule (plugin, GNUNET_NO);
231 }
232
233 int
234 client_disconnect (struct Session *s)
235 {
236   int res = GNUNET_OK;
237   CURLMcode mret;
238   struct Plugin *plugin = s->plugin;
239   struct HTTP_Message *msg;
240   struct HTTP_Message *t;
241
242
243
244   if (s->client_put != NULL)
245   {
246 #if DEBUG_HTTP
247     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
248                      "Client: %X Deleting outbound PUT session to peer `%s'\n",
249                      s->client_put, GNUNET_i2s (&s->target));
250 #endif
251
252     mret = curl_multi_remove_handle (plugin->client_mh, s->client_put);
253     if (mret != CURLM_OK)
254     {
255       curl_easy_cleanup (s->client_put);
256       res = GNUNET_SYSERR;
257       GNUNET_break (0);
258     }
259     curl_easy_cleanup (s->client_put);
260     s->client_put = NULL;
261   }
262
263
264   if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
265   {
266     GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
267     s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
268   }
269
270   if (s->client_get != NULL)
271   {
272 #if DEBUG_HTTP
273     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
274                      "Client: %X Deleting outbound GET session to peer `%s'\n",
275                      s->client_get, GNUNET_i2s (&s->target));
276 #endif
277
278     mret = curl_multi_remove_handle (plugin->client_mh, s->client_get);
279     if (mret != CURLM_OK)
280     {
281       curl_easy_cleanup (s->client_get);
282       res = GNUNET_SYSERR;
283       GNUNET_break (0);
284     }
285     curl_easy_cleanup (s->client_get);
286     s->client_get = NULL;
287   }
288
289   msg = s->msg_head;
290   while (msg != NULL)
291   {
292     t = msg->next;
293     if (NULL != msg->transmit_cont)
294       msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
295     GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
296     GNUNET_free (msg);
297     msg = t;
298   }
299
300   plugin->cur_connections -= 2;
301   /* Re-schedule since handles have changed */
302   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
303   {
304     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
305     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
306   }
307
308   client_schedule (plugin, GNUNET_YES);
309
310   return res;
311 }
312
313 static void
314 client_receive_mst_cb (void *cls, void *client,
315                        const struct GNUNET_MessageHeader *message)
316 {
317   struct Session *s = cls;
318   struct GNUNET_TIME_Relative delay;
319
320   delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
321   s->next_receive =
322       GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
323
324   if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
325   {
326 #if VERBOSE_CLIENT
327     struct Plugin *plugin = s->plugin;
328
329     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
330                      "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
331                      GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
332                      delay);
333 #endif
334   }
335 }
336
337 static void
338 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
339 {
340   struct Session *s = cls;
341
342   s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
343
344   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
345     return;
346
347   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
348                    "Client: %X Waking up receive handle\n", s->client_get);
349
350   if (s->client_get != NULL)
351     curl_easy_pause (s->client_get, CURLPAUSE_CONT);
352
353 }
354
355 /**
356 * Callback method used with libcurl
357 * Method is called when libcurl needs to write data during sending
358 * @param stream pointer where to write data
359 * @param size size of an individual element
360 * @param nmemb count of elements that can be written to the buffer
361 * @param ptr destination pointer, passed to the libcurl handle
362 * @return bytes read from stream
363 */
364 static size_t
365 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
366 {
367   struct Session *s = cls;
368   struct GNUNET_TIME_Absolute now;
369   size_t len = size * nmemb;
370
371
372 #if VERBOSE_CLIENT
373   struct Plugin *plugin = s->plugin;
374
375   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
376                    "Client: Received %Zu bytes from peer `%s'\n", len,
377                    GNUNET_i2s (&s->target));
378 #endif
379
380   now = GNUNET_TIME_absolute_get ();
381   if (now.abs_value < s->next_receive.abs_value)
382   {
383     struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
384     struct GNUNET_TIME_Relative delta =
385         GNUNET_TIME_absolute_get_difference (now, s->next_receive);
386 #if DEBUG_CLIENT
387     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
388                      "Client: %X No inbound bandwidth available! Next read was delayed for %llu ms\n",
389                      s->client_get, delta.rel_value);
390 #endif
391     if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
392     {
393       GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
394       s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
395     }
396     s->recv_wakeup_task =
397         GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
398     return CURLPAUSE_ALL;
399   }
400
401
402   if (s->msg_tk == NULL)
403     s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
404
405   GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
406
407   return len;
408 }
409
410 /**
411  * Callback method used with libcurl
412  * Method is called when libcurl needs to read data during sending
413  * @param stream pointer where to write data
414  * @param size size of an individual element
415  * @param nmemb count of elements that can be written to the buffer
416  * @param ptr source pointer, passed to the libcurl handle
417  * @return bytes written to stream, returning 0 will terminate connection!
418  */
419 static size_t
420 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
421 {
422   struct Session *s = cls;
423
424 #if VERBOSE_CLIENT
425   struct Plugin *plugin = s->plugin;
426 #endif
427   size_t bytes_sent = 0;
428   size_t len;
429
430   struct HTTP_Message *msg = s->msg_head;
431
432   if (msg == NULL)
433   {
434 #if VERBOSE_CLIENT
435     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
436                      "Client: %X Nothing to send! Suspending PUT handle!\n",
437                      s->client_put);
438 #endif
439     s->client_put_paused = GNUNET_YES;
440     return CURL_READFUNC_PAUSE;
441   }
442
443   GNUNET_assert (msg != NULL);
444   /* data to send */
445   if (msg->pos < msg->size)
446   {
447     /* data fit in buffer */
448     if ((msg->size - msg->pos) <= (size * nmemb))
449     {
450       len = (msg->size - msg->pos);
451       memcpy (stream, &msg->buf[msg->pos], len);
452       msg->pos += len;
453       bytes_sent = len;
454     }
455     else
456     {
457       len = size * nmemb;
458       memcpy (stream, &msg->buf[msg->pos], len);
459       msg->pos += len;
460       bytes_sent = len;
461     }
462   }
463   /* no data to send */
464   else
465   {
466     GNUNET_assert (0);
467     bytes_sent = 0;
468   }
469
470   if (msg->pos == msg->size)
471   {
472 #if VERBOSE_CLIENT
473     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
474                      "Client: %X Message with %u bytes sent, removing message from queue\n",
475                      s->client_put, msg->size, msg->pos);
476 #endif
477     /* Calling transmit continuation  */
478     if (NULL != msg->transmit_cont)
479       msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
480     GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
481     GNUNET_free (msg);
482   }
483   return bytes_sent;
484 }
485
486 int
487 client_connect (struct Session *s)
488 {
489   struct Plugin *plugin = s->plugin;
490   int res = GNUNET_OK;
491   char *url;
492   CURLMcode mret;
493
494 #if VERBOSE_CLIENT
495   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
496                    "Initiating outbound session peer `%s'\n",
497                    GNUNET_i2s (&s->target));
498 #endif
499
500   s->inbound = GNUNET_NO;
501
502   plugin->last_tag++;
503   /* create url */
504   GNUNET_asprintf (&url, "%s%s;%u",
505                    http_plugin_address_to_string (plugin, s->addr, s->addrlen),
506                    GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
507                    plugin->last_tag);
508 #if 0
509   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "URL `%s'\n", url);
510 #endif
511   /* create get connection */
512   s->client_get = curl_easy_init ();
513 #if VERBOSE_CURL
514   curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
515   curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
516   curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
517 #endif
518 #if BUILD_HTTPS
519   curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
520   curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
521   curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
522 #endif
523   curl_easy_setopt (s->client_get, CURLOPT_URL, url);
524   //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
525   //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
526   curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
527   curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
528   curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
529   curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
530   curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
531                     (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
532   curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
533   curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
534                     (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
535   curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
536                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
537 #if CURL_TCP_NODELAY
538   curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
539 #endif
540
541   /* create put connection */
542   s->client_put = curl_easy_init ();
543 #if VERBOSE_CURL
544   curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
545   curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
546   curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
547 #endif
548 #if BUILD_HTTPS
549   curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
550   curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
551   curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
552 #endif
553   curl_easy_setopt (s->client_put, CURLOPT_URL, url);
554   curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
555   //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
556   //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
557   curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
558   curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
559   curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
560   curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
561   curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
562                     (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
563   curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
564   curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
565                     (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
566   curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
567                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
568 #if CURL_TCP_NODELAY
569   curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
570 #endif
571
572   GNUNET_free (url);
573
574   mret = curl_multi_add_handle (plugin->client_mh, s->client_get);
575   if (mret != CURLM_OK)
576   {
577     curl_easy_cleanup (s->client_get);
578     res = GNUNET_SYSERR;
579     GNUNET_break (0);
580   }
581
582   mret = curl_multi_add_handle (plugin->client_mh, s->client_put);
583   if (mret != CURLM_OK)
584   {
585     curl_multi_remove_handle (plugin->client_mh, s->client_get);
586     curl_easy_cleanup (s->client_get);
587     curl_easy_cleanup (s->client_put);
588     res = GNUNET_SYSERR;
589     GNUNET_break (0);
590   }
591
592   /* Perform connect */
593   plugin->cur_connections += 2;
594
595   /* Re-schedule since handles have changed */
596   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
597   {
598     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
599     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
600   }
601   plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
602
603   return res;
604 }
605
606 int
607 client_start (struct Plugin *plugin)
608 {
609   int res = GNUNET_OK;
610
611   curl_global_init (CURL_GLOBAL_ALL);
612   plugin->client_mh = curl_multi_init ();
613
614   if (NULL == plugin->client_mh)
615   {
616     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
617                      _
618                      ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
619                      plugin->name);
620     res = GNUNET_SYSERR;
621   }
622   return res;
623 }
624
625 void
626 client_stop (struct Plugin *plugin)
627 {
628   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
629   {
630     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
631     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
632   }
633
634   curl_multi_cleanup (plugin->client_mh);
635   curl_global_cleanup ();
636 }
637
638
639
640 /* end of plugin_transport_http_client.c */