2 This file is part of GNUnet
3 (C) 2002-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http_client.c
23 * @brief HTTP/S client transport plugin
24 * @author Matthias Wachs
28 #define PLUGIN_NAME "https_client"
29 #define HTTP_STAT_STR_CONNECTIONS "# HTTPS client connections"
30 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
31 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
33 #define PLUGIN_NAME "http_client"
34 #define HTTP_STAT_STR_CONNECTIONS "# HTTP client connections"
35 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
36 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
39 #define VERBOSE_CURL GNUNET_YES
41 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
43 #define ENABLE_PUT GNUNET_YES
44 #define ENABLE_GET GNUNET_YES
47 #include "gnunet_util_lib.h"
48 #include "gnunet_protocols.h"
49 #include "gnunet_transport_plugin.h"
50 #include "plugin_transport_http_common.h"
51 #include <curl/curl.h>
56 * Encapsulation of all of the state of the plugin.
58 struct HTTP_Client_Plugin;
62 * Message to send using http
67 * next pointer for double linked list
69 struct HTTP_Message *next;
72 * previous pointer for double linked list
74 struct HTTP_Message *prev;
77 * buffer containing data to send
82 * amount of data already sent
92 * Continuation function to call once the transmission buffer
93 * has again space available. NULL if there is no
94 * continuation to call.
96 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
99 * Closure for transmit_cont.
101 void *transmit_cont_cls;
106 * Session handle for connections.
112 * A connection handle
115 struct ConnectionHandle
118 * The curl easy handle
123 * The related session
130 * Session handle for connections.
135 * To whom are we talking to (set to our identity
136 * if we are still waiting for the welcome message)
138 struct GNUNET_PeerIdentity target;
141 * Stored in a linked list.
143 struct Session *next;
146 * Stored in a linked list.
148 struct Session *prev;
151 * The URL to connect to
158 struct HttpAddress *addr;
166 * ATS network type in NBO
168 uint32_t ats_address_network_type;
171 * Pointer to the global plugin struct.
173 struct HTTP_Client_Plugin *plugin;
180 struct ConnectionHandle put;
181 struct ConnectionHandle get;
184 * Is the client PUT handle currently paused
189 * Is the client PUT handle disconnect in progress?
191 int put_tmp_disconnecting;
194 * Is the client PUT handle temporarily disconnected?
196 int put_tmp_disconnected;
199 * We received data to send while disconnecting, reconnect immediately
201 int put_reconnect_required;
204 * Client receive handle
209 * Outbound overhead due to HTTP connection
210 * Add to next message of this session when calling callback
215 * next pointer for double linked list
217 struct HTTP_Message *msg_head;
220 * previous pointer for double linked list
222 struct HTTP_Message *msg_tail;
225 * Message stream tokenizer for incoming data
227 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
230 * Session timeout task
232 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
235 * Session timeout task
237 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
240 * Task to wake up client receive handle when receiving is allowed again
242 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
245 * Absolute time when to receive data again
246 * Used for receive throttling
248 struct GNUNET_TIME_Absolute next_receive;
253 * Encapsulation of all of the state of the plugin.
255 struct HTTP_Client_Plugin
260 struct GNUNET_TRANSPORT_PluginEnvironment *env;
263 * Linked list head of open sessions.
265 struct Session *head;
268 * Linked list tail of open sessions.
270 struct Session *tail;
283 * My options to be included in the address
288 * Maximum number of sockets the plugin can use
289 * Each http inbound /outbound connections are two connections
291 unsigned int max_connections;
294 * Current number of sockets the plugin can use
295 * Each http inbound /outbound connections are two connections
297 unsigned int cur_connections;
300 * Last used unique HTTP connection tag
317 CURLM *curl_multi_handle;
322 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
327 * Increment session timeout due to activity for a session
328 * @param s the session
331 client_reschedule_session_timeout (struct Session *s);
335 * Function setting up file descriptors and scheduling task to run
337 * @param plugin plugin as closure
338 * @param now schedule task in 1ms, regardless of what curl may say
339 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
342 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
346 * Connect a HTTP put connection
348 * @param s the session to connect
349 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
352 client_connect_put (struct Session *s);
356 * Does a session s exists?
358 * @param plugin the plugin
359 * @param s desired session
360 * @return #GNUNET_YES or #GNUNET_NO
363 client_exist_session (struct HTTP_Client_Plugin *plugin,
366 struct Session * head;
368 for (head = plugin->head; head != NULL; head = head->next)
378 * @param curl the curl easy handle
379 * @param type message type
380 * @param data data to log, NOT a 0-terminated string
381 * @param size data length
382 * @param cls the closure
386 client_log (CURL *curl, curl_infotype type,
387 const char *data, size_t size, void *cls)
389 struct ConnectionHandle *ch = cls;
390 const char *ttype = "UNSPECIFIED";
392 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
400 case CURLINFO_HEADER_IN:
403 case CURLINFO_HEADER_OUT:
404 ttype = "HEADER_OUT";
407 GNUNET_assert (NULL != ch);
408 GNUNET_assert (NULL != ch->easyhandle);
409 GNUNET_assert (NULL != ch->s);
410 ch->s->overhead += size;
413 ttype = "UNSPECIFIED";
417 memcpy (text, data, size);
418 if (text[size - 1] == '\n')
423 text[size + 1] = '\0';
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
427 "Connection %p %s: %s", ch->easyhandle, ttype, text);
429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
430 "Connection %p %s: %s", ch->easyhandle, ttype, text);
438 * Function that can be used by the transport service to transmit
439 * a message using the plugin. Note that in the case of a
440 * peer disconnecting, the continuation MUST be called
441 * prior to the disconnect notification itself. This function
442 * will be called with this peer's HELLO message to initiate
443 * a fresh connection to another peer.
446 * @param s which session must be used
447 * @param msgbuf the message to transmit
448 * @param msgbuf_size number of bytes in 'msgbuf'
449 * @param priority how important is the message (most plugins will
450 * ignore message priority and just FIFO)
451 * @param to how long to wait at most for the transmission (does not
452 * require plugins to discard the message after the timeout,
453 * just advisory for the desired delay; most plugins will ignore
455 * @param cont continuation to call once the message has
456 * been transmitted (or if the transport is ready
457 * for the next transmission call; or if the
458 * peer disconnected...); can be NULL
459 * @param cont_cls closure for cont
460 * @return number of bytes used (on the physical network, with overheads);
461 * -1 on hard errors (i.e. address invalid); 0 is a legal value
462 * and does NOT mean that the message was not transmitted (DV)
465 http_client_plugin_send (void *cls,
467 const char *msgbuf, size_t msgbuf_size,
468 unsigned int priority,
469 struct GNUNET_TIME_Relative to,
470 GNUNET_TRANSPORT_TransmitContinuation cont,
473 struct HTTP_Client_Plugin *plugin = cls;
474 struct HTTP_Message *msg;
477 GNUNET_assert (plugin != NULL);
478 GNUNET_assert (s != NULL);
480 /* lookup if session is really existing */
481 if (GNUNET_YES != client_exist_session (plugin, s))
484 return GNUNET_SYSERR;
487 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
488 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
490 msgbuf_size, GNUNET_i2s (&s->target));
492 /* create new message and schedule */
493 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
495 msg->size = msgbuf_size;
497 msg->buf = (char *) &msg[1];
498 msg->transmit_cont = cont;
499 msg->transmit_cont_cls = cont_cls;
500 memcpy (msg->buf, msgbuf, msgbuf_size);
501 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
503 GNUNET_asprintf (&stat_txt,
504 "# bytes currently in %s_client buffers",
506 GNUNET_STATISTICS_update (plugin->env->stats,
507 stat_txt, msgbuf_size, GNUNET_NO);
508 GNUNET_free (stat_txt);
510 if (GNUNET_YES == s->put_tmp_disconnecting)
512 /* PUT connection is currently getting disconnected */
513 s->put_reconnect_required = GNUNET_YES;
514 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
515 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
519 else if (GNUNET_YES == s->put_paused)
521 /* PUT connection was paused, unpause */
522 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
523 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
524 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
525 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
526 "Session %p/connection %p: unpausing connection\n",
528 s->put_paused = GNUNET_NO;
529 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
531 else if (GNUNET_YES == s->put_tmp_disconnected)
533 /* PUT connection was disconnected, reconnect */
534 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
535 "Session %p: Reconnecting PUT connection\n",
537 s->put_tmp_disconnected = GNUNET_NO;
538 GNUNET_break (s->client_put == NULL);
539 if (GNUNET_SYSERR == client_connect_put (s))
541 return GNUNET_SYSERR;
545 client_schedule (s->plugin, GNUNET_YES);
553 * @param s the session to delete
556 client_delete_session (struct Session *s)
558 struct HTTP_Client_Plugin *plugin = s->plugin;
559 struct HTTP_Message *pos;
560 struct HTTP_Message *next;
562 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
564 GNUNET_SCHEDULER_cancel (s->timeout_task);
565 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
567 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
569 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
570 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
573 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
576 while (NULL != (pos = next))
579 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
580 if (pos->transmit_cont != NULL)
581 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
582 pos->size, pos->pos + s->overhead);
587 if (s->msg_tk != NULL)
589 GNUNET_SERVER_mst_destroy (s->msg_tk);
592 GNUNET_free (s->addr);
593 GNUNET_free (s->url);
599 * Disconnect a session
601 * @param cls the `struct HTTP_Client_Plugin`
603 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
606 http_client_session_disconnect (void *cls,
609 struct HTTP_Client_Plugin *plugin = cls;
610 struct HTTP_Message *msg;
611 struct HTTP_Message *t;
615 if (GNUNET_YES != client_exist_session (plugin, s))
618 return GNUNET_SYSERR;
621 if (s->client_put != NULL)
623 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
624 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
625 s, s->client_put, GNUNET_i2s (&s->target));
627 /* remove curl handle from multi handle */
628 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
629 if (mret != CURLM_OK)
631 /* clean up easy handle, handle is now invalid and free'd */
635 curl_easy_cleanup (s->client_put);
636 s->client_put = NULL;
640 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
642 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
643 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
646 if (s->client_get != NULL)
648 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
649 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
650 s, s->client_get, GNUNET_i2s (&s->target));
652 /* remove curl handle from multi handle */
653 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
654 if (mret != CURLM_OK)
656 /* clean up easy handle, handle is now invalid and free'd */
660 curl_easy_cleanup (s->client_get);
661 s->client_get = NULL;
668 if (NULL != msg->transmit_cont)
669 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
670 msg->size, msg->pos + s->overhead);
672 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
677 GNUNET_assert (plugin->cur_connections >= 2);
678 plugin->cur_connections -= 2;
679 GNUNET_STATISTICS_set (plugin->env->stats,
680 HTTP_STAT_STR_CONNECTIONS,
681 plugin->cur_connections,
683 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
684 "Session %p: notifying transport about ending session\n",s);
686 plugin->env->session_end (plugin->env->cls, &s->target, s);
687 client_delete_session (s);
689 /* Re-schedule since handles have changed */
690 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
692 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
693 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
695 client_schedule (plugin, GNUNET_YES);
702 * Function that is called to get the keepalive factor.
703 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
704 * calculate the interval between keepalive packets.
706 * @param cls closure with the `struct Plugin`
707 * @return keepalive factor
710 http_client_query_keepalive_factor (void *cls)
717 * Function that can be used to force the plugin to disconnect
718 * from the given peer and cancel all previous transmissions
719 * (and their continuationc).
722 * @param target peer from which to disconnect
725 http_client_peer_disconnect (void *cls,
726 const struct GNUNET_PeerIdentity *target)
728 struct HTTP_Client_Plugin *plugin = cls;
729 struct Session *next = NULL;
730 struct Session *pos = NULL;
732 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
733 "Transport tells me to disconnect `%s'\n",
734 GNUNET_i2s (target));
737 while (NULL != (pos = next))
740 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
742 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
743 "Disconnecting session %p to `%pos'\n",
744 pos, GNUNET_i2s (target));
745 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (plugin,
753 * Check if a sessions exists for an specific address
755 * @param plugin the plugin
756 * @param address the address
757 * @return the session or NULL
759 static struct Session *
760 client_lookup_session (struct HTTP_Client_Plugin *plugin,
761 const struct GNUNET_HELLO_Address *address)
765 for (pos = plugin->head; NULL != pos; pos = pos->next)
766 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
767 (address->address_length == pos->addrlen) &&
768 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
775 client_put_disconnect (void *cls,
776 const struct GNUNET_SCHEDULER_TaskContext *tc)
778 struct Session *s = cls;
780 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
781 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
782 "Session %p/connection %p: will be disconnected due to no activity\n",
784 s->put_paused = GNUNET_NO;
785 s->put_tmp_disconnecting = GNUNET_YES;
786 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
787 client_schedule (s->plugin, GNUNET_YES);
792 * Callback method used with libcurl
793 * Method is called when libcurl needs to read data during sending
795 * @param stream pointer where to write data
796 * @param size size of an individual element
797 * @param nmemb count of elements that can be written to the buffer
798 * @param cls our `struct Session`
799 * @return bytes written to stream, returning 0 will terminate connection!
802 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
804 struct Session *s = cls;
805 struct HTTP_Client_Plugin *plugin = s->plugin;
806 struct HTTP_Message *msg = s->msg_head;
810 if (GNUNET_YES != client_exist_session (plugin, s))
815 if (GNUNET_YES == s->put_tmp_disconnecting)
818 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
819 "Session %p/connection %p: disconnect due to inactivity\n",
826 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
827 "Session %p/connection %p: nothing to send, suspending\n",
829 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
830 s->put_paused = GNUNET_YES;
831 return CURL_READFUNC_PAUSE;
834 GNUNET_assert (msg->pos < msg->size);
835 /* calculate how much fits in buffer */
836 len = GNUNET_MIN (msg->size - msg->pos,
838 memcpy (stream, &msg->buf[msg->pos], len);
840 if (msg->pos == msg->size)
842 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
843 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
844 s, s->client_put, msg->size, msg->pos);
845 /* Calling transmit continuation */
846 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
847 if (NULL != msg->transmit_cont)
848 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
849 msg->size, msg->size + s->overhead);
854 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
855 GNUNET_STATISTICS_update (plugin->env->stats,
856 stat_txt, -len, GNUNET_NO);
857 GNUNET_free (stat_txt);
859 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
860 GNUNET_STATISTICS_update (plugin->env->stats,
861 stat_txt, len, GNUNET_NO);
862 GNUNET_free (stat_txt);
868 * Wake up a curl handle which was suspended
870 * @param cls the session
871 * @param tc task context
874 client_wake_up (void *cls,
875 const struct GNUNET_SCHEDULER_TaskContext *tc)
877 struct Session *s = cls;
878 struct HTTP_Client_Plugin *p = s->plugin;
880 if (GNUNET_YES != client_exist_session (p, s))
885 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
886 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
888 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
889 "Session %p/connection %p: Waking up GET handle\n",
892 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;
916 if (GNUNET_YES != client_exist_session (plugin, 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,
936 "# bytes received via %s_client",
938 GNUNET_STATISTICS_update (plugin->env->stats,
939 stat_txt, ntohs(message->size), GNUNET_NO);
940 GNUNET_free (stat_txt);
943 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
945 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
947 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
948 "Client: peer `%s' address `%s' next read delayed for %s\n",
949 GNUNET_i2s (&s->target),
950 http_common_plugin_address_to_string (NULL,
952 s->addr, s->addrlen),
953 GNUNET_STRINGS_relative_time_to_string (delay,
956 client_reschedule_session_timeout (s);
962 * Callback method used with libcurl when data for a PUT connection are
963 * received. We do not expect data here, so we just dismiss it
965 * @param stream pointer where to write data
966 * @param size size of an individual element
967 * @param nmemb count of elements that can be written to the buffer
968 * @param cls destination pointer, passed to the libcurl handle
969 * @return bytes read from stream
972 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
979 * Callback method used with libcurl when data for a GET connection are
980 * received. Forward to MST
982 * @param stream pointer where to write data
983 * @param size size of an individual element
984 * @param nmemb count of elements that can be written to the buffer
985 * @param cls destination pointer, passed to the libcurl handle
986 * @return bytes read from stream
989 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
991 struct Session *s = cls;
992 struct GNUNET_TIME_Absolute now;
993 size_t len = size * nmemb;
995 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
996 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
998 len, GNUNET_i2s (&s->target));
999 now = GNUNET_TIME_absolute_get ();
1000 if (now.abs_value_us < s->next_receive.abs_value_us)
1002 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1003 struct GNUNET_TIME_Relative delta =
1004 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1005 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1006 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1008 GNUNET_STRINGS_relative_time_to_string (delta,
1010 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1012 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1013 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1015 s->recv_wakeup_task =
1016 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1017 return CURL_WRITEFUNC_PAUSE;
1019 if (NULL == s->msg_tk)
1020 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1021 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1027 * Task performing curl operations
1029 * @param cls plugin as closure
1030 * @param tc gnunet scheduler task context
1033 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1037 * Function setting up file descriptors and scheduling task to run
1039 * @param plugin the plugin as closure
1040 * @param now schedule task in 1ms, regardless of what curl may say
1041 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1044 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1050 struct GNUNET_NETWORK_FDSet *grs;
1051 struct GNUNET_NETWORK_FDSet *gws;
1054 struct GNUNET_TIME_Relative timeout;
1056 /* Cancel previous scheduled task */
1057 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1059 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1060 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1066 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1067 if (mret != CURLM_OK)
1069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1070 "curl_multi_fdset", __FILE__, __LINE__,
1071 curl_multi_strerror (mret));
1072 return GNUNET_SYSERR;
1074 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1076 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1078 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1079 if (now == GNUNET_YES)
1080 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1082 if (mret != CURLM_OK)
1084 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1085 "curl_multi_timeout", __FILE__, __LINE__,
1086 curl_multi_strerror (mret));
1087 return GNUNET_SYSERR;
1090 grs = GNUNET_NETWORK_fdset_create ();
1091 gws = GNUNET_NETWORK_fdset_create ();
1092 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1093 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1095 plugin->client_perform_task =
1096 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1098 &client_run, plugin);
1099 GNUNET_NETWORK_fdset_destroy (gws);
1100 GNUNET_NETWORK_fdset_destroy (grs);
1106 * Task performing curl operations
1108 * @param cls plugin as closure
1109 * @param tc gnunet scheduler task context
1112 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1114 struct HTTP_Client_Plugin *plugin = cls;
1116 long http_statuscode;
1119 GNUNET_assert (cls != NULL);
1121 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1122 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1128 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1133 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1135 CURL *easy_h = msg->easy_handle;
1136 struct Session *s = NULL;
1137 char *d = (char *) s;
1142 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1143 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1145 curl_easy_strerror (msg->data.result), running);
1149 GNUNET_assert (CURLE_OK ==
1150 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1151 s = (struct Session *) d;
1153 if (GNUNET_YES != client_exist_session(plugin, s))
1159 GNUNET_assert (s != NULL);
1160 if (msg->msg == CURLMSG_DONE)
1162 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1163 if (easy_h == s->client_put)
1165 if ((0 != msg->data.result) || (http_statuscode != 200))
1167 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1168 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1169 s, msg->easy_handle, GNUNET_i2s (&s->target),
1172 curl_easy_strerror (msg->data.result));
1175 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1176 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1177 s, msg->easy_handle, GNUNET_i2s (&s->target));
1178 if (s->client_get == NULL)
1180 /* Disconnect other transmission direction and tell transport */
1182 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1183 curl_easy_cleanup (easy_h);
1184 s->put_tmp_disconnecting = GNUNET_NO;
1185 s->put_tmp_disconnected = GNUNET_YES;
1186 s->client_put = NULL;
1187 s->put.easyhandle = NULL;
1191 * Handling a rare case:
1192 * plugin_send was called during temporary put disconnect,
1193 * reconnect required after connection was disconnected
1195 if (GNUNET_YES == s->put_reconnect_required)
1197 s->put_reconnect_required = GNUNET_NO;
1198 if (GNUNET_SYSERR == client_connect_put(s))
1200 GNUNET_break (s->client_put == NULL);
1201 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1205 if (easy_h == s->client_get)
1207 if ((0 != msg->data.result) || (http_statuscode != 200))
1209 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1210 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1211 s, msg->easy_handle, GNUNET_i2s (&s->target),
1214 curl_easy_strerror (msg->data.result));
1218 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1219 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1220 s, msg->easy_handle, GNUNET_i2s (&s->target));
1221 /* Disconnect other transmission direction and tell transport */
1222 s->get.easyhandle = NULL;
1224 http_client_session_disconnect (plugin, s);
1229 while (mret == CURLM_CALL_MULTI_PERFORM);
1230 client_schedule (plugin, GNUNET_NO);
1235 * Connect GET connection for a session
1237 * @param s the session to connect
1238 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1241 client_connect_get (struct Session *s)
1245 /* create get connection */
1246 s->client_get = curl_easy_init ();
1248 s->get.easyhandle = s->client_get;
1250 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1251 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1252 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1255 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1256 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1257 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1259 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1260 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1264 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1265 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1267 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1268 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1270 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1271 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1274 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1275 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1276 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1277 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1278 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1279 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1280 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1281 /* No timeout by default, timeout done with session timeout */
1282 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1283 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1284 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1285 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1286 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1287 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1288 #if CURL_TCP_NODELAY
1289 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1291 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1293 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1294 if (mret != CURLM_OK)
1296 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1297 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1299 curl_multi_strerror (mret));
1300 curl_easy_cleanup (s->client_get);
1301 s->client_get = NULL;
1303 s->get.easyhandle = NULL;
1305 return GNUNET_SYSERR;
1313 * Connect a HTTP put connection
1315 * @param s the session to connect
1316 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1319 client_connect_put (struct Session *s)
1323 /* create put connection */
1324 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1325 "Session %p : Init PUT handle\n", s);
1326 s->client_put = curl_easy_init ();
1328 s->put.easyhandle = s->client_put;
1330 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1331 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1332 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1335 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1336 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1337 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1339 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1340 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1344 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1345 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1347 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1348 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1350 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1351 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1353 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1354 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1355 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1356 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1357 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1358 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1359 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1360 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1361 /* No timeout by default, timeout done with session timeout */
1362 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1363 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1364 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1365 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1366 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1367 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1368 #if CURL_TCP_NODELAY
1369 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1371 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1372 if (mret != CURLM_OK)
1374 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1375 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1377 curl_multi_strerror (mret));
1378 curl_easy_cleanup (s->client_put);
1379 s->client_put = NULL;
1380 s->put.easyhandle = NULL;
1382 s->put_tmp_disconnected = GNUNET_YES;
1383 return GNUNET_SYSERR;
1385 s->put_tmp_disconnected = GNUNET_NO;
1391 * Connect both PUT and GET connection for a session
1393 * @param s the session to connect
1394 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1397 client_connect (struct Session *s)
1400 struct HTTP_Client_Plugin *plugin = s->plugin;
1401 int res = GNUNET_OK;
1404 if (NULL == http_common_plugin_address_to_string (NULL,
1406 s->addr, s->addrlen))
1408 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1410 "Invalid address peer `%s'\n",
1411 GNUNET_i2s (&s->target));
1412 return GNUNET_SYSERR;
1415 GNUNET_asprintf (&s->url, "%s/%s;%u",
1416 http_common_plugin_address_to_url (NULL, s->addr, s->addrlen),
1417 GNUNET_i2s_full (plugin->env->my_identity),
1422 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1423 "Initiating outbound session peer `%s' using address `%s'\n",
1424 GNUNET_i2s (&s->target), s->url);
1426 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1427 (GNUNET_SYSERR == client_connect_put (s)))
1430 return GNUNET_SYSERR;
1433 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1435 "Session %p: connected with connections GET %p and PUT %p\n",
1436 s, s->client_get, s->client_put);
1438 /* Perform connect */
1439 plugin->cur_connections += 2;
1440 GNUNET_STATISTICS_set (plugin->env->stats,
1441 HTTP_STAT_STR_CONNECTIONS,
1442 plugin->cur_connections,
1445 /* Re-schedule since handles have changed */
1446 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1448 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1449 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1451 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1457 * Function obtain the network type for a session
1459 * @param cls closure (`struct Plugin*`)
1460 * @param session the session
1461 * @return the network type
1463 static enum GNUNET_ATS_Network_Type
1464 http_client_get_network (void *cls,
1465 struct Session *session)
1467 return ntohl (session->ats_address_network_type);
1472 * Session was idle, so disconnect it
1474 * @param cls the `struct Session` of the idle session
1475 * @param tc scheduler context
1478 client_session_timeout (void *cls,
1479 const struct GNUNET_SCHEDULER_TaskContext *tc)
1481 struct Session *s = cls;
1483 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1484 GNUNET_log (TIMEOUT_LOG,
1485 "Session %p was idle for %s, disconnecting\n",
1487 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1490 /* call session destroy function */
1491 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (s->plugin,
1497 * Creates a new outbound session the transport service will use to
1498 * send data to the peer
1500 * @param cls the plugin
1501 * @param address the address
1502 * @return the session or NULL of max connections exceeded
1504 static struct Session *
1505 http_client_plugin_get_session (void *cls,
1506 const struct GNUNET_HELLO_Address *address)
1508 struct HTTP_Client_Plugin *plugin = cls;
1509 struct Session * s = NULL;
1510 struct sockaddr *sa;
1511 struct GNUNET_ATS_Information ats;
1515 GNUNET_assert (address->address != NULL);
1517 /* find existing session */
1518 s = client_lookup_session (plugin, address);
1522 if (plugin->max_connections <= plugin->cur_connections)
1524 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1525 "Maximum number of connections (%u) reached: "
1526 "cannot connect to peer `%s'\n",
1527 plugin->max_connections,
1528 GNUNET_i2s (&address->peer));
1532 /* Determine network location */
1533 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1534 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1535 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1536 if (GNUNET_SYSERR == res)
1540 else if (GNUNET_YES == res)
1542 GNUNET_assert (NULL != sa);
1543 if (AF_INET == sa->sa_family)
1545 salen = sizeof (struct sockaddr_in);
1547 else if (AF_INET6 == sa->sa_family)
1549 salen = sizeof (struct sockaddr_in6);
1551 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1552 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1555 else if (GNUNET_NO == res)
1557 /* Cannot convert to sockaddr -> is external hostname */
1558 ats.value = htonl (GNUNET_ATS_NET_WAN);
1560 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1566 s = GNUNET_new (struct Session);
1567 s->target = address->peer;
1569 s->addr = GNUNET_malloc (address->address_length);
1570 memcpy (s->addr, address->address, address->address_length);
1571 s->addrlen = address->address_length;
1572 s->ats_address_network_type = ats.value;
1573 s->put_paused = GNUNET_NO;
1574 s->put_tmp_disconnecting = GNUNET_NO;
1575 s->put_tmp_disconnected = GNUNET_NO;
1576 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1577 &client_session_timeout,
1579 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1580 "Created new session %p for `%s' address `%s''\n",
1582 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1583 GNUNET_i2s (&s->target));
1585 /* add new session */
1586 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1588 /* initiate new connection */
1589 if (GNUNET_SYSERR == client_connect (s))
1591 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1592 "Cannot connect to peer `%s' address `%s''\n",
1593 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1594 GNUNET_i2s (&s->target));
1595 client_delete_session (s);
1603 * Setup http_client plugin
1605 * @param plugin the plugin handle
1606 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1609 client_start (struct HTTP_Client_Plugin *plugin)
1611 curl_global_init (CURL_GLOBAL_ALL);
1612 plugin->curl_multi_handle = curl_multi_init ();
1614 if (NULL == plugin->curl_multi_handle)
1616 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1617 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1619 return GNUNET_SYSERR;
1626 * Increment session timeout due to activity for session s
1628 * param s the session
1631 client_reschedule_session_timeout (struct Session *s)
1633 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1634 GNUNET_SCHEDULER_cancel (s->timeout_task);
1635 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1636 &client_session_timeout,
1638 GNUNET_log (TIMEOUT_LOG,
1639 "Timeout rescheduled for session %p set to %s\n",
1641 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1647 * Another peer has suggested an address for this
1648 * peer and transport plugin. Check that this could be a valid
1649 * address. If so, consider adding it to the list
1652 * @param cls closure with the `struct Plugin`
1653 * @param addr pointer to the address
1654 * @param addrlen length of @a addr
1655 * @return #GNUNET_OK if this is a plausible address for this peer
1656 * and transport; always returns #GNUNET_NO (this is the client!)
1659 http_client_plugin_address_suggested (void *cls,
1663 /* struct Plugin *plugin = cls; */
1665 /* A HTTP/S client does not have any valid address so:*/
1671 * Exit point from the plugin.
1673 * @param cls api as closure
1677 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1679 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1680 struct HTTP_Client_Plugin *plugin = api->cls;
1681 struct Session *pos;
1682 struct Session *next;
1684 if (NULL == api->cls)
1691 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1692 _("Shutting down plugin `%s'\n"),
1696 next = plugin->head;
1697 while (NULL != (pos = next))
1700 http_client_session_disconnect (plugin, pos);
1702 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1704 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1705 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1709 if (NULL != plugin->curl_multi_handle)
1711 curl_multi_cleanup (plugin->curl_multi_handle);
1712 plugin->curl_multi_handle = NULL;
1714 curl_global_cleanup ();
1716 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1717 _("Shutdown for plugin `%s' complete\n"),
1720 GNUNET_free (plugin);
1729 * @param plugin the plugin handle
1730 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1733 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1735 unsigned long long max_connections;
1737 /* Optional parameters */
1738 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1740 "MAX_CONNECTIONS", &max_connections))
1741 max_connections = 128;
1742 plugin->max_connections = max_connections;
1744 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1745 _("Maximum number of connections is %u\n"),
1746 plugin->max_connections);
1752 http_plugin_address_to_string (void *cls,
1756 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1760 http_client_plugin_update_session_timeout (void *cls,
1761 const struct GNUNET_PeerIdentity *peer,
1762 struct Session *session)
1764 struct HTTP_Client_Plugin *plugin = cls;
1766 /* lookup if session is really existing */
1767 if (GNUNET_YES != client_exist_session (plugin, session))
1770 client_reschedule_session_timeout (session);
1774 * Entry point for the plugin.
1777 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1779 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1780 struct GNUNET_TRANSPORT_PluginFunctions *api;
1781 struct HTTP_Client_Plugin *plugin;
1783 if (NULL == env->receive)
1785 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1786 initialze the plugin or the API */
1787 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1789 api->address_to_string = &http_plugin_address_to_string;
1790 api->string_to_address = &http_common_plugin_string_to_address;
1791 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1795 plugin = GNUNET_new (struct HTTP_Client_Plugin);
1797 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1799 api->send = &http_client_plugin_send;
1800 api->disconnect_session = &http_client_session_disconnect;
1801 api->query_keepalive_factor = &http_client_query_keepalive_factor;
1802 api->disconnect_peer = &http_client_peer_disconnect;
1803 api->check_address = &http_client_plugin_address_suggested;
1804 api->get_session = &http_client_plugin_get_session;
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;
1808 api->get_network = &http_client_get_network;
1809 api->update_session_timeout = &http_client_plugin_update_session_timeout;
1812 plugin->name = "transport-https_client";
1813 plugin->protocol = "https";
1815 plugin->name = "transport-http_client";
1816 plugin->protocol = "http";
1818 plugin->last_tag = 1;
1819 plugin->options = 0; /* Setup options */
1821 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1823 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1828 if (GNUNET_SYSERR == client_start (plugin))
1830 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1836 /* end of plugin_transport_http_client.c */