-allow caller ID to differ from zone used for resolution
[oweals/gnunet.git] / src / transport / plugin_transport_http_client.c
1 /*
2      This file is part of GNUnet
3      (C) 2002-2013 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 PLUGIN_NAME "https_client"
29 #define HTTP_STAT_STR_CONNECTIONS "# HTTPS client connections"
30 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
31 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
32 #else
33 #define PLUGIN_NAME "http_client"
34 #define HTTP_STAT_STR_CONNECTIONS "# HTTP client connections"
35 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
36 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
37 #endif
38
39 #define VERBOSE_CURL GNUNET_YES
40
41 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
42
43 #define ENABLE_PUT GNUNET_YES
44 #define ENABLE_GET GNUNET_YES
45
46 #include "platform.h"
47 #include "gnunet_util_lib.h"
48 #include "gnunet_protocols.h"
49 #include "gnunet_transport_plugin.h"
50 #include "plugin_transport_http_common.h"
51 #include <curl/curl.h>
52
53
54
55 /**
56  * Encapsulation of all of the state of the plugin.
57  */
58 struct HTTP_Client_Plugin;
59
60
61 /**
62  *  Message to send using http
63  */
64 struct HTTP_Message
65 {
66   /**
67    * next pointer for double linked list
68    */
69   struct HTTP_Message *next;
70
71   /**
72    * previous pointer for double linked list
73    */
74   struct HTTP_Message *prev;
75
76   /**
77    * buffer containing data to send
78    */
79   char *buf;
80
81   /**
82    * amount of data already sent
83    */
84   size_t pos;
85
86   /**
87    * buffer length
88    */
89   size_t size;
90
91   /**
92    * Continuation function to call once the transmission buffer
93    * has again space available.  NULL if there is no
94    * continuation to call.
95    */
96   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
97
98   /**
99    * Closure for transmit_cont.
100    */
101   void *transmit_cont_cls;
102 };
103
104
105 /**
106  * Session handle for connections.
107  */
108 struct Session;
109
110
111 /**
112  * A connection handle
113  *
114  */
115 struct ConnectionHandle
116 {
117   /**
118    * The curl easy handle
119    */
120   CURL *easyhandle;
121
122   /**
123    * The related session
124    */
125   struct Session *s;
126 };
127
128
129 /**
130  * Session handle for connections.
131  */
132 struct Session
133 {
134   /**
135    * To whom are we talking to (set to our identity
136    * if we are still waiting for the welcome message)
137    */
138   struct GNUNET_PeerIdentity target;
139
140   /**
141    * Stored in a linked list.
142    */
143   struct Session *next;
144
145   /**
146    * Stored in a linked list.
147    */
148   struct Session *prev;
149
150   /**
151    * The URL to connect to
152    */
153   char *url;
154
155   /**
156    * Address
157    */
158   struct GNUNET_HELLO_Address *address;
159
160   /**
161    * ATS network type in NBO
162    */
163   uint32_t ats_address_network_type;
164
165   /**
166    * Pointer to the global plugin struct.
167    */
168   struct HTTP_Client_Plugin *plugin;
169
170   /**
171    * Client send handle
172    */
173   void *client_put;
174
175   struct ConnectionHandle put;
176   struct ConnectionHandle get;
177
178   /**
179    * Is the client PUT handle currently paused
180    */
181   int put_paused;
182
183   /**
184    * Is the client PUT handle disconnect in progress?
185    */
186   int put_tmp_disconnecting;
187
188   /**
189    * Is the client PUT handle temporarily disconnected?
190    */
191   int put_tmp_disconnected;
192
193   /**
194    * We received data to send while disconnecting, reconnect immediately
195    */
196   int put_reconnect_required;
197
198   /**
199    * Client receive handle
200    */
201   void *client_get;
202
203   /**
204    * Outbound overhead due to HTTP connection
205    * Add to next message of this session when calling callback
206    */
207   size_t overhead;
208
209   /**
210    * next pointer for double linked list
211    */
212   struct HTTP_Message *msg_head;
213
214   /**
215    * previous pointer for double linked list
216    */
217   struct HTTP_Message *msg_tail;
218
219   /**
220    * Message stream tokenizer for incoming data
221    */
222   struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
223
224   /**
225    * Session timeout task
226    */
227   GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
228
229   /**
230    * Session timeout task
231    */
232   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
233
234   /**
235    * Task to wake up client receive handle when receiving is allowed again
236    */
237   GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
238
239   /**
240   * Absolute time when to receive data again
241   * Used for receive throttling
242   */
243   struct GNUNET_TIME_Absolute next_receive;
244 };
245
246
247 /**
248  * Encapsulation of all of the state of the plugin.
249  */
250 struct HTTP_Client_Plugin
251 {
252   /**
253    * Our environment.
254    */
255   struct GNUNET_TRANSPORT_PluginEnvironment *env;
256
257   /**
258    * Linked list head of open sessions.
259    */
260   struct Session *head;
261
262   /**
263    * Linked list tail of open sessions.
264    */
265   struct Session *tail;
266
267   /**
268    * Plugin name
269    */
270   char *name;
271
272   /**
273    * Protocol
274    */
275   char *protocol;
276
277   /**
278    * Proxy configuration: hostname or ip of the proxy server
279    */
280   char *proxy_hostname;
281
282   /**
283    * Username for the proxy server
284    */
285   char *proxy_username;
286
287   /**
288    * Password for the proxy server
289    */
290   char *proxy_password;
291
292   /**
293    * Type of proxy server:
294    *
295    * Valid values as supported by curl:
296    * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
297    * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
298    */
299   curl_proxytype proxytype;
300
301   /**
302    * Use proxy tunneling:
303    * Tunnel all operations through a given HTTP instead of have the proxy
304    * evaluate the HTTP request
305    *
306    * Default: GNUNET_NO, GNUNET_yes experimental
307    */
308   int proxy_use_httpproxytunnel;
309
310   /**
311    * My options to be included in the address
312    */
313   uint32_t options;
314
315   /**
316    * Maximum number of sockets the plugin can use
317    * Each http inbound /outbound connections are two connections
318    */
319   unsigned int max_connections;
320
321   /**
322    * Current number of sockets the plugin can use
323    * Each http inbound /outbound connections are two connections
324    */
325   unsigned int cur_connections;
326
327   /**
328    * Last used unique HTTP connection tag
329    */
330   uint32_t last_tag;
331
332   /**
333    * use IPv6
334    */
335   uint16_t use_ipv6;
336
337   /**
338    * use IPv4
339    */
340   uint16_t use_ipv4;
341
342   /**
343    * cURL Multihandle
344    */
345   CURLM *curl_multi_handle;
346
347   /**
348    * curl perform task
349    */
350   GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
351 };
352
353
354 /**
355  * Increment session timeout due to activity for a session
356  * @param s the session
357  */
358 static void
359 client_reschedule_session_timeout (struct Session *s);
360
361
362 /**
363  * Function setting up file descriptors and scheduling task to run
364  *
365  * @param  plugin plugin as closure
366  * @param now schedule task in 1ms, regardless of what curl may say
367  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
368  */
369 static int
370 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
371
372
373 /**
374  * Connect a HTTP put connection
375  *
376  * @param s the session to connect
377  * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
378  */
379 static int
380 client_connect_put (struct Session *s);
381
382
383 /**
384  * Does a session s exists?
385  *
386  * @param plugin the plugin
387  * @param s desired session
388  * @return #GNUNET_YES or #GNUNET_NO
389  */
390 static int
391 client_exist_session (struct HTTP_Client_Plugin *plugin,
392                       struct Session *s)
393 {
394   struct Session * head;
395
396   for (head = plugin->head; head != NULL; head = head->next)
397     if (head == s)
398       return GNUNET_YES;
399   return GNUNET_NO;
400 }
401
402
403 /**
404  * Loggging function
405  *
406  * @param curl the curl easy handle
407  * @param type message type
408  * @param data data to log, NOT a 0-terminated string
409  * @param size data length
410  * @param cls the closure
411  * @return always 0
412  */
413 static int
414 client_log (CURL *curl, curl_infotype type,
415             const char *data, size_t size, void *cls)
416 {
417   struct ConnectionHandle *ch = cls;
418   const char *ttype = "UNSPECIFIED";
419
420   if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
421   {
422     char text[size + 2];
423
424     switch (type) {
425       case CURLINFO_TEXT:
426         ttype = "TEXT";
427         break;
428       case CURLINFO_HEADER_IN:
429         ttype = "HEADER_IN";
430         break;
431       case CURLINFO_HEADER_OUT:
432         ttype = "HEADER_OUT";
433         /* Overhead*/
434
435         GNUNET_assert (NULL != ch);
436         GNUNET_assert (NULL != ch->easyhandle);
437         GNUNET_assert (NULL != ch->s);
438         ch->s->overhead += size;
439         break;
440       default:
441         ttype = "UNSPECIFIED";
442         break;
443     }
444
445     memcpy (text, data, size);
446     if (text[size - 1] == '\n')
447       text[size] = '\0';
448     else
449     {
450       text[size] = '\n';
451       text[size + 1] = '\0';
452     }
453 #if BUILD_HTTPS
454     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
455                      "Connection %p %s: %s", ch->easyhandle, ttype, text);
456 #else
457     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
458                      "Connection %p %s: %s", ch->easyhandle, ttype, text);
459 #endif
460   }
461   return 0;
462 }
463
464
465 /**
466  * Function that can be used by the transport service to transmit
467  * a message using the plugin.   Note that in the case of a
468  * peer disconnecting, the continuation MUST be called
469  * prior to the disconnect notification itself.  This function
470  * will be called with this peer's HELLO message to initiate
471  * a fresh connection to another peer.
472  *
473  * @param cls closure
474  * @param s which session must be used
475  * @param msgbuf the message to transmit
476  * @param msgbuf_size number of bytes in 'msgbuf'
477  * @param priority how important is the message (most plugins will
478  *                 ignore message priority and just FIFO)
479  * @param to how long to wait at most for the transmission (does not
480  *                require plugins to discard the message after the timeout,
481  *                just advisory for the desired delay; most plugins will ignore
482  *                this as well)
483  * @param cont continuation to call once the message has
484  *        been transmitted (or if the transport is ready
485  *        for the next transmission call; or if the
486  *        peer disconnected...); can be NULL
487  * @param cont_cls closure for cont
488  * @return number of bytes used (on the physical network, with overheads);
489  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
490  *         and does NOT mean that the message was not transmitted (DV)
491  */
492 static ssize_t
493 http_client_plugin_send (void *cls,
494                          struct Session *s,
495                          const char *msgbuf, size_t msgbuf_size,
496                          unsigned int priority,
497                          struct GNUNET_TIME_Relative to,
498                          GNUNET_TRANSPORT_TransmitContinuation cont,
499                          void *cont_cls)
500 {
501   struct HTTP_Client_Plugin *plugin = cls;
502   struct HTTP_Message *msg;
503   char *stat_txt;
504
505   /* lookup if session is really existing */
506   if (GNUNET_YES != client_exist_session (plugin, s))
507   {
508     GNUNET_break (0);
509     return GNUNET_SYSERR;
510   }
511
512   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
513                    "Session %p/connection %p: Sending message with %u to peer `%s' \n",
514                    s, s->client_put,
515                    msgbuf_size, GNUNET_i2s (&s->target));
516
517   /* create new message and schedule */
518   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
519   msg->next = NULL;
520   msg->size = msgbuf_size;
521   msg->pos = 0;
522   msg->buf = (char *) &msg[1];
523   msg->transmit_cont = cont;
524   msg->transmit_cont_cls = cont_cls;
525   memcpy (msg->buf, msgbuf, msgbuf_size);
526   GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
527
528   GNUNET_asprintf (&stat_txt,
529                    "# bytes currently in %s_client buffers",
530                    plugin->protocol);
531   GNUNET_STATISTICS_update (plugin->env->stats,
532                             stat_txt, msgbuf_size, GNUNET_NO);
533   GNUNET_free (stat_txt);
534
535   if (GNUNET_YES == s->put_tmp_disconnecting)
536   {
537     /* PUT connection is currently getting disconnected */
538     s->put_reconnect_required = GNUNET_YES;
539     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
540                      "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
541                      s, s->client_put);
542     return msgbuf_size;
543   }
544   else if (GNUNET_YES == s->put_paused)
545   {
546     /* PUT connection was paused, unpause */
547     GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
548     GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
549     s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
550     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
551                      "Session %p/connection %p: unpausing connection\n",
552                      s, s->client_put);
553     s->put_paused = GNUNET_NO;
554     if (NULL != s->client_put)
555       curl_easy_pause (s->client_put, CURLPAUSE_CONT);
556   }
557   else if (GNUNET_YES == s->put_tmp_disconnected)
558   {
559     /* PUT connection was disconnected, reconnect */
560     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
561                      "Session %p: Reconnecting PUT connection\n",
562                      s);
563     s->put_tmp_disconnected = GNUNET_NO;
564     GNUNET_break (s->client_put == NULL);
565     if (GNUNET_SYSERR == client_connect_put (s))
566     {
567       return GNUNET_SYSERR;
568     }
569   }
570
571   client_schedule (s->plugin, GNUNET_YES);
572   return msgbuf_size;
573 }
574
575
576 /**
577  * Delete session s
578  *
579  * @param s the session to delete
580  */
581 static void
582 client_delete_session (struct Session *s)
583 {
584   struct HTTP_Client_Plugin *plugin = s->plugin;
585   struct HTTP_Message *pos;
586   struct HTTP_Message *next;
587
588   if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
589   {
590     GNUNET_SCHEDULER_cancel (s->timeout_task);
591     s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
592   }
593   if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
594   {
595       GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
596       s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
597   }
598
599   GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
600
601   next = s->msg_head;
602   while (NULL != (pos = next))
603   {
604     next = pos->next;
605     GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
606     if (pos->transmit_cont != NULL)
607       pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
608                           pos->size, pos->pos + s->overhead);
609     s->overhead = 0;
610     GNUNET_free (pos);
611   }
612
613   if (s->msg_tk != NULL)
614   {
615     GNUNET_SERVER_mst_destroy (s->msg_tk);
616     s->msg_tk = NULL;
617   }
618   GNUNET_HELLO_address_free (s->address);
619   GNUNET_free (s->url);
620   GNUNET_free (s);
621 }
622
623
624 /**
625  * Disconnect a session
626  *
627  * @param cls the `struct HTTP_Client_Plugin`
628  * @param s session
629  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
630  */
631 static int
632 http_client_session_disconnect (void *cls,
633                                 struct Session *s)
634 {
635   struct HTTP_Client_Plugin *plugin = cls;
636   struct HTTP_Message *msg;
637   struct HTTP_Message *t;
638   int res = GNUNET_OK;
639   CURLMcode mret;
640
641   if (GNUNET_YES != client_exist_session (plugin, s))
642   {
643     GNUNET_break (0);
644     return GNUNET_SYSERR;
645   }
646
647   if (NULL != s->client_put)
648   {
649     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
650                      "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
651                      s, s->client_put, GNUNET_i2s (&s->target));
652
653     /* remove curl handle from multi handle */
654     mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
655     if (mret != CURLM_OK)
656     {
657       /* clean up easy handle, handle is now invalid and free'd */
658       res = GNUNET_SYSERR;
659       GNUNET_break (0);
660     }
661     curl_easy_cleanup (s->client_put);
662     s->client_put = NULL;
663   }
664
665
666   if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
667   {
668     GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
669     s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
670   }
671
672   if (NULL != s->client_get)
673   {
674     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
675                      "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
676                      s, s->client_get,
677                      GNUNET_i2s (&s->target));
678     /* remove curl handle from multi handle */
679     mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
680     if (mret != CURLM_OK)
681     {
682       /* clean up easy handle, handle is now invalid and free'd */
683       res = GNUNET_SYSERR;
684       GNUNET_break (0);
685     }
686     curl_easy_cleanup (s->client_get);
687     s->client_get = NULL;
688   }
689
690   msg = s->msg_head;
691   while (NULL != msg)
692   {
693     t = msg->next;
694     if (NULL != msg->transmit_cont)
695       msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
696                           msg->size, msg->pos + s->overhead);
697     s->overhead = 0;
698     GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
699     GNUNET_free (msg);
700     msg = t;
701   }
702
703   GNUNET_assert (plugin->cur_connections >= 2);
704   plugin->cur_connections -= 2;
705   GNUNET_STATISTICS_set (plugin->env->stats,
706                          HTTP_STAT_STR_CONNECTIONS,
707                          plugin->cur_connections,
708                          GNUNET_NO);
709   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
710                    "Session %p: notifying transport about ending session\n",s);
711
712   plugin->env->session_end (plugin->env->cls, s->address, s);
713   client_delete_session (s);
714
715   /* Re-schedule since handles have changed */
716   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
717   {
718     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
719     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
720   }
721   client_schedule (plugin, GNUNET_YES);
722
723   return res;
724 }
725
726
727 /**
728  * Function that is called to get the keepalive factor.
729  * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
730  * calculate the interval between keepalive packets.
731  *
732  * @param cls closure with the `struct Plugin`
733  * @return keepalive factor
734  */
735 static unsigned int
736 http_client_query_keepalive_factor (void *cls)
737 {
738   return 3;
739 }
740
741
742 /**
743  * Function that can be used to force the plugin to disconnect
744  * from the given peer and cancel all previous transmissions
745  * (and their continuationc).
746  *
747  * @param cls closure
748  * @param target peer from which to disconnect
749  */
750 static void
751 http_client_peer_disconnect (void *cls,
752                              const struct GNUNET_PeerIdentity *target)
753 {
754   struct HTTP_Client_Plugin *plugin = cls;
755   struct Session *next = NULL;
756   struct Session *pos = NULL;
757
758   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
759                    "Transport tells me to disconnect `%s'\n",
760                    GNUNET_i2s (target));
761
762   next = plugin->head;
763   while (NULL != (pos = next))
764   {
765     next = pos->next;
766     if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
767     {
768       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
769                        "Disconnecting session %p to `%pos'\n",
770                        pos, GNUNET_i2s (target));
771       GNUNET_assert (GNUNET_OK == http_client_session_disconnect (plugin,
772                                                                   pos));
773     }
774   }
775 }
776
777
778 /**
779  * Check if a sessions exists for an specific address
780  *
781  * @param plugin the plugin
782  * @param address the address
783  * @return the session or NULL
784  */
785 static struct Session *
786 client_lookup_session (struct HTTP_Client_Plugin *plugin,
787                        const struct GNUNET_HELLO_Address *address)
788 {
789   struct Session *pos;
790
791   for (pos = plugin->head; NULL != pos; pos = pos->next)
792   {
793     if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
794         (0 == GNUNET_HELLO_address_cmp(address, pos->address)))
795       return pos;
796   }
797   return NULL;
798 }
799
800
801 static void
802 client_put_disconnect (void *cls,
803                        const struct GNUNET_SCHEDULER_TaskContext *tc)
804 {
805   struct Session *s = cls;
806
807   s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
808   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
809                    "Session %p/connection %p: will be disconnected due to no activity\n",
810                    s, s->client_put);
811   s->put_paused = GNUNET_NO;
812   s->put_tmp_disconnecting = GNUNET_YES;
813   if (NULL != s->client_put)
814     curl_easy_pause (s->client_put, CURLPAUSE_CONT);
815   client_schedule (s->plugin, GNUNET_YES);
816 }
817
818
819 /**
820  * Callback method used with libcurl
821  * Method is called when libcurl needs to read data during sending
822  *
823  * @param stream pointer where to write data
824  * @param size size of an individual element
825  * @param nmemb count of elements that can be written to the buffer
826  * @param cls our `struct Session`
827  * @return bytes written to stream, returning 0 will terminate connection!
828  */
829 static size_t
830 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
831 {
832   struct Session *s = cls;
833   struct HTTP_Client_Plugin *plugin = s->plugin;
834   struct HTTP_Message *msg = s->msg_head;
835   size_t len;
836   char *stat_txt;
837
838   if (GNUNET_YES != client_exist_session (plugin, s))
839   {
840     GNUNET_break (0);
841     return 0;
842   }
843   if (GNUNET_YES == s->put_tmp_disconnecting)
844   {
845
846       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
847                        "Session %p/connection %p: disconnect due to inactivity\n",
848                        s, s->client_put);
849       return 0;
850   }
851
852   if (NULL == msg)
853   {
854     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
855                      "Session %p/connection %p: nothing to send, suspending\n",
856                      s, s->client_put);
857     s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
858     s->put_paused = GNUNET_YES;
859     return CURL_READFUNC_PAUSE;
860   }
861   /* data to send */
862   GNUNET_assert (msg->pos < msg->size);
863   /* calculate how much fits in buffer */
864   len = GNUNET_MIN (msg->size - msg->pos,
865                     size * nmemb);
866   memcpy (stream, &msg->buf[msg->pos], len);
867   msg->pos += len;
868   if (msg->pos == msg->size)
869   {
870     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
871                      "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
872                      s, s->client_put, msg->size, msg->pos);
873     /* Calling transmit continuation  */
874     GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
875     if (NULL != msg->transmit_cont)
876       msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
877                           msg->size, msg->size + s->overhead);
878     s->overhead = 0;
879     GNUNET_free (msg);
880   }
881
882   GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
883   GNUNET_STATISTICS_update (plugin->env->stats,
884                             stat_txt, -len, GNUNET_NO);
885   GNUNET_free (stat_txt);
886
887   GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
888   GNUNET_STATISTICS_update (plugin->env->stats,
889                             stat_txt, len, GNUNET_NO);
890   GNUNET_free (stat_txt);
891   return len;
892 }
893
894
895 /**
896  * Wake up a curl handle which was suspended
897  *
898  * @param cls the session
899  * @param tc task context
900  */
901 static void
902 client_wake_up (void *cls,
903                 const struct GNUNET_SCHEDULER_TaskContext *tc)
904 {
905   struct Session *s = cls;
906   struct HTTP_Client_Plugin *p = s->plugin;
907
908   if (GNUNET_YES != client_exist_session (p, s))
909   {
910     GNUNET_break (0);
911     return;
912   }
913   s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
914   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
915     return;
916   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
917                    "Session %p/connection %p: Waking up GET handle\n",
918                    s,
919                    s->client_get);
920   s->put_paused = GNUNET_NO;
921   if (NULL != s->client_get)
922     curl_easy_pause (s->client_get, CURLPAUSE_CONT);
923
924 }
925
926
927 /**
928  * Callback for message stream tokenizer
929  *
930  * @param cls the session
931  * @param client not used
932  * @param message the message received
933  * @return always #GNUNET_OK
934  */
935 static int
936 client_receive_mst_cb (void *cls, void *client,
937                        const struct GNUNET_MessageHeader *message)
938 {
939   struct Session *s = cls;
940   struct HTTP_Client_Plugin *plugin;
941   struct GNUNET_TIME_Relative delay;
942   struct GNUNET_ATS_Information atsi;
943   char *stat_txt;
944
945   plugin = s->plugin;
946   if (GNUNET_YES != client_exist_session (plugin, s))
947   {
948     GNUNET_break (0);
949     return GNUNET_OK;
950   }
951
952   atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
953   atsi.value = s->ats_address_network_type;
954   GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
955
956   delay = s->plugin->env->receive (plugin->env->cls, s->address, s, message);
957   plugin->env->update_address_metrics (plugin->env->cls,
958                                        s->address, s,
959                                        &atsi, 1);
960
961   GNUNET_asprintf (&stat_txt,
962                    "# bytes received via %s_client",
963                    plugin->protocol);
964   GNUNET_STATISTICS_update (plugin->env->stats,
965                             stat_txt, ntohs(message->size), GNUNET_NO);
966   GNUNET_free (stat_txt);
967
968   s->next_receive =
969       GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
970
971   if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
972   {
973     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
974                      "Client: peer `%s' address `%s' next read delayed for %s\n",
975                      GNUNET_i2s (&s->target),
976                      http_common_plugin_address_to_string (NULL,
977                         s->plugin->protocol, s->address->address,
978                         s->address->address_length),
979                      GNUNET_STRINGS_relative_time_to_string (delay,
980                                                              GNUNET_YES));
981   }
982   client_reschedule_session_timeout (s);
983   return GNUNET_OK;
984 }
985
986
987 /**
988  * Callback method used with libcurl when data for a PUT connection are
989  * received. We do not expect data here, so we just dismiss it
990  *
991  * @param stream pointer where to write data
992  * @param size size of an individual element
993  * @param nmemb count of elements that can be written to the buffer
994  * @param cls destination pointer, passed to the libcurl handle
995  * @return bytes read from stream
996  */
997 static size_t
998 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
999 {
1000   return size * nmemb;
1001 }
1002
1003
1004 /**
1005  * Callback method used with libcurl when data for a GET connection are
1006  * received. Forward to MST
1007  *
1008  * @param stream pointer where to write data
1009  * @param size size of an individual element
1010  * @param nmemb count of elements that can be written to the buffer
1011  * @param cls destination pointer, passed to the libcurl handle
1012  * @return bytes read from stream
1013  */
1014 static size_t
1015 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
1016 {
1017   struct Session *s = cls;
1018   struct GNUNET_TIME_Absolute now;
1019   size_t len = size * nmemb;
1020
1021   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1022                    "Session %p / connection %p: Received %u bytes from peer `%s'\n",
1023                    s, s->client_get,
1024                    len, GNUNET_i2s (&s->target));
1025   now = GNUNET_TIME_absolute_get ();
1026   if (now.abs_value_us < s->next_receive.abs_value_us)
1027   {
1028     struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1029     struct GNUNET_TIME_Relative delta =
1030         GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1031     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1032                      "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1033                      s, s->client_get,
1034                      GNUNET_STRINGS_relative_time_to_string (delta,
1035                                                              GNUNET_YES));
1036     if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1037     {
1038       GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1039       s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1040     }
1041     s->recv_wakeup_task =
1042         GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1043     return CURL_WRITEFUNC_PAUSE;
1044   }
1045   if (NULL == s->msg_tk)
1046     s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1047   GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1048   return len;
1049 }
1050
1051
1052 /**
1053  * Task performing curl operations
1054  *
1055  * @param cls plugin as closure
1056  * @param tc gnunet scheduler task context
1057  */
1058 static void
1059 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1060
1061
1062 /**
1063  * Function setting up file descriptors and scheduling task to run
1064  *
1065  * @param plugin the plugin as closure
1066  * @param now schedule task in 1ms, regardless of what curl may say
1067  * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1068  */
1069 static int
1070 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1071 {
1072   fd_set rs;
1073   fd_set ws;
1074   fd_set es;
1075   int max;
1076   struct GNUNET_NETWORK_FDSet *grs;
1077   struct GNUNET_NETWORK_FDSet *gws;
1078   long to;
1079   CURLMcode mret;
1080   struct GNUNET_TIME_Relative timeout;
1081
1082   /* Cancel previous scheduled task */
1083   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1084   {
1085     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1086     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1087   }
1088   max = -1;
1089   FD_ZERO (&rs);
1090   FD_ZERO (&ws);
1091   FD_ZERO (&es);
1092   mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1093   if (mret != CURLM_OK)
1094   {
1095     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1096                 "curl_multi_fdset", __FILE__, __LINE__,
1097                 curl_multi_strerror (mret));
1098     return GNUNET_SYSERR;
1099   }
1100   mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1101   if (to == -1)
1102     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1103   else
1104     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1105   if (now == GNUNET_YES)
1106     timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1107
1108   if (mret != CURLM_OK)
1109   {
1110     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1111                 "curl_multi_timeout", __FILE__, __LINE__,
1112                 curl_multi_strerror (mret));
1113     return GNUNET_SYSERR;
1114   }
1115
1116   grs = GNUNET_NETWORK_fdset_create ();
1117   gws = GNUNET_NETWORK_fdset_create ();
1118   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1119   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1120
1121   plugin->client_perform_task =
1122       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1123                                    timeout, grs, gws,
1124                                    &client_run, plugin);
1125   GNUNET_NETWORK_fdset_destroy (gws);
1126   GNUNET_NETWORK_fdset_destroy (grs);
1127   return GNUNET_OK;
1128 }
1129
1130
1131 /**
1132  * Task performing curl operations
1133  *
1134  * @param cls plugin as closure
1135  * @param tc gnunet scheduler task context
1136  */
1137 static void
1138 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1139 {
1140   struct HTTP_Client_Plugin *plugin = cls;
1141   int running;
1142   long http_statuscode;
1143   CURLMcode mret;
1144
1145   GNUNET_assert (cls != NULL);
1146
1147   plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1148   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1149     return;
1150
1151   do
1152   {
1153     running = 0;
1154     mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1155
1156     CURLMsg *msg;
1157     int msgs_left;
1158
1159     while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1160     {
1161       CURL *easy_h = msg->easy_handle;
1162       struct Session *s = NULL;
1163       char *d = (char *) s;
1164
1165       if (easy_h == NULL)
1166       {
1167         GNUNET_break (0);
1168         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1169                          "Client: connection to ended with reason %i: `%s', %i handles running\n",
1170                          msg->data.result,
1171                          curl_easy_strerror (msg->data.result), running);
1172         continue;
1173       }
1174
1175       GNUNET_assert (CURLE_OK ==
1176                      curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1177       s = (struct Session *) d;
1178
1179       if (GNUNET_YES != client_exist_session(plugin, s))
1180       {
1181         GNUNET_break (0);
1182         return;
1183       }
1184
1185       GNUNET_assert (s != NULL);
1186       if (msg->msg == CURLMSG_DONE)
1187       {
1188         curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1189         if (easy_h == s->client_put)
1190         {
1191             if  ((0 != msg->data.result) || (http_statuscode != 200))
1192             {
1193                 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1194                   "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1195                   s, msg->easy_handle, GNUNET_i2s (&s->target),
1196                   http_statuscode,
1197                   msg->data.result,
1198                   curl_easy_strerror (msg->data.result));
1199             }
1200             else
1201               GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1202                 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1203                 s, msg->easy_handle, GNUNET_i2s (&s->target));
1204             if (s->client_get == NULL)
1205             {
1206               /* Disconnect other transmission direction and tell transport */
1207             }
1208             curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1209             curl_easy_cleanup (easy_h);
1210             s->put_tmp_disconnecting = GNUNET_NO;
1211             s->put_tmp_disconnected = GNUNET_YES;
1212             s->client_put = NULL;
1213             s->put.easyhandle = NULL;
1214             s->put.s = NULL;
1215
1216             /*
1217              * Handling a rare case:
1218              * plugin_send was called during temporary put disconnect,
1219              * reconnect required after connection was disconnected
1220              */
1221             if (GNUNET_YES == s->put_reconnect_required)
1222             {
1223                 s->put_reconnect_required = GNUNET_NO;
1224                 if (GNUNET_SYSERR == client_connect_put(s))
1225                 {
1226                     GNUNET_break (s->client_put == NULL);
1227                     GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1228                 }
1229             }
1230         }
1231         if (easy_h == s->client_get)
1232         {
1233             if  ((0 != msg->data.result) || (http_statuscode != 200))
1234             {
1235               GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1236                 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1237                 s, msg->easy_handle, GNUNET_i2s (&s->target),
1238                 http_statuscode,
1239                 msg->data.result,
1240                 curl_easy_strerror (msg->data.result));
1241
1242             }
1243             else
1244               GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1245                 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1246                 s, msg->easy_handle, GNUNET_i2s (&s->target));
1247             /* Disconnect other transmission direction and tell transport */
1248             s->get.easyhandle = NULL;
1249             s->get.s = NULL;
1250             http_client_session_disconnect (plugin, s);
1251         }
1252       }
1253     }
1254   }
1255   while (mret == CURLM_CALL_MULTI_PERFORM);
1256   client_schedule (plugin, GNUNET_NO);
1257 }
1258
1259
1260 /**
1261  * Connect GET connection for a session
1262  *
1263  * @param s the session to connect
1264  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1265  */
1266 static int
1267 client_connect_get (struct Session *s)
1268 {
1269   CURLMcode mret;
1270
1271   /* create get connection */
1272   s->client_get = curl_easy_init ();
1273   s->get.s = s;
1274   s->get.easyhandle = s->client_get;
1275 #if VERBOSE_CURL
1276   curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1277   curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1278   curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1279 #endif
1280 #if BUILD_HTTPS
1281   curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1282   {
1283     struct HttpAddress *ha;
1284     ha = (struct HttpAddress *) s->address->address;
1285
1286     if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1287         (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1288     {
1289       curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1290       curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1291     }
1292     else
1293     {
1294       curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1295       curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1296     }
1297   }
1298   curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1299   curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1300 #else
1301   curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1302   curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1303 #endif
1304
1305   if (s->plugin->proxy_hostname != NULL)
1306   {
1307     curl_easy_setopt (s->client_get, CURLOPT_PROXY, s->plugin->proxy_hostname);
1308     curl_easy_setopt (s->client_get, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1309     if (NULL != s->plugin->proxy_username)
1310       curl_easy_setopt (s->client_get, CURLOPT_PROXYUSERNAME,
1311           s->plugin->proxy_username);
1312     if (NULL != s->plugin->proxy_password)
1313       curl_easy_setopt (s->client_get, CURLOPT_PROXYPASSWORD,
1314           s->plugin->proxy_password);
1315     if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1316       curl_easy_setopt (s->client_get, CURLOPT_HTTPPROXYTUNNEL,
1317           s->plugin->proxy_use_httpproxytunnel);
1318   }
1319
1320   curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1321   //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1322   //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1323   curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1324   curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1325   curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1326   curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1327   /* No timeout by default, timeout done with session timeout */
1328   curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1329   curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1330   curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1331                     (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1332   curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1333                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1334 #if CURL_TCP_NODELAY
1335   curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1336 #endif
1337   curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1338
1339   mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1340   if (mret != CURLM_OK)
1341   {
1342     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1343                      "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1344                      s,
1345                      curl_multi_strerror (mret));
1346     curl_easy_cleanup (s->client_get);
1347     s->client_get = NULL;
1348     s->get.s = NULL;
1349     s->get.easyhandle = NULL;
1350     GNUNET_break (0);
1351     return GNUNET_SYSERR;
1352   }
1353
1354   return GNUNET_OK;
1355 }
1356
1357
1358 /**
1359  * Connect a HTTP put connection
1360  *
1361  * @param s the session to connect
1362  * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1363  */
1364 static int
1365 client_connect_put (struct Session *s)
1366 {
1367   CURLMcode mret;
1368
1369   /* create put connection */
1370   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1371                    "Session %p : Init PUT handle\n", s);
1372   s->client_put = curl_easy_init ();
1373   s->put.s = s;
1374   s->put.easyhandle = s->client_put;
1375 #if VERBOSE_CURL
1376   curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1377   curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1378   curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1379 #endif
1380 #if BUILD_HTTPS
1381   curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1382   {
1383     struct HttpAddress *ha;
1384     ha = (struct HttpAddress *) s->address->address;
1385
1386     if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1387         (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1388     {
1389       curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1390       curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1391     }
1392     else
1393     {
1394       curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1395       curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1396     }
1397   }
1398   curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1399   curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1400 #else
1401   curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1402   curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1403 #endif
1404   if (s->plugin->proxy_hostname != NULL)
1405   {
1406     curl_easy_setopt (s->client_put, CURLOPT_PROXY, s->plugin->proxy_hostname);
1407     curl_easy_setopt (s->client_put, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1408     if (NULL != s->plugin->proxy_username)
1409       curl_easy_setopt (s->client_put, CURLOPT_PROXYUSERNAME,
1410           s->plugin->proxy_username);
1411     if (NULL != s->plugin->proxy_password)
1412       curl_easy_setopt (s->client_put, CURLOPT_PROXYPASSWORD,
1413           s->plugin->proxy_password);
1414     if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1415       curl_easy_setopt (s->client_put, CURLOPT_HTTPPROXYTUNNEL,
1416           s->plugin->proxy_use_httpproxytunnel);
1417   }
1418
1419   curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1420   curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1421   //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1422   //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1423   curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1424   curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1425   curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1426   curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1427   /* No timeout by default, timeout done with session timeout */
1428   curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1429   curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1430   curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1431                     (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1432   curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1433                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1434 #if CURL_TCP_NODELAY
1435   curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1436 #endif
1437   mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1438   if (mret != CURLM_OK)
1439   {
1440     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1441                      "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1442                      s,
1443                      curl_multi_strerror (mret));
1444     curl_easy_cleanup (s->client_put);
1445     s->client_put = NULL;
1446     s->put.easyhandle = NULL;
1447     s->put.s = NULL;
1448     s->put_tmp_disconnected = GNUNET_YES;
1449     return GNUNET_SYSERR;
1450   }
1451   s->put_tmp_disconnected = GNUNET_NO;
1452   return GNUNET_OK;
1453 }
1454
1455
1456 /**
1457  * Connect both PUT and GET connection for a session
1458  *
1459  * @param s the session to connect
1460  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1461  */
1462 static int
1463 client_connect (struct Session *s)
1464 {
1465
1466   struct HTTP_Client_Plugin *plugin = s->plugin;
1467   int res = GNUNET_OK;
1468
1469   /* create url */
1470   if (NULL == http_common_plugin_address_to_string (NULL,
1471         plugin->protocol, s->address->address, s->address->address_length))
1472   {
1473     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1474                      plugin->name,
1475                      "Invalid address peer `%s'\n",
1476                      GNUNET_i2s (&s->target));
1477     return GNUNET_SYSERR;
1478   }
1479
1480   GNUNET_asprintf (&s->url, "%s/%s;%u",
1481                    http_common_plugin_address_to_url (NULL, s->address->address,
1482                        s->address->address_length),
1483                    GNUNET_i2s_full (plugin->env->my_identity),
1484                    plugin->last_tag);
1485
1486   plugin->last_tag++;
1487
1488   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1489                    "Initiating outbound session peer `%s' using address `%s'\n",
1490                    GNUNET_i2s (&s->target), s->url);
1491
1492   if ((GNUNET_SYSERR == client_connect_get (s)) ||
1493       (GNUNET_SYSERR == client_connect_put (s)))
1494   {
1495     plugin->env->session_end (plugin->env->cls, s->address, s);
1496     client_delete_session (s);
1497     return GNUNET_SYSERR;
1498   }
1499
1500   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1501                    plugin->name,
1502                    "Session %p: connected with connections GET %p and PUT %p\n",
1503                    s, s->client_get, s->client_put);
1504
1505   /* Perform connect */
1506   plugin->cur_connections += 2;
1507   GNUNET_STATISTICS_set (plugin->env->stats,
1508                          HTTP_STAT_STR_CONNECTIONS,
1509                          plugin->cur_connections,
1510                          GNUNET_NO);
1511
1512   /* Re-schedule since handles have changed */
1513   if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1514   {
1515     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1516     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1517   }
1518   plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1519   return res;
1520 }
1521
1522
1523 /**
1524  * Function obtain the network type for a session
1525  *
1526  * @param cls closure (`struct Plugin*`)
1527  * @param session the session
1528  * @return the network type
1529  */
1530 static enum GNUNET_ATS_Network_Type
1531 http_client_get_network (void *cls,
1532                          struct Session *session)
1533 {
1534   return ntohl (session->ats_address_network_type);
1535 }
1536
1537
1538 /**
1539  * Session was idle, so disconnect it
1540  *
1541  * @param cls the `struct Session` of the idle session
1542  * @param tc scheduler context
1543  */
1544 static void
1545 client_session_timeout (void *cls,
1546                         const struct GNUNET_SCHEDULER_TaskContext *tc)
1547 {
1548   struct Session *s = cls;
1549
1550   s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1551   GNUNET_log (TIMEOUT_LOG,
1552               "Session %p was idle for %s, disconnecting\n",
1553               s,
1554               GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1555                                                       GNUNET_YES));
1556
1557   /* call session destroy function */
1558   GNUNET_assert (GNUNET_OK == http_client_session_disconnect (s->plugin,
1559                                                               s));
1560 }
1561
1562
1563 /**
1564  * Creates a new outbound session the transport service will use to
1565  * send data to the peer
1566  *
1567  * @param cls the plugin
1568  * @param address the address
1569  * @return the session or NULL of max connections exceeded
1570  */
1571 static struct Session *
1572 http_client_plugin_get_session (void *cls,
1573                                 const struct GNUNET_HELLO_Address *address)
1574 {
1575   struct HTTP_Client_Plugin *plugin = cls;
1576   struct Session * s = NULL;
1577   struct sockaddr *sa;
1578   struct GNUNET_ATS_Information ats;
1579   size_t salen = 0;
1580   int res;
1581
1582   GNUNET_assert (address->address != NULL);
1583
1584   /* find existing session */
1585   s = client_lookup_session (plugin, address);
1586   if (s != NULL)
1587     return s;
1588
1589   if (plugin->max_connections <= plugin->cur_connections)
1590   {
1591     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1592                      "Maximum number of connections (%u) reached: "
1593                      "cannot connect to peer `%s'\n",
1594                      plugin->max_connections,
1595                      GNUNET_i2s (&address->peer));
1596     return NULL;
1597   }
1598
1599   /* Determine network location */
1600   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1601   ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1602   sa = http_common_socket_from_address (address->address, address->address_length, &res);
1603   if (GNUNET_SYSERR == res)
1604   {
1605     return NULL;
1606   }
1607   else if (GNUNET_YES == res)
1608   {
1609     GNUNET_assert (NULL != sa);
1610     if (AF_INET == sa->sa_family)
1611     {
1612       salen = sizeof (struct sockaddr_in);
1613     }
1614     else if (AF_INET6 == sa->sa_family)
1615     {
1616       salen = sizeof (struct sockaddr_in6);
1617     }
1618     ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1619     //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1620     GNUNET_free (sa);
1621   }
1622   else if (GNUNET_NO == res)
1623   {
1624     /* Cannot convert to sockaddr -> is external hostname */
1625     ats.value = htonl (GNUNET_ATS_NET_WAN);
1626   }
1627   if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1628   {
1629     GNUNET_break (0);
1630     return NULL;
1631   }
1632
1633   s = GNUNET_new (struct Session);
1634   s->target = address->peer;
1635   s->plugin = plugin;
1636   s->address = GNUNET_HELLO_address_copy (address);
1637   s->ats_address_network_type = ats.value;
1638   s->put_paused = GNUNET_NO;
1639   s->put_tmp_disconnecting = GNUNET_NO;
1640   s->put_tmp_disconnected = GNUNET_NO;
1641   s->timeout_task =  GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1642                                                    &client_session_timeout,
1643                                                    s);
1644   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1645                    "Created new session %p for `%s' address `%s''\n",
1646                    s, http_common_plugin_address_to_string (NULL,
1647                        plugin->protocol, s->address->address,
1648                        s->address->address_length),
1649                    GNUNET_i2s (&s->target));
1650
1651   /* add new session */
1652   GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1653
1654   /* initiate new connection */
1655   if (GNUNET_SYSERR == client_connect (s))
1656   {
1657     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1658                      "Cannot connect to peer `%s' address `%s''\n",
1659                      http_common_plugin_address_to_string (NULL,
1660                                             plugin->protocol, s->address->address,
1661                                             s->address->address_length),
1662                      GNUNET_i2s (&s->target));
1663     client_delete_session (s);
1664     return NULL;
1665   }
1666   return s;
1667 }
1668
1669
1670 /**
1671  * Setup http_client plugin
1672  *
1673  * @param plugin the plugin handle
1674  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1675  */
1676 static int
1677 client_start (struct HTTP_Client_Plugin *plugin)
1678 {
1679   curl_global_init (CURL_GLOBAL_ALL);
1680   plugin->curl_multi_handle = curl_multi_init ();
1681
1682   if (NULL == plugin->curl_multi_handle)
1683   {
1684     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1685                      _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1686                      plugin->name);
1687     return GNUNET_SYSERR;
1688   }
1689   return GNUNET_OK;
1690 }
1691
1692
1693 /**
1694  * Increment session timeout due to activity for session s
1695  *
1696  * param s the session
1697  */
1698 static void
1699 client_reschedule_session_timeout (struct Session *s)
1700 {
1701   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1702   GNUNET_SCHEDULER_cancel (s->timeout_task);
1703   s->timeout_task =  GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1704                                                    &client_session_timeout,
1705                                                    s);
1706   GNUNET_log (TIMEOUT_LOG,
1707               "Timeout rescheduled for session %p set to %s\n",
1708               s,
1709               GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1710                                                       GNUNET_YES));
1711 }
1712
1713
1714 /**
1715  * Another peer has suggested an address for this
1716  * peer and transport plugin.  Check that this could be a valid
1717  * address.  If so, consider adding it to the list
1718  * of addresses.
1719  *
1720  * @param cls closure with the `struct Plugin`
1721  * @param addr pointer to the address
1722  * @param addrlen length of @a addr
1723  * @return #GNUNET_OK if this is a plausible address for this peer
1724  *         and transport; always returns #GNUNET_NO (this is the client!)
1725  */
1726 static int
1727 http_client_plugin_address_suggested (void *cls,
1728                                       const void *addr,
1729                                       size_t addrlen)
1730 {
1731   /* struct Plugin *plugin = cls; */
1732
1733   /* A HTTP/S client does not have any valid address so:*/
1734   return GNUNET_NO;
1735 }
1736
1737
1738 /**
1739  * Exit point from the plugin.
1740  *
1741  * @param cls api as closure
1742  * @return NULL
1743  */
1744 void *
1745 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1746 {
1747   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1748   struct HTTP_Client_Plugin *plugin = api->cls;
1749   struct Session *pos;
1750   struct Session *next;
1751
1752   if (NULL == api->cls)
1753   {
1754     /* Stub shutdown */
1755     GNUNET_free (api);
1756     return NULL;
1757   }
1758
1759   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1760                    _("Shutting down plugin `%s'\n"),
1761                    plugin->name);
1762
1763
1764   next = plugin->head;
1765   while (NULL != (pos = next))
1766   {
1767     next = pos->next;
1768     http_client_session_disconnect (plugin, pos);
1769   }
1770   if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1771   {
1772     GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1773     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1774   }
1775
1776
1777   if (NULL != plugin->curl_multi_handle)
1778   {
1779     curl_multi_cleanup (plugin->curl_multi_handle);
1780     plugin->curl_multi_handle = NULL;
1781   }
1782   curl_global_cleanup ();
1783
1784   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1785                    _("Shutdown for plugin `%s' complete\n"),
1786                    plugin->name);
1787
1788   GNUNET_free_non_null (plugin->proxy_hostname);
1789   GNUNET_free_non_null (plugin->proxy_username);
1790   GNUNET_free_non_null (plugin->proxy_password);
1791
1792   GNUNET_free (plugin);
1793   GNUNET_free (api);
1794   return NULL;
1795 }
1796
1797
1798 /**
1799  * Configure plugin
1800  *
1801  * @param plugin the plugin handle
1802  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1803  */
1804 static int
1805 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1806 {
1807   unsigned long long max_connections;
1808   char *proxy_type;
1809
1810
1811   /* Optional parameters */
1812   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1813                       plugin->name,
1814                       "MAX_CONNECTIONS", &max_connections))
1815     max_connections = 128;
1816   plugin->max_connections = max_connections;
1817
1818   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1819                    _("Maximum number of connections is %u\n"),
1820                    plugin->max_connections);
1821
1822   /* Read proxy configuration */
1823   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1824       plugin->name, "PROXY", &plugin->proxy_hostname))
1825   {
1826     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1827                      "Found proxy host: `%s'\n",
1828                      plugin->proxy_hostname);
1829     /* proxy username */
1830     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1831         plugin->name, "PROXY_USERNAME", &plugin->proxy_username))
1832     {
1833       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1834                        "Found proxy username name: `%s'\n",
1835                        plugin->proxy_username);
1836     }
1837
1838     /* proxy password */
1839     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1840         plugin->name, "PROXY_PASSWORD", &plugin->proxy_password))
1841     {
1842       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1843                        "Found proxy password name: `%s'\n",
1844                        plugin->proxy_password);
1845     }
1846
1847     /* proxy type */
1848     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1849         plugin->name, "PROXY_TYPE", &proxy_type))
1850     {
1851       GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
1852
1853       if (0 == strcmp(proxy_type, "HTTP"))
1854         plugin->proxytype = CURLPROXY_HTTP;
1855       else if (0 == strcmp(proxy_type, "SOCKS4"))
1856         plugin->proxytype = CURLPROXY_SOCKS4;
1857       else if (0 == strcmp(proxy_type, "SOCKS5"))
1858         plugin->proxytype = CURLPROXY_SOCKS5;
1859       else if (0 == strcmp(proxy_type, "SOCKS4A"))
1860         plugin->proxytype = CURLPROXY_SOCKS4A;
1861       else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
1862         plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
1863       else
1864       {
1865         GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1866              _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
1867              proxy_type);
1868
1869         GNUNET_free (proxy_type);
1870         GNUNET_free (plugin->proxy_hostname);
1871         plugin->proxy_hostname = NULL;
1872         GNUNET_free_non_null (plugin->proxy_username);
1873         plugin->proxy_username = NULL;
1874         GNUNET_free_non_null (plugin->proxy_password);
1875         plugin->proxy_password = NULL;
1876
1877         return GNUNET_SYSERR;
1878       }
1879
1880       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1881                        "Found proxy type: `%s'\n", proxy_type);
1882     }
1883
1884     /* proxy http tunneling */
1885     if (GNUNET_SYSERR == (plugin->proxy_use_httpproxytunnel == GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
1886         plugin->name, "PROXY_HTTP_TUNNELING")))
1887       plugin->proxy_use_httpproxytunnel = GNUNET_NO;
1888
1889     GNUNET_free_non_null (proxy_type);
1890   }
1891
1892
1893   return GNUNET_OK;
1894 }
1895
1896
1897 static const char *
1898 http_plugin_address_to_string (void *cls,
1899                                const void *addr,
1900                                size_t addrlen)
1901 {
1902   return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1903 }
1904
1905
1906 static void
1907 http_client_plugin_update_session_timeout (void *cls,
1908                                   const struct GNUNET_PeerIdentity *peer,
1909                                   struct Session *session)
1910 {
1911   struct HTTP_Client_Plugin *plugin = cls;
1912
1913   /* lookup if session is really existing */
1914   if (GNUNET_YES != client_exist_session (plugin, session))
1915   {
1916     GNUNET_break (0);
1917     return;
1918   }
1919   client_reschedule_session_timeout (session);
1920 }
1921
1922 /**
1923  * Entry point for the plugin.
1924  */
1925 void *
1926 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1927 {
1928   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1929   struct GNUNET_TRANSPORT_PluginFunctions *api;
1930   struct HTTP_Client_Plugin *plugin;
1931
1932   if (NULL == env->receive)
1933   {
1934     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1935        initialze the plugin or the API */
1936     api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1937     api->cls = NULL;
1938     api->address_to_string = &http_plugin_address_to_string;
1939     api->string_to_address = &http_common_plugin_string_to_address;
1940     api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1941     return api;
1942   }
1943
1944   plugin = GNUNET_new (struct HTTP_Client_Plugin);
1945   plugin->env = env;
1946   api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1947   api->cls = plugin;
1948   api->send = &http_client_plugin_send;
1949   api->disconnect_session = &http_client_session_disconnect;
1950   api->query_keepalive_factor = &http_client_query_keepalive_factor;
1951   api->disconnect_peer = &http_client_peer_disconnect;
1952   api->check_address = &http_client_plugin_address_suggested;
1953   api->get_session = &http_client_plugin_get_session;
1954   api->address_to_string = &http_plugin_address_to_string;
1955   api->string_to_address = &http_common_plugin_string_to_address;
1956   api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1957   api->get_network = &http_client_get_network;
1958   api->update_session_timeout = &http_client_plugin_update_session_timeout;
1959
1960 #if BUILD_HTTPS
1961   plugin->name = "transport-https_client";
1962   plugin->protocol = "https";
1963 #else
1964   plugin->name = "transport-http_client";
1965   plugin->protocol = "http";
1966 #endif
1967   plugin->last_tag = 1;
1968   plugin->options = 0; /* Setup options */
1969
1970   if (GNUNET_SYSERR == client_configure_plugin (plugin))
1971   {
1972     LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1973     return NULL;
1974   }
1975
1976   /* Start client */
1977   if (GNUNET_SYSERR == client_start (plugin))
1978   {
1979     LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1980     return NULL;
1981   }
1982   return api;
1983 }
1984
1985 /* end of plugin_transport_http_client.c */