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