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 GNUNET_HELLO_Address *address;
161 * ATS network type in NBO
163 uint32_t ats_address_network_type;
166 * Pointer to the global plugin struct.
168 struct HTTP_Client_Plugin *plugin;
175 struct ConnectionHandle put;
176 struct ConnectionHandle get;
179 * Is the client PUT handle currently paused
184 * Is the client PUT handle disconnect in progress?
186 int put_tmp_disconnecting;
189 * Is the client PUT handle temporarily disconnected?
191 int put_tmp_disconnected;
194 * We received data to send while disconnecting, reconnect immediately
196 int put_reconnect_required;
199 * Client receive handle
204 * Outbound overhead due to HTTP connection
205 * Add to next message of this session when calling callback
210 * next pointer for double linked list
212 struct HTTP_Message *msg_head;
215 * previous pointer for double linked list
217 struct HTTP_Message *msg_tail;
220 * Message stream tokenizer for incoming data
222 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
225 * Session timeout task
227 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
230 * Session timeout task
232 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
235 * Task to wake up client receive handle when receiving is allowed again
237 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
240 * Absolute time when to receive data again
241 * Used for receive throttling
243 struct GNUNET_TIME_Absolute next_receive;
248 * Encapsulation of all of the state of the plugin.
250 struct HTTP_Client_Plugin
255 struct GNUNET_TRANSPORT_PluginEnvironment *env;
258 * Linked list head of open sessions.
260 struct Session *head;
263 * Linked list tail of open sessions.
265 struct Session *tail;
278 * My options to be included in the address
283 * Maximum number of sockets the plugin can use
284 * Each http inbound /outbound connections are two connections
286 unsigned int max_connections;
289 * Current number of sockets the plugin can use
290 * Each http inbound /outbound connections are two connections
292 unsigned int cur_connections;
295 * Last used unique HTTP connection tag
312 CURLM *curl_multi_handle;
317 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
322 * Increment session timeout due to activity for a session
323 * @param s the session
326 client_reschedule_session_timeout (struct Session *s);
330 * Function setting up file descriptors and scheduling task to run
332 * @param plugin plugin as closure
333 * @param now schedule task in 1ms, regardless of what curl may say
334 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
337 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
341 * Connect a HTTP put connection
343 * @param s the session to connect
344 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
347 client_connect_put (struct Session *s);
351 * Does a session s exists?
353 * @param plugin the plugin
354 * @param s desired session
355 * @return #GNUNET_YES or #GNUNET_NO
358 client_exist_session (struct HTTP_Client_Plugin *plugin,
361 struct Session * head;
363 for (head = plugin->head; head != NULL; head = head->next)
373 * @param curl the curl easy handle
374 * @param type message type
375 * @param data data to log, NOT a 0-terminated string
376 * @param size data length
377 * @param cls the closure
381 client_log (CURL *curl, curl_infotype type,
382 const char *data, size_t size, void *cls)
384 struct ConnectionHandle *ch = cls;
385 const char *ttype = "UNSPECIFIED";
387 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
395 case CURLINFO_HEADER_IN:
398 case CURLINFO_HEADER_OUT:
399 ttype = "HEADER_OUT";
402 GNUNET_assert (NULL != ch);
403 GNUNET_assert (NULL != ch->easyhandle);
404 GNUNET_assert (NULL != ch->s);
405 ch->s->overhead += size;
408 ttype = "UNSPECIFIED";
412 memcpy (text, data, size);
413 if (text[size - 1] == '\n')
418 text[size + 1] = '\0';
421 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
422 "Connection %p %s: %s", ch->easyhandle, ttype, text);
424 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
425 "Connection %p %s: %s", ch->easyhandle, ttype, text);
433 * Function that can be used by the transport service to transmit
434 * a message using the plugin. Note that in the case of a
435 * peer disconnecting, the continuation MUST be called
436 * prior to the disconnect notification itself. This function
437 * will be called with this peer's HELLO message to initiate
438 * a fresh connection to another peer.
441 * @param s which session must be used
442 * @param msgbuf the message to transmit
443 * @param msgbuf_size number of bytes in 'msgbuf'
444 * @param priority how important is the message (most plugins will
445 * ignore message priority and just FIFO)
446 * @param to how long to wait at most for the transmission (does not
447 * require plugins to discard the message after the timeout,
448 * just advisory for the desired delay; most plugins will ignore
450 * @param cont continuation to call once the message has
451 * been transmitted (or if the transport is ready
452 * for the next transmission call; or if the
453 * peer disconnected...); can be NULL
454 * @param cont_cls closure for cont
455 * @return number of bytes used (on the physical network, with overheads);
456 * -1 on hard errors (i.e. address invalid); 0 is a legal value
457 * and does NOT mean that the message was not transmitted (DV)
460 http_client_plugin_send (void *cls,
462 const char *msgbuf, size_t msgbuf_size,
463 unsigned int priority,
464 struct GNUNET_TIME_Relative to,
465 GNUNET_TRANSPORT_TransmitContinuation cont,
468 struct HTTP_Client_Plugin *plugin = cls;
469 struct HTTP_Message *msg;
472 /* lookup if session is really existing */
473 if (GNUNET_YES != client_exist_session (plugin, s))
476 return GNUNET_SYSERR;
479 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
480 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
482 msgbuf_size, GNUNET_i2s (&s->target));
484 /* create new message and schedule */
485 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
487 msg->size = msgbuf_size;
489 msg->buf = (char *) &msg[1];
490 msg->transmit_cont = cont;
491 msg->transmit_cont_cls = cont_cls;
492 memcpy (msg->buf, msgbuf, msgbuf_size);
493 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
495 GNUNET_asprintf (&stat_txt,
496 "# bytes currently in %s_client buffers",
498 GNUNET_STATISTICS_update (plugin->env->stats,
499 stat_txt, msgbuf_size, GNUNET_NO);
500 GNUNET_free (stat_txt);
502 if (GNUNET_YES == s->put_tmp_disconnecting)
504 /* PUT connection is currently getting disconnected */
505 s->put_reconnect_required = GNUNET_YES;
506 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
507 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
511 else if (GNUNET_YES == s->put_paused)
513 /* PUT connection was paused, unpause */
514 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
515 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
516 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
517 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
518 "Session %p/connection %p: unpausing connection\n",
520 s->put_paused = GNUNET_NO;
521 if (NULL != s->client_put)
522 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
524 else if (GNUNET_YES == s->put_tmp_disconnected)
526 /* PUT connection was disconnected, reconnect */
527 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
528 "Session %p: Reconnecting PUT connection\n",
530 s->put_tmp_disconnected = GNUNET_NO;
531 GNUNET_break (s->client_put == NULL);
532 if (GNUNET_SYSERR == client_connect_put (s))
534 return GNUNET_SYSERR;
538 client_schedule (s->plugin, GNUNET_YES);
546 * @param s the session to delete
549 client_delete_session (struct Session *s)
551 struct HTTP_Client_Plugin *plugin = s->plugin;
552 struct HTTP_Message *pos;
553 struct HTTP_Message *next;
555 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
557 GNUNET_SCHEDULER_cancel (s->timeout_task);
558 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
560 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
562 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
563 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
566 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
569 while (NULL != (pos = next))
572 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
573 if (pos->transmit_cont != NULL)
574 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
575 pos->size, pos->pos + s->overhead);
580 if (s->msg_tk != NULL)
582 GNUNET_SERVER_mst_destroy (s->msg_tk);
585 GNUNET_HELLO_address_free (s->address);
586 GNUNET_free (s->url);
592 * Disconnect a session
594 * @param cls the `struct HTTP_Client_Plugin`
596 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
599 http_client_session_disconnect (void *cls,
602 struct HTTP_Client_Plugin *plugin = cls;
603 struct HTTP_Message *msg;
604 struct HTTP_Message *t;
608 if (GNUNET_YES != client_exist_session (plugin, s))
611 return GNUNET_SYSERR;
614 if (NULL != s->client_put)
616 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
617 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
618 s, s->client_put, GNUNET_i2s (&s->target));
620 /* remove curl handle from multi handle */
621 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
622 if (mret != CURLM_OK)
624 /* clean up easy handle, handle is now invalid and free'd */
628 curl_easy_cleanup (s->client_put);
629 s->client_put = NULL;
633 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
635 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
636 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
639 if (NULL != s->client_get)
641 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
642 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
644 GNUNET_i2s (&s->target));
645 /* remove curl handle from multi handle */
646 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
647 if (mret != CURLM_OK)
649 /* clean up easy handle, handle is now invalid and free'd */
653 curl_easy_cleanup (s->client_get);
654 s->client_get = NULL;
661 if (NULL != msg->transmit_cont)
662 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
663 msg->size, msg->pos + s->overhead);
665 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
670 GNUNET_assert (plugin->cur_connections >= 2);
671 plugin->cur_connections -= 2;
672 GNUNET_STATISTICS_set (plugin->env->stats,
673 HTTP_STAT_STR_CONNECTIONS,
674 plugin->cur_connections,
676 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
677 "Session %p: notifying transport about ending session\n",s);
679 plugin->env->session_end (plugin->env->cls, s->address, s);
680 client_delete_session (s);
682 /* Re-schedule since handles have changed */
683 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
685 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
686 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
688 client_schedule (plugin, GNUNET_YES);
695 * Function that is called to get the keepalive factor.
696 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
697 * calculate the interval between keepalive packets.
699 * @param cls closure with the `struct Plugin`
700 * @return keepalive factor
703 http_client_query_keepalive_factor (void *cls)
710 * Function that can be used to force the plugin to disconnect
711 * from the given peer and cancel all previous transmissions
712 * (and their continuationc).
715 * @param target peer from which to disconnect
718 http_client_peer_disconnect (void *cls,
719 const struct GNUNET_PeerIdentity *target)
721 struct HTTP_Client_Plugin *plugin = cls;
722 struct Session *next = NULL;
723 struct Session *pos = NULL;
725 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
726 "Transport tells me to disconnect `%s'\n",
727 GNUNET_i2s (target));
730 while (NULL != (pos = next))
733 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
735 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
736 "Disconnecting session %p to `%pos'\n",
737 pos, GNUNET_i2s (target));
738 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (plugin,
746 * Check if a sessions exists for an specific address
748 * @param plugin the plugin
749 * @param address the address
750 * @return the session or NULL
752 static struct Session *
753 client_lookup_session (struct HTTP_Client_Plugin *plugin,
754 const struct GNUNET_HELLO_Address *address)
758 for (pos = plugin->head; NULL != pos; pos = pos->next)
760 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
761 (0 == GNUNET_HELLO_address_cmp(address, pos->address)))
769 client_put_disconnect (void *cls,
770 const struct GNUNET_SCHEDULER_TaskContext *tc)
772 struct Session *s = cls;
774 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
775 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
776 "Session %p/connection %p: will be disconnected due to no activity\n",
778 s->put_paused = GNUNET_NO;
779 s->put_tmp_disconnecting = GNUNET_YES;
780 if (NULL != s->client_put)
781 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
782 client_schedule (s->plugin, GNUNET_YES);
787 * Callback method used with libcurl
788 * Method is called when libcurl needs to read data during sending
790 * @param stream pointer where to write data
791 * @param size size of an individual element
792 * @param nmemb count of elements that can be written to the buffer
793 * @param cls our `struct Session`
794 * @return bytes written to stream, returning 0 will terminate connection!
797 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
799 struct Session *s = cls;
800 struct HTTP_Client_Plugin *plugin = s->plugin;
801 struct HTTP_Message *msg = s->msg_head;
805 if (GNUNET_YES != client_exist_session (plugin, s))
810 if (GNUNET_YES == s->put_tmp_disconnecting)
813 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
814 "Session %p/connection %p: disconnect due to inactivity\n",
821 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
822 "Session %p/connection %p: nothing to send, suspending\n",
824 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
825 s->put_paused = GNUNET_YES;
826 return CURL_READFUNC_PAUSE;
829 GNUNET_assert (msg->pos < msg->size);
830 /* calculate how much fits in buffer */
831 len = GNUNET_MIN (msg->size - msg->pos,
833 memcpy (stream, &msg->buf[msg->pos], len);
835 if (msg->pos == msg->size)
837 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
838 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
839 s, s->client_put, msg->size, msg->pos);
840 /* Calling transmit continuation */
841 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
842 if (NULL != msg->transmit_cont)
843 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
844 msg->size, msg->size + s->overhead);
849 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
850 GNUNET_STATISTICS_update (plugin->env->stats,
851 stat_txt, -len, GNUNET_NO);
852 GNUNET_free (stat_txt);
854 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
855 GNUNET_STATISTICS_update (plugin->env->stats,
856 stat_txt, len, GNUNET_NO);
857 GNUNET_free (stat_txt);
863 * Wake up a curl handle which was suspended
865 * @param cls the session
866 * @param tc task context
869 client_wake_up (void *cls,
870 const struct GNUNET_SCHEDULER_TaskContext *tc)
872 struct Session *s = cls;
873 struct HTTP_Client_Plugin *p = s->plugin;
875 if (GNUNET_YES != client_exist_session (p, s))
880 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
881 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
883 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
884 "Session %p/connection %p: Waking up GET handle\n",
887 s->put_paused = GNUNET_NO;
888 if (NULL != s->client_get)
889 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
895 * Callback for message stream tokenizer
897 * @param cls the session
898 * @param client not used
899 * @param message the message received
900 * @return always #GNUNET_OK
903 client_receive_mst_cb (void *cls, void *client,
904 const struct GNUNET_MessageHeader *message)
906 struct Session *s = cls;
907 struct HTTP_Client_Plugin *plugin;
908 struct GNUNET_TIME_Relative delay;
909 struct GNUNET_ATS_Information atsi;
913 if (GNUNET_YES != client_exist_session (plugin, s))
919 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
920 atsi.value = s->ats_address_network_type;
921 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
923 delay = s->plugin->env->receive (plugin->env->cls, s->address, s, message);
924 plugin->env->update_address_metrics (plugin->env->cls,
928 GNUNET_asprintf (&stat_txt,
929 "# bytes received via %s_client",
931 GNUNET_STATISTICS_update (plugin->env->stats,
932 stat_txt, ntohs(message->size), GNUNET_NO);
933 GNUNET_free (stat_txt);
936 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
938 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
940 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
941 "Client: peer `%s' address `%s' next read delayed for %s\n",
942 GNUNET_i2s (&s->target),
943 http_common_plugin_address_to_string (NULL,
944 s->plugin->protocol, s->address->address,
945 s->address->address_length),
946 GNUNET_STRINGS_relative_time_to_string (delay,
949 client_reschedule_session_timeout (s);
955 * Callback method used with libcurl when data for a PUT connection are
956 * received. We do not expect data here, so we just dismiss it
958 * @param stream pointer where to write data
959 * @param size size of an individual element
960 * @param nmemb count of elements that can be written to the buffer
961 * @param cls destination pointer, passed to the libcurl handle
962 * @return bytes read from stream
965 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
972 * Callback method used with libcurl when data for a GET connection are
973 * received. Forward to MST
975 * @param stream pointer where to write data
976 * @param size size of an individual element
977 * @param nmemb count of elements that can be written to the buffer
978 * @param cls destination pointer, passed to the libcurl handle
979 * @return bytes read from stream
982 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
984 struct Session *s = cls;
985 struct GNUNET_TIME_Absolute now;
986 size_t len = size * nmemb;
988 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
989 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
991 len, GNUNET_i2s (&s->target));
992 now = GNUNET_TIME_absolute_get ();
993 if (now.abs_value_us < s->next_receive.abs_value_us)
995 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
996 struct GNUNET_TIME_Relative delta =
997 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
998 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
999 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1001 GNUNET_STRINGS_relative_time_to_string (delta,
1003 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1005 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1006 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1008 s->recv_wakeup_task =
1009 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1010 return CURL_WRITEFUNC_PAUSE;
1012 if (NULL == s->msg_tk)
1013 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1014 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1020 * Task performing curl operations
1022 * @param cls plugin as closure
1023 * @param tc gnunet scheduler task context
1026 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1030 * Function setting up file descriptors and scheduling task to run
1032 * @param plugin the plugin as closure
1033 * @param now schedule task in 1ms, regardless of what curl may say
1034 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1037 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1043 struct GNUNET_NETWORK_FDSet *grs;
1044 struct GNUNET_NETWORK_FDSet *gws;
1047 struct GNUNET_TIME_Relative timeout;
1049 /* Cancel previous scheduled task */
1050 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1052 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1053 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1059 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1060 if (mret != CURLM_OK)
1062 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1063 "curl_multi_fdset", __FILE__, __LINE__,
1064 curl_multi_strerror (mret));
1065 return GNUNET_SYSERR;
1067 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1069 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1071 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1072 if (now == GNUNET_YES)
1073 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1075 if (mret != CURLM_OK)
1077 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1078 "curl_multi_timeout", __FILE__, __LINE__,
1079 curl_multi_strerror (mret));
1080 return GNUNET_SYSERR;
1083 grs = GNUNET_NETWORK_fdset_create ();
1084 gws = GNUNET_NETWORK_fdset_create ();
1085 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1086 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1088 plugin->client_perform_task =
1089 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1091 &client_run, plugin);
1092 GNUNET_NETWORK_fdset_destroy (gws);
1093 GNUNET_NETWORK_fdset_destroy (grs);
1099 * Task performing curl operations
1101 * @param cls plugin as closure
1102 * @param tc gnunet scheduler task context
1105 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1107 struct HTTP_Client_Plugin *plugin = cls;
1109 long http_statuscode;
1112 GNUNET_assert (cls != NULL);
1114 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1115 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1121 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1126 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1128 CURL *easy_h = msg->easy_handle;
1129 struct Session *s = NULL;
1130 char *d = (char *) s;
1135 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1136 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1138 curl_easy_strerror (msg->data.result), running);
1142 GNUNET_assert (CURLE_OK ==
1143 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1144 s = (struct Session *) d;
1146 if (GNUNET_YES != client_exist_session(plugin, s))
1152 GNUNET_assert (s != NULL);
1153 if (msg->msg == CURLMSG_DONE)
1155 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1156 if (easy_h == s->client_put)
1158 if ((0 != msg->data.result) || (http_statuscode != 200))
1160 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1161 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1162 s, msg->easy_handle, GNUNET_i2s (&s->target),
1165 curl_easy_strerror (msg->data.result));
1168 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1169 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1170 s, msg->easy_handle, GNUNET_i2s (&s->target));
1171 if (s->client_get == NULL)
1173 /* Disconnect other transmission direction and tell transport */
1175 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1176 curl_easy_cleanup (easy_h);
1177 s->put_tmp_disconnecting = GNUNET_NO;
1178 s->put_tmp_disconnected = GNUNET_YES;
1179 s->client_put = NULL;
1180 s->put.easyhandle = NULL;
1184 * Handling a rare case:
1185 * plugin_send was called during temporary put disconnect,
1186 * reconnect required after connection was disconnected
1188 if (GNUNET_YES == s->put_reconnect_required)
1190 s->put_reconnect_required = GNUNET_NO;
1191 if (GNUNET_SYSERR == client_connect_put(s))
1193 GNUNET_break (s->client_put == NULL);
1194 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1198 if (easy_h == s->client_get)
1200 if ((0 != msg->data.result) || (http_statuscode != 200))
1202 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1203 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1204 s, msg->easy_handle, GNUNET_i2s (&s->target),
1207 curl_easy_strerror (msg->data.result));
1211 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1212 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1213 s, msg->easy_handle, GNUNET_i2s (&s->target));
1214 /* Disconnect other transmission direction and tell transport */
1215 s->get.easyhandle = NULL;
1217 http_client_session_disconnect (plugin, s);
1222 while (mret == CURLM_CALL_MULTI_PERFORM);
1223 client_schedule (plugin, GNUNET_NO);
1228 * Connect GET connection for a session
1230 * @param s the session to connect
1231 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1234 client_connect_get (struct Session *s)
1238 /* create get connection */
1239 s->client_get = curl_easy_init ();
1241 s->get.easyhandle = s->client_get;
1243 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1244 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1245 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1248 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1250 struct HttpAddress *ha;
1251 ha = (struct HttpAddress *) s->address->address;
1253 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1254 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1256 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1257 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1261 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1262 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",
1297 curl_multi_strerror (mret));
1298 curl_easy_cleanup (s->client_get);
1299 s->client_get = NULL;
1301 s->get.easyhandle = NULL;
1303 return GNUNET_SYSERR;
1311 * Connect a HTTP put connection
1313 * @param s the session to connect
1314 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1317 client_connect_put (struct Session *s)
1321 /* create put connection */
1322 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1323 "Session %p : Init PUT handle\n", s);
1324 s->client_put = curl_easy_init ();
1326 s->put.easyhandle = s->client_put;
1328 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1329 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1330 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1333 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1335 struct HttpAddress *ha;
1336 ha = (struct HttpAddress *) s->address->address;
1338 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1339 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1341 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1342 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1346 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1347 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1350 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1351 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1353 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1354 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1356 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1357 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1358 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1359 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1360 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1361 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1362 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1363 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1364 /* No timeout by default, timeout done with session timeout */
1365 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1366 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1367 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1368 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1369 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1370 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1371 #if CURL_TCP_NODELAY
1372 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1374 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1375 if (mret != CURLM_OK)
1377 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1378 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1380 curl_multi_strerror (mret));
1381 curl_easy_cleanup (s->client_put);
1382 s->client_put = NULL;
1383 s->put.easyhandle = NULL;
1385 s->put_tmp_disconnected = GNUNET_YES;
1386 return GNUNET_SYSERR;
1388 s->put_tmp_disconnected = GNUNET_NO;
1394 * Connect both PUT and GET connection for a session
1396 * @param s the session to connect
1397 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1400 client_connect (struct Session *s)
1403 struct HTTP_Client_Plugin *plugin = s->plugin;
1404 int res = GNUNET_OK;
1407 if (NULL == http_common_plugin_address_to_string (NULL,
1408 plugin->protocol, s->address->address, s->address->address_length))
1410 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1412 "Invalid address peer `%s'\n",
1413 GNUNET_i2s (&s->target));
1414 return GNUNET_SYSERR;
1417 GNUNET_asprintf (&s->url, "%s/%s;%u",
1418 http_common_plugin_address_to_url (NULL, s->address->address,
1419 s->address->address_length),
1420 GNUNET_i2s_full (plugin->env->my_identity),
1425 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1426 "Initiating outbound session peer `%s' using address `%s'\n",
1427 GNUNET_i2s (&s->target), s->url);
1429 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1430 (GNUNET_SYSERR == client_connect_put (s)))
1433 return GNUNET_SYSERR;
1436 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1438 "Session %p: connected with connections GET %p and PUT %p\n",
1439 s, s->client_get, s->client_put);
1441 /* Perform connect */
1442 plugin->cur_connections += 2;
1443 GNUNET_STATISTICS_set (plugin->env->stats,
1444 HTTP_STAT_STR_CONNECTIONS,
1445 plugin->cur_connections,
1448 /* Re-schedule since handles have changed */
1449 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1451 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1452 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1454 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1460 * Function obtain the network type for a session
1462 * @param cls closure (`struct Plugin*`)
1463 * @param session the session
1464 * @return the network type
1466 static enum GNUNET_ATS_Network_Type
1467 http_client_get_network (void *cls,
1468 struct Session *session)
1470 return ntohl (session->ats_address_network_type);
1475 * Session was idle, so disconnect it
1477 * @param cls the `struct Session` of the idle session
1478 * @param tc scheduler context
1481 client_session_timeout (void *cls,
1482 const struct GNUNET_SCHEDULER_TaskContext *tc)
1484 struct Session *s = cls;
1486 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1487 GNUNET_log (TIMEOUT_LOG,
1488 "Session %p was idle for %s, disconnecting\n",
1490 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1493 /* call session destroy function */
1494 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (s->plugin,
1500 * Creates a new outbound session the transport service will use to
1501 * send data to the peer
1503 * @param cls the plugin
1504 * @param address the address
1505 * @return the session or NULL of max connections exceeded
1507 static struct Session *
1508 http_client_plugin_get_session (void *cls,
1509 const struct GNUNET_HELLO_Address *address)
1511 struct HTTP_Client_Plugin *plugin = cls;
1512 struct Session * s = NULL;
1513 struct sockaddr *sa;
1514 struct GNUNET_ATS_Information ats;
1518 GNUNET_assert (address->address != NULL);
1520 /* find existing session */
1521 s = client_lookup_session (plugin, address);
1525 if (plugin->max_connections <= plugin->cur_connections)
1527 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1528 "Maximum number of connections (%u) reached: "
1529 "cannot connect to peer `%s'\n",
1530 plugin->max_connections,
1531 GNUNET_i2s (&address->peer));
1535 /* Determine network location */
1536 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1537 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1538 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1539 if (GNUNET_SYSERR == res)
1543 else if (GNUNET_YES == res)
1545 GNUNET_assert (NULL != sa);
1546 if (AF_INET == sa->sa_family)
1548 salen = sizeof (struct sockaddr_in);
1550 else if (AF_INET6 == sa->sa_family)
1552 salen = sizeof (struct sockaddr_in6);
1554 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1555 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1558 else if (GNUNET_NO == res)
1560 /* Cannot convert to sockaddr -> is external hostname */
1561 ats.value = htonl (GNUNET_ATS_NET_WAN);
1563 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1569 s = GNUNET_new (struct Session);
1570 s->target = address->peer;
1572 s->address = GNUNET_HELLO_address_copy (address);
1573 s->ats_address_network_type = ats.value;
1574 s->put_paused = GNUNET_NO;
1575 s->put_tmp_disconnecting = GNUNET_NO;
1576 s->put_tmp_disconnected = GNUNET_NO;
1577 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1578 &client_session_timeout,
1580 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1581 "Created new session %p for `%s' address `%s''\n",
1582 s, http_common_plugin_address_to_string (NULL,
1583 plugin->protocol, s->address->address,
1584 s->address->address_length),
1585 GNUNET_i2s (&s->target));
1587 /* add new session */
1588 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1590 /* initiate new connection */
1591 if (GNUNET_SYSERR == client_connect (s))
1593 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1594 "Cannot connect to peer `%s' address `%s''\n",
1595 http_common_plugin_address_to_string (NULL,
1596 plugin->protocol, s->address->address,
1597 s->address->address_length),
1598 GNUNET_i2s (&s->target));
1599 client_delete_session (s);
1607 * Setup http_client plugin
1609 * @param plugin the plugin handle
1610 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1613 client_start (struct HTTP_Client_Plugin *plugin)
1615 curl_global_init (CURL_GLOBAL_ALL);
1616 plugin->curl_multi_handle = curl_multi_init ();
1618 if (NULL == plugin->curl_multi_handle)
1620 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1621 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1623 return GNUNET_SYSERR;
1630 * Increment session timeout due to activity for session s
1632 * param s the session
1635 client_reschedule_session_timeout (struct Session *s)
1637 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1638 GNUNET_SCHEDULER_cancel (s->timeout_task);
1639 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1640 &client_session_timeout,
1642 GNUNET_log (TIMEOUT_LOG,
1643 "Timeout rescheduled for session %p set to %s\n",
1645 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1651 * Another peer has suggested an address for this
1652 * peer and transport plugin. Check that this could be a valid
1653 * address. If so, consider adding it to the list
1656 * @param cls closure with the `struct Plugin`
1657 * @param addr pointer to the address
1658 * @param addrlen length of @a addr
1659 * @return #GNUNET_OK if this is a plausible address for this peer
1660 * and transport; always returns #GNUNET_NO (this is the client!)
1663 http_client_plugin_address_suggested (void *cls,
1667 /* struct Plugin *plugin = cls; */
1669 /* A HTTP/S client does not have any valid address so:*/
1675 * Exit point from the plugin.
1677 * @param cls api as closure
1681 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1683 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1684 struct HTTP_Client_Plugin *plugin = api->cls;
1685 struct Session *pos;
1686 struct Session *next;
1688 if (NULL == api->cls)
1695 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1696 _("Shutting down plugin `%s'\n"),
1700 next = plugin->head;
1701 while (NULL != (pos = next))
1704 http_client_session_disconnect (plugin, pos);
1706 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1708 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1709 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1713 if (NULL != plugin->curl_multi_handle)
1715 curl_multi_cleanup (plugin->curl_multi_handle);
1716 plugin->curl_multi_handle = NULL;
1718 curl_global_cleanup ();
1720 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1721 _("Shutdown for plugin `%s' complete\n"),
1724 GNUNET_free (plugin);
1733 * @param plugin the plugin handle
1734 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1737 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1739 unsigned long long max_connections;
1741 /* Optional parameters */
1742 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1744 "MAX_CONNECTIONS", &max_connections))
1745 max_connections = 128;
1746 plugin->max_connections = max_connections;
1748 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1749 _("Maximum number of connections is %u\n"),
1750 plugin->max_connections);
1756 http_plugin_address_to_string (void *cls,
1760 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1765 http_client_plugin_update_session_timeout (void *cls,
1766 const struct GNUNET_PeerIdentity *peer,
1767 struct Session *session)
1769 struct HTTP_Client_Plugin *plugin = cls;
1771 /* lookup if session is really existing */
1772 if (GNUNET_YES != client_exist_session (plugin, session))
1777 client_reschedule_session_timeout (session);
1781 * Entry point for the plugin.
1784 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1786 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1787 struct GNUNET_TRANSPORT_PluginFunctions *api;
1788 struct HTTP_Client_Plugin *plugin;
1790 if (NULL == env->receive)
1792 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1793 initialze the plugin or the API */
1794 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1796 api->address_to_string = &http_plugin_address_to_string;
1797 api->string_to_address = &http_common_plugin_string_to_address;
1798 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1802 plugin = GNUNET_new (struct HTTP_Client_Plugin);
1804 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1806 api->send = &http_client_plugin_send;
1807 api->disconnect_session = &http_client_session_disconnect;
1808 api->query_keepalive_factor = &http_client_query_keepalive_factor;
1809 api->disconnect_peer = &http_client_peer_disconnect;
1810 api->check_address = &http_client_plugin_address_suggested;
1811 api->get_session = &http_client_plugin_get_session;
1812 api->address_to_string = &http_plugin_address_to_string;
1813 api->string_to_address = &http_common_plugin_string_to_address;
1814 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1815 api->get_network = &http_client_get_network;
1816 api->update_session_timeout = &http_client_plugin_update_session_timeout;
1819 plugin->name = "transport-https_client";
1820 plugin->protocol = "https";
1822 plugin->name = "transport-http_client";
1823 plugin->protocol = "http";
1825 plugin->last_tag = 1;
1826 plugin->options = 0; /* Setup options */
1828 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1830 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1835 if (GNUNET_SYSERR == client_start (plugin))
1837 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1843 /* end of plugin_transport_http_client.c */