2 This file is part of GNUnet
3 (C) 2002-2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/plugin_transport_http_client.c
23 * @brief HTTP/S client transport plugin
24 * @author Matthias Wachs
28 #define PLUGIN_NAME "https_client"
29 #define HTTP_STAT_STR_CONNECTIONS "# HTTPS client connections"
30 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
31 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
33 #define PLUGIN_NAME "http_client"
34 #define HTTP_STAT_STR_CONNECTIONS "# HTTP client connections"
35 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
36 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
39 #define VERBOSE_CURL GNUNET_YES
41 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
43 #define ENABLE_PUT GNUNET_YES
44 #define ENABLE_GET GNUNET_YES
47 #include "gnunet_util_lib.h"
48 #include "gnunet_protocols.h"
49 #include "gnunet_transport_plugin.h"
50 #include "plugin_transport_http_common.h"
51 #include <curl/curl.h>
56 * Encapsulation of all of the state of the plugin.
58 struct HTTP_Client_Plugin;
62 * Message to send using http
67 * next pointer for double linked list
69 struct HTTP_Message *next;
72 * previous pointer for double linked list
74 struct HTTP_Message *prev;
77 * buffer containing data to send
82 * amount of data already sent
92 * Continuation function to call once the transmission buffer
93 * has again space available. NULL if there is no
94 * continuation to call.
96 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
99 * Closure for transmit_cont.
101 void *transmit_cont_cls;
106 * Session handle for connections.
112 * A connection handle
115 struct ConnectionHandle
118 * The curl easy handle
123 * The related session
130 * Session handle for connections.
135 * To whom are we talking to (set to our identity
136 * if we are still waiting for the welcome message)
138 struct GNUNET_PeerIdentity target;
141 * Stored in a linked list.
143 struct Session *next;
146 * Stored in a linked list.
148 struct Session *prev;
151 * The URL to connect to
158 struct HttpAddress *addr;
166 * ATS network type in NBO
168 uint32_t ats_address_network_type;
171 * Pointer to the global plugin struct.
173 struct HTTP_Client_Plugin *plugin;
180 struct ConnectionHandle put;
181 struct ConnectionHandle get;
184 * Is the client PUT handle currently paused
189 * Is the client PUT handle disconnect in progress?
191 int put_tmp_disconnecting;
194 * Is the client PUT handle temporarily disconnected?
196 int put_tmp_disconnected;
199 * We received data to send while disconnecting, reconnect immediately
201 int put_reconnect_required;
204 * Client receive handle
209 * Outbound overhead due to HTTP connection
210 * Add to next message of this session when calling callback
215 * next pointer for double linked list
217 struct HTTP_Message *msg_head;
220 * previous pointer for double linked list
222 struct HTTP_Message *msg_tail;
225 * Message stream tokenizer for incoming data
227 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
230 * Session timeout task
232 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
235 * Session timeout task
237 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
240 * Task to wake up client receive handle when receiving is allowed again
242 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
245 * Absolute time when to receive data again
246 * Used for receive throttling
248 struct GNUNET_TIME_Absolute next_receive;
253 * Encapsulation of all of the state of the plugin.
255 struct HTTP_Client_Plugin
260 struct GNUNET_TRANSPORT_PluginEnvironment *env;
263 * Linked list head of open sessions.
265 struct Session *head;
268 * Linked list tail of open sessions.
270 struct Session *tail;
283 * My options to be included in the address
288 * Maximum number of sockets the plugin can use
289 * Each http inbound /outbound connections are two connections
291 unsigned int max_connections;
294 * Current number of sockets the plugin can use
295 * Each http inbound /outbound connections are two connections
297 unsigned int cur_connections;
300 * Last used unique HTTP connection tag
317 CURLM *curl_multi_handle;
322 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
327 * Increment session timeout due to activity for a session
328 * @param s the session
331 client_reschedule_session_timeout (struct Session *s);
335 * Function setting up file descriptors and scheduling task to run
337 * @param plugin plugin as closure
338 * @param now schedule task in 1ms, regardless of what curl may say
339 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
342 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
346 * Connect a HTTP put connection
348 * @param s the session to connect
349 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
352 client_connect_put (struct Session *s);
356 * Does a session s exists?
358 * @param plugin the plugin
359 * @param s desired session
360 * @return #GNUNET_YES or #GNUNET_NO
363 client_exist_session (struct HTTP_Client_Plugin *plugin,
366 struct Session * head;
368 for (head = plugin->head; head != NULL; head = head->next)
378 * @param curl the curl easy handle
379 * @param type message type
380 * @param data data to log, NOT a 0-terminated string
381 * @param size data length
382 * @param cls the closure
386 client_log (CURL *curl, curl_infotype type,
387 const char *data, size_t size, void *cls)
389 struct ConnectionHandle *ch = cls;
390 const char *ttype = "UNSPECIFIED";
392 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
400 case CURLINFO_HEADER_IN:
403 case CURLINFO_HEADER_OUT:
404 ttype = "HEADER_OUT";
407 GNUNET_assert (NULL != ch);
408 GNUNET_assert (NULL != ch->easyhandle);
409 GNUNET_assert (NULL != ch->s);
410 ch->s->overhead += size;
413 ttype = "UNSPECIFIED";
417 memcpy (text, data, size);
418 if (text[size - 1] == '\n')
423 text[size + 1] = '\0';
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
427 "Connection %p %s: %s", ch->easyhandle, ttype, text);
429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
430 "Connection %p %s: %s", ch->easyhandle, ttype, text);
438 * Function that can be used by the transport service to transmit
439 * a message using the plugin. Note that in the case of a
440 * peer disconnecting, the continuation MUST be called
441 * prior to the disconnect notification itself. This function
442 * will be called with this peer's HELLO message to initiate
443 * a fresh connection to another peer.
446 * @param s which session must be used
447 * @param msgbuf the message to transmit
448 * @param msgbuf_size number of bytes in 'msgbuf'
449 * @param priority how important is the message (most plugins will
450 * ignore message priority and just FIFO)
451 * @param to how long to wait at most for the transmission (does not
452 * require plugins to discard the message after the timeout,
453 * just advisory for the desired delay; most plugins will ignore
455 * @param cont continuation to call once the message has
456 * been transmitted (or if the transport is ready
457 * for the next transmission call; or if the
458 * peer disconnected...); can be NULL
459 * @param cont_cls closure for cont
460 * @return number of bytes used (on the physical network, with overheads);
461 * -1 on hard errors (i.e. address invalid); 0 is a legal value
462 * and does NOT mean that the message was not transmitted (DV)
465 http_client_plugin_send (void *cls,
467 const char *msgbuf, size_t msgbuf_size,
468 unsigned int priority,
469 struct GNUNET_TIME_Relative to,
470 GNUNET_TRANSPORT_TransmitContinuation cont,
473 struct HTTP_Client_Plugin *plugin = cls;
474 struct HTTP_Message *msg;
477 /* lookup if session is really existing */
478 if (GNUNET_YES != client_exist_session (plugin, s))
480 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
481 "Trying to send with invalid session %p \n", s);
482 return GNUNET_SYSERR;
485 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
486 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
488 msgbuf_size, GNUNET_i2s (&s->target));
490 /* create new message and schedule */
491 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
493 msg->size = msgbuf_size;
495 msg->buf = (char *) &msg[1];
496 msg->transmit_cont = cont;
497 msg->transmit_cont_cls = cont_cls;
498 memcpy (msg->buf, msgbuf, msgbuf_size);
499 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
501 GNUNET_asprintf (&stat_txt,
502 "# bytes currently in %s_client buffers",
504 GNUNET_STATISTICS_update (plugin->env->stats,
505 stat_txt, msgbuf_size, GNUNET_NO);
506 GNUNET_free (stat_txt);
508 if (GNUNET_YES == s->put_tmp_disconnecting)
510 /* PUT connection is currently getting disconnected */
511 s->put_reconnect_required = GNUNET_YES;
512 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
513 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
517 else if (GNUNET_YES == s->put_paused)
519 /* PUT connection was paused, unpause */
520 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
521 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
522 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
523 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
524 "Session %p/connection %p: unpausing connection\n",
526 s->put_paused = GNUNET_NO;
527 if (NULL != s->client_put)
528 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
530 else if (GNUNET_YES == s->put_tmp_disconnected)
532 /* PUT connection was disconnected, reconnect */
533 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
534 "Session %p: Reconnecting PUT connection\n",
536 s->put_tmp_disconnected = GNUNET_NO;
537 GNUNET_break (s->client_put == NULL);
538 if (GNUNET_SYSERR == client_connect_put (s))
540 return GNUNET_SYSERR;
544 client_schedule (s->plugin, GNUNET_YES);
552 * @param s the session to delete
555 client_delete_session (struct Session *s)
557 struct HTTP_Client_Plugin *plugin = s->plugin;
558 struct HTTP_Message *pos;
559 struct HTTP_Message *next;
561 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
563 GNUNET_SCHEDULER_cancel (s->timeout_task);
564 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
566 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
568 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
569 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
572 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
575 while (NULL != (pos = next))
578 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
579 if (pos->transmit_cont != NULL)
580 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
581 pos->size, pos->pos + s->overhead);
586 if (s->msg_tk != NULL)
588 GNUNET_SERVER_mst_destroy (s->msg_tk);
591 GNUNET_free (s->addr);
592 GNUNET_free (s->url);
598 * Disconnect a session
600 * @param cls the `struct HTTP_Client_Plugin`
602 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
605 http_client_session_disconnect (void *cls,
608 struct HTTP_Client_Plugin *plugin = cls;
609 struct HTTP_Message *msg;
610 struct HTTP_Message *t;
614 if (GNUNET_YES != client_exist_session (plugin, s))
617 return GNUNET_SYSERR;
620 if (NULL != s->client_put)
622 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
623 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
624 s, s->client_put, GNUNET_i2s (&s->target));
626 /* remove curl handle from multi handle */
627 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
628 if (mret != CURLM_OK)
630 /* clean up easy handle, handle is now invalid and free'd */
634 curl_easy_cleanup (s->client_put);
635 s->client_put = NULL;
639 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
641 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
642 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
645 if (NULL != s->client_get)
647 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
648 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
650 GNUNET_i2s (&s->target));
651 /* remove curl handle from multi handle */
652 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
653 if (mret != CURLM_OK)
655 /* clean up easy handle, handle is now invalid and free'd */
659 curl_easy_cleanup (s->client_get);
660 s->client_get = NULL;
667 if (NULL != msg->transmit_cont)
668 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
669 msg->size, msg->pos + s->overhead);
671 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
676 GNUNET_assert (plugin->cur_connections >= 2);
677 plugin->cur_connections -= 2;
678 GNUNET_STATISTICS_set (plugin->env->stats,
679 HTTP_STAT_STR_CONNECTIONS,
680 plugin->cur_connections,
682 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
683 "Session %p: notifying transport about ending session\n",s);
685 plugin->env->session_end (plugin->env->cls, &s->target, s);
686 client_delete_session (s);
688 /* Re-schedule since handles have changed */
689 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
691 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
692 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
694 client_schedule (plugin, GNUNET_YES);
701 * Function that is called to get the keepalive factor.
702 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
703 * calculate the interval between keepalive packets.
705 * @param cls closure with the `struct Plugin`
706 * @return keepalive factor
709 http_client_query_keepalive_factor (void *cls)
716 * Function that can be used to force the plugin to disconnect
717 * from the given peer and cancel all previous transmissions
718 * (and their continuationc).
721 * @param target peer from which to disconnect
724 http_client_peer_disconnect (void *cls,
725 const struct GNUNET_PeerIdentity *target)
727 struct HTTP_Client_Plugin *plugin = cls;
728 struct Session *next = NULL;
729 struct Session *pos = NULL;
731 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
732 "Transport tells me to disconnect `%s'\n",
733 GNUNET_i2s (target));
736 while (NULL != (pos = next))
739 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
741 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
742 "Disconnecting session %p to `%pos'\n",
743 pos, GNUNET_i2s (target));
744 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (plugin,
752 * Check if a sessions exists for an specific address
754 * @param plugin the plugin
755 * @param address the address
756 * @return the session or NULL
758 static struct Session *
759 client_lookup_session (struct HTTP_Client_Plugin *plugin,
760 const struct GNUNET_HELLO_Address *address)
764 for (pos = plugin->head; NULL != pos; pos = pos->next)
765 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
766 (address->address_length == pos->addrlen) &&
767 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
774 client_put_disconnect (void *cls,
775 const struct GNUNET_SCHEDULER_TaskContext *tc)
777 struct Session *s = cls;
779 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
780 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
781 "Session %p/connection %p: will be disconnected due to no activity\n",
783 s->put_paused = GNUNET_NO;
784 s->put_tmp_disconnecting = GNUNET_YES;
785 if (NULL != s->client_put)
786 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
787 client_schedule (s->plugin, GNUNET_YES);
792 * Callback method used with libcurl
793 * Method is called when libcurl needs to read data during sending
795 * @param stream pointer where to write data
796 * @param size size of an individual element
797 * @param nmemb count of elements that can be written to the buffer
798 * @param cls our `struct Session`
799 * @return bytes written to stream, returning 0 will terminate connection!
802 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
804 struct Session *s = cls;
805 struct HTTP_Client_Plugin *plugin = s->plugin;
806 struct HTTP_Message *msg = s->msg_head;
810 if (GNUNET_YES != client_exist_session (plugin, s))
815 if (GNUNET_YES == s->put_tmp_disconnecting)
818 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
819 "Session %p/connection %p: disconnect due to inactivity\n",
826 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
827 "Session %p/connection %p: nothing to send, suspending\n",
829 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
830 s->put_paused = GNUNET_YES;
831 return CURL_READFUNC_PAUSE;
834 GNUNET_assert (msg->pos < msg->size);
835 /* calculate how much fits in buffer */
836 len = GNUNET_MIN (msg->size - msg->pos,
838 memcpy (stream, &msg->buf[msg->pos], len);
840 if (msg->pos == msg->size)
842 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
843 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
844 s, s->client_put, msg->size, msg->pos);
845 /* Calling transmit continuation */
846 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
847 if (NULL != msg->transmit_cont)
848 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
849 msg->size, msg->size + s->overhead);
854 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
855 GNUNET_STATISTICS_update (plugin->env->stats,
856 stat_txt, -len, GNUNET_NO);
857 GNUNET_free (stat_txt);
859 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
860 GNUNET_STATISTICS_update (plugin->env->stats,
861 stat_txt, len, GNUNET_NO);
862 GNUNET_free (stat_txt);
868 * Wake up a curl handle which was suspended
870 * @param cls the session
871 * @param tc task context
874 client_wake_up (void *cls,
875 const struct GNUNET_SCHEDULER_TaskContext *tc)
877 struct Session *s = cls;
878 struct HTTP_Client_Plugin *p = s->plugin;
880 if (GNUNET_YES != client_exist_session (p, s))
885 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
886 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
888 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
889 "Session %p/connection %p: Waking up GET handle\n",
892 s->put_paused = GNUNET_NO;
893 if (NULL != s->client_get)
894 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
900 * Callback for message stream tokenizer
902 * @param cls the session
903 * @param client not used
904 * @param message the message received
905 * @return always #GNUNET_OK
908 client_receive_mst_cb (void *cls, void *client,
909 const struct GNUNET_MessageHeader *message)
911 struct Session *s = cls;
912 struct HTTP_Client_Plugin *plugin;
913 struct GNUNET_TIME_Relative delay;
914 struct GNUNET_ATS_Information atsi;
918 if (GNUNET_YES != client_exist_session (plugin, s))
924 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
925 atsi.value = s->ats_address_network_type;
926 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
927 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
928 s, (const char *) s->addr, s->addrlen);
930 plugin->env->update_address_metrics (plugin->env->cls,
937 GNUNET_asprintf (&stat_txt,
938 "# bytes received via %s_client",
940 GNUNET_STATISTICS_update (plugin->env->stats,
941 stat_txt, ntohs(message->size), GNUNET_NO);
942 GNUNET_free (stat_txt);
945 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
947 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
949 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
950 "Client: peer `%s' address `%s' next read delayed for %s\n",
951 GNUNET_i2s (&s->target),
952 http_common_plugin_address_to_string (NULL,
954 s->addr, s->addrlen),
955 GNUNET_STRINGS_relative_time_to_string (delay,
958 client_reschedule_session_timeout (s);
964 * Callback method used with libcurl when data for a PUT connection are
965 * received. We do not expect data here, so we just dismiss it
967 * @param stream pointer where to write data
968 * @param size size of an individual element
969 * @param nmemb count of elements that can be written to the buffer
970 * @param cls destination pointer, passed to the libcurl handle
971 * @return bytes read from stream
974 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
981 * Callback method used with libcurl when data for a GET connection are
982 * received. Forward to MST
984 * @param stream pointer where to write data
985 * @param size size of an individual element
986 * @param nmemb count of elements that can be written to the buffer
987 * @param cls destination pointer, passed to the libcurl handle
988 * @return bytes read from stream
991 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
993 struct Session *s = cls;
994 struct GNUNET_TIME_Absolute now;
995 size_t len = size * nmemb;
997 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
998 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
1000 len, GNUNET_i2s (&s->target));
1001 now = GNUNET_TIME_absolute_get ();
1002 if (now.abs_value_us < s->next_receive.abs_value_us)
1004 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1005 struct GNUNET_TIME_Relative delta =
1006 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1007 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1008 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1010 GNUNET_STRINGS_relative_time_to_string (delta,
1012 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1014 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1015 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1017 s->recv_wakeup_task =
1018 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1019 return CURL_WRITEFUNC_PAUSE;
1021 if (NULL == s->msg_tk)
1022 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1023 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1029 * Task performing curl operations
1031 * @param cls plugin as closure
1032 * @param tc gnunet scheduler task context
1035 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1039 * Function setting up file descriptors and scheduling task to run
1041 * @param plugin the plugin as closure
1042 * @param now schedule task in 1ms, regardless of what curl may say
1043 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1046 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1052 struct GNUNET_NETWORK_FDSet *grs;
1053 struct GNUNET_NETWORK_FDSet *gws;
1056 struct GNUNET_TIME_Relative timeout;
1058 /* Cancel previous scheduled task */
1059 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1061 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1062 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1068 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1069 if (mret != CURLM_OK)
1071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1072 "curl_multi_fdset", __FILE__, __LINE__,
1073 curl_multi_strerror (mret));
1074 return GNUNET_SYSERR;
1076 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1078 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1080 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1081 if (now == GNUNET_YES)
1082 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1084 if (mret != CURLM_OK)
1086 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1087 "curl_multi_timeout", __FILE__, __LINE__,
1088 curl_multi_strerror (mret));
1089 return GNUNET_SYSERR;
1092 grs = GNUNET_NETWORK_fdset_create ();
1093 gws = GNUNET_NETWORK_fdset_create ();
1094 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1095 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1097 plugin->client_perform_task =
1098 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1100 &client_run, plugin);
1101 GNUNET_NETWORK_fdset_destroy (gws);
1102 GNUNET_NETWORK_fdset_destroy (grs);
1108 * Task performing curl operations
1110 * @param cls plugin as closure
1111 * @param tc gnunet scheduler task context
1114 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1116 struct HTTP_Client_Plugin *plugin = cls;
1118 long http_statuscode;
1121 GNUNET_assert (cls != NULL);
1123 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1124 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1130 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1135 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1137 CURL *easy_h = msg->easy_handle;
1138 struct Session *s = NULL;
1139 char *d = (char *) s;
1144 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1145 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1147 curl_easy_strerror (msg->data.result), running);
1151 GNUNET_assert (CURLE_OK ==
1152 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1153 s = (struct Session *) d;
1155 if (GNUNET_YES != client_exist_session(plugin, s))
1161 GNUNET_assert (s != NULL);
1162 if (msg->msg == CURLMSG_DONE)
1164 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1165 if (easy_h == s->client_put)
1167 if ((0 != msg->data.result) || (http_statuscode != 200))
1169 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1170 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1171 s, msg->easy_handle, GNUNET_i2s (&s->target),
1174 curl_easy_strerror (msg->data.result));
1177 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1178 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1179 s, msg->easy_handle, GNUNET_i2s (&s->target));
1180 if (s->client_get == NULL)
1182 /* Disconnect other transmission direction and tell transport */
1184 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1185 curl_easy_cleanup (easy_h);
1186 s->put_tmp_disconnecting = GNUNET_NO;
1187 s->put_tmp_disconnected = GNUNET_YES;
1188 s->client_put = NULL;
1189 s->put.easyhandle = NULL;
1193 * Handling a rare case:
1194 * plugin_send was called during temporary put disconnect,
1195 * reconnect required after connection was disconnected
1197 if (GNUNET_YES == s->put_reconnect_required)
1199 s->put_reconnect_required = GNUNET_NO;
1200 if (GNUNET_SYSERR == client_connect_put(s))
1202 GNUNET_break (s->client_put == NULL);
1203 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1207 if (easy_h == s->client_get)
1209 if ((0 != msg->data.result) || (http_statuscode != 200))
1211 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1212 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1213 s, msg->easy_handle, GNUNET_i2s (&s->target),
1216 curl_easy_strerror (msg->data.result));
1220 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1221 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1222 s, msg->easy_handle, GNUNET_i2s (&s->target));
1223 /* Disconnect other transmission direction and tell transport */
1224 s->get.easyhandle = NULL;
1226 http_client_session_disconnect (plugin, s);
1231 while (mret == CURLM_CALL_MULTI_PERFORM);
1232 client_schedule (plugin, GNUNET_NO);
1237 * Connect GET connection for a session
1239 * @param s the session to connect
1240 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1243 client_connect_get (struct Session *s)
1247 /* create get connection */
1248 s->client_get = curl_easy_init ();
1250 s->get.easyhandle = s->client_get;
1252 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1253 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1254 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1257 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1258 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1259 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1261 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1262 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1266 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1267 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1269 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1270 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1272 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1273 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1276 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1277 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1278 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1279 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1280 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1281 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1282 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1283 /* No timeout by default, timeout done with session timeout */
1284 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1285 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1286 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1287 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1288 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1289 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1290 #if CURL_TCP_NODELAY
1291 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1293 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1295 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1296 if (mret != CURLM_OK)
1298 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1299 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1301 curl_multi_strerror (mret));
1302 curl_easy_cleanup (s->client_get);
1303 s->client_get = NULL;
1305 s->get.easyhandle = NULL;
1307 return GNUNET_SYSERR;
1315 * Connect a HTTP put connection
1317 * @param s the session to connect
1318 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1321 client_connect_put (struct Session *s)
1325 /* create put connection */
1326 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1327 "Session %p : Init PUT handle\n", s);
1328 s->client_put = curl_easy_init ();
1330 s->put.easyhandle = s->client_put;
1332 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1333 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1334 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1337 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1338 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1339 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1341 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1342 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1346 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1347 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1349 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1350 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1352 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1353 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1355 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1356 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1357 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1358 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1359 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1360 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1361 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1362 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1363 /* No timeout by default, timeout done with session timeout */
1364 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1365 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1366 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1367 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1368 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1369 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1370 #if CURL_TCP_NODELAY
1371 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1373 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1374 if (mret != CURLM_OK)
1376 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1377 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1379 curl_multi_strerror (mret));
1380 curl_easy_cleanup (s->client_put);
1381 s->client_put = NULL;
1382 s->put.easyhandle = NULL;
1384 s->put_tmp_disconnected = GNUNET_YES;
1385 return GNUNET_SYSERR;
1387 s->put_tmp_disconnected = GNUNET_NO;
1393 * Connect both PUT and GET connection for a session
1395 * @param s the session to connect
1396 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1399 client_connect (struct Session *s)
1402 struct HTTP_Client_Plugin *plugin = s->plugin;
1403 int res = GNUNET_OK;
1406 if (NULL == http_common_plugin_address_to_string (NULL,
1408 s->addr, s->addrlen))
1410 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1412 "Invalid address peer `%s'\n",
1413 GNUNET_i2s (&s->target));
1414 return GNUNET_SYSERR;
1417 GNUNET_asprintf (&s->url, "%s/%s;%u",
1418 http_common_plugin_address_to_url (NULL, s->addr, s->addrlen),
1419 GNUNET_i2s_full (plugin->env->my_identity),
1424 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1425 "Initiating outbound session peer `%s' using address `%s'\n",
1426 GNUNET_i2s (&s->target), s->url);
1428 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1429 (GNUNET_SYSERR == client_connect_put (s)))
1432 return GNUNET_SYSERR;
1435 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1437 "Session %p: connected with connections GET %p and PUT %p\n",
1438 s, s->client_get, s->client_put);
1440 /* Perform connect */
1441 plugin->cur_connections += 2;
1442 GNUNET_STATISTICS_set (plugin->env->stats,
1443 HTTP_STAT_STR_CONNECTIONS,
1444 plugin->cur_connections,
1447 /* Re-schedule since handles have changed */
1448 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1450 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1451 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1453 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1459 * Function obtain the network type for a session
1461 * @param cls closure (`struct Plugin*`)
1462 * @param session the session
1463 * @return the network type
1465 static enum GNUNET_ATS_Network_Type
1466 http_client_get_network (void *cls,
1467 struct Session *session)
1469 return ntohl (session->ats_address_network_type);
1474 * Session was idle, so disconnect it
1476 * @param cls the `struct Session` of the idle session
1477 * @param tc scheduler context
1480 client_session_timeout (void *cls,
1481 const struct GNUNET_SCHEDULER_TaskContext *tc)
1483 struct Session *s = cls;
1485 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1486 GNUNET_log (TIMEOUT_LOG,
1487 "Session %p was idle for %s, disconnecting\n",
1489 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1492 /* call session destroy function */
1493 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (s->plugin,
1499 * Creates a new outbound session the transport service will use to
1500 * send data to the peer
1502 * @param cls the plugin
1503 * @param address the address
1504 * @return the session or NULL of max connections exceeded
1506 static struct Session *
1507 http_client_plugin_get_session (void *cls,
1508 const struct GNUNET_HELLO_Address *address)
1510 struct HTTP_Client_Plugin *plugin = cls;
1511 struct Session * s = NULL;
1512 struct sockaddr *sa;
1513 struct GNUNET_ATS_Information ats;
1517 GNUNET_assert (address->address != NULL);
1519 /* find existing session */
1520 s = client_lookup_session (plugin, address);
1524 if (plugin->max_connections <= plugin->cur_connections)
1526 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1527 "Maximum number of connections (%u) reached: "
1528 "cannot connect to peer `%s'\n",
1529 plugin->max_connections,
1530 GNUNET_i2s (&address->peer));
1534 /* Determine network location */
1535 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1536 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1537 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1538 if (GNUNET_SYSERR == res)
1542 else if (GNUNET_YES == res)
1544 GNUNET_assert (NULL != sa);
1545 if (AF_INET == sa->sa_family)
1547 salen = sizeof (struct sockaddr_in);
1549 else if (AF_INET6 == sa->sa_family)
1551 salen = sizeof (struct sockaddr_in6);
1553 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1554 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1557 else if (GNUNET_NO == res)
1559 /* Cannot convert to sockaddr -> is external hostname */
1560 ats.value = htonl (GNUNET_ATS_NET_WAN);
1562 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1568 s = GNUNET_new (struct Session);
1569 s->target = address->peer;
1571 s->addr = GNUNET_malloc (address->address_length);
1572 memcpy (s->addr, address->address, address->address_length);
1573 s->addrlen = address->address_length;
1574 s->ats_address_network_type = ats.value;
1575 s->put_paused = GNUNET_NO;
1576 s->put_tmp_disconnecting = GNUNET_NO;
1577 s->put_tmp_disconnected = GNUNET_NO;
1578 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1579 &client_session_timeout,
1581 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1582 "Created new session %p for `%s' address `%s''\n",
1584 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1585 GNUNET_i2s (&s->target));
1587 /* add new session */
1588 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1590 /* initiate new connection */
1591 if (GNUNET_SYSERR == client_connect (s))
1593 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1594 "Cannot connect to peer `%s' address `%s''\n",
1595 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1596 GNUNET_i2s (&s->target));
1597 client_delete_session (s);
1605 * Setup http_client plugin
1607 * @param plugin the plugin handle
1608 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1611 client_start (struct HTTP_Client_Plugin *plugin)
1613 curl_global_init (CURL_GLOBAL_ALL);
1614 plugin->curl_multi_handle = curl_multi_init ();
1616 if (NULL == plugin->curl_multi_handle)
1618 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1619 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1621 return GNUNET_SYSERR;
1628 * Increment session timeout due to activity for session s
1630 * param s the session
1633 client_reschedule_session_timeout (struct Session *s)
1635 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1636 GNUNET_SCHEDULER_cancel (s->timeout_task);
1637 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1638 &client_session_timeout,
1640 GNUNET_log (TIMEOUT_LOG,
1641 "Timeout rescheduled for session %p set to %s\n",
1643 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1649 * Another peer has suggested an address for this
1650 * peer and transport plugin. Check that this could be a valid
1651 * address. If so, consider adding it to the list
1654 * @param cls closure with the `struct Plugin`
1655 * @param addr pointer to the address
1656 * @param addrlen length of @a addr
1657 * @return #GNUNET_OK if this is a plausible address for this peer
1658 * and transport; always returns #GNUNET_NO (this is the client!)
1661 http_client_plugin_address_suggested (void *cls,
1665 /* struct Plugin *plugin = cls; */
1667 /* A HTTP/S client does not have any valid address so:*/
1673 * Exit point from the plugin.
1675 * @param cls api as closure
1679 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1681 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1682 struct HTTP_Client_Plugin *plugin = api->cls;
1683 struct Session *pos;
1684 struct Session *next;
1686 if (NULL == api->cls)
1693 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1694 _("Shutting down plugin `%s'\n"),
1698 next = plugin->head;
1699 while (NULL != (pos = next))
1702 http_client_session_disconnect (plugin, pos);
1704 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1706 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1707 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1711 if (NULL != plugin->curl_multi_handle)
1713 curl_multi_cleanup (plugin->curl_multi_handle);
1714 plugin->curl_multi_handle = NULL;
1716 curl_global_cleanup ();
1718 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1719 _("Shutdown for plugin `%s' complete\n"),
1722 GNUNET_free (plugin);
1731 * @param plugin the plugin handle
1732 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1735 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1737 unsigned long long max_connections;
1739 /* Optional parameters */
1740 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1742 "MAX_CONNECTIONS", &max_connections))
1743 max_connections = 128;
1744 plugin->max_connections = max_connections;
1746 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1747 _("Maximum number of connections is %u\n"),
1748 plugin->max_connections);
1754 http_plugin_address_to_string (void *cls,
1758 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1762 http_client_plugin_update_session_timeout (void *cls,
1763 const struct GNUNET_PeerIdentity *peer,
1764 struct Session *session)
1766 struct HTTP_Client_Plugin *plugin = cls;
1768 /* lookup if session is really existing */
1769 if (GNUNET_YES != client_exist_session (plugin, session))
1772 client_reschedule_session_timeout (session);
1776 * Entry point for the plugin.
1779 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1781 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1782 struct GNUNET_TRANSPORT_PluginFunctions *api;
1783 struct HTTP_Client_Plugin *plugin;
1785 if (NULL == env->receive)
1787 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1788 initialze the plugin or the API */
1789 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1791 api->address_to_string = &http_plugin_address_to_string;
1792 api->string_to_address = &http_common_plugin_string_to_address;
1793 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1797 plugin = GNUNET_new (struct HTTP_Client_Plugin);
1799 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1801 api->send = &http_client_plugin_send;
1802 api->disconnect_session = &http_client_session_disconnect;
1803 api->query_keepalive_factor = &http_client_query_keepalive_factor;
1804 api->disconnect_peer = &http_client_peer_disconnect;
1805 api->check_address = &http_client_plugin_address_suggested;
1806 api->get_session = &http_client_plugin_get_session;
1807 api->address_to_string = &http_plugin_address_to_string;
1808 api->string_to_address = &http_common_plugin_string_to_address;
1809 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1810 api->get_network = &http_client_get_network;
1811 api->update_session_timeout = &http_client_plugin_update_session_timeout;
1814 plugin->name = "transport-https_client";
1815 plugin->protocol = "https";
1817 plugin->name = "transport-http_client";
1818 plugin->protocol = "http";
1820 plugin->last_tag = 1;
1821 plugin->options = 0; /* Setup options */
1823 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1825 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1830 if (GNUNET_SYSERR == client_start (plugin))
1832 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1838 /* end of plugin_transport_http_client.c */