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;
915 if (GNUNET_YES != client_exist_session(p, s))
922 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
923 atsi.value = s->ats_address_network_type;
924 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
925 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
926 s, (const char *) s->addr, s->addrlen);
928 plugin->env->update_address_metrics (plugin->env->cls,
935 GNUNET_asprintf (&stat_txt, "# bytes received via %s_client", plugin->protocol);
936 GNUNET_STATISTICS_update (plugin->env->stats,
937 stat_txt, ntohs(message->size), GNUNET_NO);
938 GNUNET_free (stat_txt);
941 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
943 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
946 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
947 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
948 GNUNET_i2s (&s->target),
949 http_common_plugin_address_to_string (NULL, s->plugin->protocol, s->addr, s->addrlen),
952 client_reschedule_session_timeout (s);
958 * Callback method used with libcurl when data for a PUT connection are
959 * received. We do not expect data here, so we just dismiss it
961 * @param stream pointer where to write data
962 * @param size size of an individual element
963 * @param nmemb count of elements that can be written to the buffer
964 * @param cls destination pointer, passed to the libcurl handle
965 * @return bytes read from stream
968 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
975 * Callback method used with libcurl when data for a GET connection are
976 * received. Forward to MST
978 * @param stream pointer where to write data
979 * @param size size of an individual element
980 * @param nmemb count of elements that can be written to the buffer
981 * @param cls destination pointer, passed to the libcurl handle
982 * @return bytes read from stream
985 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
987 struct Session *s = cls;
988 struct GNUNET_TIME_Absolute now;
989 size_t len = size * nmemb;
991 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
992 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
994 len, GNUNET_i2s (&s->target));
995 now = GNUNET_TIME_absolute_get ();
996 if (now.abs_value < s->next_receive.abs_value)
998 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
999 struct GNUNET_TIME_Relative delta =
1000 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1001 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1002 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %llu ms\n",
1003 s, s->client_get, delta.rel_value);
1004 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1006 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1007 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1009 s->recv_wakeup_task =
1010 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1011 return CURL_WRITEFUNC_PAUSE;
1013 if (NULL == s->msg_tk)
1014 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1015 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1021 * Task performing curl operations
1023 * @param cls plugin as closure
1024 * @param tc gnunet scheduler task context
1027 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1031 * Function setting up file descriptors and scheduling task to run
1033 * @param plugin the plugin as closure
1034 * @param now schedule task in 1ms, regardless of what curl may say
1035 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1038 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1044 struct GNUNET_NETWORK_FDSet *grs;
1045 struct GNUNET_NETWORK_FDSet *gws;
1048 struct GNUNET_TIME_Relative timeout;
1050 /* Cancel previous scheduled task */
1051 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1053 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1054 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1060 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1061 if (mret != CURLM_OK)
1063 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1064 "curl_multi_fdset", __FILE__, __LINE__,
1065 curl_multi_strerror (mret));
1066 return GNUNET_SYSERR;
1068 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1070 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1072 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1073 if (now == GNUNET_YES)
1074 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1076 if (mret != CURLM_OK)
1078 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1079 "curl_multi_timeout", __FILE__, __LINE__,
1080 curl_multi_strerror (mret));
1081 return GNUNET_SYSERR;
1084 grs = GNUNET_NETWORK_fdset_create ();
1085 gws = GNUNET_NETWORK_fdset_create ();
1086 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1087 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1089 plugin->client_perform_task =
1090 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1092 &client_run, plugin);
1093 GNUNET_NETWORK_fdset_destroy (gws);
1094 GNUNET_NETWORK_fdset_destroy (grs);
1100 * Task performing curl operations
1102 * @param cls plugin as closure
1103 * @param tc gnunet scheduler task context
1106 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1108 struct HTTP_Client_Plugin *plugin = cls;
1110 long http_statuscode;
1113 GNUNET_assert (cls != NULL);
1115 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1116 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1122 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1127 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1129 CURL *easy_h = msg->easy_handle;
1130 struct Session *s = NULL;
1131 char *d = (char *) s;
1136 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1137 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1139 curl_easy_strerror (msg->data.result), running);
1143 GNUNET_assert (CURLE_OK ==
1144 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1145 s = (struct Session *) d;
1147 if (GNUNET_YES != client_exist_session(plugin, s))
1153 GNUNET_assert (s != NULL);
1154 if (msg->msg == CURLMSG_DONE)
1156 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1157 if (easy_h == s->client_put)
1159 if ((0 != msg->data.result) || (http_statuscode != 200))
1161 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1162 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1163 s, msg->easy_handle, GNUNET_i2s (&s->target),
1166 curl_easy_strerror (msg->data.result));
1169 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1170 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1171 s, msg->easy_handle, GNUNET_i2s (&s->target));
1172 if (s->client_get == NULL)
1174 /* Disconnect other transmission direction and tell transport */
1176 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1177 curl_easy_cleanup (easy_h);
1178 s->put_tmp_disconnecting = GNUNET_NO;
1179 s->put_tmp_disconnected = GNUNET_YES;
1180 s->client_put = NULL;
1181 s->put.easyhandle = NULL;
1185 * Handling a rare case:
1186 * plugin_send was called during temporary put disconnect,
1187 * reconnect required after connection was disconnected
1189 if (GNUNET_YES == s->put_reconnect_required)
1191 s->put_reconnect_required = GNUNET_NO;
1192 if (GNUNET_SYSERR == client_connect_put(s))
1194 GNUNET_break (s->client_put == NULL);
1195 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1199 if (easy_h == s->client_get)
1201 if ((0 != msg->data.result) || (http_statuscode != 200))
1203 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1204 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1205 s, msg->easy_handle, GNUNET_i2s (&s->target),
1208 curl_easy_strerror (msg->data.result));
1212 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1213 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1214 s, msg->easy_handle, GNUNET_i2s (&s->target));
1215 /* Disconnect other transmission direction and tell transport */
1216 s->get.easyhandle = NULL;
1218 client_disconnect (s);
1223 while (mret == CURLM_CALL_MULTI_PERFORM);
1224 client_schedule (plugin, GNUNET_NO);
1229 * Connect GET connection for a session
1231 * @param s the session to connect
1232 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1235 client_connect_get (struct Session *s)
1239 /* create get connection */
1240 s->client_get = curl_easy_init ();
1242 s->get.easyhandle = s->client_get;
1244 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1245 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1246 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1249 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1250 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1251 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1253 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1254 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1258 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1259 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1261 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1262 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1264 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1265 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1268 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1269 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1270 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1271 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1272 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1273 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1274 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1275 /* No timeout by default, timeout done with session timeout */
1276 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1277 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1278 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1279 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1280 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1281 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1282 #if CURL_TCP_NODELAY
1283 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1285 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1287 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1288 if (mret != CURLM_OK)
1290 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1291 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1292 s, curl_multi_strerror (mret));
1293 curl_easy_cleanup (s->client_get);
1294 s->client_get = NULL;
1296 s->get.easyhandle = NULL;
1298 return GNUNET_SYSERR;
1305 * Connect a HTTP put connection
1307 * @param s the session to connect
1308 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1311 client_connect_put (struct Session *s)
1314 /* create put connection */
1315 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1316 "Session %p : Init PUT handle \n", s);
1317 s->client_put = curl_easy_init ();
1319 s->put.easyhandle = s->client_put;
1321 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1322 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1323 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1326 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1327 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1328 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1330 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1331 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1335 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1336 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1338 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1339 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1341 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1342 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1344 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1345 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1346 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1347 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1348 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1349 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1350 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1351 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1352 /* No timeout by default, timeout done with session timeout */
1353 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1354 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1355 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1356 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1357 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1358 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1359 #if CURL_TCP_NODELAY
1360 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1362 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1363 if (mret != CURLM_OK)
1365 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1366 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1367 s, curl_multi_strerror (mret));
1368 curl_easy_cleanup (s->client_put);
1369 s->client_put = NULL;
1370 s->put.easyhandle = NULL;
1372 s->put_tmp_disconnected = GNUNET_YES;
1373 return GNUNET_SYSERR;
1375 s->put_tmp_disconnected = GNUNET_NO;
1381 * Connect both PUT and GET connection for a session
1383 * @param s the session to connect
1384 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1387 client_connect (struct Session *s)
1390 struct HTTP_Client_Plugin *plugin = s->plugin;
1391 int res = GNUNET_OK;
1394 if (NULL == http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen))
1396 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1397 "Invalid address peer `%s'\n",
1398 GNUNET_i2s (&s->target));
1399 return GNUNET_SYSERR;
1402 GNUNET_asprintf (&s->url, "%s/%s;%u",
1403 http_common_plugin_address_to_url (NULL, s->addr, s->addrlen),
1404 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
1409 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1410 "Initiating outbound session peer `%s' using address `%s'\n",
1411 GNUNET_i2s (&s->target), s->url);
1413 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1414 (GNUNET_SYSERR == client_connect_put (s)))
1417 return GNUNET_SYSERR;
1420 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1421 "Session %p: connected with connections GET %p and PUT %p\n",
1422 s, s->client_get, s->client_put);
1424 /* Perform connect */
1425 plugin->cur_connections += 2;
1426 GNUNET_STATISTICS_set (plugin->env->stats,
1427 "# HTTP client connections",
1428 plugin->cur_connections,
1431 /* Re-schedule since handles have changed */
1432 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1434 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1435 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1437 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1443 * Function obtain the network type for a session
1445 * @param cls closure ('struct Plugin*')
1446 * @param session the session
1447 * @return the network type in HBO or GNUNET_SYSERR
1449 static enum GNUNET_ATS_Network_Type
1450 http_client_get_network (void *cls,
1451 struct Session *session)
1453 GNUNET_assert (NULL != session);
1454 return ntohl (session->ats_address_network_type);
1459 * Creates a new outbound session the transport service will use to send data to the
1462 * @param cls the plugin
1463 * @param address the address
1464 * @return the session or NULL of max connections exceeded
1466 static struct Session *
1467 http_client_plugin_get_session (void *cls,
1468 const struct GNUNET_HELLO_Address *address)
1470 struct HTTP_Client_Plugin *plugin = cls;
1471 struct Session * s = NULL;
1472 struct sockaddr *sa;
1473 struct GNUNET_ATS_Information ats;
1477 GNUNET_assert (plugin != NULL);
1478 GNUNET_assert (address != NULL);
1479 GNUNET_assert (address->address != NULL);
1481 /* find existing session */
1482 s = client_lookup_session (plugin, address);
1486 if (plugin->max_connections <= plugin->cur_connections)
1488 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1489 "Maximum number of connections (%u) reached: "
1490 "cannot connect to peer `%s'\n",
1491 plugin->max_connections,
1492 GNUNET_i2s (&address->peer));
1496 /* Determine network location */
1497 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1498 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1499 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1500 if (GNUNET_SYSERR == res)
1504 else if (GNUNET_YES == res)
1506 GNUNET_assert (NULL != sa);
1507 if (AF_INET == sa->sa_family)
1509 salen = sizeof (struct sockaddr_in);
1511 else if (AF_INET6 == sa->sa_family)
1513 salen = sizeof (struct sockaddr_in6);
1515 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1516 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1520 else if (GNUNET_NO == res)
1522 /* Cannot convert to sockaddr -> is external hostname */
1523 ats.value = htonl (GNUNET_ATS_NET_WAN);
1526 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1532 s = GNUNET_malloc (sizeof (struct Session));
1533 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1535 s->addr = GNUNET_malloc (address->address_length);
1536 memcpy (s->addr, address->address, address->address_length);
1537 s->addrlen = address->address_length;
1538 s->ats_address_network_type = ats.value;
1539 s->put_paused = GNUNET_NO;
1540 s->put_tmp_disconnecting = GNUNET_NO;
1541 s->put_tmp_disconnected = GNUNET_NO;
1542 client_start_session_timeout (s);
1544 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1545 "Created new session %p for `%s' address `%s''\n",
1547 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1548 GNUNET_i2s (&s->target));
1550 /* add new session */
1551 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1553 /* initiate new connection */
1554 if (GNUNET_SYSERR == client_connect (s))
1556 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1557 "Cannot connect to peer `%s' address `%s''\n",
1558 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1559 GNUNET_i2s (&s->target));
1560 client_delete_session (s);
1568 * Setup http_client plugin
1570 * @param plugin the plugin handle
1571 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1574 client_start (struct HTTP_Client_Plugin *plugin)
1576 curl_global_init (CURL_GLOBAL_ALL);
1577 plugin->curl_multi_handle = curl_multi_init ();
1579 if (NULL == plugin->curl_multi_handle)
1581 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1582 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1584 return GNUNET_SYSERR;
1590 * Session was idle, so disconnect it
1593 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1595 GNUNET_assert (NULL != cls);
1596 struct Session *s = cls;
1598 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1599 GNUNET_log (TIMEOUT_LOG,
1600 "Session %p was idle for %llu ms, disconnecting\n",
1601 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1603 /* call session destroy function */
1604 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1609 * Start session timeout for session s
1611 * @param s the session
1614 client_start_session_timeout (struct Session *s)
1617 GNUNET_assert (NULL != s);
1618 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1619 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1620 &client_session_timeout,
1622 GNUNET_log (TIMEOUT_LOG,
1623 "Timeout for session %p set to %llu ms\n",
1624 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1629 * Increment session timeout due to activity for session s
1631 * param s the session
1634 client_reschedule_session_timeout (struct Session *s)
1637 GNUNET_assert (NULL != s);
1638 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1640 GNUNET_SCHEDULER_cancel (s->timeout_task);
1641 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1642 &client_session_timeout,
1644 GNUNET_log (TIMEOUT_LOG,
1645 "Timeout rescheduled for session %p set to %llu ms\n",
1646 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1651 * Cancel timeout due to activity for session s
1653 * param s the session
1656 client_stop_session_timeout (struct Session *s)
1658 GNUNET_assert (NULL != s);
1660 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1662 GNUNET_SCHEDULER_cancel (s->timeout_task);
1663 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1664 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1670 * Another peer has suggested an address for this
1671 * peer and transport plugin. Check that this could be a valid
1672 * address. If so, consider adding it to the list
1675 * @param cls closure
1676 * @param addr pointer to the address
1677 * @param addrlen length of addr
1678 * @return GNUNET_OK if this is a plausible address for this peer
1682 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1684 /* struct Plugin *plugin = cls; */
1686 /* A HTTP/S client does not have any valid address so:*/
1692 * Exit point from the plugin.
1694 * @param cls api as closure
1698 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1700 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1701 struct HTTP_Client_Plugin *plugin = api->cls;
1702 struct Session *pos;
1703 struct Session *next;
1705 if (NULL == api->cls)
1712 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1713 _("Shutting down plugin `%s'\n"),
1717 next = plugin->head;
1718 while (NULL != (pos = next))
1721 client_disconnect (pos);
1723 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1725 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1726 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1730 if (NULL != plugin->curl_multi_handle)
1732 curl_multi_cleanup (plugin->curl_multi_handle);
1733 plugin->curl_multi_handle = NULL;
1735 curl_global_cleanup ();
1737 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1738 _("Shutdown for plugin `%s' complete\n"),
1741 GNUNET_free (plugin);
1750 * @param plugin the plugin handle
1751 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1754 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1756 unsigned long long max_connections;
1758 /* Optional parameters */
1759 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1761 "MAX_CONNECTIONS", &max_connections))
1762 max_connections = 128;
1763 plugin->max_connections = max_connections;
1765 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1766 _("Maximum number of connections is %u\n"),
1767 plugin->max_connections);
1771 const char *http_plugin_address_to_string (void *cls,
1775 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1779 * Entry point for the plugin.
1782 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1784 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1785 struct GNUNET_TRANSPORT_PluginFunctions *api;
1786 struct HTTP_Client_Plugin *plugin;
1788 if (NULL == env->receive)
1790 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1791 initialze the plugin or the API */
1792 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1794 api->address_to_string = &http_plugin_address_to_string;
1795 api->string_to_address = &http_common_plugin_string_to_address;
1796 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1800 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1803 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1805 api->send = &http_client_plugin_send;
1806 api->disconnect = &http_client_plugin_disconnect;
1807 api->check_address = &http_client_plugin_address_suggested;
1808 api->get_session = &http_client_plugin_get_session;
1809 api->address_to_string = &http_plugin_address_to_string;
1810 api->string_to_address = &http_common_plugin_string_to_address;
1811 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1812 api->get_network = &http_client_get_network;
1815 plugin->name = "transport-https_client";
1816 plugin->protocol = "https";
1818 plugin->name = "transport-http_client";
1819 plugin->protocol = "http";
1821 plugin->last_tag = 1;
1822 plugin->options = 0; /* Setup options */
1824 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1826 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1831 if (GNUNET_SYSERR == client_start (plugin))
1833 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1839 /* end of plugin_transport_http_client.c */