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 * To whom are we talking to (set to our identity
139 * if we are still waiting for the welcome message)
141 struct GNUNET_PeerIdentity target;
144 * The URL to connect to
151 struct GNUNET_HELLO_Address *address;
154 * Pointer to the global plugin struct.
156 struct HTTP_Client_Plugin *plugin;
159 * Curl client PUT handle.
160 * FIXME: delta to put.easyhandle?
165 * Handle for the HTTP PUT request.
167 struct ConnectionHandle put;
170 * Curl client GET handle
171 * FIXME: delta to get.easyhandle?
176 * Handle for the HTTP GET request.
178 struct ConnectionHandle get;
181 * next pointer for double linked list
183 struct HTTP_Message *msg_head;
186 * previous pointer for double linked list
188 struct HTTP_Message *msg_tail;
191 * Message stream tokenizer for incoming data
193 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
196 * Session timeout task
198 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
201 * Session timeout task
203 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
206 * Task to wake up client receive handle when receiving is allowed again
208 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
211 * Absolute time when to receive data again.
212 * Used for receive throttling.
214 struct GNUNET_TIME_Absolute next_receive;
217 * When does this session time out.
219 struct GNUNET_TIME_Absolute timeout;
222 * Number of bytes waiting for transmission to this peer.
224 unsigned long long bytes_in_queue;
227 * Outbound overhead due to HTTP connection
228 * Add to next message of this session when calling callback
233 * Number of messages waiting for transmission to this peer.
235 unsigned int msgs_in_queue;
238 * ATS network type in NBO
240 uint32_t ats_address_network_type;
243 * Is the client PUT handle currently paused?
248 * Is the client PUT handle disconnect in progress?
250 int put_tmp_disconnecting;
253 * Is the client PUT handle temporarily disconnected?
255 int put_tmp_disconnected;
258 * We received data to send while disconnecting, reconnect immediately
260 int put_reconnect_required;
265 * Encapsulation of all of the state of the plugin.
267 struct HTTP_Client_Plugin
272 struct GNUNET_TRANSPORT_PluginEnvironment *env;
277 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
280 * Function to call about session status changes.
282 GNUNET_TRANSPORT_SessionInfoCallback sic;
285 * Closure for @e sic.
300 * Proxy configuration: hostname or ip of the proxy server
302 char *proxy_hostname;
305 * Username for the proxy server
307 char *proxy_username;
310 * Password for the proxy server
312 char *proxy_password;
317 CURLM *curl_multi_handle;
322 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
325 * Type of proxy server:
327 * Valid values as supported by curl:
328 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
329 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
331 curl_proxytype proxytype;
334 * Use proxy tunneling:
335 * Tunnel all operations through a given HTTP instead of have the proxy
336 * evaluate the HTTP request
338 * Default: #GNUNET_NO, #GNUNET_YES experimental
340 int proxy_use_httpproxytunnel;
343 * My options to be included in the address
348 * Maximum number of sockets the plugin can use
349 * Each http inbound /outbound connections are two connections
351 unsigned int max_connections;
354 * Current number of sockets the plugin can use
355 * Each http inbound /outbound connections are two connections
357 unsigned int cur_connections;
360 * Last used unique HTTP connection tag
379 * If a session monitor is attached, notify it about the new
382 * @param plugin our plugin
383 * @param session session that changed state
384 * @param state new state of the session
387 notify_session_monitor (struct HTTP_Client_Plugin *plugin,
388 struct Session *session,
389 enum GNUNET_TRANSPORT_SessionState state)
391 struct GNUNET_TRANSPORT_SessionInfo info;
393 if (NULL == plugin->sic)
395 memset (&info, 0, sizeof (info));
397 info.is_inbound = GNUNET_NO;
398 info.num_msg_pending = session->msgs_in_queue;
399 info.num_bytes_pending = session->bytes_in_queue;
400 info.receive_delay = session->next_receive;
401 info.session_timeout = session->timeout;
402 info.address = session->address;
403 plugin->sic (plugin->sic_cls,
410 * Delete session @a s.
412 * @param s the session to delete
415 client_delete_session (struct Session *s)
417 struct HTTP_Client_Plugin *plugin = s->plugin;
418 struct HTTP_Message *pos;
419 struct HTTP_Message *next;
422 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
424 GNUNET_SCHEDULER_cancel (s->timeout_task);
425 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
426 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
428 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
430 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
431 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
433 if (GNUNET_SCHEDULER_NO_TASK != s->recv_wakeup_task)
435 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
436 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
438 GNUNET_assert (GNUNET_OK ==
439 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
442 if (NULL != s->client_put)
444 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
448 GNUNET_i2s (&s->target));
450 /* remove curl handle from multi handle */
451 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
453 GNUNET_break (CURLM_OK == mret);
454 curl_easy_cleanup (s->client_put);
455 s->client_put = NULL;
457 if (NULL != s->client_get)
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
462 GNUNET_i2s (&s->target));
463 /* remove curl handle from multi handle */
464 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
466 GNUNET_break (CURLM_OK == mret);
467 curl_easy_cleanup (s->client_get);
468 GNUNET_assert (plugin->cur_connections > 0);
469 plugin->cur_connections--;
470 s->client_get = NULL;
472 GNUNET_STATISTICS_set (plugin->env->stats,
473 HTTP_STAT_STR_CONNECTIONS,
474 plugin->cur_connections,
477 while (NULL != (pos = next))
480 GNUNET_CONTAINER_DLL_remove (s->msg_head,
483 GNUNET_assert (0 < s->msgs_in_queue);
485 GNUNET_assert (pos->size <= s->bytes_in_queue);
486 s->bytes_in_queue -= pos->size;
487 if (NULL != pos->transmit_cont)
488 pos->transmit_cont (pos->transmit_cont_cls,
492 pos->pos + s->overhead);
496 GNUNET_assert (0 == s->msgs_in_queue);
497 GNUNET_assert (0 == s->bytes_in_queue);
498 notify_session_monitor (plugin,
500 GNUNET_TRANSPORT_SS_DOWN);
501 if (NULL != s->msg_tk)
503 GNUNET_SERVER_mst_destroy (s->msg_tk);
506 GNUNET_HELLO_address_free (s->address);
507 GNUNET_free (s->url);
513 * Increment session timeout due to activity for session @a s.
515 * @param s the session
518 client_reschedule_session_timeout (struct Session *s)
520 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
521 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
526 * Task performing curl operations
528 * @param cls plugin as closure
529 * @param tc gnunet scheduler task context
532 client_run (void *cls,
533 const struct GNUNET_SCHEDULER_TaskContext *tc);
537 * Function setting up file descriptors and scheduling task to run
539 * @param plugin the plugin as closure
540 * @param now schedule task in 1ms, regardless of what curl may say
541 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
544 client_schedule (struct HTTP_Client_Plugin *plugin,
551 struct GNUNET_NETWORK_FDSet *grs;
552 struct GNUNET_NETWORK_FDSet *gws;
555 struct GNUNET_TIME_Relative timeout;
557 /* Cancel previous scheduled task */
558 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
560 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
561 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
567 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
568 if (mret != CURLM_OK)
570 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
571 "curl_multi_fdset", __FILE__, __LINE__,
572 curl_multi_strerror (mret));
573 return GNUNET_SYSERR;
575 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
577 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
579 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
580 if (now == GNUNET_YES)
581 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
583 if (mret != CURLM_OK)
585 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
586 _("%s failed at %s:%d: `%s'\n"),
587 "curl_multi_timeout", __FILE__, __LINE__,
588 curl_multi_strerror (mret));
589 return GNUNET_SYSERR;
592 grs = GNUNET_NETWORK_fdset_create ();
593 gws = GNUNET_NETWORK_fdset_create ();
594 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
595 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
597 plugin->client_perform_task =
598 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
600 &client_run, plugin);
601 GNUNET_NETWORK_fdset_destroy (gws);
602 GNUNET_NETWORK_fdset_destroy (grs);
610 * @param curl the curl easy handle
611 * @param type message type
612 * @param data data to log, NOT a 0-terminated string
613 * @param size data length
614 * @param cls the closure
618 client_log (CURL *curl,
624 struct ConnectionHandle *ch = cls;
625 const char *ttype = "UNSPECIFIED";
628 if (! ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT)))
635 case CURLINFO_HEADER_IN:
638 case CURLINFO_HEADER_OUT:
639 ttype = "HEADER_OUT";
641 GNUNET_assert (NULL != ch);
642 GNUNET_assert (NULL != ch->easyhandle);
643 GNUNET_assert (NULL != ch->s);
644 ch->s->overhead += size;
647 ttype = "UNSPECIFIED";
650 memcpy (text, data, size);
651 if (text[size - 1] == '\n')
658 text[size + 1] = '\0';
660 LOG (GNUNET_ERROR_TYPE_DEBUG,
661 "Connection %p %s: %s",
670 * Connect a HTTP put connection
672 * @param s the session to connect
673 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
676 client_connect_put (struct Session *s);
680 * Function that can be used by the transport service to transmit
681 * a message using the plugin. Note that in the case of a
682 * peer disconnecting, the continuation MUST be called
683 * prior to the disconnect notification itself. This function
684 * will be called with this peer's HELLO message to initiate
685 * a fresh connection to another peer.
688 * @param s which session must be used
689 * @param msgbuf the message to transmit
690 * @param msgbuf_size number of bytes in @a msgbuf
691 * @param priority how important is the message (most plugins will
692 * ignore message priority and just FIFO)
693 * @param to how long to wait at most for the transmission (does not
694 * require plugins to discard the message after the timeout,
695 * just advisory for the desired delay; most plugins will ignore
697 * @param cont continuation to call once the message has
698 * been transmitted (or if the transport is ready
699 * for the next transmission call; or if the
700 * peer disconnected...); can be NULL
701 * @param cont_cls closure for cont
702 * @return number of bytes used (on the physical network, with overheads);
703 * -1 on hard errors (i.e. address invalid); 0 is a legal value
704 * and does NOT mean that the message was not transmitted (DV)
707 http_client_plugin_send (void *cls,
711 unsigned int priority,
712 struct GNUNET_TIME_Relative to,
713 GNUNET_TRANSPORT_TransmitContinuation cont,
716 struct HTTP_Client_Plugin *plugin = cls;
717 struct HTTP_Message *msg;
720 LOG (GNUNET_ERROR_TYPE_DEBUG,
721 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
723 msgbuf_size, GNUNET_i2s (&s->target));
725 /* create new message and schedule */
726 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
728 msg->size = msgbuf_size;
730 msg->buf = (char *) &msg[1];
731 msg->transmit_cont = cont;
732 msg->transmit_cont_cls = cont_cls;
733 memcpy (msg->buf, msgbuf, msgbuf_size);
734 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head,
738 s->bytes_in_queue += msg->size;
740 GNUNET_asprintf (&stat_txt,
741 "# bytes currently in %s_client buffers",
743 GNUNET_STATISTICS_update (plugin->env->stats,
744 stat_txt, msgbuf_size, GNUNET_NO);
745 GNUNET_free (stat_txt);
746 notify_session_monitor (plugin,
748 GNUNET_TRANSPORT_SS_UP);
749 if (GNUNET_YES == s->put_tmp_disconnecting)
751 /* PUT connection is currently getting disconnected */
752 s->put_reconnect_required = GNUNET_YES;
753 LOG (GNUNET_ERROR_TYPE_DEBUG,
754 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
759 if (GNUNET_YES == s->put_paused)
761 /* PUT connection was paused, unpause */
762 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
763 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
764 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
765 LOG (GNUNET_ERROR_TYPE_DEBUG,
766 "Session %p/connection %p: unpausing connection\n",
768 s->put_paused = GNUNET_NO;
769 if (NULL != s->client_put)
770 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
772 else if (GNUNET_YES == s->put_tmp_disconnected)
774 /* PUT connection was disconnected, reconnect */
775 LOG (GNUNET_ERROR_TYPE_DEBUG,
776 "Session %p: Reconnecting PUT connection\n",
778 s->put_tmp_disconnected = GNUNET_NO;
779 GNUNET_break (NULL == s->client_put);
780 if (GNUNET_SYSERR == client_connect_put (s))
781 return GNUNET_SYSERR;
783 client_schedule (s->plugin,
790 * Disconnect a session
792 * @param cls the `struct HTTP_Client_Plugin *`
794 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
797 http_client_plugin_session_disconnect (void *cls,
800 struct HTTP_Client_Plugin *plugin = cls;
802 LOG (GNUNET_ERROR_TYPE_DEBUG,
803 "Session %p: notifying transport about ending session\n",s);
804 plugin->env->session_end (plugin->env->cls,
807 client_delete_session (s);
809 /* Re-schedule since handles have changed */
810 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
812 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
813 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
815 client_schedule (plugin, GNUNET_YES);
822 * Function that is called to get the keepalive factor.
823 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
824 * calculate the interval between keepalive packets.
826 * @param cls closure with the `struct Plugin`
827 * @return keepalive factor
830 http_client_query_keepalive_factor (void *cls)
837 * Callback to destroys all sessions on exit.
839 * @param cls the `struct HTTP_Client_Plugin *`
840 * @param peer identity of the peer
841 * @param value the `struct Session *`
842 * @return #GNUNET_OK (continue iterating)
845 destroy_session_cb (void *cls,
846 const struct GNUNET_PeerIdentity *peer,
849 struct HTTP_Client_Plugin *plugin = cls;
850 struct Session *session = value;
852 http_client_plugin_session_disconnect (plugin, session);
858 * Function that can be used to force the plugin to disconnect
859 * from the given peer and cancel all previous transmissions
860 * (and their continuationc).
863 * @param target peer from which to disconnect
866 http_client_plugin_peer_disconnect (void *cls,
867 const struct GNUNET_PeerIdentity *target)
869 struct HTTP_Client_Plugin *plugin = cls;
871 LOG (GNUNET_ERROR_TYPE_DEBUG,
872 "Transport tells me to disconnect `%s'\n",
873 GNUNET_i2s (target));
874 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
882 * Closure for #session_lookup_client_by_address().
884 struct SessionClientCtx
887 * Address we are looking for.
889 const struct GNUNET_HELLO_Address *address;
892 * Session that was found.
899 * Locate the seession object for a given address.
901 * @param cls the `struct SessionClientCtx *`
902 * @param key peer identity
903 * @param value the `struct Session` to check
904 * @return #GNUNET_NO if found, #GNUNET_OK if not
907 session_lookup_client_by_address (void *cls,
908 const struct GNUNET_PeerIdentity *key,
911 struct SessionClientCtx *sc_ctx = cls;
912 struct Session *s = value;
914 if (0 == GNUNET_HELLO_address_cmp (sc_ctx->address,
925 * Check if a sessions exists for an specific address
927 * @param plugin the plugin
928 * @param address the address
929 * @return the session or NULL
931 static struct Session *
932 client_lookup_session (struct HTTP_Client_Plugin *plugin,
933 const struct GNUNET_HELLO_Address *address)
935 struct SessionClientCtx sc_ctx;
937 sc_ctx.address = address;
939 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
940 &session_lookup_client_by_address,
947 * When we have nothing to transmit, we pause the HTTP PUT
948 * after a while (so that gnurl stops asking). This task
949 * is the delayed task that actually pauses the PUT.
951 * @param cls the `struct Session *` with the put
952 * @param tc scheduler context
955 client_put_disconnect (void *cls,
956 const struct GNUNET_SCHEDULER_TaskContext *tc)
958 struct Session *s = cls;
960 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
961 LOG (GNUNET_ERROR_TYPE_DEBUG,
962 "Session %p/connection %p: will be disconnected due to no activity\n",
965 s->put_paused = GNUNET_NO;
966 s->put_tmp_disconnecting = GNUNET_YES;
967 if (NULL != s->client_put)
968 curl_easy_pause (s->client_put,
970 client_schedule (s->plugin, GNUNET_YES);
975 * Callback method used with libcurl
976 * Method is called when libcurl needs to read data during sending
978 * @param stream pointer where to write data
979 * @param size size of an individual element
980 * @param nmemb count of elements that can be written to the buffer
981 * @param cls our `struct Session`
982 * @return bytes written to stream, returning 0 will terminate connection!
985 client_send_cb (void *stream,
990 struct Session *s = cls;
991 struct HTTP_Client_Plugin *plugin = s->plugin;
992 struct HTTP_Message *msg = s->msg_head;
996 if (GNUNET_YES == s->put_tmp_disconnecting)
998 LOG (GNUNET_ERROR_TYPE_DEBUG,
999 "Session %p/connection %p: disconnect due to inactivity\n",
1007 LOG (GNUNET_ERROR_TYPE_DEBUG,
1008 "Session %p/connection %p: nothing to send, suspending\n",
1010 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
1011 &client_put_disconnect,
1013 s->put_paused = GNUNET_YES;
1014 return CURL_READFUNC_PAUSE;
1017 GNUNET_assert (msg->pos < msg->size);
1018 /* calculate how much fits in buffer */
1019 len = GNUNET_MIN (msg->size - msg->pos,
1021 memcpy (stream, &msg->buf[msg->pos], len);
1023 if (msg->pos == msg->size)
1025 LOG (GNUNET_ERROR_TYPE_DEBUG,
1026 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
1027 s, s->client_put, msg->size, msg->pos);
1028 /* Calling transmit continuation */
1029 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1032 GNUNET_assert (0 < s->msgs_in_queue);
1034 GNUNET_assert (msg->size <= s->bytes_in_queue);
1035 s->bytes_in_queue -= msg->size;
1036 if (NULL != msg->transmit_cont)
1037 msg->transmit_cont (msg->transmit_cont_cls,
1041 msg->size + s->overhead);
1045 notify_session_monitor (plugin,
1047 GNUNET_TRANSPORT_SS_UP);
1048 GNUNET_asprintf (&stat_txt,
1049 "# bytes currently in %s_client buffers",
1051 GNUNET_STATISTICS_update (plugin->env->stats,
1055 GNUNET_free (stat_txt);
1056 GNUNET_asprintf (&stat_txt,
1057 "# bytes transmitted via %s_client",
1059 GNUNET_STATISTICS_update (plugin->env->stats,
1063 GNUNET_free (stat_txt);
1069 * Wake up a curl handle which was suspended
1071 * @param cls the session
1072 * @param tc task context
1075 client_wake_up (void *cls,
1076 const struct GNUNET_SCHEDULER_TaskContext *tc)
1078 struct Session *s = cls;
1080 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1081 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1083 LOG (GNUNET_ERROR_TYPE_DEBUG,
1084 "Session %p/connection %p: Waking up GET handle\n",
1087 if (GNUNET_YES == s->put_paused)
1089 /* PUT connection was paused, unpause */
1090 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
1091 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
1092 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
1093 s->put_paused = GNUNET_NO;
1094 if (NULL != s->client_put)
1095 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
1097 if (NULL != s->client_get)
1098 curl_easy_pause (s->client_get,
1104 * Callback for message stream tokenizer
1106 * @param cls the session
1107 * @param client not used
1108 * @param message the message received
1109 * @return always #GNUNET_OK
1112 client_receive_mst_cb (void *cls,
1114 const struct GNUNET_MessageHeader *message)
1116 struct Session *s = cls;
1117 struct HTTP_Client_Plugin *plugin;
1118 struct GNUNET_TIME_Relative delay;
1119 struct GNUNET_ATS_Information atsi;
1123 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1124 atsi.value = s->ats_address_network_type;
1125 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1127 delay = s->plugin->env->receive (plugin->env->cls,
1131 plugin->env->update_address_metrics (plugin->env->cls,
1135 GNUNET_asprintf (&stat_txt,
1136 "# bytes received via %s_client",
1138 GNUNET_STATISTICS_update (plugin->env->stats,
1140 ntohs (message->size),
1142 GNUNET_free (stat_txt);
1144 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1145 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
1147 LOG (GNUNET_ERROR_TYPE_DEBUG,
1148 "Client: peer `%s' address `%s' next read delayed for %s\n",
1149 GNUNET_i2s (&s->target),
1150 http_common_plugin_address_to_string (s->plugin->protocol,
1151 s->address->address,
1152 s->address->address_length),
1153 GNUNET_STRINGS_relative_time_to_string (delay,
1156 client_reschedule_session_timeout (s);
1162 * Callback method used with libcurl when data for a PUT connection are
1163 * received. We do not expect data here, so we just discard it.
1165 * @param stream pointer where to write data
1166 * @param size size of an individual element
1167 * @param nmemb count of elements that can be written to the buffer
1168 * @param cls destination pointer, passed to the libcurl handle
1169 * @return bytes read from stream
1172 client_receive_put (void *stream,
1177 return size * nmemb;
1182 * Callback method used with libcurl when data for a GET connection are
1183 * received. Forward to MST
1185 * @param stream pointer where to write data
1186 * @param size size of an individual element
1187 * @param nmemb count of elements that can be written to the buffer
1188 * @param cls destination pointer, passed to the libcurl handle
1189 * @return bytes read from stream
1192 client_receive (void *stream,
1197 struct Session *s = cls;
1198 struct GNUNET_TIME_Absolute now;
1199 size_t len = size * nmemb;
1201 LOG (GNUNET_ERROR_TYPE_DEBUG,
1202 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
1204 len, GNUNET_i2s (&s->target));
1205 now = GNUNET_TIME_absolute_get ();
1206 if (now.abs_value_us < s->next_receive.abs_value_us)
1208 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1209 struct GNUNET_TIME_Relative delta
1210 = GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1212 LOG (GNUNET_ERROR_TYPE_DEBUG,
1213 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1216 GNUNET_STRINGS_relative_time_to_string (delta,
1218 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1220 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1221 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1224 = GNUNET_SCHEDULER_add_delayed (delta,
1227 return CURL_WRITEFUNC_PAUSE;
1229 if (NULL == s->msg_tk)
1230 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb,
1232 GNUNET_SERVER_mst_receive (s->msg_tk,
1243 * Task performing curl operations
1245 * @param cls plugin as closure
1246 * @param tc scheduler task context
1249 client_run (void *cls,
1250 const struct GNUNET_SCHEDULER_TaskContext *tc)
1252 struct HTTP_Client_Plugin *plugin = cls;
1254 long http_statuscode;
1259 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1260 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1265 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1268 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1270 CURL *easy_h = msg->easy_handle;
1271 struct Session *s = NULL;
1272 char *d = NULL; /* curl requires 'd' to be a 'char *' */
1277 LOG (GNUNET_ERROR_TYPE_DEBUG,
1278 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1280 curl_easy_strerror (msg->data.result), running);
1284 GNUNET_assert (CURLE_OK ==
1285 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1286 s = (struct Session *) d;
1287 GNUNET_assert (NULL != s);
1288 if (msg->msg == CURLMSG_DONE)
1290 GNUNET_break (CURLE_OK ==
1291 curl_easy_getinfo (easy_h,
1292 CURLINFO_RESPONSE_CODE,
1294 if (easy_h == s->client_put)
1296 if ((0 != msg->data.result) || (http_statuscode != 200))
1298 LOG (GNUNET_ERROR_TYPE_DEBUG,
1299 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1300 s, msg->easy_handle,
1301 GNUNET_i2s (&s->target),
1304 curl_easy_strerror (msg->data.result));
1307 LOG (GNUNET_ERROR_TYPE_DEBUG,
1308 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1309 s, msg->easy_handle,
1310 GNUNET_i2s (&s->target));
1311 if (NULL == s->client_get)
1313 /* Disconnect other transmission direction and tell transport */
1316 curl_multi_remove_handle (plugin->curl_multi_handle,
1318 curl_easy_cleanup (easy_h);
1319 GNUNET_assert (plugin->cur_connections > 0);
1320 plugin->cur_connections--;
1321 s->put_tmp_disconnecting = GNUNET_NO;
1322 s->put_tmp_disconnected = GNUNET_YES;
1323 s->client_put = NULL;
1324 s->put.easyhandle = NULL;
1328 * Handling a rare case:
1329 * plugin_send was called during temporary put disconnect,
1330 * reconnect required after connection was disconnected
1332 if (GNUNET_YES == s->put_reconnect_required)
1334 s->put_reconnect_required = GNUNET_NO;
1335 if (GNUNET_SYSERR == client_connect_put (s))
1337 GNUNET_break (s->client_put == NULL);
1338 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1342 if (easy_h == s->client_get)
1344 if ((0 != msg->data.result) || (http_statuscode != 200))
1346 LOG (GNUNET_ERROR_TYPE_DEBUG,
1347 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1350 GNUNET_i2s (&s->target),
1353 curl_easy_strerror (msg->data.result));
1357 LOG (GNUNET_ERROR_TYPE_DEBUG,
1358 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1361 GNUNET_i2s (&s->target));
1362 /* Disconnect other transmission direction and tell transport */
1363 s->get.easyhandle = NULL;
1365 /* FIXME: who calls curl_multi_remove on 'easy_h' now!? */
1366 GNUNET_assert (plugin->cur_connections > 0);
1367 plugin->cur_connections--;
1368 http_client_plugin_session_disconnect (plugin, s);
1373 while (mret == CURLM_CALL_MULTI_PERFORM);
1374 client_schedule (plugin, GNUNET_NO);
1379 * Connect GET connection for a session
1381 * @param s the session to connect
1382 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1385 client_connect_get (struct Session *s)
1389 /* create get connection */
1390 s->client_get = curl_easy_init ();
1392 s->get.easyhandle = s->client_get;
1394 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1395 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1396 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1399 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1401 struct HttpAddress *ha;
1403 ha = (struct HttpAddress *) s->address->address;
1405 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1406 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1408 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1409 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1413 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1414 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1417 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1418 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1420 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1421 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1424 if (NULL != s->plugin->proxy_hostname)
1426 curl_easy_setopt (s->client_get, CURLOPT_PROXY, s->plugin->proxy_hostname);
1427 curl_easy_setopt (s->client_get, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1428 if (NULL != s->plugin->proxy_username)
1429 curl_easy_setopt (s->client_get, CURLOPT_PROXYUSERNAME,
1430 s->plugin->proxy_username);
1431 if (NULL != s->plugin->proxy_password)
1432 curl_easy_setopt (s->client_get, CURLOPT_PROXYPASSWORD,
1433 s->plugin->proxy_password);
1434 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1435 curl_easy_setopt (s->client_get, CURLOPT_HTTPPROXYTUNNEL,
1436 s->plugin->proxy_use_httpproxytunnel);
1439 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1440 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1441 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1442 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1443 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1444 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1445 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1446 /* No timeout by default, timeout done with session timeout */
1447 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1448 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1449 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1450 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1451 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1452 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1453 #if CURL_TCP_NODELAY
1454 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1456 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1458 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1460 if (CURLM_OK != mret)
1462 LOG (GNUNET_ERROR_TYPE_ERROR,
1463 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1465 curl_multi_strerror (mret));
1466 curl_easy_cleanup (s->client_get);
1467 s->client_get = NULL;
1469 s->get.easyhandle = NULL;
1471 return GNUNET_SYSERR;
1473 s->plugin->cur_connections++;
1479 * Connect a HTTP put connection
1481 * @param s the session to connect
1482 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1485 client_connect_put (struct Session *s)
1489 /* create put connection */
1490 LOG (GNUNET_ERROR_TYPE_DEBUG,
1491 "Session %p: Init PUT handle\n", s);
1492 s->client_put = curl_easy_init ();
1494 s->put.easyhandle = s->client_put;
1496 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1497 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1498 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1501 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1503 struct HttpAddress *ha;
1504 ha = (struct HttpAddress *) s->address->address;
1506 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1507 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1509 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1510 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1514 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1515 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1518 curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1519 curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1521 curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1522 curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1524 if (s->plugin->proxy_hostname != NULL)
1526 curl_easy_setopt (s->client_put, CURLOPT_PROXY, s->plugin->proxy_hostname);
1527 curl_easy_setopt (s->client_put, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1528 if (NULL != s->plugin->proxy_username)
1529 curl_easy_setopt (s->client_put, CURLOPT_PROXYUSERNAME,
1530 s->plugin->proxy_username);
1531 if (NULL != s->plugin->proxy_password)
1532 curl_easy_setopt (s->client_put, CURLOPT_PROXYPASSWORD,
1533 s->plugin->proxy_password);
1534 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1535 curl_easy_setopt (s->client_put, CURLOPT_HTTPPROXYTUNNEL,
1536 s->plugin->proxy_use_httpproxytunnel);
1539 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1540 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1541 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1542 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1543 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1544 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1545 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1546 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1547 /* No timeout by default, timeout done with session timeout */
1548 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1549 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1550 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1551 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1552 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1553 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1554 #if CURL_TCP_NODELAY
1555 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1557 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1559 if (CURLM_OK != mret)
1561 LOG (GNUNET_ERROR_TYPE_ERROR,
1562 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1564 curl_multi_strerror (mret));
1565 curl_easy_cleanup (s->client_put);
1566 s->client_put = NULL;
1567 s->put.easyhandle = NULL;
1569 s->put_tmp_disconnected = GNUNET_YES;
1570 return GNUNET_SYSERR;
1572 s->put_tmp_disconnected = GNUNET_NO;
1573 s->plugin->cur_connections++;
1580 * Connect both PUT and GET connection for a session
1582 * @param s the session to connect
1583 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1586 client_connect (struct Session *s)
1588 struct HTTP_Client_Plugin *plugin = s->plugin;
1589 int res = GNUNET_OK;
1592 if (NULL == http_common_plugin_address_to_string (plugin->protocol,
1593 s->address->address,
1594 s->address->address_length))
1596 LOG (GNUNET_ERROR_TYPE_DEBUG,
1597 "Invalid address peer `%s'\n",
1598 GNUNET_i2s (&s->target));
1599 return GNUNET_SYSERR;
1602 GNUNET_asprintf (&s->url, "%s/%s;%u",
1603 http_common_plugin_address_to_url (NULL,
1604 s->address->address,
1605 s->address->address_length),
1606 GNUNET_i2s_full (plugin->env->my_identity),
1610 LOG (GNUNET_ERROR_TYPE_DEBUG,
1611 "Initiating outbound session peer `%s' using address `%s'\n",
1612 GNUNET_i2s (&s->target), s->url);
1614 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1615 (GNUNET_SYSERR == client_connect_put (s)))
1616 return GNUNET_SYSERR;
1618 LOG (GNUNET_ERROR_TYPE_DEBUG,
1619 "Session %p: connected with connections GET %p and PUT %p\n",
1623 /* Perform connect */
1624 GNUNET_STATISTICS_set (plugin->env->stats,
1625 HTTP_STAT_STR_CONNECTIONS,
1626 plugin->cur_connections,
1628 /* Re-schedule since handles have changed */
1629 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1631 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1632 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1634 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1640 * Function obtain the network type for a session
1642 * @param cls closure (`struct Plugin*`)
1643 * @param session the session
1644 * @return the network type
1646 static enum GNUNET_ATS_Network_Type
1647 http_client_plugin_get_network (void *cls,
1648 struct Session *session)
1650 return ntohl (session->ats_address_network_type);
1655 * Session was idle, so disconnect it
1657 * @param cls the `struct Session` of the idle session
1658 * @param tc scheduler context
1661 client_session_timeout (void *cls,
1662 const struct GNUNET_SCHEDULER_TaskContext *tc)
1664 struct Session *s = cls;
1665 struct GNUNET_TIME_Relative left;
1667 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1668 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1669 if (0 != left.rel_value_us)
1671 /* not actually our turn yet, but let's at least update
1672 the monitor, it may think we're about to die ... */
1673 notify_session_monitor (s->plugin,
1675 GNUNET_TRANSPORT_SS_UP);
1676 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1677 &client_session_timeout,
1682 "Session %p was idle for %s, disconnecting\n",
1684 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1686 GNUNET_assert (GNUNET_OK ==
1687 http_client_plugin_session_disconnect (s->plugin,
1693 * Creates a new outbound session the transport service will use to
1694 * send data to the peer
1696 * @param cls the plugin
1697 * @param address the address
1698 * @return the session or NULL of max connections exceeded
1700 static struct Session *
1701 http_client_plugin_get_session (void *cls,
1702 const struct GNUNET_HELLO_Address *address)
1704 struct HTTP_Client_Plugin *plugin = cls;
1706 struct sockaddr *sa;
1707 struct GNUNET_ATS_Information ats;
1711 GNUNET_assert (NULL != address->address);
1713 /* find existing session */
1714 s = client_lookup_session (plugin, address);
1718 if (plugin->max_connections <= plugin->cur_connections)
1720 LOG (GNUNET_ERROR_TYPE_WARNING,
1721 "Maximum number of connections (%u) reached: "
1722 "cannot connect to peer `%s'\n",
1723 plugin->max_connections,
1724 GNUNET_i2s (&address->peer));
1728 /* Determine network location */
1729 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1730 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1731 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1732 if (GNUNET_SYSERR == res)
1734 if (GNUNET_YES == res)
1736 GNUNET_assert (NULL != sa);
1737 if (AF_INET == sa->sa_family)
1739 salen = sizeof (struct sockaddr_in);
1741 else if (AF_INET6 == sa->sa_family)
1743 salen = sizeof (struct sockaddr_in6);
1745 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1748 else if (GNUNET_NO == res)
1750 /* Cannot convert to sockaddr -> is external hostname */
1751 ats.value = htonl (GNUNET_ATS_NET_WAN);
1753 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1759 s = GNUNET_new (struct Session);
1760 s->target = address->peer;
1762 s->address = GNUNET_HELLO_address_copy (address);
1763 s->ats_address_network_type = ats.value;
1764 s->put_paused = GNUNET_NO;
1765 s->put_tmp_disconnecting = GNUNET_NO;
1766 s->put_tmp_disconnected = GNUNET_NO;
1767 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT);
1768 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1769 &client_session_timeout,
1771 LOG (GNUNET_ERROR_TYPE_DEBUG,
1772 "Created new session %p for `%s' address `%s''\n",
1774 http_common_plugin_address_to_string (plugin->protocol,
1775 s->address->address,
1776 s->address->address_length),
1777 GNUNET_i2s (&s->target));
1779 /* add new session */
1780 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1783 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1784 /* initiate new connection */
1785 if (GNUNET_SYSERR == client_connect (s))
1787 LOG (GNUNET_ERROR_TYPE_ERROR,
1788 "Cannot connect to peer `%s' address `%s''\n",
1789 http_common_plugin_address_to_string (plugin->protocol,
1790 s->address->address,
1791 s->address->address_length),
1792 GNUNET_i2s (&s->target));
1793 client_delete_session (s);
1796 notify_session_monitor (plugin,
1798 GNUNET_TRANSPORT_SS_UP); /* or handshake? */
1804 * Setup http_client plugin
1806 * @param plugin the plugin handle
1807 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1810 client_start (struct HTTP_Client_Plugin *plugin)
1812 curl_global_init (CURL_GLOBAL_ALL);
1813 plugin->curl_multi_handle = curl_multi_init ();
1815 if (NULL == plugin->curl_multi_handle)
1817 LOG (GNUNET_ERROR_TYPE_ERROR,
1818 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1820 return GNUNET_SYSERR;
1827 * Another peer has suggested an address for this
1828 * peer and transport plugin. Check that this could be a valid
1829 * address. If so, consider adding it to the list
1832 * @param cls closure with the `struct Plugin`
1833 * @param addr pointer to the address
1834 * @param addrlen length of @a addr
1835 * @return #GNUNET_OK if this is a plausible address for this peer
1836 * and transport; always returns #GNUNET_NO (this is the client!)
1839 http_client_plugin_address_suggested (void *cls,
1843 /* A HTTP/S client does not have any valid address so:*/
1849 * Exit point from the plugin.
1851 * @param cls api as closure
1855 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1857 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1858 struct HTTP_Client_Plugin *plugin = api->cls;
1860 if (NULL == api->cls)
1866 LOG (GNUNET_ERROR_TYPE_DEBUG,
1867 _("Shutting down plugin `%s'\n"),
1869 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
1870 &destroy_session_cb,
1872 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1874 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1875 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1877 if (NULL != plugin->curl_multi_handle)
1879 curl_multi_cleanup (plugin->curl_multi_handle);
1880 plugin->curl_multi_handle = NULL;
1882 curl_global_cleanup ();
1883 LOG (GNUNET_ERROR_TYPE_DEBUG,
1884 _("Shutdown for plugin `%s' complete\n"),
1886 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
1887 GNUNET_free_non_null (plugin->proxy_hostname);
1888 GNUNET_free_non_null (plugin->proxy_username);
1889 GNUNET_free_non_null (plugin->proxy_password);
1890 GNUNET_free (plugin);
1899 * @param plugin the plugin handle
1900 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1903 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1905 unsigned long long max_connections;
1909 /* Optional parameters */
1911 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1915 max_connections = 128;
1916 plugin->max_connections = max_connections;
1918 LOG (GNUNET_ERROR_TYPE_DEBUG,
1919 _("Maximum number of connections is %u\n"),
1920 plugin->max_connections);
1922 /* Read proxy configuration */
1923 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1924 plugin->name, "PROXY", &plugin->proxy_hostname))
1926 LOG (GNUNET_ERROR_TYPE_DEBUG,
1927 "Found proxy host: `%s'\n",
1928 plugin->proxy_hostname);
1929 /* proxy username */
1931 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1934 &plugin->proxy_username))
1936 LOG (GNUNET_ERROR_TYPE_DEBUG,
1937 "Found proxy username name: `%s'\n",
1938 plugin->proxy_username);
1941 /* proxy password */
1943 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1946 &plugin->proxy_password))
1948 LOG (GNUNET_ERROR_TYPE_DEBUG,
1949 "Found proxy password name: `%s'\n",
1950 plugin->proxy_password);
1955 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1960 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
1962 if (0 == strcmp(proxy_type, "HTTP"))
1963 plugin->proxytype = CURLPROXY_HTTP;
1964 else if (0 == strcmp(proxy_type, "SOCKS4"))
1965 plugin->proxytype = CURLPROXY_SOCKS4;
1966 else if (0 == strcmp(proxy_type, "SOCKS5"))
1967 plugin->proxytype = CURLPROXY_SOCKS5;
1968 else if (0 == strcmp(proxy_type, "SOCKS4A"))
1969 plugin->proxytype = CURLPROXY_SOCKS4A;
1970 else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
1971 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
1974 LOG (GNUNET_ERROR_TYPE_ERROR,
1975 _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
1978 GNUNET_free (proxy_type);
1979 GNUNET_free (plugin->proxy_hostname);
1980 plugin->proxy_hostname = NULL;
1981 GNUNET_free_non_null (plugin->proxy_username);
1982 plugin->proxy_username = NULL;
1983 GNUNET_free_non_null (plugin->proxy_password);
1984 plugin->proxy_password = NULL;
1986 return GNUNET_SYSERR;
1989 LOG (GNUNET_ERROR_TYPE_DEBUG,
1990 "Found proxy type: `%s'\n",
1994 /* proxy http tunneling */
1995 plugin->proxy_use_httpproxytunnel
1996 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
1998 "PROXY_HTTP_TUNNELING");
1999 if (GNUNET_SYSERR == plugin->proxy_use_httpproxytunnel)
2000 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
2002 GNUNET_free_non_null (proxy_type);
2009 * Function called by the pretty printer for the resolved address for
2010 * each human-readable address obtained. The callback can be called
2011 * several times. The last invocation must be with a @a address of
2012 * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
2013 * errors, the callback might be called first with @a address NULL and
2014 * @a res being #GNUNET_SYSERR. In that case, there must still be a
2015 * subsequent call later with @a address NULL and @a res #GNUNET_OK.
2017 * @param cls closure
2018 * @param address one of the names for the host, NULL on last callback
2019 * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on failure,
2020 * #GNUNET_OK on last callback
2023 http_client_plugin_address_to_string (void *cls,
2027 return http_common_plugin_address_to_string (PLUGIN_NAME,
2034 * Function that will be called whenever the transport service wants to
2035 * notify the plugin that a session is still active and in use and
2036 * therefore the session timeout for this session has to be updated
2038 * @param cls closure
2039 * @param peer which peer was the session for
2040 * @param session which session is being updated
2043 http_client_plugin_update_session_timeout (void *cls,
2044 const struct GNUNET_PeerIdentity *peer,
2045 struct Session *session)
2047 client_reschedule_session_timeout (session);
2052 * Function that will be called whenever the transport service wants to
2053 * notify the plugin that the inbound quota changed and that the plugin
2054 * should update it's delay for the next receive value
2056 * @param cls closure
2057 * @param peer which peer was the session for
2058 * @param session which session is being updated
2059 * @param delay new delay to use for receiving
2062 http_client_plugin_update_inbound_delay (void *cls,
2063 const struct GNUNET_PeerIdentity *peer,
2065 struct GNUNET_TIME_Relative delay)
2067 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
2068 LOG (GNUNET_ERROR_TYPE_DEBUG,
2069 "New inbound delay %s\n",
2070 GNUNET_STRINGS_relative_time_to_string (delay,
2072 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
2074 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
2076 = GNUNET_SCHEDULER_add_delayed (delay,
2084 * Return information about the given session to the
2087 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2088 * @param peer peer we send information about
2089 * @param value our `struct Session` to send information about
2090 * @return #GNUNET_OK (continue to iterate)
2093 send_session_info_iter (void *cls,
2094 const struct GNUNET_PeerIdentity *peer,
2097 struct HTTP_Client_Plugin *plugin = cls;
2098 struct Session *session = value;
2100 notify_session_monitor (plugin,
2102 GNUNET_TRANSPORT_SS_UP);
2108 * Begin monitoring sessions of a plugin. There can only
2109 * be one active monitor per plugin (i.e. if there are
2110 * multiple monitors, the transport service needs to
2111 * multiplex the generated events over all of them).
2113 * @param cls closure of the plugin
2114 * @param sic callback to invoke, NULL to disable monitor;
2115 * plugin will being by iterating over all active
2116 * sessions immediately and then enter monitor mode
2117 * @param sic_cls closure for @a sic
2120 http_client_plugin_setup_monitor (void *cls,
2121 GNUNET_TRANSPORT_SessionInfoCallback sic,
2124 struct HTTP_Client_Plugin *plugin = cls;
2127 plugin->sic_cls = sic_cls;
2130 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2131 &send_session_info_iter,
2133 /* signal end of first iteration */
2134 sic (sic_cls, NULL, NULL);
2140 * Entry point for the plugin.
2143 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2145 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2146 struct GNUNET_TRANSPORT_PluginFunctions *api;
2147 struct HTTP_Client_Plugin *plugin;
2149 if (NULL == env->receive)
2151 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2152 initialze the plugin or the API */
2153 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2155 api->address_to_string = &http_client_plugin_address_to_string;
2156 api->string_to_address = &http_common_plugin_string_to_address;
2157 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2161 plugin = GNUNET_new (struct HTTP_Client_Plugin);
2163 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
2165 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2167 api->send = &http_client_plugin_send;
2168 api->disconnect_session = &http_client_plugin_session_disconnect;
2169 api->query_keepalive_factor = &http_client_query_keepalive_factor;
2170 api->disconnect_peer = &http_client_plugin_peer_disconnect;
2171 api->check_address = &http_client_plugin_address_suggested;
2172 api->get_session = &http_client_plugin_get_session;
2173 api->address_to_string = &http_client_plugin_address_to_string;
2174 api->string_to_address = &http_common_plugin_string_to_address;
2175 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2176 api->get_network = &http_client_plugin_get_network;
2177 api->update_session_timeout = &http_client_plugin_update_session_timeout;
2178 api->update_inbound_delay = &http_client_plugin_update_inbound_delay;
2179 api->setup_monitor = &http_client_plugin_setup_monitor;
2181 plugin->name = "transport-https_client";
2182 plugin->protocol = "https";
2184 plugin->name = "transport-http_client";
2185 plugin->protocol = "http";
2187 plugin->last_tag = 1;
2189 if (GNUNET_SYSERR == client_configure_plugin (plugin))
2191 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2196 if (GNUNET_SYSERR == client_start (plugin))
2198 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2204 /* end of plugin_transport_http_client.c */