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 LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
30 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
32 #define PLUGIN_NAME "http_client"
33 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
34 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
37 #define VERBOSE_CURL GNUNET_YES
39 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
41 #define ENABLE_PUT GNUNET_YES
42 #define ENABLE_GET GNUNET_YES
45 #include "gnunet_protocols.h"
46 #include "gnunet_common.h"
47 #include "gnunet_server_lib.h"
48 #include "gnunet_transport_plugin.h"
49 #include "plugin_transport_http_common.h"
50 #include <curl/curl.h>
55 * Encapsulation of all of the state of the plugin.
57 struct HTTP_Client_Plugin;
61 * Message to send using http
66 * next pointer for double linked list
68 struct HTTP_Message *next;
71 * previous pointer for double linked list
73 struct HTTP_Message *prev;
76 * buffer containing data to send
81 * amount of data already sent
91 * Continuation function to call once the transmission buffer
92 * has again space available. NULL if there is no
93 * continuation to call.
95 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
98 * Closure for transmit_cont.
100 void *transmit_cont_cls;
105 * Session handle for connections.
110 * A connection handle
113 struct ConnectionHandle
116 * The curl easy handle
121 * The related session
129 * Session handle for connections.
134 * To whom are we talking to (set to our identity
135 * if we are still waiting for the welcome message)
137 struct GNUNET_PeerIdentity target;
140 * Stored in a linked list.
142 struct Session *next;
145 * Stored in a linked list.
147 struct Session *prev;
150 * The URL to connect to
157 struct HttpAddress *addr;
165 * ATS network type in NBO
167 uint32_t ats_address_network_type;
170 * Pointer to the global plugin struct.
172 struct HTTP_Client_Plugin *plugin;
179 struct ConnectionHandle put;
180 struct ConnectionHandle get;
183 * Is the client PUT handle currently paused
188 * Is the client PUT handle disconnect in progress?
190 int put_tmp_disconnecting;
193 * Is the client PUT handle temporarily disconnected?
195 int put_tmp_disconnected;
198 * We received data to send while disconnecting, reconnect immediately
200 int put_reconnect_required;
203 * Client receive handle
208 * Outbound overhead due to HTTP connection
209 * Add to next message of this session when calling callback
214 * next pointer for double linked list
216 struct HTTP_Message *msg_head;
219 * previous pointer for double linked list
221 struct HTTP_Message *msg_tail;
224 * Message stream tokenizer for incoming data
226 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
229 * Session timeout task
231 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
234 * Session timeout task
236 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
239 * Task to wake up client receive handle when receiving is allowed again
241 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
244 * Absolute time when to receive data again
245 * Used for receive throttling
247 struct GNUNET_TIME_Absolute next_receive;
252 * Encapsulation of all of the state of the plugin.
254 struct HTTP_Client_Plugin
259 struct GNUNET_TRANSPORT_PluginEnvironment *env;
262 * Linked list head of open sessions.
264 struct Session *head;
267 * Linked list tail of open sessions.
269 struct Session *tail;
282 * My options to be included in the address
287 * Maximum number of sockets the plugin can use
288 * Each http inbound /outbound connections are two connections
290 unsigned int max_connections;
293 * Current number of sockets the plugin can use
294 * Each http inbound /outbound connections are two connections
296 unsigned int cur_connections;
299 * Last used unique HTTP connection tag
316 CURLM *curl_multi_handle;
321 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
326 * Encapsulation of all of the state of the plugin.
328 struct HTTP_Client_Plugin *p;
332 * Start session timeout for a session
333 * @param s the session
336 client_start_session_timeout (struct Session *s);
340 * Increment session timeout due to activity for a session
341 * @param s the session
344 client_reschedule_session_timeout (struct Session *s);
348 * Cancel timeout for a session
349 * @param s the session
352 client_stop_session_timeout (struct Session *s);
356 * Function setting up file descriptors and scheduling task to run
358 * @param plugin plugin as closure
359 * @param now schedule task in 1ms, regardless of what curl may say
360 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
363 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
367 * Connect a HTTP put connection
369 * @param s the session to connect
370 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for success
373 client_connect_put (struct Session *s);
377 * Does a session s exists?
379 * @param plugin the plugin
380 * @param s desired session
381 * @return GNUNET_YES or GNUNET_NO
384 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
386 struct Session * head;
388 GNUNET_assert (NULL != plugin);
389 GNUNET_assert (NULL != s);
391 for (head = plugin->head; head != NULL; head = head->next)
403 * @param curl the curl easy handle
404 * @param type message type
405 * @param data data to log, NOT a 0-terminated string
406 * @param size data length
407 * @param cls the closure
411 client_log (CURL *curl, curl_infotype type,
412 const char *data, size_t size, void *cls)
414 struct ConnectionHandle *ch = cls;
415 const char *ttype = "UNSPECIFIED";
417 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
425 case CURLINFO_HEADER_IN:
428 case CURLINFO_HEADER_OUT:
429 ttype = "HEADER_OUT";
432 GNUNET_assert (NULL != ch);
433 GNUNET_assert (NULL != ch->easyhandle);
434 GNUNET_assert (NULL != ch->s);
435 ch->s->overhead += size;
438 ttype = "UNSPECIFIED";
442 memcpy (text, data, size);
443 if (text[size - 1] == '\n')
448 text[size + 1] = '\0';
451 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
452 "Connection %p %s: %s", ch->easyhandle, ttype, text);
454 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
455 "Connection %p %s: %s", ch->easyhandle, ttype, text);
463 * Function that can be used by the transport service to transmit
464 * a message using the plugin. Note that in the case of a
465 * peer disconnecting, the continuation MUST be called
466 * prior to the disconnect notification itself. This function
467 * will be called with this peer's HELLO message to initiate
468 * a fresh connection to another peer.
471 * @param s which session must be used
472 * @param msgbuf the message to transmit
473 * @param msgbuf_size number of bytes in 'msgbuf'
474 * @param priority how important is the message (most plugins will
475 * ignore message priority and just FIFO)
476 * @param to how long to wait at most for the transmission (does not
477 * require plugins to discard the message after the timeout,
478 * just advisory for the desired delay; most plugins will ignore
480 * @param cont continuation to call once the message has
481 * been transmitted (or if the transport is ready
482 * for the next transmission call; or if the
483 * peer disconnected...); can be NULL
484 * @param cont_cls closure for cont
485 * @return number of bytes used (on the physical network, with overheads);
486 * -1 on hard errors (i.e. address invalid); 0 is a legal value
487 * and does NOT mean that the message was not transmitted (DV)
490 http_client_plugin_send (void *cls,
492 const char *msgbuf, size_t msgbuf_size,
493 unsigned int priority,
494 struct GNUNET_TIME_Relative to,
495 GNUNET_TRANSPORT_TransmitContinuation cont,
498 struct HTTP_Client_Plugin *plugin = cls;
499 struct HTTP_Message *msg;
502 GNUNET_assert (plugin != NULL);
503 GNUNET_assert (s != NULL);
505 /* lookup if session is really existing */
506 if (GNUNET_YES != client_exist_session (plugin, s))
509 return GNUNET_SYSERR;
512 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
513 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
515 msgbuf_size, GNUNET_i2s (&s->target));
517 /* create new message and schedule */
518 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
520 msg->size = msgbuf_size;
522 msg->buf = (char *) &msg[1];
523 msg->transmit_cont = cont;
524 msg->transmit_cont_cls = cont_cls;
525 memcpy (msg->buf, msgbuf, msgbuf_size);
526 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
528 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
529 GNUNET_STATISTICS_update (plugin->env->stats,
530 stat_txt, msgbuf_size, GNUNET_NO);
532 GNUNET_free (stat_txt);
534 if (GNUNET_YES == s->put_tmp_disconnecting)
536 /* PUT connection is currently getting disconnected */
537 s->put_reconnect_required = GNUNET_YES;
538 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
539 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
543 else if (GNUNET_YES == s->put_paused)
545 /* PUT connection was paused, unpause */
546 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
547 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
548 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
549 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
550 "Session %p/connection %p: unpausing connection\n",
552 s->put_paused = GNUNET_NO;
553 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
555 else if (GNUNET_YES == s->put_tmp_disconnected)
557 /* PUT connection was disconnected, reconnect */
558 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
559 "Session %p: Reconnecting PUT connection\n",
561 s->put_tmp_disconnected = GNUNET_NO;
562 GNUNET_break (s->client_put == NULL);
563 if (GNUNET_SYSERR == client_connect_put (s))
565 return GNUNET_SYSERR;
569 client_schedule (s->plugin, GNUNET_YES);
570 client_reschedule_session_timeout (s);
578 * @param s the session to delete
581 client_delete_session (struct Session *s)
583 struct HTTP_Client_Plugin *plugin = s->plugin;
584 struct HTTP_Message *pos;
585 struct HTTP_Message *next;
587 client_stop_session_timeout (s);
589 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
591 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
592 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
595 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
598 while (NULL != (pos = next))
601 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
602 if (pos->transmit_cont != NULL)
603 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
604 pos->size, pos->pos + s->overhead);
609 if (s->msg_tk != NULL)
611 GNUNET_SERVER_mst_destroy (s->msg_tk);
614 GNUNET_free (s->addr);
615 GNUNET_free (s->url);
622 * Disconnect a session
625 * @return GNUNET_OK on success, GNUNET_SYSERR on error
628 client_disconnect (struct Session *s)
630 struct HTTP_Client_Plugin *plugin = s->plugin;
631 struct HTTP_Message *msg;
632 struct HTTP_Message *t;
636 if (GNUNET_YES != client_exist_session (plugin, s))
639 return GNUNET_SYSERR;
642 if (s->client_put != NULL)
644 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
645 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
646 s, s->client_put, GNUNET_i2s (&s->target));
648 /* remove curl handle from multi handle */
649 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
650 if (mret != CURLM_OK)
652 /* clean up easy handle, handle is now invalid and free'd */
656 curl_easy_cleanup (s->client_put);
657 s->client_put = NULL;
661 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
663 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
664 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
667 if (s->client_get != NULL)
669 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
670 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
671 s, s->client_get, GNUNET_i2s (&s->target));
673 /* remove curl handle from multi handle */
674 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
675 if (mret != CURLM_OK)
677 /* clean up easy handle, handle is now invalid and free'd */
681 curl_easy_cleanup (s->client_get);
682 s->client_get = NULL;
689 if (NULL != msg->transmit_cont)
690 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
691 msg->size, msg->pos + s->overhead);
693 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
698 GNUNET_assert (plugin->cur_connections >= 2);
699 plugin->cur_connections -= 2;
700 GNUNET_STATISTICS_set (plugin->env->stats,
701 "# HTTP client sessions",
702 plugin->cur_connections,
705 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
706 "Session %p: notifying transport about ending session\n",s);
708 plugin->env->session_end (plugin->env->cls, &s->target, s);
709 client_delete_session (s);
711 /* Re-schedule since handles have changed */
712 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
714 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
715 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
717 client_schedule (plugin, GNUNET_YES);
724 * Function that can be used to force the plugin to disconnect
725 * from the given peer and cancel all previous transmissions
726 * (and their continuationc).
729 * @param target peer from which to disconnect
732 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
734 struct HTTP_Client_Plugin *plugin = cls;
735 struct Session *next = NULL;
736 struct Session *pos = NULL;
738 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
739 "Transport tells me to disconnect `%s'\n",
740 GNUNET_i2s (target));
743 while (NULL != (pos = next))
746 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
748 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
749 "Disconnecting session %p to `%pos'\n",
750 pos, GNUNET_i2s (target));
751 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
758 * Check if a sessions exists for an specific address
760 * @param plugin the plugin
761 * @param address the address
762 * @return the session or NULL
764 static struct Session *
765 client_lookup_session (struct HTTP_Client_Plugin *plugin,
766 const struct GNUNET_HELLO_Address *address)
770 for (pos = plugin->head; NULL != pos; pos = pos->next)
771 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
772 (address->address_length == pos->addrlen) &&
773 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
779 client_put_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
781 struct Session *s = cls;
782 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
783 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
784 "Session %p/connection %p: will be disconnected due to no activity\n",
786 s->put_paused = GNUNET_NO;
787 s->put_tmp_disconnecting = GNUNET_YES;
788 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
789 client_schedule (s->plugin, GNUNET_YES);
795 * Callback method used with libcurl
796 * Method is called when libcurl needs to read data during sending
798 * @param stream pointer where to write data
799 * @param size size of an individual element
800 * @param nmemb count of elements that can be written to the buffer
801 * @param cls source pointer, passed to the libcurl handle
802 * @return bytes written to stream, returning 0 will terminate connection!
805 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
807 struct Session *s = cls;
808 struct HTTP_Client_Plugin *plugin = s->plugin;
809 struct HTTP_Message *msg = s->msg_head;
813 if (GNUNET_YES != client_exist_session (plugin, s))
818 if (GNUNET_YES == s->put_tmp_disconnecting)
821 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
822 "Session %p/connection %p: disconnect due to inactivity\n",
829 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
830 "Session %p/connection %p: nothing to send, suspending\n",
832 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
833 s->put_paused = GNUNET_YES;
834 return CURL_READFUNC_PAUSE;
837 GNUNET_assert (msg->pos < msg->size);
838 /* calculate how much fits in buffer */
839 len = GNUNET_MIN (msg->size - msg->pos,
841 memcpy (stream, &msg->buf[msg->pos], len);
843 if (msg->pos == msg->size)
845 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
846 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
847 s, s->client_put, msg->size, msg->pos);
848 /* Calling transmit continuation */
849 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
850 if (NULL != msg->transmit_cont)
851 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
852 msg->size, msg->size + s->overhead);
857 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
858 GNUNET_STATISTICS_update (plugin->env->stats,
859 stat_txt, -len, GNUNET_NO);
860 GNUNET_free (stat_txt);
862 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
863 GNUNET_STATISTICS_update (plugin->env->stats,
864 stat_txt, len, GNUNET_NO);
865 GNUNET_free (stat_txt);
867 client_reschedule_session_timeout (s);
873 * Wake up a curl handle which was suspended
875 * @param cls the session
876 * @param tc task context
879 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
881 struct Session *s = cls;
883 if (GNUNET_YES != client_exist_session(p, s))
888 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
889 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
891 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
892 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
893 if (s->client_get != NULL)
894 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
899 * Callback for message stream tokenizer
901 * @param cls the session
902 * @param client not used
903 * @param message the message received
904 * @return always GNUNET_OK
907 client_receive_mst_cb (void *cls, void *client,
908 const struct GNUNET_MessageHeader *message)
910 struct Session *s = cls;
911 struct HTTP_Client_Plugin *plugin;
912 struct GNUNET_TIME_Relative delay;
913 struct GNUNET_ATS_Information atsi;
916 if (GNUNET_YES != client_exist_session(p, s))
923 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
924 atsi.value = s->ats_address_network_type;
925 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
926 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
927 s, (const char *) s->addr, s->addrlen);
929 plugin->env->update_address_metrics (plugin->env->cls,
936 GNUNET_asprintf (&stat_txt, "# bytes received via %s_client", plugin->protocol);
937 GNUNET_STATISTICS_update (plugin->env->stats,
938 stat_txt, ntohs(message->size), GNUNET_NO);
939 GNUNET_free (stat_txt);
942 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
944 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
946 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
947 "Client: peer `%s' address `%s' next read delayed for %s\n",
948 GNUNET_i2s (&s->target),
949 http_common_plugin_address_to_string (NULL,
951 s->addr, s->addrlen),
952 GNUNET_STRINGS_relative_time_to_string (delay,
955 client_reschedule_session_timeout (s);
961 * Callback method used with libcurl when data for a PUT connection are
962 * received. We do not expect data here, so we just dismiss it
964 * @param stream pointer where to write data
965 * @param size size of an individual element
966 * @param nmemb count of elements that can be written to the buffer
967 * @param cls destination pointer, passed to the libcurl handle
968 * @return bytes read from stream
971 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
978 * Callback method used with libcurl when data for a GET connection are
979 * received. Forward to MST
981 * @param stream pointer where to write data
982 * @param size size of an individual element
983 * @param nmemb count of elements that can be written to the buffer
984 * @param cls destination pointer, passed to the libcurl handle
985 * @return bytes read from stream
988 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
990 struct Session *s = cls;
991 struct GNUNET_TIME_Absolute now;
992 size_t len = size * nmemb;
994 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
995 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
997 len, GNUNET_i2s (&s->target));
998 now = GNUNET_TIME_absolute_get ();
999 if (now.abs_value_us < s->next_receive.abs_value_us)
1001 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1002 struct GNUNET_TIME_Relative delta =
1003 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1004 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1005 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1007 GNUNET_STRINGS_relative_time_to_string (delta,
1009 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1011 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1012 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1014 s->recv_wakeup_task =
1015 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1016 return CURL_WRITEFUNC_PAUSE;
1018 if (NULL == s->msg_tk)
1019 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1020 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1026 * Task performing curl operations
1028 * @param cls plugin as closure
1029 * @param tc gnunet scheduler task context
1032 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1036 * Function setting up file descriptors and scheduling task to run
1038 * @param plugin the plugin as closure
1039 * @param now schedule task in 1ms, regardless of what curl may say
1040 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1043 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1049 struct GNUNET_NETWORK_FDSet *grs;
1050 struct GNUNET_NETWORK_FDSet *gws;
1053 struct GNUNET_TIME_Relative timeout;
1055 /* Cancel previous scheduled task */
1056 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1058 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1059 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1065 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1066 if (mret != CURLM_OK)
1068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1069 "curl_multi_fdset", __FILE__, __LINE__,
1070 curl_multi_strerror (mret));
1071 return GNUNET_SYSERR;
1073 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1075 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1077 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1078 if (now == GNUNET_YES)
1079 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1081 if (mret != CURLM_OK)
1083 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1084 "curl_multi_timeout", __FILE__, __LINE__,
1085 curl_multi_strerror (mret));
1086 return GNUNET_SYSERR;
1089 grs = GNUNET_NETWORK_fdset_create ();
1090 gws = GNUNET_NETWORK_fdset_create ();
1091 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1092 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1094 plugin->client_perform_task =
1095 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1097 &client_run, plugin);
1098 GNUNET_NETWORK_fdset_destroy (gws);
1099 GNUNET_NETWORK_fdset_destroy (grs);
1105 * Task performing curl operations
1107 * @param cls plugin as closure
1108 * @param tc gnunet scheduler task context
1111 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1113 struct HTTP_Client_Plugin *plugin = cls;
1115 long http_statuscode;
1118 GNUNET_assert (cls != NULL);
1120 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1121 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1127 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1132 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1134 CURL *easy_h = msg->easy_handle;
1135 struct Session *s = NULL;
1136 char *d = (char *) s;
1141 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1142 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1144 curl_easy_strerror (msg->data.result), running);
1148 GNUNET_assert (CURLE_OK ==
1149 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1150 s = (struct Session *) d;
1152 if (GNUNET_YES != client_exist_session(plugin, s))
1158 GNUNET_assert (s != NULL);
1159 if (msg->msg == CURLMSG_DONE)
1161 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1162 if (easy_h == s->client_put)
1164 if ((0 != msg->data.result) || (http_statuscode != 200))
1166 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1167 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1168 s, msg->easy_handle, GNUNET_i2s (&s->target),
1171 curl_easy_strerror (msg->data.result));
1174 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1175 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1176 s, msg->easy_handle, GNUNET_i2s (&s->target));
1177 if (s->client_get == NULL)
1179 /* Disconnect other transmission direction and tell transport */
1181 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1182 curl_easy_cleanup (easy_h);
1183 s->put_tmp_disconnecting = GNUNET_NO;
1184 s->put_tmp_disconnected = GNUNET_YES;
1185 s->client_put = NULL;
1186 s->put.easyhandle = NULL;
1190 * Handling a rare case:
1191 * plugin_send was called during temporary put disconnect,
1192 * reconnect required after connection was disconnected
1194 if (GNUNET_YES == s->put_reconnect_required)
1196 s->put_reconnect_required = GNUNET_NO;
1197 if (GNUNET_SYSERR == client_connect_put(s))
1199 GNUNET_break (s->client_put == NULL);
1200 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1204 if (easy_h == s->client_get)
1206 if ((0 != msg->data.result) || (http_statuscode != 200))
1208 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1209 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1210 s, msg->easy_handle, GNUNET_i2s (&s->target),
1213 curl_easy_strerror (msg->data.result));
1217 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1218 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1219 s, msg->easy_handle, GNUNET_i2s (&s->target));
1220 /* Disconnect other transmission direction and tell transport */
1221 s->get.easyhandle = NULL;
1223 client_disconnect (s);
1228 while (mret == CURLM_CALL_MULTI_PERFORM);
1229 client_schedule (plugin, GNUNET_NO);
1234 * Connect GET connection for a session
1236 * @param s the session to connect
1237 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1240 client_connect_get (struct Session *s)
1244 /* create get connection */
1245 s->client_get = curl_easy_init ();
1247 s->get.easyhandle = s->client_get;
1249 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1250 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1251 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1254 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1255 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1256 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1258 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1259 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1263 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1264 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1266 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1267 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1269 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1270 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1273 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1274 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1275 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1276 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1277 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1278 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1279 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1280 /* No timeout by default, timeout done with session timeout */
1281 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1282 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1283 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1284 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1285 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1286 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1287 #if CURL_TCP_NODELAY
1288 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1290 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1292 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1293 if (mret != CURLM_OK)
1295 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1296 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1297 s, curl_multi_strerror (mret));
1298 curl_easy_cleanup (s->client_get);
1299 s->client_get = NULL;
1301 s->get.easyhandle = NULL;
1303 return GNUNET_SYSERR;
1310 * Connect a HTTP put connection
1312 * @param s the session to connect
1313 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1316 client_connect_put (struct Session *s)
1319 /* create put connection */
1320 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1321 "Session %p : Init PUT handle \n", s);
1322 s->client_put = curl_easy_init ();
1324 s->put.easyhandle = s->client_put;
1326 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1327 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1328 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1331 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1332 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1333 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1335 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1336 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1340 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1341 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1343 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1344 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1346 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1347 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1349 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1350 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1351 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1352 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1353 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1354 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1355 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1356 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1357 /* No timeout by default, timeout done with session timeout */
1358 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1359 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1360 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1361 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1362 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1363 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1364 #if CURL_TCP_NODELAY
1365 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1367 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1368 if (mret != CURLM_OK)
1370 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1371 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1372 s, curl_multi_strerror (mret));
1373 curl_easy_cleanup (s->client_put);
1374 s->client_put = NULL;
1375 s->put.easyhandle = NULL;
1377 s->put_tmp_disconnected = GNUNET_YES;
1378 return GNUNET_SYSERR;
1380 s->put_tmp_disconnected = GNUNET_NO;
1386 * Connect both PUT and GET connection for a session
1388 * @param s the session to connect
1389 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1392 client_connect (struct Session *s)
1395 struct HTTP_Client_Plugin *plugin = s->plugin;
1396 int res = GNUNET_OK;
1399 if (NULL == http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen))
1401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1402 "Invalid address peer `%s'\n",
1403 GNUNET_i2s (&s->target));
1404 return GNUNET_SYSERR;
1407 GNUNET_asprintf (&s->url, "%s/%s;%u",
1408 http_common_plugin_address_to_url (NULL, s->addr, s->addrlen),
1409 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
1414 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1415 "Initiating outbound session peer `%s' using address `%s'\n",
1416 GNUNET_i2s (&s->target), s->url);
1418 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1419 (GNUNET_SYSERR == client_connect_put (s)))
1422 return GNUNET_SYSERR;
1425 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1426 "Session %p: connected with connections GET %p and PUT %p\n",
1427 s, s->client_get, s->client_put);
1429 /* Perform connect */
1430 plugin->cur_connections += 2;
1431 GNUNET_STATISTICS_set (plugin->env->stats,
1432 "# HTTP client connections",
1433 plugin->cur_connections,
1436 /* Re-schedule since handles have changed */
1437 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1439 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1440 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1442 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1448 * Function obtain the network type for a session
1450 * @param cls closure ('struct Plugin*')
1451 * @param session the session
1452 * @return the network type in HBO or GNUNET_SYSERR
1454 static enum GNUNET_ATS_Network_Type
1455 http_client_get_network (void *cls,
1456 struct Session *session)
1458 GNUNET_assert (NULL != session);
1459 return ntohl (session->ats_address_network_type);
1464 * Creates a new outbound session the transport service will use to send data to the
1467 * @param cls the plugin
1468 * @param address the address
1469 * @return the session or NULL of max connections exceeded
1471 static struct Session *
1472 http_client_plugin_get_session (void *cls,
1473 const struct GNUNET_HELLO_Address *address)
1475 struct HTTP_Client_Plugin *plugin = cls;
1476 struct Session * s = NULL;
1477 struct sockaddr *sa;
1478 struct GNUNET_ATS_Information ats;
1482 GNUNET_assert (plugin != NULL);
1483 GNUNET_assert (address != NULL);
1484 GNUNET_assert (address->address != NULL);
1486 /* find existing session */
1487 s = client_lookup_session (plugin, address);
1491 if (plugin->max_connections <= plugin->cur_connections)
1493 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1494 "Maximum number of connections (%u) reached: "
1495 "cannot connect to peer `%s'\n",
1496 plugin->max_connections,
1497 GNUNET_i2s (&address->peer));
1501 /* Determine network location */
1502 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1503 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1504 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1505 if (GNUNET_SYSERR == res)
1509 else if (GNUNET_YES == res)
1511 GNUNET_assert (NULL != sa);
1512 if (AF_INET == sa->sa_family)
1514 salen = sizeof (struct sockaddr_in);
1516 else if (AF_INET6 == sa->sa_family)
1518 salen = sizeof (struct sockaddr_in6);
1520 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1521 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1525 else if (GNUNET_NO == res)
1527 /* Cannot convert to sockaddr -> is external hostname */
1528 ats.value = htonl (GNUNET_ATS_NET_WAN);
1531 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1537 s = GNUNET_malloc (sizeof (struct Session));
1538 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1540 s->addr = GNUNET_malloc (address->address_length);
1541 memcpy (s->addr, address->address, address->address_length);
1542 s->addrlen = address->address_length;
1543 s->ats_address_network_type = ats.value;
1544 s->put_paused = GNUNET_NO;
1545 s->put_tmp_disconnecting = GNUNET_NO;
1546 s->put_tmp_disconnected = GNUNET_NO;
1547 client_start_session_timeout (s);
1549 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1550 "Created new session %p for `%s' address `%s''\n",
1552 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1553 GNUNET_i2s (&s->target));
1555 /* add new session */
1556 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1558 /* initiate new connection */
1559 if (GNUNET_SYSERR == client_connect (s))
1561 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1562 "Cannot connect to peer `%s' address `%s''\n",
1563 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1564 GNUNET_i2s (&s->target));
1565 client_delete_session (s);
1573 * Setup http_client plugin
1575 * @param plugin the plugin handle
1576 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1579 client_start (struct HTTP_Client_Plugin *plugin)
1581 curl_global_init (CURL_GLOBAL_ALL);
1582 plugin->curl_multi_handle = curl_multi_init ();
1584 if (NULL == plugin->curl_multi_handle)
1586 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1587 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1589 return GNUNET_SYSERR;
1595 * Session was idle, so disconnect it
1598 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1600 GNUNET_assert (NULL != cls);
1601 struct Session *s = cls;
1603 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1604 GNUNET_log (TIMEOUT_LOG,
1605 "Session %p was idle for %s, disconnecting\n",
1607 GNUNET_STRINGS_relative_time_to_string (CLIENT_SESSION_TIMEOUT,
1610 /* call session destroy function */
1611 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1616 * Start session timeout for session s
1618 * @param s the session
1621 client_start_session_timeout (struct Session *s)
1624 GNUNET_assert (NULL != s);
1625 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1626 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1627 &client_session_timeout,
1629 GNUNET_log (TIMEOUT_LOG,
1630 "Timeout for session %p set to %s\n",
1632 GNUNET_STRINGS_relative_time_to_string (CLIENT_SESSION_TIMEOUT,
1638 * Increment session timeout due to activity for session s
1640 * param s the session
1643 client_reschedule_session_timeout (struct Session *s)
1646 GNUNET_assert (NULL != s);
1647 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1649 GNUNET_SCHEDULER_cancel (s->timeout_task);
1650 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1651 &client_session_timeout,
1653 GNUNET_log (TIMEOUT_LOG,
1654 "Timeout rescheduled for session %p set to %s\n",
1656 GNUNET_STRINGS_relative_time_to_string (CLIENT_SESSION_TIMEOUT,
1662 * Cancel timeout due to activity for session s
1664 * param s the session
1667 client_stop_session_timeout (struct Session *s)
1669 GNUNET_assert (NULL != s);
1671 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1673 GNUNET_SCHEDULER_cancel (s->timeout_task);
1674 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1675 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1681 * Another peer has suggested an address for this
1682 * peer and transport plugin. Check that this could be a valid
1683 * address. If so, consider adding it to the list
1686 * @param cls closure
1687 * @param addr pointer to the address
1688 * @param addrlen length of addr
1689 * @return GNUNET_OK if this is a plausible address for this peer
1693 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1695 /* struct Plugin *plugin = cls; */
1697 /* A HTTP/S client does not have any valid address so:*/
1703 * Exit point from the plugin.
1705 * @param cls api as closure
1709 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1711 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1712 struct HTTP_Client_Plugin *plugin = api->cls;
1713 struct Session *pos;
1714 struct Session *next;
1716 if (NULL == api->cls)
1723 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1724 _("Shutting down plugin `%s'\n"),
1728 next = plugin->head;
1729 while (NULL != (pos = next))
1732 client_disconnect (pos);
1734 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1736 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1737 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1741 if (NULL != plugin->curl_multi_handle)
1743 curl_multi_cleanup (plugin->curl_multi_handle);
1744 plugin->curl_multi_handle = NULL;
1746 curl_global_cleanup ();
1748 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1749 _("Shutdown for plugin `%s' complete\n"),
1752 GNUNET_free (plugin);
1761 * @param plugin the plugin handle
1762 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1765 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1767 unsigned long long max_connections;
1769 /* Optional parameters */
1770 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1772 "MAX_CONNECTIONS", &max_connections))
1773 max_connections = 128;
1774 plugin->max_connections = max_connections;
1776 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1777 _("Maximum number of connections is %u\n"),
1778 plugin->max_connections);
1782 const char *http_plugin_address_to_string (void *cls,
1786 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1790 * Entry point for the plugin.
1793 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1795 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1796 struct GNUNET_TRANSPORT_PluginFunctions *api;
1797 struct HTTP_Client_Plugin *plugin;
1799 if (NULL == env->receive)
1801 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1802 initialze the plugin or the API */
1803 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1805 api->address_to_string = &http_plugin_address_to_string;
1806 api->string_to_address = &http_common_plugin_string_to_address;
1807 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1811 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1814 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1816 api->send = &http_client_plugin_send;
1817 api->disconnect = &http_client_plugin_disconnect;
1818 api->check_address = &http_client_plugin_address_suggested;
1819 api->get_session = &http_client_plugin_get_session;
1820 api->address_to_string = &http_plugin_address_to_string;
1821 api->string_to_address = &http_common_plugin_string_to_address;
1822 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1823 api->get_network = &http_client_get_network;
1826 plugin->name = "transport-https_client";
1827 plugin->protocol = "https";
1829 plugin->name = "transport-http_client";
1830 plugin->protocol = "http";
1832 plugin->last_tag = 1;
1833 plugin->options = 0; /* Setup options */
1835 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1837 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1842 if (GNUNET_SYSERR == client_start (plugin))
1844 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1850 /* end of plugin_transport_http_client.c */