2 This file is part of GNUnet
3 (C) 2002-2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http_client.c
23 * @brief HTTP/S client transport plugin
24 * @author Matthias Wachs
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 #include <curl/curl.h>
55 #define LOG(kind,...) GNUNET_log_from(kind, PLUGIN_NAME, __VA_ARGS__)
58 * Encapsulation of all of the state of the plugin.
60 struct HTTP_Client_Plugin;
63 * State of a HTTP PUT request
65 enum HTTP_PUT_REQUEST_STATE
68 * Just created, not yet connected
78 * Paused, nothing to send
83 * Temporary disconnect in progress due to inactivity
88 * Send request while temporary disconnect, reconnect
90 H_TMP_RECONNECT_REQUIRED,
93 * Temporarily disconnected
104 * Message to send using http
109 * next pointer for double linked list
111 struct HTTP_Message *next;
114 * previous pointer for double linked list
116 struct HTTP_Message *prev;
119 * buffer containing data to send
124 * Continuation function to call once the transmission buffer
125 * has again space available. NULL if there is no
126 * continuation to call.
128 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
131 * Closure for @e transmit_cont.
133 void *transmit_cont_cls;
136 * amount of data already sent
149 * Session handle for HTTP(S) connections.
161 * Current state of this request
163 enum HTTP_PUT_REQUEST_STATE state;
166 * The curl easy handle
171 * The related session
178 * Session handle for connections.
183 * The URL to connect to
190 struct GNUNET_HELLO_Address *address;
193 * Pointer to the global plugin struct.
195 struct HTTP_Client_Plugin *plugin;
198 * Handle for the HTTP PUT request.
200 struct RequestHandle put;
203 * Handle for the HTTP GET request.
205 struct RequestHandle get;
208 * next pointer for double linked list
210 struct HTTP_Message *msg_head;
213 * previous pointer for double linked list
215 struct HTTP_Message *msg_tail;
218 * Message stream tokenizer for incoming data
220 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
223 * Session timeout task
225 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
228 * Session timeout task
230 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
233 * Task to wake up client receive handle when receiving is allowed again
235 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
238 * Absolute time when to receive data again.
239 * Used for receive throttling.
241 struct GNUNET_TIME_Absolute next_receive;
244 * When does this session time out.
246 struct GNUNET_TIME_Absolute timeout;
249 * Number of bytes waiting for transmission to this peer.
251 unsigned long long bytes_in_queue;
254 * Outbound overhead due to HTTP connection
255 * Add to next message of this session when calling callback
260 * Number of messages waiting for transmission to this peer.
262 unsigned int msgs_in_queue;
265 * ATS network type in NBO
267 uint32_t ats_address_network_type;
272 * Encapsulation of all of the state of the plugin.
274 struct HTTP_Client_Plugin
279 struct GNUNET_TRANSPORT_PluginEnvironment *env;
284 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
287 * Function to call about session status changes.
289 GNUNET_TRANSPORT_SessionInfoCallback sic;
292 * Closure for @e sic.
307 * Proxy configuration: hostname or ip of the proxy server
309 char *proxy_hostname;
312 * Username for the proxy server
314 char *proxy_username;
317 * Password for the proxy server
319 char *proxy_password;
324 CURLM *curl_multi_handle;
329 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
332 * Type of proxy server:
334 * Valid values as supported by curl:
335 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
336 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
338 curl_proxytype proxytype;
341 * Use proxy tunneling:
342 * Tunnel all operations through a given HTTP instead of have the proxy
343 * evaluate the HTTP request
345 * Default: #GNUNET_NO, #GNUNET_YES experimental
347 int proxy_use_httpproxytunnel;
350 * My options to be included in the address
355 * Maximum number of sockets the plugin can use
356 * Each http connections are two requests
358 unsigned int max_requests;
361 * Current number of sockets the plugin can use
362 * Each http connections are two requests
364 unsigned int cur_requests;
367 * Last used unique HTTP connection tag
382 * Should we emulate an XHR client for testing?
389 * Disconnect a session
391 * @param cls the `struct HTTP_Client_Plugin *`
393 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
396 http_client_plugin_session_disconnect (void *cls, struct Session *s);
400 * If a session monitor is attached, notify it about the new
403 * @param plugin our plugin
404 * @param session session that changed state
405 * @param state new state of the session
408 notify_session_monitor (struct HTTP_Client_Plugin *plugin,
409 struct Session *session,
410 enum GNUNET_TRANSPORT_SessionState state)
412 struct GNUNET_TRANSPORT_SessionInfo info;
414 if (NULL == plugin->sic)
416 memset (&info, 0, sizeof (info));
418 info.is_inbound = GNUNET_NO;
419 info.num_msg_pending = session->msgs_in_queue;
420 info.num_bytes_pending = session->bytes_in_queue;
421 info.receive_delay = session->next_receive;
422 info.session_timeout = session->timeout;
423 info.address = session->address;
424 plugin->sic (plugin->sic_cls,
431 * Delete session @a s.
433 * @param s the session to delete
436 client_delete_session (struct Session *s)
438 struct HTTP_Client_Plugin *plugin = s->plugin;
439 struct HTTP_Message *pos;
440 struct HTTP_Message *next;
443 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
445 GNUNET_SCHEDULER_cancel (s->timeout_task);
446 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
447 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
449 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
451 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
452 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
454 if (GNUNET_SCHEDULER_NO_TASK != s->recv_wakeup_task)
456 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
457 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
459 GNUNET_assert (GNUNET_OK ==
460 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
463 if (NULL != s->put.easyhandle)
465 LOG (GNUNET_ERROR_TYPE_DEBUG,
466 "Session %p/request %p: disconnecting PUT request to peer `%s'\n",
469 GNUNET_i2s (&s->address->peer));
471 /* remove curl handle from multi handle */
472 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
474 GNUNET_break (CURLM_OK == mret);
475 curl_easy_cleanup (s->put.easyhandle);
476 GNUNET_assert (plugin->cur_requests > 0);
477 plugin->cur_requests--;
478 s->put.easyhandle = NULL;
480 if (NULL != s->get.easyhandle)
482 LOG (GNUNET_ERROR_TYPE_DEBUG,
483 "Session %p/request %p: disconnecting GET request to peer `%s'\n",
484 s, s->get.easyhandle,
485 GNUNET_i2s (&s->address->peer));
486 /* remove curl handle from multi handle */
487 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
489 GNUNET_break (CURLM_OK == mret);
490 curl_easy_cleanup (s->get.easyhandle);
491 GNUNET_assert (plugin->cur_requests > 0);
492 plugin->cur_requests--;
493 s->get.easyhandle = NULL;
496 GNUNET_STATISTICS_set (plugin->env->stats,
497 HTTP_STAT_STR_CONNECTIONS,
498 plugin->cur_requests,
501 while (NULL != (pos = next))
504 GNUNET_CONTAINER_DLL_remove (s->msg_head,
507 GNUNET_assert (0 < s->msgs_in_queue);
509 GNUNET_assert (pos->size <= s->bytes_in_queue);
510 s->bytes_in_queue -= pos->size;
511 if (NULL != pos->transmit_cont)
512 pos->transmit_cont (pos->transmit_cont_cls,
516 pos->pos + s->overhead);
520 GNUNET_assert (0 == s->msgs_in_queue);
521 GNUNET_assert (0 == s->bytes_in_queue);
522 notify_session_monitor (plugin,
524 GNUNET_TRANSPORT_SS_DONE);
525 if (NULL != s->msg_tk)
527 GNUNET_SERVER_mst_destroy (s->msg_tk);
530 GNUNET_HELLO_address_free (s->address);
531 GNUNET_free (s->url);
537 * Increment session timeout due to activity for session @a s.
539 * @param s the session
542 client_reschedule_session_timeout (struct Session *s)
544 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
545 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
550 * Task performing curl operations
552 * @param cls plugin as closure
553 * @param tc gnunet scheduler task context
556 client_run (void *cls,
557 const struct GNUNET_SCHEDULER_TaskContext *tc);
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 != GNUNET_SCHEDULER_NO_TASK)
584 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
585 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
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 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 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 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,
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;
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 != GNUNET_SCHEDULER_NO_TASK);
806 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
807 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
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,
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 (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
856 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
857 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
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 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 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 SessionClientCtx
931 * Address we are looking for.
933 const struct GNUNET_HELLO_Address *address;
936 * Session that was found.
943 * Locate the seession object for a given address.
945 * @param cls the `struct SessionClientCtx *`
946 * @param key peer identity
947 * @param value the `struct 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 SessionClientCtx *sc_ctx = cls;
956 struct 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 Session *
976 client_lookup_session (struct HTTP_Client_Plugin *plugin,
977 const struct GNUNET_HELLO_Address *address)
979 struct 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 Session *` with the put
996 * @param tc scheduler context
999 client_put_disconnect (void *cls,
1000 const struct GNUNET_SCHEDULER_TaskContext *tc)
1002 struct Session *s = cls;
1004 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
1005 LOG (GNUNET_ERROR_TYPE_DEBUG,
1006 "Session %p/request %p: will be disconnected due to no activity\n",
1007 s, s->put.easyhandle);
1008 s->put.state = H_TMP_DISCONNECTING;
1009 if (NULL != s->put.easyhandle)
1010 curl_easy_pause (s->put.easyhandle,
1012 client_schedule (s->plugin, GNUNET_YES);
1017 * Callback method used with libcurl
1018 * Method is called when libcurl needs to read data during sending
1020 * @param stream pointer where to write data
1021 * @param size size of an individual element
1022 * @param nmemb count of elements that can be written to the buffer
1023 * @param cls our `struct Session`
1024 * @return bytes written to stream, returning 0 will terminate request!
1027 client_send_cb (void *stream,
1032 struct Session *s = cls;
1033 struct HTTP_Client_Plugin *plugin = s->plugin;
1034 struct HTTP_Message *msg = s->msg_head;
1038 if (H_TMP_DISCONNECTING == s->put.state)
1040 LOG (GNUNET_ERROR_TYPE_DEBUG,
1041 "Session %p/request %p: disconnect due to inactivity\n",
1042 s, s->put.easyhandle);
1048 if (GNUNET_YES == plugin->emulate_xhr)
1050 LOG (GNUNET_ERROR_TYPE_DEBUG,
1051 "Session %p/request %p: PUT request finished\n",
1054 s->put.state = H_TMP_DISCONNECTING;
1058 /* We have nothing to send, so pause PUT request */
1059 LOG (GNUNET_ERROR_TYPE_DEBUG,
1060 "Session %p/request %p: nothing to send, suspending\n",
1063 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
1064 &client_put_disconnect, s);
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 memcpy (stream, &msg->buf[msg->pos], len);
1075 if (msg->pos == msg->size)
1077 LOG (GNUNET_ERROR_TYPE_DEBUG,
1078 "Session %p/request %p: sent message with %u bytes sent, removing message from queue\n",
1083 /* Calling transmit continuation */
1084 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1087 GNUNET_assert (0 < s->msgs_in_queue);
1089 GNUNET_assert (msg->size <= s->bytes_in_queue);
1090 s->bytes_in_queue -= msg->size;
1091 if (NULL != msg->transmit_cont)
1092 msg->transmit_cont (msg->transmit_cont_cls,
1096 msg->size + s->overhead);
1100 notify_session_monitor (plugin,
1102 GNUNET_TRANSPORT_SS_UPDATE);
1103 GNUNET_asprintf (&stat_txt,
1104 "# bytes currently in %s_client buffers",
1106 GNUNET_STATISTICS_update (plugin->env->stats,
1110 GNUNET_free (stat_txt);
1111 GNUNET_asprintf (&stat_txt,
1112 "# bytes transmitted via %s_client",
1114 GNUNET_STATISTICS_update (plugin->env->stats,
1118 GNUNET_free (stat_txt);
1124 * Wake up a curl handle which was suspended
1126 * @param cls the session
1127 * @param tc task context
1130 client_wake_up (void *cls,
1131 const struct GNUNET_SCHEDULER_TaskContext *tc)
1133 struct Session *s = cls;
1135 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1136 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1138 LOG (GNUNET_ERROR_TYPE_DEBUG,
1139 "Session %p/request %p: Waking up GET handle\n",
1140 s, s->get.easyhandle);
1141 if (H_PAUSED == s->put.state)
1143 /* PUT request was paused, unpause */
1144 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
1145 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
1146 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
1147 s->put.state = H_CONNECTED;
1148 if (NULL != s->put.easyhandle)
1149 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
1151 if (NULL != s->get.easyhandle)
1152 curl_easy_pause (s->get.easyhandle, CURLPAUSE_CONT);
1157 * Callback for message stream tokenizer
1159 * @param cls the session
1160 * @param client not used
1161 * @param message the message received
1162 * @return always #GNUNET_OK
1165 client_receive_mst_cb (void *cls,
1167 const struct GNUNET_MessageHeader *message)
1169 struct Session *s = cls;
1170 struct HTTP_Client_Plugin *plugin;
1171 struct GNUNET_TIME_Relative delay;
1172 struct GNUNET_ATS_Information atsi;
1176 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1177 atsi.value = s->ats_address_network_type;
1178 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1180 delay = s->plugin->env->receive (plugin->env->cls,
1184 plugin->env->update_address_metrics (plugin->env->cls,
1189 GNUNET_asprintf (&stat_txt,
1190 "# bytes received via %s_client",
1192 GNUNET_STATISTICS_update (plugin->env->stats,
1194 ntohs (message->size),
1196 GNUNET_free (stat_txt);
1198 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1199 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
1201 LOG (GNUNET_ERROR_TYPE_DEBUG,
1202 "Client: peer `%s' address `%s' next read delayed for %s\n",
1203 GNUNET_i2s (&s->address->peer),
1204 http_common_plugin_address_to_string (s->plugin->protocol,
1205 s->address->address,
1206 s->address->address_length),
1207 GNUNET_STRINGS_relative_time_to_string (delay,
1210 client_reschedule_session_timeout (s);
1216 * Callback method used with libcurl when data for a PUT request are
1217 * received. We do not expect data here, so we just discard it.
1219 * @param stream pointer where to write data
1220 * @param size size of an individual element
1221 * @param nmemb count of elements that can be written to the buffer
1222 * @param cls destination pointer, passed to the libcurl handle
1223 * @return bytes read from stream
1226 client_receive_put (void *stream,
1231 return size * nmemb;
1236 * Callback method used with libcurl when data for a GET request are
1237 * received. Forward to MST
1239 * @param stream pointer where to write data
1240 * @param size size of an individual element
1241 * @param nmemb count of elements that can be written to the buffer
1242 * @param cls destination pointer, passed to the libcurl handle
1243 * @return bytes read from stream
1246 client_receive (void *stream,
1251 struct Session *s = cls;
1252 struct GNUNET_TIME_Absolute now;
1253 size_t len = size * nmemb;
1255 LOG (GNUNET_ERROR_TYPE_DEBUG,
1256 "Session %p / request %p: Received %u bytes from peer `%s'\n",
1260 GNUNET_i2s (&s->address->peer));
1261 now = GNUNET_TIME_absolute_get ();
1262 if (now.abs_value_us < s->next_receive.abs_value_us)
1264 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1265 struct GNUNET_TIME_Relative delta
1266 = GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1268 LOG (GNUNET_ERROR_TYPE_DEBUG,
1269 "Session %p / request %p: No inbound bandwidth available! Next read was delayed for %s\n",
1272 GNUNET_STRINGS_relative_time_to_string (delta,
1274 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1276 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1277 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1280 = GNUNET_SCHEDULER_add_delayed (delta,
1283 return CURL_WRITEFUNC_PAUSE;
1285 if (NULL == s->msg_tk)
1286 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb,
1288 GNUNET_SERVER_mst_receive (s->msg_tk,
1299 * Task performing curl operations
1301 * @param cls plugin as closure
1302 * @param tc scheduler task context
1305 client_run (void *cls,
1306 const struct GNUNET_SCHEDULER_TaskContext *tc)
1308 struct HTTP_Client_Plugin *plugin = cls;
1310 long http_statuscode;
1313 int put_request; /* GNUNET_YES if easy handle is put, GNUNET_NO for get */
1316 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1317 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1320 /* While data are available or timeouts occured */
1324 /* Perform operations for all handles */
1325 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1327 /* Get additional information for all handles */
1328 while (NULL != (msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1330 CURL *easy_h = msg->easy_handle;
1331 struct Session *s = NULL;
1332 char *d = NULL; /* curl requires 'd' to be a 'char *' */
1334 GNUNET_assert (NULL != easy_h);
1336 /* Obtain session from easy handle */
1337 GNUNET_assert (CURLE_OK == curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1338 s = (struct Session *) d;
1339 GNUNET_assert (NULL != s);
1341 if (msg->msg != CURLMSG_DONE)
1342 continue; /* This should not happen */
1344 /* Get HTTP response code */
1345 GNUNET_break (CURLE_OK == curl_easy_getinfo (easy_h,
1346 CURLINFO_RESPONSE_CODE, &http_statuscode));
1348 if (easy_h == s->put.easyhandle)
1349 put_request = GNUNET_YES;
1351 put_request = GNUNET_NO;
1353 /* Log status of terminated request */
1354 if ((0 != msg->data.result) || (http_statuscode != 200))
1355 LOG (GNUNET_ERROR_TYPE_DEBUG,
1356 "Session %p/request %p: %s request to `%s' ended with status %i reason %i: `%s'\n",
1357 s, msg->easy_handle,
1358 (GNUNET_YES == put_request) ? "PUT" : "GET",
1359 GNUNET_i2s (&s->address->peer),
1362 curl_easy_strerror (msg->data.result));
1364 LOG (GNUNET_ERROR_TYPE_DEBUG,
1365 "Session %p/request %p: %s request to `%s' ended normal\n",
1366 s, msg->easy_handle,
1367 (GNUNET_YES == put_request) ? "PUT" : "GET",
1368 GNUNET_i2s (&s->address->peer));
1370 /* Remove easy handle from multi handle */
1371 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1373 /* Clean up easy handle */
1374 curl_easy_cleanup (easy_h);
1376 /* Remove information */
1377 GNUNET_assert (plugin->cur_requests > 0);
1378 plugin->cur_requests--;
1379 LOG (GNUNET_ERROR_TYPE_INFO,
1380 "%s request to %s done, number of requests decreased to %u\n",
1381 (GNUNET_YES == put_request) ? "PUT" : "GET",
1383 plugin->cur_requests);
1385 if (GNUNET_YES == put_request)
1387 /* Clean up a PUT request */
1388 s->put.easyhandle = NULL;
1391 switch (s->put.state) {
1392 case H_NOT_CONNECTED:
1393 case H_DISCONNECTED:
1394 case H_TMP_DISCONNECTED:
1395 /* This must not happen */
1398 case H_TMP_RECONNECT_REQUIRED:
1399 /* Transport called send while disconnect in progess, reconnect */
1400 if (GNUNET_SYSERR == client_connect_put (s))
1402 /* Reconnect failed, disconnect session */
1403 http_client_plugin_session_disconnect (plugin, s);
1406 case H_TMP_DISCONNECTING:
1407 /* PUT gets temporarily disconnected */
1408 s->put.state = H_TMP_DISCONNECTED;
1412 /* PUT gets permanently disconnected */
1413 s->put.state = H_DISCONNECTED;
1414 http_client_plugin_session_disconnect (plugin, s);
1421 else if (GNUNET_NO == put_request)
1423 /* Clean up a GET request */
1424 s->get.easyhandle = NULL;
1427 /* If we are emulating an XHR client we need to make another GET
1430 if (GNUNET_YES == plugin->emulate_xhr)
1432 if (GNUNET_SYSERR == client_connect_get (s))
1433 http_client_plugin_session_disconnect (plugin, s);
1437 /* GET request was terminated, so disconnect session */
1438 http_client_plugin_session_disconnect (plugin, s);
1442 GNUNET_break (0); /* Must not happen */
1444 GNUNET_STATISTICS_set (plugin->env->stats,
1445 HTTP_STAT_STR_CONNECTIONS,
1446 plugin->cur_requests,
1450 while (mret == CURLM_CALL_MULTI_PERFORM);
1451 client_schedule (plugin, GNUNET_NO);
1455 #ifdef SO_TCPSTEALTH
1457 * Open TCP socket with TCP STEALTH enabled.
1459 * @param clientp our `struct Session *`
1460 * @param purpose why does curl want to open a socket
1461 * @param address what kind of socket does curl want to have opened?
1462 * @return opened socket
1464 static curl_socket_t
1465 open_tcp_stealth_socket_cb (void *clientp,
1466 curlsocktype purpose,
1467 struct curl_sockaddr *address)
1469 struct Session *s = clientp;
1474 case CURLSOCKTYPE_IPCXN:
1475 ret = socket (address->family,
1479 return CURL_SOCKET_BAD;
1480 if ( ( (SOCK_STREAM != address->socktype) ||
1481 ( (0 != address->protocol) &&
1482 (IPPROTO_TCP != address->protocol))) )
1483 return (curl_socket_t) ret;
1484 if ( (0 != setsockopt (ret,
1488 sizeof (struct GNUNET_PeerIdentity))) )
1490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1491 _("TCP_STEALTH not supported on this platform.\n"));
1493 return CURL_SOCKET_BAD;
1495 return (curl_socket_t) ret;
1496 case CURLSOCKTYPE_ACCEPT:
1498 return CURL_SOCKET_BAD;
1500 case CURLSOCKTYPE_LAST:
1502 return CURL_SOCKET_BAD;
1505 return CURL_SOCKET_BAD;
1512 * Connect GET request for a session
1514 * @param s the session to connect
1515 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1518 client_connect_get (struct Session *s)
1521 struct HttpAddress *ha;
1524 ha = (struct HttpAddress *) s->address->address;
1525 options = ntohl (ha->options);
1526 /* create get request */
1527 s->get.easyhandle = curl_easy_init ();
1529 if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
1531 #ifdef SO_TCPSTEALTH
1532 curl_easy_setopt (s->get.easyhandle,
1533 CURLOPT_OPENSOCKETFUNCTION,
1534 &open_tcp_stealth_socket_cb);
1535 curl_easy_setopt (s->get.easyhandle,
1536 CURLOPT_OPENSOCKETDATA,
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1540 "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
1541 curl_easy_cleanup (s->get.easyhandle);
1542 s->get.easyhandle = NULL;
1544 return GNUNET_SYSERR;
1549 curl_easy_setopt (s->get.easyhandle,
1552 curl_easy_setopt (s->get.easyhandle,
1553 CURLOPT_DEBUGFUNCTION,
1555 curl_easy_setopt (s->get.easyhandle,
1560 curl_easy_setopt (s->get.easyhandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1562 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1563 (options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1565 curl_easy_setopt (s->get.easyhandle,
1566 CURLOPT_SSL_VERIFYPEER, 1L);
1567 curl_easy_setopt (s->get.easyhandle,
1568 CURLOPT_SSL_VERIFYHOST,
1573 curl_easy_setopt (s->get.easyhandle,
1574 CURLOPT_SSL_VERIFYPEER,
1576 curl_easy_setopt (s->get.easyhandle,
1577 CURLOPT_SSL_VERIFYHOST,
1581 curl_easy_setopt (s->get.easyhandle,
1584 curl_easy_setopt (s->get.easyhandle,
1585 CURLOPT_REDIR_PROTOCOLS,
1588 curl_easy_setopt (s->get.easyhandle,
1591 curl_easy_setopt (s->get.easyhandle,
1592 CURLOPT_REDIR_PROTOCOLS,
1596 if (NULL != s->plugin->proxy_hostname)
1598 curl_easy_setopt (s->get.easyhandle,
1600 s->plugin->proxy_hostname);
1601 curl_easy_setopt (s->get.easyhandle,
1603 s->plugin->proxytype);
1604 if (NULL != s->plugin->proxy_username)
1605 curl_easy_setopt (s->get.easyhandle,
1606 CURLOPT_PROXYUSERNAME,
1607 s->plugin->proxy_username);
1608 if (NULL != s->plugin->proxy_password)
1609 curl_easy_setopt (s->get.easyhandle,
1610 CURLOPT_PROXYPASSWORD,
1611 s->plugin->proxy_password);
1612 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1613 curl_easy_setopt (s->get.easyhandle,
1614 CURLOPT_HTTPPROXYTUNNEL,
1615 s->plugin->proxy_use_httpproxytunnel);
1618 if (GNUNET_YES == s->plugin->emulate_xhr)
1622 GNUNET_asprintf (&url,
1625 curl_easy_setopt (s->get.easyhandle,
1632 curl_easy_setopt (s->get.easyhandle,
1636 curl_easy_setopt (s->get.easyhandle,
1637 CURLOPT_READFUNCTION,
1639 curl_easy_setopt (s->get.easyhandle,
1642 curl_easy_setopt (s->get.easyhandle,
1643 CURLOPT_WRITEFUNCTION,
1645 curl_easy_setopt (s->get.easyhandle,
1648 /* No timeout by default, timeout done with session timeout */
1649 curl_easy_setopt (s->get.easyhandle,
1652 curl_easy_setopt (s->get.easyhandle,
1653 CURLOPT_PRIVATE, s);
1654 curl_easy_setopt (s->get.easyhandle,
1655 CURLOPT_CONNECTTIMEOUT_MS,
1656 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1657 curl_easy_setopt (s->get.easyhandle, CURLOPT_BUFFERSIZE,
1658 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1659 #if CURL_TCP_NODELAY
1660 curl_easy_setopt (ps->recv_endpoint,
1661 CURLOPT_TCP_NODELAY,
1664 curl_easy_setopt (s->get.easyhandle,
1665 CURLOPT_FOLLOWLOCATION,
1668 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1670 if (CURLM_OK != mret)
1672 LOG (GNUNET_ERROR_TYPE_ERROR,
1673 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1675 curl_multi_strerror (mret));
1676 curl_easy_cleanup (s->get.easyhandle);
1677 s->get.easyhandle = NULL;
1680 return GNUNET_SYSERR;
1682 s->plugin->cur_requests++;
1683 LOG (GNUNET_ERROR_TYPE_INFO,
1684 "GET request `%s' established, number of requests increased to %u\n",
1686 s->plugin->cur_requests);
1692 * Connect a HTTP put request
1694 * @param s the session to connect
1695 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1698 client_connect_put (struct Session *s)
1701 struct HttpAddress *ha;
1704 ha = (struct HttpAddress *) s->address->address;
1705 options = ntohl (ha->options);
1706 /* create put request */
1707 LOG (GNUNET_ERROR_TYPE_DEBUG,
1708 "Session %p: Init PUT handle\n",
1710 s->put.easyhandle = curl_easy_init ();
1713 curl_easy_setopt (s->put.easyhandle,
1716 curl_easy_setopt (s->put.easyhandle,
1717 CURLOPT_DEBUGFUNCTION,
1719 curl_easy_setopt (s->put.easyhandle,
1723 if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
1725 #ifdef SO_TCPSTEALTH
1726 curl_easy_setopt (s->put.easyhandle,
1727 CURLOPT_OPENSOCKETFUNCTION,
1728 &open_tcp_stealth_socket_cb);
1729 curl_easy_setopt (s->put.easyhandle,
1730 CURLOPT_OPENSOCKETDATA,
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734 "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
1735 curl_easy_cleanup (s->put.easyhandle);
1736 s->put.easyhandle = NULL;
1738 s->put.state = H_DISCONNECTED;
1739 return GNUNET_SYSERR;
1743 curl_easy_setopt (s->put.easyhandle,
1745 CURL_SSLVERSION_TLSv1);
1747 struct HttpAddress *ha;
1748 ha = (struct HttpAddress *) s->address->address;
1750 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1751 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1753 curl_easy_setopt (s->put.easyhandle,
1754 CURLOPT_SSL_VERIFYPEER,
1756 curl_easy_setopt (s->put.easyhandle,
1757 CURLOPT_SSL_VERIFYHOST,
1762 curl_easy_setopt (s->put.easyhandle,
1763 CURLOPT_SSL_VERIFYPEER,
1765 curl_easy_setopt (s->put.easyhandle,
1766 CURLOPT_SSL_VERIFYHOST,
1770 curl_easy_setopt (s->put.easyhandle,
1773 curl_easy_setopt (s->put.easyhandle,
1774 CURLOPT_REDIR_PROTOCOLS,
1777 curl_easy_setopt (s->put.easyhandle,
1780 curl_easy_setopt (s->put.easyhandle,
1781 CURLOPT_REDIR_PROTOCOLS,
1784 if (NULL != s->plugin->proxy_hostname)
1786 curl_easy_setopt (s->put.easyhandle,
1788 s->plugin->proxy_hostname);
1789 curl_easy_setopt (s->put.easyhandle,
1791 s->plugin->proxytype);
1792 if (NULL != s->plugin->proxy_username)
1793 curl_easy_setopt (s->put.easyhandle,
1794 CURLOPT_PROXYUSERNAME,
1795 s->plugin->proxy_username);
1796 if (NULL != s->plugin->proxy_password)
1797 curl_easy_setopt (s->put.easyhandle,
1798 CURLOPT_PROXYPASSWORD,
1799 s->plugin->proxy_password);
1800 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1801 curl_easy_setopt (s->put.easyhandle,
1802 CURLOPT_HTTPPROXYTUNNEL,
1803 s->plugin->proxy_use_httpproxytunnel);
1806 curl_easy_setopt (s->put.easyhandle,
1809 curl_easy_setopt (s->put.easyhandle,
1812 curl_easy_setopt (s->put.easyhandle,
1813 CURLOPT_READFUNCTION,
1815 curl_easy_setopt (s->put.easyhandle,
1818 curl_easy_setopt (s->put.easyhandle,
1819 CURLOPT_WRITEFUNCTION,
1820 &client_receive_put);
1821 curl_easy_setopt (s->put.easyhandle,
1824 /* No timeout by default, timeout done with session timeout */
1825 curl_easy_setopt (s->put.easyhandle,
1828 curl_easy_setopt (s->put.easyhandle,
1831 curl_easy_setopt (s->put.easyhandle,
1832 CURLOPT_CONNECTTIMEOUT_MS,
1833 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1834 curl_easy_setopt (s->put.easyhandle, CURLOPT_BUFFERSIZE,
1835 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1836 #if CURL_TCP_NODELAY
1837 curl_easy_setopt (s->put.easyhandle, CURLOPT_TCP_NODELAY, 1);
1839 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1841 if (CURLM_OK != mret)
1843 LOG (GNUNET_ERROR_TYPE_ERROR,
1844 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1845 s, curl_multi_strerror (mret));
1846 curl_easy_cleanup (s->put.easyhandle);
1847 s->put.easyhandle = NULL;
1849 s->put.state = H_DISCONNECTED;
1850 return GNUNET_SYSERR;
1852 s->put.state = H_CONNECTED;
1853 s->plugin->cur_requests++;
1855 LOG (GNUNET_ERROR_TYPE_INFO,
1856 "PUT request `%s' established, number of requests increased to %u\n",
1857 s->url, s->plugin->cur_requests);
1864 * Connect both PUT and GET request for a session
1866 * @param s the session to connect
1867 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1870 client_connect (struct Session *s)
1872 struct HTTP_Client_Plugin *plugin = s->plugin;
1873 int res = GNUNET_OK;
1877 http_common_plugin_address_to_string(plugin->protocol,
1878 s->address->address,
1879 s->address->address_length))
1881 LOG (GNUNET_ERROR_TYPE_DEBUG,
1882 "Invalid address peer `%s'\n",
1883 GNUNET_i2s(&s->address->peer));
1884 return GNUNET_SYSERR;
1887 GNUNET_asprintf(&s->url,
1889 http_common_plugin_address_to_url(NULL,
1890 s->address->address,
1891 s->address->address_length),
1892 GNUNET_i2s_full (plugin->env->my_identity),
1896 LOG (GNUNET_ERROR_TYPE_DEBUG,
1897 "Initiating outbound session peer `%s' using address `%s'\n",
1898 GNUNET_i2s (&s->address->peer), s->url);
1900 if (GNUNET_SYSERR == client_connect_get (s))
1901 return GNUNET_SYSERR;
1902 /* If we are emulating an XHR client then delay sending a PUT request until
1903 * there is something to send.
1905 if (GNUNET_YES == plugin->emulate_xhr)
1907 s->put.state = H_TMP_DISCONNECTED;
1909 else if (GNUNET_SYSERR == client_connect_put (s))
1910 return GNUNET_SYSERR;
1912 LOG (GNUNET_ERROR_TYPE_DEBUG,
1913 "Session %p: connected with GET %p and PUT %p\n",
1914 s, s->get.easyhandle, s->put.easyhandle);
1915 /* Perform connect */
1916 GNUNET_STATISTICS_set (plugin->env->stats,
1917 HTTP_STAT_STR_CONNECTIONS,
1918 plugin->cur_requests,
1920 /* Re-schedule since handles have changed */
1921 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1923 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1924 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1927 /* Schedule task to run immediately */
1928 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1934 * Function obtain the network type for a session
1936 * @param cls closure (`struct Plugin*`)
1937 * @param session the session
1938 * @return the network type
1940 static enum GNUNET_ATS_Network_Type
1941 http_client_plugin_get_network (void *cls,
1942 struct Session *session)
1944 return ntohl (session->ats_address_network_type);
1949 * Session was idle, so disconnect it
1951 * @param cls the `struct Session` of the idle session
1952 * @param tc scheduler context
1955 client_session_timeout (void *cls,
1956 const struct GNUNET_SCHEDULER_TaskContext *tc)
1958 struct Session *s = cls;
1959 struct GNUNET_TIME_Relative left;
1961 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
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 Session *
1995 http_client_plugin_get_session (void *cls,
1996 const struct GNUNET_HELLO_Address *address)
1998 struct HTTP_Client_Plugin *plugin = cls;
2000 struct sockaddr *sa;
2001 struct GNUNET_ATS_Information ats;
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 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2025 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
2026 sa = http_common_socket_from_address (address->address,
2027 address->address_length,
2029 if (GNUNET_SYSERR == res)
2031 if (GNUNET_YES == res)
2033 GNUNET_assert (NULL != sa);
2034 if (AF_INET == sa->sa_family)
2036 salen = sizeof (struct sockaddr_in);
2038 else if (AF_INET6 == sa->sa_family)
2040 salen = sizeof (struct sockaddr_in6);
2042 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
2045 else if (GNUNET_NO == res)
2047 /* Cannot convert to sockaddr -> is external hostname */
2048 ats.value = htonl (GNUNET_ATS_NET_WAN);
2050 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
2056 s = GNUNET_new (struct Session);
2058 s->address = GNUNET_HELLO_address_copy (address);
2059 s->ats_address_network_type = ats.value;
2061 s->put.state = H_NOT_CONNECTED;
2062 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT);
2063 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
2064 &client_session_timeout,
2066 LOG (GNUNET_ERROR_TYPE_DEBUG,
2067 "Created new session %p for `%s' address `%s''\n",
2069 http_common_plugin_address_to_string (plugin->protocol,
2070 s->address->address,
2071 s->address->address_length),
2072 GNUNET_i2s (&s->address->peer));
2074 /* add new session */
2075 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2078 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2079 /* initiate new connection */
2080 if (GNUNET_SYSERR == client_connect (s))
2082 LOG (GNUNET_ERROR_TYPE_ERROR,
2083 "Cannot connect to peer `%s' address `%s''\n",
2084 http_common_plugin_address_to_string (plugin->protocol,
2085 s->address->address, s->address->address_length),
2086 GNUNET_i2s (&s->address->peer));
2087 client_delete_session (s);
2090 notify_session_monitor (plugin,
2092 GNUNET_TRANSPORT_SS_INIT);
2093 notify_session_monitor (plugin,
2095 GNUNET_TRANSPORT_SS_UP); /* or handshake? */
2101 * Setup http_client plugin
2103 * @param plugin the plugin handle
2104 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
2107 client_start (struct HTTP_Client_Plugin *plugin)
2109 curl_global_init (CURL_GLOBAL_ALL);
2110 plugin->curl_multi_handle = curl_multi_init ();
2112 if (NULL == plugin->curl_multi_handle)
2114 LOG (GNUNET_ERROR_TYPE_ERROR,
2115 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
2117 return GNUNET_SYSERR;
2124 * Another peer has suggested an address for this
2125 * peer and transport plugin. Check that this could be a valid
2126 * address. If so, consider adding it to the list
2129 * @param cls closure with the `struct Plugin`
2130 * @param addr pointer to the address
2131 * @param addrlen length of @a addr
2132 * @return #GNUNET_OK if this is a plausible address for this peer
2133 * and transport; always returns #GNUNET_NO (this is the client!)
2136 http_client_plugin_address_suggested (void *cls,
2140 /* A HTTP/S client does not have any valid address so:*/
2146 * Exit point from the plugin.
2148 * @param cls api as closure
2152 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2154 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2155 struct HTTP_Client_Plugin *plugin = api->cls;
2157 if (NULL == api->cls)
2163 LOG (GNUNET_ERROR_TYPE_DEBUG,
2164 _("Shutting down plugin `%s'\n"),
2166 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2167 &destroy_session_cb,
2169 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
2171 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
2172 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
2174 if (NULL != plugin->curl_multi_handle)
2176 curl_multi_cleanup (plugin->curl_multi_handle);
2177 plugin->curl_multi_handle = NULL;
2179 curl_global_cleanup ();
2180 LOG (GNUNET_ERROR_TYPE_DEBUG,
2181 _("Shutdown for plugin `%s' complete\n"),
2183 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
2184 GNUNET_free_non_null (plugin->proxy_hostname);
2185 GNUNET_free_non_null (plugin->proxy_username);
2186 GNUNET_free_non_null (plugin->proxy_password);
2187 GNUNET_free (plugin);
2196 * @param plugin the plugin handle
2197 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2200 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
2202 unsigned long long max_requests;
2205 /* Optional parameters */
2207 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2212 plugin->max_requests = max_requests;
2214 LOG (GNUNET_ERROR_TYPE_DEBUG,
2215 _("Maximum number of requests is %u\n"),
2216 plugin->max_requests);
2218 /* Read proxy configuration */
2220 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2223 &plugin->proxy_hostname))
2225 LOG (GNUNET_ERROR_TYPE_DEBUG,
2226 "Found proxy host: `%s'\n",
2227 plugin->proxy_hostname);
2228 /* proxy username */
2230 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2233 &plugin->proxy_username))
2235 LOG (GNUNET_ERROR_TYPE_DEBUG,
2236 "Found proxy username name: `%s'\n",
2237 plugin->proxy_username);
2240 /* proxy password */
2242 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2245 &plugin->proxy_password))
2247 LOG (GNUNET_ERROR_TYPE_DEBUG,
2248 "Found proxy password name: `%s'\n",
2249 plugin->proxy_password);
2254 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2259 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
2261 if (0 == strcmp(proxy_type, "HTTP"))
2262 plugin->proxytype = CURLPROXY_HTTP;
2263 else if (0 == strcmp(proxy_type, "SOCKS4"))
2264 plugin->proxytype = CURLPROXY_SOCKS4;
2265 else if (0 == strcmp(proxy_type, "SOCKS5"))
2266 plugin->proxytype = CURLPROXY_SOCKS5;
2267 else if (0 == strcmp(proxy_type, "SOCKS4A"))
2268 plugin->proxytype = CURLPROXY_SOCKS4A;
2269 else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
2270 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
2273 LOG (GNUNET_ERROR_TYPE_ERROR,
2274 _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
2277 GNUNET_free (proxy_type);
2278 GNUNET_free (plugin->proxy_hostname);
2279 plugin->proxy_hostname = NULL;
2280 GNUNET_free_non_null (plugin->proxy_username);
2281 plugin->proxy_username = NULL;
2282 GNUNET_free_non_null (plugin->proxy_password);
2283 plugin->proxy_password = NULL;
2285 return GNUNET_SYSERR;
2288 LOG (GNUNET_ERROR_TYPE_DEBUG,
2289 "Found proxy type: `%s'\n",
2293 /* proxy http tunneling */
2294 plugin->proxy_use_httpproxytunnel
2295 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2297 "PROXY_HTTP_TUNNELING");
2298 if (GNUNET_SYSERR == plugin->proxy_use_httpproxytunnel)
2299 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
2301 GNUNET_free_non_null (proxy_type);
2304 /* Should we emulate an XHR client for testing? */
2306 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2314 * Function to convert an address to a human-readable string.
2316 * @param cls closure
2317 * @param addr address to convert
2318 * @param addrlen address length
2319 * @return res string if conversion was successful, NULL otherwise
2322 http_client_plugin_address_to_string (void *cls,
2326 return http_common_plugin_address_to_string (PLUGIN_NAME,
2333 * Function that will be called whenever the transport service wants to
2334 * notify the plugin that a session is still active and in use and
2335 * therefore the session timeout for this session has to be updated
2337 * @param cls closure
2338 * @param peer which peer was the session for
2339 * @param session which session is being updated
2342 http_client_plugin_update_session_timeout (void *cls,
2343 const struct GNUNET_PeerIdentity *peer,
2344 struct Session *session)
2346 client_reschedule_session_timeout (session);
2351 * Function that will be called whenever the transport service wants to
2352 * notify the plugin that the inbound quota changed and that the plugin
2353 * should update it's delay for the next receive value
2355 * @param cls closure
2356 * @param peer which peer was the session for
2357 * @param s which session is being updated
2358 * @param delay new delay to use for receiving
2361 http_client_plugin_update_inbound_delay (void *cls,
2362 const struct GNUNET_PeerIdentity *peer,
2364 struct GNUNET_TIME_Relative delay)
2366 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
2367 LOG (GNUNET_ERROR_TYPE_DEBUG,
2368 "New inbound delay %s\n",
2369 GNUNET_STRINGS_relative_time_to_string (delay,
2371 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
2373 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
2374 s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed (delay,
2375 &client_wake_up, s);
2381 * Return information about the given session to the
2384 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2385 * @param peer peer we send information about
2386 * @param value our `struct Session` to send information about
2387 * @return #GNUNET_OK (continue to iterate)
2390 send_session_info_iter (void *cls,
2391 const struct GNUNET_PeerIdentity *peer,
2394 struct HTTP_Client_Plugin *plugin = cls;
2395 struct Session *session = value;
2397 notify_session_monitor (plugin,
2399 GNUNET_TRANSPORT_SS_INIT);
2400 notify_session_monitor (plugin,
2402 GNUNET_TRANSPORT_SS_UP); /* FIXME: or handshake? */
2408 * Begin monitoring sessions of a plugin. There can only
2409 * be one active monitor per plugin (i.e. if there are
2410 * multiple monitors, the transport service needs to
2411 * multiplex the generated events over all of them).
2413 * @param cls closure of the plugin
2414 * @param sic callback to invoke, NULL to disable monitor;
2415 * plugin will being by iterating over all active
2416 * sessions immediately and then enter monitor mode
2417 * @param sic_cls closure for @a sic
2420 http_client_plugin_setup_monitor (void *cls,
2421 GNUNET_TRANSPORT_SessionInfoCallback sic,
2424 struct HTTP_Client_Plugin *plugin = cls;
2427 plugin->sic_cls = sic_cls;
2430 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2431 &send_session_info_iter,
2433 /* signal end of first iteration */
2434 sic (sic_cls, NULL, NULL);
2440 * Entry point for the plugin.
2443 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2445 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2446 struct GNUNET_TRANSPORT_PluginFunctions *api;
2447 struct HTTP_Client_Plugin *plugin;
2449 if (NULL == env->receive)
2451 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2452 initialze the plugin or the API */
2453 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2455 api->address_to_string = &http_client_plugin_address_to_string;
2456 api->string_to_address = &http_common_plugin_string_to_address;
2457 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2461 plugin = GNUNET_new (struct HTTP_Client_Plugin);
2463 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
2465 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2467 api->send = &http_client_plugin_send;
2468 api->disconnect_session = &http_client_plugin_session_disconnect;
2469 api->query_keepalive_factor = &http_client_query_keepalive_factor;
2470 api->disconnect_peer = &http_client_plugin_peer_disconnect;
2471 api->check_address = &http_client_plugin_address_suggested;
2472 api->get_session = &http_client_plugin_get_session;
2473 api->address_to_string = &http_client_plugin_address_to_string;
2474 api->string_to_address = &http_common_plugin_string_to_address;
2475 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2476 api->get_network = &http_client_plugin_get_network;
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 */