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 \
32 libgnunet_plugin_transport_https_client_init
33 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE \
34 libgnunet_plugin_transport_https_client_done
36 #define PLUGIN_NAME "http_client"
37 #define HTTP_STAT_STR_CONNECTIONS "# HTTP client connections"
38 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT \
39 libgnunet_plugin_transport_http_client_init
40 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE \
41 libgnunet_plugin_transport_http_client_done
44 #define VERBOSE_CURL GNUNET_NO
46 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply ( \
47 GNUNET_TIME_UNIT_SECONDS, 1)
49 #define ENABLE_PUT GNUNET_YES
50 #define ENABLE_GET GNUNET_YES
53 #include "gnunet_util_lib.h"
54 #include "gnunet_protocols.h"
55 #include "gnunet_transport_plugin.h"
56 #include "plugin_transport_http_common.h"
57 /* Just included for the right curl.h */
58 #include "gnunet_curl_lib.h"
61 #define LOG(kind, ...) GNUNET_log_from (kind, PLUGIN_NAME, __VA_ARGS__)
64 * Encapsulation of all of the state of the plugin.
66 struct HTTP_Client_Plugin;
69 * State of a HTTP PUT request
71 enum HTTP_PUT_REQUEST_STATE
74 * Just created, not yet connected
84 * Paused, nothing to send
89 * Temporary disconnect in progress due to inactivity
94 * Send request while temporary disconnect, reconnect
96 H_TMP_RECONNECT_REQUIRED,
99 * Temporarily disconnected
110 * Message to send using http
115 * next pointer for double linked list
117 struct HTTP_Message *next;
120 * previous pointer for double linked list
122 struct HTTP_Message *prev;
125 * buffer containing data to send
130 * Continuation function to call once the transmission buffer
131 * has again space available. NULL if there is no
132 * continuation to call.
134 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
137 * Closure for @e transmit_cont.
139 void *transmit_cont_cls;
142 * amount of data already sent
154 * Session handle for HTTP(S) connections.
156 struct GNUNET_ATS_Session;
166 * Current state of this request
168 enum HTTP_PUT_REQUEST_STATE state;
171 * The curl easy handle
176 * The related session
178 struct GNUNET_ATS_Session *s;
183 * Session handle for connections.
185 struct GNUNET_ATS_Session
188 * The URL to connect to
195 struct GNUNET_HELLO_Address *address;
198 * Pointer to the global plugin struct.
200 struct HTTP_Client_Plugin *plugin;
203 * Handle for the HTTP PUT request.
205 struct RequestHandle put;
208 * Handle for the HTTP GET request.
210 struct RequestHandle get;
213 * next pointer for double linked list
215 struct HTTP_Message *msg_head;
218 * previous pointer for double linked list
220 struct HTTP_Message *msg_tail;
223 * Message stream tokenizer for incoming data
225 struct GNUNET_MessageStreamTokenizer *msg_tk;
228 * Session timeout task
230 struct GNUNET_SCHEDULER_Task *put_disconnect_task;
233 * Session timeout task
235 struct GNUNET_SCHEDULER_Task *timeout_task;
238 * Task to wake up client receive handle when receiving is allowed again
240 struct GNUNET_SCHEDULER_Task *recv_wakeup_task;
243 * Absolute time when to receive data again.
244 * Used for receive throttling.
246 struct GNUNET_TIME_Absolute next_receive;
249 * When does this session time out.
251 struct GNUNET_TIME_Absolute timeout;
254 * Number of bytes waiting for transmission to this peer.
256 unsigned long long bytes_in_queue;
259 * Outbound overhead due to HTTP connection
260 * Add to next message of this session when calling callback
265 * Number of messages waiting for transmission to this peer.
267 unsigned int msgs_in_queue;
272 enum GNUNET_NetworkType scope;
277 * Encapsulation of all of the state of the plugin.
279 struct HTTP_Client_Plugin
284 struct GNUNET_TRANSPORT_PluginEnvironment *env;
289 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
292 * Function to call about session status changes.
294 GNUNET_TRANSPORT_SessionInfoCallback sic;
297 * Closure for @e sic.
312 * Proxy configuration: hostname or ip of the proxy server
314 char *proxy_hostname;
317 * Username for the proxy server
319 char *proxy_username;
322 * Password for the proxy server
324 char *proxy_password;
329 CURLM *curl_multi_handle;
334 struct GNUNET_SCHEDULER_Task *client_perform_task;
337 * Type of proxy server:
339 * Valid values as supported by curl:
340 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
341 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
343 curl_proxytype proxytype;
346 * Use proxy tunneling:
347 * Tunnel all operations through a given HTTP instead of have the proxy
348 * evaluate the HTTP request
350 * Default: #GNUNET_NO, #GNUNET_YES experimental
352 int proxy_use_httpproxytunnel;
355 * My options to be included in the address
360 * Maximum number of sockets the plugin can use
361 * Each http connections are two requests
363 unsigned int max_requests;
366 * Current number of sockets the plugin can use
367 * Each http connections are two requests
369 unsigned int cur_requests;
372 * Last used unique HTTP connection tag
387 * Should we emulate an XHR client for testing?
394 * Disconnect a session
396 * @param cls the `struct HTTP_Client_Plugin *`
398 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
401 http_client_plugin_session_disconnect (void *cls, struct GNUNET_ATS_Session *s);
405 * If a session monitor is attached, notify it about the new
408 * @param plugin our plugin
409 * @param session session that changed state
410 * @param state new state of the session
413 notify_session_monitor (struct HTTP_Client_Plugin *plugin,
414 struct GNUNET_ATS_Session *session,
415 enum GNUNET_TRANSPORT_SessionState state)
417 struct GNUNET_TRANSPORT_SessionInfo info;
419 if (NULL == plugin->sic)
421 memset (&info, 0, sizeof(info));
423 info.is_inbound = GNUNET_NO;
424 info.num_msg_pending = session->msgs_in_queue;
425 info.num_bytes_pending = session->bytes_in_queue;
426 info.receive_delay = session->next_receive;
427 info.session_timeout = session->timeout;
428 info.address = session->address;
429 plugin->sic (plugin->sic_cls,
436 * Delete session @a s.
438 * @param s the session to delete
441 client_delete_session (struct GNUNET_ATS_Session *s)
443 struct HTTP_Client_Plugin *plugin = s->plugin;
444 struct HTTP_Message *pos;
445 struct HTTP_Message *next;
448 if (NULL != s->timeout_task)
450 GNUNET_SCHEDULER_cancel (s->timeout_task);
451 s->timeout_task = NULL;
452 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
454 if (NULL != s->put_disconnect_task)
456 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
457 s->put_disconnect_task = NULL;
459 if (NULL != s->recv_wakeup_task)
461 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
462 s->recv_wakeup_task = NULL;
464 GNUNET_assert (GNUNET_OK ==
465 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
468 if (NULL != s->put.easyhandle)
470 LOG (GNUNET_ERROR_TYPE_DEBUG,
471 "Session %p/request %p: disconnecting PUT request to peer `%s'\n",
474 GNUNET_i2s (&s->address->peer));
476 /* remove curl handle from multi handle */
477 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
479 GNUNET_break (CURLM_OK == mret);
480 curl_easy_cleanup (s->put.easyhandle);
481 GNUNET_assert (plugin->cur_requests > 0);
482 plugin->cur_requests--;
483 s->put.easyhandle = NULL;
485 if (NULL != s->get.easyhandle)
487 LOG (GNUNET_ERROR_TYPE_DEBUG,
488 "Session %p/request %p: disconnecting GET request to peer `%s'\n",
489 s, s->get.easyhandle,
490 GNUNET_i2s (&s->address->peer));
491 /* remove curl handle from multi handle */
492 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
494 GNUNET_break (CURLM_OK == mret);
495 curl_easy_cleanup (s->get.easyhandle);
496 GNUNET_assert (plugin->cur_requests > 0);
497 plugin->cur_requests--;
498 s->get.easyhandle = NULL;
501 GNUNET_STATISTICS_set (plugin->env->stats,
502 HTTP_STAT_STR_CONNECTIONS,
503 plugin->cur_requests,
506 while (NULL != (pos = next))
509 GNUNET_CONTAINER_DLL_remove (s->msg_head,
512 GNUNET_assert (0 < s->msgs_in_queue);
514 GNUNET_assert (pos->size <= s->bytes_in_queue);
515 s->bytes_in_queue -= pos->size;
516 if (NULL != pos->transmit_cont)
517 pos->transmit_cont (pos->transmit_cont_cls,
521 pos->pos + s->overhead);
525 GNUNET_assert (0 == s->msgs_in_queue);
526 GNUNET_assert (0 == s->bytes_in_queue);
527 notify_session_monitor (plugin,
529 GNUNET_TRANSPORT_SS_DONE);
530 if (NULL != s->msg_tk)
532 GNUNET_MST_destroy (s->msg_tk);
535 GNUNET_HELLO_address_free (s->address);
536 GNUNET_free (s->url);
542 * Increment session timeout due to activity for session @a s.
544 * @param s the session
547 client_reschedule_session_timeout (struct GNUNET_ATS_Session *s)
549 GNUNET_assert (NULL != s->timeout_task);
550 s->timeout = GNUNET_TIME_relative_to_absolute (
551 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
556 * Task performing curl operations
558 * @param cls plugin as closure
559 * @param tc gnunet scheduler task context
562 client_run (void *cls);
566 * Function setting up file descriptors and scheduling task to run
568 * @param plugin the plugin as closure
569 * @param now schedule task in 1ms, regardless of what curl may say
570 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
573 client_schedule (struct HTTP_Client_Plugin *plugin,
580 struct GNUNET_NETWORK_FDSet *grs;
581 struct GNUNET_NETWORK_FDSet *gws;
584 struct GNUNET_TIME_Relative timeout;
586 /* Cancel previous scheduled task */
587 if (plugin->client_perform_task != NULL)
589 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
590 plugin->client_perform_task = NULL;
596 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
597 if (mret != CURLM_OK)
599 LOG (GNUNET_ERROR_TYPE_ERROR,
600 _ ("%s failed at %s:%d: `%s'\n"),
604 curl_multi_strerror (mret));
605 return GNUNET_SYSERR;
607 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
609 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
611 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
612 if (now == GNUNET_YES)
613 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
615 if (CURLM_OK != mret)
617 LOG (GNUNET_ERROR_TYPE_ERROR,
618 _ ("%s failed at %s:%d: `%s'\n"),
619 "curl_multi_timeout", __FILE__, __LINE__,
620 curl_multi_strerror (mret));
621 return GNUNET_SYSERR;
624 grs = GNUNET_NETWORK_fdset_create ();
625 gws = GNUNET_NETWORK_fdset_create ();
626 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
627 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
629 /* Schedule task to run when select is ready to read or write */
630 plugin->client_perform_task =
631 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
633 &client_run, plugin);
634 GNUNET_NETWORK_fdset_destroy (gws);
635 GNUNET_NETWORK_fdset_destroy (grs);
644 * @param curl the curl easy handle
645 * @param type message type
646 * @param data data to log, NOT a 0-terminated string
647 * @param size data length
648 * @param cls the closure
652 client_log (CURL *curl,
658 struct RequestHandle *ch = cls;
659 const char *ttype = "UNSPECIFIED";
662 if (! ((CURLINFO_TEXT == type) ||
663 (CURLINFO_HEADER_IN == type) ||
664 (CURLINFO_HEADER_OUT == type)))
672 case CURLINFO_HEADER_IN:
676 case CURLINFO_HEADER_OUT:
677 ttype = "HEADER_OUT";
679 GNUNET_assert (NULL != ch);
680 GNUNET_assert (NULL != ch->easyhandle);
681 GNUNET_assert (NULL != ch->s);
682 ch->s->overhead += size;
686 ttype = "UNSPECIFIED";
689 GNUNET_memcpy (text, data, size);
690 if (text[size - 1] == '\n')
697 text[size + 1] = '\0';
699 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 * Connect GET request
713 * @param s the session to connect
714 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
717 client_connect_get (struct GNUNET_ATS_Session *s);
721 * Connect a HTTP put request
723 * @param s the session to connect
724 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
727 client_connect_put (struct GNUNET_ATS_Session *s);
731 * Function that can be used by the transport service to transmit
732 * a message using the plugin. Note that in the case of a
733 * peer disconnecting, the continuation MUST be called
734 * prior to the disconnect notification itself. This function
735 * will be called with this peer's HELLO message to initiate
736 * a fresh connection to another peer.
739 * @param s which session must be used
740 * @param msgbuf the message to transmit
741 * @param msgbuf_size number of bytes in @a msgbuf
742 * @param priority how important is the message (most plugins will
743 * ignore message priority and just FIFO)
744 * @param to how long to wait at most for the transmission (does not
745 * require plugins to discard the message after the timeout,
746 * just advisory for the desired delay; most plugins will ignore
748 * @param cont continuation to call once the message has
749 * been transmitted (or if the transport is ready
750 * for the next transmission call; or if the
751 * peer disconnected...); can be NULL
752 * @param cont_cls closure for @a cont
753 * @return number of bytes used (on the physical network, with overheads);
754 * -1 on hard errors (i.e. address invalid); 0 is a legal value
755 * and does NOT mean that the message was not transmitted (DV)
758 http_client_plugin_send (void *cls,
759 struct GNUNET_ATS_Session *s,
762 unsigned int priority,
763 struct GNUNET_TIME_Relative to,
764 GNUNET_TRANSPORT_TransmitContinuation cont,
767 struct HTTP_Client_Plugin *plugin = cls;
768 struct HTTP_Message *msg;
771 LOG (GNUNET_ERROR_TYPE_DEBUG,
772 "Session %p/request %p: Sending message with %u to peer `%s' \n",
776 GNUNET_i2s (&s->address->peer));
778 /* create new message and schedule */
779 msg = GNUNET_malloc (sizeof(struct HTTP_Message) + msgbuf_size);
780 msg->size = msgbuf_size;
781 msg->buf = (char *) &msg[1];
782 msg->transmit_cont = cont;
783 msg->transmit_cont_cls = cont_cls;
784 GNUNET_memcpy (msg->buf,
787 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head,
791 s->bytes_in_queue += msg->size;
793 GNUNET_asprintf (&stat_txt,
794 "# bytes currently in %s_client buffers",
796 GNUNET_STATISTICS_update (plugin->env->stats,
797 stat_txt, msgbuf_size, GNUNET_NO);
798 GNUNET_free (stat_txt);
799 notify_session_monitor (plugin,
801 GNUNET_TRANSPORT_SS_UPDATE);
802 if (H_TMP_DISCONNECTING == s->put.state)
804 /* PUT request is currently getting disconnected */
805 s->put.state = H_TMP_RECONNECT_REQUIRED;
806 LOG (GNUNET_ERROR_TYPE_DEBUG,
807 "Session %p/request %p: currently disconnecting, reconnecting immediately\n",
812 if (H_PAUSED == s->put.state)
814 /* PUT request was paused, unpause */
815 GNUNET_assert (s->put_disconnect_task != NULL);
816 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
817 s->put_disconnect_task = NULL;
818 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 "Session %p/request %p: unpausing request\n",
820 s, s->put.easyhandle);
821 s->put.state = H_CONNECTED;
822 if (NULL != s->put.easyhandle)
823 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
825 else if (H_TMP_DISCONNECTED == s->put.state)
827 /* PUT request was disconnected, reconnect */
828 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session %p: Reconnecting PUT request\n", s);
829 GNUNET_break (NULL == s->put.easyhandle);
830 if (GNUNET_SYSERR == client_connect_put (s))
832 /* Could not reconnect */
833 http_client_plugin_session_disconnect (plugin, s);
834 return GNUNET_SYSERR;
837 client_schedule (s->plugin, GNUNET_YES);
843 * Disconnect a session
845 * @param cls the `struct HTTP_Client_Plugin *`
847 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
850 http_client_plugin_session_disconnect (void *cls,
851 struct GNUNET_ATS_Session *s)
853 struct HTTP_Client_Plugin *plugin = cls;
855 LOG (GNUNET_ERROR_TYPE_DEBUG,
856 "Session %p: notifying transport about ending session\n",
858 plugin->env->session_end (plugin->env->cls,
861 client_delete_session (s);
863 /* Re-schedule since handles have changed */
864 if (NULL != plugin->client_perform_task)
866 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
867 plugin->client_perform_task = NULL;
869 client_schedule (plugin, GNUNET_YES);
876 * Function that is called to get the keepalive factor.
877 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
878 * calculate the interval between keepalive packets.
880 * @param cls closure with the `struct Plugin`
881 * @return keepalive factor
884 http_client_query_keepalive_factor (void *cls)
891 * Callback to destroys all sessions on exit.
893 * @param cls the `struct HTTP_Client_Plugin *`
894 * @param peer identity of the peer
895 * @param value the `struct GNUNET_ATS_Session *`
896 * @return #GNUNET_OK (continue iterating)
899 destroy_session_cb (void *cls,
900 const struct GNUNET_PeerIdentity *peer,
903 struct HTTP_Client_Plugin *plugin = cls;
904 struct GNUNET_ATS_Session *session = value;
906 http_client_plugin_session_disconnect (plugin, session);
912 * Function that can be used to force the plugin to disconnect
913 * from the given peer and cancel all previous transmissions
914 * (and their continuationc).
917 * @param target peer from which to disconnect
920 http_client_plugin_peer_disconnect (void *cls,
921 const struct GNUNET_PeerIdentity *target)
923 struct HTTP_Client_Plugin *plugin = cls;
925 LOG (GNUNET_ERROR_TYPE_DEBUG,
926 "Transport tells me to disconnect `%s'\n",
927 GNUNET_i2s (target));
928 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
936 * Closure for #session_lookup_client_by_address().
938 struct GNUNET_ATS_SessionClientCtx
941 * Address we are looking for.
943 const struct GNUNET_HELLO_Address *address;
946 * Session that was found.
948 struct GNUNET_ATS_Session *ret;
953 * Locate the seession object for a given address.
955 * @param cls the `struct GNUNET_ATS_SessionClientCtx *`
956 * @param key peer identity
957 * @param value the `struct GNUNET_ATS_Session` to check
958 * @return #GNUNET_NO if found, #GNUNET_OK if not
961 session_lookup_client_by_address (void *cls,
962 const struct GNUNET_PeerIdentity *key,
965 struct GNUNET_ATS_SessionClientCtx *sc_ctx = cls;
966 struct GNUNET_ATS_Session *s = value;
968 if (0 == GNUNET_HELLO_address_cmp (sc_ctx->address,
979 * Check if a sessions exists for an specific address
981 * @param plugin the plugin
982 * @param address the address
983 * @return the session or NULL
985 static struct GNUNET_ATS_Session *
986 client_lookup_session (struct HTTP_Client_Plugin *plugin,
987 const struct GNUNET_HELLO_Address *address)
989 struct GNUNET_ATS_SessionClientCtx sc_ctx;
991 sc_ctx.address = address;
993 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
994 &session_lookup_client_by_address,
1001 * When we have nothing to transmit, we pause the HTTP PUT
1002 * after a while (so that gnurl stops asking). This task
1003 * is the delayed task that actually disconnects the PUT.
1005 * @param cls the `struct GNUNET_ATS_Session *` with the put
1008 client_put_disconnect (void *cls)
1010 struct GNUNET_ATS_Session *s = cls;
1012 s->put_disconnect_task = NULL;
1013 LOG (GNUNET_ERROR_TYPE_DEBUG,
1014 "Session %p/request %p: will be disconnected due to no activity\n",
1015 s, s->put.easyhandle);
1016 s->put.state = H_TMP_DISCONNECTING;
1017 if (NULL != s->put.easyhandle)
1018 curl_easy_pause (s->put.easyhandle,
1020 client_schedule (s->plugin, GNUNET_YES);
1025 * Callback method used with libcurl
1026 * Method is called when libcurl needs to read data during sending
1028 * @param stream pointer where to write data
1029 * @param size size of an individual element
1030 * @param nmemb count of elements that can be written to the buffer
1031 * @param cls our `struct GNUNET_ATS_Session`
1032 * @return bytes written to stream, returning 0 will terminate request!
1035 client_send_cb (void *stream,
1040 struct GNUNET_ATS_Session *s = cls;
1041 struct HTTP_Client_Plugin *plugin = s->plugin;
1042 struct HTTP_Message *msg = s->msg_head;
1046 if (H_TMP_DISCONNECTING == s->put.state)
1048 LOG (GNUNET_ERROR_TYPE_DEBUG,
1049 "Session %p/request %p: disconnect due to inactivity\n",
1050 s, s->put.easyhandle);
1056 if (GNUNET_YES == plugin->emulate_xhr)
1058 LOG (GNUNET_ERROR_TYPE_DEBUG,
1059 "Session %p/request %p: PUT request finished\n",
1062 s->put.state = H_TMP_DISCONNECTING;
1066 /* We have nothing to send, so pause PUT request */
1067 LOG (GNUNET_ERROR_TYPE_DEBUG,
1068 "Session %p/request %p: nothing to send, suspending\n",
1071 s->put_disconnect_task
1072 = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
1073 &client_put_disconnect,
1075 s->put.state = H_PAUSED;
1076 return CURL_READFUNC_PAUSE;
1079 GNUNET_assert (msg->pos < msg->size);
1080 /* calculate how much fits in buffer */
1081 len = GNUNET_MIN (msg->size - msg->pos,
1083 GNUNET_memcpy (stream,
1084 &msg->buf[msg->pos],
1087 if (msg->pos == msg->size)
1089 LOG (GNUNET_ERROR_TYPE_DEBUG,
1090 "Session %p/request %p: sent message with %u bytes sent, removing message from queue\n",
1095 /* Calling transmit continuation */
1096 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1099 GNUNET_assert (0 < s->msgs_in_queue);
1101 GNUNET_assert (msg->size <= s->bytes_in_queue);
1102 s->bytes_in_queue -= msg->size;
1103 if (NULL != msg->transmit_cont)
1104 msg->transmit_cont (msg->transmit_cont_cls,
1108 msg->size + s->overhead);
1112 notify_session_monitor (plugin,
1114 GNUNET_TRANSPORT_SS_UPDATE);
1115 GNUNET_asprintf (&stat_txt,
1116 "# bytes currently in %s_client buffers",
1118 GNUNET_STATISTICS_update (plugin->env->stats,
1122 GNUNET_free (stat_txt);
1123 GNUNET_asprintf (&stat_txt,
1124 "# bytes transmitted via %s_client",
1126 GNUNET_STATISTICS_update (plugin->env->stats,
1130 GNUNET_free (stat_txt);
1136 * Wake up a curl handle which was suspended
1138 * @param cls the session
1141 client_wake_up (void *cls)
1143 struct GNUNET_ATS_Session *s = cls;
1145 s->recv_wakeup_task = NULL;
1146 LOG (GNUNET_ERROR_TYPE_DEBUG,
1147 "Session %p/request %p: Waking up GET handle\n",
1148 s, s->get.easyhandle);
1149 if (H_PAUSED == s->put.state)
1151 /* PUT request was paused, unpause */
1152 GNUNET_assert (s->put_disconnect_task != NULL);
1153 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
1154 s->put_disconnect_task = NULL;
1155 s->put.state = H_CONNECTED;
1156 if (NULL != s->put.easyhandle)
1157 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
1159 if (NULL != s->get.easyhandle)
1160 curl_easy_pause (s->get.easyhandle, CURLPAUSE_CONT);
1165 * Callback for message stream tokenizer
1167 * @param cls the session
1168 * @param message the message received
1169 * @return always #GNUNET_OK
1172 client_receive_mst_cb (void *cls,
1173 const struct GNUNET_MessageHeader *message)
1175 struct GNUNET_ATS_Session *s = cls;
1176 struct HTTP_Client_Plugin *plugin;
1177 struct GNUNET_TIME_Relative delay;
1181 delay = s->plugin->env->receive (plugin->env->cls,
1185 GNUNET_asprintf (&stat_txt,
1186 "# bytes received via %s_client",
1188 GNUNET_STATISTICS_update (plugin->env->stats,
1190 ntohs (message->size),
1192 GNUNET_free (stat_txt);
1194 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1195 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 "Client: peer `%s' address `%s' next read delayed for %s\n",
1199 GNUNET_i2s (&s->address->peer),
1200 http_common_plugin_address_to_string (s->plugin->protocol,
1201 s->address->address,
1202 s->address->address_length),
1203 GNUNET_STRINGS_relative_time_to_string (delay,
1206 client_reschedule_session_timeout (s);
1212 * Callback method used with libcurl when data for a PUT request are
1213 * received. We do not expect data here, so we just discard it.
1215 * @param stream pointer where to write data
1216 * @param size size of an individual element
1217 * @param nmemb count of elements that can be written to the buffer
1218 * @param cls destination pointer, passed to the libcurl handle
1219 * @return bytes read from stream
1222 client_receive_put (void *stream,
1227 return size * nmemb;
1232 * Callback method used with libcurl when data for a GET request are
1233 * received. Forward to MST
1235 * @param stream pointer where to write data
1236 * @param size size of an individual element
1237 * @param nmemb count of elements that can be written to the buffer
1238 * @param cls destination pointer, passed to the libcurl handle
1239 * @return bytes read from stream
1242 client_receive (void *stream,
1247 struct GNUNET_ATS_Session *s = cls;
1248 struct GNUNET_TIME_Absolute now;
1249 size_t len = size * nmemb;
1251 LOG (GNUNET_ERROR_TYPE_DEBUG,
1252 "Session %p / request %p: Received %u bytes from peer `%s'\n",
1256 GNUNET_i2s (&s->address->peer));
1257 now = GNUNET_TIME_absolute_get ();
1258 if (now.abs_value_us < s->next_receive.abs_value_us)
1260 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1261 struct GNUNET_TIME_Relative delta
1262 = GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1264 LOG (GNUNET_ERROR_TYPE_DEBUG,
1265 "Session %p / request %p: No inbound bandwidth available! Next read was delayed for %s\n",
1268 GNUNET_STRINGS_relative_time_to_string (delta,
1270 if (s->recv_wakeup_task != NULL)
1272 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1273 s->recv_wakeup_task = NULL;
1276 = GNUNET_SCHEDULER_add_delayed (delta,
1279 return CURL_WRITEFUNC_PAUSE;
1281 if (NULL == s->msg_tk)
1282 s->msg_tk = GNUNET_MST_create (&client_receive_mst_cb,
1284 GNUNET_MST_from_buffer (s->msg_tk,
1294 * Task performing curl operations
1296 * @param cls plugin as closure
1299 client_run (void *cls)
1301 struct HTTP_Client_Plugin *plugin = cls;
1303 long http_statuscode;
1306 int put_request; /* GNUNET_YES if easy handle is put, GNUNET_NO for get */
1309 plugin->client_perform_task = NULL;
1310 /* While data are available or timeouts occured */
1314 /* Perform operations for all handles */
1315 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1317 /* Get additional information for all handles */
1318 while (NULL != (msg = curl_multi_info_read (plugin->curl_multi_handle,
1321 CURL *easy_h = msg->easy_handle;
1322 struct GNUNET_ATS_Session *s = NULL;
1323 char *d = NULL; /* curl requires 'd' to be a 'char *' */
1325 GNUNET_assert (NULL != easy_h);
1327 /* Obtain session from easy handle */
1328 GNUNET_assert (CURLE_OK == curl_easy_getinfo (easy_h, CURLINFO_PRIVATE,
1330 s = (struct GNUNET_ATS_Session *) d;
1331 GNUNET_assert (NULL != s);
1333 if (msg->msg != CURLMSG_DONE)
1334 continue; /* This should not happen */
1336 /* Get HTTP response code */
1337 GNUNET_break (CURLE_OK == curl_easy_getinfo (easy_h,
1338 CURLINFO_RESPONSE_CODE,
1341 if (easy_h == s->put.easyhandle)
1342 put_request = GNUNET_YES;
1344 put_request = GNUNET_NO;
1346 /* Log status of terminated request */
1347 if ((0 != msg->data.result) || (http_statuscode != 200))
1348 LOG (GNUNET_ERROR_TYPE_DEBUG,
1349 "Session %p/request %p: %s request to `%s' ended with status %i reason %i: `%s'\n",
1350 s, msg->easy_handle,
1351 (GNUNET_YES == put_request) ? "PUT" : "GET",
1352 GNUNET_i2s (&s->address->peer),
1355 curl_easy_strerror (msg->data.result));
1357 LOG (GNUNET_ERROR_TYPE_DEBUG,
1358 "Session %p/request %p: %s request to `%s' ended normal\n",
1359 s, msg->easy_handle,
1360 (GNUNET_YES == put_request) ? "PUT" : "GET",
1361 GNUNET_i2s (&s->address->peer));
1363 /* Remove easy handle from multi handle */
1364 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1366 /* Clean up easy handle */
1367 curl_easy_cleanup (easy_h);
1369 /* Remove information */
1370 GNUNET_assert (plugin->cur_requests > 0);
1371 plugin->cur_requests--;
1372 LOG (GNUNET_ERROR_TYPE_INFO,
1373 "%s request to %s done, number of requests decreased to %u\n",
1374 (GNUNET_YES == put_request) ? "PUT" : "GET",
1376 plugin->cur_requests);
1378 if (GNUNET_YES == put_request)
1380 /* Clean up a PUT request */
1381 s->put.easyhandle = NULL;
1384 switch (s->put.state)
1386 case H_NOT_CONNECTED:
1387 case H_DISCONNECTED:
1388 case H_TMP_DISCONNECTED:
1389 /* This must not happen */
1393 case H_TMP_RECONNECT_REQUIRED:
1394 /* Transport called send while disconnect in progess, reconnect */
1395 if (GNUNET_SYSERR == client_connect_put (s))
1397 /* Reconnect failed, disconnect session */
1398 http_client_plugin_session_disconnect (plugin, s);
1402 case H_TMP_DISCONNECTING:
1403 /* PUT gets temporarily disconnected */
1404 s->put.state = H_TMP_DISCONNECTED;
1409 /* PUT gets permanently disconnected */
1410 s->put.state = H_DISCONNECTED;
1411 http_client_plugin_session_disconnect (plugin, s);
1419 else if (GNUNET_NO == put_request)
1421 /* Clean up a GET request */
1422 s->get.easyhandle = NULL;
1425 /* If we are emulating an XHR client we need to make another GET
1428 if (GNUNET_YES == plugin->emulate_xhr)
1430 if (GNUNET_SYSERR == client_connect_get (s))
1431 http_client_plugin_session_disconnect (plugin, s);
1435 /* GET request was terminated, so disconnect session */
1436 http_client_plugin_session_disconnect (plugin, s);
1440 GNUNET_break (0); /* Must not happen */
1442 GNUNET_STATISTICS_set (plugin->env->stats,
1443 HTTP_STAT_STR_CONNECTIONS,
1444 plugin->cur_requests,
1448 while (mret == CURLM_CALL_MULTI_PERFORM);
1449 client_schedule (plugin, GNUNET_NO);
1455 * Open TCP socket with TCP STEALTH enabled.
1457 * @param clientp our `struct GNUNET_ATS_Session *`
1458 * @param purpose why does curl want to open a socket
1459 * @param address what kind of socket does curl want to have opened?
1460 * @return opened socket
1462 static curl_socket_t
1463 open_tcp_stealth_socket_cb (void *clientp,
1464 curlsocktype purpose,
1465 struct curl_sockaddr *address)
1467 struct GNUNET_ATS_Session *s = clientp;
1472 case CURLSOCKTYPE_IPCXN:
1473 ret = socket (address->family,
1477 return CURL_SOCKET_BAD;
1478 if (((SOCK_STREAM != address->socktype) ||
1479 ((0 != address->protocol) &&
1480 (IPPROTO_TCP != address->protocol))))
1481 return (curl_socket_t) ret;
1482 if ((0 != setsockopt (ret,
1486 sizeof(struct GNUNET_PeerIdentity))))
1488 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1489 _ ("TCP_STEALTH not supported on this platform.\n"));
1491 return CURL_SOCKET_BAD;
1493 return (curl_socket_t) ret;
1495 case CURLSOCKTYPE_ACCEPT:
1497 return CURL_SOCKET_BAD;
1500 case CURLSOCKTYPE_LAST:
1502 return CURL_SOCKET_BAD;
1506 return CURL_SOCKET_BAD;
1515 * Connect GET request for a session
1517 * @param s the session to connect
1518 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1521 client_connect_get (struct GNUNET_ATS_Session *s)
1524 struct HttpAddress *ha;
1527 ha = (struct HttpAddress *) s->address->address;
1528 options = ntohl (ha->options);
1529 /* create get request */
1530 s->get.easyhandle = curl_easy_init ();
1532 if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
1535 curl_easy_setopt (s->get.easyhandle,
1536 CURLOPT_OPENSOCKETFUNCTION,
1537 &open_tcp_stealth_socket_cb);
1538 curl_easy_setopt (s->get.easyhandle,
1539 CURLOPT_OPENSOCKETDATA,
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
1544 curl_easy_cleanup (s->get.easyhandle);
1545 s->get.easyhandle = NULL;
1547 return GNUNET_SYSERR;
1552 curl_easy_setopt (s->get.easyhandle,
1555 curl_easy_setopt (s->get.easyhandle,
1556 CURLOPT_DEBUGFUNCTION,
1558 curl_easy_setopt (s->get.easyhandle,
1563 curl_easy_setopt (s->get.easyhandle, CURLOPT_SSLVERSION,
1564 CURL_SSLVERSION_TLSv1);
1566 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1567 (options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1569 curl_easy_setopt (s->get.easyhandle,
1570 CURLOPT_SSL_VERIFYPEER, 1L);
1571 curl_easy_setopt (s->get.easyhandle,
1572 CURLOPT_SSL_VERIFYHOST,
1577 curl_easy_setopt (s->get.easyhandle,
1578 CURLOPT_SSL_VERIFYPEER,
1580 curl_easy_setopt (s->get.easyhandle,
1581 CURLOPT_SSL_VERIFYHOST,
1585 curl_easy_setopt (s->get.easyhandle,
1588 curl_easy_setopt (s->get.easyhandle,
1589 CURLOPT_REDIR_PROTOCOLS,
1592 curl_easy_setopt (s->get.easyhandle,
1595 curl_easy_setopt (s->get.easyhandle,
1596 CURLOPT_REDIR_PROTOCOLS,
1600 if (NULL != s->plugin->proxy_hostname)
1602 curl_easy_setopt (s->get.easyhandle,
1604 s->plugin->proxy_hostname);
1605 curl_easy_setopt (s->get.easyhandle,
1607 s->plugin->proxytype);
1608 if (NULL != s->plugin->proxy_username)
1609 curl_easy_setopt (s->get.easyhandle,
1610 CURLOPT_PROXYUSERNAME,
1611 s->plugin->proxy_username);
1612 if (NULL != s->plugin->proxy_password)
1613 curl_easy_setopt (s->get.easyhandle,
1614 CURLOPT_PROXYPASSWORD,
1615 s->plugin->proxy_password);
1616 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1617 curl_easy_setopt (s->get.easyhandle,
1618 CURLOPT_HTTPPROXYTUNNEL,
1619 s->plugin->proxy_use_httpproxytunnel);
1622 if (GNUNET_YES == s->plugin->emulate_xhr)
1626 GNUNET_asprintf (&url,
1629 curl_easy_setopt (s->get.easyhandle,
1636 curl_easy_setopt (s->get.easyhandle,
1640 curl_easy_setopt (s->get.easyhandle,
1641 CURLOPT_READFUNCTION,
1643 curl_easy_setopt (s->get.easyhandle,
1646 curl_easy_setopt (s->get.easyhandle,
1647 CURLOPT_WRITEFUNCTION,
1649 curl_easy_setopt (s->get.easyhandle,
1652 /* No timeout by default, timeout done with session timeout */
1653 curl_easy_setopt (s->get.easyhandle,
1656 curl_easy_setopt (s->get.easyhandle,
1657 CURLOPT_PRIVATE, s);
1658 curl_easy_setopt (s->get.easyhandle,
1659 CURLOPT_CONNECTTIMEOUT_MS,
1660 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us
1662 curl_easy_setopt (s->get.easyhandle, CURLOPT_BUFFERSIZE,
1663 2 * GNUNET_MAX_MESSAGE_SIZE);
1664 #if CURL_TCP_NODELAY
1665 curl_easy_setopt (ps->recv_endpoint,
1666 CURLOPT_TCP_NODELAY,
1669 curl_easy_setopt (s->get.easyhandle,
1670 CURLOPT_FOLLOWLOCATION,
1673 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1675 if (CURLM_OK != mret)
1677 LOG (GNUNET_ERROR_TYPE_ERROR,
1678 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1680 curl_multi_strerror (mret));
1681 curl_easy_cleanup (s->get.easyhandle);
1682 s->get.easyhandle = NULL;
1685 return GNUNET_SYSERR;
1687 s->plugin->cur_requests++;
1688 LOG (GNUNET_ERROR_TYPE_INFO,
1689 "GET request `%s' established, number of requests increased to %u\n",
1691 s->plugin->cur_requests);
1697 * Connect a HTTP put request
1699 * @param s the session to connect
1700 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1703 client_connect_put (struct GNUNET_ATS_Session *s)
1706 struct HttpAddress *ha;
1709 ha = (struct HttpAddress *) s->address->address;
1710 options = ntohl (ha->options);
1711 /* create put request */
1712 LOG (GNUNET_ERROR_TYPE_DEBUG,
1713 "Session %p: Init PUT handle\n",
1715 s->put.easyhandle = curl_easy_init ();
1718 curl_easy_setopt (s->put.easyhandle,
1721 curl_easy_setopt (s->put.easyhandle,
1722 CURLOPT_DEBUGFUNCTION,
1724 curl_easy_setopt (s->put.easyhandle,
1728 if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
1731 curl_easy_setopt (s->put.easyhandle,
1732 CURLOPT_OPENSOCKETFUNCTION,
1733 &open_tcp_stealth_socket_cb);
1734 curl_easy_setopt (s->put.easyhandle,
1735 CURLOPT_OPENSOCKETDATA,
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1739 "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
1740 curl_easy_cleanup (s->put.easyhandle);
1741 s->put.easyhandle = NULL;
1743 s->put.state = H_DISCONNECTED;
1744 return GNUNET_SYSERR;
1748 curl_easy_setopt (s->put.easyhandle,
1750 CURL_SSLVERSION_TLSv1);
1752 struct HttpAddress *ha;
1753 ha = (struct HttpAddress *) s->address->address;
1755 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1756 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1758 curl_easy_setopt (s->put.easyhandle,
1759 CURLOPT_SSL_VERIFYPEER,
1761 curl_easy_setopt (s->put.easyhandle,
1762 CURLOPT_SSL_VERIFYHOST,
1767 curl_easy_setopt (s->put.easyhandle,
1768 CURLOPT_SSL_VERIFYPEER,
1770 curl_easy_setopt (s->put.easyhandle,
1771 CURLOPT_SSL_VERIFYHOST,
1775 curl_easy_setopt (s->put.easyhandle,
1778 curl_easy_setopt (s->put.easyhandle,
1779 CURLOPT_REDIR_PROTOCOLS,
1782 curl_easy_setopt (s->put.easyhandle,
1785 curl_easy_setopt (s->put.easyhandle,
1786 CURLOPT_REDIR_PROTOCOLS,
1789 if (NULL != s->plugin->proxy_hostname)
1791 curl_easy_setopt (s->put.easyhandle,
1793 s->plugin->proxy_hostname);
1794 curl_easy_setopt (s->put.easyhandle,
1796 s->plugin->proxytype);
1797 if (NULL != s->plugin->proxy_username)
1798 curl_easy_setopt (s->put.easyhandle,
1799 CURLOPT_PROXYUSERNAME,
1800 s->plugin->proxy_username);
1801 if (NULL != s->plugin->proxy_password)
1802 curl_easy_setopt (s->put.easyhandle,
1803 CURLOPT_PROXYPASSWORD,
1804 s->plugin->proxy_password);
1805 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1806 curl_easy_setopt (s->put.easyhandle,
1807 CURLOPT_HTTPPROXYTUNNEL,
1808 s->plugin->proxy_use_httpproxytunnel);
1811 curl_easy_setopt (s->put.easyhandle,
1814 curl_easy_setopt (s->put.easyhandle,
1817 curl_easy_setopt (s->put.easyhandle,
1818 CURLOPT_READFUNCTION,
1820 curl_easy_setopt (s->put.easyhandle,
1823 curl_easy_setopt (s->put.easyhandle,
1824 CURLOPT_WRITEFUNCTION,
1825 &client_receive_put);
1826 curl_easy_setopt (s->put.easyhandle,
1829 /* No timeout by default, timeout done with session timeout */
1830 curl_easy_setopt (s->put.easyhandle,
1833 curl_easy_setopt (s->put.easyhandle,
1836 curl_easy_setopt (s->put.easyhandle,
1837 CURLOPT_CONNECTTIMEOUT_MS,
1838 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us
1840 curl_easy_setopt (s->put.easyhandle, CURLOPT_BUFFERSIZE,
1841 2 * GNUNET_MAX_MESSAGE_SIZE);
1842 #if CURL_TCP_NODELAY
1843 curl_easy_setopt (s->put.easyhandle, CURLOPT_TCP_NODELAY, 1);
1845 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1847 if (CURLM_OK != mret)
1849 LOG (GNUNET_ERROR_TYPE_ERROR,
1850 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1851 s, curl_multi_strerror (mret));
1852 curl_easy_cleanup (s->put.easyhandle);
1853 s->put.easyhandle = NULL;
1855 s->put.state = H_DISCONNECTED;
1856 return GNUNET_SYSERR;
1858 s->put.state = H_CONNECTED;
1859 s->plugin->cur_requests++;
1861 LOG (GNUNET_ERROR_TYPE_INFO,
1862 "PUT request `%s' established, number of requests increased to %u\n",
1863 s->url, s->plugin->cur_requests);
1870 * Connect both PUT and GET request for a session
1872 * @param s the session to connect
1873 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1876 client_connect (struct GNUNET_ATS_Session *s)
1878 struct HTTP_Client_Plugin *plugin = s->plugin;
1879 int res = GNUNET_OK;
1883 http_common_plugin_address_to_string (plugin->protocol,
1884 s->address->address,
1885 s->address->address_length))
1887 LOG (GNUNET_ERROR_TYPE_DEBUG,
1888 "Invalid address peer `%s'\n",
1889 GNUNET_i2s (&s->address->peer));
1890 return GNUNET_SYSERR;
1893 GNUNET_asprintf (&s->url,
1895 http_common_plugin_address_to_url (NULL,
1896 s->address->address,
1897 s->address->address_length),
1898 GNUNET_i2s_full (plugin->env->my_identity),
1902 LOG (GNUNET_ERROR_TYPE_DEBUG,
1903 "Initiating outbound session peer `%s' using address `%s'\n",
1904 GNUNET_i2s (&s->address->peer), s->url);
1906 if (GNUNET_SYSERR == client_connect_get (s))
1907 return GNUNET_SYSERR;
1908 /* If we are emulating an XHR client then delay sending a PUT request until
1909 * there is something to send.
1911 if (GNUNET_YES == plugin->emulate_xhr)
1913 s->put.state = H_TMP_DISCONNECTED;
1915 else if (GNUNET_SYSERR == client_connect_put (s))
1916 return GNUNET_SYSERR;
1918 LOG (GNUNET_ERROR_TYPE_DEBUG,
1919 "Session %p: connected with GET %p and PUT %p\n",
1920 s, s->get.easyhandle,
1922 /* Perform connect */
1923 GNUNET_STATISTICS_set (plugin->env->stats,
1924 HTTP_STAT_STR_CONNECTIONS,
1925 plugin->cur_requests,
1927 /* Re-schedule since handles have changed */
1928 if (NULL != plugin->client_perform_task)
1930 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1931 plugin->client_perform_task = NULL;
1934 /* Schedule task to run immediately */
1935 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run,
1942 * Function obtain the network type for a session
1944 * @param cls closure (`struct Plugin*`)
1945 * @param session the session
1946 * @return the network type
1948 static enum GNUNET_NetworkType
1949 http_client_plugin_get_network (void *cls,
1950 struct GNUNET_ATS_Session *session)
1952 return session->scope;
1957 * Function obtain the network type for an address.
1959 * @param cls closure (`struct Plugin *`)
1960 * @param address the address
1961 * @return the network type
1963 static enum GNUNET_NetworkType
1964 http_client_plugin_get_network_for_address (void *cls,
1966 GNUNET_HELLO_Address *address)
1968 struct HTTP_Client_Plugin *plugin = cls;
1970 return http_common_get_network_for_address (plugin->env,
1976 * Session was idle, so disconnect it
1978 * @param cls the `struct GNUNET_ATS_Session` of the idle session
1981 client_session_timeout (void *cls)
1983 struct GNUNET_ATS_Session *s = cls;
1984 struct GNUNET_TIME_Relative left;
1986 s->timeout_task = NULL;
1987 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1988 if (0 != left.rel_value_us)
1990 /* not actually our turn yet, but let's at least update
1991 the monitor, it may think we're about to die ... */
1992 notify_session_monitor (s->plugin,
1994 GNUNET_TRANSPORT_SS_UPDATE);
1995 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1996 &client_session_timeout,
2001 "Session %p was idle for %s, disconnecting\n",
2003 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
2005 GNUNET_assert (GNUNET_OK ==
2006 http_client_plugin_session_disconnect (s->plugin,
2012 * Creates a new outbound session the transport service will use to
2013 * send data to the peer
2015 * @param cls the plugin
2016 * @param address the address
2017 * @return the session or NULL of max connections exceeded
2019 static struct GNUNET_ATS_Session *
2020 http_client_plugin_get_session (void *cls,
2021 const struct GNUNET_HELLO_Address *address)
2023 struct HTTP_Client_Plugin *plugin = cls;
2024 struct GNUNET_ATS_Session *s;
2025 struct sockaddr *sa;
2026 enum GNUNET_NetworkType net_type;
2030 GNUNET_assert (NULL != address->address);
2032 /* find existing session */
2033 s = client_lookup_session (plugin, address);
2037 /* create a new session */
2038 if (plugin->max_requests <= plugin->cur_requests)
2040 LOG (GNUNET_ERROR_TYPE_WARNING,
2041 "Maximum number of requests (%u) reached: "
2042 "cannot connect to peer `%s'\n",
2043 plugin->max_requests,
2044 GNUNET_i2s (&address->peer));
2048 /* Determine network location */
2049 net_type = GNUNET_NT_UNSPECIFIED;
2050 sa = http_common_socket_from_address (address->address,
2051 address->address_length,
2053 if (GNUNET_SYSERR == res)
2055 if (GNUNET_YES == res)
2057 GNUNET_assert (NULL != sa);
2058 if (AF_INET == sa->sa_family)
2060 salen = sizeof(struct sockaddr_in);
2062 else if (AF_INET6 == sa->sa_family)
2064 salen = sizeof(struct sockaddr_in6);
2066 net_type = plugin->env->get_address_type (plugin->env->cls, sa, salen);
2069 else if (GNUNET_NO == res)
2071 /* Cannot convert to sockaddr -> is external hostname */
2072 net_type = GNUNET_NT_WAN;
2074 if (GNUNET_NT_UNSPECIFIED == net_type)
2080 s = GNUNET_new (struct GNUNET_ATS_Session);
2082 s->address = GNUNET_HELLO_address_copy (address);
2083 s->scope = net_type;
2085 s->put.state = H_NOT_CONNECTED;
2086 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT);
2087 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
2088 &client_session_timeout,
2090 LOG (GNUNET_ERROR_TYPE_DEBUG,
2091 "Created new session %p for `%s' address `%s''\n",
2093 http_common_plugin_address_to_string (plugin->protocol,
2094 s->address->address,
2095 s->address->address_length),
2096 GNUNET_i2s (&s->address->peer));
2098 /* add new session */
2099 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2102 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2103 /* initiate new connection */
2104 if (GNUNET_SYSERR == client_connect (s))
2106 LOG (GNUNET_ERROR_TYPE_ERROR,
2107 "Cannot connect to peer `%s' address `%s''\n",
2108 http_common_plugin_address_to_string (plugin->protocol,
2109 s->address->address,
2110 s->address->address_length),
2111 GNUNET_i2s (&s->address->peer));
2112 client_delete_session (s);
2115 notify_session_monitor (plugin,
2117 GNUNET_TRANSPORT_SS_INIT);
2118 notify_session_monitor (plugin,
2120 GNUNET_TRANSPORT_SS_UP); /* or handshake? */
2126 * Setup http_client plugin
2128 * @param plugin the plugin handle
2129 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
2132 client_start (struct HTTP_Client_Plugin *plugin)
2134 curl_global_init (CURL_GLOBAL_ALL);
2135 plugin->curl_multi_handle = curl_multi_init ();
2137 if (NULL == plugin->curl_multi_handle)
2139 LOG (GNUNET_ERROR_TYPE_ERROR,
2141 "Could not initialize curl multi handle, failed to start %s plugin!\n"),
2143 return GNUNET_SYSERR;
2150 * Another peer has suggested an address for this
2151 * peer and transport plugin. Check that this could be a valid
2152 * address. If so, consider adding it to the list
2155 * @param cls closure with the `struct Plugin`
2156 * @param addr pointer to the address
2157 * @param addrlen length of @a addr
2158 * @return #GNUNET_OK if this is a plausible address for this peer
2159 * and transport; always returns #GNUNET_NO (this is the client!)
2162 http_client_plugin_address_suggested (void *cls,
2166 /* A HTTP/S client does not have any valid address so:*/
2172 * Exit point from the plugin.
2174 * @param cls api as closure
2178 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2180 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2181 struct HTTP_Client_Plugin *plugin = api->cls;
2183 if (NULL == api->cls)
2189 LOG (GNUNET_ERROR_TYPE_DEBUG,
2190 _ ("Shutting down plugin `%s'\n"),
2192 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2193 &destroy_session_cb,
2195 if (NULL != plugin->client_perform_task)
2197 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
2198 plugin->client_perform_task = NULL;
2200 if (NULL != plugin->curl_multi_handle)
2202 curl_multi_cleanup (plugin->curl_multi_handle);
2203 plugin->curl_multi_handle = NULL;
2205 curl_global_cleanup ();
2206 LOG (GNUNET_ERROR_TYPE_DEBUG,
2207 _ ("Shutdown for plugin `%s' complete\n"),
2209 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
2210 GNUNET_free_non_null (plugin->proxy_hostname);
2211 GNUNET_free_non_null (plugin->proxy_username);
2212 GNUNET_free_non_null (plugin->proxy_password);
2213 GNUNET_free (plugin);
2222 * @param plugin the plugin handle
2223 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2226 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
2228 unsigned long long max_requests;
2231 /* Optional parameters */
2233 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2238 plugin->max_requests = max_requests;
2240 LOG (GNUNET_ERROR_TYPE_DEBUG,
2241 _ ("Maximum number of requests is %u\n"),
2242 plugin->max_requests);
2244 /* Read proxy configuration */
2246 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2249 &plugin->proxy_hostname))
2251 LOG (GNUNET_ERROR_TYPE_DEBUG,
2252 "Found proxy host: `%s'\n",
2253 plugin->proxy_hostname);
2254 /* proxy username */
2256 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2259 &plugin->proxy_username))
2261 LOG (GNUNET_ERROR_TYPE_DEBUG,
2262 "Found proxy username name: `%s'\n",
2263 plugin->proxy_username);
2266 /* proxy password */
2268 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2271 &plugin->proxy_password))
2273 LOG (GNUNET_ERROR_TYPE_DEBUG,
2274 "Found proxy password name: `%s'\n",
2275 plugin->proxy_password);
2280 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2285 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
2287 if (0 == strcmp (proxy_type, "HTTP"))
2288 plugin->proxytype = CURLPROXY_HTTP;
2289 else if (0 == strcmp (proxy_type, "SOCKS4"))
2290 plugin->proxytype = CURLPROXY_SOCKS4;
2291 else if (0 == strcmp (proxy_type, "SOCKS5"))
2292 plugin->proxytype = CURLPROXY_SOCKS5;
2293 else if (0 == strcmp (proxy_type, "SOCKS4A"))
2294 plugin->proxytype = CURLPROXY_SOCKS4A;
2295 else if (0 == strcmp (proxy_type, "SOCKS5_HOSTNAME "))
2296 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME;
2299 LOG (GNUNET_ERROR_TYPE_ERROR,
2301 "Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
2304 GNUNET_free (proxy_type);
2305 GNUNET_free (plugin->proxy_hostname);
2306 plugin->proxy_hostname = NULL;
2307 GNUNET_free_non_null (plugin->proxy_username);
2308 plugin->proxy_username = NULL;
2309 GNUNET_free_non_null (plugin->proxy_password);
2310 plugin->proxy_password = NULL;
2312 return GNUNET_SYSERR;
2315 LOG (GNUNET_ERROR_TYPE_DEBUG,
2316 "Found proxy type: `%s'\n",
2320 /* proxy http tunneling */
2321 plugin->proxy_use_httpproxytunnel
2322 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2324 "PROXY_HTTP_TUNNELING");
2325 if (GNUNET_SYSERR == plugin->proxy_use_httpproxytunnel)
2326 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
2328 GNUNET_free_non_null (proxy_type);
2331 /* Should we emulate an XHR client for testing? */
2333 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2341 * Function to convert an address to a human-readable string.
2343 * @param cls closure
2344 * @param addr address to convert
2345 * @param addrlen address length
2346 * @return res string if conversion was successful, NULL otherwise
2349 http_client_plugin_address_to_string (void *cls,
2353 return http_common_plugin_address_to_string (PLUGIN_NAME,
2360 * Function that will be called whenever the transport service wants to
2361 * notify the plugin that a session is still active and in use and
2362 * therefore the session timeout for this session has to be updated
2364 * @param cls closure
2365 * @param peer which peer was the session for
2366 * @param session which session is being updated
2369 http_client_plugin_update_session_timeout (void *cls,
2371 GNUNET_PeerIdentity *peer,
2372 struct GNUNET_ATS_Session *session)
2374 client_reschedule_session_timeout (session);
2379 * Function that will be called whenever the transport service wants to
2380 * notify the plugin that the inbound quota changed and that the plugin
2381 * should update it's delay for the next receive value
2383 * @param cls closure
2384 * @param peer which peer was the session for
2385 * @param s which session is being updated
2386 * @param delay new delay to use for receiving
2389 http_client_plugin_update_inbound_delay (void *cls,
2390 const struct GNUNET_PeerIdentity *peer,
2391 struct GNUNET_ATS_Session *s,
2392 struct GNUNET_TIME_Relative delay)
2394 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
2395 LOG (GNUNET_ERROR_TYPE_DEBUG,
2396 "New inbound delay %s\n",
2397 GNUNET_STRINGS_relative_time_to_string (delay,
2399 if (s->recv_wakeup_task != NULL)
2401 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
2403 = GNUNET_SCHEDULER_add_delayed (delay,
2411 * Return information about the given session to the
2414 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2415 * @param peer peer we send information about
2416 * @param value our `struct GNUNET_ATS_Session` to send information about
2417 * @return #GNUNET_OK (continue to iterate)
2420 send_session_info_iter (void *cls,
2421 const struct GNUNET_PeerIdentity *peer,
2424 struct HTTP_Client_Plugin *plugin = cls;
2425 struct GNUNET_ATS_Session *session = value;
2427 notify_session_monitor (plugin,
2429 GNUNET_TRANSPORT_SS_INIT);
2430 notify_session_monitor (plugin,
2432 GNUNET_TRANSPORT_SS_UP); /* FIXME: or handshake? */
2438 * Begin monitoring sessions of a plugin. There can only
2439 * be one active monitor per plugin (i.e. if there are
2440 * multiple monitors, the transport service needs to
2441 * multiplex the generated events over all of them).
2443 * @param cls closure of the plugin
2444 * @param sic callback to invoke, NULL to disable monitor;
2445 * plugin will being by iterating over all active
2446 * sessions immediately and then enter monitor mode
2447 * @param sic_cls closure for @a sic
2450 http_client_plugin_setup_monitor (void *cls,
2451 GNUNET_TRANSPORT_SessionInfoCallback sic,
2454 struct HTTP_Client_Plugin *plugin = cls;
2457 plugin->sic_cls = sic_cls;
2460 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2461 &send_session_info_iter,
2463 /* signal end of first iteration */
2464 sic (sic_cls, NULL, NULL);
2470 * Entry point for the plugin.
2473 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2475 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2476 struct GNUNET_TRANSPORT_PluginFunctions *api;
2477 struct HTTP_Client_Plugin *plugin;
2479 if (NULL == env->receive)
2481 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2482 initialze the plugin or the API */
2483 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2485 api->address_to_string = &http_client_plugin_address_to_string;
2486 api->string_to_address = &http_common_plugin_string_to_address;
2487 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2491 plugin = GNUNET_new (struct HTTP_Client_Plugin);
2493 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
2495 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2497 api->send = &http_client_plugin_send;
2498 api->disconnect_session = &http_client_plugin_session_disconnect;
2499 api->query_keepalive_factor = &http_client_query_keepalive_factor;
2500 api->disconnect_peer = &http_client_plugin_peer_disconnect;
2501 api->check_address = &http_client_plugin_address_suggested;
2502 api->get_session = &http_client_plugin_get_session;
2503 api->address_to_string = &http_client_plugin_address_to_string;
2504 api->string_to_address = &http_common_plugin_string_to_address;
2505 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2506 api->get_network = &http_client_plugin_get_network;
2507 api->get_network_for_address = &http_client_plugin_get_network_for_address;
2508 api->update_session_timeout = &http_client_plugin_update_session_timeout;
2509 api->update_inbound_delay = &http_client_plugin_update_inbound_delay;
2510 api->setup_monitor = &http_client_plugin_setup_monitor;
2512 plugin->name = "transport-https_client";
2513 plugin->protocol = "https";
2515 plugin->name = "transport-http_client";
2516 plugin->protocol = "http";
2518 plugin->last_tag = 1;
2520 if (GNUNET_SYSERR == client_configure_plugin (plugin))
2522 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2527 if (GNUNET_SYSERR == client_start (plugin))
2529 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2536 /* end of plugin_transport_http_client.c */