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 * Curl client PUT handle.
154 * FIXME: delta to put.easyhandle?
159 * Handle for the HTTP PUT request.
161 struct ConnectionHandle put;
164 * Curl client GET handle
165 * FIXME: delta to get.easyhandle?
170 * Handle for the HTTP GET request.
172 struct ConnectionHandle get;
175 * next pointer for double linked list
177 struct HTTP_Message *msg_head;
180 * previous pointer for double linked list
182 struct HTTP_Message *msg_tail;
185 * Message stream tokenizer for incoming data
187 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
190 * Session timeout task
192 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
195 * Session timeout task
197 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
200 * Task to wake up client receive handle when receiving is allowed again
202 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
205 * Absolute time when to receive data again.
206 * Used for receive throttling.
208 struct GNUNET_TIME_Absolute next_receive;
211 * When does this session time out.
213 struct GNUNET_TIME_Absolute timeout;
216 * Number of bytes waiting for transmission to this peer.
218 unsigned long long bytes_in_queue;
221 * Outbound overhead due to HTTP connection
222 * Add to next message of this session when calling callback
227 * Number of messages waiting for transmission to this peer.
229 unsigned int msgs_in_queue;
232 * ATS network type in NBO
234 uint32_t ats_address_network_type;
237 * Is the client PUT handle currently paused?
242 * Is the client PUT handle disconnect in progress?
244 int put_tmp_disconnecting;
247 * Is the client PUT handle temporarily disconnected?
249 int put_tmp_disconnected;
252 * We received data to send while disconnecting, reconnect immediately
254 int put_reconnect_required;
259 * Encapsulation of all of the state of the plugin.
261 struct HTTP_Client_Plugin
266 struct GNUNET_TRANSPORT_PluginEnvironment *env;
271 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
274 * Function to call about session status changes.
276 GNUNET_TRANSPORT_SessionInfoCallback sic;
279 * Closure for @e sic.
294 * Proxy configuration: hostname or ip of the proxy server
296 char *proxy_hostname;
299 * Username for the proxy server
301 char *proxy_username;
304 * Password for the proxy server
306 char *proxy_password;
311 CURLM *curl_multi_handle;
316 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
319 * Type of proxy server:
321 * Valid values as supported by curl:
322 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
323 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
325 curl_proxytype proxytype;
328 * Use proxy tunneling:
329 * Tunnel all operations through a given HTTP instead of have the proxy
330 * evaluate the HTTP request
332 * Default: #GNUNET_NO, #GNUNET_YES experimental
334 int proxy_use_httpproxytunnel;
337 * My options to be included in the address
342 * Maximum number of sockets the plugin can use
343 * Each http inbound /outbound connections are two connections
345 unsigned int max_connections;
348 * Current number of sockets the plugin can use
349 * Each http inbound /outbound connections are two connections
351 unsigned int cur_connections;
354 * Last used unique HTTP connection tag
373 * If a session monitor is attached, notify it about the new
376 * @param plugin our plugin
377 * @param session session that changed state
378 * @param state new state of the session
381 notify_session_monitor (struct HTTP_Client_Plugin *plugin,
382 struct Session *session,
383 enum GNUNET_TRANSPORT_SessionState state)
385 struct GNUNET_TRANSPORT_SessionInfo info;
387 if (NULL == plugin->sic)
389 memset (&info, 0, sizeof (info));
391 info.is_inbound = GNUNET_NO;
392 info.num_msg_pending = session->msgs_in_queue;
393 info.num_bytes_pending = session->bytes_in_queue;
394 info.receive_delay = session->next_receive;
395 info.session_timeout = session->timeout;
396 info.address = session->address;
397 plugin->sic (plugin->sic_cls,
404 * Delete session @a s.
406 * @param s the session to delete
409 client_delete_session (struct Session *s)
411 struct HTTP_Client_Plugin *plugin = s->plugin;
412 struct HTTP_Message *pos;
413 struct HTTP_Message *next;
416 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
418 GNUNET_SCHEDULER_cancel (s->timeout_task);
419 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
420 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
422 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
424 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
425 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
427 if (GNUNET_SCHEDULER_NO_TASK != s->recv_wakeup_task)
429 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
430 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
432 GNUNET_assert (GNUNET_OK ==
433 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
436 if (NULL != s->client_put)
438 LOG (GNUNET_ERROR_TYPE_DEBUG,
439 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
442 GNUNET_i2s (&s->address->peer));
444 /* remove curl handle from multi handle */
445 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
447 GNUNET_break (CURLM_OK == mret);
448 curl_easy_cleanup (s->client_put);
449 s->client_put = NULL;
451 if (NULL != s->client_get)
453 LOG (GNUNET_ERROR_TYPE_DEBUG,
454 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
456 GNUNET_i2s (&s->address->peer));
457 /* remove curl handle from multi handle */
458 mret = curl_multi_remove_handle (plugin->curl_multi_handle,
460 GNUNET_break (CURLM_OK == mret);
461 curl_easy_cleanup (s->client_get);
462 GNUNET_assert (plugin->cur_connections > 0);
463 plugin->cur_connections--;
464 s->client_get = NULL;
466 GNUNET_STATISTICS_set (plugin->env->stats,
467 HTTP_STAT_STR_CONNECTIONS,
468 plugin->cur_connections,
471 while (NULL != (pos = next))
474 GNUNET_CONTAINER_DLL_remove (s->msg_head,
477 GNUNET_assert (0 < s->msgs_in_queue);
479 GNUNET_assert (pos->size <= s->bytes_in_queue);
480 s->bytes_in_queue -= pos->size;
481 if (NULL != pos->transmit_cont)
482 pos->transmit_cont (pos->transmit_cont_cls,
486 pos->pos + s->overhead);
490 GNUNET_assert (0 == s->msgs_in_queue);
491 GNUNET_assert (0 == s->bytes_in_queue);
492 notify_session_monitor (plugin,
494 GNUNET_TRANSPORT_SS_DOWN);
495 if (NULL != s->msg_tk)
497 GNUNET_SERVER_mst_destroy (s->msg_tk);
500 GNUNET_HELLO_address_free (s->address);
501 GNUNET_free (s->url);
507 * Increment session timeout due to activity for session @a s.
509 * @param s the session
512 client_reschedule_session_timeout (struct Session *s)
514 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
515 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
520 * Task performing curl operations
522 * @param cls plugin as closure
523 * @param tc gnunet scheduler task context
526 client_run (void *cls,
527 const struct GNUNET_SCHEDULER_TaskContext *tc);
531 * Function setting up file descriptors and scheduling task to run
533 * @param plugin the plugin as closure
534 * @param now schedule task in 1ms, regardless of what curl may say
535 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
538 client_schedule (struct HTTP_Client_Plugin *plugin,
545 struct GNUNET_NETWORK_FDSet *grs;
546 struct GNUNET_NETWORK_FDSet *gws;
549 struct GNUNET_TIME_Relative timeout;
551 /* Cancel previous scheduled task */
552 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
554 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
555 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
561 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
562 if (mret != CURLM_OK)
564 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
565 "curl_multi_fdset", __FILE__, __LINE__,
566 curl_multi_strerror (mret));
567 return GNUNET_SYSERR;
569 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
571 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
573 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
574 if (now == GNUNET_YES)
575 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
577 if (mret != CURLM_OK)
579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
580 _("%s failed at %s:%d: `%s'\n"),
581 "curl_multi_timeout", __FILE__, __LINE__,
582 curl_multi_strerror (mret));
583 return GNUNET_SYSERR;
586 grs = GNUNET_NETWORK_fdset_create ();
587 gws = GNUNET_NETWORK_fdset_create ();
588 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
589 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
591 plugin->client_perform_task =
592 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
594 &client_run, plugin);
595 GNUNET_NETWORK_fdset_destroy (gws);
596 GNUNET_NETWORK_fdset_destroy (grs);
604 * @param curl the curl easy handle
605 * @param type message type
606 * @param data data to log, NOT a 0-terminated string
607 * @param size data length
608 * @param cls the closure
612 client_log (CURL *curl,
618 struct ConnectionHandle *ch = cls;
619 const char *ttype = "UNSPECIFIED";
622 if (! ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT)))
629 case CURLINFO_HEADER_IN:
632 case CURLINFO_HEADER_OUT:
633 ttype = "HEADER_OUT";
635 GNUNET_assert (NULL != ch);
636 GNUNET_assert (NULL != ch->easyhandle);
637 GNUNET_assert (NULL != ch->s);
638 ch->s->overhead += size;
641 ttype = "UNSPECIFIED";
644 memcpy (text, data, size);
645 if (text[size - 1] == '\n')
652 text[size + 1] = '\0';
654 LOG (GNUNET_ERROR_TYPE_DEBUG,
655 "Connection %p %s: %s",
664 * Connect a HTTP put connection
666 * @param s the session to connect
667 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
670 client_connect_put (struct Session *s);
674 * Function that can be used by the transport service to transmit
675 * a message using the plugin. Note that in the case of a
676 * peer disconnecting, the continuation MUST be called
677 * prior to the disconnect notification itself. This function
678 * will be called with this peer's HELLO message to initiate
679 * a fresh connection to another peer.
682 * @param s which session must be used
683 * @param msgbuf the message to transmit
684 * @param msgbuf_size number of bytes in @a msgbuf
685 * @param priority how important is the message (most plugins will
686 * ignore message priority and just FIFO)
687 * @param to how long to wait at most for the transmission (does not
688 * require plugins to discard the message after the timeout,
689 * just advisory for the desired delay; most plugins will ignore
691 * @param cont continuation to call once the message has
692 * been transmitted (or if the transport is ready
693 * for the next transmission call; or if the
694 * peer disconnected...); can be NULL
695 * @param cont_cls closure for cont
696 * @return number of bytes used (on the physical network, with overheads);
697 * -1 on hard errors (i.e. address invalid); 0 is a legal value
698 * and does NOT mean that the message was not transmitted (DV)
701 http_client_plugin_send (void *cls,
705 unsigned int priority,
706 struct GNUNET_TIME_Relative to,
707 GNUNET_TRANSPORT_TransmitContinuation cont,
710 struct HTTP_Client_Plugin *plugin = cls;
711 struct HTTP_Message *msg;
714 LOG (GNUNET_ERROR_TYPE_DEBUG,
715 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
717 msgbuf_size, GNUNET_i2s (&s->address->peer));
719 /* create new message and schedule */
720 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
722 msg->size = msgbuf_size;
724 msg->buf = (char *) &msg[1];
725 msg->transmit_cont = cont;
726 msg->transmit_cont_cls = cont_cls;
727 memcpy (msg->buf, msgbuf, msgbuf_size);
728 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head,
732 s->bytes_in_queue += msg->size;
734 GNUNET_asprintf (&stat_txt,
735 "# bytes currently in %s_client buffers",
737 GNUNET_STATISTICS_update (plugin->env->stats,
738 stat_txt, msgbuf_size, GNUNET_NO);
739 GNUNET_free (stat_txt);
740 notify_session_monitor (plugin,
742 GNUNET_TRANSPORT_SS_UP);
743 if (GNUNET_YES == s->put_tmp_disconnecting)
745 /* PUT connection is currently getting disconnected */
746 s->put_reconnect_required = GNUNET_YES;
747 LOG (GNUNET_ERROR_TYPE_DEBUG,
748 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
753 if (GNUNET_YES == s->put_paused)
755 /* PUT connection was paused, unpause */
756 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
757 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
758 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
759 LOG (GNUNET_ERROR_TYPE_DEBUG,
760 "Session %p/connection %p: unpausing connection\n",
762 s->put_paused = GNUNET_NO;
763 if (NULL != s->client_put)
764 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
766 else if (GNUNET_YES == s->put_tmp_disconnected)
768 /* PUT connection was disconnected, reconnect */
769 LOG (GNUNET_ERROR_TYPE_DEBUG,
770 "Session %p: Reconnecting PUT connection\n",
772 s->put_tmp_disconnected = GNUNET_NO;
773 GNUNET_break (NULL == s->client_put);
774 if (GNUNET_SYSERR == client_connect_put (s))
775 return GNUNET_SYSERR;
777 client_schedule (s->plugin,
784 * Disconnect a session
786 * @param cls the `struct HTTP_Client_Plugin *`
788 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
791 http_client_plugin_session_disconnect (void *cls,
794 struct HTTP_Client_Plugin *plugin = cls;
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "Session %p: notifying transport about ending session\n",s);
798 plugin->env->session_end (plugin->env->cls,
801 client_delete_session (s);
803 /* Re-schedule since handles have changed */
804 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
806 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
807 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
809 client_schedule (plugin, GNUNET_YES);
816 * Function that is called to get the keepalive factor.
817 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
818 * calculate the interval between keepalive packets.
820 * @param cls closure with the `struct Plugin`
821 * @return keepalive factor
824 http_client_query_keepalive_factor (void *cls)
831 * Callback to destroys all sessions on exit.
833 * @param cls the `struct HTTP_Client_Plugin *`
834 * @param peer identity of the peer
835 * @param value the `struct Session *`
836 * @return #GNUNET_OK (continue iterating)
839 destroy_session_cb (void *cls,
840 const struct GNUNET_PeerIdentity *peer,
843 struct HTTP_Client_Plugin *plugin = cls;
844 struct Session *session = value;
846 http_client_plugin_session_disconnect (plugin, session);
852 * Function that can be used to force the plugin to disconnect
853 * from the given peer and cancel all previous transmissions
854 * (and their continuationc).
857 * @param target peer from which to disconnect
860 http_client_plugin_peer_disconnect (void *cls,
861 const struct GNUNET_PeerIdentity *target)
863 struct HTTP_Client_Plugin *plugin = cls;
865 LOG (GNUNET_ERROR_TYPE_DEBUG,
866 "Transport tells me to disconnect `%s'\n",
867 GNUNET_i2s (target));
868 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
876 * Closure for #session_lookup_client_by_address().
878 struct SessionClientCtx
881 * Address we are looking for.
883 const struct GNUNET_HELLO_Address *address;
886 * Session that was found.
893 * Locate the seession object for a given address.
895 * @param cls the `struct SessionClientCtx *`
896 * @param key peer identity
897 * @param value the `struct Session` to check
898 * @return #GNUNET_NO if found, #GNUNET_OK if not
901 session_lookup_client_by_address (void *cls,
902 const struct GNUNET_PeerIdentity *key,
905 struct SessionClientCtx *sc_ctx = cls;
906 struct Session *s = value;
908 if (0 == GNUNET_HELLO_address_cmp (sc_ctx->address,
919 * Check if a sessions exists for an specific address
921 * @param plugin the plugin
922 * @param address the address
923 * @return the session or NULL
925 static struct Session *
926 client_lookup_session (struct HTTP_Client_Plugin *plugin,
927 const struct GNUNET_HELLO_Address *address)
929 struct SessionClientCtx sc_ctx;
931 sc_ctx.address = address;
933 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
934 &session_lookup_client_by_address,
941 * When we have nothing to transmit, we pause the HTTP PUT
942 * after a while (so that gnurl stops asking). This task
943 * is the delayed task that actually pauses the PUT.
945 * @param cls the `struct Session *` with the put
946 * @param tc scheduler context
949 client_put_disconnect (void *cls,
950 const struct GNUNET_SCHEDULER_TaskContext *tc)
952 struct Session *s = cls;
954 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
955 LOG (GNUNET_ERROR_TYPE_DEBUG,
956 "Session %p/connection %p: will be disconnected due to no activity\n",
959 s->put_paused = GNUNET_NO;
960 s->put_tmp_disconnecting = GNUNET_YES;
961 if (NULL != s->client_put)
962 curl_easy_pause (s->client_put,
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",
1001 LOG (GNUNET_ERROR_TYPE_DEBUG,
1002 "Session %p/connection %p: nothing to send, suspending\n",
1004 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
1005 &client_put_disconnect,
1007 s->put_paused = GNUNET_YES;
1008 return CURL_READFUNC_PAUSE;
1011 GNUNET_assert (msg->pos < msg->size);
1012 /* calculate how much fits in buffer */
1013 len = GNUNET_MIN (msg->size - msg->pos,
1015 memcpy (stream, &msg->buf[msg->pos], len);
1017 if (msg->pos == msg->size)
1019 LOG (GNUNET_ERROR_TYPE_DEBUG,
1020 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
1021 s, s->client_put, msg->size, msg->pos);
1022 /* Calling transmit continuation */
1023 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1026 GNUNET_assert (0 < s->msgs_in_queue);
1028 GNUNET_assert (msg->size <= s->bytes_in_queue);
1029 s->bytes_in_queue -= msg->size;
1030 if (NULL != msg->transmit_cont)
1031 msg->transmit_cont (msg->transmit_cont_cls,
1035 msg->size + s->overhead);
1039 notify_session_monitor (plugin,
1041 GNUNET_TRANSPORT_SS_UP);
1042 GNUNET_asprintf (&stat_txt,
1043 "# bytes currently in %s_client buffers",
1045 GNUNET_STATISTICS_update (plugin->env->stats,
1049 GNUNET_free (stat_txt);
1050 GNUNET_asprintf (&stat_txt,
1051 "# bytes transmitted via %s_client",
1053 GNUNET_STATISTICS_update (plugin->env->stats,
1057 GNUNET_free (stat_txt);
1063 * Wake up a curl handle which was suspended
1065 * @param cls the session
1066 * @param tc task context
1069 client_wake_up (void *cls,
1070 const struct GNUNET_SCHEDULER_TaskContext *tc)
1072 struct Session *s = cls;
1074 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1075 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1077 LOG (GNUNET_ERROR_TYPE_DEBUG,
1078 "Session %p/connection %p: Waking up GET handle\n",
1081 if (GNUNET_YES == s->put_paused)
1083 /* PUT connection was paused, unpause */
1084 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
1085 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
1086 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
1087 s->put_paused = GNUNET_NO;
1088 if (NULL != s->client_put)
1089 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
1091 if (NULL != s->client_get)
1092 curl_easy_pause (s->client_get,
1098 * Callback for message stream tokenizer
1100 * @param cls the session
1101 * @param client not used
1102 * @param message the message received
1103 * @return always #GNUNET_OK
1106 client_receive_mst_cb (void *cls,
1108 const struct GNUNET_MessageHeader *message)
1110 struct Session *s = cls;
1111 struct HTTP_Client_Plugin *plugin;
1112 struct GNUNET_TIME_Relative delay;
1113 struct GNUNET_ATS_Information atsi;
1117 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1118 atsi.value = s->ats_address_network_type;
1119 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1121 delay = s->plugin->env->receive (plugin->env->cls,
1125 plugin->env->update_address_metrics (plugin->env->cls,
1129 GNUNET_asprintf (&stat_txt,
1130 "# bytes received via %s_client",
1132 GNUNET_STATISTICS_update (plugin->env->stats,
1134 ntohs (message->size),
1136 GNUNET_free (stat_txt);
1138 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1139 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 "Client: peer `%s' address `%s' next read delayed for %s\n",
1143 GNUNET_i2s (&s->address->peer),
1144 http_common_plugin_address_to_string (s->plugin->protocol,
1145 s->address->address,
1146 s->address->address_length),
1147 GNUNET_STRINGS_relative_time_to_string (delay,
1150 client_reschedule_session_timeout (s);
1156 * Callback method used with libcurl when data for a PUT connection are
1157 * received. We do not expect data here, so we just discard it.
1159 * @param stream pointer where to write data
1160 * @param size size of an individual element
1161 * @param nmemb count of elements that can be written to the buffer
1162 * @param cls destination pointer, passed to the libcurl handle
1163 * @return bytes read from stream
1166 client_receive_put (void *stream,
1171 return size * nmemb;
1176 * Callback method used with libcurl when data for a GET connection are
1177 * received. Forward to MST
1179 * @param stream pointer where to write data
1180 * @param size size of an individual element
1181 * @param nmemb count of elements that can be written to the buffer
1182 * @param cls destination pointer, passed to the libcurl handle
1183 * @return bytes read from stream
1186 client_receive (void *stream,
1191 struct Session *s = cls;
1192 struct GNUNET_TIME_Absolute now;
1193 size_t len = size * nmemb;
1195 LOG (GNUNET_ERROR_TYPE_DEBUG,
1196 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
1198 len, GNUNET_i2s (&s->address->peer));
1199 now = GNUNET_TIME_absolute_get ();
1200 if (now.abs_value_us < s->next_receive.abs_value_us)
1202 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1203 struct GNUNET_TIME_Relative delta
1204 = GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1206 LOG (GNUNET_ERROR_TYPE_DEBUG,
1207 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1210 GNUNET_STRINGS_relative_time_to_string (delta,
1212 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1214 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1215 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1218 = GNUNET_SCHEDULER_add_delayed (delta,
1221 return CURL_WRITEFUNC_PAUSE;
1223 if (NULL == s->msg_tk)
1224 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb,
1226 GNUNET_SERVER_mst_receive (s->msg_tk,
1237 * Task performing curl operations
1239 * @param cls plugin as closure
1240 * @param tc scheduler task context
1243 client_run (void *cls,
1244 const struct GNUNET_SCHEDULER_TaskContext *tc)
1246 struct HTTP_Client_Plugin *plugin = cls;
1248 long http_statuscode;
1253 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1254 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1259 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1262 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1264 CURL *easy_h = msg->easy_handle;
1265 struct Session *s = NULL;
1266 char *d = NULL; /* curl requires 'd' to be a 'char *' */
1271 LOG (GNUNET_ERROR_TYPE_DEBUG,
1272 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1274 curl_easy_strerror (msg->data.result), running);
1278 GNUNET_assert (CURLE_OK ==
1279 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1280 s = (struct Session *) d;
1281 GNUNET_assert (NULL != s);
1282 if (msg->msg == CURLMSG_DONE)
1284 GNUNET_break (CURLE_OK ==
1285 curl_easy_getinfo (easy_h,
1286 CURLINFO_RESPONSE_CODE,
1288 if (easy_h == s->client_put)
1290 if ((0 != msg->data.result) || (http_statuscode != 200))
1292 LOG (GNUNET_ERROR_TYPE_DEBUG,
1293 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1294 s, msg->easy_handle,
1295 GNUNET_i2s (&s->address->peer),
1298 curl_easy_strerror (msg->data.result));
1301 LOG (GNUNET_ERROR_TYPE_DEBUG,
1302 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1303 s, msg->easy_handle,
1304 GNUNET_i2s (&s->address->peer));
1305 if (NULL == s->client_get)
1307 /* Disconnect other transmission direction and tell transport */
1310 curl_multi_remove_handle (plugin->curl_multi_handle,
1312 curl_easy_cleanup (easy_h);
1313 GNUNET_assert (plugin->cur_connections > 0);
1314 plugin->cur_connections--;
1315 s->put_tmp_disconnecting = GNUNET_NO;
1316 s->put_tmp_disconnected = GNUNET_YES;
1317 s->client_put = NULL;
1318 s->put.easyhandle = NULL;
1322 * Handling a rare case:
1323 * plugin_send was called during temporary put disconnect,
1324 * reconnect required after connection was disconnected
1326 if (GNUNET_YES == s->put_reconnect_required)
1328 s->put_reconnect_required = GNUNET_NO;
1329 if (GNUNET_SYSERR == client_connect_put (s))
1331 GNUNET_break (s->client_put == NULL);
1332 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1336 if (easy_h == s->client_get)
1338 if ((0 != msg->data.result) || (http_statuscode != 200))
1340 LOG (GNUNET_ERROR_TYPE_DEBUG,
1341 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1344 GNUNET_i2s (&s->address->peer),
1347 curl_easy_strerror (msg->data.result));
1351 LOG (GNUNET_ERROR_TYPE_DEBUG,
1352 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1355 GNUNET_i2s (&s->address->peer));
1356 /* Disconnect other transmission direction and tell transport */
1357 s->get.easyhandle = NULL;
1359 /* FIXME: who calls curl_multi_remove on 'easy_h' now!? */
1360 GNUNET_assert (plugin->cur_connections > 0);
1361 plugin->cur_connections--;
1362 http_client_plugin_session_disconnect (plugin, s);
1367 while (mret == CURLM_CALL_MULTI_PERFORM);
1368 client_schedule (plugin, GNUNET_NO);
1373 * Connect GET connection for a session
1375 * @param s the session to connect
1376 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1379 client_connect_get (struct Session *s)
1383 /* create get connection */
1384 s->client_get = curl_easy_init ();
1386 s->get.easyhandle = s->client_get;
1388 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1389 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1390 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1393 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1395 struct HttpAddress *ha;
1397 ha = (struct HttpAddress *) s->address->address;
1399 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1400 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1402 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1403 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1407 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1408 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1411 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1412 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1414 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1415 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1418 if (NULL != s->plugin->proxy_hostname)
1420 curl_easy_setopt (s->client_get, CURLOPT_PROXY, s->plugin->proxy_hostname);
1421 curl_easy_setopt (s->client_get, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1422 if (NULL != s->plugin->proxy_username)
1423 curl_easy_setopt (s->client_get, CURLOPT_PROXYUSERNAME,
1424 s->plugin->proxy_username);
1425 if (NULL != s->plugin->proxy_password)
1426 curl_easy_setopt (s->client_get, CURLOPT_PROXYPASSWORD,
1427 s->plugin->proxy_password);
1428 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1429 curl_easy_setopt (s->client_get, CURLOPT_HTTPPROXYTUNNEL,
1430 s->plugin->proxy_use_httpproxytunnel);
1433 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1434 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1435 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1436 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1437 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1438 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1439 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1440 /* No timeout by default, timeout done with session timeout */
1441 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1442 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1443 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1444 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1445 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1446 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1447 #if CURL_TCP_NODELAY
1448 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1450 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1452 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1454 if (CURLM_OK != mret)
1456 LOG (GNUNET_ERROR_TYPE_ERROR,
1457 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1459 curl_multi_strerror (mret));
1460 curl_easy_cleanup (s->client_get);
1461 s->client_get = NULL;
1463 s->get.easyhandle = NULL;
1465 return GNUNET_SYSERR;
1467 s->plugin->cur_connections++;
1473 * Connect a HTTP put connection
1475 * @param s the session to connect
1476 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1479 client_connect_put (struct Session *s)
1483 /* create put connection */
1484 LOG (GNUNET_ERROR_TYPE_DEBUG,
1485 "Session %p: Init PUT handle\n", s);
1486 s->client_put = curl_easy_init ();
1488 s->put.easyhandle = s->client_put;
1490 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1491 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1492 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1495 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1497 struct HttpAddress *ha;
1498 ha = (struct HttpAddress *) s->address->address;
1500 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1501 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1503 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1504 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1508 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1509 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1512 curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1513 curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1515 curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1516 curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1518 if (s->plugin->proxy_hostname != NULL)
1520 curl_easy_setopt (s->client_put, CURLOPT_PROXY, s->plugin->proxy_hostname);
1521 curl_easy_setopt (s->client_put, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1522 if (NULL != s->plugin->proxy_username)
1523 curl_easy_setopt (s->client_put, CURLOPT_PROXYUSERNAME,
1524 s->plugin->proxy_username);
1525 if (NULL != s->plugin->proxy_password)
1526 curl_easy_setopt (s->client_put, CURLOPT_PROXYPASSWORD,
1527 s->plugin->proxy_password);
1528 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1529 curl_easy_setopt (s->client_put, CURLOPT_HTTPPROXYTUNNEL,
1530 s->plugin->proxy_use_httpproxytunnel);
1533 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1534 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1535 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1536 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1537 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1538 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1539 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1540 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1541 /* No timeout by default, timeout done with session timeout */
1542 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1543 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1544 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1545 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1546 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1547 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1548 #if CURL_TCP_NODELAY
1549 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1551 mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
1553 if (CURLM_OK != mret)
1555 LOG (GNUNET_ERROR_TYPE_ERROR,
1556 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1558 curl_multi_strerror (mret));
1559 curl_easy_cleanup (s->client_put);
1560 s->client_put = NULL;
1561 s->put.easyhandle = NULL;
1563 s->put_tmp_disconnected = GNUNET_YES;
1564 return GNUNET_SYSERR;
1566 s->put_tmp_disconnected = GNUNET_NO;
1567 s->plugin->cur_connections++;
1574 * Connect both PUT and GET connection for a session
1576 * @param s the session to connect
1577 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1580 client_connect (struct Session *s)
1582 struct HTTP_Client_Plugin *plugin = s->plugin;
1583 int res = GNUNET_OK;
1586 if (NULL == http_common_plugin_address_to_string (plugin->protocol,
1587 s->address->address,
1588 s->address->address_length))
1590 LOG (GNUNET_ERROR_TYPE_DEBUG,
1591 "Invalid address peer `%s'\n",
1592 GNUNET_i2s (&s->address->peer));
1593 return GNUNET_SYSERR;
1596 GNUNET_asprintf (&s->url, "%s/%s;%u",
1597 http_common_plugin_address_to_url (NULL,
1598 s->address->address,
1599 s->address->address_length),
1600 GNUNET_i2s_full (plugin->env->my_identity),
1604 LOG (GNUNET_ERROR_TYPE_DEBUG,
1605 "Initiating outbound session peer `%s' using address `%s'\n",
1606 GNUNET_i2s (&s->address->peer), s->url);
1608 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1609 (GNUNET_SYSERR == client_connect_put (s)))
1610 return GNUNET_SYSERR;
1612 LOG (GNUNET_ERROR_TYPE_DEBUG,
1613 "Session %p: connected with connections GET %p and PUT %p\n",
1617 /* Perform connect */
1618 GNUNET_STATISTICS_set (plugin->env->stats,
1619 HTTP_STAT_STR_CONNECTIONS,
1620 plugin->cur_connections,
1622 /* Re-schedule since handles have changed */
1623 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1625 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1626 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1628 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1634 * Function obtain the network type for a session
1636 * @param cls closure (`struct Plugin*`)
1637 * @param session the session
1638 * @return the network type
1640 static enum GNUNET_ATS_Network_Type
1641 http_client_plugin_get_network (void *cls,
1642 struct Session *session)
1644 return ntohl (session->ats_address_network_type);
1649 * Session was idle, so disconnect it
1651 * @param cls the `struct Session` of the idle session
1652 * @param tc scheduler context
1655 client_session_timeout (void *cls,
1656 const struct GNUNET_SCHEDULER_TaskContext *tc)
1658 struct Session *s = cls;
1659 struct GNUNET_TIME_Relative left;
1661 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1662 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1663 if (0 != left.rel_value_us)
1665 /* not actually our turn yet, but let's at least update
1666 the monitor, it may think we're about to die ... */
1667 notify_session_monitor (s->plugin,
1669 GNUNET_TRANSPORT_SS_UP);
1670 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1671 &client_session_timeout,
1676 "Session %p was idle for %s, disconnecting\n",
1678 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1680 GNUNET_assert (GNUNET_OK ==
1681 http_client_plugin_session_disconnect (s->plugin,
1687 * Creates a new outbound session the transport service will use to
1688 * send data to the peer
1690 * @param cls the plugin
1691 * @param address the address
1692 * @return the session or NULL of max connections exceeded
1694 static struct Session *
1695 http_client_plugin_get_session (void *cls,
1696 const struct GNUNET_HELLO_Address *address)
1698 struct HTTP_Client_Plugin *plugin = cls;
1700 struct sockaddr *sa;
1701 struct GNUNET_ATS_Information ats;
1705 GNUNET_assert (NULL != address->address);
1707 /* find existing session */
1708 s = client_lookup_session (plugin, address);
1712 if (plugin->max_connections <= plugin->cur_connections)
1714 LOG (GNUNET_ERROR_TYPE_WARNING,
1715 "Maximum number of connections (%u) reached: "
1716 "cannot connect to peer `%s'\n",
1717 plugin->max_connections,
1718 GNUNET_i2s (&address->peer));
1722 /* Determine network location */
1723 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1724 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1725 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1726 if (GNUNET_SYSERR == res)
1728 if (GNUNET_YES == res)
1730 GNUNET_assert (NULL != sa);
1731 if (AF_INET == sa->sa_family)
1733 salen = sizeof (struct sockaddr_in);
1735 else if (AF_INET6 == sa->sa_family)
1737 salen = sizeof (struct sockaddr_in6);
1739 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1742 else if (GNUNET_NO == res)
1744 /* Cannot convert to sockaddr -> is external hostname */
1745 ats.value = htonl (GNUNET_ATS_NET_WAN);
1747 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1753 s = GNUNET_new (struct Session);
1755 s->address = GNUNET_HELLO_address_copy (address);
1756 s->ats_address_network_type = ats.value;
1757 s->put_paused = GNUNET_NO;
1758 s->put_tmp_disconnecting = GNUNET_NO;
1759 s->put_tmp_disconnected = GNUNET_NO;
1760 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT);
1761 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1762 &client_session_timeout,
1764 LOG (GNUNET_ERROR_TYPE_DEBUG,
1765 "Created new session %p for `%s' address `%s''\n",
1767 http_common_plugin_address_to_string (plugin->protocol,
1768 s->address->address,
1769 s->address->address_length),
1770 GNUNET_i2s (&s->address->peer));
1772 /* add new session */
1773 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1776 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1777 /* initiate new connection */
1778 if (GNUNET_SYSERR == client_connect (s))
1780 LOG (GNUNET_ERROR_TYPE_ERROR,
1781 "Cannot connect to peer `%s' address `%s''\n",
1782 http_common_plugin_address_to_string (plugin->protocol,
1783 s->address->address,
1784 s->address->address_length),
1785 GNUNET_i2s (&s->address->peer));
1786 client_delete_session (s);
1789 notify_session_monitor (plugin,
1791 GNUNET_TRANSPORT_SS_UP); /* or handshake? */
1797 * Setup http_client plugin
1799 * @param plugin the plugin handle
1800 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1803 client_start (struct HTTP_Client_Plugin *plugin)
1805 curl_global_init (CURL_GLOBAL_ALL);
1806 plugin->curl_multi_handle = curl_multi_init ();
1808 if (NULL == plugin->curl_multi_handle)
1810 LOG (GNUNET_ERROR_TYPE_ERROR,
1811 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1813 return GNUNET_SYSERR;
1820 * Another peer has suggested an address for this
1821 * peer and transport plugin. Check that this could be a valid
1822 * address. If so, consider adding it to the list
1825 * @param cls closure with the `struct Plugin`
1826 * @param addr pointer to the address
1827 * @param addrlen length of @a addr
1828 * @return #GNUNET_OK if this is a plausible address for this peer
1829 * and transport; always returns #GNUNET_NO (this is the client!)
1832 http_client_plugin_address_suggested (void *cls,
1836 /* A HTTP/S client does not have any valid address so:*/
1842 * Exit point from the plugin.
1844 * @param cls api as closure
1848 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1850 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1851 struct HTTP_Client_Plugin *plugin = api->cls;
1853 if (NULL == api->cls)
1859 LOG (GNUNET_ERROR_TYPE_DEBUG,
1860 _("Shutting down plugin `%s'\n"),
1862 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
1863 &destroy_session_cb,
1865 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1867 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1868 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1870 if (NULL != plugin->curl_multi_handle)
1872 curl_multi_cleanup (plugin->curl_multi_handle);
1873 plugin->curl_multi_handle = NULL;
1875 curl_global_cleanup ();
1876 LOG (GNUNET_ERROR_TYPE_DEBUG,
1877 _("Shutdown for plugin `%s' complete\n"),
1879 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
1880 GNUNET_free_non_null (plugin->proxy_hostname);
1881 GNUNET_free_non_null (plugin->proxy_username);
1882 GNUNET_free_non_null (plugin->proxy_password);
1883 GNUNET_free (plugin);
1892 * @param plugin the plugin handle
1893 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1896 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1898 unsigned long long max_connections;
1902 /* Optional parameters */
1904 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1908 max_connections = 128;
1909 plugin->max_connections = max_connections;
1911 LOG (GNUNET_ERROR_TYPE_DEBUG,
1912 _("Maximum number of connections is %u\n"),
1913 plugin->max_connections);
1915 /* Read proxy configuration */
1916 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1917 plugin->name, "PROXY", &plugin->proxy_hostname))
1919 LOG (GNUNET_ERROR_TYPE_DEBUG,
1920 "Found proxy host: `%s'\n",
1921 plugin->proxy_hostname);
1922 /* proxy username */
1924 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1927 &plugin->proxy_username))
1929 LOG (GNUNET_ERROR_TYPE_DEBUG,
1930 "Found proxy username name: `%s'\n",
1931 plugin->proxy_username);
1934 /* proxy password */
1936 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1939 &plugin->proxy_password))
1941 LOG (GNUNET_ERROR_TYPE_DEBUG,
1942 "Found proxy password name: `%s'\n",
1943 plugin->proxy_password);
1948 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1953 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
1955 if (0 == strcmp(proxy_type, "HTTP"))
1956 plugin->proxytype = CURLPROXY_HTTP;
1957 else if (0 == strcmp(proxy_type, "SOCKS4"))
1958 plugin->proxytype = CURLPROXY_SOCKS4;
1959 else if (0 == strcmp(proxy_type, "SOCKS5"))
1960 plugin->proxytype = CURLPROXY_SOCKS5;
1961 else if (0 == strcmp(proxy_type, "SOCKS4A"))
1962 plugin->proxytype = CURLPROXY_SOCKS4A;
1963 else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
1964 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
1967 LOG (GNUNET_ERROR_TYPE_ERROR,
1968 _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
1971 GNUNET_free (proxy_type);
1972 GNUNET_free (plugin->proxy_hostname);
1973 plugin->proxy_hostname = NULL;
1974 GNUNET_free_non_null (plugin->proxy_username);
1975 plugin->proxy_username = NULL;
1976 GNUNET_free_non_null (plugin->proxy_password);
1977 plugin->proxy_password = NULL;
1979 return GNUNET_SYSERR;
1982 LOG (GNUNET_ERROR_TYPE_DEBUG,
1983 "Found proxy type: `%s'\n",
1987 /* proxy http tunneling */
1988 plugin->proxy_use_httpproxytunnel
1989 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
1991 "PROXY_HTTP_TUNNELING");
1992 if (GNUNET_SYSERR == plugin->proxy_use_httpproxytunnel)
1993 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
1995 GNUNET_free_non_null (proxy_type);
2002 * Function called by the pretty printer for the resolved address for
2003 * each human-readable address obtained. The callback can be called
2004 * several times. The last invocation must be with a @a address of
2005 * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
2006 * errors, the callback might be called first with @a address NULL and
2007 * @a res being #GNUNET_SYSERR. In that case, there must still be a
2008 * subsequent call later with @a address NULL and @a res #GNUNET_OK.
2010 * @param cls closure
2011 * @param address one of the names for the host, NULL on last callback
2012 * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on failure,
2013 * #GNUNET_OK on last callback
2016 http_client_plugin_address_to_string (void *cls,
2020 return http_common_plugin_address_to_string (PLUGIN_NAME,
2027 * Function that will be called whenever the transport service wants to
2028 * notify the plugin that a session is still active and in use and
2029 * therefore the session timeout for this session has to be updated
2031 * @param cls closure
2032 * @param peer which peer was the session for
2033 * @param session which session is being updated
2036 http_client_plugin_update_session_timeout (void *cls,
2037 const struct GNUNET_PeerIdentity *peer,
2038 struct Session *session)
2040 client_reschedule_session_timeout (session);
2045 * Function that will be called whenever the transport service wants to
2046 * notify the plugin that the inbound quota changed and that the plugin
2047 * should update it's delay for the next receive value
2049 * @param cls closure
2050 * @param peer which peer was the session for
2051 * @param session which session is being updated
2052 * @param delay new delay to use for receiving
2055 http_client_plugin_update_inbound_delay (void *cls,
2056 const struct GNUNET_PeerIdentity *peer,
2058 struct GNUNET_TIME_Relative delay)
2060 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
2061 LOG (GNUNET_ERROR_TYPE_DEBUG,
2062 "New inbound delay %s\n",
2063 GNUNET_STRINGS_relative_time_to_string (delay,
2065 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
2067 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
2069 = GNUNET_SCHEDULER_add_delayed (delay,
2077 * Return information about the given session to the
2080 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2081 * @param peer peer we send information about
2082 * @param value our `struct Session` to send information about
2083 * @return #GNUNET_OK (continue to iterate)
2086 send_session_info_iter (void *cls,
2087 const struct GNUNET_PeerIdentity *peer,
2090 struct HTTP_Client_Plugin *plugin = cls;
2091 struct Session *session = value;
2093 notify_session_monitor (plugin,
2095 GNUNET_TRANSPORT_SS_UP);
2101 * Begin monitoring sessions of a plugin. There can only
2102 * be one active monitor per plugin (i.e. if there are
2103 * multiple monitors, the transport service needs to
2104 * multiplex the generated events over all of them).
2106 * @param cls closure of the plugin
2107 * @param sic callback to invoke, NULL to disable monitor;
2108 * plugin will being by iterating over all active
2109 * sessions immediately and then enter monitor mode
2110 * @param sic_cls closure for @a sic
2113 http_client_plugin_setup_monitor (void *cls,
2114 GNUNET_TRANSPORT_SessionInfoCallback sic,
2117 struct HTTP_Client_Plugin *plugin = cls;
2120 plugin->sic_cls = sic_cls;
2123 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2124 &send_session_info_iter,
2126 /* signal end of first iteration */
2127 sic (sic_cls, NULL, NULL);
2133 * Entry point for the plugin.
2136 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2138 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2139 struct GNUNET_TRANSPORT_PluginFunctions *api;
2140 struct HTTP_Client_Plugin *plugin;
2142 if (NULL == env->receive)
2144 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2145 initialze the plugin or the API */
2146 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2148 api->address_to_string = &http_client_plugin_address_to_string;
2149 api->string_to_address = &http_common_plugin_string_to_address;
2150 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2154 plugin = GNUNET_new (struct HTTP_Client_Plugin);
2156 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
2158 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2160 api->send = &http_client_plugin_send;
2161 api->disconnect_session = &http_client_plugin_session_disconnect;
2162 api->query_keepalive_factor = &http_client_query_keepalive_factor;
2163 api->disconnect_peer = &http_client_plugin_peer_disconnect;
2164 api->check_address = &http_client_plugin_address_suggested;
2165 api->get_session = &http_client_plugin_get_session;
2166 api->address_to_string = &http_client_plugin_address_to_string;
2167 api->string_to_address = &http_common_plugin_string_to_address;
2168 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2169 api->get_network = &http_client_plugin_get_network;
2170 api->update_session_timeout = &http_client_plugin_update_session_timeout;
2171 api->update_inbound_delay = &http_client_plugin_update_inbound_delay;
2172 api->setup_monitor = &http_client_plugin_setup_monitor;
2174 plugin->name = "transport-https_client";
2175 plugin->protocol = "https";
2177 plugin->name = "transport-http_client";
2178 plugin->protocol = "http";
2180 plugin->last_tag = 1;
2182 if (GNUNET_SYSERR == client_configure_plugin (plugin))
2184 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2189 if (GNUNET_SYSERR == client_start (plugin))
2191 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2197 /* end of plugin_transport_http_client.c */