2 This file is part of GNUnet
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file transport/plugin_transport_http_client.c
23 * @brief HTTP/S client transport plugin
24 * @author Matthias Wachs
25 * @author Christian Grothoff
29 #define PLUGIN_NAME "https_client"
30 #define HTTP_STAT_STR_CONNECTIONS "# HTTPS client connections"
31 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
32 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
34 #define PLUGIN_NAME "http_client"
35 #define HTTP_STAT_STR_CONNECTIONS "# HTTP client connections"
36 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
37 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
40 #define VERBOSE_CURL GNUNET_NO
42 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
44 #define ENABLE_PUT GNUNET_YES
45 #define ENABLE_GET GNUNET_YES
48 #include "gnunet_util_lib.h"
49 #include "gnunet_protocols.h"
50 #include "gnunet_transport_plugin.h"
51 #include "plugin_transport_http_common.h"
53 #include <curl/curl.h>
54 #elif HAVE_GNURL_CURL_H
55 #include <gnurl/curl.h>
59 #define LOG(kind,...) GNUNET_log_from(kind, PLUGIN_NAME, __VA_ARGS__)
62 * Encapsulation of all of the state of the plugin.
64 struct HTTP_Client_Plugin;
67 * State of a HTTP PUT request
69 enum HTTP_PUT_REQUEST_STATE
72 * Just created, not yet connected
82 * Paused, nothing to send
87 * Temporary disconnect in progress due to inactivity
92 * Send request while temporary disconnect, reconnect
94 H_TMP_RECONNECT_REQUIRED,
97 * Temporarily disconnected
108 * Message to send using http
113 * next pointer for double linked list
115 struct HTTP_Message *next;
118 * previous pointer for double linked list
120 struct HTTP_Message *prev;
123 * buffer containing data to send
128 * Continuation function to call once the transmission buffer
129 * has again space available. NULL if there is no
130 * continuation to call.
132 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
135 * Closure for @e transmit_cont.
137 void *transmit_cont_cls;
140 * amount of data already sent
153 * Session handle for HTTP(S) connections.
165 * Current state of this request
167 enum HTTP_PUT_REQUEST_STATE state;
170 * The curl easy handle
175 * The related session
182 * Session handle for connections.
187 * The URL to connect to
194 struct GNUNET_HELLO_Address *address;
197 * Pointer to the global plugin struct.
199 struct HTTP_Client_Plugin *plugin;
202 * Handle for the HTTP PUT request.
204 struct RequestHandle put;
207 * Handle for the HTTP GET request.
209 struct RequestHandle get;
212 * next pointer for double linked list
214 struct HTTP_Message *msg_head;
217 * previous pointer for double linked list
219 struct HTTP_Message *msg_tail;
222 * Message stream tokenizer for incoming data
224 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
227 * Session timeout task
229 struct GNUNET_SCHEDULER_Task * put_disconnect_task;
232 * Session timeout task
234 struct GNUNET_SCHEDULER_Task * timeout_task;
237 * Task to wake up client receive handle when receiving is allowed again
239 struct GNUNET_SCHEDULER_Task * recv_wakeup_task;
242 * Absolute time when to receive data again.
243 * Used for receive throttling.
245 struct GNUNET_TIME_Absolute next_receive;
248 * When does this session time out.
250 struct GNUNET_TIME_Absolute timeout;
253 * Number of bytes waiting for transmission to this peer.
255 unsigned long long bytes_in_queue;
258 * Outbound overhead due to HTTP connection
259 * Add to next message of this session when calling callback
264 * Number of messages waiting for transmission to this peer.
266 unsigned int msgs_in_queue;
271 enum GNUNET_ATS_Network_Type scope;
276 * Encapsulation of all of the state of the plugin.
278 struct HTTP_Client_Plugin
283 struct GNUNET_TRANSPORT_PluginEnvironment *env;
288 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
291 * Function to call about session status changes.
293 GNUNET_TRANSPORT_SessionInfoCallback sic;
296 * Closure for @e sic.
311 * Proxy configuration: hostname or ip of the proxy server
313 char *proxy_hostname;
316 * Username for the proxy server
318 char *proxy_username;
321 * Password for the proxy server
323 char *proxy_password;
328 CURLM *curl_multi_handle;
333 struct GNUNET_SCHEDULER_Task * client_perform_task;
336 * Type of proxy server:
338 * Valid values as supported by curl:
339 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
340 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
342 curl_proxytype proxytype;
345 * Use proxy tunneling:
346 * Tunnel all operations through a given HTTP instead of have the proxy
347 * evaluate the HTTP request
349 * Default: #GNUNET_NO, #GNUNET_YES experimental
351 int proxy_use_httpproxytunnel;
354 * My options to be included in the address
359 * Maximum number of sockets the plugin can use
360 * Each http connections are two requests
362 unsigned int max_requests;
365 * Current number of sockets the plugin can use
366 * Each http connections are two requests
368 unsigned int cur_requests;
371 * Last used unique HTTP connection tag
386 * Should we emulate an XHR client for testing?
393 * Disconnect a session
395 * @param cls the `struct HTTP_Client_Plugin *`
397 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
400 http_client_plugin_session_disconnect (void *cls, struct Session *s);
404 * If a session monitor is attached, notify it about the new
407 * @param plugin our plugin
408 * @param session session that changed state
409 * @param state new state of the session
412 notify_session_monitor (struct HTTP_Client_Plugin *plugin,
413 struct Session *session,
414 enum GNUNET_TRANSPORT_SessionState state)
416 struct GNUNET_TRANSPORT_SessionInfo info;
418 if (NULL == plugin->sic)
420 memset (&info, 0, sizeof (info));
422 info.is_inbound = GNUNET_NO;
423 info.num_msg_pending = session->msgs_in_queue;
424 info.num_bytes_pending = session->bytes_in_queue;
425 info.receive_delay = session->next_receive;
426 info.session_timeout = session->timeout;
427 info.address = session->address;
428 plugin->sic (plugin->sic_cls,
435 * Delete session @a s.
437 * @param s the session to delete
440 client_delete_session (struct Session *s)
442 struct HTTP_Client_Plugin *plugin = s->plugin;
443 struct HTTP_Message *pos;
444 struct HTTP_Message *next;
447 if (NULL != s->timeout_task)
449 GNUNET_SCHEDULER_cancel (s->timeout_task);
450 s->timeout_task = NULL;
451 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
453 if (NULL != s->put_disconnect_task)
455 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
456 s->put_disconnect_task = NULL;
458 if (NULL != s->recv_wakeup_task)
460 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
461 s->recv_wakeup_task = NULL;
463 GNUNET_assert (GNUNET_OK ==
464 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
467 if (NULL != s->put.easyhandle)
469 LOG (GNUNET_ERROR_TYPE_DEBUG,
470 "Session %p/request %p: disconnecting PUT request to peer `%s'\n",
473 GNUNET_i2s (&s->address->peer));
475 /* remove curl handle from multi handle */
476 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
478 GNUNET_break (CURLM_OK == mret);
479 curl_easy_cleanup (s->put.easyhandle);
480 GNUNET_assert (plugin->cur_requests > 0);
481 plugin->cur_requests--;
482 s->put.easyhandle = NULL;
484 if (NULL != s->get.easyhandle)
486 LOG (GNUNET_ERROR_TYPE_DEBUG,
487 "Session %p/request %p: disconnecting GET request to peer `%s'\n",
488 s, s->get.easyhandle,
489 GNUNET_i2s (&s->address->peer));
490 /* remove curl handle from multi handle */
491 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
493 GNUNET_break (CURLM_OK == mret);
494 curl_easy_cleanup (s->get.easyhandle);
495 GNUNET_assert (plugin->cur_requests > 0);
496 plugin->cur_requests--;
497 s->get.easyhandle = NULL;
500 GNUNET_STATISTICS_set (plugin->env->stats,
501 HTTP_STAT_STR_CONNECTIONS,
502 plugin->cur_requests,
505 while (NULL != (pos = next))
508 GNUNET_CONTAINER_DLL_remove (s->msg_head,
511 GNUNET_assert (0 < s->msgs_in_queue);
513 GNUNET_assert (pos->size <= s->bytes_in_queue);
514 s->bytes_in_queue -= pos->size;
515 if (NULL != pos->transmit_cont)
516 pos->transmit_cont (pos->transmit_cont_cls,
520 pos->pos + s->overhead);
524 GNUNET_assert (0 == s->msgs_in_queue);
525 GNUNET_assert (0 == s->bytes_in_queue);
526 notify_session_monitor (plugin,
528 GNUNET_TRANSPORT_SS_DONE);
529 if (NULL != s->msg_tk)
531 GNUNET_SERVER_mst_destroy (s->msg_tk);
534 GNUNET_HELLO_address_free (s->address);
535 GNUNET_free (s->url);
541 * Increment session timeout due to activity for session @a s.
543 * @param s the session
546 client_reschedule_session_timeout (struct Session *s)
548 GNUNET_assert (NULL != s->timeout_task);
549 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
554 * Task performing curl operations
556 * @param cls plugin as closure
557 * @param tc gnunet scheduler task context
560 client_run (void *cls,
561 const struct GNUNET_SCHEDULER_TaskContext *tc);
565 * Function setting up file descriptors and scheduling task to run
567 * @param plugin the plugin as closure
568 * @param now schedule task in 1ms, regardless of what curl may say
569 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
572 client_schedule (struct HTTP_Client_Plugin *plugin,
579 struct GNUNET_NETWORK_FDSet *grs;
580 struct GNUNET_NETWORK_FDSet *gws;
583 struct GNUNET_TIME_Relative timeout;
585 /* Cancel previous scheduled task */
586 if (plugin->client_perform_task != NULL)
588 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
589 plugin->client_perform_task = NULL;
595 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
596 if (mret != CURLM_OK)
598 LOG (GNUNET_ERROR_TYPE_ERROR,
599 _("%s failed at %s:%d: `%s'\n"),
603 curl_multi_strerror (mret));
604 return GNUNET_SYSERR;
606 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
608 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
610 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
611 if (now == GNUNET_YES)
612 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
614 if (CURLM_OK != mret)
616 LOG (GNUNET_ERROR_TYPE_ERROR,
617 _("%s failed at %s:%d: `%s'\n"),
618 "curl_multi_timeout", __FILE__, __LINE__,
619 curl_multi_strerror (mret));
620 return GNUNET_SYSERR;
623 grs = GNUNET_NETWORK_fdset_create ();
624 gws = GNUNET_NETWORK_fdset_create ();
625 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
626 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
628 /* Schedule task to run when select is ready to read or write */
629 plugin->client_perform_task =
630 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
632 &client_run, plugin);
633 GNUNET_NETWORK_fdset_destroy (gws);
634 GNUNET_NETWORK_fdset_destroy (grs);
643 * @param curl the curl easy handle
644 * @param type message type
645 * @param data data to log, NOT a 0-terminated string
646 * @param size data length
647 * @param cls the closure
651 client_log (CURL *curl,
657 struct RequestHandle *ch = cls;
658 const char *ttype = "UNSPECIFIED";
661 if (! ((CURLINFO_TEXT == type) ||
662 (CURLINFO_HEADER_IN == type) ||
663 (CURLINFO_HEADER_OUT == type)))
670 case CURLINFO_HEADER_IN:
673 case CURLINFO_HEADER_OUT:
674 ttype = "HEADER_OUT";
676 GNUNET_assert (NULL != ch);
677 GNUNET_assert (NULL != ch->easyhandle);
678 GNUNET_assert (NULL != ch->s);
679 ch->s->overhead += size;
682 ttype = "UNSPECIFIED";
685 memcpy (text, data, size);
686 if (text[size - 1] == '\n')
693 text[size + 1] = '\0';
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 * Connect GET request
707 * @param s the session to connect
708 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
711 client_connect_get (struct Session *s);
715 * Connect a HTTP put request
717 * @param s the session to connect
718 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
721 client_connect_put (struct Session *s);
725 * Function that can be used by the transport service to transmit
726 * a message using the plugin. Note that in the case of a
727 * peer disconnecting, the continuation MUST be called
728 * prior to the disconnect notification itself. This function
729 * will be called with this peer's HELLO message to initiate
730 * a fresh connection to another peer.
733 * @param s which session must be used
734 * @param msgbuf the message to transmit
735 * @param msgbuf_size number of bytes in @a msgbuf
736 * @param priority how important is the message (most plugins will
737 * ignore message priority and just FIFO)
738 * @param to how long to wait at most for the transmission (does not
739 * require plugins to discard the message after the timeout,
740 * just advisory for the desired delay; most plugins will ignore
742 * @param cont continuation to call once the message has
743 * been transmitted (or if the transport is ready
744 * for the next transmission call; or if the
745 * peer disconnected...); can be NULL
746 * @param cont_cls closure for @a cont
747 * @return number of bytes used (on the physical network, with overheads);
748 * -1 on hard errors (i.e. address invalid); 0 is a legal value
749 * and does NOT mean that the message was not transmitted (DV)
752 http_client_plugin_send (void *cls,
756 unsigned int priority,
757 struct GNUNET_TIME_Relative to,
758 GNUNET_TRANSPORT_TransmitContinuation cont,
761 struct HTTP_Client_Plugin *plugin = cls;
762 struct HTTP_Message *msg;
765 LOG (GNUNET_ERROR_TYPE_DEBUG,
766 "Session %p/request %p: Sending message with %u to peer `%s' \n",
770 GNUNET_i2s (&s->address->peer));
772 /* create new message and schedule */
773 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
774 msg->size = msgbuf_size;
775 msg->buf = (char *) &msg[1];
776 msg->transmit_cont = cont;
777 msg->transmit_cont_cls = cont_cls;
781 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head,
785 s->bytes_in_queue += msg->size;
787 GNUNET_asprintf (&stat_txt,
788 "# bytes currently in %s_client buffers",
790 GNUNET_STATISTICS_update (plugin->env->stats,
791 stat_txt, msgbuf_size, GNUNET_NO);
792 GNUNET_free (stat_txt);
793 notify_session_monitor (plugin,
795 GNUNET_TRANSPORT_SS_UPDATE);
796 if (H_TMP_DISCONNECTING == s->put.state)
798 /* PUT request is currently getting disconnected */
799 s->put.state = H_TMP_RECONNECT_REQUIRED;
800 LOG (GNUNET_ERROR_TYPE_DEBUG,
801 "Session %p/request %p: currently disconnecting, reconnecting immediately\n",
806 if (H_PAUSED == s->put.state)
808 /* PUT request was paused, unpause */
809 GNUNET_assert (s->put_disconnect_task != NULL);
810 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
811 s->put_disconnect_task = NULL;
812 LOG (GNUNET_ERROR_TYPE_DEBUG,
813 "Session %p/request %p: unpausing request\n",
814 s, s->put.easyhandle);
815 s->put.state = H_CONNECTED;
816 if (NULL != s->put.easyhandle)
817 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
819 else if (H_TMP_DISCONNECTED == s->put.state)
821 /* PUT request was disconnected, reconnect */
822 LOG (GNUNET_ERROR_TYPE_DEBUG, "Session %p: Reconnecting PUT request\n", s);
823 GNUNET_break (NULL == s->put.easyhandle);
824 if (GNUNET_SYSERR == client_connect_put (s))
826 /* Could not reconnect */
827 http_client_plugin_session_disconnect (plugin, s);
828 return GNUNET_SYSERR;
831 client_schedule (s->plugin, GNUNET_YES);
837 * Disconnect a session
839 * @param cls the `struct HTTP_Client_Plugin *`
841 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
844 http_client_plugin_session_disconnect (void *cls,
847 struct HTTP_Client_Plugin *plugin = cls;
849 LOG (GNUNET_ERROR_TYPE_DEBUG,
850 "Session %p: notifying transport about ending session\n",
852 plugin->env->session_end (plugin->env->cls,
855 client_delete_session (s);
857 /* Re-schedule since handles have changed */
858 if (NULL != plugin->client_perform_task)
860 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
861 plugin->client_perform_task = NULL;
863 client_schedule (plugin, GNUNET_YES);
870 * Function that is called to get the keepalive factor.
871 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
872 * calculate the interval between keepalive packets.
874 * @param cls closure with the `struct Plugin`
875 * @return keepalive factor
878 http_client_query_keepalive_factor (void *cls)
885 * Callback to destroys all sessions on exit.
887 * @param cls the `struct HTTP_Client_Plugin *`
888 * @param peer identity of the peer
889 * @param value the `struct Session *`
890 * @return #GNUNET_OK (continue iterating)
893 destroy_session_cb (void *cls,
894 const struct GNUNET_PeerIdentity *peer,
897 struct HTTP_Client_Plugin *plugin = cls;
898 struct Session *session = value;
900 http_client_plugin_session_disconnect (plugin, session);
906 * Function that can be used to force the plugin to disconnect
907 * from the given peer and cancel all previous transmissions
908 * (and their continuationc).
911 * @param target peer from which to disconnect
914 http_client_plugin_peer_disconnect (void *cls,
915 const struct GNUNET_PeerIdentity *target)
917 struct HTTP_Client_Plugin *plugin = cls;
919 LOG (GNUNET_ERROR_TYPE_DEBUG,
920 "Transport tells me to disconnect `%s'\n",
921 GNUNET_i2s (target));
922 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
930 * Closure for #session_lookup_client_by_address().
932 struct SessionClientCtx
935 * Address we are looking for.
937 const struct GNUNET_HELLO_Address *address;
940 * Session that was found.
947 * Locate the seession object for a given address.
949 * @param cls the `struct SessionClientCtx *`
950 * @param key peer identity
951 * @param value the `struct Session` to check
952 * @return #GNUNET_NO if found, #GNUNET_OK if not
955 session_lookup_client_by_address (void *cls,
956 const struct GNUNET_PeerIdentity *key,
959 struct SessionClientCtx *sc_ctx = cls;
960 struct Session *s = value;
962 if (0 == GNUNET_HELLO_address_cmp (sc_ctx->address,
973 * Check if a sessions exists for an specific address
975 * @param plugin the plugin
976 * @param address the address
977 * @return the session or NULL
979 static struct Session *
980 client_lookup_session (struct HTTP_Client_Plugin *plugin,
981 const struct GNUNET_HELLO_Address *address)
983 struct SessionClientCtx sc_ctx;
985 sc_ctx.address = address;
987 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
988 &session_lookup_client_by_address,
995 * When we have nothing to transmit, we pause the HTTP PUT
996 * after a while (so that gnurl stops asking). This task
997 * is the delayed task that actually disconnects the PUT.
999 * @param cls the `struct Session *` with the put
1000 * @param tc scheduler context
1003 client_put_disconnect (void *cls,
1004 const struct GNUNET_SCHEDULER_TaskContext *tc)
1006 struct Session *s = cls;
1008 s->put_disconnect_task = NULL;
1009 LOG (GNUNET_ERROR_TYPE_DEBUG,
1010 "Session %p/request %p: will be disconnected due to no activity\n",
1011 s, s->put.easyhandle);
1012 s->put.state = H_TMP_DISCONNECTING;
1013 if (NULL != s->put.easyhandle)
1014 curl_easy_pause (s->put.easyhandle,
1016 client_schedule (s->plugin, GNUNET_YES);
1021 * Callback method used with libcurl
1022 * Method is called when libcurl needs to read data during sending
1024 * @param stream pointer where to write data
1025 * @param size size of an individual element
1026 * @param nmemb count of elements that can be written to the buffer
1027 * @param cls our `struct Session`
1028 * @return bytes written to stream, returning 0 will terminate request!
1031 client_send_cb (void *stream,
1036 struct Session *s = cls;
1037 struct HTTP_Client_Plugin *plugin = s->plugin;
1038 struct HTTP_Message *msg = s->msg_head;
1042 if (H_TMP_DISCONNECTING == s->put.state)
1044 LOG (GNUNET_ERROR_TYPE_DEBUG,
1045 "Session %p/request %p: disconnect due to inactivity\n",
1046 s, s->put.easyhandle);
1052 if (GNUNET_YES == plugin->emulate_xhr)
1054 LOG (GNUNET_ERROR_TYPE_DEBUG,
1055 "Session %p/request %p: PUT request finished\n",
1058 s->put.state = H_TMP_DISCONNECTING;
1062 /* We have nothing to send, so pause PUT request */
1063 LOG (GNUNET_ERROR_TYPE_DEBUG,
1064 "Session %p/request %p: nothing to send, suspending\n",
1067 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
1068 &client_put_disconnect, s);
1069 s->put.state = H_PAUSED;
1070 return CURL_READFUNC_PAUSE;
1073 GNUNET_assert (msg->pos < msg->size);
1074 /* calculate how much fits in buffer */
1075 len = GNUNET_MIN (msg->size - msg->pos,
1077 memcpy (stream, &msg->buf[msg->pos], len);
1079 if (msg->pos == msg->size)
1081 LOG (GNUNET_ERROR_TYPE_DEBUG,
1082 "Session %p/request %p: sent message with %u bytes sent, removing message from queue\n",
1087 /* Calling transmit continuation */
1088 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1091 GNUNET_assert (0 < s->msgs_in_queue);
1093 GNUNET_assert (msg->size <= s->bytes_in_queue);
1094 s->bytes_in_queue -= msg->size;
1095 if (NULL != msg->transmit_cont)
1096 msg->transmit_cont (msg->transmit_cont_cls,
1100 msg->size + s->overhead);
1104 notify_session_monitor (plugin,
1106 GNUNET_TRANSPORT_SS_UPDATE);
1107 GNUNET_asprintf (&stat_txt,
1108 "# bytes currently in %s_client buffers",
1110 GNUNET_STATISTICS_update (plugin->env->stats,
1114 GNUNET_free (stat_txt);
1115 GNUNET_asprintf (&stat_txt,
1116 "# bytes transmitted via %s_client",
1118 GNUNET_STATISTICS_update (plugin->env->stats,
1122 GNUNET_free (stat_txt);
1128 * Wake up a curl handle which was suspended
1130 * @param cls the session
1131 * @param tc task context
1134 client_wake_up (void *cls,
1135 const struct GNUNET_SCHEDULER_TaskContext *tc)
1137 struct Session *s = cls;
1139 s->recv_wakeup_task = NULL;
1140 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1142 LOG (GNUNET_ERROR_TYPE_DEBUG,
1143 "Session %p/request %p: Waking up GET handle\n",
1144 s, s->get.easyhandle);
1145 if (H_PAUSED == s->put.state)
1147 /* PUT request was paused, unpause */
1148 GNUNET_assert (s->put_disconnect_task != NULL);
1149 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
1150 s->put_disconnect_task = NULL;
1151 s->put.state = H_CONNECTED;
1152 if (NULL != s->put.easyhandle)
1153 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
1155 if (NULL != s->get.easyhandle)
1156 curl_easy_pause (s->get.easyhandle, CURLPAUSE_CONT);
1161 * Callback for message stream tokenizer
1163 * @param cls the session
1164 * @param client not used
1165 * @param message the message received
1166 * @return always #GNUNET_OK
1169 client_receive_mst_cb (void *cls,
1171 const struct GNUNET_MessageHeader *message)
1173 struct Session *s = cls;
1174 struct HTTP_Client_Plugin *plugin;
1175 struct GNUNET_TIME_Relative delay;
1179 delay = s->plugin->env->receive (plugin->env->cls,
1183 GNUNET_asprintf (&stat_txt,
1184 "# bytes received via %s_client",
1186 GNUNET_STATISTICS_update (plugin->env->stats,
1188 ntohs (message->size),
1190 GNUNET_free (stat_txt);
1192 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1193 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
1195 LOG (GNUNET_ERROR_TYPE_DEBUG,
1196 "Client: peer `%s' address `%s' next read delayed for %s\n",
1197 GNUNET_i2s (&s->address->peer),
1198 http_common_plugin_address_to_string (s->plugin->protocol,
1199 s->address->address,
1200 s->address->address_length),
1201 GNUNET_STRINGS_relative_time_to_string (delay,
1204 client_reschedule_session_timeout (s);
1210 * Callback method used with libcurl when data for a PUT request are
1211 * received. We do not expect data here, so we just discard it.
1213 * @param stream pointer where to write data
1214 * @param size size of an individual element
1215 * @param nmemb count of elements that can be written to the buffer
1216 * @param cls destination pointer, passed to the libcurl handle
1217 * @return bytes read from stream
1220 client_receive_put (void *stream,
1225 return size * nmemb;
1230 * Callback method used with libcurl when data for a GET request are
1231 * received. Forward to MST
1233 * @param stream pointer where to write data
1234 * @param size size of an individual element
1235 * @param nmemb count of elements that can be written to the buffer
1236 * @param cls destination pointer, passed to the libcurl handle
1237 * @return bytes read from stream
1240 client_receive (void *stream,
1245 struct Session *s = cls;
1246 struct GNUNET_TIME_Absolute now;
1247 size_t len = size * nmemb;
1249 LOG (GNUNET_ERROR_TYPE_DEBUG,
1250 "Session %p / request %p: Received %u bytes from peer `%s'\n",
1254 GNUNET_i2s (&s->address->peer));
1255 now = GNUNET_TIME_absolute_get ();
1256 if (now.abs_value_us < s->next_receive.abs_value_us)
1258 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1259 struct GNUNET_TIME_Relative delta
1260 = GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1262 LOG (GNUNET_ERROR_TYPE_DEBUG,
1263 "Session %p / request %p: No inbound bandwidth available! Next read was delayed for %s\n",
1266 GNUNET_STRINGS_relative_time_to_string (delta,
1268 if (s->recv_wakeup_task != NULL)
1270 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1271 s->recv_wakeup_task = NULL;
1274 = GNUNET_SCHEDULER_add_delayed (delta,
1277 return CURL_WRITEFUNC_PAUSE;
1279 if (NULL == s->msg_tk)
1280 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb,
1282 GNUNET_SERVER_mst_receive (s->msg_tk,
1293 * Task performing curl operations
1295 * @param cls plugin as closure
1296 * @param tc scheduler task context
1299 client_run (void *cls,
1300 const struct GNUNET_SCHEDULER_TaskContext *tc)
1302 struct HTTP_Client_Plugin *plugin = cls;
1304 long http_statuscode;
1307 int put_request; /* GNUNET_YES if easy handle is put, GNUNET_NO for get */
1310 plugin->client_perform_task = NULL;
1311 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1314 /* While data are available or timeouts occured */
1318 /* Perform operations for all handles */
1319 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1321 /* Get additional information for all handles */
1322 while (NULL != (msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1324 CURL *easy_h = msg->easy_handle;
1325 struct Session *s = NULL;
1326 char *d = NULL; /* curl requires 'd' to be a 'char *' */
1328 GNUNET_assert (NULL != easy_h);
1330 /* Obtain session from easy handle */
1331 GNUNET_assert (CURLE_OK == curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1332 s = (struct Session *) d;
1333 GNUNET_assert (NULL != s);
1335 if (msg->msg != CURLMSG_DONE)
1336 continue; /* This should not happen */
1338 /* Get HTTP response code */
1339 GNUNET_break (CURLE_OK == curl_easy_getinfo (easy_h,
1340 CURLINFO_RESPONSE_CODE, &http_statuscode));
1342 if (easy_h == s->put.easyhandle)
1343 put_request = GNUNET_YES;
1345 put_request = GNUNET_NO;
1347 /* Log status of terminated request */
1348 if ((0 != msg->data.result) || (http_statuscode != 200))
1349 LOG (GNUNET_ERROR_TYPE_DEBUG,
1350 "Session %p/request %p: %s request to `%s' ended with status %i reason %i: `%s'\n",
1351 s, msg->easy_handle,
1352 (GNUNET_YES == put_request) ? "PUT" : "GET",
1353 GNUNET_i2s (&s->address->peer),
1356 curl_easy_strerror (msg->data.result));
1358 LOG (GNUNET_ERROR_TYPE_DEBUG,
1359 "Session %p/request %p: %s request to `%s' ended normal\n",
1360 s, msg->easy_handle,
1361 (GNUNET_YES == put_request) ? "PUT" : "GET",
1362 GNUNET_i2s (&s->address->peer));
1364 /* Remove easy handle from multi handle */
1365 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1367 /* Clean up easy handle */
1368 curl_easy_cleanup (easy_h);
1370 /* Remove information */
1371 GNUNET_assert (plugin->cur_requests > 0);
1372 plugin->cur_requests--;
1373 LOG (GNUNET_ERROR_TYPE_INFO,
1374 "%s request to %s done, number of requests decreased to %u\n",
1375 (GNUNET_YES == put_request) ? "PUT" : "GET",
1377 plugin->cur_requests);
1379 if (GNUNET_YES == put_request)
1381 /* Clean up a PUT request */
1382 s->put.easyhandle = NULL;
1385 switch (s->put.state) {
1386 case H_NOT_CONNECTED:
1387 case H_DISCONNECTED:
1388 case H_TMP_DISCONNECTED:
1389 /* This must not happen */
1392 case H_TMP_RECONNECT_REQUIRED:
1393 /* Transport called send while disconnect in progess, reconnect */
1394 if (GNUNET_SYSERR == client_connect_put (s))
1396 /* Reconnect failed, disconnect session */
1397 http_client_plugin_session_disconnect (plugin, s);
1400 case H_TMP_DISCONNECTING:
1401 /* PUT gets temporarily disconnected */
1402 s->put.state = H_TMP_DISCONNECTED;
1406 /* PUT gets permanently disconnected */
1407 s->put.state = H_DISCONNECTED;
1408 http_client_plugin_session_disconnect (plugin, s);
1415 else if (GNUNET_NO == put_request)
1417 /* Clean up a GET request */
1418 s->get.easyhandle = NULL;
1421 /* If we are emulating an XHR client we need to make another GET
1424 if (GNUNET_YES == plugin->emulate_xhr)
1426 if (GNUNET_SYSERR == client_connect_get (s))
1427 http_client_plugin_session_disconnect (plugin, s);
1431 /* GET request was terminated, so disconnect session */
1432 http_client_plugin_session_disconnect (plugin, s);
1436 GNUNET_break (0); /* Must not happen */
1438 GNUNET_STATISTICS_set (plugin->env->stats,
1439 HTTP_STAT_STR_CONNECTIONS,
1440 plugin->cur_requests,
1444 while (mret == CURLM_CALL_MULTI_PERFORM);
1445 client_schedule (plugin, GNUNET_NO);
1451 * Open TCP socket with TCP STEALTH enabled.
1453 * @param clientp our `struct Session *`
1454 * @param purpose why does curl want to open a socket
1455 * @param address what kind of socket does curl want to have opened?
1456 * @return opened socket
1458 static curl_socket_t
1459 open_tcp_stealth_socket_cb (void *clientp,
1460 curlsocktype purpose,
1461 struct curl_sockaddr *address)
1463 struct Session *s = clientp;
1468 case CURLSOCKTYPE_IPCXN:
1469 ret = socket (address->family,
1473 return CURL_SOCKET_BAD;
1474 if ( ( (SOCK_STREAM != address->socktype) ||
1475 ( (0 != address->protocol) &&
1476 (IPPROTO_TCP != address->protocol))) )
1477 return (curl_socket_t) ret;
1478 if ( (0 != setsockopt (ret,
1482 sizeof (struct GNUNET_PeerIdentity))) )
1484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1485 _("TCP_STEALTH not supported on this platform.\n"));
1487 return CURL_SOCKET_BAD;
1489 return (curl_socket_t) ret;
1490 case CURLSOCKTYPE_ACCEPT:
1492 return CURL_SOCKET_BAD;
1494 case CURLSOCKTYPE_LAST:
1496 return CURL_SOCKET_BAD;
1499 return CURL_SOCKET_BAD;
1506 * Connect GET request for a session
1508 * @param s the session to connect
1509 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1512 client_connect_get (struct Session *s)
1515 struct HttpAddress *ha;
1518 ha = (struct HttpAddress *) s->address->address;
1519 options = ntohl (ha->options);
1520 /* create get request */
1521 s->get.easyhandle = curl_easy_init ();
1523 if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
1526 curl_easy_setopt (s->get.easyhandle,
1527 CURLOPT_OPENSOCKETFUNCTION,
1528 &open_tcp_stealth_socket_cb);
1529 curl_easy_setopt (s->get.easyhandle,
1530 CURLOPT_OPENSOCKETDATA,
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1534 "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
1535 curl_easy_cleanup (s->get.easyhandle);
1536 s->get.easyhandle = NULL;
1538 return GNUNET_SYSERR;
1543 curl_easy_setopt (s->get.easyhandle,
1546 curl_easy_setopt (s->get.easyhandle,
1547 CURLOPT_DEBUGFUNCTION,
1549 curl_easy_setopt (s->get.easyhandle,
1554 curl_easy_setopt (s->get.easyhandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1556 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1557 (options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1559 curl_easy_setopt (s->get.easyhandle,
1560 CURLOPT_SSL_VERIFYPEER, 1L);
1561 curl_easy_setopt (s->get.easyhandle,
1562 CURLOPT_SSL_VERIFYHOST,
1567 curl_easy_setopt (s->get.easyhandle,
1568 CURLOPT_SSL_VERIFYPEER,
1570 curl_easy_setopt (s->get.easyhandle,
1571 CURLOPT_SSL_VERIFYHOST,
1575 curl_easy_setopt (s->get.easyhandle,
1578 curl_easy_setopt (s->get.easyhandle,
1579 CURLOPT_REDIR_PROTOCOLS,
1582 curl_easy_setopt (s->get.easyhandle,
1585 curl_easy_setopt (s->get.easyhandle,
1586 CURLOPT_REDIR_PROTOCOLS,
1590 if (NULL != s->plugin->proxy_hostname)
1592 curl_easy_setopt (s->get.easyhandle,
1594 s->plugin->proxy_hostname);
1595 curl_easy_setopt (s->get.easyhandle,
1597 s->plugin->proxytype);
1598 if (NULL != s->plugin->proxy_username)
1599 curl_easy_setopt (s->get.easyhandle,
1600 CURLOPT_PROXYUSERNAME,
1601 s->plugin->proxy_username);
1602 if (NULL != s->plugin->proxy_password)
1603 curl_easy_setopt (s->get.easyhandle,
1604 CURLOPT_PROXYPASSWORD,
1605 s->plugin->proxy_password);
1606 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1607 curl_easy_setopt (s->get.easyhandle,
1608 CURLOPT_HTTPPROXYTUNNEL,
1609 s->plugin->proxy_use_httpproxytunnel);
1612 if (GNUNET_YES == s->plugin->emulate_xhr)
1616 GNUNET_asprintf (&url,
1619 curl_easy_setopt (s->get.easyhandle,
1626 curl_easy_setopt (s->get.easyhandle,
1630 curl_easy_setopt (s->get.easyhandle,
1631 CURLOPT_READFUNCTION,
1633 curl_easy_setopt (s->get.easyhandle,
1636 curl_easy_setopt (s->get.easyhandle,
1637 CURLOPT_WRITEFUNCTION,
1639 curl_easy_setopt (s->get.easyhandle,
1642 /* No timeout by default, timeout done with session timeout */
1643 curl_easy_setopt (s->get.easyhandle,
1646 curl_easy_setopt (s->get.easyhandle,
1647 CURLOPT_PRIVATE, s);
1648 curl_easy_setopt (s->get.easyhandle,
1649 CURLOPT_CONNECTTIMEOUT_MS,
1650 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1651 curl_easy_setopt (s->get.easyhandle, CURLOPT_BUFFERSIZE,
1652 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1653 #if CURL_TCP_NODELAY
1654 curl_easy_setopt (ps->recv_endpoint,
1655 CURLOPT_TCP_NODELAY,
1658 curl_easy_setopt (s->get.easyhandle,
1659 CURLOPT_FOLLOWLOCATION,
1662 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1664 if (CURLM_OK != mret)
1666 LOG (GNUNET_ERROR_TYPE_ERROR,
1667 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1669 curl_multi_strerror (mret));
1670 curl_easy_cleanup (s->get.easyhandle);
1671 s->get.easyhandle = NULL;
1674 return GNUNET_SYSERR;
1676 s->plugin->cur_requests++;
1677 LOG (GNUNET_ERROR_TYPE_INFO,
1678 "GET request `%s' established, number of requests increased to %u\n",
1680 s->plugin->cur_requests);
1686 * Connect a HTTP put request
1688 * @param s the session to connect
1689 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1692 client_connect_put (struct Session *s)
1695 struct HttpAddress *ha;
1698 ha = (struct HttpAddress *) s->address->address;
1699 options = ntohl (ha->options);
1700 /* create put request */
1701 LOG (GNUNET_ERROR_TYPE_DEBUG,
1702 "Session %p: Init PUT handle\n",
1704 s->put.easyhandle = curl_easy_init ();
1707 curl_easy_setopt (s->put.easyhandle,
1710 curl_easy_setopt (s->put.easyhandle,
1711 CURLOPT_DEBUGFUNCTION,
1713 curl_easy_setopt (s->put.easyhandle,
1717 if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
1720 curl_easy_setopt (s->put.easyhandle,
1721 CURLOPT_OPENSOCKETFUNCTION,
1722 &open_tcp_stealth_socket_cb);
1723 curl_easy_setopt (s->put.easyhandle,
1724 CURLOPT_OPENSOCKETDATA,
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1728 "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
1729 curl_easy_cleanup (s->put.easyhandle);
1730 s->put.easyhandle = NULL;
1732 s->put.state = H_DISCONNECTED;
1733 return GNUNET_SYSERR;
1737 curl_easy_setopt (s->put.easyhandle,
1739 CURL_SSLVERSION_TLSv1);
1741 struct HttpAddress *ha;
1742 ha = (struct HttpAddress *) s->address->address;
1744 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1745 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1747 curl_easy_setopt (s->put.easyhandle,
1748 CURLOPT_SSL_VERIFYPEER,
1750 curl_easy_setopt (s->put.easyhandle,
1751 CURLOPT_SSL_VERIFYHOST,
1756 curl_easy_setopt (s->put.easyhandle,
1757 CURLOPT_SSL_VERIFYPEER,
1759 curl_easy_setopt (s->put.easyhandle,
1760 CURLOPT_SSL_VERIFYHOST,
1764 curl_easy_setopt (s->put.easyhandle,
1767 curl_easy_setopt (s->put.easyhandle,
1768 CURLOPT_REDIR_PROTOCOLS,
1771 curl_easy_setopt (s->put.easyhandle,
1774 curl_easy_setopt (s->put.easyhandle,
1775 CURLOPT_REDIR_PROTOCOLS,
1778 if (NULL != s->plugin->proxy_hostname)
1780 curl_easy_setopt (s->put.easyhandle,
1782 s->plugin->proxy_hostname);
1783 curl_easy_setopt (s->put.easyhandle,
1785 s->plugin->proxytype);
1786 if (NULL != s->plugin->proxy_username)
1787 curl_easy_setopt (s->put.easyhandle,
1788 CURLOPT_PROXYUSERNAME,
1789 s->plugin->proxy_username);
1790 if (NULL != s->plugin->proxy_password)
1791 curl_easy_setopt (s->put.easyhandle,
1792 CURLOPT_PROXYPASSWORD,
1793 s->plugin->proxy_password);
1794 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1795 curl_easy_setopt (s->put.easyhandle,
1796 CURLOPT_HTTPPROXYTUNNEL,
1797 s->plugin->proxy_use_httpproxytunnel);
1800 curl_easy_setopt (s->put.easyhandle,
1803 curl_easy_setopt (s->put.easyhandle,
1806 curl_easy_setopt (s->put.easyhandle,
1807 CURLOPT_READFUNCTION,
1809 curl_easy_setopt (s->put.easyhandle,
1812 curl_easy_setopt (s->put.easyhandle,
1813 CURLOPT_WRITEFUNCTION,
1814 &client_receive_put);
1815 curl_easy_setopt (s->put.easyhandle,
1818 /* No timeout by default, timeout done with session timeout */
1819 curl_easy_setopt (s->put.easyhandle,
1822 curl_easy_setopt (s->put.easyhandle,
1825 curl_easy_setopt (s->put.easyhandle,
1826 CURLOPT_CONNECTTIMEOUT_MS,
1827 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1828 curl_easy_setopt (s->put.easyhandle, CURLOPT_BUFFERSIZE,
1829 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1830 #if CURL_TCP_NODELAY
1831 curl_easy_setopt (s->put.easyhandle, CURLOPT_TCP_NODELAY, 1);
1833 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1835 if (CURLM_OK != mret)
1837 LOG (GNUNET_ERROR_TYPE_ERROR,
1838 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1839 s, curl_multi_strerror (mret));
1840 curl_easy_cleanup (s->put.easyhandle);
1841 s->put.easyhandle = NULL;
1843 s->put.state = H_DISCONNECTED;
1844 return GNUNET_SYSERR;
1846 s->put.state = H_CONNECTED;
1847 s->plugin->cur_requests++;
1849 LOG (GNUNET_ERROR_TYPE_INFO,
1850 "PUT request `%s' established, number of requests increased to %u\n",
1851 s->url, s->plugin->cur_requests);
1858 * Connect both PUT and GET request for a session
1860 * @param s the session to connect
1861 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1864 client_connect (struct Session *s)
1866 struct HTTP_Client_Plugin *plugin = s->plugin;
1867 int res = GNUNET_OK;
1871 http_common_plugin_address_to_string(plugin->protocol,
1872 s->address->address,
1873 s->address->address_length))
1875 LOG (GNUNET_ERROR_TYPE_DEBUG,
1876 "Invalid address peer `%s'\n",
1877 GNUNET_i2s(&s->address->peer));
1878 return GNUNET_SYSERR;
1881 GNUNET_asprintf (&s->url,
1883 http_common_plugin_address_to_url (NULL,
1884 s->address->address,
1885 s->address->address_length),
1886 GNUNET_i2s_full (plugin->env->my_identity),
1890 LOG (GNUNET_ERROR_TYPE_DEBUG,
1891 "Initiating outbound session peer `%s' using address `%s'\n",
1892 GNUNET_i2s (&s->address->peer), s->url);
1894 if (GNUNET_SYSERR == client_connect_get (s))
1895 return GNUNET_SYSERR;
1896 /* If we are emulating an XHR client then delay sending a PUT request until
1897 * there is something to send.
1899 if (GNUNET_YES == plugin->emulate_xhr)
1901 s->put.state = H_TMP_DISCONNECTED;
1903 else if (GNUNET_SYSERR == client_connect_put (s))
1904 return GNUNET_SYSERR;
1906 LOG (GNUNET_ERROR_TYPE_DEBUG,
1907 "Session %p: connected with GET %p and PUT %p\n",
1908 s, s->get.easyhandle,
1910 /* Perform connect */
1911 GNUNET_STATISTICS_set (plugin->env->stats,
1912 HTTP_STAT_STR_CONNECTIONS,
1913 plugin->cur_requests,
1915 /* Re-schedule since handles have changed */
1916 if (NULL != plugin->client_perform_task)
1918 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1919 plugin->client_perform_task = NULL;
1922 /* Schedule task to run immediately */
1923 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run,
1930 * Function obtain the network type for a session
1932 * @param cls closure (`struct Plugin*`)
1933 * @param session the session
1934 * @return the network type
1936 static enum GNUNET_ATS_Network_Type
1937 http_client_plugin_get_network (void *cls,
1938 struct Session *session)
1940 return session->scope;
1945 * Function obtain the network type for an address.
1947 * @param cls closure (`struct Plugin *`)
1948 * @param address the address
1949 * @return the network type
1951 static enum GNUNET_ATS_Network_Type
1952 http_client_plugin_get_network_for_address (void *cls,
1953 const struct GNUNET_HELLO_Address *address)
1955 struct HTTP_Client_Plugin *plugin = cls;
1957 return http_common_get_network_for_address (plugin->env,
1963 * Session was idle, so disconnect it
1965 * @param cls the `struct Session` of the idle session
1966 * @param tc scheduler context
1969 client_session_timeout (void *cls,
1970 const struct GNUNET_SCHEDULER_TaskContext *tc)
1972 struct Session *s = cls;
1973 struct GNUNET_TIME_Relative left;
1975 s->timeout_task = NULL;
1976 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1977 if (0 != left.rel_value_us)
1979 /* not actually our turn yet, but let's at least update
1980 the monitor, it may think we're about to die ... */
1981 notify_session_monitor (s->plugin,
1983 GNUNET_TRANSPORT_SS_UPDATE);
1984 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1985 &client_session_timeout,
1990 "Session %p was idle for %s, disconnecting\n",
1992 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1994 GNUNET_assert (GNUNET_OK ==
1995 http_client_plugin_session_disconnect (s->plugin,
2001 * Creates a new outbound session the transport service will use to
2002 * send data to the peer
2004 * @param cls the plugin
2005 * @param address the address
2006 * @return the session or NULL of max connections exceeded
2008 static struct Session *
2009 http_client_plugin_get_session (void *cls,
2010 const struct GNUNET_HELLO_Address *address)
2012 struct HTTP_Client_Plugin *plugin = cls;
2014 struct sockaddr *sa;
2015 enum GNUNET_ATS_Network_Type net_type;
2019 GNUNET_assert (NULL != address->address);
2021 /* find existing session */
2022 s = client_lookup_session (plugin, address);
2026 /* create a new session */
2027 if (plugin->max_requests <= plugin->cur_requests)
2029 LOG (GNUNET_ERROR_TYPE_WARNING,
2030 "Maximum number of requests (%u) reached: "
2031 "cannot connect to peer `%s'\n",
2032 plugin->max_requests,
2033 GNUNET_i2s (&address->peer));
2037 /* Determine network location */
2038 net_type = GNUNET_ATS_NET_UNSPECIFIED;
2039 sa = http_common_socket_from_address (address->address,
2040 address->address_length,
2042 if (GNUNET_SYSERR == res)
2044 if (GNUNET_YES == res)
2046 GNUNET_assert (NULL != sa);
2047 if (AF_INET == sa->sa_family)
2049 salen = sizeof (struct sockaddr_in);
2051 else if (AF_INET6 == sa->sa_family)
2053 salen = sizeof (struct sockaddr_in6);
2055 net_type = plugin->env->get_address_type (plugin->env->cls, sa, salen);
2058 else if (GNUNET_NO == res)
2060 /* Cannot convert to sockaddr -> is external hostname */
2061 net_type = GNUNET_ATS_NET_WAN;
2063 if (GNUNET_ATS_NET_UNSPECIFIED == net_type)
2069 s = GNUNET_new (struct Session);
2071 s->address = GNUNET_HELLO_address_copy (address);
2072 s->scope = net_type;
2074 s->put.state = H_NOT_CONNECTED;
2075 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT);
2076 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
2077 &client_session_timeout,
2079 LOG (GNUNET_ERROR_TYPE_DEBUG,
2080 "Created new session %p for `%s' address `%s''\n",
2082 http_common_plugin_address_to_string (plugin->protocol,
2083 s->address->address,
2084 s->address->address_length),
2085 GNUNET_i2s (&s->address->peer));
2087 /* add new session */
2088 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2091 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2092 /* initiate new connection */
2093 if (GNUNET_SYSERR == client_connect (s))
2095 LOG (GNUNET_ERROR_TYPE_ERROR,
2096 "Cannot connect to peer `%s' address `%s''\n",
2097 http_common_plugin_address_to_string (plugin->protocol,
2098 s->address->address, s->address->address_length),
2099 GNUNET_i2s (&s->address->peer));
2100 client_delete_session (s);
2103 notify_session_monitor (plugin,
2105 GNUNET_TRANSPORT_SS_INIT);
2106 notify_session_monitor (plugin,
2108 GNUNET_TRANSPORT_SS_UP); /* or handshake? */
2114 * Setup http_client plugin
2116 * @param plugin the plugin handle
2117 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
2120 client_start (struct HTTP_Client_Plugin *plugin)
2122 curl_global_init (CURL_GLOBAL_ALL);
2123 plugin->curl_multi_handle = curl_multi_init ();
2125 if (NULL == plugin->curl_multi_handle)
2127 LOG (GNUNET_ERROR_TYPE_ERROR,
2128 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
2130 return GNUNET_SYSERR;
2137 * Another peer has suggested an address for this
2138 * peer and transport plugin. Check that this could be a valid
2139 * address. If so, consider adding it to the list
2142 * @param cls closure with the `struct Plugin`
2143 * @param addr pointer to the address
2144 * @param addrlen length of @a addr
2145 * @return #GNUNET_OK if this is a plausible address for this peer
2146 * and transport; always returns #GNUNET_NO (this is the client!)
2149 http_client_plugin_address_suggested (void *cls,
2153 /* A HTTP/S client does not have any valid address so:*/
2159 * Exit point from the plugin.
2161 * @param cls api as closure
2165 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2167 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2168 struct HTTP_Client_Plugin *plugin = api->cls;
2170 if (NULL == api->cls)
2176 LOG (GNUNET_ERROR_TYPE_DEBUG,
2177 _("Shutting down plugin `%s'\n"),
2179 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2180 &destroy_session_cb,
2182 if (NULL != plugin->client_perform_task)
2184 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
2185 plugin->client_perform_task = NULL;
2187 if (NULL != plugin->curl_multi_handle)
2189 curl_multi_cleanup (plugin->curl_multi_handle);
2190 plugin->curl_multi_handle = NULL;
2192 curl_global_cleanup ();
2193 LOG (GNUNET_ERROR_TYPE_DEBUG,
2194 _("Shutdown for plugin `%s' complete\n"),
2196 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
2197 GNUNET_free_non_null (plugin->proxy_hostname);
2198 GNUNET_free_non_null (plugin->proxy_username);
2199 GNUNET_free_non_null (plugin->proxy_password);
2200 GNUNET_free (plugin);
2209 * @param plugin the plugin handle
2210 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2213 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
2215 unsigned long long max_requests;
2218 /* Optional parameters */
2220 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2225 plugin->max_requests = max_requests;
2227 LOG (GNUNET_ERROR_TYPE_DEBUG,
2228 _("Maximum number of requests is %u\n"),
2229 plugin->max_requests);
2231 /* Read proxy configuration */
2233 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2236 &plugin->proxy_hostname))
2238 LOG (GNUNET_ERROR_TYPE_DEBUG,
2239 "Found proxy host: `%s'\n",
2240 plugin->proxy_hostname);
2241 /* proxy username */
2243 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2246 &plugin->proxy_username))
2248 LOG (GNUNET_ERROR_TYPE_DEBUG,
2249 "Found proxy username name: `%s'\n",
2250 plugin->proxy_username);
2253 /* proxy password */
2255 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2258 &plugin->proxy_password))
2260 LOG (GNUNET_ERROR_TYPE_DEBUG,
2261 "Found proxy password name: `%s'\n",
2262 plugin->proxy_password);
2267 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2272 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
2274 if (0 == strcmp(proxy_type, "HTTP"))
2275 plugin->proxytype = CURLPROXY_HTTP;
2276 else if (0 == strcmp(proxy_type, "SOCKS4"))
2277 plugin->proxytype = CURLPROXY_SOCKS4;
2278 else if (0 == strcmp(proxy_type, "SOCKS5"))
2279 plugin->proxytype = CURLPROXY_SOCKS5;
2280 else if (0 == strcmp(proxy_type, "SOCKS4A"))
2281 plugin->proxytype = CURLPROXY_SOCKS4A;
2282 else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
2283 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
2286 LOG (GNUNET_ERROR_TYPE_ERROR,
2287 _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
2290 GNUNET_free (proxy_type);
2291 GNUNET_free (plugin->proxy_hostname);
2292 plugin->proxy_hostname = NULL;
2293 GNUNET_free_non_null (plugin->proxy_username);
2294 plugin->proxy_username = NULL;
2295 GNUNET_free_non_null (plugin->proxy_password);
2296 plugin->proxy_password = NULL;
2298 return GNUNET_SYSERR;
2301 LOG (GNUNET_ERROR_TYPE_DEBUG,
2302 "Found proxy type: `%s'\n",
2306 /* proxy http tunneling */
2307 plugin->proxy_use_httpproxytunnel
2308 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2310 "PROXY_HTTP_TUNNELING");
2311 if (GNUNET_SYSERR == plugin->proxy_use_httpproxytunnel)
2312 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
2314 GNUNET_free_non_null (proxy_type);
2317 /* Should we emulate an XHR client for testing? */
2319 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2327 * Function to convert an address to a human-readable string.
2329 * @param cls closure
2330 * @param addr address to convert
2331 * @param addrlen address length
2332 * @return res string if conversion was successful, NULL otherwise
2335 http_client_plugin_address_to_string (void *cls,
2339 return http_common_plugin_address_to_string (PLUGIN_NAME,
2346 * Function that will be called whenever the transport service wants to
2347 * notify the plugin that a session is still active and in use and
2348 * therefore the session timeout for this session has to be updated
2350 * @param cls closure
2351 * @param peer which peer was the session for
2352 * @param session which session is being updated
2355 http_client_plugin_update_session_timeout (void *cls,
2356 const struct GNUNET_PeerIdentity *peer,
2357 struct Session *session)
2359 client_reschedule_session_timeout (session);
2364 * Function that will be called whenever the transport service wants to
2365 * notify the plugin that the inbound quota changed and that the plugin
2366 * should update it's delay for the next receive value
2368 * @param cls closure
2369 * @param peer which peer was the session for
2370 * @param s which session is being updated
2371 * @param delay new delay to use for receiving
2374 http_client_plugin_update_inbound_delay (void *cls,
2375 const struct GNUNET_PeerIdentity *peer,
2377 struct GNUNET_TIME_Relative delay)
2379 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
2380 LOG (GNUNET_ERROR_TYPE_DEBUG,
2381 "New inbound delay %s\n",
2382 GNUNET_STRINGS_relative_time_to_string (delay,
2384 if (s->recv_wakeup_task != NULL)
2386 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
2387 s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed (delay,
2388 &client_wake_up, s);
2394 * Return information about the given session to the
2397 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2398 * @param peer peer we send information about
2399 * @param value our `struct Session` to send information about
2400 * @return #GNUNET_OK (continue to iterate)
2403 send_session_info_iter (void *cls,
2404 const struct GNUNET_PeerIdentity *peer,
2407 struct HTTP_Client_Plugin *plugin = cls;
2408 struct Session *session = value;
2410 notify_session_monitor (plugin,
2412 GNUNET_TRANSPORT_SS_INIT);
2413 notify_session_monitor (plugin,
2415 GNUNET_TRANSPORT_SS_UP); /* FIXME: or handshake? */
2421 * Begin monitoring sessions of a plugin. There can only
2422 * be one active monitor per plugin (i.e. if there are
2423 * multiple monitors, the transport service needs to
2424 * multiplex the generated events over all of them).
2426 * @param cls closure of the plugin
2427 * @param sic callback to invoke, NULL to disable monitor;
2428 * plugin will being by iterating over all active
2429 * sessions immediately and then enter monitor mode
2430 * @param sic_cls closure for @a sic
2433 http_client_plugin_setup_monitor (void *cls,
2434 GNUNET_TRANSPORT_SessionInfoCallback sic,
2437 struct HTTP_Client_Plugin *plugin = cls;
2440 plugin->sic_cls = sic_cls;
2443 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2444 &send_session_info_iter,
2446 /* signal end of first iteration */
2447 sic (sic_cls, NULL, NULL);
2453 * Entry point for the plugin.
2456 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2458 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2459 struct GNUNET_TRANSPORT_PluginFunctions *api;
2460 struct HTTP_Client_Plugin *plugin;
2462 if (NULL == env->receive)
2464 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2465 initialze the plugin or the API */
2466 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2468 api->address_to_string = &http_client_plugin_address_to_string;
2469 api->string_to_address = &http_common_plugin_string_to_address;
2470 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2474 plugin = GNUNET_new (struct HTTP_Client_Plugin);
2476 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
2478 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2480 api->send = &http_client_plugin_send;
2481 api->disconnect_session = &http_client_plugin_session_disconnect;
2482 api->query_keepalive_factor = &http_client_query_keepalive_factor;
2483 api->disconnect_peer = &http_client_plugin_peer_disconnect;
2484 api->check_address = &http_client_plugin_address_suggested;
2485 api->get_session = &http_client_plugin_get_session;
2486 api->address_to_string = &http_client_plugin_address_to_string;
2487 api->string_to_address = &http_common_plugin_string_to_address;
2488 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2489 api->get_network = &http_client_plugin_get_network;
2490 api->get_network_for_address = &http_client_plugin_get_network_for_address;
2491 api->update_session_timeout = &http_client_plugin_update_session_timeout;
2492 api->update_inbound_delay = &http_client_plugin_update_inbound_delay;
2493 api->setup_monitor = &http_client_plugin_setup_monitor;
2495 plugin->name = "transport-https_client";
2496 plugin->protocol = "https";
2498 plugin->name = "transport-http_client";
2499 plugin->protocol = "http";
2501 plugin->last_tag = 1;
2503 if (GNUNET_SYSERR == client_configure_plugin (plugin))
2505 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2510 if (GNUNET_SYSERR == client_start (plugin))
2512 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2518 /* end of plugin_transport_http_client.c */