2 This file is part of GNUnet
3 (C) 2002-2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http_client.c
23 * @brief HTTP/S client transport plugin
24 * @author Matthias Wachs
25 * @author Christian Grothoff
29 #define PLUGIN_NAME "https_client"
30 #define HTTP_STAT_STR_CONNECTIONS "# HTTPS client connections"
31 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
32 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
34 #define PLUGIN_NAME "http_client"
35 #define HTTP_STAT_STR_CONNECTIONS "# HTTP client connections"
36 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
37 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
40 #define VERBOSE_CURL GNUNET_YES
42 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
44 #define ENABLE_PUT GNUNET_YES
45 #define ENABLE_GET GNUNET_YES
48 #include "gnunet_util_lib.h"
49 #include "gnunet_protocols.h"
50 #include "gnunet_transport_plugin.h"
51 #include "plugin_transport_http_common.h"
52 #include <curl/curl.h>
55 #define LOG(kind,...) GNUNET_log_from(kind, PLUGIN_NAME, __VA_ARGS__)
58 * Encapsulation of all of the state of the plugin.
60 struct HTTP_Client_Plugin;
64 * Message to send using http
69 * next pointer for double linked list
71 struct HTTP_Message *next;
74 * previous pointer for double linked list
76 struct HTTP_Message *prev;
79 * buffer containing data to send
84 * Continuation function to call once the transmission buffer
85 * has again space available. NULL if there is no
86 * continuation to call.
88 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
91 * Closure for @e transmit_cont.
93 void *transmit_cont_cls;
96 * amount of data already sent
109 * Session handle for connections.
115 * A connection handle
118 struct ConnectionHandle
121 * The curl easy handle
126 * The related session
133 * Session handle for connections.
138 * The URL to connect to
145 struct GNUNET_HELLO_Address *address;
148 * Pointer to the global plugin struct.
150 struct HTTP_Client_Plugin *plugin;
153 * Handle for the HTTP PUT request.
155 struct ConnectionHandle put;
158 * Handle for the HTTP GET request.
160 struct ConnectionHandle get;
163 * next pointer for double linked list
165 struct HTTP_Message *msg_head;
168 * previous pointer for double linked list
170 struct HTTP_Message *msg_tail;
173 * Message stream tokenizer for incoming data
175 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
178 * Session timeout task
180 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
183 * Session timeout task
185 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
188 * Task to wake up client receive handle when receiving is allowed again
190 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
193 * Absolute time when to receive data again.
194 * Used for receive throttling.
196 struct GNUNET_TIME_Absolute next_receive;
199 * When does this session time out.
201 struct GNUNET_TIME_Absolute timeout;
204 * Number of bytes waiting for transmission to this peer.
206 unsigned long long bytes_in_queue;
209 * Outbound overhead due to HTTP connection
210 * Add to next message of this session when calling callback
215 * Number of messages waiting for transmission to this peer.
217 unsigned int msgs_in_queue;
220 * ATS network type in NBO
222 uint32_t ats_address_network_type;
225 * Is the client PUT handle currently paused?
230 * Is the client PUT handle disconnect in progress?
232 int put_tmp_disconnecting;
235 * Is the client PUT handle temporarily disconnected?
237 int put_tmp_disconnected;
240 * We received data to send while disconnecting, reconnect immediately
242 int put_reconnect_required;
247 * Encapsulation of all of the state of the plugin.
249 struct HTTP_Client_Plugin
254 struct GNUNET_TRANSPORT_PluginEnvironment *env;
259 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
262 * Function to call about session status changes.
264 GNUNET_TRANSPORT_SessionInfoCallback sic;
267 * Closure for @e sic.
282 * Proxy configuration: hostname or ip of the proxy server
284 char *proxy_hostname;
287 * Username for the proxy server
289 char *proxy_username;
292 * Password for the proxy server
294 char *proxy_password;
299 CURLM *curl_multi_handle;
304 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
307 * Type of proxy server:
309 * Valid values as supported by curl:
310 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
311 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
313 curl_proxytype proxytype;
316 * Use proxy tunneling:
317 * Tunnel all operations through a given HTTP instead of have the proxy
318 * evaluate the HTTP request
320 * Default: #GNUNET_NO, #GNUNET_YES experimental
322 int proxy_use_httpproxytunnel;
325 * My options to be included in the address
330 * Maximum number of sockets the plugin can use
331 * Each http inbound /outbound connections are two connections
333 unsigned int max_connections;
336 * Current number of sockets the plugin can use
337 * Each http inbound /outbound connections are two connections
339 unsigned int cur_connections;
342 * Last used unique HTTP connection tag
357 * Should we emulate an XHR client for testing?
365 * If a session monitor is attached, notify it about the new
368 * @param plugin our plugin
369 * @param session session that changed state
370 * @param state new state of the session
373 notify_session_monitor (struct HTTP_Client_Plugin *plugin,
374 struct Session *session,
375 enum GNUNET_TRANSPORT_SessionState state)
377 struct GNUNET_TRANSPORT_SessionInfo info;
379 if (NULL == plugin->sic)
381 memset (&info, 0, sizeof (info));
383 info.is_inbound = GNUNET_NO;
384 info.num_msg_pending = session->msgs_in_queue;
385 info.num_bytes_pending = session->bytes_in_queue;
386 info.receive_delay = session->next_receive;
387 info.session_timeout = session->timeout;
388 info.address = session->address;
389 plugin->sic (plugin->sic_cls,
396 * Delete session @a s.
398 * @param s the session to delete
401 client_delete_session (struct Session *s)
403 struct HTTP_Client_Plugin *plugin = s->plugin;
404 struct HTTP_Message *pos;
405 struct HTTP_Message *next;
408 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
410 GNUNET_SCHEDULER_cancel (s->timeout_task);
411 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
412 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
414 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
416 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
417 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
419 if (GNUNET_SCHEDULER_NO_TASK != s->recv_wakeup_task)
421 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
422 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
424 GNUNET_assert (GNUNET_OK ==
425 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
428 if (NULL != s->put.easyhandle)
430 LOG (GNUNET_ERROR_TYPE_DEBUG,
431 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
434 GNUNET_i2s (&s->address->peer));
436 /* remove curl handle from multi handle */
437 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
439 GNUNET_break (CURLM_OK == mret);
440 curl_easy_cleanup (s->put.easyhandle);
441 s->put.easyhandle = NULL;
443 if (NULL != s->get.easyhandle)
445 LOG (GNUNET_ERROR_TYPE_DEBUG,
446 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
447 s, s->get.easyhandle,
448 GNUNET_i2s (&s->address->peer));
449 /* remove curl handle from multi handle */
450 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
452 GNUNET_break (CURLM_OK == mret);
453 curl_easy_cleanup (s->get.easyhandle);
454 GNUNET_assert (plugin->cur_connections > 0);
455 plugin->cur_connections--;
456 s->get.easyhandle = NULL;
458 GNUNET_STATISTICS_set (plugin->env->stats,
459 HTTP_STAT_STR_CONNECTIONS,
460 plugin->cur_connections,
463 while (NULL != (pos = next))
466 GNUNET_CONTAINER_DLL_remove (s->msg_head,
469 GNUNET_assert (0 < s->msgs_in_queue);
471 GNUNET_assert (pos->size <= s->bytes_in_queue);
472 s->bytes_in_queue -= pos->size;
473 if (NULL != pos->transmit_cont)
474 pos->transmit_cont (pos->transmit_cont_cls,
478 pos->pos + s->overhead);
482 GNUNET_assert (0 == s->msgs_in_queue);
483 GNUNET_assert (0 == s->bytes_in_queue);
484 notify_session_monitor (plugin,
486 GNUNET_TRANSPORT_SS_DOWN);
487 if (NULL != s->msg_tk)
489 GNUNET_SERVER_mst_destroy (s->msg_tk);
492 GNUNET_HELLO_address_free (s->address);
493 GNUNET_free (s->url);
499 * Increment session timeout due to activity for session @a s.
501 * @param s the session
504 client_reschedule_session_timeout (struct Session *s)
506 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
507 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
512 * Task performing curl operations
514 * @param cls plugin as closure
515 * @param tc gnunet scheduler task context
518 client_run (void *cls,
519 const struct GNUNET_SCHEDULER_TaskContext *tc);
523 * Function setting up file descriptors and scheduling task to run
525 * @param plugin the plugin as closure
526 * @param now schedule task in 1ms, regardless of what curl may say
527 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
530 client_schedule (struct HTTP_Client_Plugin *plugin,
537 struct GNUNET_NETWORK_FDSet *grs;
538 struct GNUNET_NETWORK_FDSet *gws;
541 struct GNUNET_TIME_Relative timeout;
543 /* Cancel previous scheduled task */
544 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
546 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
547 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
553 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
554 if (mret != CURLM_OK)
556 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
557 "curl_multi_fdset", __FILE__, __LINE__,
558 curl_multi_strerror (mret));
559 return GNUNET_SYSERR;
561 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
563 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
565 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
566 if (now == GNUNET_YES)
567 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
569 if (mret != CURLM_OK)
571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
572 _("%s failed at %s:%d: `%s'\n"),
573 "curl_multi_timeout", __FILE__, __LINE__,
574 curl_multi_strerror (mret));
575 return GNUNET_SYSERR;
578 grs = GNUNET_NETWORK_fdset_create ();
579 gws = GNUNET_NETWORK_fdset_create ();
580 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
581 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
583 plugin->client_perform_task =
584 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
586 &client_run, plugin);
587 GNUNET_NETWORK_fdset_destroy (gws);
588 GNUNET_NETWORK_fdset_destroy (grs);
596 * @param curl the curl easy handle
597 * @param type message type
598 * @param data data to log, NOT a 0-terminated string
599 * @param size data length
600 * @param cls the closure
604 client_log (CURL *curl,
610 struct ConnectionHandle *ch = cls;
611 const char *ttype = "UNSPECIFIED";
614 if (! ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT)))
621 case CURLINFO_HEADER_IN:
624 case CURLINFO_HEADER_OUT:
625 ttype = "HEADER_OUT";
627 GNUNET_assert (NULL != ch);
628 GNUNET_assert (NULL != ch->easyhandle);
629 GNUNET_assert (NULL != ch->s);
630 ch->s->overhead += size;
633 ttype = "UNSPECIFIED";
636 memcpy (text, data, size);
637 if (text[size - 1] == '\n')
644 text[size + 1] = '\0';
646 LOG (GNUNET_ERROR_TYPE_DEBUG,
647 "Connection %p %s: %s",
656 * Connect GET connection for a session
658 * @param s the session to connect
659 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
662 client_connect_get (struct Session *s);
666 * Connect a HTTP put connection
668 * @param s the session to connect
669 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
672 client_connect_put (struct Session *s);
676 * Function that can be used by the transport service to transmit
677 * a message using the plugin. Note that in the case of a
678 * peer disconnecting, the continuation MUST be called
679 * prior to the disconnect notification itself. This function
680 * will be called with this peer's HELLO message to initiate
681 * a fresh connection to another peer.
684 * @param s which session must be used
685 * @param msgbuf the message to transmit
686 * @param msgbuf_size number of bytes in @a msgbuf
687 * @param priority how important is the message (most plugins will
688 * ignore message priority and just FIFO)
689 * @param to how long to wait at most for the transmission (does not
690 * require plugins to discard the message after the timeout,
691 * just advisory for the desired delay; most plugins will ignore
693 * @param cont continuation to call once the message has
694 * been transmitted (or if the transport is ready
695 * for the next transmission call; or if the
696 * peer disconnected...); can be NULL
697 * @param cont_cls closure for cont
698 * @return number of bytes used (on the physical network, with overheads);
699 * -1 on hard errors (i.e. address invalid); 0 is a legal value
700 * and does NOT mean that the message was not transmitted (DV)
703 http_client_plugin_send (void *cls,
707 unsigned int priority,
708 struct GNUNET_TIME_Relative to,
709 GNUNET_TRANSPORT_TransmitContinuation cont,
712 struct HTTP_Client_Plugin *plugin = cls;
713 struct HTTP_Message *msg;
716 LOG (GNUNET_ERROR_TYPE_DEBUG,
717 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
718 s, s->put.easyhandle,
719 msgbuf_size, GNUNET_i2s (&s->address->peer));
721 /* create new message and schedule */
722 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
724 msg->size = msgbuf_size;
726 msg->buf = (char *) &msg[1];
727 msg->transmit_cont = cont;
728 msg->transmit_cont_cls = cont_cls;
729 memcpy (msg->buf, msgbuf, msgbuf_size);
730 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head,
734 s->bytes_in_queue += msg->size;
736 GNUNET_asprintf (&stat_txt,
737 "# bytes currently in %s_client buffers",
739 GNUNET_STATISTICS_update (plugin->env->stats,
740 stat_txt, msgbuf_size, GNUNET_NO);
741 GNUNET_free (stat_txt);
742 notify_session_monitor (plugin,
744 GNUNET_TRANSPORT_SS_UP);
745 if (GNUNET_YES == s->put_tmp_disconnecting)
747 /* PUT connection is currently getting disconnected */
748 s->put_reconnect_required = GNUNET_YES;
749 LOG (GNUNET_ERROR_TYPE_DEBUG,
750 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
755 if (GNUNET_YES == s->put_paused)
757 /* PUT connection was paused, unpause */
758 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
759 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
760 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
761 LOG (GNUNET_ERROR_TYPE_DEBUG,
762 "Session %p/connection %p: unpausing connection\n",
763 s, s->put.easyhandle);
764 s->put_paused = GNUNET_NO;
765 if (NULL != s->put.easyhandle)
766 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
768 else if (GNUNET_YES == s->put_tmp_disconnected)
770 /* PUT connection was disconnected, reconnect */
771 LOG (GNUNET_ERROR_TYPE_DEBUG,
772 "Session %p: Reconnecting PUT connection\n",
774 s->put_tmp_disconnected = GNUNET_NO;
775 GNUNET_break (NULL == s->put.easyhandle);
776 if (GNUNET_SYSERR == client_connect_put (s))
777 return GNUNET_SYSERR;
779 client_schedule (s->plugin,
786 * Disconnect a session
788 * @param cls the `struct HTTP_Client_Plugin *`
790 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
793 http_client_plugin_session_disconnect (void *cls,
796 struct HTTP_Client_Plugin *plugin = cls;
798 LOG (GNUNET_ERROR_TYPE_DEBUG,
799 "Session %p: notifying transport about ending session\n",s);
800 plugin->env->session_end (plugin->env->cls,
803 client_delete_session (s);
805 /* Re-schedule since handles have changed */
806 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
808 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
809 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
811 client_schedule (plugin, GNUNET_YES);
818 * Function that is called to get the keepalive factor.
819 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
820 * calculate the interval between keepalive packets.
822 * @param cls closure with the `struct Plugin`
823 * @return keepalive factor
826 http_client_query_keepalive_factor (void *cls)
833 * Callback to destroys all sessions on exit.
835 * @param cls the `struct HTTP_Client_Plugin *`
836 * @param peer identity of the peer
837 * @param value the `struct Session *`
838 * @return #GNUNET_OK (continue iterating)
841 destroy_session_cb (void *cls,
842 const struct GNUNET_PeerIdentity *peer,
845 struct HTTP_Client_Plugin *plugin = cls;
846 struct Session *session = value;
848 http_client_plugin_session_disconnect (plugin, session);
854 * Function that can be used to force the plugin to disconnect
855 * from the given peer and cancel all previous transmissions
856 * (and their continuationc).
859 * @param target peer from which to disconnect
862 http_client_plugin_peer_disconnect (void *cls,
863 const struct GNUNET_PeerIdentity *target)
865 struct HTTP_Client_Plugin *plugin = cls;
867 LOG (GNUNET_ERROR_TYPE_DEBUG,
868 "Transport tells me to disconnect `%s'\n",
869 GNUNET_i2s (target));
870 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
878 * Closure for #session_lookup_client_by_address().
880 struct SessionClientCtx
883 * Address we are looking for.
885 const struct GNUNET_HELLO_Address *address;
888 * Session that was found.
895 * Locate the seession object for a given address.
897 * @param cls the `struct SessionClientCtx *`
898 * @param key peer identity
899 * @param value the `struct Session` to check
900 * @return #GNUNET_NO if found, #GNUNET_OK if not
903 session_lookup_client_by_address (void *cls,
904 const struct GNUNET_PeerIdentity *key,
907 struct SessionClientCtx *sc_ctx = cls;
908 struct Session *s = value;
910 if (0 == GNUNET_HELLO_address_cmp (sc_ctx->address,
921 * Check if a sessions exists for an specific address
923 * @param plugin the plugin
924 * @param address the address
925 * @return the session or NULL
927 static struct Session *
928 client_lookup_session (struct HTTP_Client_Plugin *plugin,
929 const struct GNUNET_HELLO_Address *address)
931 struct SessionClientCtx sc_ctx;
933 sc_ctx.address = address;
935 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
936 &session_lookup_client_by_address,
943 * When we have nothing to transmit, we pause the HTTP PUT
944 * after a while (so that gnurl stops asking). This task
945 * is the delayed task that actually pauses the PUT.
947 * @param cls the `struct Session *` with the put
948 * @param tc scheduler context
951 client_put_disconnect (void *cls,
952 const struct GNUNET_SCHEDULER_TaskContext *tc)
954 struct Session *s = cls;
956 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
957 LOG (GNUNET_ERROR_TYPE_DEBUG,
958 "Session %p/connection %p: will be disconnected due to no activity\n",
959 s, s->put.easyhandle);
960 s->put_paused = GNUNET_NO;
961 s->put_tmp_disconnecting = GNUNET_YES;
962 if (NULL != s->put.easyhandle)
963 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
964 client_schedule (s->plugin, GNUNET_YES);
969 * Callback method used with libcurl
970 * Method is called when libcurl needs to read data during sending
972 * @param stream pointer where to write data
973 * @param size size of an individual element
974 * @param nmemb count of elements that can be written to the buffer
975 * @param cls our `struct Session`
976 * @return bytes written to stream, returning 0 will terminate connection!
979 client_send_cb (void *stream,
984 struct Session *s = cls;
985 struct HTTP_Client_Plugin *plugin = s->plugin;
986 struct HTTP_Message *msg = s->msg_head;
990 if (GNUNET_YES == s->put_tmp_disconnecting)
992 LOG (GNUNET_ERROR_TYPE_DEBUG,
993 "Session %p/connection %p: disconnect due to inactivity\n",
994 s, s->put.easyhandle);
1000 if (GNUNET_YES == plugin->emulate_xhr)
1002 LOG (GNUNET_ERROR_TYPE_DEBUG,
1003 "Session %p/connection %p: PUT request finished\n",
1004 s, s->put.easyhandle);
1005 s->put_tmp_disconnecting = GNUNET_YES;
1009 LOG (GNUNET_ERROR_TYPE_DEBUG,
1010 "Session %p/connection %p: nothing to send, suspending\n",
1011 s, s->put.easyhandle);
1012 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
1013 &client_put_disconnect,
1015 s->put_paused = GNUNET_YES;
1016 return CURL_READFUNC_PAUSE;
1019 GNUNET_assert (msg->pos < msg->size);
1020 /* calculate how much fits in buffer */
1021 len = GNUNET_MIN (msg->size - msg->pos,
1023 memcpy (stream, &msg->buf[msg->pos], len);
1025 if (msg->pos == msg->size)
1027 LOG (GNUNET_ERROR_TYPE_DEBUG,
1028 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
1029 s, s->put.easyhandle, msg->size, msg->pos);
1030 /* Calling transmit continuation */
1031 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1034 GNUNET_assert (0 < s->msgs_in_queue);
1036 GNUNET_assert (msg->size <= s->bytes_in_queue);
1037 s->bytes_in_queue -= msg->size;
1038 if (NULL != msg->transmit_cont)
1039 msg->transmit_cont (msg->transmit_cont_cls,
1043 msg->size + s->overhead);
1047 notify_session_monitor (plugin,
1049 GNUNET_TRANSPORT_SS_UP);
1050 GNUNET_asprintf (&stat_txt,
1051 "# bytes currently in %s_client buffers",
1053 GNUNET_STATISTICS_update (plugin->env->stats,
1057 GNUNET_free (stat_txt);
1058 GNUNET_asprintf (&stat_txt,
1059 "# bytes transmitted via %s_client",
1061 GNUNET_STATISTICS_update (plugin->env->stats,
1065 GNUNET_free (stat_txt);
1071 * Wake up a curl handle which was suspended
1073 * @param cls the session
1074 * @param tc task context
1077 client_wake_up (void *cls,
1078 const struct GNUNET_SCHEDULER_TaskContext *tc)
1080 struct Session *s = cls;
1082 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1083 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1085 LOG (GNUNET_ERROR_TYPE_DEBUG,
1086 "Session %p/connection %p: Waking up GET handle\n",
1087 s, s->get.easyhandle);
1088 if (GNUNET_YES == s->put_paused)
1090 /* PUT connection was paused, unpause */
1091 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
1092 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
1093 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
1094 s->put_paused = GNUNET_NO;
1095 if (NULL != s->put.easyhandle)
1096 curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
1098 if (NULL != s->get.easyhandle)
1099 curl_easy_pause (s->get.easyhandle,
1105 * Callback for message stream tokenizer
1107 * @param cls the session
1108 * @param client not used
1109 * @param message the message received
1110 * @return always #GNUNET_OK
1113 client_receive_mst_cb (void *cls,
1115 const struct GNUNET_MessageHeader *message)
1117 struct Session *s = cls;
1118 struct HTTP_Client_Plugin *plugin;
1119 struct GNUNET_TIME_Relative delay;
1120 struct GNUNET_ATS_Information atsi;
1124 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1125 atsi.value = s->ats_address_network_type;
1126 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1128 delay = s->plugin->env->receive (plugin->env->cls,
1132 plugin->env->update_address_metrics (plugin->env->cls,
1136 GNUNET_asprintf (&stat_txt,
1137 "# bytes received via %s_client",
1139 GNUNET_STATISTICS_update (plugin->env->stats,
1141 ntohs (message->size),
1143 GNUNET_free (stat_txt);
1145 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1146 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
1148 LOG (GNUNET_ERROR_TYPE_DEBUG,
1149 "Client: peer `%s' address `%s' next read delayed for %s\n",
1150 GNUNET_i2s (&s->address->peer),
1151 http_common_plugin_address_to_string (s->plugin->protocol,
1152 s->address->address,
1153 s->address->address_length),
1154 GNUNET_STRINGS_relative_time_to_string (delay,
1157 client_reschedule_session_timeout (s);
1163 * Callback method used with libcurl when data for a PUT connection are
1164 * received. We do not expect data here, so we just discard it.
1166 * @param stream pointer where to write data
1167 * @param size size of an individual element
1168 * @param nmemb count of elements that can be written to the buffer
1169 * @param cls destination pointer, passed to the libcurl handle
1170 * @return bytes read from stream
1173 client_receive_put (void *stream,
1178 return size * nmemb;
1183 * Callback method used with libcurl when data for a GET connection are
1184 * received. Forward to MST
1186 * @param stream pointer where to write data
1187 * @param size size of an individual element
1188 * @param nmemb count of elements that can be written to the buffer
1189 * @param cls destination pointer, passed to the libcurl handle
1190 * @return bytes read from stream
1193 client_receive (void *stream,
1198 struct Session *s = cls;
1199 struct GNUNET_TIME_Absolute now;
1200 size_t len = size * nmemb;
1202 LOG (GNUNET_ERROR_TYPE_DEBUG,
1203 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
1204 s, s->get.easyhandle,
1205 len, GNUNET_i2s (&s->address->peer));
1206 now = GNUNET_TIME_absolute_get ();
1207 if (now.abs_value_us < s->next_receive.abs_value_us)
1209 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1210 struct GNUNET_TIME_Relative delta
1211 = GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1213 LOG (GNUNET_ERROR_TYPE_DEBUG,
1214 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1217 GNUNET_STRINGS_relative_time_to_string (delta,
1219 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1221 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1222 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1225 = GNUNET_SCHEDULER_add_delayed (delta,
1228 return CURL_WRITEFUNC_PAUSE;
1230 if (NULL == s->msg_tk)
1231 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb,
1233 GNUNET_SERVER_mst_receive (s->msg_tk,
1244 * Task performing curl operations
1246 * @param cls plugin as closure
1247 * @param tc scheduler task context
1250 client_run (void *cls,
1251 const struct GNUNET_SCHEDULER_TaskContext *tc)
1253 struct HTTP_Client_Plugin *plugin = cls;
1255 long http_statuscode;
1260 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1261 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1266 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1269 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1271 CURL *easy_h = msg->easy_handle;
1272 struct Session *s = NULL;
1273 char *d = NULL; /* curl requires 'd' to be a 'char *' */
1278 LOG (GNUNET_ERROR_TYPE_DEBUG,
1279 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1281 curl_easy_strerror (msg->data.result), running);
1285 GNUNET_assert (CURLE_OK ==
1286 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1287 s = (struct Session *) d;
1288 GNUNET_assert (NULL != s);
1289 if (msg->msg == CURLMSG_DONE)
1291 GNUNET_break (CURLE_OK ==
1292 curl_easy_getinfo (easy_h,
1293 CURLINFO_RESPONSE_CODE,
1295 if (easy_h == s->put.easyhandle)
1297 if ((0 != msg->data.result) || (http_statuscode != 200))
1299 LOG (GNUNET_ERROR_TYPE_DEBUG,
1300 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1301 s, msg->easy_handle,
1302 GNUNET_i2s (&s->address->peer),
1305 curl_easy_strerror (msg->data.result));
1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1309 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1310 s, msg->easy_handle,
1311 GNUNET_i2s (&s->address->peer));
1312 if (NULL == s->get.easyhandle)
1314 /* Disconnect other transmission direction and tell transport */
1317 curl_multi_remove_handle (plugin->curl_multi_handle,
1319 curl_easy_cleanup (easy_h);
1320 GNUNET_assert (plugin->cur_connections > 0);
1321 plugin->cur_connections--;
1322 s->put_tmp_disconnecting = GNUNET_NO;
1323 s->put_tmp_disconnected = GNUNET_YES;
1324 s->put.easyhandle = NULL;
1325 s->put.easyhandle = NULL;
1329 * Handling a rare case:
1330 * plugin_send was called during temporary put disconnect,
1331 * reconnect required after connection was disconnected
1333 if (GNUNET_YES == s->put_reconnect_required)
1335 s->put_reconnect_required = GNUNET_NO;
1336 if (GNUNET_SYSERR == client_connect_put (s))
1338 GNUNET_break (s->put.easyhandle == NULL);
1339 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1343 if (easy_h == s->get.easyhandle)
1345 if ((0 != msg->data.result) || (http_statuscode != 200))
1347 LOG (GNUNET_ERROR_TYPE_DEBUG,
1348 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1351 GNUNET_i2s (&s->address->peer),
1354 curl_easy_strerror (msg->data.result));
1358 LOG (GNUNET_ERROR_TYPE_DEBUG,
1359 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1362 GNUNET_i2s (&s->address->peer));
1363 /* Disconnect other transmission direction and tell transport */
1364 s->get.easyhandle = NULL;
1366 /* FIXME: who calls curl_multi_remove on 'easy_h' now!? */
1367 GNUNET_assert (plugin->cur_connections > 0);
1368 plugin->cur_connections--;
1369 /* If we are emulating an XHR client we need to make another GET
1372 if (GNUNET_YES == plugin->emulate_xhr)
1374 if (GNUNET_SYSERR == client_connect_get (s))
1375 http_client_plugin_session_disconnect (plugin, s);
1377 http_client_plugin_session_disconnect (plugin, s);
1382 while (mret == CURLM_CALL_MULTI_PERFORM);
1383 client_schedule (plugin, GNUNET_NO);
1388 * Connect GET connection for a session
1390 * @param s the session to connect
1391 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1394 client_connect_get (struct Session *s)
1398 /* create get connection */
1399 s->get.easyhandle = curl_easy_init ();
1402 curl_easy_setopt (s->get.easyhandle, CURLOPT_VERBOSE, 1L);
1403 curl_easy_setopt (s->get.easyhandle, CURLOPT_DEBUGFUNCTION, &client_log);
1404 curl_easy_setopt (s->get.easyhandle, CURLOPT_DEBUGDATA, &s->get);
1407 curl_easy_setopt (s->get.easyhandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1409 struct HttpAddress *ha;
1411 ha = (struct HttpAddress *) s->address->address;
1413 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1414 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1416 curl_easy_setopt (s->get.easyhandle, CURLOPT_SSL_VERIFYPEER, 1L);
1417 curl_easy_setopt (s->get.easyhandle, CURLOPT_SSL_VERIFYHOST, 2L);
1421 curl_easy_setopt (s->get.easyhandle, CURLOPT_SSL_VERIFYPEER, 0);
1422 curl_easy_setopt (s->get.easyhandle, CURLOPT_SSL_VERIFYHOST, 0);
1425 curl_easy_setopt (s->get.easyhandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1426 curl_easy_setopt (s->get.easyhandle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1428 curl_easy_setopt (s->get.easyhandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1429 curl_easy_setopt (s->get.easyhandle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1432 if (NULL != s->plugin->proxy_hostname)
1434 curl_easy_setopt (s->get.easyhandle, CURLOPT_PROXY, s->plugin->proxy_hostname);
1435 curl_easy_setopt (s->get.easyhandle, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1436 if (NULL != s->plugin->proxy_username)
1437 curl_easy_setopt (s->get.easyhandle, CURLOPT_PROXYUSERNAME,
1438 s->plugin->proxy_username);
1439 if (NULL != s->plugin->proxy_password)
1440 curl_easy_setopt (s->get.easyhandle, CURLOPT_PROXYPASSWORD,
1441 s->plugin->proxy_password);
1442 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1443 curl_easy_setopt (s->get.easyhandle, CURLOPT_HTTPPROXYTUNNEL,
1444 s->plugin->proxy_use_httpproxytunnel);
1447 if (GNUNET_YES == s->plugin->emulate_xhr)
1451 GNUNET_asprintf(&url, "%s,1", s->url);
1452 curl_easy_setopt (s->get.easyhandle, CURLOPT_URL, url);
1455 curl_easy_setopt (s->get.easyhandle, CURLOPT_URL, s->url);
1456 //curl_easy_setopt (s->get.easyhandle, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1457 //curl_easy_setopt (s->get.easyhandle, CURLOPT_WRITEHEADER, ps);
1458 curl_easy_setopt (s->get.easyhandle, CURLOPT_READFUNCTION, client_send_cb);
1459 curl_easy_setopt (s->get.easyhandle, CURLOPT_READDATA, s);
1460 curl_easy_setopt (s->get.easyhandle, CURLOPT_WRITEFUNCTION, client_receive);
1461 curl_easy_setopt (s->get.easyhandle, CURLOPT_WRITEDATA, s);
1462 /* No timeout by default, timeout done with session timeout */
1463 curl_easy_setopt (s->get.easyhandle, CURLOPT_TIMEOUT, 0);
1464 curl_easy_setopt (s->get.easyhandle, CURLOPT_PRIVATE, s);
1465 curl_easy_setopt (s->get.easyhandle, CURLOPT_CONNECTTIMEOUT_MS,
1466 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1467 curl_easy_setopt (s->get.easyhandle, CURLOPT_BUFFERSIZE,
1468 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1469 #if CURL_TCP_NODELAY
1470 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1472 curl_easy_setopt (s->get.easyhandle, CURLOPT_FOLLOWLOCATION, 0);
1474 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1476 if (CURLM_OK != mret)
1478 LOG (GNUNET_ERROR_TYPE_ERROR,
1479 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1481 curl_multi_strerror (mret));
1482 curl_easy_cleanup (s->get.easyhandle);
1483 s->get.easyhandle = NULL;
1485 s->get.easyhandle = NULL;
1487 return GNUNET_SYSERR;
1489 s->plugin->cur_connections++;
1495 * Connect a HTTP put connection
1497 * @param s the session to connect
1498 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1501 client_connect_put (struct Session *s)
1505 /* create put connection */
1506 LOG (GNUNET_ERROR_TYPE_DEBUG,
1507 "Session %p: Init PUT handle\n", s);
1508 s->put.easyhandle = curl_easy_init ();
1511 curl_easy_setopt (s->put.easyhandle, CURLOPT_VERBOSE, 1L);
1512 curl_easy_setopt (s->put.easyhandle, CURLOPT_DEBUGFUNCTION, &client_log);
1513 curl_easy_setopt (s->put.easyhandle, CURLOPT_DEBUGDATA, &s->put);
1516 curl_easy_setopt (s->put.easyhandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1518 struct HttpAddress *ha;
1519 ha = (struct HttpAddress *) s->address->address;
1521 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1522 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1524 curl_easy_setopt (s->put.easyhandle, CURLOPT_SSL_VERIFYPEER, 1L);
1525 curl_easy_setopt (s->put.easyhandle, CURLOPT_SSL_VERIFYHOST, 2L);
1529 curl_easy_setopt (s->put.easyhandle, CURLOPT_SSL_VERIFYPEER, 0);
1530 curl_easy_setopt (s->put.easyhandle, CURLOPT_SSL_VERIFYHOST, 0);
1533 curl_easy_setopt (s->put.easyhandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1534 curl_easy_setopt (s->put.easyhandle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1536 curl_easy_setopt (s->put.easyhandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1537 curl_easy_setopt (s->put.easyhandle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1539 if (s->plugin->proxy_hostname != NULL)
1541 curl_easy_setopt (s->put.easyhandle, CURLOPT_PROXY, s->plugin->proxy_hostname);
1542 curl_easy_setopt (s->put.easyhandle, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1543 if (NULL != s->plugin->proxy_username)
1544 curl_easy_setopt (s->put.easyhandle, CURLOPT_PROXYUSERNAME,
1545 s->plugin->proxy_username);
1546 if (NULL != s->plugin->proxy_password)
1547 curl_easy_setopt (s->put.easyhandle, CURLOPT_PROXYPASSWORD,
1548 s->plugin->proxy_password);
1549 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1550 curl_easy_setopt (s->put.easyhandle, CURLOPT_HTTPPROXYTUNNEL,
1551 s->plugin->proxy_use_httpproxytunnel);
1554 curl_easy_setopt (s->put.easyhandle, CURLOPT_URL, s->url);
1555 curl_easy_setopt (s->put.easyhandle, CURLOPT_UPLOAD, 1L);
1556 //curl_easy_setopt (s->put.easyhandle, CURLOPT_HEADERFUNCTION, &client_curl_header);
1557 //curl_easy_setopt (s->put.easyhandle, CURLOPT_WRITEHEADER, ps);
1558 curl_easy_setopt (s->put.easyhandle, CURLOPT_READFUNCTION, client_send_cb);
1559 curl_easy_setopt (s->put.easyhandle, CURLOPT_READDATA, s);
1560 curl_easy_setopt (s->put.easyhandle, CURLOPT_WRITEFUNCTION, client_receive_put);
1561 curl_easy_setopt (s->put.easyhandle, CURLOPT_WRITEDATA, s);
1562 /* No timeout by default, timeout done with session timeout */
1563 curl_easy_setopt (s->put.easyhandle, CURLOPT_TIMEOUT, 0);
1564 curl_easy_setopt (s->put.easyhandle, CURLOPT_PRIVATE, s);
1565 curl_easy_setopt (s->put.easyhandle, CURLOPT_CONNECTTIMEOUT_MS,
1566 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1567 curl_easy_setopt (s->put.easyhandle, CURLOPT_BUFFERSIZE,
1568 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1569 #if CURL_TCP_NODELAY
1570 curl_easy_setopt (s->put.easyhandle, CURLOPT_TCP_NODELAY, 1);
1572 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1574 if (CURLM_OK != mret)
1576 LOG (GNUNET_ERROR_TYPE_ERROR,
1577 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1579 curl_multi_strerror (mret));
1580 curl_easy_cleanup (s->put.easyhandle);
1581 s->put.easyhandle = NULL;
1582 s->put.easyhandle = NULL;
1584 s->put_tmp_disconnected = GNUNET_YES;
1585 return GNUNET_SYSERR;
1587 s->put_tmp_disconnected = GNUNET_NO;
1588 s->plugin->cur_connections++;
1595 * Connect both PUT and GET connection for a session
1597 * @param s the session to connect
1598 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1601 client_connect (struct Session *s)
1603 struct HTTP_Client_Plugin *plugin = s->plugin;
1604 int res = GNUNET_OK;
1607 if (NULL == http_common_plugin_address_to_string(plugin->protocol,
1608 s->address->address, s->address->address_length))
1610 LOG(GNUNET_ERROR_TYPE_DEBUG, "Invalid address peer `%s'\n",
1611 GNUNET_i2s(&s->address->peer));
1612 return GNUNET_SYSERR;
1615 GNUNET_asprintf(&s->url, "%s/%s;%u",
1616 http_common_plugin_address_to_url(NULL, s->address->address,
1617 s->address->address_length),
1618 GNUNET_i2s_full(plugin->env->my_identity), plugin->last_tag);
1621 LOG (GNUNET_ERROR_TYPE_DEBUG,
1622 "Initiating outbound session peer `%s' using address `%s'\n",
1623 GNUNET_i2s (&s->address->peer), s->url);
1625 if (GNUNET_SYSERR == client_connect_get (s))
1626 return GNUNET_SYSERR;
1627 /* If we are emulating an XHR client then delay sending a PUT request until
1628 * there is something to send.
1630 if (GNUNET_YES == plugin->emulate_xhr)
1631 s->put_tmp_disconnected = GNUNET_YES;
1632 else if (GNUNET_SYSERR == client_connect_put (s))
1633 return GNUNET_SYSERR;
1635 LOG (GNUNET_ERROR_TYPE_DEBUG,
1636 "Session %p: connected with connections GET %p and PUT %p\n",
1637 s, s->get.easyhandle, s->put.easyhandle);
1638 /* Perform connect */
1639 GNUNET_STATISTICS_set (plugin->env->stats,
1640 HTTP_STAT_STR_CONNECTIONS,
1641 plugin->cur_connections,
1643 /* Re-schedule since handles have changed */
1644 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1646 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1647 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1649 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1655 * Function obtain the network type for a session
1657 * @param cls closure (`struct Plugin*`)
1658 * @param session the session
1659 * @return the network type
1661 static enum GNUNET_ATS_Network_Type
1662 http_client_plugin_get_network (void *cls,
1663 struct Session *session)
1665 return ntohl (session->ats_address_network_type);
1670 * Session was idle, so disconnect it
1672 * @param cls the `struct Session` of the idle session
1673 * @param tc scheduler context
1676 client_session_timeout (void *cls,
1677 const struct GNUNET_SCHEDULER_TaskContext *tc)
1679 struct Session *s = cls;
1680 struct GNUNET_TIME_Relative left;
1682 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1683 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1684 if (0 != left.rel_value_us)
1686 /* not actually our turn yet, but let's at least update
1687 the monitor, it may think we're about to die ... */
1688 notify_session_monitor (s->plugin,
1690 GNUNET_TRANSPORT_SS_UP);
1691 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1692 &client_session_timeout,
1697 "Session %p was idle for %s, disconnecting\n",
1699 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1701 GNUNET_assert (GNUNET_OK ==
1702 http_client_plugin_session_disconnect (s->plugin,
1708 * Creates a new outbound session the transport service will use to
1709 * send data to the peer
1711 * @param cls the plugin
1712 * @param address the address
1713 * @return the session or NULL of max connections exceeded
1715 static struct Session *
1716 http_client_plugin_get_session (void *cls,
1717 const struct GNUNET_HELLO_Address *address)
1719 struct HTTP_Client_Plugin *plugin = cls;
1721 struct sockaddr *sa;
1722 struct GNUNET_ATS_Information ats;
1726 GNUNET_assert (NULL != address->address);
1728 /* find existing session */
1729 s = client_lookup_session (plugin, address);
1733 if (plugin->max_connections <= plugin->cur_connections)
1735 LOG (GNUNET_ERROR_TYPE_WARNING,
1736 "Maximum number of connections (%u) reached: "
1737 "cannot connect to peer `%s'\n",
1738 plugin->max_connections,
1739 GNUNET_i2s (&address->peer));
1743 /* Determine network location */
1744 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1745 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1746 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1747 if (GNUNET_SYSERR == res)
1749 if (GNUNET_YES == res)
1751 GNUNET_assert (NULL != sa);
1752 if (AF_INET == sa->sa_family)
1754 salen = sizeof (struct sockaddr_in);
1756 else if (AF_INET6 == sa->sa_family)
1758 salen = sizeof (struct sockaddr_in6);
1760 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1763 else if (GNUNET_NO == res)
1765 /* Cannot convert to sockaddr -> is external hostname */
1766 ats.value = htonl (GNUNET_ATS_NET_WAN);
1768 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1774 s = GNUNET_new (struct Session);
1776 s->address = GNUNET_HELLO_address_copy (address);
1777 s->ats_address_network_type = ats.value;
1778 s->put_paused = GNUNET_NO;
1779 s->put_tmp_disconnecting = GNUNET_NO;
1780 s->put_tmp_disconnected = GNUNET_NO;
1781 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT);
1782 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1783 &client_session_timeout,
1785 LOG (GNUNET_ERROR_TYPE_DEBUG,
1786 "Created new session %p for `%s' address `%s''\n",
1788 http_common_plugin_address_to_string (plugin->protocol,
1789 s->address->address,
1790 s->address->address_length),
1791 GNUNET_i2s (&s->address->peer));
1793 /* add new session */
1794 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1797 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1798 /* initiate new connection */
1799 if (GNUNET_SYSERR == client_connect (s))
1801 LOG (GNUNET_ERROR_TYPE_ERROR,
1802 "Cannot connect to peer `%s' address `%s''\n",
1803 http_common_plugin_address_to_string (plugin->protocol,
1804 s->address->address,
1805 s->address->address_length),
1806 GNUNET_i2s (&s->address->peer));
1807 client_delete_session (s);
1810 notify_session_monitor (plugin,
1812 GNUNET_TRANSPORT_SS_UP); /* or handshake? */
1818 * Setup http_client plugin
1820 * @param plugin the plugin handle
1821 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1824 client_start (struct HTTP_Client_Plugin *plugin)
1826 curl_global_init (CURL_GLOBAL_ALL);
1827 plugin->curl_multi_handle = curl_multi_init ();
1829 if (NULL == plugin->curl_multi_handle)
1831 LOG (GNUNET_ERROR_TYPE_ERROR,
1832 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1834 return GNUNET_SYSERR;
1841 * Another peer has suggested an address for this
1842 * peer and transport plugin. Check that this could be a valid
1843 * address. If so, consider adding it to the list
1846 * @param cls closure with the `struct Plugin`
1847 * @param addr pointer to the address
1848 * @param addrlen length of @a addr
1849 * @return #GNUNET_OK if this is a plausible address for this peer
1850 * and transport; always returns #GNUNET_NO (this is the client!)
1853 http_client_plugin_address_suggested (void *cls,
1857 /* A HTTP/S client does not have any valid address so:*/
1863 * Exit point from the plugin.
1865 * @param cls api as closure
1869 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1871 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1872 struct HTTP_Client_Plugin *plugin = api->cls;
1874 if (NULL == api->cls)
1880 LOG (GNUNET_ERROR_TYPE_DEBUG,
1881 _("Shutting down plugin `%s'\n"),
1883 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
1884 &destroy_session_cb,
1886 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1888 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1889 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1891 if (NULL != plugin->curl_multi_handle)
1893 curl_multi_cleanup (plugin->curl_multi_handle);
1894 plugin->curl_multi_handle = NULL;
1896 curl_global_cleanup ();
1897 LOG (GNUNET_ERROR_TYPE_DEBUG,
1898 _("Shutdown for plugin `%s' complete\n"),
1900 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
1901 GNUNET_free_non_null (plugin->proxy_hostname);
1902 GNUNET_free_non_null (plugin->proxy_username);
1903 GNUNET_free_non_null (plugin->proxy_password);
1904 GNUNET_free (plugin);
1913 * @param plugin the plugin handle
1914 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1917 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1919 unsigned long long max_connections;
1923 /* Optional parameters */
1925 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1929 max_connections = 128;
1930 plugin->max_connections = max_connections;
1932 LOG (GNUNET_ERROR_TYPE_DEBUG,
1933 _("Maximum number of connections is %u\n"),
1934 plugin->max_connections);
1936 /* Read proxy configuration */
1937 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1938 plugin->name, "PROXY", &plugin->proxy_hostname))
1940 LOG (GNUNET_ERROR_TYPE_DEBUG,
1941 "Found proxy host: `%s'\n",
1942 plugin->proxy_hostname);
1943 /* proxy username */
1945 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1948 &plugin->proxy_username))
1950 LOG (GNUNET_ERROR_TYPE_DEBUG,
1951 "Found proxy username name: `%s'\n",
1952 plugin->proxy_username);
1955 /* proxy password */
1957 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1960 &plugin->proxy_password))
1962 LOG (GNUNET_ERROR_TYPE_DEBUG,
1963 "Found proxy password name: `%s'\n",
1964 plugin->proxy_password);
1969 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1974 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
1976 if (0 == strcmp(proxy_type, "HTTP"))
1977 plugin->proxytype = CURLPROXY_HTTP;
1978 else if (0 == strcmp(proxy_type, "SOCKS4"))
1979 plugin->proxytype = CURLPROXY_SOCKS4;
1980 else if (0 == strcmp(proxy_type, "SOCKS5"))
1981 plugin->proxytype = CURLPROXY_SOCKS5;
1982 else if (0 == strcmp(proxy_type, "SOCKS4A"))
1983 plugin->proxytype = CURLPROXY_SOCKS4A;
1984 else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
1985 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
1988 LOG (GNUNET_ERROR_TYPE_ERROR,
1989 _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
1992 GNUNET_free (proxy_type);
1993 GNUNET_free (plugin->proxy_hostname);
1994 plugin->proxy_hostname = NULL;
1995 GNUNET_free_non_null (plugin->proxy_username);
1996 plugin->proxy_username = NULL;
1997 GNUNET_free_non_null (plugin->proxy_password);
1998 plugin->proxy_password = NULL;
2000 return GNUNET_SYSERR;
2003 LOG (GNUNET_ERROR_TYPE_DEBUG,
2004 "Found proxy type: `%s'\n",
2008 /* proxy http tunneling */
2009 plugin->proxy_use_httpproxytunnel
2010 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2012 "PROXY_HTTP_TUNNELING");
2013 if (GNUNET_SYSERR == plugin->proxy_use_httpproxytunnel)
2014 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
2016 GNUNET_free_non_null (proxy_type);
2019 /* Should we emulate an XHR client for testing? */
2021 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2029 * Function called by the pretty printer for the resolved address for
2030 * each human-readable address obtained. The callback can be called
2031 * several times. The last invocation must be with a @a address of
2032 * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
2033 * errors, the callback might be called first with @a address NULL and
2034 * @a res being #GNUNET_SYSERR. In that case, there must still be a
2035 * subsequent call later with @a address NULL and @a res #GNUNET_OK.
2037 * @param cls closure
2038 * @param address one of the names for the host, NULL on last callback
2039 * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on failure,
2040 * #GNUNET_OK on last callback
2043 http_client_plugin_address_to_string (void *cls,
2047 return http_common_plugin_address_to_string (PLUGIN_NAME,
2054 * Function that will be called whenever the transport service wants to
2055 * notify the plugin that a session is still active and in use and
2056 * therefore the session timeout for this session has to be updated
2058 * @param cls closure
2059 * @param peer which peer was the session for
2060 * @param session which session is being updated
2063 http_client_plugin_update_session_timeout (void *cls,
2064 const struct GNUNET_PeerIdentity *peer,
2065 struct Session *session)
2067 client_reschedule_session_timeout (session);
2072 * Function that will be called whenever the transport service wants to
2073 * notify the plugin that the inbound quota changed and that the plugin
2074 * should update it's delay for the next receive value
2076 * @param cls closure
2077 * @param peer which peer was the session for
2078 * @param session which session is being updated
2079 * @param delay new delay to use for receiving
2082 http_client_plugin_update_inbound_delay (void *cls,
2083 const struct GNUNET_PeerIdentity *peer,
2085 struct GNUNET_TIME_Relative delay)
2087 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
2088 LOG (GNUNET_ERROR_TYPE_DEBUG,
2089 "New inbound delay %s\n",
2090 GNUNET_STRINGS_relative_time_to_string (delay,
2092 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
2094 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
2096 = GNUNET_SCHEDULER_add_delayed (delay,
2104 * Return information about the given session to the
2107 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2108 * @param peer peer we send information about
2109 * @param value our `struct Session` to send information about
2110 * @return #GNUNET_OK (continue to iterate)
2113 send_session_info_iter (void *cls,
2114 const struct GNUNET_PeerIdentity *peer,
2117 struct HTTP_Client_Plugin *plugin = cls;
2118 struct Session *session = value;
2120 notify_session_monitor (plugin,
2122 GNUNET_TRANSPORT_SS_UP);
2128 * Begin monitoring sessions of a plugin. There can only
2129 * be one active monitor per plugin (i.e. if there are
2130 * multiple monitors, the transport service needs to
2131 * multiplex the generated events over all of them).
2133 * @param cls closure of the plugin
2134 * @param sic callback to invoke, NULL to disable monitor;
2135 * plugin will being by iterating over all active
2136 * sessions immediately and then enter monitor mode
2137 * @param sic_cls closure for @a sic
2140 http_client_plugin_setup_monitor (void *cls,
2141 GNUNET_TRANSPORT_SessionInfoCallback sic,
2144 struct HTTP_Client_Plugin *plugin = cls;
2147 plugin->sic_cls = sic_cls;
2150 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2151 &send_session_info_iter,
2153 /* signal end of first iteration */
2154 sic (sic_cls, NULL, NULL);
2160 * Entry point for the plugin.
2163 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2165 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2166 struct GNUNET_TRANSPORT_PluginFunctions *api;
2167 struct HTTP_Client_Plugin *plugin;
2169 if (NULL == env->receive)
2171 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2172 initialze the plugin or the API */
2173 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2175 api->address_to_string = &http_client_plugin_address_to_string;
2176 api->string_to_address = &http_common_plugin_string_to_address;
2177 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2181 plugin = GNUNET_new (struct HTTP_Client_Plugin);
2183 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
2185 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2187 api->send = &http_client_plugin_send;
2188 api->disconnect_session = &http_client_plugin_session_disconnect;
2189 api->query_keepalive_factor = &http_client_query_keepalive_factor;
2190 api->disconnect_peer = &http_client_plugin_peer_disconnect;
2191 api->check_address = &http_client_plugin_address_suggested;
2192 api->get_session = &http_client_plugin_get_session;
2193 api->address_to_string = &http_client_plugin_address_to_string;
2194 api->string_to_address = &http_common_plugin_string_to_address;
2195 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2196 api->get_network = &http_client_plugin_get_network;
2197 api->update_session_timeout = &http_client_plugin_update_session_timeout;
2198 api->update_inbound_delay = &http_client_plugin_update_inbound_delay;
2199 api->setup_monitor = &http_client_plugin_setup_monitor;
2201 plugin->name = "transport-https_client";
2202 plugin->protocol = "https";
2204 plugin->name = "transport-http_client";
2205 plugin->protocol = "http";
2207 plugin->last_tag = 1;
2209 if (GNUNET_SYSERR == client_configure_plugin (plugin))
2211 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2216 if (GNUNET_SYSERR == client_start (plugin))
2218 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2224 /* end of plugin_transport_http_client.c */