-test
[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/S client transport plugin
24  * @author Matthias Wachs
25  */
26
27 #if BUILD_HTTPS
28 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
29 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
30 #else
31 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
32 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
33 #endif
34
35 #define VERBOSE_CURL GNUNET_NO
36
37 #include "platform.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_common.h"
40 #include "gnunet_server_lib.h"
41 #include "gnunet_transport_plugin.h"
42 #include "plugin_transport_http_common.h"
43 #include <curl/curl.h>
44
45
46 /**
47  * Encapsulation of all of the state of the plugin.
48  */
49 struct HTTP_Client_Plugin;
50
51
52 /**
53  *  Message to send using http
54  */
55 struct HTTP_Message
56 {
57   /**
58    * next pointer for double linked list
59    */
60   struct HTTP_Message *next;
61
62   /**
63    * previous pointer for double linked list
64    */
65   struct HTTP_Message *prev;
66
67   /**
68    * buffer containing data to send
69    */
70   char *buf;
71
72   /**
73    * amount of data already sent
74    */
75   size_t pos;
76
77   /**
78    * buffer length
79    */
80   size_t size;
81
82   /**
83    * Continuation function to call once the transmission buffer
84    * has again space available.  NULL if there is no
85    * continuation to call.
86    */
87   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
88
89   /**
90    * Closure for transmit_cont.
91    */
92   void *transmit_cont_cls;
93 };
94
95
96 /**
97  * Session handle for connections.
98  */
99 struct Session
100 {
101   /**
102    * To whom are we talking to (set to our identity
103    * if we are still waiting for the welcome message)
104    */
105   struct GNUNET_PeerIdentity target;
106
107   /**
108    * Stored in a linked list.
109    */
110   struct Session *next;
111
112   /**
113    * Stored in a linked list.
114    */
115   struct Session *prev;
116
117   /**
118    * Address
119    */
120   void *addr;
121
122   /**
123    * Address length
124    */
125   size_t addrlen;
126
127   /**
128    * ATS network type in NBO
129    */
130   uint32_t ats_address_network_type;
131
132   /**
133    * Pointer to the global plugin struct.
134    */
135   struct HTTP_Client_Plugin *plugin;
136
137   /**
138    * Is client send handle paused since there are no data to send?
139    * GNUNET_YES/NO
140    */
141   int client_put_paused;
142
143   /**
144    * Was session given to transport service?
145    */
146  // int session_passed;
147
148   /**
149    * Client send handle
150    */
151   void *client_put;
152
153   /**
154    * Client receive handle
155    */
156   void *client_get;
157
158   /**
159    * next pointer for double linked list
160    */
161   struct HTTP_Message *msg_head;
162
163   /**
164    * previous pointer for double linked list
165    */
166   struct HTTP_Message *msg_tail;
167
168   /**
169    * Message stream tokenizer for incoming data
170    */
171   struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
172
173   /**
174    * Session timeout task
175    */
176   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
177
178   /**
179    * Task to wake up client receive handle when receiving is allowed again
180    */
181   GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
182
183   /**
184   * Absolute time when to receive data again
185   * Used for receive throttling
186   */
187  struct GNUNET_TIME_Absolute next_receive;
188 };
189
190
191 /**
192  * Encapsulation of all of the state of the plugin.
193  */
194 struct HTTP_Client_Plugin
195 {
196   /**
197    * Our environment.
198    */
199   struct GNUNET_TRANSPORT_PluginEnvironment *env;
200
201   /**
202    * Linked list head of open sessions.
203    */
204   struct Session *head;
205
206   /**
207    * Linked list tail of open sessions.
208    */
209   struct Session *tail;
210
211   /**
212    * Plugin name
213    */
214   char *name;
215
216   /**
217    * Protocol
218    */
219   char *protocol;
220
221   /**
222    * Maximum number of sockets the plugin can use
223    * Each http inbound /outbound connections are two connections
224    */
225   unsigned int max_connections;
226
227   /**
228    * Current number of sockets the plugin can use
229    * Each http inbound /outbound connections are two connections
230    */
231   unsigned int cur_connections;
232
233   /**
234    * Last used unique HTTP connection tag
235    */
236   uint32_t last_tag;
237
238   /**
239    * use IPv6
240    */
241   uint16_t use_ipv6;
242
243   /**
244    * use IPv4
245    */
246   uint16_t use_ipv4;
247
248   /**
249    * cURL Multihandle
250    */
251   CURLM *curl_multi_handle;
252
253   /**
254    * curl perform task
255    */
256   GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
257 };
258
259
260 /**
261  * Encapsulation of all of the state of the plugin.
262  */
263 struct HTTP_Client_Plugin *p;
264
265
266 /**
267  * Start session timeout for a session
268  * @param s the session
269  */
270 static void
271 client_start_session_timeout (struct Session *s);
272
273
274 /**
275  * Increment session timeout due to activity for a session
276  * @param s the session
277  */
278 static void
279 client_reschedule_session_timeout (struct Session *s);
280
281
282 /**
283  * Cancel timeout for a session
284  * @param s the session
285  */
286 static void
287 client_stop_session_timeout (struct Session *s);
288
289
290 /**
291  * Function setting up file descriptors and scheduling task to run
292  *
293  * @param  plugin plugin as closure
294  * @param now schedule task in 1ms, regardless of what curl may say
295  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
296  */
297 static int
298 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
299
300
301 /**
302  * Does a session s exists?
303  *
304  * @param plugin the plugin
305  * @param s desired session
306  * @return GNUNET_YES or GNUNET_NO
307  */
308 static int
309 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
310 {
311   struct Session * head;
312
313   GNUNET_assert (NULL != plugin);
314   GNUNET_assert (NULL != s);
315
316   for (head = plugin->head; head != NULL; head = head->next)
317   {
318     if (head == s)
319       return GNUNET_YES;
320   }
321   return GNUNET_NO;
322 }
323
324 #if VERBOSE_CURL
325 /**
326  * Function to log curl debug messages with GNUNET_log
327  *
328  * @param curl handle
329  * @param type curl_infotype
330  * @param data data
331  * @param size size
332  * @param cls  closure
333  * @return 0
334  */
335 static int
336 client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
337 {
338   char *ttype;
339   if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
340   {
341     char text[size + 2];
342
343     switch (type) {
344       case CURLINFO_TEXT:
345         ttype = "TEXT";
346         break;
347       case CURLINFO_HEADER_IN:
348         ttype = "HEADER_IN";
349         break;
350       case CURLINFO_HEADER_OUT:
351         ttype = "HEADER_OUT";
352         break;
353       default:
354         break;
355     }
356
357     memcpy (text, data, size);
358     if (text[size - 1] == '\n')
359       text[size] = '\0';
360     else
361     {
362       text[size] = '\n';
363       text[size + 1] = '\0';
364     }
365 #if BUILD_HTTPS
366     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
367                      "Connection %p %s: %s", cls, ttype, text);
368 #else
369     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
370                      "Connection %p %s: %s", cls, ttype, text);
371 #endif
372   }
373   return 0;
374 }
375 #endif
376
377
378 /**
379  * Function that can be used by the transport service to transmit
380  * a message using the plugin.   Note that in the case of a
381  * peer disconnecting, the continuation MUST be called
382  * prior to the disconnect notification itself.  This function
383  * will be called with this peer's HELLO message to initiate
384  * a fresh connection to another peer.
385  *
386  * @param cls closure
387  * @param session which session must be used
388  * @param msgbuf the message to transmit
389  * @param msgbuf_size number of bytes in 'msgbuf'
390  * @param priority how important is the message (most plugins will
391  *                 ignore message priority and just FIFO)
392  * @param to how long to wait at most for the transmission (does not
393  *                require plugins to discard the message after the timeout,
394  *                just advisory for the desired delay; most plugins will ignore
395  *                this as well)
396  * @param cont continuation to call once the message has
397  *        been transmitted (or if the transport is ready
398  *        for the next transmission call; or if the
399  *        peer disconnected...); can be NULL
400  * @param cont_cls closure for cont
401  * @return number of bytes used (on the physical network, with overheads);
402  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
403  *         and does NOT mean that the message was not transmitted (DV)
404  */
405 static ssize_t
406 http_client_plugin_send (void *cls,
407                   struct Session *session,
408                   const char *msgbuf, size_t msgbuf_size,
409                   unsigned int priority,
410                   struct GNUNET_TIME_Relative to,
411                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
412 {
413   struct HTTP_Client_Plugin *plugin = cls;
414   struct HTTP_Message *msg;
415
416   GNUNET_assert (plugin != NULL);
417   GNUNET_assert (session != NULL);
418
419   /* lookup if session is really existing */
420   if (GNUNET_YES != client_exist_session (plugin, session))
421   {
422     GNUNET_break (0);
423     return GNUNET_SYSERR;
424   }
425
426   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
427                    "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
428                    session, session->client_put,
429                    msgbuf_size, GNUNET_i2s (&session->target));
430
431   /* create new message and schedule */
432   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
433   msg->next = NULL;
434   msg->size = msgbuf_size;
435   msg->pos = 0;
436   msg->buf = (char *) &msg[1];
437   msg->transmit_cont = cont;
438   msg->transmit_cont_cls = cont_cls;
439   memcpy (msg->buf, msgbuf, msgbuf_size);
440   GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
441
442   if (session->client_put_paused == GNUNET_YES)
443   {
444     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
445                      "Session %p/connection %p: unpausing connection\n",
446                      session, session->client_put);
447     session->client_put_paused = GNUNET_NO;
448     curl_easy_pause (session->client_put, CURLPAUSE_CONT);
449   }
450   client_schedule (session->plugin, GNUNET_YES);
451   client_reschedule_session_timeout (session);
452   return msgbuf_size;
453 }
454
455
456 /**
457  * Delete session s
458  *
459  * @param s the session to delete
460  */
461 static void
462 client_delete_session (struct Session *s)
463 {
464   struct HTTP_Client_Plugin *plugin = s->plugin;
465   struct HTTP_Message *pos;
466   struct HTTP_Message *next;
467
468   client_stop_session_timeout (s);
469
470   GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
471
472   next = s->msg_head;
473   while (NULL != (pos = next))
474   {
475     next = pos->next;
476     GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
477     if (pos->transmit_cont != NULL)
478       pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR);
479     GNUNET_free (pos);
480   }
481
482   if (s->msg_tk != NULL)
483   {
484     GNUNET_SERVER_mst_destroy (s->msg_tk);
485     s->msg_tk = NULL;
486   }
487   GNUNET_free (s->addr);
488   GNUNET_free (s);
489 }
490
491
492
493 /**
494  * Disconnect a session
495  *
496  * @param s session
497  * @return GNUNET_OK on success, GNUNET_SYSERR on error
498  */
499 static int
500 client_disconnect (struct Session *s)
501 {
502   struct HTTP_Client_Plugin *plugin = s->plugin;
503   struct HTTP_Message *msg;
504   struct HTTP_Message *t;
505   int res = GNUNET_OK;
506   CURLMcode mret;
507
508   if (GNUNET_YES != client_exist_session (plugin, s))
509   {
510     GNUNET_break (0);
511     return GNUNET_SYSERR;
512   }
513
514   if (s->client_put != NULL)
515   {
516     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
517                      "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
518                      s, s->client_put, GNUNET_i2s (&s->target));
519
520     /* remove curl handle from multi handle */
521     mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
522     if (mret != CURLM_OK)
523     {
524       /* clean up easy handle, handle is now invalid and free'd */
525       res = GNUNET_SYSERR;
526       GNUNET_break (0);
527     }
528     curl_easy_cleanup (s->client_put);
529     s->client_put = NULL;
530   }
531
532
533   if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
534   {
535     GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
536     s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
537   }
538
539   if (s->client_get != NULL)
540   {
541       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
542                        "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
543                        s, s->client_get, GNUNET_i2s (&s->target));
544
545     /* remove curl handle from multi handle */
546     mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
547     if (mret != CURLM_OK)
548     {
549       /* clean up easy handle, handle is now invalid and free'd */
550       res = GNUNET_SYSERR;
551       GNUNET_break (0);
552     }
553     curl_easy_cleanup (s->client_get);
554     s->client_get = NULL;
555   }
556
557   msg = s->msg_head;
558   while (msg != NULL)
559   {
560     t = msg->next;
561     if (NULL != msg->transmit_cont)
562       msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
563     GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
564     GNUNET_free (msg);
565     msg = t;
566   }
567
568   GNUNET_assert (plugin->cur_connections >= 2);
569   plugin->cur_connections -= 2;
570   GNUNET_STATISTICS_set (plugin->env->stats,
571       "# HTTP client sessions",
572       plugin->cur_connections,
573       GNUNET_NO);
574
575   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
576                    "Session %p: notifying transport about ending session\n",s);
577
578   plugin->env->session_end (plugin->env->cls, &s->target, s);
579   client_delete_session (s);
580
581   /* Re-schedule since handles have changed */
582   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
583   {
584     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
585     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
586   }
587   client_schedule (plugin, GNUNET_YES);
588
589   return res;
590 }
591
592
593 /**
594  * Function that can be used to force the plugin to disconnect
595  * from the given peer and cancel all previous transmissions
596  * (and their continuationc).
597  *
598  * @param cls closure
599  * @param target peer from which to disconnect
600  */
601 static void
602 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
603 {
604   struct HTTP_Client_Plugin *plugin = cls;
605   struct Session *next = NULL;
606   struct Session *pos = NULL;
607
608   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
609                    "Transport tells me to disconnect `%s'\n",
610                    GNUNET_i2s (target));
611
612   next = plugin->head;
613   while (NULL != (pos = next))
614   {
615     next = pos->next;
616     if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
617     {
618       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
619                        "Disconnecting session %p to `%pos'\n",
620                        pos, GNUNET_i2s (target));
621       GNUNET_assert (GNUNET_OK == client_disconnect (pos));
622     }
623   }
624
625 }
626
627
628 static struct Session *
629 client_lookup_session (struct HTTP_Client_Plugin *plugin,
630                        const struct GNUNET_HELLO_Address *address)
631 {
632   struct Session *pos;
633
634   for (pos = plugin->head; NULL != pos; pos = pos->next)
635     if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
636         (address->address_length == pos->addrlen) &&
637         (0 == memcmp (address->address, pos->addr, pos->addrlen)))
638       return pos;
639   return NULL;
640 }
641
642
643 /**
644  * Callback method used with libcurl
645  * Method is called when libcurl needs to read data during sending
646  *
647  * @param stream pointer where to write data
648  * @param size size of an individual element
649  * @param nmemb count of elements that can be written to the buffer
650  * @param cls source pointer, passed to the libcurl handle
651  * @return bytes written to stream, returning 0 will terminate connection!
652  */
653 static size_t
654 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
655 {
656   struct Session *s = cls;
657   struct HTTP_Client_Plugin *plugin = s->plugin;
658   struct HTTP_Message *msg = s->msg_head;
659   size_t len;
660
661   if (GNUNET_YES != client_exist_session (plugin, s))
662   {
663     GNUNET_break (0);
664     return 0;
665   }
666   if (NULL == msg)
667   {
668     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
669                      "Session %p/connection %p: nothing to send, suspending\n",
670                      s, s->client_put);
671     s->client_put_paused = GNUNET_YES;
672     return CURL_READFUNC_PAUSE;
673   }
674   /* data to send */
675   GNUNET_assert (msg->pos < msg->size);
676   /* calculate how much fits in buffer */
677   len = GNUNET_MIN (msg->size - msg->pos,
678                     size * nmemb);
679   memcpy (stream, &msg->buf[msg->pos], len);
680   msg->pos += len;
681   if (msg->pos == msg->size)
682   {
683     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
684                      "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
685                      s, s->client_put, msg->size, msg->pos);
686     /* Calling transmit continuation  */
687     GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
688     if (NULL != msg->transmit_cont)
689       msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
690     GNUNET_free (msg);
691   }
692   client_reschedule_session_timeout (s);
693   return len;
694 }
695
696
697 /**
698  * Wake up a curl handle which was suspended
699  *
700  * @param cls the session
701  * @param tc task context
702  */
703 static void
704 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
705 {
706   struct Session *s = cls;
707
708   if (GNUNET_YES != client_exist_session(p, s))
709   {
710     GNUNET_break (0);
711     return;
712   }
713   s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
714   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
715     return;
716   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
717                    "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
718   if (s->client_get != NULL)
719     curl_easy_pause (s->client_get, CURLPAUSE_CONT);
720 }
721
722
723 /**
724  * Callback for message stream tokenixer
725  *
726  * @param cls the session
727  * @param client not used
728  * @param message the message received
729  * @return always GNUNET_OK
730  */
731 static int
732 client_receive_mst_cb (void *cls, void *client,
733                        const struct GNUNET_MessageHeader *message)
734 {
735   struct Session *s = cls;
736   struct HTTP_Client_Plugin *plugin;
737   struct GNUNET_TIME_Relative delay;
738   struct GNUNET_ATS_Information atsi[2];
739
740   if (GNUNET_YES != client_exist_session(p, s))
741   {
742     GNUNET_break (0);
743     return GNUNET_OK;
744   }
745   plugin = s->plugin;
746
747
748   atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
749   atsi[0].value = htonl (1);
750   atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
751   atsi[1].value = s->ats_address_network_type;
752   GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
753
754   delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
755                                    (const struct GNUNET_ATS_Information *) &atsi, 2,
756                                    s, s->addr, s->addrlen);
757   s->next_receive =
758       GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
759
760   if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
761   {
762
763     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
764                      "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
765                      GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
766                      delay);
767   }
768   client_reschedule_session_timeout (s);
769   return GNUNET_OK;
770 }
771
772
773
774 /**
775  * Callback method used with libcurl
776  * Method is called when libcurl needs to write data during sending
777  *
778  * @param stream pointer where to write data
779  * @param size size of an individual element
780  * @param nmemb count of elements that can be written to the buffer
781  * @param cls destination pointer, passed to the libcurl handle
782  * @return bytes read from stream
783  */
784 static size_t
785 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
786 {
787   struct Session *s = cls;
788   struct GNUNET_TIME_Absolute now;
789   size_t len = size * nmemb;
790   struct HTTP_Client_Plugin *plugin = s->plugin;
791
792   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
793                    "Received %u bytes from peer `%s'\n", len,
794                    GNUNET_i2s (&s->target));
795   now = GNUNET_TIME_absolute_get ();
796   if (now.abs_value < s->next_receive.abs_value)
797   {
798     struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
799     struct GNUNET_TIME_Relative delta =
800         GNUNET_TIME_absolute_get_difference (now, s->next_receive);
801     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
802                      "No inbound bandwidth for session %p available! Next read was delayed for %llu ms\n",
803                      s, delta.rel_value);
804     if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
805     {
806       GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
807       s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
808     }
809     s->recv_wakeup_task =
810         GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
811     return CURLPAUSE_ALL;
812   }
813   if (NULL == s->msg_tk)
814     s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
815   GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
816   return len;
817
818 }
819
820
821 /**
822  * Task performing curl operations
823  *
824  * @param cls plugin as closure
825  * @param tc gnunet scheduler task context
826  */
827 static void
828 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
829
830
831 /**
832  * Function setting up file descriptors and scheduling task to run
833  *
834  * @param  plugin plugin as closure
835  * @param now schedule task in 1ms, regardless of what curl may say
836  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
837  */
838 static int
839 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
840 {
841   fd_set rs;
842   fd_set ws;
843   fd_set es;
844   int max;
845   struct GNUNET_NETWORK_FDSet *grs;
846   struct GNUNET_NETWORK_FDSet *gws;
847   long to;
848   CURLMcode mret;
849   struct GNUNET_TIME_Relative timeout;
850
851   /* Cancel previous scheduled task */
852   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
853   {
854     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
855     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
856   }
857   max = -1;
858   FD_ZERO (&rs);
859   FD_ZERO (&ws);
860   FD_ZERO (&es);
861   mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
862   if (mret != CURLM_OK)
863   {
864     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
865                 "curl_multi_fdset", __FILE__, __LINE__,
866                 curl_multi_strerror (mret));
867     return GNUNET_SYSERR;
868   }
869   mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
870   if (to == -1)
871     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
872   else
873     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
874   if (now == GNUNET_YES)
875     timeout = GNUNET_TIME_UNIT_MILLISECONDS;
876
877   if (mret != CURLM_OK)
878   {
879     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
880                 "curl_multi_timeout", __FILE__, __LINE__,
881                 curl_multi_strerror (mret));
882     return GNUNET_SYSERR;
883   }
884
885   grs = GNUNET_NETWORK_fdset_create ();
886   gws = GNUNET_NETWORK_fdset_create ();
887   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
888   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
889
890   plugin->client_perform_task =
891       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
892                                    timeout, grs, gws,
893                                    &client_run, plugin);
894   GNUNET_NETWORK_fdset_destroy (gws);
895   GNUNET_NETWORK_fdset_destroy (grs);
896   return GNUNET_OK;
897 }
898
899
900 /**
901  * Task performing curl operations
902  *
903  * @param cls plugin as closure
904  * @param tc gnunet scheduler task context
905  */
906 static void
907 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
908 {
909   struct HTTP_Client_Plugin *plugin = cls;
910   int running;
911   CURLMcode mret;
912
913   GNUNET_assert (cls != NULL);
914
915   plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
916   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
917     return;
918
919   do
920   {
921     running = 0;
922     mret = curl_multi_perform (plugin->curl_multi_handle, &running);
923
924     CURLMsg *msg;
925     int msgs_left;
926
927     while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
928     {
929       CURL *easy_h = msg->easy_handle;
930       struct Session *s = NULL;
931       char *d = (char *) s;
932
933       if (easy_h == NULL)
934       {
935         GNUNET_break (0);
936         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
937                          "Client: connection to ended with reason %i: `%s', %i handles running\n",
938                          msg->data.result,
939                          curl_easy_strerror (msg->data.result), running);
940         continue;
941       }
942
943       GNUNET_assert (CURLE_OK ==
944                      curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
945       s = (struct Session *) d;
946
947       if (GNUNET_YES != client_exist_session(plugin, s))
948       {
949         GNUNET_break (0);
950         return;
951       }
952
953       GNUNET_assert (s != NULL);
954       if (msg->msg == CURLMSG_DONE)
955       {
956         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
957                          "Session %p/connection %p: connection to `%s' ended with reason %i: `%s'\n",
958                          s, msg->easy_handle, GNUNET_i2s (&s->target),
959                          msg->data.result,
960                          curl_easy_strerror (msg->data.result));
961
962         /* Disconnect other transmission direction and tell transport */
963         client_disconnect (s);
964       }
965     }
966   }
967   while (mret == CURLM_CALL_MULTI_PERFORM);
968   client_schedule (plugin, GNUNET_NO);
969 }
970
971 static int
972 client_connect (struct Session *s)
973 {
974
975   struct HTTP_Client_Plugin *plugin = s->plugin;
976   int res = GNUNET_OK;
977   char *url;
978   CURLMcode mret;
979
980   /* create url */
981   if (NULL == http_common_plugin_address_to_string (NULL, s->addr, s->addrlen))
982   {
983     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
984                      "Invalid address peer `%s'\n",
985                      GNUNET_i2s (&s->target));
986     return GNUNET_SYSERR;
987   }
988
989   GNUNET_asprintf (&url, "%s/%s;%u",
990       http_common_plugin_address_to_string (plugin, s->addr, s->addrlen),
991                    GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
992                    plugin->last_tag);
993   plugin->last_tag++;
994   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
995                    "Initiating outbound session peer `%s' using address `%s'\n",
996                    GNUNET_i2s (&s->target), url);
997
998   /* create get connection */
999   s->client_get = curl_easy_init ();
1000 #if VERBOSE_CURL
1001   curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1002   curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1003   curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
1004 #endif
1005 #if BUILD_HTTPS
1006   curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1007   curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1008   curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1009 #endif
1010   curl_easy_setopt (s->client_get, CURLOPT_URL, url);
1011   //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1012   //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1013   curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1014   curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1015   curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1016   curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1017   /* No timeout by default, timeout done with session timeout */
1018   curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1019   curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1020   curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1021                     (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1022   curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1023                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1024 #if CURL_TCP_NODELAY
1025   curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1026 #endif
1027
1028   /* create put connection */
1029   s->client_put = curl_easy_init ();
1030 #if VERBOSE_CURL
1031   curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1032   curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1033   curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
1034 #endif
1035 #if BUILD_HTTPS
1036   curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1037   curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1038   curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1039 #endif
1040   curl_easy_setopt (s->client_put, CURLOPT_URL, url);
1041   curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1042   /*
1043   struct curl_slist *m_headerlist;
1044   m_headerlist = NULL;
1045   m_headerlist = curl_slist_append(m_headerlist, "Transfer-Encoding: chunked");
1046   curl_easy_setopt(s->client_put, CURLOPT_HTTPHEADER, m_headerlist);*/
1047   //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1048   //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1049   curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1050   curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1051   curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
1052   curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1053   /* No timeout by default, timeout done with session timeout */
1054   curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1055   curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1056   curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1057                     (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1058   curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1059                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1060 #if CURL_TCP_NODELAY
1061   curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1062 #endif
1063   GNUNET_free (url);
1064
1065   mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_get);
1066   if (mret != CURLM_OK)
1067   {
1068     curl_easy_cleanup (s->client_get);
1069     GNUNET_break (0);
1070     return GNUNET_SYSERR;
1071   }
1072
1073   mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_put);
1074   if (mret != CURLM_OK)
1075   {
1076     curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
1077     curl_easy_cleanup (s->client_get);
1078     curl_easy_cleanup (s->client_put);
1079     GNUNET_break (0);
1080     return GNUNET_SYSERR;
1081   }
1082
1083   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1084                    "Session %p: connected with connections GET %p and PUT %p\n",
1085                    s, s->client_get, s->client_put);
1086
1087   /* Perform connect */
1088   plugin->cur_connections += 2;
1089   GNUNET_STATISTICS_set (plugin->env->stats,
1090       "# HTTP client connections",
1091       plugin->cur_connections,
1092       GNUNET_NO);
1093
1094   /* Re-schedule since handles have changed */
1095   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1096   {
1097     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1098     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1099   }
1100   plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1101   return res;
1102 }
1103
1104
1105 /**
1106  * Creates a new outbound session the transport service will use to send data to the
1107  * peer
1108  *
1109  * @param cls the plugin
1110  * @param address the address
1111  * @return the session or NULL of max connections exceeded
1112  */
1113 static struct Session *
1114 http_client_plugin_get_session (void *cls,
1115                   const struct GNUNET_HELLO_Address *address)
1116 {
1117   struct HTTP_Client_Plugin *plugin = cls;
1118   struct Session * s = NULL;
1119   struct sockaddr *sa;
1120   struct GNUNET_ATS_Information ats;
1121   size_t salen = 0;
1122   int res;
1123
1124   GNUNET_assert (plugin != NULL);
1125   GNUNET_assert (address != NULL);
1126   GNUNET_assert (address->address != NULL);
1127
1128   /* find existing session */
1129   s = client_lookup_session (plugin, address);
1130   if (s != NULL)
1131     return s;
1132
1133   if (plugin->max_connections <= plugin->cur_connections)
1134   {
1135     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1136                      "Maximum number of connections (%u) reached: "
1137                      "cannot connect to peer `%s'\n",
1138                      plugin->max_connections,
1139                      GNUNET_i2s (&address->peer));
1140     return NULL;
1141   }
1142
1143   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1144   ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1145   sa = http_common_socket_from_address (address->address, address->address_length, &res);
1146
1147   if (GNUNET_SYSERR == res)
1148   {
1149       return NULL;
1150   }
1151   else if (GNUNET_YES == res)
1152   {
1153       GNUNET_assert (NULL != sa);
1154       if (AF_INET == sa->sa_family)
1155       {
1156           salen = sizeof (struct sockaddr_in);
1157       }
1158       else if (AF_INET == sa->sa_family)
1159       {
1160           salen = sizeof (struct sockaddr_in6);
1161       }
1162       ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1163       GNUNET_free (sa);
1164   }
1165   else if (GNUNET_NO == res)
1166   {
1167       ats.value = htonl (GNUNET_ATS_COST_WAN);
1168   }
1169
1170   if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1171   {
1172       GNUNET_break (0);
1173       return NULL;
1174   }
1175
1176   s = GNUNET_malloc (sizeof (struct Session));
1177   memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1178   s->plugin = plugin;
1179   s->addr = GNUNET_malloc (address->address_length);
1180   memcpy (s->addr, address->address, address->address_length);
1181   s->addrlen = address->address_length;
1182   s->ats_address_network_type = ats.value;
1183
1184   client_start_session_timeout (s);
1185
1186   /* add new session */
1187   GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1188
1189   /* initiate new connection */
1190   if (GNUNET_SYSERR == client_connect (s))
1191   {
1192     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1193                      "Cannot connect to peer `%s' address `%s''\n",
1194                      http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1195                      GNUNET_i2s (&s->target));
1196     client_delete_session (s);
1197     return NULL;
1198   }
1199   return s;
1200 }
1201
1202
1203 /**
1204  * Setup http_client plugin
1205  *
1206  * @param plugin the plugin handle
1207  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1208  */
1209 static int
1210 client_start (struct HTTP_Client_Plugin *plugin)
1211 {
1212   curl_global_init (CURL_GLOBAL_ALL);
1213   plugin->curl_multi_handle = curl_multi_init ();
1214
1215   if (NULL == plugin->curl_multi_handle)
1216   {
1217     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1218                      _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1219                      plugin->name);
1220     return GNUNET_SYSERR;
1221   }
1222   return GNUNET_OK;
1223 }
1224
1225 /**
1226  * Session was idle, so disconnect it
1227  */
1228 static void
1229 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1230 {
1231   GNUNET_assert (NULL != cls);
1232   struct Session *s = cls;
1233
1234   s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1235   GNUNET_log (TIMEOUT_LOG,
1236               "Session %p was idle for %llu ms, disconnecting\n",
1237               s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1238
1239   /* call session destroy function */
1240   GNUNET_assert (GNUNET_OK == client_disconnect (s));
1241 }
1242
1243
1244 /**
1245  * Start session timeout for session s
1246  *
1247  * @param s the session
1248  */
1249 static void
1250 client_start_session_timeout (struct Session *s)
1251 {
1252
1253  GNUNET_assert (NULL != s);
1254  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1255  s->timeout_task =  GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1256                                                   &client_session_timeout,
1257                                                   s);
1258  GNUNET_log (TIMEOUT_LOG,
1259              "Timeout for session %p set to %llu ms\n",
1260              s,  (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1261 }
1262
1263
1264 /**
1265  * Increment session timeout due to activity for session s
1266  *
1267  * param s the session
1268  */
1269 static void
1270 client_reschedule_session_timeout (struct Session *s)
1271 {
1272
1273  GNUNET_assert (NULL != s);
1274  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1275
1276  GNUNET_SCHEDULER_cancel (s->timeout_task);
1277  s->timeout_task =  GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1278                                                   &client_session_timeout,
1279                                                   s);
1280  GNUNET_log (TIMEOUT_LOG,
1281              "Timeout rescheduled for session %p set to %llu ms\n",
1282              s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1283 }
1284
1285
1286 /**
1287  * Cancel timeout due to activity for session s
1288  *
1289  * param s the session
1290  */
1291 static void
1292 client_stop_session_timeout (struct Session *s)
1293 {
1294  GNUNET_assert (NULL != s);
1295
1296  if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1297  {
1298    GNUNET_SCHEDULER_cancel (s->timeout_task);
1299    s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1300    GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1301  }
1302 }
1303
1304
1305 /**
1306  * Another peer has suggested an address for this
1307  * peer and transport plugin.  Check that this could be a valid
1308  * address.  If so, consider adding it to the list
1309  * of addresses.
1310  *
1311  * @param cls closure
1312  * @param addr pointer to the address
1313  * @param addrlen length of addr
1314  * @return GNUNET_OK if this is a plausible address for this peer
1315  *         and transport
1316  */
1317 static int
1318 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1319 {
1320   /* struct Plugin *plugin = cls; */
1321
1322   /* A HTTP/S client does not have any valid address so:*/
1323   return GNUNET_NO;
1324 }
1325
1326
1327 /**
1328  * Exit point from the plugin.
1329  *
1330  * @param cls api as closure
1331  * @return NULL
1332  */
1333 void *
1334 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1335 {
1336   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1337   struct HTTP_Client_Plugin *plugin = api->cls;
1338   struct Session *pos;
1339   struct Session *next;
1340
1341   if (NULL == api->cls)
1342   {
1343     /* Stub shutdown */
1344     GNUNET_free (api);
1345     return NULL;
1346   }
1347
1348   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1349                    _("Shutting down plugin `%s'\n"),
1350                    plugin->name);
1351
1352
1353   next = plugin->head;
1354   while (NULL != (pos = next))
1355   {
1356       next = pos->next;
1357       client_disconnect (pos);
1358   }
1359   if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1360   {
1361       GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1362       plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1363   }
1364
1365
1366   if (NULL != plugin->curl_multi_handle)
1367   {
1368     curl_multi_cleanup (plugin->curl_multi_handle);
1369     plugin->curl_multi_handle = NULL;
1370   }
1371   curl_global_cleanup ();
1372
1373   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1374                    _("Shutdown for plugin `%s' complete\n"),
1375                    plugin->name);
1376
1377   GNUNET_free (plugin);
1378   GNUNET_free (api);
1379   return NULL;
1380 }
1381
1382
1383 /**
1384  * Configure plugin
1385  *
1386  * @param plugin the plugin handle
1387  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1388  */
1389 static int
1390 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1391 {
1392   unsigned long long max_connections;
1393
1394   /* Optional parameters */
1395   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1396                       plugin->name,
1397                       "MAX_CONNECTIONS", &max_connections))
1398     max_connections = 128;
1399   plugin->max_connections = max_connections;
1400
1401   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1402                    _("Maximum number of connections is %u\n"),
1403                    plugin->max_connections);
1404   return GNUNET_OK;
1405 }
1406
1407 /**
1408  * Entry point for the plugin.
1409  */
1410 void *
1411 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1412 {
1413   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1414   struct GNUNET_TRANSPORT_PluginFunctions *api;
1415   struct HTTP_Client_Plugin *plugin;
1416
1417   if (NULL == env->receive)
1418   {
1419     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1420        initialze the plugin or the API */
1421     api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1422     api->cls = NULL;
1423     api->address_to_string = &http_common_plugin_address_to_string;
1424     api->string_to_address = &http_common_plugin_string_to_address;
1425     api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1426     return api;
1427   }
1428
1429   plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1430   p = plugin;
1431   plugin->env = env;
1432   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1433   api->cls = plugin;
1434   api->send = &http_client_plugin_send;
1435   api->disconnect = &http_client_plugin_disconnect;
1436   api->check_address = &http_client_plugin_address_suggested;
1437   api->get_session = &http_client_plugin_get_session;
1438   api->address_to_string = &http_common_plugin_address_to_string;
1439   api->string_to_address = &http_common_plugin_string_to_address;
1440   api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1441
1442
1443 #if BUILD_HTTPS
1444   plugin->name = "transport-https_client";
1445   plugin->protocol = "https";
1446 #else
1447   plugin->name = "transport-http_client";
1448   plugin->protocol = "http";
1449 #endif
1450   plugin->last_tag = 1;
1451
1452   if (GNUNET_SYSERR == client_configure_plugin (plugin))
1453   {
1454       LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1455       return NULL;
1456   }
1457
1458   /* Start client */
1459   if (GNUNET_SYSERR == client_start (plugin))
1460   {
1461       LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1462       return NULL;
1463   }
1464   return api;
1465 }
1466
1467 /* end of plugin_transport_http_client.c */