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;
164 * Handle for the HTTP PUT request.
166 struct ConnectionHandle put;
169 * Handle for the HTTP GET request.
171 struct ConnectionHandle get;
174 * Client receive handle
179 * next pointer for double linked list
181 struct HTTP_Message *msg_head;
184 * previous pointer for double linked list
186 struct HTTP_Message *msg_tail;
189 * Message stream tokenizer for incoming data
191 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
194 * Session timeout task
196 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
199 * Session timeout task
201 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
204 * Task to wake up client receive handle when receiving is allowed again
206 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
209 * Absolute time when to receive data again.
210 * Used for receive throttling.
212 struct GNUNET_TIME_Absolute next_receive;
215 * When does this session time out.
217 struct GNUNET_TIME_Absolute timeout;
220 * Number of bytes waiting for transmission to this peer.
222 unsigned long long bytes_in_queue;
225 * Outbound overhead due to HTTP connection
226 * Add to next message of this session when calling callback
231 * Number of messages waiting for transmission to this peer.
233 unsigned int msgs_in_queue;
236 * ATS network type in NBO
238 uint32_t ats_address_network_type;
241 * Is the client PUT handle currently paused?
246 * Is the client PUT handle disconnect in progress?
248 int put_tmp_disconnecting;
251 * Is the client PUT handle temporarily disconnected?
253 int put_tmp_disconnected;
256 * We received data to send while disconnecting, reconnect immediately
258 int put_reconnect_required;
263 * Encapsulation of all of the state of the plugin.
265 struct HTTP_Client_Plugin
270 struct GNUNET_TRANSPORT_PluginEnvironment *env;
275 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
278 * Function to call about session status changes.
280 GNUNET_TRANSPORT_SessionInfoCallback sic;
283 * Closure for @e sic.
298 * Proxy configuration: hostname or ip of the proxy server
300 char *proxy_hostname;
303 * Username for the proxy server
305 char *proxy_username;
308 * Password for the proxy server
310 char *proxy_password;
315 CURLM *curl_multi_handle;
320 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
323 * Type of proxy server:
325 * Valid values as supported by curl:
326 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
327 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
329 curl_proxytype proxytype;
332 * Use proxy tunneling:
333 * Tunnel all operations through a given HTTP instead of have the proxy
334 * evaluate the HTTP request
336 * Default: #GNUNET_NO, #GNUNET_YES experimental
338 int proxy_use_httpproxytunnel;
341 * My options to be included in the address
346 * Maximum number of sockets the plugin can use
347 * Each http inbound /outbound connections are two connections
349 unsigned int max_connections;
352 * Current number of sockets the plugin can use
353 * Each http inbound /outbound connections are two connections
355 unsigned int cur_connections;
358 * Last used unique HTTP connection tag
377 * If a session monitor is attached, notify it about the new
380 * @param plugin our plugin
381 * @param session session that changed state
382 * @param state new state of the session
385 notify_session_monitor (struct HTTP_Client_Plugin *plugin,
386 struct Session *session,
387 enum GNUNET_TRANSPORT_SessionState state)
389 struct GNUNET_TRANSPORT_SessionInfo info;
391 if (NULL == plugin->sic)
393 memset (&info, 0, sizeof (info));
395 info.is_inbound = GNUNET_SYSERR; /* hard to say */
396 info.num_msg_pending = session->msgs_in_queue;
397 info.num_bytes_pending = session->bytes_in_queue;
398 info.receive_delay = session->next_receive;
399 info.session_timeout = session->timeout;
400 info.address = session->address;
401 plugin->sic (plugin->sic_cls,
408 * Increment session timeout due to activity for session @a s.
410 * @param s the session
413 client_reschedule_session_timeout (struct Session *s)
415 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
416 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
421 * Function setting up file descriptors and scheduling task to run
423 * @param plugin plugin as closure
424 * @param now schedule task in 1ms, regardless of what curl may say
425 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
428 client_schedule (struct HTTP_Client_Plugin *plugin,
433 * Connect a HTTP put connection
435 * @param s the session to connect
436 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
439 client_connect_put (struct Session *s);
445 * @param curl the curl easy handle
446 * @param type message type
447 * @param data data to log, NOT a 0-terminated string
448 * @param size data length
449 * @param cls the closure
453 client_log (CURL *curl,
459 struct ConnectionHandle *ch = cls;
460 const char *ttype = "UNSPECIFIED";
463 if (! ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT)))
470 case CURLINFO_HEADER_IN:
473 case CURLINFO_HEADER_OUT:
474 ttype = "HEADER_OUT";
476 GNUNET_assert (NULL != ch);
477 GNUNET_assert (NULL != ch->easyhandle);
478 GNUNET_assert (NULL != ch->s);
479 ch->s->overhead += size;
482 ttype = "UNSPECIFIED";
485 memcpy (text, data, size);
486 if (text[size - 1] == '\n')
493 text[size + 1] = '\0';
495 LOG (GNUNET_ERROR_TYPE_DEBUG,
496 "Connection %p %s: %s",
505 * Function that can be used by the transport service to transmit
506 * a message using the plugin. Note that in the case of a
507 * peer disconnecting, the continuation MUST be called
508 * prior to the disconnect notification itself. This function
509 * will be called with this peer's HELLO message to initiate
510 * a fresh connection to another peer.
513 * @param s which session must be used
514 * @param msgbuf the message to transmit
515 * @param msgbuf_size number of bytes in @a msgbuf
516 * @param priority how important is the message (most plugins will
517 * ignore message priority and just FIFO)
518 * @param to how long to wait at most for the transmission (does not
519 * require plugins to discard the message after the timeout,
520 * just advisory for the desired delay; most plugins will ignore
522 * @param cont continuation to call once the message has
523 * been transmitted (or if the transport is ready
524 * for the next transmission call; or if the
525 * peer disconnected...); can be NULL
526 * @param cont_cls closure for cont
527 * @return number of bytes used (on the physical network, with overheads);
528 * -1 on hard errors (i.e. address invalid); 0 is a legal value
529 * and does NOT mean that the message was not transmitted (DV)
532 http_client_plugin_send (void *cls,
536 unsigned int priority,
537 struct GNUNET_TIME_Relative to,
538 GNUNET_TRANSPORT_TransmitContinuation cont,
541 struct HTTP_Client_Plugin *plugin = cls;
542 struct HTTP_Message *msg;
545 LOG (GNUNET_ERROR_TYPE_DEBUG,
546 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
548 msgbuf_size, GNUNET_i2s (&s->target));
550 /* create new message and schedule */
551 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
553 msg->size = msgbuf_size;
555 msg->buf = (char *) &msg[1];
556 msg->transmit_cont = cont;
557 msg->transmit_cont_cls = cont_cls;
558 memcpy (msg->buf, msgbuf, msgbuf_size);
559 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head,
563 s->bytes_in_queue += msg->size;
565 GNUNET_asprintf (&stat_txt,
566 "# bytes currently in %s_client buffers",
568 GNUNET_STATISTICS_update (plugin->env->stats,
569 stat_txt, msgbuf_size, GNUNET_NO);
570 GNUNET_free (stat_txt);
571 notify_session_monitor (plugin,
573 GNUNET_TRANSPORT_SS_UP);
574 if (GNUNET_YES == s->put_tmp_disconnecting)
576 /* PUT connection is currently getting disconnected */
577 s->put_reconnect_required = GNUNET_YES;
578 LOG (GNUNET_ERROR_TYPE_DEBUG,
579 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
584 if (GNUNET_YES == s->put_paused)
586 /* PUT connection was paused, unpause */
587 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
588 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
589 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
590 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Session %p/connection %p: unpausing connection\n",
593 s->put_paused = GNUNET_NO;
594 if (NULL != s->client_put)
595 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
597 else if (GNUNET_YES == s->put_tmp_disconnected)
599 /* PUT connection was disconnected, reconnect */
600 LOG (GNUNET_ERROR_TYPE_DEBUG,
601 "Session %p: Reconnecting PUT connection\n",
603 s->put_tmp_disconnected = GNUNET_NO;
604 GNUNET_break (s->client_put == NULL);
605 if (GNUNET_SYSERR == client_connect_put (s))
606 return GNUNET_SYSERR;
608 client_schedule (s->plugin,
615 * Delete session @a s.
617 * @param s the session to delete
620 client_delete_session (struct Session *s)
622 struct HTTP_Client_Plugin *plugin = s->plugin;
623 struct HTTP_Message *pos;
624 struct HTTP_Message *next;
626 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
628 GNUNET_SCHEDULER_cancel (s->timeout_task);
629 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
630 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
632 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
634 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
635 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
637 GNUNET_assert (GNUNET_OK ==
638 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
643 while (NULL != (pos = next))
646 GNUNET_CONTAINER_DLL_remove (s->msg_head,
649 GNUNET_assert (0 < s->msgs_in_queue);
651 GNUNET_assert (pos->size <= s->bytes_in_queue);
652 s->bytes_in_queue -= pos->size;
653 if (NULL != pos->transmit_cont)
654 pos->transmit_cont (pos->transmit_cont_cls,
658 pos->pos + s->overhead);
662 GNUNET_assert (0 == s->msgs_in_queue);
663 GNUNET_assert (0 == s->bytes_in_queue);
664 notify_session_monitor (plugin,
666 GNUNET_TRANSPORT_SS_DOWN);
667 if (NULL != s->msg_tk)
669 GNUNET_SERVER_mst_destroy (s->msg_tk);
672 GNUNET_HELLO_address_free (s->address);
673 GNUNET_free (s->url);
679 * Disconnect a session
681 * @param cls the `struct HTTP_Client_Plugin *`
683 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
686 http_client_plugin_session_disconnect (void *cls,
689 struct HTTP_Client_Plugin *plugin = cls;
690 struct HTTP_Message *msg;
691 struct HTTP_Message *t;
695 if (NULL != s->client_put)
697 LOG (GNUNET_ERROR_TYPE_DEBUG,
698 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
701 GNUNET_i2s (&s->target));
703 /* remove curl handle from multi handle */
704 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
705 if (mret != CURLM_OK)
707 /* clean up easy handle, handle is now invalid and free'd */
711 curl_easy_cleanup (s->client_put);
712 s->client_put = NULL;
716 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
718 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
719 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
722 if (NULL != s->client_get)
724 LOG (GNUNET_ERROR_TYPE_DEBUG,
725 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
727 GNUNET_i2s (&s->target));
728 /* remove curl handle from multi handle */
729 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
730 if (mret != CURLM_OK)
732 /* clean up easy handle, handle is now invalid and free'd */
736 curl_easy_cleanup (s->client_get);
737 s->client_get = NULL;
744 if (NULL != msg->transmit_cont)
745 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
746 msg->size, msg->pos + s->overhead);
748 GNUNET_CONTAINER_DLL_remove (s->msg_head,
751 GNUNET_assert (0 < s->msgs_in_queue);
753 GNUNET_assert (msg->size <= s->bytes_in_queue);
754 s->bytes_in_queue -= msg->size;
759 GNUNET_assert (plugin->cur_connections >= 2);
760 plugin->cur_connections -= 2;
761 GNUNET_STATISTICS_set (plugin->env->stats,
762 HTTP_STAT_STR_CONNECTIONS,
763 plugin->cur_connections,
765 LOG (GNUNET_ERROR_TYPE_DEBUG,
766 "Session %p: notifying transport about ending session\n",s);
768 plugin->env->session_end (plugin->env->cls, s->address, s);
769 client_delete_session (s);
771 /* Re-schedule since handles have changed */
772 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
774 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
775 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
777 client_schedule (plugin, GNUNET_YES);
784 * Function that is called to get the keepalive factor.
785 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
786 * calculate the interval between keepalive packets.
788 * @param cls closure with the `struct Plugin`
789 * @return keepalive factor
792 http_client_query_keepalive_factor (void *cls)
799 * Callback to destroys all sessions on exit.
801 * @param cls the `struct HTTP_Client_Plugin *`
802 * @param peer identity of the peer
803 * @param value the `struct Session *`
804 * @return #GNUNET_OK (continue iterating)
807 destroy_session_cb (void *cls,
808 const struct GNUNET_PeerIdentity *peer,
811 struct HTTP_Client_Plugin *plugin = cls;
812 struct Session *session = value;
814 http_client_plugin_session_disconnect (plugin, session);
820 * Function that can be used to force the plugin to disconnect
821 * from the given peer and cancel all previous transmissions
822 * (and their continuationc).
825 * @param target peer from which to disconnect
828 http_client_plugin_peer_disconnect (void *cls,
829 const struct GNUNET_PeerIdentity *target)
831 struct HTTP_Client_Plugin *plugin = cls;
833 LOG (GNUNET_ERROR_TYPE_DEBUG,
834 "Transport tells me to disconnect `%s'\n",
835 GNUNET_i2s (target));
836 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
844 * Closure for #session_lookup_client_by_address().
846 struct SessionClientCtx
849 * Address we are looking for.
851 const struct GNUNET_HELLO_Address *address;
854 * Session that was found.
861 * Locate the seession object for a given address.
863 * @param cls the `struct SessionClientCtx *`
864 * @param key peer identity
865 * @param value the `struct Session` to check
866 * @return #GNUNET_NO if found, #GNUNET_OK if not
869 session_lookup_client_by_address (void *cls,
870 const struct GNUNET_PeerIdentity *key,
873 struct SessionClientCtx *sc_ctx = cls;
874 struct Session *s = value;
876 if (0 == GNUNET_HELLO_address_cmp (sc_ctx->address,
887 * Check if a sessions exists for an specific address
889 * @param plugin the plugin
890 * @param address the address
891 * @return the session or NULL
893 static struct Session *
894 client_lookup_session (struct HTTP_Client_Plugin *plugin,
895 const struct GNUNET_HELLO_Address *address)
897 struct SessionClientCtx sc_ctx;
899 sc_ctx.address = address;
901 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
902 &session_lookup_client_by_address,
909 * When we have nothing to transmit, we pause the HTTP PUT
910 * after a while (so that gnurl stops asking). This task
911 * is the delayed task that actually pauses the PUT.
913 * @param cls the `struct Session *` with the put
914 * @param tc scheduler context
917 client_put_disconnect (void *cls,
918 const struct GNUNET_SCHEDULER_TaskContext *tc)
920 struct Session *s = cls;
922 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
923 LOG (GNUNET_ERROR_TYPE_DEBUG,
924 "Session %p/connection %p: will be disconnected due to no activity\n",
926 s->put_paused = GNUNET_NO;
927 s->put_tmp_disconnecting = GNUNET_YES;
928 if (NULL != s->client_put)
929 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
930 client_schedule (s->plugin, GNUNET_YES);
935 * Callback method used with libcurl
936 * Method is called when libcurl needs to read data during sending
938 * @param stream pointer where to write data
939 * @param size size of an individual element
940 * @param nmemb count of elements that can be written to the buffer
941 * @param cls our `struct Session`
942 * @return bytes written to stream, returning 0 will terminate connection!
945 client_send_cb (void *stream,
950 struct Session *s = cls;
951 struct HTTP_Client_Plugin *plugin = s->plugin;
952 struct HTTP_Message *msg = s->msg_head;
956 if (GNUNET_YES == s->put_tmp_disconnecting)
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Session %p/connection %p: disconnect due to inactivity\n",
967 LOG (GNUNET_ERROR_TYPE_DEBUG,
968 "Session %p/connection %p: nothing to send, suspending\n",
970 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
971 &client_put_disconnect,
973 s->put_paused = GNUNET_YES;
974 return CURL_READFUNC_PAUSE;
977 GNUNET_assert (msg->pos < msg->size);
978 /* calculate how much fits in buffer */
979 len = GNUNET_MIN (msg->size - msg->pos,
981 memcpy (stream, &msg->buf[msg->pos], len);
983 if (msg->pos == msg->size)
985 LOG (GNUNET_ERROR_TYPE_DEBUG,
986 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
987 s, s->client_put, msg->size, msg->pos);
988 /* Calling transmit continuation */
989 GNUNET_CONTAINER_DLL_remove (s->msg_head,
992 GNUNET_assert (0 < s->msgs_in_queue);
994 GNUNET_assert (msg->size <= s->bytes_in_queue);
995 s->bytes_in_queue -= msg->size;
996 if (NULL != msg->transmit_cont)
997 msg->transmit_cont (msg->transmit_cont_cls,
1001 msg->size + s->overhead);
1005 notify_session_monitor (plugin,
1007 GNUNET_TRANSPORT_SS_UP);
1008 GNUNET_asprintf (&stat_txt,
1009 "# bytes currently in %s_client buffers",
1011 GNUNET_STATISTICS_update (plugin->env->stats,
1015 GNUNET_free (stat_txt);
1016 GNUNET_asprintf (&stat_txt,
1017 "# bytes transmitted via %s_client",
1019 GNUNET_STATISTICS_update (plugin->env->stats,
1023 GNUNET_free (stat_txt);
1029 * Wake up a curl handle which was suspended
1031 * @param cls the session
1032 * @param tc task context
1035 client_wake_up (void *cls,
1036 const struct GNUNET_SCHEDULER_TaskContext *tc)
1038 struct Session *s = cls;
1040 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1041 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1043 LOG (GNUNET_ERROR_TYPE_DEBUG,
1044 "Session %p/connection %p: Waking up GET handle\n",
1047 if (GNUNET_YES == s->put_paused)
1049 /* PUT connection was paused, unpause */
1050 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
1051 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
1052 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
1053 s->put_paused = GNUNET_NO;
1054 if (NULL != s->client_put)
1055 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
1057 if (NULL != s->client_get)
1058 curl_easy_pause (s->client_get,
1064 * Callback for message stream tokenizer
1066 * @param cls the session
1067 * @param client not used
1068 * @param message the message received
1069 * @return always #GNUNET_OK
1072 client_receive_mst_cb (void *cls,
1074 const struct GNUNET_MessageHeader *message)
1076 struct Session *s = cls;
1077 struct HTTP_Client_Plugin *plugin;
1078 struct GNUNET_TIME_Relative delay;
1079 struct GNUNET_ATS_Information atsi;
1083 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1084 atsi.value = s->ats_address_network_type;
1085 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1087 delay = s->plugin->env->receive (plugin->env->cls,
1091 plugin->env->update_address_metrics (plugin->env->cls,
1095 GNUNET_asprintf (&stat_txt,
1096 "# bytes received via %s_client",
1098 GNUNET_STATISTICS_update (plugin->env->stats,
1100 ntohs (message->size),
1102 GNUNET_free (stat_txt);
1104 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1105 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
1107 LOG (GNUNET_ERROR_TYPE_DEBUG,
1108 "Client: peer `%s' address `%s' next read delayed for %s\n",
1109 GNUNET_i2s (&s->target),
1110 http_common_plugin_address_to_string (s->plugin->protocol,
1111 s->address->address,
1112 s->address->address_length),
1113 GNUNET_STRINGS_relative_time_to_string (delay,
1116 client_reschedule_session_timeout (s);
1122 * Callback method used with libcurl when data for a PUT connection are
1123 * received. We do not expect data here, so we just discard it.
1125 * @param stream pointer where to write data
1126 * @param size size of an individual element
1127 * @param nmemb count of elements that can be written to the buffer
1128 * @param cls destination pointer, passed to the libcurl handle
1129 * @return bytes read from stream
1132 client_receive_put (void *stream,
1137 return size * nmemb;
1142 * Callback method used with libcurl when data for a GET connection are
1143 * received. Forward to MST
1145 * @param stream pointer where to write data
1146 * @param size size of an individual element
1147 * @param nmemb count of elements that can be written to the buffer
1148 * @param cls destination pointer, passed to the libcurl handle
1149 * @return bytes read from stream
1152 client_receive (void *stream,
1157 struct Session *s = cls;
1158 struct GNUNET_TIME_Absolute now;
1159 size_t len = size * nmemb;
1161 LOG (GNUNET_ERROR_TYPE_DEBUG,
1162 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
1164 len, GNUNET_i2s (&s->target));
1165 now = GNUNET_TIME_absolute_get ();
1166 if (now.abs_value_us < s->next_receive.abs_value_us)
1168 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1169 struct GNUNET_TIME_Relative delta
1170 = GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1172 LOG (GNUNET_ERROR_TYPE_DEBUG,
1173 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1176 GNUNET_STRINGS_relative_time_to_string (delta,
1178 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1180 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1181 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1184 = GNUNET_SCHEDULER_add_delayed (delta,
1187 return CURL_WRITEFUNC_PAUSE;
1189 if (NULL == s->msg_tk)
1190 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb,
1192 GNUNET_SERVER_mst_receive (s->msg_tk,
1203 * Task performing curl operations
1205 * @param cls plugin as closure
1206 * @param tc gnunet scheduler task context
1209 client_run (void *cls,
1210 const struct GNUNET_SCHEDULER_TaskContext *tc);
1214 * Function setting up file descriptors and scheduling task to run
1216 * @param plugin the plugin as closure
1217 * @param now schedule task in 1ms, regardless of what curl may say
1218 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1221 client_schedule (struct HTTP_Client_Plugin *plugin,
1228 struct GNUNET_NETWORK_FDSet *grs;
1229 struct GNUNET_NETWORK_FDSet *gws;
1232 struct GNUNET_TIME_Relative timeout;
1234 /* Cancel previous scheduled task */
1235 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1237 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1238 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1244 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1245 if (mret != CURLM_OK)
1247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1248 "curl_multi_fdset", __FILE__, __LINE__,
1249 curl_multi_strerror (mret));
1250 return GNUNET_SYSERR;
1252 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1254 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1256 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1257 if (now == GNUNET_YES)
1258 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1260 if (mret != CURLM_OK)
1262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1263 _("%s failed at %s:%d: `%s'\n"),
1264 "curl_multi_timeout", __FILE__, __LINE__,
1265 curl_multi_strerror (mret));
1266 return GNUNET_SYSERR;
1269 grs = GNUNET_NETWORK_fdset_create ();
1270 gws = GNUNET_NETWORK_fdset_create ();
1271 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1272 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1274 plugin->client_perform_task =
1275 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1277 &client_run, plugin);
1278 GNUNET_NETWORK_fdset_destroy (gws);
1279 GNUNET_NETWORK_fdset_destroy (grs);
1285 * Task performing curl operations
1287 * @param cls plugin as closure
1288 * @param tc scheduler task context
1291 client_run (void *cls,
1292 const struct GNUNET_SCHEDULER_TaskContext *tc)
1294 struct HTTP_Client_Plugin *plugin = cls;
1296 long http_statuscode;
1299 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1300 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1306 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1311 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1313 CURL *easy_h = msg->easy_handle;
1314 struct Session *s = NULL;
1315 char *d = (char *) s;
1320 LOG (GNUNET_ERROR_TYPE_DEBUG,
1321 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1323 curl_easy_strerror (msg->data.result), running);
1327 GNUNET_assert (CURLE_OK ==
1328 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1329 s = (struct Session *) d;
1330 GNUNET_assert (s != NULL);
1331 if (msg->msg == CURLMSG_DONE)
1333 GNUNET_break (CURLE_OK == curl_easy_getinfo (easy_h,
1334 CURLINFO_RESPONSE_CODE, &http_statuscode));
1335 if (easy_h == s->client_put)
1337 if ((0 != msg->data.result) || (http_statuscode != 200))
1339 LOG (GNUNET_ERROR_TYPE_DEBUG,
1340 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1341 s, msg->easy_handle,
1342 GNUNET_i2s (&s->target),
1345 curl_easy_strerror (msg->data.result));
1348 LOG (GNUNET_ERROR_TYPE_DEBUG,
1349 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1350 s, msg->easy_handle,
1351 GNUNET_i2s (&s->target));
1352 if (NULL == s->client_get)
1354 /* Disconnect other transmission direction and tell transport */
1357 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1358 curl_easy_cleanup (easy_h);
1359 s->put_tmp_disconnecting = GNUNET_NO;
1360 s->put_tmp_disconnected = GNUNET_YES;
1361 s->client_put = NULL;
1362 s->put.easyhandle = NULL;
1366 * Handling a rare case:
1367 * plugin_send was called during temporary put disconnect,
1368 * reconnect required after connection was disconnected
1370 if (GNUNET_YES == s->put_reconnect_required)
1372 s->put_reconnect_required = GNUNET_NO;
1373 if (GNUNET_SYSERR == client_connect_put(s))
1375 GNUNET_break (s->client_put == NULL);
1376 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1380 if (easy_h == s->client_get)
1382 if ((0 != msg->data.result) || (http_statuscode != 200))
1384 LOG (GNUNET_ERROR_TYPE_DEBUG,
1385 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1388 GNUNET_i2s (&s->target),
1391 curl_easy_strerror (msg->data.result));
1395 LOG (GNUNET_ERROR_TYPE_DEBUG,
1396 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1399 GNUNET_i2s (&s->target));
1400 /* Disconnect other transmission direction and tell transport */
1401 s->get.easyhandle = NULL;
1403 http_client_plugin_session_disconnect (plugin, s);
1408 while (mret == CURLM_CALL_MULTI_PERFORM);
1409 client_schedule (plugin, GNUNET_NO);
1414 * Connect GET connection for a session
1416 * @param s the session to connect
1417 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1420 client_connect_get (struct Session *s)
1424 /* create get connection */
1425 s->client_get = curl_easy_init ();
1427 s->get.easyhandle = s->client_get;
1429 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1430 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1431 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1434 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1436 struct HttpAddress *ha;
1437 ha = (struct HttpAddress *) s->address->address;
1439 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1440 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1442 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1443 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1447 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1448 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1451 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1452 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1454 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1455 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1458 if (s->plugin->proxy_hostname != NULL)
1460 curl_easy_setopt (s->client_get, CURLOPT_PROXY, s->plugin->proxy_hostname);
1461 curl_easy_setopt (s->client_get, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1462 if (NULL != s->plugin->proxy_username)
1463 curl_easy_setopt (s->client_get, CURLOPT_PROXYUSERNAME,
1464 s->plugin->proxy_username);
1465 if (NULL != s->plugin->proxy_password)
1466 curl_easy_setopt (s->client_get, CURLOPT_PROXYPASSWORD,
1467 s->plugin->proxy_password);
1468 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1469 curl_easy_setopt (s->client_get, CURLOPT_HTTPPROXYTUNNEL,
1470 s->plugin->proxy_use_httpproxytunnel);
1473 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1474 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1475 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1476 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1477 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1478 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1479 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1480 /* No timeout by default, timeout done with session timeout */
1481 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1482 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1483 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1484 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1485 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1486 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1487 #if CURL_TCP_NODELAY
1488 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1490 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1492 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1493 if (mret != CURLM_OK)
1495 LOG (GNUNET_ERROR_TYPE_ERROR,
1496 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1498 curl_multi_strerror (mret));
1499 curl_easy_cleanup (s->client_get);
1500 s->client_get = NULL;
1502 s->get.easyhandle = NULL;
1504 return GNUNET_SYSERR;
1512 * Connect a HTTP put connection
1514 * @param s the session to connect
1515 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1518 client_connect_put (struct Session *s)
1522 /* create put connection */
1523 LOG (GNUNET_ERROR_TYPE_DEBUG,
1524 "Session %p: Init PUT handle\n", s);
1525 s->client_put = curl_easy_init ();
1527 s->put.easyhandle = s->client_put;
1529 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1530 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1531 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1534 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1536 struct HttpAddress *ha;
1537 ha = (struct HttpAddress *) s->address->address;
1539 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1540 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1542 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1543 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1547 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1548 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1551 curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1552 curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1554 curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1555 curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1557 if (s->plugin->proxy_hostname != NULL)
1559 curl_easy_setopt (s->client_put, CURLOPT_PROXY, s->plugin->proxy_hostname);
1560 curl_easy_setopt (s->client_put, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1561 if (NULL != s->plugin->proxy_username)
1562 curl_easy_setopt (s->client_put, CURLOPT_PROXYUSERNAME,
1563 s->plugin->proxy_username);
1564 if (NULL != s->plugin->proxy_password)
1565 curl_easy_setopt (s->client_put, CURLOPT_PROXYPASSWORD,
1566 s->plugin->proxy_password);
1567 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1568 curl_easy_setopt (s->client_put, CURLOPT_HTTPPROXYTUNNEL,
1569 s->plugin->proxy_use_httpproxytunnel);
1572 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1573 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1574 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1575 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1576 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1577 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1578 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1579 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1580 /* No timeout by default, timeout done with session timeout */
1581 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1582 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1583 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1584 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1585 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1586 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1587 #if CURL_TCP_NODELAY
1588 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1590 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1591 if (mret != CURLM_OK)
1593 LOG (GNUNET_ERROR_TYPE_ERROR,
1594 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1596 curl_multi_strerror (mret));
1597 curl_easy_cleanup (s->client_put);
1598 s->client_put = NULL;
1599 s->put.easyhandle = NULL;
1601 s->put_tmp_disconnected = GNUNET_YES;
1602 return GNUNET_SYSERR;
1604 s->put_tmp_disconnected = GNUNET_NO;
1610 * Connect both PUT and GET connection for a session
1612 * @param s the session to connect
1613 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1616 client_connect (struct Session *s)
1618 struct HTTP_Client_Plugin *plugin = s->plugin;
1619 int res = GNUNET_OK;
1622 if (NULL == http_common_plugin_address_to_string (plugin->protocol,
1623 s->address->address,
1624 s->address->address_length))
1626 LOG (GNUNET_ERROR_TYPE_DEBUG,
1627 "Invalid address peer `%s'\n",
1628 GNUNET_i2s (&s->target));
1629 return GNUNET_SYSERR;
1632 GNUNET_asprintf (&s->url, "%s/%s;%u",
1633 http_common_plugin_address_to_url (NULL,
1634 s->address->address,
1635 s->address->address_length),
1636 GNUNET_i2s_full (plugin->env->my_identity),
1640 LOG (GNUNET_ERROR_TYPE_DEBUG,
1641 "Initiating outbound session peer `%s' using address `%s'\n",
1642 GNUNET_i2s (&s->target), s->url);
1644 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1645 (GNUNET_SYSERR == client_connect_put (s)))
1647 plugin->env->session_end (plugin->env->cls, s->address, s);
1648 client_delete_session (s);
1649 return GNUNET_SYSERR;
1652 LOG (GNUNET_ERROR_TYPE_DEBUG,
1653 "Session %p: connected with connections GET %p and PUT %p\n",
1657 /* Perform connect */
1658 plugin->cur_connections += 2;
1659 GNUNET_STATISTICS_set (plugin->env->stats,
1660 HTTP_STAT_STR_CONNECTIONS,
1661 plugin->cur_connections,
1663 /* Re-schedule since handles have changed */
1664 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1666 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1667 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1669 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1675 * Function obtain the network type for a session
1677 * @param cls closure (`struct Plugin*`)
1678 * @param session the session
1679 * @return the network type
1681 static enum GNUNET_ATS_Network_Type
1682 http_client_plugin_get_network (void *cls,
1683 struct Session *session)
1685 return ntohl (session->ats_address_network_type);
1690 * Session was idle, so disconnect it
1692 * @param cls the `struct Session` of the idle session
1693 * @param tc scheduler context
1696 client_session_timeout (void *cls,
1697 const struct GNUNET_SCHEDULER_TaskContext *tc)
1699 struct Session *s = cls;
1700 struct GNUNET_TIME_Relative left;
1702 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1703 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1704 if (0 != left.rel_value_us)
1706 /* not actually our turn yet, but let's at least update
1707 the monitor, it may think we're about to die ... */
1708 notify_session_monitor (s->plugin,
1710 GNUNET_TRANSPORT_SS_UP);
1711 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1712 &client_session_timeout,
1717 "Session %p was idle for %s, disconnecting\n",
1719 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1721 GNUNET_assert (GNUNET_OK ==
1722 http_client_plugin_session_disconnect (s->plugin,
1728 * Creates a new outbound session the transport service will use to
1729 * send data to the peer
1731 * @param cls the plugin
1732 * @param address the address
1733 * @return the session or NULL of max connections exceeded
1735 static struct Session *
1736 http_client_plugin_get_session (void *cls,
1737 const struct GNUNET_HELLO_Address *address)
1739 struct HTTP_Client_Plugin *plugin = cls;
1741 struct sockaddr *sa;
1742 struct GNUNET_ATS_Information ats;
1746 GNUNET_assert (NULL != address->address);
1748 /* find existing session */
1749 s = client_lookup_session (plugin, address);
1753 if (plugin->max_connections <= plugin->cur_connections)
1755 LOG (GNUNET_ERROR_TYPE_WARNING,
1756 "Maximum number of connections (%u) reached: "
1757 "cannot connect to peer `%s'\n",
1758 plugin->max_connections,
1759 GNUNET_i2s (&address->peer));
1763 /* Determine network location */
1764 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1765 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1766 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1767 if (GNUNET_SYSERR == res)
1769 if (GNUNET_YES == res)
1771 GNUNET_assert (NULL != sa);
1772 if (AF_INET == sa->sa_family)
1774 salen = sizeof (struct sockaddr_in);
1776 else if (AF_INET6 == sa->sa_family)
1778 salen = sizeof (struct sockaddr_in6);
1780 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1783 else if (GNUNET_NO == res)
1785 /* Cannot convert to sockaddr -> is external hostname */
1786 ats.value = htonl (GNUNET_ATS_NET_WAN);
1788 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1794 s = GNUNET_new (struct Session);
1795 s->target = address->peer;
1797 s->address = GNUNET_HELLO_address_copy (address);
1798 s->ats_address_network_type = ats.value;
1799 s->put_paused = GNUNET_NO;
1800 s->put_tmp_disconnecting = GNUNET_NO;
1801 s->put_tmp_disconnected = GNUNET_NO;
1802 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT);
1803 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1804 &client_session_timeout,
1806 LOG (GNUNET_ERROR_TYPE_DEBUG,
1807 "Created new session %p for `%s' address `%s''\n",
1809 http_common_plugin_address_to_string (plugin->protocol,
1810 s->address->address,
1811 s->address->address_length),
1812 GNUNET_i2s (&s->target));
1814 /* add new session */
1815 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1818 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1819 /* initiate new connection */
1820 if (GNUNET_SYSERR == client_connect (s))
1822 LOG (GNUNET_ERROR_TYPE_ERROR,
1823 "Cannot connect to peer `%s' address `%s''\n",
1824 http_common_plugin_address_to_string (plugin->protocol,
1825 s->address->address,
1826 s->address->address_length),
1827 GNUNET_i2s (&s->target));
1828 client_delete_session (s);
1831 notify_session_monitor (plugin,
1833 GNUNET_TRANSPORT_SS_UP); /* or handshake? */
1839 * Setup http_client plugin
1841 * @param plugin the plugin handle
1842 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1845 client_start (struct HTTP_Client_Plugin *plugin)
1847 curl_global_init (CURL_GLOBAL_ALL);
1848 plugin->curl_multi_handle = curl_multi_init ();
1850 if (NULL == plugin->curl_multi_handle)
1852 LOG (GNUNET_ERROR_TYPE_ERROR,
1853 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1855 return GNUNET_SYSERR;
1862 * Another peer has suggested an address for this
1863 * peer and transport plugin. Check that this could be a valid
1864 * address. If so, consider adding it to the list
1867 * @param cls closure with the `struct Plugin`
1868 * @param addr pointer to the address
1869 * @param addrlen length of @a addr
1870 * @return #GNUNET_OK if this is a plausible address for this peer
1871 * and transport; always returns #GNUNET_NO (this is the client!)
1874 http_client_plugin_address_suggested (void *cls,
1878 /* A HTTP/S client does not have any valid address so:*/
1884 * Exit point from the plugin.
1886 * @param cls api as closure
1890 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1892 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1893 struct HTTP_Client_Plugin *plugin = api->cls;
1895 if (NULL == api->cls)
1901 LOG (GNUNET_ERROR_TYPE_DEBUG,
1902 _("Shutting down plugin `%s'\n"),
1904 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
1905 &destroy_session_cb,
1907 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1909 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1910 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1912 if (NULL != plugin->curl_multi_handle)
1914 curl_multi_cleanup (plugin->curl_multi_handle);
1915 plugin->curl_multi_handle = NULL;
1917 curl_global_cleanup ();
1918 LOG (GNUNET_ERROR_TYPE_DEBUG,
1919 _("Shutdown for plugin `%s' complete\n"),
1921 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
1922 GNUNET_free_non_null (plugin->proxy_hostname);
1923 GNUNET_free_non_null (plugin->proxy_username);
1924 GNUNET_free_non_null (plugin->proxy_password);
1925 GNUNET_free (plugin);
1934 * @param plugin the plugin handle
1935 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1938 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1940 unsigned long long max_connections;
1944 /* Optional parameters */
1946 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1950 max_connections = 128;
1951 plugin->max_connections = max_connections;
1953 LOG (GNUNET_ERROR_TYPE_DEBUG,
1954 _("Maximum number of connections is %u\n"),
1955 plugin->max_connections);
1957 /* Read proxy configuration */
1958 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1959 plugin->name, "PROXY", &plugin->proxy_hostname))
1961 LOG (GNUNET_ERROR_TYPE_DEBUG,
1962 "Found proxy host: `%s'\n",
1963 plugin->proxy_hostname);
1964 /* proxy username */
1966 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1969 &plugin->proxy_username))
1971 LOG (GNUNET_ERROR_TYPE_DEBUG,
1972 "Found proxy username name: `%s'\n",
1973 plugin->proxy_username);
1976 /* proxy password */
1978 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1981 &plugin->proxy_password))
1983 LOG (GNUNET_ERROR_TYPE_DEBUG,
1984 "Found proxy password name: `%s'\n",
1985 plugin->proxy_password);
1990 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1995 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
1997 if (0 == strcmp(proxy_type, "HTTP"))
1998 plugin->proxytype = CURLPROXY_HTTP;
1999 else if (0 == strcmp(proxy_type, "SOCKS4"))
2000 plugin->proxytype = CURLPROXY_SOCKS4;
2001 else if (0 == strcmp(proxy_type, "SOCKS5"))
2002 plugin->proxytype = CURLPROXY_SOCKS5;
2003 else if (0 == strcmp(proxy_type, "SOCKS4A"))
2004 plugin->proxytype = CURLPROXY_SOCKS4A;
2005 else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
2006 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
2009 LOG (GNUNET_ERROR_TYPE_ERROR,
2010 _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
2013 GNUNET_free (proxy_type);
2014 GNUNET_free (plugin->proxy_hostname);
2015 plugin->proxy_hostname = NULL;
2016 GNUNET_free_non_null (plugin->proxy_username);
2017 plugin->proxy_username = NULL;
2018 GNUNET_free_non_null (plugin->proxy_password);
2019 plugin->proxy_password = NULL;
2021 return GNUNET_SYSERR;
2024 LOG (GNUNET_ERROR_TYPE_DEBUG,
2025 "Found proxy type: `%s'\n",
2029 /* proxy http tunneling */
2030 plugin->proxy_use_httpproxytunnel
2031 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2033 "PROXY_HTTP_TUNNELING");
2034 if (GNUNET_SYSERR == plugin->proxy_use_httpproxytunnel)
2035 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
2037 GNUNET_free_non_null (proxy_type);
2044 * Function called by the pretty printer for the resolved address for
2045 * each human-readable address obtained. The callback can be called
2046 * several times. The last invocation must be with a @a address of
2047 * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
2048 * errors, the callback might be called first with @a address NULL and
2049 * @a res being #GNUNET_SYSERR. In that case, there must still be a
2050 * subsequent call later with @a address NULL and @a res #GNUNET_OK.
2052 * @param cls closure
2053 * @param address one of the names for the host, NULL on last callback
2054 * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on failure,
2055 * #GNUNET_OK on last callback
2058 http_client_plugin_address_to_string (void *cls,
2062 return http_common_plugin_address_to_string (PLUGIN_NAME,
2069 * Function that will be called whenever the transport service wants to
2070 * notify the plugin that a session is still active and in use and
2071 * therefore the session timeout for this session has to be updated
2073 * @param cls closure
2074 * @param peer which peer was the session for
2075 * @param session which session is being updated
2078 http_client_plugin_update_session_timeout (void *cls,
2079 const struct GNUNET_PeerIdentity *peer,
2080 struct Session *session)
2082 client_reschedule_session_timeout (session);
2087 * Function that will be called whenever the transport service wants to
2088 * notify the plugin that the inbound quota changed and that the plugin
2089 * should update it's delay for the next receive value
2091 * @param cls closure
2092 * @param peer which peer was the session for
2093 * @param session which session is being updated
2094 * @param delay new delay to use for receiving
2097 http_client_plugin_update_inbound_delay (void *cls,
2098 const struct GNUNET_PeerIdentity *peer,
2100 struct GNUNET_TIME_Relative delay)
2102 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
2103 LOG (GNUNET_ERROR_TYPE_DEBUG,
2104 "New inbound delay %s\n",
2105 GNUNET_STRINGS_relative_time_to_string (delay,
2107 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
2109 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
2111 = GNUNET_SCHEDULER_add_delayed (delay,
2119 * Return information about the given session to the
2122 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2123 * @param peer peer we send information about
2124 * @param value our `struct Session` to send information about
2125 * @return #GNUNET_OK (continue to iterate)
2128 send_session_info_iter (void *cls,
2129 const struct GNUNET_PeerIdentity *peer,
2132 struct HTTP_Client_Plugin *plugin = cls;
2133 struct Session *session = value;
2135 notify_session_monitor (plugin,
2137 GNUNET_TRANSPORT_SS_UP);
2143 * Begin monitoring sessions of a plugin. There can only
2144 * be one active monitor per plugin (i.e. if there are
2145 * multiple monitors, the transport service needs to
2146 * multiplex the generated events over all of them).
2148 * @param cls closure of the plugin
2149 * @param sic callback to invoke, NULL to disable monitor;
2150 * plugin will being by iterating over all active
2151 * sessions immediately and then enter monitor mode
2152 * @param sic_cls closure for @a sic
2155 http_client_plugin_setup_monitor (void *cls,
2156 GNUNET_TRANSPORT_SessionInfoCallback sic,
2159 struct HTTP_Client_Plugin *plugin = cls;
2162 plugin->sic_cls = sic_cls;
2165 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2166 &send_session_info_iter,
2168 /* signal end of first iteration */
2169 sic (sic_cls, NULL, NULL);
2176 * Entry point for the plugin.
2179 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2181 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2182 struct GNUNET_TRANSPORT_PluginFunctions *api;
2183 struct HTTP_Client_Plugin *plugin;
2185 if (NULL == env->receive)
2187 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2188 initialze the plugin or the API */
2189 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2191 api->address_to_string = &http_client_plugin_address_to_string;
2192 api->string_to_address = &http_common_plugin_string_to_address;
2193 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2197 plugin = GNUNET_new (struct HTTP_Client_Plugin);
2199 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
2201 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2203 api->send = &http_client_plugin_send;
2204 api->disconnect_session = &http_client_plugin_session_disconnect;
2205 api->query_keepalive_factor = &http_client_query_keepalive_factor;
2206 api->disconnect_peer = &http_client_plugin_peer_disconnect;
2207 api->check_address = &http_client_plugin_address_suggested;
2208 api->get_session = &http_client_plugin_get_session;
2209 api->address_to_string = &http_client_plugin_address_to_string;
2210 api->string_to_address = &http_common_plugin_string_to_address;
2211 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2212 api->get_network = &http_client_plugin_get_network;
2213 api->update_session_timeout = &http_client_plugin_update_session_timeout;
2214 api->update_inbound_delay = &http_client_plugin_update_inbound_delay;
2215 api->setup_monitor = &http_client_plugin_setup_monitor;
2217 plugin->name = "transport-https_client";
2218 plugin->protocol = "https";
2220 plugin->name = "transport-http_client";
2221 plugin->protocol = "http";
2223 plugin->last_tag = 1;
2225 if (GNUNET_SYSERR == client_configure_plugin (plugin))
2227 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2232 if (GNUNET_SYSERR == client_start (plugin))
2234 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2240 /* end of plugin_transport_http_client.c */