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