2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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.
111 * A connection handle
114 struct ConnectionHandle
117 * The curl easy handle
122 * 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 * Encapsulation of all of the state of the plugin.
329 struct HTTP_Client_Plugin *p;
333 * Start session timeout for a session
334 * @param s the session
337 client_start_session_timeout (struct Session *s);
341 * Increment session timeout due to activity for a session
342 * @param s the session
345 client_reschedule_session_timeout (struct Session *s);
349 * Cancel timeout for a session
350 * @param s the session
353 client_stop_session_timeout (struct Session *s);
357 * Function setting up file descriptors and scheduling task to run
359 * @param plugin plugin as closure
360 * @param now schedule task in 1ms, regardless of what curl may say
361 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
364 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
368 * Connect a HTTP put connection
370 * @param s the session to connect
371 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for success
374 client_connect_put (struct Session *s);
378 * Does a session s exists?
380 * @param plugin the plugin
381 * @param s desired session
382 * @return GNUNET_YES or GNUNET_NO
385 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
387 struct Session * head;
389 GNUNET_assert (NULL != plugin);
390 GNUNET_assert (NULL != s);
392 for (head = plugin->head; head != NULL; head = head->next)
404 * @param curl the curl easy handle
405 * @param type message type
406 * @param data data to log, NOT a 0-terminated string
407 * @param size data length
408 * @param cls the closure
412 client_log (CURL *curl, curl_infotype type,
413 const char *data, size_t size, void *cls)
415 struct ConnectionHandle *ch = cls;
416 const char *ttype = "UNSPECIFIED";
418 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
426 case CURLINFO_HEADER_IN:
429 case CURLINFO_HEADER_OUT:
430 ttype = "HEADER_OUT";
433 GNUNET_assert (NULL != ch);
434 GNUNET_assert (NULL != ch->easyhandle);
435 GNUNET_assert (NULL != ch->s);
436 ch->s->overhead += size;
439 ttype = "UNSPECIFIED";
443 memcpy (text, data, size);
444 if (text[size - 1] == '\n')
449 text[size + 1] = '\0';
452 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
453 "Connection %p %s: %s", ch->easyhandle, ttype, text);
455 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
456 "Connection %p %s: %s", ch->easyhandle, ttype, text);
464 * Function that can be used by the transport service to transmit
465 * a message using the plugin. Note that in the case of a
466 * peer disconnecting, the continuation MUST be called
467 * prior to the disconnect notification itself. This function
468 * will be called with this peer's HELLO message to initiate
469 * a fresh connection to another peer.
472 * @param s which session must be used
473 * @param msgbuf the message to transmit
474 * @param msgbuf_size number of bytes in 'msgbuf'
475 * @param priority how important is the message (most plugins will
476 * ignore message priority and just FIFO)
477 * @param to how long to wait at most for the transmission (does not
478 * require plugins to discard the message after the timeout,
479 * just advisory for the desired delay; most plugins will ignore
481 * @param cont continuation to call once the message has
482 * been transmitted (or if the transport is ready
483 * for the next transmission call; or if the
484 * peer disconnected...); can be NULL
485 * @param cont_cls closure for cont
486 * @return number of bytes used (on the physical network, with overheads);
487 * -1 on hard errors (i.e. address invalid); 0 is a legal value
488 * and does NOT mean that the message was not transmitted (DV)
491 http_client_plugin_send (void *cls,
493 const char *msgbuf, size_t msgbuf_size,
494 unsigned int priority,
495 struct GNUNET_TIME_Relative to,
496 GNUNET_TRANSPORT_TransmitContinuation cont,
499 struct HTTP_Client_Plugin *plugin = cls;
500 struct HTTP_Message *msg;
503 GNUNET_assert (plugin != NULL);
504 GNUNET_assert (s != NULL);
506 /* lookup if session is really existing */
507 if (GNUNET_YES != client_exist_session (plugin, s))
510 return GNUNET_SYSERR;
513 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
514 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
516 msgbuf_size, GNUNET_i2s (&s->target));
518 /* create new message and schedule */
519 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
521 msg->size = msgbuf_size;
523 msg->buf = (char *) &msg[1];
524 msg->transmit_cont = cont;
525 msg->transmit_cont_cls = cont_cls;
526 memcpy (msg->buf, msgbuf, msgbuf_size);
527 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
529 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
530 GNUNET_STATISTICS_update (plugin->env->stats,
531 stat_txt, msgbuf_size, GNUNET_NO);
533 GNUNET_free (stat_txt);
535 if (GNUNET_YES == s->put_tmp_disconnecting)
537 /* PUT connection is currently getting disconnected */
538 s->put_reconnect_required = GNUNET_YES;
539 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
540 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
544 else if (GNUNET_YES == s->put_paused)
546 /* PUT connection was paused, unpause */
547 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
548 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
549 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
550 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
551 "Session %p/connection %p: unpausing connection\n",
553 s->put_paused = GNUNET_NO;
554 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
556 else if (GNUNET_YES == s->put_tmp_disconnected)
558 /* PUT connection was disconnected, reconnect */
559 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
560 "Session %p: Reconnecting PUT connection\n",
562 s->put_tmp_disconnected = GNUNET_NO;
563 GNUNET_break (s->client_put == NULL);
564 if (GNUNET_SYSERR == client_connect_put (s))
566 return GNUNET_SYSERR;
570 client_schedule (s->plugin, GNUNET_YES);
571 client_reschedule_session_timeout (s);
579 * @param s the session to delete
582 client_delete_session (struct Session *s)
584 struct HTTP_Client_Plugin *plugin = s->plugin;
585 struct HTTP_Message *pos;
586 struct HTTP_Message *next;
588 client_stop_session_timeout (s);
590 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
592 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
593 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
596 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
599 while (NULL != (pos = next))
602 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
603 if (pos->transmit_cont != NULL)
604 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
605 pos->size, pos->pos + s->overhead);
610 if (s->msg_tk != NULL)
612 GNUNET_SERVER_mst_destroy (s->msg_tk);
615 GNUNET_free (s->addr);
616 GNUNET_free (s->url);
623 * Disconnect a session
626 * @return GNUNET_OK on success, GNUNET_SYSERR on error
629 client_disconnect (struct Session *s)
631 struct HTTP_Client_Plugin *plugin = s->plugin;
632 struct HTTP_Message *msg;
633 struct HTTP_Message *t;
637 if (GNUNET_YES != client_exist_session (plugin, s))
640 return GNUNET_SYSERR;
643 if (s->client_put != NULL)
645 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
646 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
647 s, s->client_put, GNUNET_i2s (&s->target));
649 /* remove curl handle from multi handle */
650 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
651 if (mret != CURLM_OK)
653 /* clean up easy handle, handle is now invalid and free'd */
657 curl_easy_cleanup (s->client_put);
658 s->client_put = NULL;
662 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
664 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
665 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
668 if (s->client_get != NULL)
670 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
671 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
672 s, s->client_get, GNUNET_i2s (&s->target));
674 /* remove curl handle from multi handle */
675 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
676 if (mret != CURLM_OK)
678 /* clean up easy handle, handle is now invalid and free'd */
682 curl_easy_cleanup (s->client_get);
683 s->client_get = NULL;
690 if (NULL != msg->transmit_cont)
691 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
692 msg->size, msg->pos + s->overhead);
694 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
699 GNUNET_assert (plugin->cur_connections >= 2);
700 plugin->cur_connections -= 2;
701 GNUNET_STATISTICS_set (plugin->env->stats,
702 HTTP_STAT_STR_CONNECTIONS,
703 plugin->cur_connections,
706 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
707 "Session %p: notifying transport about ending session\n",s);
709 plugin->env->session_end (plugin->env->cls, &s->target, s);
710 client_delete_session (s);
712 /* Re-schedule since handles have changed */
713 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
715 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
716 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
718 client_schedule (plugin, GNUNET_YES);
725 * Function that can be used to force the plugin to disconnect
726 * from the given peer and cancel all previous transmissions
727 * (and their continuationc).
730 * @param target peer from which to disconnect
733 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
735 struct HTTP_Client_Plugin *plugin = cls;
736 struct Session *next = NULL;
737 struct Session *pos = NULL;
739 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
740 "Transport tells me to disconnect `%s'\n",
741 GNUNET_i2s (target));
744 while (NULL != (pos = next))
747 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
749 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
750 "Disconnecting session %p to `%pos'\n",
751 pos, GNUNET_i2s (target));
752 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
759 * Check if a sessions exists for an specific address
761 * @param plugin the plugin
762 * @param address the address
763 * @return the session or NULL
765 static struct Session *
766 client_lookup_session (struct HTTP_Client_Plugin *plugin,
767 const struct GNUNET_HELLO_Address *address)
771 for (pos = plugin->head; NULL != pos; pos = pos->next)
772 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
773 (address->address_length == pos->addrlen) &&
774 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
780 client_put_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
782 struct Session *s = cls;
783 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
784 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
785 "Session %p/connection %p: will be disconnected due to no activity\n",
787 s->put_paused = GNUNET_NO;
788 s->put_tmp_disconnecting = GNUNET_YES;
789 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
790 client_schedule (s->plugin, GNUNET_YES);
796 * Callback method used with libcurl
797 * Method is called when libcurl needs to read data during sending
799 * @param stream pointer where to write data
800 * @param size size of an individual element
801 * @param nmemb count of elements that can be written to the buffer
802 * @param cls source pointer, passed to the libcurl handle
803 * @return bytes written to stream, returning 0 will terminate connection!
806 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
808 struct Session *s = cls;
809 struct HTTP_Client_Plugin *plugin = s->plugin;
810 struct HTTP_Message *msg = s->msg_head;
814 if (GNUNET_YES != client_exist_session (plugin, s))
819 if (GNUNET_YES == s->put_tmp_disconnecting)
822 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
823 "Session %p/connection %p: disconnect due to inactivity\n",
830 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
831 "Session %p/connection %p: nothing to send, suspending\n",
833 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
834 s->put_paused = GNUNET_YES;
835 return CURL_READFUNC_PAUSE;
838 GNUNET_assert (msg->pos < msg->size);
839 /* calculate how much fits in buffer */
840 len = GNUNET_MIN (msg->size - msg->pos,
842 memcpy (stream, &msg->buf[msg->pos], len);
844 if (msg->pos == msg->size)
846 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
847 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
848 s, s->client_put, msg->size, msg->pos);
849 /* Calling transmit continuation */
850 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
851 if (NULL != msg->transmit_cont)
852 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
853 msg->size, msg->size + s->overhead);
858 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
859 GNUNET_STATISTICS_update (plugin->env->stats,
860 stat_txt, -len, GNUNET_NO);
861 GNUNET_free (stat_txt);
863 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
864 GNUNET_STATISTICS_update (plugin->env->stats,
865 stat_txt, len, GNUNET_NO);
866 GNUNET_free (stat_txt);
868 client_reschedule_session_timeout (s);
874 * Wake up a curl handle which was suspended
876 * @param cls the session
877 * @param tc task context
880 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
882 struct Session *s = cls;
884 if (GNUNET_YES != client_exist_session(p, s))
889 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
890 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
892 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
893 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
894 if (s->client_get != NULL)
895 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;
917 if (GNUNET_YES != client_exist_session(p, 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, "# bytes received via %s_client", plugin->protocol);
938 GNUNET_STATISTICS_update (plugin->env->stats,
939 stat_txt, ntohs(message->size), GNUNET_NO);
940 GNUNET_free (stat_txt);
943 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
945 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
947 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
948 "Client: peer `%s' address `%s' next read delayed for %s\n",
949 GNUNET_i2s (&s->target),
950 http_common_plugin_address_to_string (NULL,
952 s->addr, s->addrlen),
953 GNUNET_STRINGS_relative_time_to_string (delay,
956 client_reschedule_session_timeout (s);
962 * Callback method used with libcurl when data for a PUT connection are
963 * received. We do not expect data here, so we just dismiss it
965 * @param stream pointer where to write data
966 * @param size size of an individual element
967 * @param nmemb count of elements that can be written to the buffer
968 * @param cls destination pointer, passed to the libcurl handle
969 * @return bytes read from stream
972 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
979 * Callback method used with libcurl when data for a GET connection are
980 * received. Forward to MST
982 * @param stream pointer where to write data
983 * @param size size of an individual element
984 * @param nmemb count of elements that can be written to the buffer
985 * @param cls destination pointer, passed to the libcurl handle
986 * @return bytes read from stream
989 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
991 struct Session *s = cls;
992 struct GNUNET_TIME_Absolute now;
993 size_t len = size * nmemb;
995 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
996 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
998 len, GNUNET_i2s (&s->target));
999 now = GNUNET_TIME_absolute_get ();
1000 if (now.abs_value_us < s->next_receive.abs_value_us)
1002 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1003 struct GNUNET_TIME_Relative delta =
1004 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1005 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1006 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1008 GNUNET_STRINGS_relative_time_to_string (delta,
1010 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1012 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1013 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1015 s->recv_wakeup_task =
1016 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1017 return CURL_WRITEFUNC_PAUSE;
1019 if (NULL == s->msg_tk)
1020 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1021 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1027 * Task performing curl operations
1029 * @param cls plugin as closure
1030 * @param tc gnunet scheduler task context
1033 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1037 * Function setting up file descriptors and scheduling task to run
1039 * @param plugin the plugin as closure
1040 * @param now schedule task in 1ms, regardless of what curl may say
1041 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1044 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1050 struct GNUNET_NETWORK_FDSet *grs;
1051 struct GNUNET_NETWORK_FDSet *gws;
1054 struct GNUNET_TIME_Relative timeout;
1056 /* Cancel previous scheduled task */
1057 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1059 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1060 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1066 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1067 if (mret != CURLM_OK)
1069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1070 "curl_multi_fdset", __FILE__, __LINE__,
1071 curl_multi_strerror (mret));
1072 return GNUNET_SYSERR;
1074 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1076 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1078 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1079 if (now == GNUNET_YES)
1080 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1082 if (mret != CURLM_OK)
1084 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1085 "curl_multi_timeout", __FILE__, __LINE__,
1086 curl_multi_strerror (mret));
1087 return GNUNET_SYSERR;
1090 grs = GNUNET_NETWORK_fdset_create ();
1091 gws = GNUNET_NETWORK_fdset_create ();
1092 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1093 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1095 plugin->client_perform_task =
1096 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1098 &client_run, plugin);
1099 GNUNET_NETWORK_fdset_destroy (gws);
1100 GNUNET_NETWORK_fdset_destroy (grs);
1106 * Task performing curl operations
1108 * @param cls plugin as closure
1109 * @param tc gnunet scheduler task context
1112 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1114 struct HTTP_Client_Plugin *plugin = cls;
1116 long http_statuscode;
1119 GNUNET_assert (cls != NULL);
1121 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1122 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1128 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1133 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1135 CURL *easy_h = msg->easy_handle;
1136 struct Session *s = NULL;
1137 char *d = (char *) s;
1142 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1143 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1145 curl_easy_strerror (msg->data.result), running);
1149 GNUNET_assert (CURLE_OK ==
1150 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1151 s = (struct Session *) d;
1153 if (GNUNET_YES != client_exist_session(plugin, s))
1159 GNUNET_assert (s != NULL);
1160 if (msg->msg == CURLMSG_DONE)
1162 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1163 if (easy_h == s->client_put)
1165 if ((0 != msg->data.result) || (http_statuscode != 200))
1167 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1168 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1169 s, msg->easy_handle, GNUNET_i2s (&s->target),
1172 curl_easy_strerror (msg->data.result));
1175 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1176 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1177 s, msg->easy_handle, GNUNET_i2s (&s->target));
1178 if (s->client_get == NULL)
1180 /* Disconnect other transmission direction and tell transport */
1182 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1183 curl_easy_cleanup (easy_h);
1184 s->put_tmp_disconnecting = GNUNET_NO;
1185 s->put_tmp_disconnected = GNUNET_YES;
1186 s->client_put = NULL;
1187 s->put.easyhandle = NULL;
1191 * Handling a rare case:
1192 * plugin_send was called during temporary put disconnect,
1193 * reconnect required after connection was disconnected
1195 if (GNUNET_YES == s->put_reconnect_required)
1197 s->put_reconnect_required = GNUNET_NO;
1198 if (GNUNET_SYSERR == client_connect_put(s))
1200 GNUNET_break (s->client_put == NULL);
1201 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1205 if (easy_h == s->client_get)
1207 if ((0 != msg->data.result) || (http_statuscode != 200))
1209 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1210 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1211 s, msg->easy_handle, GNUNET_i2s (&s->target),
1214 curl_easy_strerror (msg->data.result));
1218 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1219 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1220 s, msg->easy_handle, GNUNET_i2s (&s->target));
1221 /* Disconnect other transmission direction and tell transport */
1222 s->get.easyhandle = NULL;
1224 client_disconnect (s);
1229 while (mret == CURLM_CALL_MULTI_PERFORM);
1230 client_schedule (plugin, GNUNET_NO);
1235 * Connect GET connection for a session
1237 * @param s the session to connect
1238 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1241 client_connect_get (struct Session *s)
1245 /* create get connection */
1246 s->client_get = curl_easy_init ();
1248 s->get.easyhandle = s->client_get;
1250 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1251 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1252 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1255 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1256 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1257 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1259 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1260 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1264 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1265 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1267 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1268 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1270 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1271 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1274 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1275 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1276 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1277 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1278 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1279 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1280 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1281 /* No timeout by default, timeout done with session timeout */
1282 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1283 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1284 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1285 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1286 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1287 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1288 #if CURL_TCP_NODELAY
1289 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1291 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1293 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1294 if (mret != CURLM_OK)
1296 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1297 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1298 s, curl_multi_strerror (mret));
1299 curl_easy_cleanup (s->client_get);
1300 s->client_get = NULL;
1302 s->get.easyhandle = NULL;
1304 return GNUNET_SYSERR;
1311 * Connect a HTTP put connection
1313 * @param s the session to connect
1314 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1317 client_connect_put (struct Session *s)
1320 /* create put connection */
1321 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1322 "Session %p : Init PUT handle \n", s);
1323 s->client_put = curl_easy_init ();
1325 s->put.easyhandle = s->client_put;
1327 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1328 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1329 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1332 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1333 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1334 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1336 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1337 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1341 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1342 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1344 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1345 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1347 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1348 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1350 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1351 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1352 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1353 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1354 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1355 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1356 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1357 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1358 /* No timeout by default, timeout done with session timeout */
1359 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1360 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1361 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1362 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1363 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1364 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1365 #if CURL_TCP_NODELAY
1366 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1368 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1369 if (mret != CURLM_OK)
1371 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1372 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1373 s, curl_multi_strerror (mret));
1374 curl_easy_cleanup (s->client_put);
1375 s->client_put = NULL;
1376 s->put.easyhandle = NULL;
1378 s->put_tmp_disconnected = GNUNET_YES;
1379 return GNUNET_SYSERR;
1381 s->put_tmp_disconnected = GNUNET_NO;
1387 * Connect both PUT and GET connection for a session
1389 * @param s the session to connect
1390 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1393 client_connect (struct Session *s)
1396 struct HTTP_Client_Plugin *plugin = s->plugin;
1397 int res = GNUNET_OK;
1400 if (NULL == http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen))
1402 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1403 "Invalid address peer `%s'\n",
1404 GNUNET_i2s (&s->target));
1405 return GNUNET_SYSERR;
1408 GNUNET_asprintf (&s->url, "%s/%s;%u",
1409 http_common_plugin_address_to_url (NULL, s->addr, s->addrlen),
1410 GNUNET_i2s_full (plugin->env->my_identity),
1415 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1416 "Initiating outbound session peer `%s' using address `%s'\n",
1417 GNUNET_i2s (&s->target), s->url);
1419 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1420 (GNUNET_SYSERR == client_connect_put (s)))
1423 return GNUNET_SYSERR;
1426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1427 "Session %p: connected with connections GET %p and PUT %p\n",
1428 s, s->client_get, s->client_put);
1430 /* Perform connect */
1431 plugin->cur_connections += 2;
1432 GNUNET_STATISTICS_set (plugin->env->stats,
1433 HTTP_STAT_STR_CONNECTIONS,
1434 plugin->cur_connections,
1437 /* Re-schedule since handles have changed */
1438 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1440 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1441 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1443 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1449 * Function obtain the network type for a session
1451 * @param cls closure ('struct Plugin*')
1452 * @param session the session
1453 * @return the network type in HBO or GNUNET_SYSERR
1455 static enum GNUNET_ATS_Network_Type
1456 http_client_get_network (void *cls,
1457 struct Session *session)
1459 GNUNET_assert (NULL != session);
1460 return ntohl (session->ats_address_network_type);
1465 * Creates a new outbound session the transport service will use to send data to the
1468 * @param cls the plugin
1469 * @param address the address
1470 * @return the session or NULL of max connections exceeded
1472 static struct Session *
1473 http_client_plugin_get_session (void *cls,
1474 const struct GNUNET_HELLO_Address *address)
1476 struct HTTP_Client_Plugin *plugin = cls;
1477 struct Session * s = NULL;
1478 struct sockaddr *sa;
1479 struct GNUNET_ATS_Information ats;
1483 GNUNET_assert (plugin != NULL);
1484 GNUNET_assert (address != NULL);
1485 GNUNET_assert (address->address != NULL);
1487 /* find existing session */
1488 s = client_lookup_session (plugin, address);
1492 if (plugin->max_connections <= plugin->cur_connections)
1494 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1495 "Maximum number of connections (%u) reached: "
1496 "cannot connect to peer `%s'\n",
1497 plugin->max_connections,
1498 GNUNET_i2s (&address->peer));
1502 /* Determine network location */
1503 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1504 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1505 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1506 if (GNUNET_SYSERR == res)
1510 else if (GNUNET_YES == res)
1512 GNUNET_assert (NULL != sa);
1513 if (AF_INET == sa->sa_family)
1515 salen = sizeof (struct sockaddr_in);
1517 else if (AF_INET6 == sa->sa_family)
1519 salen = sizeof (struct sockaddr_in6);
1521 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1522 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1526 else if (GNUNET_NO == res)
1528 /* Cannot convert to sockaddr -> is external hostname */
1529 ats.value = htonl (GNUNET_ATS_NET_WAN);
1532 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1538 s = GNUNET_malloc (sizeof (struct Session));
1539 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1541 s->addr = GNUNET_malloc (address->address_length);
1542 memcpy (s->addr, address->address, address->address_length);
1543 s->addrlen = address->address_length;
1544 s->ats_address_network_type = ats.value;
1545 s->put_paused = GNUNET_NO;
1546 s->put_tmp_disconnecting = GNUNET_NO;
1547 s->put_tmp_disconnected = GNUNET_NO;
1548 client_start_session_timeout (s);
1550 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1551 "Created new session %p for `%s' address `%s''\n",
1553 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1554 GNUNET_i2s (&s->target));
1556 /* add new session */
1557 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1559 /* initiate new connection */
1560 if (GNUNET_SYSERR == client_connect (s))
1562 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1563 "Cannot connect to peer `%s' address `%s''\n",
1564 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1565 GNUNET_i2s (&s->target));
1566 client_delete_session (s);
1574 * Setup http_client plugin
1576 * @param plugin the plugin handle
1577 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1580 client_start (struct HTTP_Client_Plugin *plugin)
1582 curl_global_init (CURL_GLOBAL_ALL);
1583 plugin->curl_multi_handle = curl_multi_init ();
1585 if (NULL == plugin->curl_multi_handle)
1587 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1588 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1590 return GNUNET_SYSERR;
1596 * Session was idle, so disconnect it
1599 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1601 GNUNET_assert (NULL != cls);
1602 struct Session *s = cls;
1604 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1605 GNUNET_log (TIMEOUT_LOG,
1606 "Session %p was idle for %s, disconnecting\n",
1608 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1611 /* call session destroy function */
1612 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1617 * Start session timeout for session s
1619 * @param s the session
1622 client_start_session_timeout (struct Session *s)
1625 GNUNET_assert (NULL != s);
1626 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1627 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1628 &client_session_timeout,
1630 GNUNET_log (TIMEOUT_LOG,
1631 "Timeout for session %p set to %s\n",
1633 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1639 * Increment session timeout due to activity for session s
1641 * param s the session
1644 client_reschedule_session_timeout (struct Session *s)
1647 GNUNET_assert (NULL != s);
1648 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1650 GNUNET_SCHEDULER_cancel (s->timeout_task);
1651 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1652 &client_session_timeout,
1654 GNUNET_log (TIMEOUT_LOG,
1655 "Timeout rescheduled for session %p set to %s\n",
1657 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1663 * Cancel timeout due to activity for session s
1665 * param s the session
1668 client_stop_session_timeout (struct Session *s)
1670 GNUNET_assert (NULL != s);
1672 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1674 GNUNET_SCHEDULER_cancel (s->timeout_task);
1675 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1676 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1682 * Another peer has suggested an address for this
1683 * peer and transport plugin. Check that this could be a valid
1684 * address. If so, consider adding it to the list
1687 * @param cls closure
1688 * @param addr pointer to the address
1689 * @param addrlen length of addr
1690 * @return GNUNET_OK if this is a plausible address for this peer
1694 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1696 /* struct Plugin *plugin = cls; */
1698 /* A HTTP/S client does not have any valid address so:*/
1704 * Exit point from the plugin.
1706 * @param cls api as closure
1710 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1712 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1713 struct HTTP_Client_Plugin *plugin = api->cls;
1714 struct Session *pos;
1715 struct Session *next;
1717 if (NULL == api->cls)
1724 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1725 _("Shutting down plugin `%s'\n"),
1729 next = plugin->head;
1730 while (NULL != (pos = next))
1733 client_disconnect (pos);
1735 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1737 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1738 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1742 if (NULL != plugin->curl_multi_handle)
1744 curl_multi_cleanup (plugin->curl_multi_handle);
1745 plugin->curl_multi_handle = NULL;
1747 curl_global_cleanup ();
1749 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1750 _("Shutdown for plugin `%s' complete\n"),
1753 GNUNET_free (plugin);
1762 * @param plugin the plugin handle
1763 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1766 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1768 unsigned long long max_connections;
1770 /* Optional parameters */
1771 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1773 "MAX_CONNECTIONS", &max_connections))
1774 max_connections = 128;
1775 plugin->max_connections = max_connections;
1777 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1778 _("Maximum number of connections is %u\n"),
1779 plugin->max_connections);
1783 const char *http_plugin_address_to_string (void *cls,
1787 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1791 * Entry point for the plugin.
1794 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1796 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1797 struct GNUNET_TRANSPORT_PluginFunctions *api;
1798 struct HTTP_Client_Plugin *plugin;
1800 if (NULL == env->receive)
1802 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1803 initialze the plugin or the API */
1804 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1806 api->address_to_string = &http_plugin_address_to_string;
1807 api->string_to_address = &http_common_plugin_string_to_address;
1808 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1812 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1815 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1817 api->send = &http_client_plugin_send;
1818 api->disconnect = &http_client_plugin_disconnect;
1819 api->check_address = &http_client_plugin_address_suggested;
1820 api->get_session = &http_client_plugin_get_session;
1821 api->address_to_string = &http_plugin_address_to_string;
1822 api->string_to_address = &http_common_plugin_string_to_address;
1823 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1824 api->get_network = &http_client_get_network;
1827 plugin->name = "transport-https_client";
1828 plugin->protocol = "https";
1830 plugin->name = "transport-http_client";
1831 plugin->protocol = "http";
1833 plugin->last_tag = 1;
1834 plugin->options = 0; /* Setup options */
1836 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1838 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1843 if (GNUNET_SYSERR == client_start (plugin))
1845 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1851 /* end of plugin_transport_http_client.c */