2 This file is part of GNUnet
3 Copyright (C) 2002-2014 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file transport/plugin_transport_http_client.c
23 * @brief HTTP/S client transport plugin
24 * @author Matthias Wachs
25 * @author Christian Grothoff
29 #define PLUGIN_NAME "https_client"
30 #define HTTP_STAT_STR_CONNECTIONS "# HTTPS client connections"
31 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
32 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
34 #define PLUGIN_NAME "http_client"
35 #define HTTP_STAT_STR_CONNECTIONS "# HTTP client connections"
36 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
37 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
40 #define VERBOSE_CURL GNUNET_NO
42 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
44 #define ENABLE_PUT GNUNET_YES
45 #define ENABLE_GET GNUNET_YES
48 #include "gnunet_util_lib.h"
49 #include "gnunet_protocols.h"
50 #include "gnunet_transport_plugin.h"
51 #include "plugin_transport_http_common.h"
53 #include <curl/curl.h>
54 #elif HAVE_GNURL_CURL_H
55 #include <gnurl/curl.h>
59 #define LOG(kind,...) GNUNET_log_from(kind, PLUGIN_NAME, __VA_ARGS__)
62 * Encapsulation of all of the state of the plugin.
64 struct HTTP_Client_Plugin;
67 * State of a HTTP PUT request
69 enum HTTP_PUT_REQUEST_STATE
72 * Just created, not yet connected
82 * Paused, nothing to send
87 * Temporary disconnect in progress due to inactivity
92 * Send request while temporary disconnect, reconnect
94 H_TMP_RECONNECT_REQUIRED,
97 * Temporarily disconnected
108 * Message to send using http
113 * next pointer for double linked list
115 struct HTTP_Message *next;
118 * previous pointer for double linked list
120 struct HTTP_Message *prev;
123 * buffer containing data to send
128 * Continuation function to call once the transmission buffer
129 * has again space available. NULL if there is no
130 * continuation to call.
132 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
135 * Closure for @e transmit_cont.
137 void *transmit_cont_cls;
140 * amount of data already sent
153 * Session handle for HTTP(S) connections.
155 struct GNUNET_ATS_Session;
165 * Current state of this request
167 enum HTTP_PUT_REQUEST_STATE state;
170 * The curl easy handle
175 * The related session
177 struct GNUNET_ATS_Session *s;
182 * Session handle for connections.
184 struct GNUNET_ATS_Session
187 * The URL to connect to
194 struct GNUNET_HELLO_Address *address;
197 * Pointer to the global plugin struct.
199 struct HTTP_Client_Plugin *plugin;
202 * Handle for the HTTP PUT request.
204 struct RequestHandle put;
207 * Handle for the HTTP GET request.
209 struct RequestHandle get;
212 * next pointer for double linked list
214 struct HTTP_Message *msg_head;
217 * previous pointer for double linked list
219 struct HTTP_Message *msg_tail;
222 * Message stream tokenizer for incoming data
224 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
227 * Session timeout task
229 struct GNUNET_SCHEDULER_Task *put_disconnect_task;
232 * Session timeout task
234 struct GNUNET_SCHEDULER_Task *timeout_task;
237 * Task to wake up client receive handle when receiving is allowed again
239 struct GNUNET_SCHEDULER_Task *recv_wakeup_task;
242 * Absolute time when to receive data again.
243 * Used for receive throttling.
245 struct GNUNET_TIME_Absolute next_receive;
248 * When does this session time out.
250 struct GNUNET_TIME_Absolute timeout;
253 * Number of bytes waiting for transmission to this peer.
255 unsigned long long bytes_in_queue;
258 * Outbound overhead due to HTTP connection
259 * Add to next message of this session when calling callback
264 * Number of messages waiting for transmission to this peer.
266 unsigned int msgs_in_queue;
271 enum GNUNET_ATS_Network_Type scope;
276 * Encapsulation of all of the state of the plugin.
278 struct HTTP_Client_Plugin
283 struct GNUNET_TRANSPORT_PluginEnvironment *env;
288 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
291 * Function to call about session status changes.
293 GNUNET_TRANSPORT_SessionInfoCallback sic;
296 * Closure for @e sic.
311 * Proxy configuration: hostname or ip of the proxy server
313 char *proxy_hostname;
316 * Username for the proxy server
318 char *proxy_username;
321 * Password for the proxy server
323 char *proxy_password;
328 CURLM *curl_multi_handle;
333 struct GNUNET_SCHEDULER_Task * client_perform_task;
336 * Type of proxy server:
338 * Valid values as supported by curl:
339 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
340 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
342 curl_proxytype proxytype;
345 * Use proxy tunneling:
346 * Tunnel all operations through a given HTTP instead of have the proxy
347 * evaluate the HTTP request
349 * Default: #GNUNET_NO, #GNUNET_YES experimental
351 int proxy_use_httpproxytunnel;
354 * My options to be included in the address
359 * Maximum number of sockets the plugin can use
360 * Each http connections are two requests
362 unsigned int max_requests;
365 * Current number of sockets the plugin can use
366 * Each http connections are two requests
368 unsigned int cur_requests;
371 * Last used unique HTTP connection tag
386 * Should we emulate an XHR client for testing?
393 * Disconnect a session
395 * @param cls the `struct HTTP_Client_Plugin *`
397 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
400 http_client_plugin_session_disconnect (void *cls, struct GNUNET_ATS_Session *s);
404 * If a session monitor is attached, notify it about the new
407 * @param plugin our plugin
408 * @param session session that changed state
409 * @param state new state of the session
412 notify_session_monitor (struct HTTP_Client_Plugin *plugin,
413 struct GNUNET_ATS_Session *session,
414 enum GNUNET_TRANSPORT_SessionState state)
416 struct GNUNET_TRANSPORT_SessionInfo info;
418 if (NULL == plugin->sic)
420 memset (&info, 0, sizeof (info));
422 info.is_inbound = GNUNET_NO;
423 info.num_msg_pending = session->msgs_in_queue;
424 info.num_bytes_pending = session->bytes_in_queue;
425 info.receive_delay = session->next_receive;
426 info.session_timeout = session->timeout;
427 info.address = session->address;
428 plugin->sic (plugin->sic_cls,
435 * Delete session @a s.
437 * @param s the session to delete
440 client_delete_session (struct GNUNET_ATS_Session *s)
442 struct HTTP_Client_Plugin *plugin = s->plugin;
443 struct HTTP_Message *pos;
444 struct HTTP_Message *next;
447 if (NULL != s->timeout_task)
449 GNUNET_SCHEDULER_cancel (s->timeout_task);
450 s->timeout_task = NULL;
451 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
453 if (NULL != s->put_disconnect_task)
455 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
456 s->put_disconnect_task = NULL;
458 if (NULL != s->recv_wakeup_task)
460 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
461 s->recv_wakeup_task = NULL;
463 GNUNET_assert (GNUNET_OK ==
464 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
467 if (NULL != s->put.easyhandle)
469 LOG (GNUNET_ERROR_TYPE_DEBUG,
470 "Session %p/request %p: disconnecting PUT request to peer `%s'\n",
473 GNUNET_i2s (&s->address->peer));
475 /* remove curl handle from multi handle */
476 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
478 GNUNET_break (CURLM_OK == mret);
479 curl_easy_cleanup (s->put.easyhandle);
480 GNUNET_assert (plugin->cur_requests > 0);
481 plugin->cur_requests--;
482 s->put.easyhandle = NULL;
484 if (NULL != s->get.easyhandle)
486 LOG (GNUNET_ERROR_TYPE_DEBUG,
487 "Session %p/request %p: disconnecting GET request to peer `%s'\n",
488 s, s->get.easyhandle,
489 GNUNET_i2s (&s->address->peer));
490 /* remove curl handle from multi handle */
491 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
493 GNUNET_break (CURLM_OK == mret);
494 curl_easy_cleanup (s->get.easyhandle);
495 GNUNET_assert (plugin->cur_requests > 0);
496 plugin->cur_requests--;
497 s->get.easyhandle = NULL;
500 GNUNET_STATISTICS_set (plugin->env->stats,
501 HTTP_STAT_STR_CONNECTIONS,
502 plugin->cur_requests,
505 while (NULL != (pos = next))
508 GNUNET_CONTAINER_DLL_remove (s->msg_head,
511 GNUNET_assert (0 < s->msgs_in_queue);
513 GNUNET_assert (pos->size <= s->bytes_in_queue);
514 s->bytes_in_queue -= pos->size;
515 if (NULL != pos->transmit_cont)
516 pos->transmit_cont (pos->transmit_cont_cls,
520 pos->pos + s->overhead);
524 GNUNET_assert (0 == s->msgs_in_queue);
525 GNUNET_assert (0 == s->bytes_in_queue);
526 notify_session_monitor (plugin,
528 GNUNET_TRANSPORT_SS_DONE);
529 if (NULL != s->msg_tk)
531 GNUNET_SERVER_mst_destroy (s->msg_tk);
534 GNUNET_HELLO_address_free (s->address);
535 GNUNET_free (s->url);
541 * Increment session timeout due to activity for session @a s.
543 * @param s the session
546 client_reschedule_session_timeout (struct GNUNET_ATS_Session *s)
548 GNUNET_assert (NULL != s->timeout_task);
549 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
554 * Task performing curl operations
556 * @param cls plugin as closure
557 * @param tc gnunet scheduler task context
560 client_run (void *cls);
564 * Function setting up file descriptors and scheduling task to run
566 * @param plugin the plugin as closure
567 * @param now schedule task in 1ms, regardless of what curl may say
568 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
571 client_schedule (struct HTTP_Client_Plugin *plugin,
578 struct GNUNET_NETWORK_FDSet *grs;
579 struct GNUNET_NETWORK_FDSet *gws;
582 struct GNUNET_TIME_Relative timeout;
584 /* Cancel previous scheduled task */
585 if (plugin->client_perform_task != NULL)
587 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
588 plugin->client_perform_task = NULL;
594 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
595 if (mret != CURLM_OK)
597 LOG (GNUNET_ERROR_TYPE_ERROR,
598 _("%s failed at %s:%d: `%s'\n"),
602 curl_multi_strerror (mret));
603 return GNUNET_SYSERR;
605 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
607 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
609 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
610 if (now == GNUNET_YES)
611 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
613 if (CURLM_OK != mret)
615 LOG (GNUNET_ERROR_TYPE_ERROR,
616 _("%s failed at %s:%d: `%s'\n"),
617 "curl_multi_timeout", __FILE__, __LINE__,
618 curl_multi_strerror (mret));
619 return GNUNET_SYSERR;
622 grs = GNUNET_NETWORK_fdset_create ();
623 gws = GNUNET_NETWORK_fdset_create ();
624 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
625 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
627 /* Schedule task to run when select is ready to read or write */
628 plugin->client_perform_task =
629 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
631 &client_run, plugin);
632 GNUNET_NETWORK_fdset_destroy (gws);
633 GNUNET_NETWORK_fdset_destroy (grs);
642 * @param curl the curl easy handle
643 * @param type message type
644 * @param data data to log, NOT a 0-terminated string
645 * @param size data length
646 * @param cls the closure
650 client_log (CURL *curl,
656 struct RequestHandle *ch = cls;
657 const char *ttype = "UNSPECIFIED";
660 if (! ((CURLINFO_TEXT == type) ||
661 (CURLINFO_HEADER_IN == type) ||
662 (CURLINFO_HEADER_OUT == type)))
669 case CURLINFO_HEADER_IN:
672 case CURLINFO_HEADER_OUT:
673 ttype = "HEADER_OUT";
675 GNUNET_assert (NULL != ch);
676 GNUNET_assert (NULL != ch->easyhandle);
677 GNUNET_assert (NULL != ch->s);
678 ch->s->overhead += size;
681 ttype = "UNSPECIFIED";
684 memcpy (text, data, size);
685 if (text[size - 1] == '\n')
692 text[size + 1] = '\0';
694 LOG (GNUNET_ERROR_TYPE_DEBUG,
704 * Connect GET request
706 * @param s the session to connect
707 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
710 client_connect_get (struct GNUNET_ATS_Session *s);
714 * Connect a HTTP put request
716 * @param s the session to connect
717 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
720 client_connect_put (struct GNUNET_ATS_Session *s);
724 * Function that can be used by the transport service to transmit
725 * a message using the plugin. Note that in the case of a
726 * peer disconnecting, the continuation MUST be called
727 * prior to the disconnect notification itself. This function
728 * will be called with this peer's HELLO message to initiate
729 * a fresh connection to another peer.
732 * @param s which session must be used
733 * @param msgbuf the message to transmit
734 * @param msgbuf_size number of bytes in @a msgbuf
735 * @param priority how important is the message (most plugins will
736 * ignore message priority and just FIFO)
737 * @param to how long to wait at most for the transmission (does not
738 * require plugins to discard the message after the timeout,
739 * just advisory for the desired delay; most plugins will ignore
741 * @param cont continuation to call once the message has
742 * been transmitted (or if the transport is ready
743 * for the next transmission call; or if the
744 * peer disconnected...); can be NULL
745 * @param cont_cls closure for @a cont
746 * @return number of bytes used (on the physical network, with overheads);
747 * -1 on hard errors (i.e. address invalid); 0 is a legal value
748 * and does NOT mean that the message was not transmitted (DV)
751 http_client_plugin_send (void *cls,
752 struct GNUNET_ATS_Session *s,
755 unsigned int priority,
756 struct GNUNET_TIME_Relative to,
757 GNUNET_TRANSPORT_TransmitContinuation cont,
760 struct HTTP_Client_Plugin *plugin = cls;
761 struct HTTP_Message *msg;
764 LOG (GNUNET_ERROR_TYPE_DEBUG,
765 "Session %p/request %p: Sending message with %u to peer `%s' \n",
769 GNUNET_i2s (&s->address->peer));
771 /* create new message and schedule */
772 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
773 msg->size = msgbuf_size;
774 msg->buf = (char *) &msg[1];
775 msg->transmit_cont = cont;
776 msg->transmit_cont_cls = cont_cls;
780 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head,
784 s->bytes_in_queue += msg->size;
786 GNUNET_asprintf (&stat_txt,
787 "# bytes currently in %s_client buffers",
789 GNUNET_STATISTICS_update (plugin->env->stats,
790 stat_txt, msgbuf_size, GNUNET_NO);
791 GNUNET_free (stat_txt);
792 notify_session_monitor (plugin,
794 GNUNET_TRANSPORT_SS_UPDATE);
795 if (H_TMP_DISCONNECTING == s->put.state)
797 /* PUT request is currently getting disconnected */
798 s->put.state = H_TMP_RECONNECT_REQUIRED;
799 LOG (GNUNET_ERROR_TYPE_DEBUG,
800 "Session %p/request %p: currently disconnecting, reconnecting immediately\n",
805 if (H_PAUSED == s->put.state)
807 /* PUT request was paused, unpause */
808 GNUNET_assert (s->put_disconnect_task != NULL);
809 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
810 s->put_disconnect_task = NULL;
811 LOG (GNUNET_ERROR_TYPE_DEBUG,
812 "Session %p/request %p: unpausing request\n",
813 s, s->put.easyhandle);
814 s->put.state = H_CONNECTED;
815 if (NULL != s->put.easyhandle)
816 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
818 else if (H_TMP_DISCONNECTED == s->put.state)
820 /* PUT request was disconnected, reconnect */
821 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session %p: Reconnecting PUT request\n", s);
822 GNUNET_break (NULL == s->put.easyhandle);
823 if (GNUNET_SYSERR == client_connect_put (s))
825 /* Could not reconnect */
826 http_client_plugin_session_disconnect (plugin, s);
827 return GNUNET_SYSERR;
830 client_schedule (s->plugin, GNUNET_YES);
836 * Disconnect a session
838 * @param cls the `struct HTTP_Client_Plugin *`
840 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
843 http_client_plugin_session_disconnect (void *cls,
844 struct GNUNET_ATS_Session *s)
846 struct HTTP_Client_Plugin *plugin = cls;
848 LOG (GNUNET_ERROR_TYPE_DEBUG,
849 "Session %p: notifying transport about ending session\n",
851 plugin->env->session_end (plugin->env->cls,
854 client_delete_session (s);
856 /* Re-schedule since handles have changed */
857 if (NULL != plugin->client_perform_task)
859 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
860 plugin->client_perform_task = NULL;
862 client_schedule (plugin, GNUNET_YES);
869 * Function that is called to get the keepalive factor.
870 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
871 * calculate the interval between keepalive packets.
873 * @param cls closure with the `struct Plugin`
874 * @return keepalive factor
877 http_client_query_keepalive_factor (void *cls)
884 * Callback to destroys all sessions on exit.
886 * @param cls the `struct HTTP_Client_Plugin *`
887 * @param peer identity of the peer
888 * @param value the `struct GNUNET_ATS_Session *`
889 * @return #GNUNET_OK (continue iterating)
892 destroy_session_cb (void *cls,
893 const struct GNUNET_PeerIdentity *peer,
896 struct HTTP_Client_Plugin *plugin = cls;
897 struct GNUNET_ATS_Session *session = value;
899 http_client_plugin_session_disconnect (plugin, session);
905 * Function that can be used to force the plugin to disconnect
906 * from the given peer and cancel all previous transmissions
907 * (and their continuationc).
910 * @param target peer from which to disconnect
913 http_client_plugin_peer_disconnect (void *cls,
914 const struct GNUNET_PeerIdentity *target)
916 struct HTTP_Client_Plugin *plugin = cls;
918 LOG (GNUNET_ERROR_TYPE_DEBUG,
919 "Transport tells me to disconnect `%s'\n",
920 GNUNET_i2s (target));
921 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
929 * Closure for #session_lookup_client_by_address().
931 struct GNUNET_ATS_SessionClientCtx
934 * Address we are looking for.
936 const struct GNUNET_HELLO_Address *address;
939 * Session that was found.
941 struct GNUNET_ATS_Session *ret;
946 * Locate the seession object for a given address.
948 * @param cls the `struct GNUNET_ATS_SessionClientCtx *`
949 * @param key peer identity
950 * @param value the `struct GNUNET_ATS_Session` to check
951 * @return #GNUNET_NO if found, #GNUNET_OK if not
954 session_lookup_client_by_address (void *cls,
955 const struct GNUNET_PeerIdentity *key,
958 struct GNUNET_ATS_SessionClientCtx *sc_ctx = cls;
959 struct GNUNET_ATS_Session *s = value;
961 if (0 == GNUNET_HELLO_address_cmp (sc_ctx->address,
972 * Check if a sessions exists for an specific address
974 * @param plugin the plugin
975 * @param address the address
976 * @return the session or NULL
978 static struct GNUNET_ATS_Session *
979 client_lookup_session (struct HTTP_Client_Plugin *plugin,
980 const struct GNUNET_HELLO_Address *address)
982 struct GNUNET_ATS_SessionClientCtx sc_ctx;
984 sc_ctx.address = address;
986 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
987 &session_lookup_client_by_address,
994 * When we have nothing to transmit, we pause the HTTP PUT
995 * after a while (so that gnurl stops asking). This task
996 * is the delayed task that actually disconnects the PUT.
998 * @param cls the `struct GNUNET_ATS_Session *` with the put
1001 client_put_disconnect (void *cls)
1003 struct GNUNET_ATS_Session *s = cls;
1005 s->put_disconnect_task = NULL;
1006 LOG (GNUNET_ERROR_TYPE_DEBUG,
1007 "Session %p/request %p: will be disconnected due to no activity\n",
1008 s, s->put.easyhandle);
1009 s->put.state = H_TMP_DISCONNECTING;
1010 if (NULL != s->put.easyhandle)
1011 curl_easy_pause (s->put.easyhandle,
1013 client_schedule (s->plugin, GNUNET_YES);
1018 * Callback method used with libcurl
1019 * Method is called when libcurl needs to read data during sending
1021 * @param stream pointer where to write data
1022 * @param size size of an individual element
1023 * @param nmemb count of elements that can be written to the buffer
1024 * @param cls our `struct GNUNET_ATS_Session`
1025 * @return bytes written to stream, returning 0 will terminate request!
1028 client_send_cb (void *stream,
1033 struct GNUNET_ATS_Session *s = cls;
1034 struct HTTP_Client_Plugin *plugin = s->plugin;
1035 struct HTTP_Message *msg = s->msg_head;
1039 if (H_TMP_DISCONNECTING == s->put.state)
1041 LOG (GNUNET_ERROR_TYPE_DEBUG,
1042 "Session %p/request %p: disconnect due to inactivity\n",
1043 s, s->put.easyhandle);
1049 if (GNUNET_YES == plugin->emulate_xhr)
1051 LOG (GNUNET_ERROR_TYPE_DEBUG,
1052 "Session %p/request %p: PUT request finished\n",
1055 s->put.state = H_TMP_DISCONNECTING;
1059 /* We have nothing to send, so pause PUT request */
1060 LOG (GNUNET_ERROR_TYPE_DEBUG,
1061 "Session %p/request %p: nothing to send, suspending\n",
1064 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
1065 &client_put_disconnect, s);
1066 s->put.state = H_PAUSED;
1067 return CURL_READFUNC_PAUSE;
1070 GNUNET_assert (msg->pos < msg->size);
1071 /* calculate how much fits in buffer */
1072 len = GNUNET_MIN (msg->size - msg->pos,
1074 memcpy (stream, &msg->buf[msg->pos], len);
1076 if (msg->pos == msg->size)
1078 LOG (GNUNET_ERROR_TYPE_DEBUG,
1079 "Session %p/request %p: sent message with %u bytes sent, removing message from queue\n",
1084 /* Calling transmit continuation */
1085 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1088 GNUNET_assert (0 < s->msgs_in_queue);
1090 GNUNET_assert (msg->size <= s->bytes_in_queue);
1091 s->bytes_in_queue -= msg->size;
1092 if (NULL != msg->transmit_cont)
1093 msg->transmit_cont (msg->transmit_cont_cls,
1097 msg->size + s->overhead);
1101 notify_session_monitor (plugin,
1103 GNUNET_TRANSPORT_SS_UPDATE);
1104 GNUNET_asprintf (&stat_txt,
1105 "# bytes currently in %s_client buffers",
1107 GNUNET_STATISTICS_update (plugin->env->stats,
1111 GNUNET_free (stat_txt);
1112 GNUNET_asprintf (&stat_txt,
1113 "# bytes transmitted via %s_client",
1115 GNUNET_STATISTICS_update (plugin->env->stats,
1119 GNUNET_free (stat_txt);
1125 * Wake up a curl handle which was suspended
1127 * @param cls the session
1130 client_wake_up (void *cls)
1132 struct GNUNET_ATS_Session *s = cls;
1134 s->recv_wakeup_task = NULL;
1135 LOG (GNUNET_ERROR_TYPE_DEBUG,
1136 "Session %p/request %p: Waking up GET handle\n",
1137 s, s->get.easyhandle);
1138 if (H_PAUSED == s->put.state)
1140 /* PUT request was paused, unpause */
1141 GNUNET_assert (s->put_disconnect_task != NULL);
1142 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
1143 s->put_disconnect_task = NULL;
1144 s->put.state = H_CONNECTED;
1145 if (NULL != s->put.easyhandle)
1146 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
1148 if (NULL != s->get.easyhandle)
1149 curl_easy_pause (s->get.easyhandle, CURLPAUSE_CONT);
1154 * Callback for message stream tokenizer
1156 * @param cls the session
1157 * @param client not used
1158 * @param message the message received
1159 * @return always #GNUNET_OK
1162 client_receive_mst_cb (void *cls,
1164 const struct GNUNET_MessageHeader *message)
1166 struct GNUNET_ATS_Session *s = cls;
1167 struct HTTP_Client_Plugin *plugin;
1168 struct GNUNET_TIME_Relative delay;
1172 delay = s->plugin->env->receive (plugin->env->cls,
1176 GNUNET_asprintf (&stat_txt,
1177 "# bytes received via %s_client",
1179 GNUNET_STATISTICS_update (plugin->env->stats,
1181 ntohs (message->size),
1183 GNUNET_free (stat_txt);
1185 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1186 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
1188 LOG (GNUNET_ERROR_TYPE_DEBUG,
1189 "Client: peer `%s' address `%s' next read delayed for %s\n",
1190 GNUNET_i2s (&s->address->peer),
1191 http_common_plugin_address_to_string (s->plugin->protocol,
1192 s->address->address,
1193 s->address->address_length),
1194 GNUNET_STRINGS_relative_time_to_string (delay,
1197 client_reschedule_session_timeout (s);
1203 * Callback method used with libcurl when data for a PUT request are
1204 * received. We do not expect data here, so we just discard it.
1206 * @param stream pointer where to write data
1207 * @param size size of an individual element
1208 * @param nmemb count of elements that can be written to the buffer
1209 * @param cls destination pointer, passed to the libcurl handle
1210 * @return bytes read from stream
1213 client_receive_put (void *stream,
1218 return size * nmemb;
1223 * Callback method used with libcurl when data for a GET request are
1224 * received. Forward to MST
1226 * @param stream pointer where to write data
1227 * @param size size of an individual element
1228 * @param nmemb count of elements that can be written to the buffer
1229 * @param cls destination pointer, passed to the libcurl handle
1230 * @return bytes read from stream
1233 client_receive (void *stream,
1238 struct GNUNET_ATS_Session *s = cls;
1239 struct GNUNET_TIME_Absolute now;
1240 size_t len = size * nmemb;
1242 LOG (GNUNET_ERROR_TYPE_DEBUG,
1243 "Session %p / request %p: Received %u bytes from peer `%s'\n",
1247 GNUNET_i2s (&s->address->peer));
1248 now = GNUNET_TIME_absolute_get ();
1249 if (now.abs_value_us < s->next_receive.abs_value_us)
1251 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1252 struct GNUNET_TIME_Relative delta
1253 = GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1255 LOG (GNUNET_ERROR_TYPE_DEBUG,
1256 "Session %p / request %p: No inbound bandwidth available! Next read was delayed for %s\n",
1259 GNUNET_STRINGS_relative_time_to_string (delta,
1261 if (s->recv_wakeup_task != NULL)
1263 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1264 s->recv_wakeup_task = NULL;
1267 = GNUNET_SCHEDULER_add_delayed (delta,
1270 return CURL_WRITEFUNC_PAUSE;
1272 if (NULL == s->msg_tk)
1273 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb,
1275 GNUNET_SERVER_mst_receive (s->msg_tk,
1286 * Task performing curl operations
1288 * @param cls plugin as closure
1291 client_run (void *cls)
1293 struct HTTP_Client_Plugin *plugin = cls;
1295 long http_statuscode;
1298 int put_request; /* GNUNET_YES if easy handle is put, GNUNET_NO for get */
1301 plugin->client_perform_task = NULL;
1302 /* While data are available or timeouts occured */
1306 /* Perform operations for all handles */
1307 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1309 /* Get additional information for all handles */
1310 while (NULL != (msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1312 CURL *easy_h = msg->easy_handle;
1313 struct GNUNET_ATS_Session *s = NULL;
1314 char *d = NULL; /* curl requires 'd' to be a 'char *' */
1316 GNUNET_assert (NULL != easy_h);
1318 /* Obtain session from easy handle */
1319 GNUNET_assert (CURLE_OK == curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1320 s = (struct GNUNET_ATS_Session *) d;
1321 GNUNET_assert (NULL != s);
1323 if (msg->msg != CURLMSG_DONE)
1324 continue; /* This should not happen */
1326 /* Get HTTP response code */
1327 GNUNET_break (CURLE_OK == curl_easy_getinfo (easy_h,
1328 CURLINFO_RESPONSE_CODE, &http_statuscode));
1330 if (easy_h == s->put.easyhandle)
1331 put_request = GNUNET_YES;
1333 put_request = GNUNET_NO;
1335 /* Log status of terminated request */
1336 if ((0 != msg->data.result) || (http_statuscode != 200))
1337 LOG (GNUNET_ERROR_TYPE_DEBUG,
1338 "Session %p/request %p: %s request to `%s' ended with status %i reason %i: `%s'\n",
1339 s, msg->easy_handle,
1340 (GNUNET_YES == put_request) ? "PUT" : "GET",
1341 GNUNET_i2s (&s->address->peer),
1344 curl_easy_strerror (msg->data.result));
1346 LOG (GNUNET_ERROR_TYPE_DEBUG,
1347 "Session %p/request %p: %s request to `%s' ended normal\n",
1348 s, msg->easy_handle,
1349 (GNUNET_YES == put_request) ? "PUT" : "GET",
1350 GNUNET_i2s (&s->address->peer));
1352 /* Remove easy handle from multi handle */
1353 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1355 /* Clean up easy handle */
1356 curl_easy_cleanup (easy_h);
1358 /* Remove information */
1359 GNUNET_assert (plugin->cur_requests > 0);
1360 plugin->cur_requests--;
1361 LOG (GNUNET_ERROR_TYPE_INFO,
1362 "%s request to %s done, number of requests decreased to %u\n",
1363 (GNUNET_YES == put_request) ? "PUT" : "GET",
1365 plugin->cur_requests);
1367 if (GNUNET_YES == put_request)
1369 /* Clean up a PUT request */
1370 s->put.easyhandle = NULL;
1373 switch (s->put.state) {
1374 case H_NOT_CONNECTED:
1375 case H_DISCONNECTED:
1376 case H_TMP_DISCONNECTED:
1377 /* This must not happen */
1380 case H_TMP_RECONNECT_REQUIRED:
1381 /* Transport called send while disconnect in progess, reconnect */
1382 if (GNUNET_SYSERR == client_connect_put (s))
1384 /* Reconnect failed, disconnect session */
1385 http_client_plugin_session_disconnect (plugin, s);
1388 case H_TMP_DISCONNECTING:
1389 /* PUT gets temporarily disconnected */
1390 s->put.state = H_TMP_DISCONNECTED;
1394 /* PUT gets permanently disconnected */
1395 s->put.state = H_DISCONNECTED;
1396 http_client_plugin_session_disconnect (plugin, s);
1403 else if (GNUNET_NO == put_request)
1405 /* Clean up a GET request */
1406 s->get.easyhandle = NULL;
1409 /* If we are emulating an XHR client we need to make another GET
1412 if (GNUNET_YES == plugin->emulate_xhr)
1414 if (GNUNET_SYSERR == client_connect_get (s))
1415 http_client_plugin_session_disconnect (plugin, s);
1419 /* GET request was terminated, so disconnect session */
1420 http_client_plugin_session_disconnect (plugin, s);
1424 GNUNET_break (0); /* Must not happen */
1426 GNUNET_STATISTICS_set (plugin->env->stats,
1427 HTTP_STAT_STR_CONNECTIONS,
1428 plugin->cur_requests,
1432 while (mret == CURLM_CALL_MULTI_PERFORM);
1433 client_schedule (plugin, GNUNET_NO);
1439 * Open TCP socket with TCP STEALTH enabled.
1441 * @param clientp our `struct GNUNET_ATS_Session *`
1442 * @param purpose why does curl want to open a socket
1443 * @param address what kind of socket does curl want to have opened?
1444 * @return opened socket
1446 static curl_socket_t
1447 open_tcp_stealth_socket_cb (void *clientp,
1448 curlsocktype purpose,
1449 struct curl_sockaddr *address)
1451 struct GNUNET_ATS_Session *s = clientp;
1456 case CURLSOCKTYPE_IPCXN:
1457 ret = socket (address->family,
1461 return CURL_SOCKET_BAD;
1462 if ( ( (SOCK_STREAM != address->socktype) ||
1463 ( (0 != address->protocol) &&
1464 (IPPROTO_TCP != address->protocol))) )
1465 return (curl_socket_t) ret;
1466 if ( (0 != setsockopt (ret,
1470 sizeof (struct GNUNET_PeerIdentity))) )
1472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1473 _("TCP_STEALTH not supported on this platform.\n"));
1475 return CURL_SOCKET_BAD;
1477 return (curl_socket_t) ret;
1478 case CURLSOCKTYPE_ACCEPT:
1480 return CURL_SOCKET_BAD;
1482 case CURLSOCKTYPE_LAST:
1484 return CURL_SOCKET_BAD;
1487 return CURL_SOCKET_BAD;
1494 * Connect GET request for a session
1496 * @param s the session to connect
1497 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1500 client_connect_get (struct GNUNET_ATS_Session *s)
1503 struct HttpAddress *ha;
1506 ha = (struct HttpAddress *) s->address->address;
1507 options = ntohl (ha->options);
1508 /* create get request */
1509 s->get.easyhandle = curl_easy_init ();
1511 if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
1514 curl_easy_setopt (s->get.easyhandle,
1515 CURLOPT_OPENSOCKETFUNCTION,
1516 &open_tcp_stealth_socket_cb);
1517 curl_easy_setopt (s->get.easyhandle,
1518 CURLOPT_OPENSOCKETDATA,
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
1523 curl_easy_cleanup (s->get.easyhandle);
1524 s->get.easyhandle = NULL;
1526 return GNUNET_SYSERR;
1531 curl_easy_setopt (s->get.easyhandle,
1534 curl_easy_setopt (s->get.easyhandle,
1535 CURLOPT_DEBUGFUNCTION,
1537 curl_easy_setopt (s->get.easyhandle,
1542 curl_easy_setopt (s->get.easyhandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1544 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1545 (options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1547 curl_easy_setopt (s->get.easyhandle,
1548 CURLOPT_SSL_VERIFYPEER, 1L);
1549 curl_easy_setopt (s->get.easyhandle,
1550 CURLOPT_SSL_VERIFYHOST,
1555 curl_easy_setopt (s->get.easyhandle,
1556 CURLOPT_SSL_VERIFYPEER,
1558 curl_easy_setopt (s->get.easyhandle,
1559 CURLOPT_SSL_VERIFYHOST,
1563 curl_easy_setopt (s->get.easyhandle,
1566 curl_easy_setopt (s->get.easyhandle,
1567 CURLOPT_REDIR_PROTOCOLS,
1570 curl_easy_setopt (s->get.easyhandle,
1573 curl_easy_setopt (s->get.easyhandle,
1574 CURLOPT_REDIR_PROTOCOLS,
1578 if (NULL != s->plugin->proxy_hostname)
1580 curl_easy_setopt (s->get.easyhandle,
1582 s->plugin->proxy_hostname);
1583 curl_easy_setopt (s->get.easyhandle,
1585 s->plugin->proxytype);
1586 if (NULL != s->plugin->proxy_username)
1587 curl_easy_setopt (s->get.easyhandle,
1588 CURLOPT_PROXYUSERNAME,
1589 s->plugin->proxy_username);
1590 if (NULL != s->plugin->proxy_password)
1591 curl_easy_setopt (s->get.easyhandle,
1592 CURLOPT_PROXYPASSWORD,
1593 s->plugin->proxy_password);
1594 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1595 curl_easy_setopt (s->get.easyhandle,
1596 CURLOPT_HTTPPROXYTUNNEL,
1597 s->plugin->proxy_use_httpproxytunnel);
1600 if (GNUNET_YES == s->plugin->emulate_xhr)
1604 GNUNET_asprintf (&url,
1607 curl_easy_setopt (s->get.easyhandle,
1614 curl_easy_setopt (s->get.easyhandle,
1618 curl_easy_setopt (s->get.easyhandle,
1619 CURLOPT_READFUNCTION,
1621 curl_easy_setopt (s->get.easyhandle,
1624 curl_easy_setopt (s->get.easyhandle,
1625 CURLOPT_WRITEFUNCTION,
1627 curl_easy_setopt (s->get.easyhandle,
1630 /* No timeout by default, timeout done with session timeout */
1631 curl_easy_setopt (s->get.easyhandle,
1634 curl_easy_setopt (s->get.easyhandle,
1635 CURLOPT_PRIVATE, s);
1636 curl_easy_setopt (s->get.easyhandle,
1637 CURLOPT_CONNECTTIMEOUT_MS,
1638 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1639 curl_easy_setopt (s->get.easyhandle, CURLOPT_BUFFERSIZE,
1640 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1641 #if CURL_TCP_NODELAY
1642 curl_easy_setopt (ps->recv_endpoint,
1643 CURLOPT_TCP_NODELAY,
1646 curl_easy_setopt (s->get.easyhandle,
1647 CURLOPT_FOLLOWLOCATION,
1650 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1652 if (CURLM_OK != mret)
1654 LOG (GNUNET_ERROR_TYPE_ERROR,
1655 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1657 curl_multi_strerror (mret));
1658 curl_easy_cleanup (s->get.easyhandle);
1659 s->get.easyhandle = NULL;
1662 return GNUNET_SYSERR;
1664 s->plugin->cur_requests++;
1665 LOG (GNUNET_ERROR_TYPE_INFO,
1666 "GET request `%s' established, number of requests increased to %u\n",
1668 s->plugin->cur_requests);
1674 * Connect a HTTP put request
1676 * @param s the session to connect
1677 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1680 client_connect_put (struct GNUNET_ATS_Session *s)
1683 struct HttpAddress *ha;
1686 ha = (struct HttpAddress *) s->address->address;
1687 options = ntohl (ha->options);
1688 /* create put request */
1689 LOG (GNUNET_ERROR_TYPE_DEBUG,
1690 "Session %p: Init PUT handle\n",
1692 s->put.easyhandle = curl_easy_init ();
1695 curl_easy_setopt (s->put.easyhandle,
1698 curl_easy_setopt (s->put.easyhandle,
1699 CURLOPT_DEBUGFUNCTION,
1701 curl_easy_setopt (s->put.easyhandle,
1705 if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
1708 curl_easy_setopt (s->put.easyhandle,
1709 CURLOPT_OPENSOCKETFUNCTION,
1710 &open_tcp_stealth_socket_cb);
1711 curl_easy_setopt (s->put.easyhandle,
1712 CURLOPT_OPENSOCKETDATA,
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1716 "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
1717 curl_easy_cleanup (s->put.easyhandle);
1718 s->put.easyhandle = NULL;
1720 s->put.state = H_DISCONNECTED;
1721 return GNUNET_SYSERR;
1725 curl_easy_setopt (s->put.easyhandle,
1727 CURL_SSLVERSION_TLSv1);
1729 struct HttpAddress *ha;
1730 ha = (struct HttpAddress *) s->address->address;
1732 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1733 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1735 curl_easy_setopt (s->put.easyhandle,
1736 CURLOPT_SSL_VERIFYPEER,
1738 curl_easy_setopt (s->put.easyhandle,
1739 CURLOPT_SSL_VERIFYHOST,
1744 curl_easy_setopt (s->put.easyhandle,
1745 CURLOPT_SSL_VERIFYPEER,
1747 curl_easy_setopt (s->put.easyhandle,
1748 CURLOPT_SSL_VERIFYHOST,
1752 curl_easy_setopt (s->put.easyhandle,
1755 curl_easy_setopt (s->put.easyhandle,
1756 CURLOPT_REDIR_PROTOCOLS,
1759 curl_easy_setopt (s->put.easyhandle,
1762 curl_easy_setopt (s->put.easyhandle,
1763 CURLOPT_REDIR_PROTOCOLS,
1766 if (NULL != s->plugin->proxy_hostname)
1768 curl_easy_setopt (s->put.easyhandle,
1770 s->plugin->proxy_hostname);
1771 curl_easy_setopt (s->put.easyhandle,
1773 s->plugin->proxytype);
1774 if (NULL != s->plugin->proxy_username)
1775 curl_easy_setopt (s->put.easyhandle,
1776 CURLOPT_PROXYUSERNAME,
1777 s->plugin->proxy_username);
1778 if (NULL != s->plugin->proxy_password)
1779 curl_easy_setopt (s->put.easyhandle,
1780 CURLOPT_PROXYPASSWORD,
1781 s->plugin->proxy_password);
1782 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1783 curl_easy_setopt (s->put.easyhandle,
1784 CURLOPT_HTTPPROXYTUNNEL,
1785 s->plugin->proxy_use_httpproxytunnel);
1788 curl_easy_setopt (s->put.easyhandle,
1791 curl_easy_setopt (s->put.easyhandle,
1794 curl_easy_setopt (s->put.easyhandle,
1795 CURLOPT_READFUNCTION,
1797 curl_easy_setopt (s->put.easyhandle,
1800 curl_easy_setopt (s->put.easyhandle,
1801 CURLOPT_WRITEFUNCTION,
1802 &client_receive_put);
1803 curl_easy_setopt (s->put.easyhandle,
1806 /* No timeout by default, timeout done with session timeout */
1807 curl_easy_setopt (s->put.easyhandle,
1810 curl_easy_setopt (s->put.easyhandle,
1813 curl_easy_setopt (s->put.easyhandle,
1814 CURLOPT_CONNECTTIMEOUT_MS,
1815 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1816 curl_easy_setopt (s->put.easyhandle, CURLOPT_BUFFERSIZE,
1817 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1818 #if CURL_TCP_NODELAY
1819 curl_easy_setopt (s->put.easyhandle, CURLOPT_TCP_NODELAY, 1);
1821 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1823 if (CURLM_OK != mret)
1825 LOG (GNUNET_ERROR_TYPE_ERROR,
1826 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1827 s, curl_multi_strerror (mret));
1828 curl_easy_cleanup (s->put.easyhandle);
1829 s->put.easyhandle = NULL;
1831 s->put.state = H_DISCONNECTED;
1832 return GNUNET_SYSERR;
1834 s->put.state = H_CONNECTED;
1835 s->plugin->cur_requests++;
1837 LOG (GNUNET_ERROR_TYPE_INFO,
1838 "PUT request `%s' established, number of requests increased to %u\n",
1839 s->url, s->plugin->cur_requests);
1846 * Connect both PUT and GET request for a session
1848 * @param s the session to connect
1849 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1852 client_connect (struct GNUNET_ATS_Session *s)
1854 struct HTTP_Client_Plugin *plugin = s->plugin;
1855 int res = GNUNET_OK;
1859 http_common_plugin_address_to_string(plugin->protocol,
1860 s->address->address,
1861 s->address->address_length))
1863 LOG (GNUNET_ERROR_TYPE_DEBUG,
1864 "Invalid address peer `%s'\n",
1865 GNUNET_i2s(&s->address->peer));
1866 return GNUNET_SYSERR;
1869 GNUNET_asprintf (&s->url,
1871 http_common_plugin_address_to_url (NULL,
1872 s->address->address,
1873 s->address->address_length),
1874 GNUNET_i2s_full (plugin->env->my_identity),
1878 LOG (GNUNET_ERROR_TYPE_DEBUG,
1879 "Initiating outbound session peer `%s' using address `%s'\n",
1880 GNUNET_i2s (&s->address->peer), s->url);
1882 if (GNUNET_SYSERR == client_connect_get (s))
1883 return GNUNET_SYSERR;
1884 /* If we are emulating an XHR client then delay sending a PUT request until
1885 * there is something to send.
1887 if (GNUNET_YES == plugin->emulate_xhr)
1889 s->put.state = H_TMP_DISCONNECTED;
1891 else if (GNUNET_SYSERR == client_connect_put (s))
1892 return GNUNET_SYSERR;
1894 LOG (GNUNET_ERROR_TYPE_DEBUG,
1895 "Session %p: connected with GET %p and PUT %p\n",
1896 s, s->get.easyhandle,
1898 /* Perform connect */
1899 GNUNET_STATISTICS_set (plugin->env->stats,
1900 HTTP_STAT_STR_CONNECTIONS,
1901 plugin->cur_requests,
1903 /* Re-schedule since handles have changed */
1904 if (NULL != plugin->client_perform_task)
1906 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1907 plugin->client_perform_task = NULL;
1910 /* Schedule task to run immediately */
1911 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run,
1918 * Function obtain the network type for a session
1920 * @param cls closure (`struct Plugin*`)
1921 * @param session the session
1922 * @return the network type
1924 static enum GNUNET_ATS_Network_Type
1925 http_client_plugin_get_network (void *cls,
1926 struct GNUNET_ATS_Session *session)
1928 return session->scope;
1933 * Function obtain the network type for an address.
1935 * @param cls closure (`struct Plugin *`)
1936 * @param address the address
1937 * @return the network type
1939 static enum GNUNET_ATS_Network_Type
1940 http_client_plugin_get_network_for_address (void *cls,
1941 const struct GNUNET_HELLO_Address *address)
1943 struct HTTP_Client_Plugin *plugin = cls;
1945 return http_common_get_network_for_address (plugin->env,
1951 * Session was idle, so disconnect it
1953 * @param cls the `struct GNUNET_ATS_Session` of the idle session
1956 client_session_timeout (void *cls)
1958 struct GNUNET_ATS_Session *s = cls;
1959 struct GNUNET_TIME_Relative left;
1961 s->timeout_task = NULL;
1962 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1963 if (0 != left.rel_value_us)
1965 /* not actually our turn yet, but let's at least update
1966 the monitor, it may think we're about to die ... */
1967 notify_session_monitor (s->plugin,
1969 GNUNET_TRANSPORT_SS_UPDATE);
1970 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1971 &client_session_timeout,
1976 "Session %p was idle for %s, disconnecting\n",
1978 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1980 GNUNET_assert (GNUNET_OK ==
1981 http_client_plugin_session_disconnect (s->plugin,
1987 * Creates a new outbound session the transport service will use to
1988 * send data to the peer
1990 * @param cls the plugin
1991 * @param address the address
1992 * @return the session or NULL of max connections exceeded
1994 static struct GNUNET_ATS_Session *
1995 http_client_plugin_get_session (void *cls,
1996 const struct GNUNET_HELLO_Address *address)
1998 struct HTTP_Client_Plugin *plugin = cls;
1999 struct GNUNET_ATS_Session *s;
2000 struct sockaddr *sa;
2001 enum GNUNET_ATS_Network_Type net_type;
2005 GNUNET_assert (NULL != address->address);
2007 /* find existing session */
2008 s = client_lookup_session (plugin, address);
2012 /* create a new session */
2013 if (plugin->max_requests <= plugin->cur_requests)
2015 LOG (GNUNET_ERROR_TYPE_WARNING,
2016 "Maximum number of requests (%u) reached: "
2017 "cannot connect to peer `%s'\n",
2018 plugin->max_requests,
2019 GNUNET_i2s (&address->peer));
2023 /* Determine network location */
2024 net_type = GNUNET_ATS_NET_UNSPECIFIED;
2025 sa = http_common_socket_from_address (address->address,
2026 address->address_length,
2028 if (GNUNET_SYSERR == res)
2030 if (GNUNET_YES == res)
2032 GNUNET_assert (NULL != sa);
2033 if (AF_INET == sa->sa_family)
2035 salen = sizeof (struct sockaddr_in);
2037 else if (AF_INET6 == sa->sa_family)
2039 salen = sizeof (struct sockaddr_in6);
2041 net_type = plugin->env->get_address_type (plugin->env->cls, sa, salen);
2044 else if (GNUNET_NO == res)
2046 /* Cannot convert to sockaddr -> is external hostname */
2047 net_type = GNUNET_ATS_NET_WAN;
2049 if (GNUNET_ATS_NET_UNSPECIFIED == net_type)
2055 s = GNUNET_new (struct GNUNET_ATS_Session);
2057 s->address = GNUNET_HELLO_address_copy (address);
2058 s->scope = net_type;
2060 s->put.state = H_NOT_CONNECTED;
2061 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT);
2062 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
2063 &client_session_timeout,
2065 LOG (GNUNET_ERROR_TYPE_DEBUG,
2066 "Created new session %p for `%s' address `%s''\n",
2068 http_common_plugin_address_to_string (plugin->protocol,
2069 s->address->address,
2070 s->address->address_length),
2071 GNUNET_i2s (&s->address->peer));
2073 /* add new session */
2074 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2077 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2078 /* initiate new connection */
2079 if (GNUNET_SYSERR == client_connect (s))
2081 LOG (GNUNET_ERROR_TYPE_ERROR,
2082 "Cannot connect to peer `%s' address `%s''\n",
2083 http_common_plugin_address_to_string (plugin->protocol,
2084 s->address->address, s->address->address_length),
2085 GNUNET_i2s (&s->address->peer));
2086 client_delete_session (s);
2089 notify_session_monitor (plugin,
2091 GNUNET_TRANSPORT_SS_INIT);
2092 notify_session_monitor (plugin,
2094 GNUNET_TRANSPORT_SS_UP); /* or handshake? */
2100 * Setup http_client plugin
2102 * @param plugin the plugin handle
2103 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
2106 client_start (struct HTTP_Client_Plugin *plugin)
2108 curl_global_init (CURL_GLOBAL_ALL);
2109 plugin->curl_multi_handle = curl_multi_init ();
2111 if (NULL == plugin->curl_multi_handle)
2113 LOG (GNUNET_ERROR_TYPE_ERROR,
2114 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
2116 return GNUNET_SYSERR;
2123 * Another peer has suggested an address for this
2124 * peer and transport plugin. Check that this could be a valid
2125 * address. If so, consider adding it to the list
2128 * @param cls closure with the `struct Plugin`
2129 * @param addr pointer to the address
2130 * @param addrlen length of @a addr
2131 * @return #GNUNET_OK if this is a plausible address for this peer
2132 * and transport; always returns #GNUNET_NO (this is the client!)
2135 http_client_plugin_address_suggested (void *cls,
2139 /* A HTTP/S client does not have any valid address so:*/
2145 * Exit point from the plugin.
2147 * @param cls api as closure
2151 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2153 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2154 struct HTTP_Client_Plugin *plugin = api->cls;
2156 if (NULL == api->cls)
2162 LOG (GNUNET_ERROR_TYPE_DEBUG,
2163 _("Shutting down plugin `%s'\n"),
2165 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2166 &destroy_session_cb,
2168 if (NULL != plugin->client_perform_task)
2170 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
2171 plugin->client_perform_task = NULL;
2173 if (NULL != plugin->curl_multi_handle)
2175 curl_multi_cleanup (plugin->curl_multi_handle);
2176 plugin->curl_multi_handle = NULL;
2178 curl_global_cleanup ();
2179 LOG (GNUNET_ERROR_TYPE_DEBUG,
2180 _("Shutdown for plugin `%s' complete\n"),
2182 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
2183 GNUNET_free_non_null (plugin->proxy_hostname);
2184 GNUNET_free_non_null (plugin->proxy_username);
2185 GNUNET_free_non_null (plugin->proxy_password);
2186 GNUNET_free (plugin);
2195 * @param plugin the plugin handle
2196 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2199 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
2201 unsigned long long max_requests;
2204 /* Optional parameters */
2206 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2211 plugin->max_requests = max_requests;
2213 LOG (GNUNET_ERROR_TYPE_DEBUG,
2214 _("Maximum number of requests is %u\n"),
2215 plugin->max_requests);
2217 /* Read proxy configuration */
2219 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2222 &plugin->proxy_hostname))
2224 LOG (GNUNET_ERROR_TYPE_DEBUG,
2225 "Found proxy host: `%s'\n",
2226 plugin->proxy_hostname);
2227 /* proxy username */
2229 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2232 &plugin->proxy_username))
2234 LOG (GNUNET_ERROR_TYPE_DEBUG,
2235 "Found proxy username name: `%s'\n",
2236 plugin->proxy_username);
2239 /* proxy password */
2241 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2244 &plugin->proxy_password))
2246 LOG (GNUNET_ERROR_TYPE_DEBUG,
2247 "Found proxy password name: `%s'\n",
2248 plugin->proxy_password);
2253 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2258 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
2260 if (0 == strcmp(proxy_type, "HTTP"))
2261 plugin->proxytype = CURLPROXY_HTTP;
2262 else if (0 == strcmp(proxy_type, "SOCKS4"))
2263 plugin->proxytype = CURLPROXY_SOCKS4;
2264 else if (0 == strcmp(proxy_type, "SOCKS5"))
2265 plugin->proxytype = CURLPROXY_SOCKS5;
2266 else if (0 == strcmp(proxy_type, "SOCKS4A"))
2267 plugin->proxytype = CURLPROXY_SOCKS4A;
2268 else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
2269 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
2272 LOG (GNUNET_ERROR_TYPE_ERROR,
2273 _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
2276 GNUNET_free (proxy_type);
2277 GNUNET_free (plugin->proxy_hostname);
2278 plugin->proxy_hostname = NULL;
2279 GNUNET_free_non_null (plugin->proxy_username);
2280 plugin->proxy_username = NULL;
2281 GNUNET_free_non_null (plugin->proxy_password);
2282 plugin->proxy_password = NULL;
2284 return GNUNET_SYSERR;
2287 LOG (GNUNET_ERROR_TYPE_DEBUG,
2288 "Found proxy type: `%s'\n",
2292 /* proxy http tunneling */
2293 plugin->proxy_use_httpproxytunnel
2294 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2296 "PROXY_HTTP_TUNNELING");
2297 if (GNUNET_SYSERR == plugin->proxy_use_httpproxytunnel)
2298 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
2300 GNUNET_free_non_null (proxy_type);
2303 /* Should we emulate an XHR client for testing? */
2305 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2313 * Function to convert an address to a human-readable string.
2315 * @param cls closure
2316 * @param addr address to convert
2317 * @param addrlen address length
2318 * @return res string if conversion was successful, NULL otherwise
2321 http_client_plugin_address_to_string (void *cls,
2325 return http_common_plugin_address_to_string (PLUGIN_NAME,
2332 * Function that will be called whenever the transport service wants to
2333 * notify the plugin that a session is still active and in use and
2334 * therefore the session timeout for this session has to be updated
2336 * @param cls closure
2337 * @param peer which peer was the session for
2338 * @param session which session is being updated
2341 http_client_plugin_update_session_timeout (void *cls,
2342 const struct GNUNET_PeerIdentity *peer,
2343 struct GNUNET_ATS_Session *session)
2345 client_reschedule_session_timeout (session);
2350 * Function that will be called whenever the transport service wants to
2351 * notify the plugin that the inbound quota changed and that the plugin
2352 * should update it's delay for the next receive value
2354 * @param cls closure
2355 * @param peer which peer was the session for
2356 * @param s which session is being updated
2357 * @param delay new delay to use for receiving
2360 http_client_plugin_update_inbound_delay (void *cls,
2361 const struct GNUNET_PeerIdentity *peer,
2362 struct GNUNET_ATS_Session *s,
2363 struct GNUNET_TIME_Relative delay)
2365 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
2366 LOG (GNUNET_ERROR_TYPE_DEBUG,
2367 "New inbound delay %s\n",
2368 GNUNET_STRINGS_relative_time_to_string (delay,
2370 if (s->recv_wakeup_task != NULL)
2372 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
2374 = GNUNET_SCHEDULER_add_delayed (delay,
2382 * Return information about the given session to the
2385 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2386 * @param peer peer we send information about
2387 * @param value our `struct GNUNET_ATS_Session` to send information about
2388 * @return #GNUNET_OK (continue to iterate)
2391 send_session_info_iter (void *cls,
2392 const struct GNUNET_PeerIdentity *peer,
2395 struct HTTP_Client_Plugin *plugin = cls;
2396 struct GNUNET_ATS_Session *session = value;
2398 notify_session_monitor (plugin,
2400 GNUNET_TRANSPORT_SS_INIT);
2401 notify_session_monitor (plugin,
2403 GNUNET_TRANSPORT_SS_UP); /* FIXME: or handshake? */
2409 * Begin monitoring sessions of a plugin. There can only
2410 * be one active monitor per plugin (i.e. if there are
2411 * multiple monitors, the transport service needs to
2412 * multiplex the generated events over all of them).
2414 * @param cls closure of the plugin
2415 * @param sic callback to invoke, NULL to disable monitor;
2416 * plugin will being by iterating over all active
2417 * sessions immediately and then enter monitor mode
2418 * @param sic_cls closure for @a sic
2421 http_client_plugin_setup_monitor (void *cls,
2422 GNUNET_TRANSPORT_SessionInfoCallback sic,
2425 struct HTTP_Client_Plugin *plugin = cls;
2428 plugin->sic_cls = sic_cls;
2431 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2432 &send_session_info_iter,
2434 /* signal end of first iteration */
2435 sic (sic_cls, NULL, NULL);
2441 * Entry point for the plugin.
2444 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2446 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2447 struct GNUNET_TRANSPORT_PluginFunctions *api;
2448 struct HTTP_Client_Plugin *plugin;
2450 if (NULL == env->receive)
2452 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2453 initialze the plugin or the API */
2454 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2456 api->address_to_string = &http_client_plugin_address_to_string;
2457 api->string_to_address = &http_common_plugin_string_to_address;
2458 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2462 plugin = GNUNET_new (struct HTTP_Client_Plugin);
2464 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
2466 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2468 api->send = &http_client_plugin_send;
2469 api->disconnect_session = &http_client_plugin_session_disconnect;
2470 api->query_keepalive_factor = &http_client_query_keepalive_factor;
2471 api->disconnect_peer = &http_client_plugin_peer_disconnect;
2472 api->check_address = &http_client_plugin_address_suggested;
2473 api->get_session = &http_client_plugin_get_session;
2474 api->address_to_string = &http_client_plugin_address_to_string;
2475 api->string_to_address = &http_common_plugin_string_to_address;
2476 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2477 api->get_network = &http_client_plugin_get_network;
2478 api->get_network_for_address = &http_client_plugin_get_network_for_address;
2479 api->update_session_timeout = &http_client_plugin_update_session_timeout;
2480 api->update_inbound_delay = &http_client_plugin_update_inbound_delay;
2481 api->setup_monitor = &http_client_plugin_setup_monitor;
2483 plugin->name = "transport-https_client";
2484 plugin->protocol = "https";
2486 plugin->name = "transport-http_client";
2487 plugin->protocol = "http";
2489 plugin->last_tag = 1;
2491 if (GNUNET_SYSERR == client_configure_plugin (plugin))
2493 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2498 if (GNUNET_SYSERR == client_start (plugin))
2500 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2506 /* end of plugin_transport_http_client.c */