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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
52 /* Just included for the right curl.h */
53 #include "gnunet_curl_lib.h"
56 #define LOG(kind,...) GNUNET_log_from(kind, PLUGIN_NAME, __VA_ARGS__)
59 * Encapsulation of all of the state of the plugin.
61 struct HTTP_Client_Plugin;
64 * State of a HTTP PUT request
66 enum HTTP_PUT_REQUEST_STATE
69 * Just created, not yet connected
79 * Paused, nothing to send
84 * Temporary disconnect in progress due to inactivity
89 * Send request while temporary disconnect, reconnect
91 H_TMP_RECONNECT_REQUIRED,
94 * Temporarily disconnected
105 * Message to send using http
110 * next pointer for double linked list
112 struct HTTP_Message *next;
115 * previous pointer for double linked list
117 struct HTTP_Message *prev;
120 * buffer containing data to send
125 * Continuation function to call once the transmission buffer
126 * has again space available. NULL if there is no
127 * continuation to call.
129 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
132 * Closure for @e transmit_cont.
134 void *transmit_cont_cls;
137 * amount of data already sent
150 * Session handle for HTTP(S) connections.
152 struct GNUNET_ATS_Session;
162 * Current state of this request
164 enum HTTP_PUT_REQUEST_STATE state;
167 * The curl easy handle
172 * The related session
174 struct GNUNET_ATS_Session *s;
179 * Session handle for connections.
181 struct GNUNET_ATS_Session
184 * The URL to connect to
191 struct GNUNET_HELLO_Address *address;
194 * Pointer to the global plugin struct.
196 struct HTTP_Client_Plugin *plugin;
199 * Handle for the HTTP PUT request.
201 struct RequestHandle put;
204 * Handle for the HTTP GET request.
206 struct RequestHandle get;
209 * next pointer for double linked list
211 struct HTTP_Message *msg_head;
214 * previous pointer for double linked list
216 struct HTTP_Message *msg_tail;
219 * Message stream tokenizer for incoming data
221 struct GNUNET_MessageStreamTokenizer *msg_tk;
224 * Session timeout task
226 struct GNUNET_SCHEDULER_Task *put_disconnect_task;
229 * Session timeout task
231 struct GNUNET_SCHEDULER_Task *timeout_task;
234 * Task to wake up client receive handle when receiving is allowed again
236 struct GNUNET_SCHEDULER_Task *recv_wakeup_task;
239 * Absolute time when to receive data again.
240 * Used for receive throttling.
242 struct GNUNET_TIME_Absolute next_receive;
245 * When does this session time out.
247 struct GNUNET_TIME_Absolute timeout;
250 * Number of bytes waiting for transmission to this peer.
252 unsigned long long bytes_in_queue;
255 * Outbound overhead due to HTTP connection
256 * Add to next message of this session when calling callback
261 * Number of messages waiting for transmission to this peer.
263 unsigned int msgs_in_queue;
268 enum GNUNET_NetworkType scope;
273 * Encapsulation of all of the state of the plugin.
275 struct HTTP_Client_Plugin
280 struct GNUNET_TRANSPORT_PluginEnvironment *env;
285 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
288 * Function to call about session status changes.
290 GNUNET_TRANSPORT_SessionInfoCallback sic;
293 * Closure for @e sic.
308 * Proxy configuration: hostname or ip of the proxy server
310 char *proxy_hostname;
313 * Username for the proxy server
315 char *proxy_username;
318 * Password for the proxy server
320 char *proxy_password;
325 CURLM *curl_multi_handle;
330 struct GNUNET_SCHEDULER_Task * client_perform_task;
333 * Type of proxy server:
335 * Valid values as supported by curl:
336 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
337 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
339 curl_proxytype proxytype;
342 * Use proxy tunneling:
343 * Tunnel all operations through a given HTTP instead of have the proxy
344 * evaluate the HTTP request
346 * Default: #GNUNET_NO, #GNUNET_YES experimental
348 int proxy_use_httpproxytunnel;
351 * My options to be included in the address
356 * Maximum number of sockets the plugin can use
357 * Each http connections are two requests
359 unsigned int max_requests;
362 * Current number of sockets the plugin can use
363 * Each http connections are two requests
365 unsigned int cur_requests;
368 * Last used unique HTTP connection tag
383 * Should we emulate an XHR client for testing?
390 * Disconnect a session
392 * @param cls the `struct HTTP_Client_Plugin *`
394 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
397 http_client_plugin_session_disconnect (void *cls, struct GNUNET_ATS_Session *s);
401 * If a session monitor is attached, notify it about the new
404 * @param plugin our plugin
405 * @param session session that changed state
406 * @param state new state of the session
409 notify_session_monitor (struct HTTP_Client_Plugin *plugin,
410 struct GNUNET_ATS_Session *session,
411 enum GNUNET_TRANSPORT_SessionState state)
413 struct GNUNET_TRANSPORT_SessionInfo info;
415 if (NULL == plugin->sic)
417 memset (&info, 0, sizeof (info));
419 info.is_inbound = GNUNET_NO;
420 info.num_msg_pending = session->msgs_in_queue;
421 info.num_bytes_pending = session->bytes_in_queue;
422 info.receive_delay = session->next_receive;
423 info.session_timeout = session->timeout;
424 info.address = session->address;
425 plugin->sic (plugin->sic_cls,
432 * Delete session @a s.
434 * @param s the session to delete
437 client_delete_session (struct GNUNET_ATS_Session *s)
439 struct HTTP_Client_Plugin *plugin = s->plugin;
440 struct HTTP_Message *pos;
441 struct HTTP_Message *next;
444 if (NULL != s->timeout_task)
446 GNUNET_SCHEDULER_cancel (s->timeout_task);
447 s->timeout_task = NULL;
448 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
450 if (NULL != s->put_disconnect_task)
452 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
453 s->put_disconnect_task = NULL;
455 if (NULL != s->recv_wakeup_task)
457 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
458 s->recv_wakeup_task = NULL;
460 GNUNET_assert (GNUNET_OK ==
461 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
464 if (NULL != s->put.easyhandle)
466 LOG (GNUNET_ERROR_TYPE_DEBUG,
467 "Session %p/request %p: disconnecting PUT request to peer `%s'\n",
470 GNUNET_i2s (&s->address->peer));
472 /* remove curl handle from multi handle */
473 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
475 GNUNET_break (CURLM_OK == mret);
476 curl_easy_cleanup (s->put.easyhandle);
477 GNUNET_assert (plugin->cur_requests > 0);
478 plugin->cur_requests--;
479 s->put.easyhandle = NULL;
481 if (NULL != s->get.easyhandle)
483 LOG (GNUNET_ERROR_TYPE_DEBUG,
484 "Session %p/request %p: disconnecting GET request to peer `%s'\n",
485 s, s->get.easyhandle,
486 GNUNET_i2s (&s->address->peer));
487 /* remove curl handle from multi handle */
488 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
490 GNUNET_break (CURLM_OK == mret);
491 curl_easy_cleanup (s->get.easyhandle);
492 GNUNET_assert (plugin->cur_requests > 0);
493 plugin->cur_requests--;
494 s->get.easyhandle = NULL;
497 GNUNET_STATISTICS_set (plugin->env->stats,
498 HTTP_STAT_STR_CONNECTIONS,
499 plugin->cur_requests,
502 while (NULL != (pos = next))
505 GNUNET_CONTAINER_DLL_remove (s->msg_head,
508 GNUNET_assert (0 < s->msgs_in_queue);
510 GNUNET_assert (pos->size <= s->bytes_in_queue);
511 s->bytes_in_queue -= pos->size;
512 if (NULL != pos->transmit_cont)
513 pos->transmit_cont (pos->transmit_cont_cls,
517 pos->pos + s->overhead);
521 GNUNET_assert (0 == s->msgs_in_queue);
522 GNUNET_assert (0 == s->bytes_in_queue);
523 notify_session_monitor (plugin,
525 GNUNET_TRANSPORT_SS_DONE);
526 if (NULL != s->msg_tk)
528 GNUNET_MST_destroy (s->msg_tk);
531 GNUNET_HELLO_address_free (s->address);
532 GNUNET_free (s->url);
538 * Increment session timeout due to activity for session @a s.
540 * @param s the session
543 client_reschedule_session_timeout (struct GNUNET_ATS_Session *s)
545 GNUNET_assert (NULL != s->timeout_task);
546 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
551 * Task performing curl operations
553 * @param cls plugin as closure
554 * @param tc gnunet scheduler task context
557 client_run (void *cls);
561 * Function setting up file descriptors and scheduling task to run
563 * @param plugin the plugin as closure
564 * @param now schedule task in 1ms, regardless of what curl may say
565 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
568 client_schedule (struct HTTP_Client_Plugin *plugin,
575 struct GNUNET_NETWORK_FDSet *grs;
576 struct GNUNET_NETWORK_FDSet *gws;
579 struct GNUNET_TIME_Relative timeout;
581 /* Cancel previous scheduled task */
582 if (plugin->client_perform_task != NULL)
584 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
585 plugin->client_perform_task = NULL;
591 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
592 if (mret != CURLM_OK)
594 LOG (GNUNET_ERROR_TYPE_ERROR,
595 _("%s failed at %s:%d: `%s'\n"),
599 curl_multi_strerror (mret));
600 return GNUNET_SYSERR;
602 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
604 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
606 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
607 if (now == GNUNET_YES)
608 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
610 if (CURLM_OK != mret)
612 LOG (GNUNET_ERROR_TYPE_ERROR,
613 _("%s failed at %s:%d: `%s'\n"),
614 "curl_multi_timeout", __FILE__, __LINE__,
615 curl_multi_strerror (mret));
616 return GNUNET_SYSERR;
619 grs = GNUNET_NETWORK_fdset_create ();
620 gws = GNUNET_NETWORK_fdset_create ();
621 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
622 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
624 /* Schedule task to run when select is ready to read or write */
625 plugin->client_perform_task =
626 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
628 &client_run, plugin);
629 GNUNET_NETWORK_fdset_destroy (gws);
630 GNUNET_NETWORK_fdset_destroy (grs);
639 * @param curl the curl easy handle
640 * @param type message type
641 * @param data data to log, NOT a 0-terminated string
642 * @param size data length
643 * @param cls the closure
647 client_log (CURL *curl,
653 struct RequestHandle *ch = cls;
654 const char *ttype = "UNSPECIFIED";
657 if (! ((CURLINFO_TEXT == type) ||
658 (CURLINFO_HEADER_IN == type) ||
659 (CURLINFO_HEADER_OUT == type)))
666 case CURLINFO_HEADER_IN:
669 case CURLINFO_HEADER_OUT:
670 ttype = "HEADER_OUT";
672 GNUNET_assert (NULL != ch);
673 GNUNET_assert (NULL != ch->easyhandle);
674 GNUNET_assert (NULL != ch->s);
675 ch->s->overhead += size;
678 ttype = "UNSPECIFIED";
681 GNUNET_memcpy (text, data, size);
682 if (text[size - 1] == '\n')
689 text[size + 1] = '\0';
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
701 * Connect GET request
703 * @param s the session to connect
704 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
707 client_connect_get (struct GNUNET_ATS_Session *s);
711 * Connect a HTTP put request
713 * @param s the session to connect
714 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
717 client_connect_put (struct GNUNET_ATS_Session *s);
721 * Function that can be used by the transport service to transmit
722 * a message using the plugin. Note that in the case of a
723 * peer disconnecting, the continuation MUST be called
724 * prior to the disconnect notification itself. This function
725 * will be called with this peer's HELLO message to initiate
726 * a fresh connection to another peer.
729 * @param s which session must be used
730 * @param msgbuf the message to transmit
731 * @param msgbuf_size number of bytes in @a msgbuf
732 * @param priority how important is the message (most plugins will
733 * ignore message priority and just FIFO)
734 * @param to how long to wait at most for the transmission (does not
735 * require plugins to discard the message after the timeout,
736 * just advisory for the desired delay; most plugins will ignore
738 * @param cont continuation to call once the message has
739 * been transmitted (or if the transport is ready
740 * for the next transmission call; or if the
741 * peer disconnected...); can be NULL
742 * @param cont_cls closure for @a cont
743 * @return number of bytes used (on the physical network, with overheads);
744 * -1 on hard errors (i.e. address invalid); 0 is a legal value
745 * and does NOT mean that the message was not transmitted (DV)
748 http_client_plugin_send (void *cls,
749 struct GNUNET_ATS_Session *s,
752 unsigned int priority,
753 struct GNUNET_TIME_Relative to,
754 GNUNET_TRANSPORT_TransmitContinuation cont,
757 struct HTTP_Client_Plugin *plugin = cls;
758 struct HTTP_Message *msg;
761 LOG (GNUNET_ERROR_TYPE_DEBUG,
762 "Session %p/request %p: Sending message with %u to peer `%s' \n",
766 GNUNET_i2s (&s->address->peer));
768 /* create new message and schedule */
769 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
770 msg->size = msgbuf_size;
771 msg->buf = (char *) &msg[1];
772 msg->transmit_cont = cont;
773 msg->transmit_cont_cls = cont_cls;
774 GNUNET_memcpy (msg->buf,
777 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head,
781 s->bytes_in_queue += msg->size;
783 GNUNET_asprintf (&stat_txt,
784 "# bytes currently in %s_client buffers",
786 GNUNET_STATISTICS_update (plugin->env->stats,
787 stat_txt, msgbuf_size, GNUNET_NO);
788 GNUNET_free (stat_txt);
789 notify_session_monitor (plugin,
791 GNUNET_TRANSPORT_SS_UPDATE);
792 if (H_TMP_DISCONNECTING == s->put.state)
794 /* PUT request is currently getting disconnected */
795 s->put.state = H_TMP_RECONNECT_REQUIRED;
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "Session %p/request %p: currently disconnecting, reconnecting immediately\n",
802 if (H_PAUSED == s->put.state)
804 /* PUT request was paused, unpause */
805 GNUNET_assert (s->put_disconnect_task != NULL);
806 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
807 s->put_disconnect_task = NULL;
808 LOG (GNUNET_ERROR_TYPE_DEBUG,
809 "Session %p/request %p: unpausing request\n",
810 s, s->put.easyhandle);
811 s->put.state = H_CONNECTED;
812 if (NULL != s->put.easyhandle)
813 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
815 else if (H_TMP_DISCONNECTED == s->put.state)
817 /* PUT request was disconnected, reconnect */
818 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session %p: Reconnecting PUT request\n", s);
819 GNUNET_break (NULL == s->put.easyhandle);
820 if (GNUNET_SYSERR == client_connect_put (s))
822 /* Could not reconnect */
823 http_client_plugin_session_disconnect (plugin, s);
824 return GNUNET_SYSERR;
827 client_schedule (s->plugin, GNUNET_YES);
833 * Disconnect a session
835 * @param cls the `struct HTTP_Client_Plugin *`
837 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
840 http_client_plugin_session_disconnect (void *cls,
841 struct GNUNET_ATS_Session *s)
843 struct HTTP_Client_Plugin *plugin = cls;
845 LOG (GNUNET_ERROR_TYPE_DEBUG,
846 "Session %p: notifying transport about ending session\n",
848 plugin->env->session_end (plugin->env->cls,
851 client_delete_session (s);
853 /* Re-schedule since handles have changed */
854 if (NULL != plugin->client_perform_task)
856 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
857 plugin->client_perform_task = NULL;
859 client_schedule (plugin, GNUNET_YES);
866 * Function that is called to get the keepalive factor.
867 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
868 * calculate the interval between keepalive packets.
870 * @param cls closure with the `struct Plugin`
871 * @return keepalive factor
874 http_client_query_keepalive_factor (void *cls)
881 * Callback to destroys all sessions on exit.
883 * @param cls the `struct HTTP_Client_Plugin *`
884 * @param peer identity of the peer
885 * @param value the `struct GNUNET_ATS_Session *`
886 * @return #GNUNET_OK (continue iterating)
889 destroy_session_cb (void *cls,
890 const struct GNUNET_PeerIdentity *peer,
893 struct HTTP_Client_Plugin *plugin = cls;
894 struct GNUNET_ATS_Session *session = value;
896 http_client_plugin_session_disconnect (plugin, session);
902 * Function that can be used to force the plugin to disconnect
903 * from the given peer and cancel all previous transmissions
904 * (and their continuationc).
907 * @param target peer from which to disconnect
910 http_client_plugin_peer_disconnect (void *cls,
911 const struct GNUNET_PeerIdentity *target)
913 struct HTTP_Client_Plugin *plugin = cls;
915 LOG (GNUNET_ERROR_TYPE_DEBUG,
916 "Transport tells me to disconnect `%s'\n",
917 GNUNET_i2s (target));
918 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
926 * Closure for #session_lookup_client_by_address().
928 struct GNUNET_ATS_SessionClientCtx
931 * Address we are looking for.
933 const struct GNUNET_HELLO_Address *address;
936 * Session that was found.
938 struct GNUNET_ATS_Session *ret;
943 * Locate the seession object for a given address.
945 * @param cls the `struct GNUNET_ATS_SessionClientCtx *`
946 * @param key peer identity
947 * @param value the `struct GNUNET_ATS_Session` to check
948 * @return #GNUNET_NO if found, #GNUNET_OK if not
951 session_lookup_client_by_address (void *cls,
952 const struct GNUNET_PeerIdentity *key,
955 struct GNUNET_ATS_SessionClientCtx *sc_ctx = cls;
956 struct GNUNET_ATS_Session *s = value;
958 if (0 == GNUNET_HELLO_address_cmp (sc_ctx->address,
969 * Check if a sessions exists for an specific address
971 * @param plugin the plugin
972 * @param address the address
973 * @return the session or NULL
975 static struct GNUNET_ATS_Session *
976 client_lookup_session (struct HTTP_Client_Plugin *plugin,
977 const struct GNUNET_HELLO_Address *address)
979 struct GNUNET_ATS_SessionClientCtx sc_ctx;
981 sc_ctx.address = address;
983 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
984 &session_lookup_client_by_address,
991 * When we have nothing to transmit, we pause the HTTP PUT
992 * after a while (so that gnurl stops asking). This task
993 * is the delayed task that actually disconnects the PUT.
995 * @param cls the `struct GNUNET_ATS_Session *` with the put
998 client_put_disconnect (void *cls)
1000 struct GNUNET_ATS_Session *s = cls;
1002 s->put_disconnect_task = NULL;
1003 LOG (GNUNET_ERROR_TYPE_DEBUG,
1004 "Session %p/request %p: will be disconnected due to no activity\n",
1005 s, s->put.easyhandle);
1006 s->put.state = H_TMP_DISCONNECTING;
1007 if (NULL != s->put.easyhandle)
1008 curl_easy_pause (s->put.easyhandle,
1010 client_schedule (s->plugin, GNUNET_YES);
1015 * Callback method used with libcurl
1016 * Method is called when libcurl needs to read data during sending
1018 * @param stream pointer where to write data
1019 * @param size size of an individual element
1020 * @param nmemb count of elements that can be written to the buffer
1021 * @param cls our `struct GNUNET_ATS_Session`
1022 * @return bytes written to stream, returning 0 will terminate request!
1025 client_send_cb (void *stream,
1030 struct GNUNET_ATS_Session *s = cls;
1031 struct HTTP_Client_Plugin *plugin = s->plugin;
1032 struct HTTP_Message *msg = s->msg_head;
1036 if (H_TMP_DISCONNECTING == s->put.state)
1038 LOG (GNUNET_ERROR_TYPE_DEBUG,
1039 "Session %p/request %p: disconnect due to inactivity\n",
1040 s, s->put.easyhandle);
1046 if (GNUNET_YES == plugin->emulate_xhr)
1048 LOG (GNUNET_ERROR_TYPE_DEBUG,
1049 "Session %p/request %p: PUT request finished\n",
1052 s->put.state = H_TMP_DISCONNECTING;
1056 /* We have nothing to send, so pause PUT request */
1057 LOG (GNUNET_ERROR_TYPE_DEBUG,
1058 "Session %p/request %p: nothing to send, suspending\n",
1061 s->put_disconnect_task
1062 = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
1063 &client_put_disconnect,
1065 s->put.state = H_PAUSED;
1066 return CURL_READFUNC_PAUSE;
1069 GNUNET_assert (msg->pos < msg->size);
1070 /* calculate how much fits in buffer */
1071 len = GNUNET_MIN (msg->size - msg->pos,
1073 GNUNET_memcpy (stream,
1074 &msg->buf[msg->pos],
1077 if (msg->pos == msg->size)
1079 LOG (GNUNET_ERROR_TYPE_DEBUG,
1080 "Session %p/request %p: sent message with %u bytes sent, removing message from queue\n",
1085 /* Calling transmit continuation */
1086 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1089 GNUNET_assert (0 < s->msgs_in_queue);
1091 GNUNET_assert (msg->size <= s->bytes_in_queue);
1092 s->bytes_in_queue -= msg->size;
1093 if (NULL != msg->transmit_cont)
1094 msg->transmit_cont (msg->transmit_cont_cls,
1098 msg->size + s->overhead);
1102 notify_session_monitor (plugin,
1104 GNUNET_TRANSPORT_SS_UPDATE);
1105 GNUNET_asprintf (&stat_txt,
1106 "# bytes currently in %s_client buffers",
1108 GNUNET_STATISTICS_update (plugin->env->stats,
1112 GNUNET_free (stat_txt);
1113 GNUNET_asprintf (&stat_txt,
1114 "# bytes transmitted via %s_client",
1116 GNUNET_STATISTICS_update (plugin->env->stats,
1120 GNUNET_free (stat_txt);
1126 * Wake up a curl handle which was suspended
1128 * @param cls the session
1131 client_wake_up (void *cls)
1133 struct GNUNET_ATS_Session *s = cls;
1135 s->recv_wakeup_task = NULL;
1136 LOG (GNUNET_ERROR_TYPE_DEBUG,
1137 "Session %p/request %p: Waking up GET handle\n",
1138 s, s->get.easyhandle);
1139 if (H_PAUSED == s->put.state)
1141 /* PUT request was paused, unpause */
1142 GNUNET_assert (s->put_disconnect_task != NULL);
1143 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
1144 s->put_disconnect_task = NULL;
1145 s->put.state = H_CONNECTED;
1146 if (NULL != s->put.easyhandle)
1147 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
1149 if (NULL != s->get.easyhandle)
1150 curl_easy_pause (s->get.easyhandle, CURLPAUSE_CONT);
1155 * Callback for message stream tokenizer
1157 * @param cls the session
1158 * @param message the message received
1159 * @return always #GNUNET_OK
1162 client_receive_mst_cb (void *cls,
1163 const struct GNUNET_MessageHeader *message)
1165 struct GNUNET_ATS_Session *s = cls;
1166 struct HTTP_Client_Plugin *plugin;
1167 struct GNUNET_TIME_Relative delay;
1171 delay = s->plugin->env->receive (plugin->env->cls,
1175 GNUNET_asprintf (&stat_txt,
1176 "# bytes received via %s_client",
1178 GNUNET_STATISTICS_update (plugin->env->stats,
1180 ntohs (message->size),
1182 GNUNET_free (stat_txt);
1184 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1185 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
1187 LOG (GNUNET_ERROR_TYPE_DEBUG,
1188 "Client: peer `%s' address `%s' next read delayed for %s\n",
1189 GNUNET_i2s (&s->address->peer),
1190 http_common_plugin_address_to_string (s->plugin->protocol,
1191 s->address->address,
1192 s->address->address_length),
1193 GNUNET_STRINGS_relative_time_to_string (delay,
1196 client_reschedule_session_timeout (s);
1202 * Callback method used with libcurl when data for a PUT request are
1203 * received. We do not expect data here, so we just discard it.
1205 * @param stream pointer where to write data
1206 * @param size size of an individual element
1207 * @param nmemb count of elements that can be written to the buffer
1208 * @param cls destination pointer, passed to the libcurl handle
1209 * @return bytes read from stream
1212 client_receive_put (void *stream,
1217 return size * nmemb;
1222 * Callback method used with libcurl when data for a GET request are
1223 * received. Forward to MST
1225 * @param stream pointer where to write data
1226 * @param size size of an individual element
1227 * @param nmemb count of elements that can be written to the buffer
1228 * @param cls destination pointer, passed to the libcurl handle
1229 * @return bytes read from stream
1232 client_receive (void *stream,
1237 struct GNUNET_ATS_Session *s = cls;
1238 struct GNUNET_TIME_Absolute now;
1239 size_t len = size * nmemb;
1241 LOG (GNUNET_ERROR_TYPE_DEBUG,
1242 "Session %p / request %p: Received %u bytes from peer `%s'\n",
1246 GNUNET_i2s (&s->address->peer));
1247 now = GNUNET_TIME_absolute_get ();
1248 if (now.abs_value_us < s->next_receive.abs_value_us)
1250 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1251 struct GNUNET_TIME_Relative delta
1252 = GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "Session %p / request %p: No inbound bandwidth available! Next read was delayed for %s\n",
1258 GNUNET_STRINGS_relative_time_to_string (delta,
1260 if (s->recv_wakeup_task != NULL)
1262 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1263 s->recv_wakeup_task = NULL;
1266 = GNUNET_SCHEDULER_add_delayed (delta,
1269 return CURL_WRITEFUNC_PAUSE;
1271 if (NULL == s->msg_tk)
1272 s->msg_tk = GNUNET_MST_create (&client_receive_mst_cb,
1274 GNUNET_MST_from_buffer (s->msg_tk,
1284 * Task performing curl operations
1286 * @param cls plugin as closure
1289 client_run (void *cls)
1291 struct HTTP_Client_Plugin *plugin = cls;
1293 long http_statuscode;
1296 int put_request; /* GNUNET_YES if easy handle is put, GNUNET_NO for get */
1299 plugin->client_perform_task = NULL;
1300 /* While data are available or timeouts occured */
1304 /* Perform operations for all handles */
1305 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1307 /* Get additional information for all handles */
1308 while (NULL != (msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1310 CURL *easy_h = msg->easy_handle;
1311 struct GNUNET_ATS_Session *s = NULL;
1312 char *d = NULL; /* curl requires 'd' to be a 'char *' */
1314 GNUNET_assert (NULL != easy_h);
1316 /* Obtain session from easy handle */
1317 GNUNET_assert (CURLE_OK == curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1318 s = (struct GNUNET_ATS_Session *) d;
1319 GNUNET_assert (NULL != s);
1321 if (msg->msg != CURLMSG_DONE)
1322 continue; /* This should not happen */
1324 /* Get HTTP response code */
1325 GNUNET_break (CURLE_OK == curl_easy_getinfo (easy_h,
1326 CURLINFO_RESPONSE_CODE, &http_statuscode));
1328 if (easy_h == s->put.easyhandle)
1329 put_request = GNUNET_YES;
1331 put_request = GNUNET_NO;
1333 /* Log status of terminated request */
1334 if ((0 != msg->data.result) || (http_statuscode != 200))
1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1336 "Session %p/request %p: %s request to `%s' ended with status %i reason %i: `%s'\n",
1337 s, msg->easy_handle,
1338 (GNUNET_YES == put_request) ? "PUT" : "GET",
1339 GNUNET_i2s (&s->address->peer),
1342 curl_easy_strerror (msg->data.result));
1344 LOG (GNUNET_ERROR_TYPE_DEBUG,
1345 "Session %p/request %p: %s request to `%s' ended normal\n",
1346 s, msg->easy_handle,
1347 (GNUNET_YES == put_request) ? "PUT" : "GET",
1348 GNUNET_i2s (&s->address->peer));
1350 /* Remove easy handle from multi handle */
1351 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1353 /* Clean up easy handle */
1354 curl_easy_cleanup (easy_h);
1356 /* Remove information */
1357 GNUNET_assert (plugin->cur_requests > 0);
1358 plugin->cur_requests--;
1359 LOG (GNUNET_ERROR_TYPE_INFO,
1360 "%s request to %s done, number of requests decreased to %u\n",
1361 (GNUNET_YES == put_request) ? "PUT" : "GET",
1363 plugin->cur_requests);
1365 if (GNUNET_YES == put_request)
1367 /* Clean up a PUT request */
1368 s->put.easyhandle = NULL;
1371 switch (s->put.state) {
1372 case H_NOT_CONNECTED:
1373 case H_DISCONNECTED:
1374 case H_TMP_DISCONNECTED:
1375 /* This must not happen */
1378 case H_TMP_RECONNECT_REQUIRED:
1379 /* Transport called send while disconnect in progess, reconnect */
1380 if (GNUNET_SYSERR == client_connect_put (s))
1382 /* Reconnect failed, disconnect session */
1383 http_client_plugin_session_disconnect (plugin, s);
1386 case H_TMP_DISCONNECTING:
1387 /* PUT gets temporarily disconnected */
1388 s->put.state = H_TMP_DISCONNECTED;
1392 /* PUT gets permanently disconnected */
1393 s->put.state = H_DISCONNECTED;
1394 http_client_plugin_session_disconnect (plugin, s);
1401 else if (GNUNET_NO == put_request)
1403 /* Clean up a GET request */
1404 s->get.easyhandle = NULL;
1407 /* If we are emulating an XHR client we need to make another GET
1410 if (GNUNET_YES == plugin->emulate_xhr)
1412 if (GNUNET_SYSERR == client_connect_get (s))
1413 http_client_plugin_session_disconnect (plugin, s);
1417 /* GET request was terminated, so disconnect session */
1418 http_client_plugin_session_disconnect (plugin, s);
1422 GNUNET_break (0); /* Must not happen */
1424 GNUNET_STATISTICS_set (plugin->env->stats,
1425 HTTP_STAT_STR_CONNECTIONS,
1426 plugin->cur_requests,
1430 while (mret == CURLM_CALL_MULTI_PERFORM);
1431 client_schedule (plugin, GNUNET_NO);
1437 * Open TCP socket with TCP STEALTH enabled.
1439 * @param clientp our `struct GNUNET_ATS_Session *`
1440 * @param purpose why does curl want to open a socket
1441 * @param address what kind of socket does curl want to have opened?
1442 * @return opened socket
1444 static curl_socket_t
1445 open_tcp_stealth_socket_cb (void *clientp,
1446 curlsocktype purpose,
1447 struct curl_sockaddr *address)
1449 struct GNUNET_ATS_Session *s = clientp;
1454 case CURLSOCKTYPE_IPCXN:
1455 ret = socket (address->family,
1459 return CURL_SOCKET_BAD;
1460 if ( ( (SOCK_STREAM != address->socktype) ||
1461 ( (0 != address->protocol) &&
1462 (IPPROTO_TCP != address->protocol))) )
1463 return (curl_socket_t) ret;
1464 if ( (0 != setsockopt (ret,
1468 sizeof (struct GNUNET_PeerIdentity))) )
1470 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1471 _("TCP_STEALTH not supported on this platform.\n"));
1473 return CURL_SOCKET_BAD;
1475 return (curl_socket_t) ret;
1476 case CURLSOCKTYPE_ACCEPT:
1478 return CURL_SOCKET_BAD;
1480 case CURLSOCKTYPE_LAST:
1482 return CURL_SOCKET_BAD;
1485 return CURL_SOCKET_BAD;
1492 * Connect GET request for a session
1494 * @param s the session to connect
1495 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1498 client_connect_get (struct GNUNET_ATS_Session *s)
1501 struct HttpAddress *ha;
1504 ha = (struct HttpAddress *) s->address->address;
1505 options = ntohl (ha->options);
1506 /* create get request */
1507 s->get.easyhandle = curl_easy_init ();
1509 if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
1512 curl_easy_setopt (s->get.easyhandle,
1513 CURLOPT_OPENSOCKETFUNCTION,
1514 &open_tcp_stealth_socket_cb);
1515 curl_easy_setopt (s->get.easyhandle,
1516 CURLOPT_OPENSOCKETDATA,
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1520 "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
1521 curl_easy_cleanup (s->get.easyhandle);
1522 s->get.easyhandle = NULL;
1524 return GNUNET_SYSERR;
1529 curl_easy_setopt (s->get.easyhandle,
1532 curl_easy_setopt (s->get.easyhandle,
1533 CURLOPT_DEBUGFUNCTION,
1535 curl_easy_setopt (s->get.easyhandle,
1540 curl_easy_setopt (s->get.easyhandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1542 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1543 (options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1545 curl_easy_setopt (s->get.easyhandle,
1546 CURLOPT_SSL_VERIFYPEER, 1L);
1547 curl_easy_setopt (s->get.easyhandle,
1548 CURLOPT_SSL_VERIFYHOST,
1553 curl_easy_setopt (s->get.easyhandle,
1554 CURLOPT_SSL_VERIFYPEER,
1556 curl_easy_setopt (s->get.easyhandle,
1557 CURLOPT_SSL_VERIFYHOST,
1561 curl_easy_setopt (s->get.easyhandle,
1564 curl_easy_setopt (s->get.easyhandle,
1565 CURLOPT_REDIR_PROTOCOLS,
1568 curl_easy_setopt (s->get.easyhandle,
1571 curl_easy_setopt (s->get.easyhandle,
1572 CURLOPT_REDIR_PROTOCOLS,
1576 if (NULL != s->plugin->proxy_hostname)
1578 curl_easy_setopt (s->get.easyhandle,
1580 s->plugin->proxy_hostname);
1581 curl_easy_setopt (s->get.easyhandle,
1583 s->plugin->proxytype);
1584 if (NULL != s->plugin->proxy_username)
1585 curl_easy_setopt (s->get.easyhandle,
1586 CURLOPT_PROXYUSERNAME,
1587 s->plugin->proxy_username);
1588 if (NULL != s->plugin->proxy_password)
1589 curl_easy_setopt (s->get.easyhandle,
1590 CURLOPT_PROXYPASSWORD,
1591 s->plugin->proxy_password);
1592 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1593 curl_easy_setopt (s->get.easyhandle,
1594 CURLOPT_HTTPPROXYTUNNEL,
1595 s->plugin->proxy_use_httpproxytunnel);
1598 if (GNUNET_YES == s->plugin->emulate_xhr)
1602 GNUNET_asprintf (&url,
1605 curl_easy_setopt (s->get.easyhandle,
1612 curl_easy_setopt (s->get.easyhandle,
1616 curl_easy_setopt (s->get.easyhandle,
1617 CURLOPT_READFUNCTION,
1619 curl_easy_setopt (s->get.easyhandle,
1622 curl_easy_setopt (s->get.easyhandle,
1623 CURLOPT_WRITEFUNCTION,
1625 curl_easy_setopt (s->get.easyhandle,
1628 /* No timeout by default, timeout done with session timeout */
1629 curl_easy_setopt (s->get.easyhandle,
1632 curl_easy_setopt (s->get.easyhandle,
1633 CURLOPT_PRIVATE, s);
1634 curl_easy_setopt (s->get.easyhandle,
1635 CURLOPT_CONNECTTIMEOUT_MS,
1636 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1637 curl_easy_setopt (s->get.easyhandle, CURLOPT_BUFFERSIZE,
1638 2 * GNUNET_MAX_MESSAGE_SIZE);
1639 #if CURL_TCP_NODELAY
1640 curl_easy_setopt (ps->recv_endpoint,
1641 CURLOPT_TCP_NODELAY,
1644 curl_easy_setopt (s->get.easyhandle,
1645 CURLOPT_FOLLOWLOCATION,
1648 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1650 if (CURLM_OK != mret)
1652 LOG (GNUNET_ERROR_TYPE_ERROR,
1653 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1655 curl_multi_strerror (mret));
1656 curl_easy_cleanup (s->get.easyhandle);
1657 s->get.easyhandle = NULL;
1660 return GNUNET_SYSERR;
1662 s->plugin->cur_requests++;
1663 LOG (GNUNET_ERROR_TYPE_INFO,
1664 "GET request `%s' established, number of requests increased to %u\n",
1666 s->plugin->cur_requests);
1672 * Connect a HTTP put request
1674 * @param s the session to connect
1675 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1678 client_connect_put (struct GNUNET_ATS_Session *s)
1681 struct HttpAddress *ha;
1684 ha = (struct HttpAddress *) s->address->address;
1685 options = ntohl (ha->options);
1686 /* create put request */
1687 LOG (GNUNET_ERROR_TYPE_DEBUG,
1688 "Session %p: Init PUT handle\n",
1690 s->put.easyhandle = curl_easy_init ();
1693 curl_easy_setopt (s->put.easyhandle,
1696 curl_easy_setopt (s->put.easyhandle,
1697 CURLOPT_DEBUGFUNCTION,
1699 curl_easy_setopt (s->put.easyhandle,
1703 if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
1706 curl_easy_setopt (s->put.easyhandle,
1707 CURLOPT_OPENSOCKETFUNCTION,
1708 &open_tcp_stealth_socket_cb);
1709 curl_easy_setopt (s->put.easyhandle,
1710 CURLOPT_OPENSOCKETDATA,
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714 "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
1715 curl_easy_cleanup (s->put.easyhandle);
1716 s->put.easyhandle = NULL;
1718 s->put.state = H_DISCONNECTED;
1719 return GNUNET_SYSERR;
1723 curl_easy_setopt (s->put.easyhandle,
1725 CURL_SSLVERSION_TLSv1);
1727 struct HttpAddress *ha;
1728 ha = (struct HttpAddress *) s->address->address;
1730 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1731 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1733 curl_easy_setopt (s->put.easyhandle,
1734 CURLOPT_SSL_VERIFYPEER,
1736 curl_easy_setopt (s->put.easyhandle,
1737 CURLOPT_SSL_VERIFYHOST,
1742 curl_easy_setopt (s->put.easyhandle,
1743 CURLOPT_SSL_VERIFYPEER,
1745 curl_easy_setopt (s->put.easyhandle,
1746 CURLOPT_SSL_VERIFYHOST,
1750 curl_easy_setopt (s->put.easyhandle,
1753 curl_easy_setopt (s->put.easyhandle,
1754 CURLOPT_REDIR_PROTOCOLS,
1757 curl_easy_setopt (s->put.easyhandle,
1760 curl_easy_setopt (s->put.easyhandle,
1761 CURLOPT_REDIR_PROTOCOLS,
1764 if (NULL != s->plugin->proxy_hostname)
1766 curl_easy_setopt (s->put.easyhandle,
1768 s->plugin->proxy_hostname);
1769 curl_easy_setopt (s->put.easyhandle,
1771 s->plugin->proxytype);
1772 if (NULL != s->plugin->proxy_username)
1773 curl_easy_setopt (s->put.easyhandle,
1774 CURLOPT_PROXYUSERNAME,
1775 s->plugin->proxy_username);
1776 if (NULL != s->plugin->proxy_password)
1777 curl_easy_setopt (s->put.easyhandle,
1778 CURLOPT_PROXYPASSWORD,
1779 s->plugin->proxy_password);
1780 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1781 curl_easy_setopt (s->put.easyhandle,
1782 CURLOPT_HTTPPROXYTUNNEL,
1783 s->plugin->proxy_use_httpproxytunnel);
1786 curl_easy_setopt (s->put.easyhandle,
1789 curl_easy_setopt (s->put.easyhandle,
1792 curl_easy_setopt (s->put.easyhandle,
1793 CURLOPT_READFUNCTION,
1795 curl_easy_setopt (s->put.easyhandle,
1798 curl_easy_setopt (s->put.easyhandle,
1799 CURLOPT_WRITEFUNCTION,
1800 &client_receive_put);
1801 curl_easy_setopt (s->put.easyhandle,
1804 /* No timeout by default, timeout done with session timeout */
1805 curl_easy_setopt (s->put.easyhandle,
1808 curl_easy_setopt (s->put.easyhandle,
1811 curl_easy_setopt (s->put.easyhandle,
1812 CURLOPT_CONNECTTIMEOUT_MS,
1813 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1814 curl_easy_setopt (s->put.easyhandle, CURLOPT_BUFFERSIZE,
1815 2 * GNUNET_MAX_MESSAGE_SIZE);
1816 #if CURL_TCP_NODELAY
1817 curl_easy_setopt (s->put.easyhandle, CURLOPT_TCP_NODELAY, 1);
1819 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1821 if (CURLM_OK != mret)
1823 LOG (GNUNET_ERROR_TYPE_ERROR,
1824 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1825 s, curl_multi_strerror (mret));
1826 curl_easy_cleanup (s->put.easyhandle);
1827 s->put.easyhandle = NULL;
1829 s->put.state = H_DISCONNECTED;
1830 return GNUNET_SYSERR;
1832 s->put.state = H_CONNECTED;
1833 s->plugin->cur_requests++;
1835 LOG (GNUNET_ERROR_TYPE_INFO,
1836 "PUT request `%s' established, number of requests increased to %u\n",
1837 s->url, s->plugin->cur_requests);
1844 * Connect both PUT and GET request for a session
1846 * @param s the session to connect
1847 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1850 client_connect (struct GNUNET_ATS_Session *s)
1852 struct HTTP_Client_Plugin *plugin = s->plugin;
1853 int res = GNUNET_OK;
1857 http_common_plugin_address_to_string(plugin->protocol,
1858 s->address->address,
1859 s->address->address_length))
1861 LOG (GNUNET_ERROR_TYPE_DEBUG,
1862 "Invalid address peer `%s'\n",
1863 GNUNET_i2s(&s->address->peer));
1864 return GNUNET_SYSERR;
1867 GNUNET_asprintf (&s->url,
1869 http_common_plugin_address_to_url (NULL,
1870 s->address->address,
1871 s->address->address_length),
1872 GNUNET_i2s_full (plugin->env->my_identity),
1876 LOG (GNUNET_ERROR_TYPE_DEBUG,
1877 "Initiating outbound session peer `%s' using address `%s'\n",
1878 GNUNET_i2s (&s->address->peer), s->url);
1880 if (GNUNET_SYSERR == client_connect_get (s))
1881 return GNUNET_SYSERR;
1882 /* If we are emulating an XHR client then delay sending a PUT request until
1883 * there is something to send.
1885 if (GNUNET_YES == plugin->emulate_xhr)
1887 s->put.state = H_TMP_DISCONNECTED;
1889 else if (GNUNET_SYSERR == client_connect_put (s))
1890 return GNUNET_SYSERR;
1892 LOG (GNUNET_ERROR_TYPE_DEBUG,
1893 "Session %p: connected with GET %p and PUT %p\n",
1894 s, s->get.easyhandle,
1896 /* Perform connect */
1897 GNUNET_STATISTICS_set (plugin->env->stats,
1898 HTTP_STAT_STR_CONNECTIONS,
1899 plugin->cur_requests,
1901 /* Re-schedule since handles have changed */
1902 if (NULL != plugin->client_perform_task)
1904 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1905 plugin->client_perform_task = NULL;
1908 /* Schedule task to run immediately */
1909 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run,
1916 * Function obtain the network type for a session
1918 * @param cls closure (`struct Plugin*`)
1919 * @param session the session
1920 * @return the network type
1922 static enum GNUNET_NetworkType
1923 http_client_plugin_get_network (void *cls,
1924 struct GNUNET_ATS_Session *session)
1926 return session->scope;
1931 * Function obtain the network type for an address.
1933 * @param cls closure (`struct Plugin *`)
1934 * @param address the address
1935 * @return the network type
1937 static enum GNUNET_NetworkType
1938 http_client_plugin_get_network_for_address (void *cls,
1939 const struct GNUNET_HELLO_Address *address)
1941 struct HTTP_Client_Plugin *plugin = cls;
1943 return http_common_get_network_for_address (plugin->env,
1949 * Session was idle, so disconnect it
1951 * @param cls the `struct GNUNET_ATS_Session` of the idle session
1954 client_session_timeout (void *cls)
1956 struct GNUNET_ATS_Session *s = cls;
1957 struct GNUNET_TIME_Relative left;
1959 s->timeout_task = NULL;
1960 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1961 if (0 != left.rel_value_us)
1963 /* not actually our turn yet, but let's at least update
1964 the monitor, it may think we're about to die ... */
1965 notify_session_monitor (s->plugin,
1967 GNUNET_TRANSPORT_SS_UPDATE);
1968 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1969 &client_session_timeout,
1974 "Session %p was idle for %s, disconnecting\n",
1976 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1978 GNUNET_assert (GNUNET_OK ==
1979 http_client_plugin_session_disconnect (s->plugin,
1985 * Creates a new outbound session the transport service will use to
1986 * send data to the peer
1988 * @param cls the plugin
1989 * @param address the address
1990 * @return the session or NULL of max connections exceeded
1992 static struct GNUNET_ATS_Session *
1993 http_client_plugin_get_session (void *cls,
1994 const struct GNUNET_HELLO_Address *address)
1996 struct HTTP_Client_Plugin *plugin = cls;
1997 struct GNUNET_ATS_Session *s;
1998 struct sockaddr *sa;
1999 enum GNUNET_NetworkType net_type;
2003 GNUNET_assert (NULL != address->address);
2005 /* find existing session */
2006 s = client_lookup_session (plugin, address);
2010 /* create a new session */
2011 if (plugin->max_requests <= plugin->cur_requests)
2013 LOG (GNUNET_ERROR_TYPE_WARNING,
2014 "Maximum number of requests (%u) reached: "
2015 "cannot connect to peer `%s'\n",
2016 plugin->max_requests,
2017 GNUNET_i2s (&address->peer));
2021 /* Determine network location */
2022 net_type = GNUNET_NT_UNSPECIFIED;
2023 sa = http_common_socket_from_address (address->address,
2024 address->address_length,
2026 if (GNUNET_SYSERR == res)
2028 if (GNUNET_YES == res)
2030 GNUNET_assert (NULL != sa);
2031 if (AF_INET == sa->sa_family)
2033 salen = sizeof (struct sockaddr_in);
2035 else if (AF_INET6 == sa->sa_family)
2037 salen = sizeof (struct sockaddr_in6);
2039 net_type = plugin->env->get_address_type (plugin->env->cls, sa, salen);
2042 else if (GNUNET_NO == res)
2044 /* Cannot convert to sockaddr -> is external hostname */
2045 net_type = GNUNET_NT_WAN;
2047 if (GNUNET_NT_UNSPECIFIED == net_type)
2053 s = GNUNET_new (struct GNUNET_ATS_Session);
2055 s->address = GNUNET_HELLO_address_copy (address);
2056 s->scope = net_type;
2058 s->put.state = H_NOT_CONNECTED;
2059 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT);
2060 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
2061 &client_session_timeout,
2063 LOG (GNUNET_ERROR_TYPE_DEBUG,
2064 "Created new session %p for `%s' address `%s''\n",
2066 http_common_plugin_address_to_string (plugin->protocol,
2067 s->address->address,
2068 s->address->address_length),
2069 GNUNET_i2s (&s->address->peer));
2071 /* add new session */
2072 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2075 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2076 /* initiate new connection */
2077 if (GNUNET_SYSERR == client_connect (s))
2079 LOG (GNUNET_ERROR_TYPE_ERROR,
2080 "Cannot connect to peer `%s' address `%s''\n",
2081 http_common_plugin_address_to_string (plugin->protocol,
2082 s->address->address, s->address->address_length),
2083 GNUNET_i2s (&s->address->peer));
2084 client_delete_session (s);
2087 notify_session_monitor (plugin,
2089 GNUNET_TRANSPORT_SS_INIT);
2090 notify_session_monitor (plugin,
2092 GNUNET_TRANSPORT_SS_UP); /* or handshake? */
2098 * Setup http_client plugin
2100 * @param plugin the plugin handle
2101 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
2104 client_start (struct HTTP_Client_Plugin *plugin)
2106 curl_global_init (CURL_GLOBAL_ALL);
2107 plugin->curl_multi_handle = curl_multi_init ();
2109 if (NULL == plugin->curl_multi_handle)
2111 LOG (GNUNET_ERROR_TYPE_ERROR,
2112 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
2114 return GNUNET_SYSERR;
2121 * Another peer has suggested an address for this
2122 * peer and transport plugin. Check that this could be a valid
2123 * address. If so, consider adding it to the list
2126 * @param cls closure with the `struct Plugin`
2127 * @param addr pointer to the address
2128 * @param addrlen length of @a addr
2129 * @return #GNUNET_OK if this is a plausible address for this peer
2130 * and transport; always returns #GNUNET_NO (this is the client!)
2133 http_client_plugin_address_suggested (void *cls,
2137 /* A HTTP/S client does not have any valid address so:*/
2143 * Exit point from the plugin.
2145 * @param cls api as closure
2149 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2151 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2152 struct HTTP_Client_Plugin *plugin = api->cls;
2154 if (NULL == api->cls)
2160 LOG (GNUNET_ERROR_TYPE_DEBUG,
2161 _("Shutting down plugin `%s'\n"),
2163 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2164 &destroy_session_cb,
2166 if (NULL != plugin->client_perform_task)
2168 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
2169 plugin->client_perform_task = NULL;
2171 if (NULL != plugin->curl_multi_handle)
2173 curl_multi_cleanup (plugin->curl_multi_handle);
2174 plugin->curl_multi_handle = NULL;
2176 curl_global_cleanup ();
2177 LOG (GNUNET_ERROR_TYPE_DEBUG,
2178 _("Shutdown for plugin `%s' complete\n"),
2180 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
2181 GNUNET_free_non_null (plugin->proxy_hostname);
2182 GNUNET_free_non_null (plugin->proxy_username);
2183 GNUNET_free_non_null (plugin->proxy_password);
2184 GNUNET_free (plugin);
2193 * @param plugin the plugin handle
2194 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2197 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
2199 unsigned long long max_requests;
2202 /* Optional parameters */
2204 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2209 plugin->max_requests = max_requests;
2211 LOG (GNUNET_ERROR_TYPE_DEBUG,
2212 _("Maximum number of requests is %u\n"),
2213 plugin->max_requests);
2215 /* Read proxy configuration */
2217 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2220 &plugin->proxy_hostname))
2222 LOG (GNUNET_ERROR_TYPE_DEBUG,
2223 "Found proxy host: `%s'\n",
2224 plugin->proxy_hostname);
2225 /* proxy username */
2227 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2230 &plugin->proxy_username))
2232 LOG (GNUNET_ERROR_TYPE_DEBUG,
2233 "Found proxy username name: `%s'\n",
2234 plugin->proxy_username);
2237 /* proxy password */
2239 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2242 &plugin->proxy_password))
2244 LOG (GNUNET_ERROR_TYPE_DEBUG,
2245 "Found proxy password name: `%s'\n",
2246 plugin->proxy_password);
2251 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2256 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
2258 if (0 == strcmp(proxy_type, "HTTP"))
2259 plugin->proxytype = CURLPROXY_HTTP;
2260 else if (0 == strcmp(proxy_type, "SOCKS4"))
2261 plugin->proxytype = CURLPROXY_SOCKS4;
2262 else if (0 == strcmp(proxy_type, "SOCKS5"))
2263 plugin->proxytype = CURLPROXY_SOCKS5;
2264 else if (0 == strcmp(proxy_type, "SOCKS4A"))
2265 plugin->proxytype = CURLPROXY_SOCKS4A;
2266 else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
2267 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
2270 LOG (GNUNET_ERROR_TYPE_ERROR,
2271 _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
2274 GNUNET_free (proxy_type);
2275 GNUNET_free (plugin->proxy_hostname);
2276 plugin->proxy_hostname = NULL;
2277 GNUNET_free_non_null (plugin->proxy_username);
2278 plugin->proxy_username = NULL;
2279 GNUNET_free_non_null (plugin->proxy_password);
2280 plugin->proxy_password = NULL;
2282 return GNUNET_SYSERR;
2285 LOG (GNUNET_ERROR_TYPE_DEBUG,
2286 "Found proxy type: `%s'\n",
2290 /* proxy http tunneling */
2291 plugin->proxy_use_httpproxytunnel
2292 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2294 "PROXY_HTTP_TUNNELING");
2295 if (GNUNET_SYSERR == plugin->proxy_use_httpproxytunnel)
2296 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
2298 GNUNET_free_non_null (proxy_type);
2301 /* Should we emulate an XHR client for testing? */
2303 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2311 * Function to convert an address to a human-readable string.
2313 * @param cls closure
2314 * @param addr address to convert
2315 * @param addrlen address length
2316 * @return res string if conversion was successful, NULL otherwise
2319 http_client_plugin_address_to_string (void *cls,
2323 return http_common_plugin_address_to_string (PLUGIN_NAME,
2330 * Function that will be called whenever the transport service wants to
2331 * notify the plugin that a session is still active and in use and
2332 * therefore the session timeout for this session has to be updated
2334 * @param cls closure
2335 * @param peer which peer was the session for
2336 * @param session which session is being updated
2339 http_client_plugin_update_session_timeout (void *cls,
2340 const struct GNUNET_PeerIdentity *peer,
2341 struct GNUNET_ATS_Session *session)
2343 client_reschedule_session_timeout (session);
2348 * Function that will be called whenever the transport service wants to
2349 * notify the plugin that the inbound quota changed and that the plugin
2350 * should update it's delay for the next receive value
2352 * @param cls closure
2353 * @param peer which peer was the session for
2354 * @param s which session is being updated
2355 * @param delay new delay to use for receiving
2358 http_client_plugin_update_inbound_delay (void *cls,
2359 const struct GNUNET_PeerIdentity *peer,
2360 struct GNUNET_ATS_Session *s,
2361 struct GNUNET_TIME_Relative delay)
2363 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
2364 LOG (GNUNET_ERROR_TYPE_DEBUG,
2365 "New inbound delay %s\n",
2366 GNUNET_STRINGS_relative_time_to_string (delay,
2368 if (s->recv_wakeup_task != NULL)
2370 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
2372 = GNUNET_SCHEDULER_add_delayed (delay,
2380 * Return information about the given session to the
2383 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2384 * @param peer peer we send information about
2385 * @param value our `struct GNUNET_ATS_Session` to send information about
2386 * @return #GNUNET_OK (continue to iterate)
2389 send_session_info_iter (void *cls,
2390 const struct GNUNET_PeerIdentity *peer,
2393 struct HTTP_Client_Plugin *plugin = cls;
2394 struct GNUNET_ATS_Session *session = value;
2396 notify_session_monitor (plugin,
2398 GNUNET_TRANSPORT_SS_INIT);
2399 notify_session_monitor (plugin,
2401 GNUNET_TRANSPORT_SS_UP); /* FIXME: or handshake? */
2407 * Begin monitoring sessions of a plugin. There can only
2408 * be one active monitor per plugin (i.e. if there are
2409 * multiple monitors, the transport service needs to
2410 * multiplex the generated events over all of them).
2412 * @param cls closure of the plugin
2413 * @param sic callback to invoke, NULL to disable monitor;
2414 * plugin will being by iterating over all active
2415 * sessions immediately and then enter monitor mode
2416 * @param sic_cls closure for @a sic
2419 http_client_plugin_setup_monitor (void *cls,
2420 GNUNET_TRANSPORT_SessionInfoCallback sic,
2423 struct HTTP_Client_Plugin *plugin = cls;
2426 plugin->sic_cls = sic_cls;
2429 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2430 &send_session_info_iter,
2432 /* signal end of first iteration */
2433 sic (sic_cls, NULL, NULL);
2439 * Entry point for the plugin.
2442 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2444 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2445 struct GNUNET_TRANSPORT_PluginFunctions *api;
2446 struct HTTP_Client_Plugin *plugin;
2448 if (NULL == env->receive)
2450 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2451 initialze the plugin or the API */
2452 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2454 api->address_to_string = &http_client_plugin_address_to_string;
2455 api->string_to_address = &http_common_plugin_string_to_address;
2456 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2460 plugin = GNUNET_new (struct HTTP_Client_Plugin);
2462 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
2464 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2466 api->send = &http_client_plugin_send;
2467 api->disconnect_session = &http_client_plugin_session_disconnect;
2468 api->query_keepalive_factor = &http_client_query_keepalive_factor;
2469 api->disconnect_peer = &http_client_plugin_peer_disconnect;
2470 api->check_address = &http_client_plugin_address_suggested;
2471 api->get_session = &http_client_plugin_get_session;
2472 api->address_to_string = &http_client_plugin_address_to_string;
2473 api->string_to_address = &http_common_plugin_string_to_address;
2474 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2475 api->get_network = &http_client_plugin_get_network;
2476 api->get_network_for_address = &http_client_plugin_get_network_for_address;
2477 api->update_session_timeout = &http_client_plugin_update_session_timeout;
2478 api->update_inbound_delay = &http_client_plugin_update_inbound_delay;
2479 api->setup_monitor = &http_client_plugin_setup_monitor;
2481 plugin->name = "transport-https_client";
2482 plugin->protocol = "https";
2484 plugin->name = "transport-http_client";
2485 plugin->protocol = "http";
2487 plugin->last_tag = 1;
2489 if (GNUNET_SYSERR == client_configure_plugin (plugin))
2491 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2496 if (GNUNET_SYSERR == client_start (plugin))
2498 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2504 /* end of plugin_transport_http_client.c */