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