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