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_util_lib.h"
46 #include "gnunet_protocols.h"
47 #include "gnunet_transport_plugin.h"
48 #include "plugin_transport_http_common.h"
49 #include <curl/curl.h>
54 * Encapsulation of all of the state of the plugin.
56 struct HTTP_Client_Plugin;
60 * Message to send using http
65 * next pointer for double linked list
67 struct HTTP_Message *next;
70 * previous pointer for double linked list
72 struct HTTP_Message *prev;
75 * buffer containing data to send
80 * amount of data already sent
90 * Continuation function to call once the transmission buffer
91 * has again space available. NULL if there is no
92 * continuation to call.
94 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
97 * Closure for transmit_cont.
99 void *transmit_cont_cls;
104 * Session handle for connections.
109 * A connection handle
112 struct ConnectionHandle
115 * The curl easy handle
120 * The related session
128 * Session handle for connections.
133 * To whom are we talking to (set to our identity
134 * if we are still waiting for the welcome message)
136 struct GNUNET_PeerIdentity target;
139 * Stored in a linked list.
141 struct Session *next;
144 * Stored in a linked list.
146 struct Session *prev;
149 * The URL to connect to
156 struct HttpAddress *addr;
164 * ATS network type in NBO
166 uint32_t ats_address_network_type;
169 * Pointer to the global plugin struct.
171 struct HTTP_Client_Plugin *plugin;
178 struct ConnectionHandle put;
179 struct ConnectionHandle get;
182 * Is the client PUT handle currently paused
187 * Is the client PUT handle disconnect in progress?
189 int put_tmp_disconnecting;
192 * Is the client PUT handle temporarily disconnected?
194 int put_tmp_disconnected;
197 * We received data to send while disconnecting, reconnect immediately
199 int put_reconnect_required;
202 * Client receive handle
207 * Outbound overhead due to HTTP connection
208 * Add to next message of this session when calling callback
213 * next pointer for double linked list
215 struct HTTP_Message *msg_head;
218 * previous pointer for double linked list
220 struct HTTP_Message *msg_tail;
223 * Message stream tokenizer for incoming data
225 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
228 * Session timeout task
230 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
233 * Session timeout task
235 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
238 * Task to wake up client receive handle when receiving is allowed again
240 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
243 * Absolute time when to receive data again
244 * Used for receive throttling
246 struct GNUNET_TIME_Absolute next_receive;
251 * Encapsulation of all of the state of the plugin.
253 struct HTTP_Client_Plugin
258 struct GNUNET_TRANSPORT_PluginEnvironment *env;
261 * Linked list head of open sessions.
263 struct Session *head;
266 * Linked list tail of open sessions.
268 struct Session *tail;
281 * My options to be included in the address
286 * Maximum number of sockets the plugin can use
287 * Each http inbound /outbound connections are two connections
289 unsigned int max_connections;
292 * Current number of sockets the plugin can use
293 * Each http inbound /outbound connections are two connections
295 unsigned int cur_connections;
298 * Last used unique HTTP connection tag
315 CURLM *curl_multi_handle;
320 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
325 * Encapsulation of all of the state of the plugin.
327 struct HTTP_Client_Plugin *p;
331 * Start session timeout for a session
332 * @param s the session
335 client_start_session_timeout (struct Session *s);
339 * Increment session timeout due to activity for a session
340 * @param s the session
343 client_reschedule_session_timeout (struct Session *s);
347 * Cancel timeout for a session
348 * @param s the session
351 client_stop_session_timeout (struct Session *s);
355 * Function setting up file descriptors and scheduling task to run
357 * @param plugin plugin as closure
358 * @param now schedule task in 1ms, regardless of what curl may say
359 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
362 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
366 * Connect a HTTP put connection
368 * @param s the session to connect
369 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for success
372 client_connect_put (struct Session *s);
376 * Does a session s exists?
378 * @param plugin the plugin
379 * @param s desired session
380 * @return GNUNET_YES or GNUNET_NO
383 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
385 struct Session * head;
387 GNUNET_assert (NULL != plugin);
388 GNUNET_assert (NULL != s);
390 for (head = plugin->head; head != NULL; head = head->next)
402 * @param curl the curl easy handle
403 * @param type message type
404 * @param data data to log, NOT a 0-terminated string
405 * @param size data length
406 * @param cls the closure
410 client_log (CURL *curl, curl_infotype type,
411 const char *data, size_t size, void *cls)
413 struct ConnectionHandle *ch = cls;
414 const char *ttype = "UNSPECIFIED";
416 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
424 case CURLINFO_HEADER_IN:
427 case CURLINFO_HEADER_OUT:
428 ttype = "HEADER_OUT";
431 GNUNET_assert (NULL != ch);
432 GNUNET_assert (NULL != ch->easyhandle);
433 GNUNET_assert (NULL != ch->s);
434 ch->s->overhead += size;
437 ttype = "UNSPECIFIED";
441 memcpy (text, data, size);
442 if (text[size - 1] == '\n')
447 text[size + 1] = '\0';
450 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
451 "Connection %p %s: %s", ch->easyhandle, ttype, text);
453 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
454 "Connection %p %s: %s", ch->easyhandle, ttype, text);
462 * Function that can be used by the transport service to transmit
463 * a message using the plugin. Note that in the case of a
464 * peer disconnecting, the continuation MUST be called
465 * prior to the disconnect notification itself. This function
466 * will be called with this peer's HELLO message to initiate
467 * a fresh connection to another peer.
470 * @param s which session must be used
471 * @param msgbuf the message to transmit
472 * @param msgbuf_size number of bytes in 'msgbuf'
473 * @param priority how important is the message (most plugins will
474 * ignore message priority and just FIFO)
475 * @param to how long to wait at most for the transmission (does not
476 * require plugins to discard the message after the timeout,
477 * just advisory for the desired delay; most plugins will ignore
479 * @param cont continuation to call once the message has
480 * been transmitted (or if the transport is ready
481 * for the next transmission call; or if the
482 * peer disconnected...); can be NULL
483 * @param cont_cls closure for cont
484 * @return number of bytes used (on the physical network, with overheads);
485 * -1 on hard errors (i.e. address invalid); 0 is a legal value
486 * and does NOT mean that the message was not transmitted (DV)
489 http_client_plugin_send (void *cls,
491 const char *msgbuf, size_t msgbuf_size,
492 unsigned int priority,
493 struct GNUNET_TIME_Relative to,
494 GNUNET_TRANSPORT_TransmitContinuation cont,
497 struct HTTP_Client_Plugin *plugin = cls;
498 struct HTTP_Message *msg;
501 GNUNET_assert (plugin != NULL);
502 GNUNET_assert (s != NULL);
504 /* lookup if session is really existing */
505 if (GNUNET_YES != client_exist_session (plugin, s))
508 return GNUNET_SYSERR;
511 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
512 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
514 msgbuf_size, GNUNET_i2s (&s->target));
516 /* create new message and schedule */
517 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
519 msg->size = msgbuf_size;
521 msg->buf = (char *) &msg[1];
522 msg->transmit_cont = cont;
523 msg->transmit_cont_cls = cont_cls;
524 memcpy (msg->buf, msgbuf, msgbuf_size);
525 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
527 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
528 GNUNET_STATISTICS_update (plugin->env->stats,
529 stat_txt, msgbuf_size, GNUNET_NO);
531 GNUNET_free (stat_txt);
533 if (GNUNET_YES == s->put_tmp_disconnecting)
535 /* PUT connection is currently getting disconnected */
536 s->put_reconnect_required = GNUNET_YES;
537 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
538 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
542 else if (GNUNET_YES == s->put_paused)
544 /* PUT connection was paused, unpause */
545 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
546 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
547 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
548 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
549 "Session %p/connection %p: unpausing connection\n",
551 s->put_paused = GNUNET_NO;
552 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
554 else if (GNUNET_YES == s->put_tmp_disconnected)
556 /* PUT connection was disconnected, reconnect */
557 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
558 "Session %p: Reconnecting PUT connection\n",
560 s->put_tmp_disconnected = GNUNET_NO;
561 GNUNET_break (s->client_put == NULL);
562 if (GNUNET_SYSERR == client_connect_put (s))
564 return GNUNET_SYSERR;
568 client_schedule (s->plugin, GNUNET_YES);
569 client_reschedule_session_timeout (s);
577 * @param s the session to delete
580 client_delete_session (struct Session *s)
582 struct HTTP_Client_Plugin *plugin = s->plugin;
583 struct HTTP_Message *pos;
584 struct HTTP_Message *next;
586 client_stop_session_timeout (s);
588 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
590 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
591 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
594 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
597 while (NULL != (pos = next))
600 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
601 if (pos->transmit_cont != NULL)
602 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
603 pos->size, pos->pos + s->overhead);
608 if (s->msg_tk != NULL)
610 GNUNET_SERVER_mst_destroy (s->msg_tk);
613 GNUNET_free (s->addr);
614 GNUNET_free (s->url);
621 * Disconnect a session
624 * @return GNUNET_OK on success, GNUNET_SYSERR on error
627 client_disconnect (struct Session *s)
629 struct HTTP_Client_Plugin *plugin = s->plugin;
630 struct HTTP_Message *msg;
631 struct HTTP_Message *t;
635 if (GNUNET_YES != client_exist_session (plugin, s))
638 return GNUNET_SYSERR;
641 if (s->client_put != NULL)
643 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
644 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
645 s, s->client_put, GNUNET_i2s (&s->target));
647 /* remove curl handle from multi handle */
648 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
649 if (mret != CURLM_OK)
651 /* clean up easy handle, handle is now invalid and free'd */
655 curl_easy_cleanup (s->client_put);
656 s->client_put = NULL;
660 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
662 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
663 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
666 if (s->client_get != NULL)
668 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
669 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
670 s, s->client_get, GNUNET_i2s (&s->target));
672 /* remove curl handle from multi handle */
673 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
674 if (mret != CURLM_OK)
676 /* clean up easy handle, handle is now invalid and free'd */
680 curl_easy_cleanup (s->client_get);
681 s->client_get = NULL;
688 if (NULL != msg->transmit_cont)
689 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
690 msg->size, msg->pos + s->overhead);
692 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
697 GNUNET_assert (plugin->cur_connections >= 2);
698 plugin->cur_connections -= 2;
699 GNUNET_STATISTICS_set (plugin->env->stats,
700 "# HTTP client sessions",
701 plugin->cur_connections,
704 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
705 "Session %p: notifying transport about ending session\n",s);
707 plugin->env->session_end (plugin->env->cls, &s->target, s);
708 client_delete_session (s);
710 /* Re-schedule since handles have changed */
711 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
713 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
714 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
716 client_schedule (plugin, GNUNET_YES);
723 * Function that can be used to force the plugin to disconnect
724 * from the given peer and cancel all previous transmissions
725 * (and their continuationc).
728 * @param target peer from which to disconnect
731 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
733 struct HTTP_Client_Plugin *plugin = cls;
734 struct Session *next = NULL;
735 struct Session *pos = NULL;
737 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
738 "Transport tells me to disconnect `%s'\n",
739 GNUNET_i2s (target));
742 while (NULL != (pos = next))
745 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
747 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
748 "Disconnecting session %p to `%pos'\n",
749 pos, GNUNET_i2s (target));
750 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
757 * Check if a sessions exists for an specific address
759 * @param plugin the plugin
760 * @param address the address
761 * @return the session or NULL
763 static struct Session *
764 client_lookup_session (struct HTTP_Client_Plugin *plugin,
765 const struct GNUNET_HELLO_Address *address)
769 for (pos = plugin->head; NULL != pos; pos = pos->next)
770 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
771 (address->address_length == pos->addrlen) &&
772 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
778 client_put_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
780 struct Session *s = cls;
781 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
782 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
783 "Session %p/connection %p: will be disconnected due to no activity\n",
785 s->put_paused = GNUNET_NO;
786 s->put_tmp_disconnecting = GNUNET_YES;
787 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
788 client_schedule (s->plugin, GNUNET_YES);
794 * Callback method used with libcurl
795 * Method is called when libcurl needs to read data during sending
797 * @param stream pointer where to write data
798 * @param size size of an individual element
799 * @param nmemb count of elements that can be written to the buffer
800 * @param cls source pointer, passed to the libcurl handle
801 * @return bytes written to stream, returning 0 will terminate connection!
804 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
806 struct Session *s = cls;
807 struct HTTP_Client_Plugin *plugin = s->plugin;
808 struct HTTP_Message *msg = s->msg_head;
812 if (GNUNET_YES != client_exist_session (plugin, s))
817 if (GNUNET_YES == s->put_tmp_disconnecting)
820 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
821 "Session %p/connection %p: disconnect due to inactivity\n",
828 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
829 "Session %p/connection %p: nothing to send, suspending\n",
831 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
832 s->put_paused = GNUNET_YES;
833 return CURL_READFUNC_PAUSE;
836 GNUNET_assert (msg->pos < msg->size);
837 /* calculate how much fits in buffer */
838 len = GNUNET_MIN (msg->size - msg->pos,
840 memcpy (stream, &msg->buf[msg->pos], len);
842 if (msg->pos == msg->size)
844 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
845 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
846 s, s->client_put, msg->size, msg->pos);
847 /* Calling transmit continuation */
848 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
849 if (NULL != msg->transmit_cont)
850 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
851 msg->size, msg->size + s->overhead);
856 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
857 GNUNET_STATISTICS_update (plugin->env->stats,
858 stat_txt, -len, GNUNET_NO);
859 GNUNET_free (stat_txt);
861 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
862 GNUNET_STATISTICS_update (plugin->env->stats,
863 stat_txt, len, GNUNET_NO);
864 GNUNET_free (stat_txt);
866 client_reschedule_session_timeout (s);
872 * Wake up a curl handle which was suspended
874 * @param cls the session
875 * @param tc task context
878 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
880 struct Session *s = cls;
882 if (GNUNET_YES != client_exist_session(p, s))
887 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
888 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
890 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
891 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
892 if (s->client_get != NULL)
893 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
898 * Callback for message stream tokenizer
900 * @param cls the session
901 * @param client not used
902 * @param message the message received
903 * @return always GNUNET_OK
906 client_receive_mst_cb (void *cls, void *client,
907 const struct GNUNET_MessageHeader *message)
909 struct Session *s = cls;
910 struct HTTP_Client_Plugin *plugin;
911 struct GNUNET_TIME_Relative delay;
912 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_us < s->next_receive.abs_value_us)
945 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
946 "Client: peer `%s' address `%s' next read delayed for %s\n",
947 GNUNET_i2s (&s->target),
948 http_common_plugin_address_to_string (NULL,
950 s->addr, s->addrlen),
951 GNUNET_STRINGS_relative_time_to_string (delay,
954 client_reschedule_session_timeout (s);
960 * Callback method used with libcurl when data for a PUT connection are
961 * received. We do not expect data here, so we just dismiss it
963 * @param stream pointer where to write data
964 * @param size size of an individual element
965 * @param nmemb count of elements that can be written to the buffer
966 * @param cls destination pointer, passed to the libcurl handle
967 * @return bytes read from stream
970 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
977 * Callback method used with libcurl when data for a GET connection are
978 * received. Forward to MST
980 * @param stream pointer where to write data
981 * @param size size of an individual element
982 * @param nmemb count of elements that can be written to the buffer
983 * @param cls destination pointer, passed to the libcurl handle
984 * @return bytes read from stream
987 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
989 struct Session *s = cls;
990 struct GNUNET_TIME_Absolute now;
991 size_t len = size * nmemb;
993 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
994 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
996 len, GNUNET_i2s (&s->target));
997 now = GNUNET_TIME_absolute_get ();
998 if (now.abs_value_us < s->next_receive.abs_value_us)
1000 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1001 struct GNUNET_TIME_Relative delta =
1002 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1003 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1004 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1006 GNUNET_STRINGS_relative_time_to_string (delta,
1008 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1010 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1011 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1013 s->recv_wakeup_task =
1014 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1015 return CURL_WRITEFUNC_PAUSE;
1017 if (NULL == s->msg_tk)
1018 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1019 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1025 * Task performing curl operations
1027 * @param cls plugin as closure
1028 * @param tc gnunet scheduler task context
1031 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1035 * Function setting up file descriptors and scheduling task to run
1037 * @param plugin the plugin as closure
1038 * @param now schedule task in 1ms, regardless of what curl may say
1039 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1042 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1048 struct GNUNET_NETWORK_FDSet *grs;
1049 struct GNUNET_NETWORK_FDSet *gws;
1052 struct GNUNET_TIME_Relative timeout;
1054 /* Cancel previous scheduled task */
1055 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1057 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1058 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1064 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1065 if (mret != CURLM_OK)
1067 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1068 "curl_multi_fdset", __FILE__, __LINE__,
1069 curl_multi_strerror (mret));
1070 return GNUNET_SYSERR;
1072 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1074 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1076 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1077 if (now == GNUNET_YES)
1078 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1080 if (mret != CURLM_OK)
1082 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1083 "curl_multi_timeout", __FILE__, __LINE__,
1084 curl_multi_strerror (mret));
1085 return GNUNET_SYSERR;
1088 grs = GNUNET_NETWORK_fdset_create ();
1089 gws = GNUNET_NETWORK_fdset_create ();
1090 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1091 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1093 plugin->client_perform_task =
1094 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1096 &client_run, plugin);
1097 GNUNET_NETWORK_fdset_destroy (gws);
1098 GNUNET_NETWORK_fdset_destroy (grs);
1104 * Task performing curl operations
1106 * @param cls plugin as closure
1107 * @param tc gnunet scheduler task context
1110 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1112 struct HTTP_Client_Plugin *plugin = cls;
1114 long http_statuscode;
1117 GNUNET_assert (cls != NULL);
1119 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1120 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1126 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1131 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1133 CURL *easy_h = msg->easy_handle;
1134 struct Session *s = NULL;
1135 char *d = (char *) s;
1140 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1141 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1143 curl_easy_strerror (msg->data.result), running);
1147 GNUNET_assert (CURLE_OK ==
1148 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1149 s = (struct Session *) d;
1151 if (GNUNET_YES != client_exist_session(plugin, s))
1157 GNUNET_assert (s != NULL);
1158 if (msg->msg == CURLMSG_DONE)
1160 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1161 if (easy_h == s->client_put)
1163 if ((0 != msg->data.result) || (http_statuscode != 200))
1165 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1166 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1167 s, msg->easy_handle, GNUNET_i2s (&s->target),
1170 curl_easy_strerror (msg->data.result));
1173 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1174 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1175 s, msg->easy_handle, GNUNET_i2s (&s->target));
1176 if (s->client_get == NULL)
1178 /* Disconnect other transmission direction and tell transport */
1180 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1181 curl_easy_cleanup (easy_h);
1182 s->put_tmp_disconnecting = GNUNET_NO;
1183 s->put_tmp_disconnected = GNUNET_YES;
1184 s->client_put = NULL;
1185 s->put.easyhandle = NULL;
1189 * Handling a rare case:
1190 * plugin_send was called during temporary put disconnect,
1191 * reconnect required after connection was disconnected
1193 if (GNUNET_YES == s->put_reconnect_required)
1195 s->put_reconnect_required = GNUNET_NO;
1196 if (GNUNET_SYSERR == client_connect_put(s))
1198 GNUNET_break (s->client_put == NULL);
1199 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1203 if (easy_h == s->client_get)
1205 if ((0 != msg->data.result) || (http_statuscode != 200))
1207 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1208 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1209 s, msg->easy_handle, GNUNET_i2s (&s->target),
1212 curl_easy_strerror (msg->data.result));
1216 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1217 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1218 s, msg->easy_handle, GNUNET_i2s (&s->target));
1219 /* Disconnect other transmission direction and tell transport */
1220 s->get.easyhandle = NULL;
1222 client_disconnect (s);
1227 while (mret == CURLM_CALL_MULTI_PERFORM);
1228 client_schedule (plugin, GNUNET_NO);
1233 * Connect GET connection for a session
1235 * @param s the session to connect
1236 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1239 client_connect_get (struct Session *s)
1243 /* create get connection */
1244 s->client_get = curl_easy_init ();
1246 s->get.easyhandle = s->client_get;
1248 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1249 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1250 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1253 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1254 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1255 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1257 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1258 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1262 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1263 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1265 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1266 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1268 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1269 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1272 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1273 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1274 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1275 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1276 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1277 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1278 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1279 /* No timeout by default, timeout done with session timeout */
1280 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1281 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1282 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1283 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1284 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1285 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1286 #if CURL_TCP_NODELAY
1287 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1289 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1291 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1292 if (mret != CURLM_OK)
1294 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1295 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1296 s, curl_multi_strerror (mret));
1297 curl_easy_cleanup (s->client_get);
1298 s->client_get = NULL;
1300 s->get.easyhandle = NULL;
1302 return GNUNET_SYSERR;
1309 * Connect a HTTP put connection
1311 * @param s the session to connect
1312 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1315 client_connect_put (struct Session *s)
1318 /* create put connection */
1319 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1320 "Session %p : Init PUT handle \n", s);
1321 s->client_put = curl_easy_init ();
1323 s->put.easyhandle = s->client_put;
1325 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1326 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1327 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1330 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1331 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1332 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1334 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1335 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1339 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1340 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1342 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1343 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1345 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1346 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1348 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1349 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1350 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1351 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1352 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1353 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1354 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1355 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1356 /* No timeout by default, timeout done with session timeout */
1357 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1358 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1359 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1360 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1361 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1362 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1363 #if CURL_TCP_NODELAY
1364 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1366 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1367 if (mret != CURLM_OK)
1369 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1370 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1371 s, curl_multi_strerror (mret));
1372 curl_easy_cleanup (s->client_put);
1373 s->client_put = NULL;
1374 s->put.easyhandle = NULL;
1376 s->put_tmp_disconnected = GNUNET_YES;
1377 return GNUNET_SYSERR;
1379 s->put_tmp_disconnected = GNUNET_NO;
1385 * Connect both PUT and GET connection for a session
1387 * @param s the session to connect
1388 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1391 client_connect (struct Session *s)
1394 struct HTTP_Client_Plugin *plugin = s->plugin;
1395 int res = GNUNET_OK;
1398 if (NULL == http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen))
1400 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1401 "Invalid address peer `%s'\n",
1402 GNUNET_i2s (&s->target));
1403 return GNUNET_SYSERR;
1406 GNUNET_asprintf (&s->url, "%s/%s;%u",
1407 http_common_plugin_address_to_url (NULL, s->addr, s->addrlen),
1408 GNUNET_i2s_full (plugin->env->my_identity),
1413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1414 "Initiating outbound session peer `%s' using address `%s'\n",
1415 GNUNET_i2s (&s->target), s->url);
1417 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1418 (GNUNET_SYSERR == client_connect_put (s)))
1421 return GNUNET_SYSERR;
1424 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1425 "Session %p: connected with connections GET %p and PUT %p\n",
1426 s, s->client_get, s->client_put);
1428 /* Perform connect */
1429 plugin->cur_connections += 2;
1430 GNUNET_STATISTICS_set (plugin->env->stats,
1431 "# HTTP client connections",
1432 plugin->cur_connections,
1435 /* Re-schedule since handles have changed */
1436 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1438 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1439 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1441 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1447 * Function obtain the network type for a session
1449 * @param cls closure ('struct Plugin*')
1450 * @param session the session
1451 * @return the network type in HBO or GNUNET_SYSERR
1453 static enum GNUNET_ATS_Network_Type
1454 http_client_get_network (void *cls,
1455 struct Session *session)
1457 GNUNET_assert (NULL != session);
1458 return ntohl (session->ats_address_network_type);
1463 * Creates a new outbound session the transport service will use to send data to the
1466 * @param cls the plugin
1467 * @param address the address
1468 * @return the session or NULL of max connections exceeded
1470 static struct Session *
1471 http_client_plugin_get_session (void *cls,
1472 const struct GNUNET_HELLO_Address *address)
1474 struct HTTP_Client_Plugin *plugin = cls;
1475 struct Session * s = NULL;
1476 struct sockaddr *sa;
1477 struct GNUNET_ATS_Information ats;
1481 GNUNET_assert (plugin != NULL);
1482 GNUNET_assert (address != NULL);
1483 GNUNET_assert (address->address != NULL);
1485 /* find existing session */
1486 s = client_lookup_session (plugin, address);
1490 if (plugin->max_connections <= plugin->cur_connections)
1492 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1493 "Maximum number of connections (%u) reached: "
1494 "cannot connect to peer `%s'\n",
1495 plugin->max_connections,
1496 GNUNET_i2s (&address->peer));
1500 /* Determine network location */
1501 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1502 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1503 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1504 if (GNUNET_SYSERR == res)
1508 else if (GNUNET_YES == res)
1510 GNUNET_assert (NULL != sa);
1511 if (AF_INET == sa->sa_family)
1513 salen = sizeof (struct sockaddr_in);
1515 else if (AF_INET6 == sa->sa_family)
1517 salen = sizeof (struct sockaddr_in6);
1519 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1520 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1524 else if (GNUNET_NO == res)
1526 /* Cannot convert to sockaddr -> is external hostname */
1527 ats.value = htonl (GNUNET_ATS_NET_WAN);
1530 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1536 s = GNUNET_malloc (sizeof (struct Session));
1537 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1539 s->addr = GNUNET_malloc (address->address_length);
1540 memcpy (s->addr, address->address, address->address_length);
1541 s->addrlen = address->address_length;
1542 s->ats_address_network_type = ats.value;
1543 s->put_paused = GNUNET_NO;
1544 s->put_tmp_disconnecting = GNUNET_NO;
1545 s->put_tmp_disconnected = GNUNET_NO;
1546 client_start_session_timeout (s);
1548 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1549 "Created new session %p for `%s' address `%s''\n",
1551 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1552 GNUNET_i2s (&s->target));
1554 /* add new session */
1555 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1557 /* initiate new connection */
1558 if (GNUNET_SYSERR == client_connect (s))
1560 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1561 "Cannot connect to peer `%s' address `%s''\n",
1562 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1563 GNUNET_i2s (&s->target));
1564 client_delete_session (s);
1572 * Setup http_client plugin
1574 * @param plugin the plugin handle
1575 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1578 client_start (struct HTTP_Client_Plugin *plugin)
1580 curl_global_init (CURL_GLOBAL_ALL);
1581 plugin->curl_multi_handle = curl_multi_init ();
1583 if (NULL == plugin->curl_multi_handle)
1585 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1586 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1588 return GNUNET_SYSERR;
1594 * Session was idle, so disconnect it
1597 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1599 GNUNET_assert (NULL != cls);
1600 struct Session *s = cls;
1602 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1603 GNUNET_log (TIMEOUT_LOG,
1604 "Session %p was idle for %s, disconnecting\n",
1606 GNUNET_STRINGS_relative_time_to_string (CLIENT_SESSION_TIMEOUT,
1609 /* call session destroy function */
1610 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1615 * Start session timeout for session s
1617 * @param s the session
1620 client_start_session_timeout (struct Session *s)
1623 GNUNET_assert (NULL != s);
1624 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1625 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1626 &client_session_timeout,
1628 GNUNET_log (TIMEOUT_LOG,
1629 "Timeout for session %p set to %s\n",
1631 GNUNET_STRINGS_relative_time_to_string (CLIENT_SESSION_TIMEOUT,
1637 * Increment session timeout due to activity for session s
1639 * param s the session
1642 client_reschedule_session_timeout (struct Session *s)
1645 GNUNET_assert (NULL != s);
1646 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1648 GNUNET_SCHEDULER_cancel (s->timeout_task);
1649 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1650 &client_session_timeout,
1652 GNUNET_log (TIMEOUT_LOG,
1653 "Timeout rescheduled for session %p set to %s\n",
1655 GNUNET_STRINGS_relative_time_to_string (CLIENT_SESSION_TIMEOUT,
1661 * Cancel timeout due to activity for session s
1663 * param s the session
1666 client_stop_session_timeout (struct Session *s)
1668 GNUNET_assert (NULL != s);
1670 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1672 GNUNET_SCHEDULER_cancel (s->timeout_task);
1673 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1674 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1680 * Another peer has suggested an address for this
1681 * peer and transport plugin. Check that this could be a valid
1682 * address. If so, consider adding it to the list
1685 * @param cls closure
1686 * @param addr pointer to the address
1687 * @param addrlen length of addr
1688 * @return GNUNET_OK if this is a plausible address for this peer
1692 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1694 /* struct Plugin *plugin = cls; */
1696 /* A HTTP/S client does not have any valid address so:*/
1702 * Exit point from the plugin.
1704 * @param cls api as closure
1708 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1710 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1711 struct HTTP_Client_Plugin *plugin = api->cls;
1712 struct Session *pos;
1713 struct Session *next;
1715 if (NULL == api->cls)
1722 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1723 _("Shutting down plugin `%s'\n"),
1727 next = plugin->head;
1728 while (NULL != (pos = next))
1731 client_disconnect (pos);
1733 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1735 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1736 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1740 if (NULL != plugin->curl_multi_handle)
1742 curl_multi_cleanup (plugin->curl_multi_handle);
1743 plugin->curl_multi_handle = NULL;
1745 curl_global_cleanup ();
1747 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1748 _("Shutdown for plugin `%s' complete\n"),
1751 GNUNET_free (plugin);
1760 * @param plugin the plugin handle
1761 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1764 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1766 unsigned long long max_connections;
1768 /* Optional parameters */
1769 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1771 "MAX_CONNECTIONS", &max_connections))
1772 max_connections = 128;
1773 plugin->max_connections = max_connections;
1775 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1776 _("Maximum number of connections is %u\n"),
1777 plugin->max_connections);
1781 const char *http_plugin_address_to_string (void *cls,
1785 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1789 * Entry point for the plugin.
1792 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1794 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1795 struct GNUNET_TRANSPORT_PluginFunctions *api;
1796 struct HTTP_Client_Plugin *plugin;
1798 if (NULL == env->receive)
1800 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1801 initialze the plugin or the API */
1802 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1804 api->address_to_string = &http_plugin_address_to_string;
1805 api->string_to_address = &http_common_plugin_string_to_address;
1806 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1810 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1813 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1815 api->send = &http_client_plugin_send;
1816 api->disconnect = &http_client_plugin_disconnect;
1817 api->check_address = &http_client_plugin_address_suggested;
1818 api->get_session = &http_client_plugin_get_session;
1819 api->address_to_string = &http_plugin_address_to_string;
1820 api->string_to_address = &http_common_plugin_string_to_address;
1821 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1822 api->get_network = &http_client_get_network;
1825 plugin->name = "transport-https_client";
1826 plugin->protocol = "https";
1828 plugin->name = "transport-http_client";
1829 plugin->protocol = "http";
1831 plugin->last_tag = 1;
1832 plugin->options = 0; /* Setup options */
1834 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1836 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1841 if (GNUNET_SYSERR == client_start (plugin))
1843 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1849 /* end of plugin_transport_http_client.c */