2 This file is part of GNUnet
3 (C) 2002-2013 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
28 #define PLUGIN_NAME "https_client"
29 #define HTTP_STAT_STR_CONNECTIONS "# HTTPS client connections"
30 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
31 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
33 #define PLUGIN_NAME "http_client"
34 #define HTTP_STAT_STR_CONNECTIONS "# HTTP client connections"
35 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
36 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
39 #define VERBOSE_CURL GNUNET_YES
41 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
43 #define ENABLE_PUT GNUNET_YES
44 #define ENABLE_GET GNUNET_YES
47 #include "gnunet_util_lib.h"
48 #include "gnunet_protocols.h"
49 #include "gnunet_transport_plugin.h"
50 #include "plugin_transport_http_common.h"
51 #include <curl/curl.h>
56 * Encapsulation of all of the state of the plugin.
58 struct HTTP_Client_Plugin;
62 * Message to send using http
67 * next pointer for double linked list
69 struct HTTP_Message *next;
72 * previous pointer for double linked list
74 struct HTTP_Message *prev;
77 * buffer containing data to send
82 * amount of data already sent
92 * Continuation function to call once the transmission buffer
93 * has again space available. NULL if there is no
94 * continuation to call.
96 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
99 * Closure for transmit_cont.
101 void *transmit_cont_cls;
106 * Session handle for connections.
112 * A connection handle
115 struct ConnectionHandle
118 * The curl easy handle
123 * The related session
130 * Session handle for connections.
135 * To whom are we talking to (set to our identity
136 * if we are still waiting for the welcome message)
138 struct GNUNET_PeerIdentity target;
141 * Stored in a linked list.
143 struct Session *next;
146 * Stored in a linked list.
148 struct Session *prev;
151 * The URL to connect to
158 struct GNUNET_HELLO_Address *address;
161 * ATS network type in NBO
163 uint32_t ats_address_network_type;
166 * Pointer to the global plugin struct.
168 struct HTTP_Client_Plugin *plugin;
175 struct ConnectionHandle put;
176 struct ConnectionHandle get;
179 * Is the client PUT handle currently paused
184 * Is the client PUT handle disconnect in progress?
186 int put_tmp_disconnecting;
189 * Is the client PUT handle temporarily disconnected?
191 int put_tmp_disconnected;
194 * We received data to send while disconnecting, reconnect immediately
196 int put_reconnect_required;
199 * Client receive handle
204 * Outbound overhead due to HTTP connection
205 * Add to next message of this session when calling callback
210 * next pointer for double linked list
212 struct HTTP_Message *msg_head;
215 * previous pointer for double linked list
217 struct HTTP_Message *msg_tail;
220 * Message stream tokenizer for incoming data
222 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
225 * Session timeout task
227 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
230 * Session timeout task
232 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
235 * Task to wake up client receive handle when receiving is allowed again
237 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
240 * Absolute time when to receive data again
241 * Used for receive throttling
243 struct GNUNET_TIME_Absolute next_receive;
248 * Encapsulation of all of the state of the plugin.
250 struct HTTP_Client_Plugin
255 struct GNUNET_TRANSPORT_PluginEnvironment *env;
258 * Linked list head of open sessions.
260 struct Session *head;
263 * Linked list tail of open sessions.
265 struct Session *tail;
278 * Proxy configuration: hostname or ip of the proxy server
280 char *proxy_hostname;
283 * Username for the proxy server
285 char *proxy_username;
288 * Password for the proxy server
290 char *proxy_password;
293 * Type of proxy server:
295 * Valid values as supported by curl:
296 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
297 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
299 curl_proxytype proxytype;
302 * Use proxy tunneling:
303 * Tunnel all operations through a given HTTP instead of have the proxy
304 * evaluate the HTTP request
306 * Default: GNUNET_NO, GNUNET_yes experimental
308 int proxy_use_httpproxytunnel;
311 * My options to be included in the address
316 * Maximum number of sockets the plugin can use
317 * Each http inbound /outbound connections are two connections
319 unsigned int max_connections;
322 * Current number of sockets the plugin can use
323 * Each http inbound /outbound connections are two connections
325 unsigned int cur_connections;
328 * Last used unique HTTP connection tag
345 CURLM *curl_multi_handle;
350 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
355 * Increment session timeout due to activity for a session
356 * @param s the session
359 client_reschedule_session_timeout (struct Session *s);
363 * Function setting up file descriptors and scheduling task to run
365 * @param plugin plugin as closure
366 * @param now schedule task in 1ms, regardless of what curl may say
367 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
370 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
374 * Connect a HTTP put connection
376 * @param s the session to connect
377 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
380 client_connect_put (struct Session *s);
384 * Does a session s exists?
386 * @param plugin the plugin
387 * @param s desired session
388 * @return #GNUNET_YES or #GNUNET_NO
391 client_exist_session (struct HTTP_Client_Plugin *plugin,
394 struct Session * head;
396 for (head = plugin->head; head != NULL; head = head->next)
406 * @param curl the curl easy handle
407 * @param type message type
408 * @param data data to log, NOT a 0-terminated string
409 * @param size data length
410 * @param cls the closure
414 client_log (CURL *curl, curl_infotype type,
415 const char *data, size_t size, void *cls)
417 struct ConnectionHandle *ch = cls;
418 const char *ttype = "UNSPECIFIED";
420 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
428 case CURLINFO_HEADER_IN:
431 case CURLINFO_HEADER_OUT:
432 ttype = "HEADER_OUT";
435 GNUNET_assert (NULL != ch);
436 GNUNET_assert (NULL != ch->easyhandle);
437 GNUNET_assert (NULL != ch->s);
438 ch->s->overhead += size;
441 ttype = "UNSPECIFIED";
445 memcpy (text, data, size);
446 if (text[size - 1] == '\n')
451 text[size + 1] = '\0';
454 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
455 "Connection %p %s: %s", ch->easyhandle, ttype, text);
457 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
458 "Connection %p %s: %s", ch->easyhandle, ttype, text);
466 * Function that can be used by the transport service to transmit
467 * a message using the plugin. Note that in the case of a
468 * peer disconnecting, the continuation MUST be called
469 * prior to the disconnect notification itself. This function
470 * will be called with this peer's HELLO message to initiate
471 * a fresh connection to another peer.
474 * @param s which session must be used
475 * @param msgbuf the message to transmit
476 * @param msgbuf_size number of bytes in 'msgbuf'
477 * @param priority how important is the message (most plugins will
478 * ignore message priority and just FIFO)
479 * @param to how long to wait at most for the transmission (does not
480 * require plugins to discard the message after the timeout,
481 * just advisory for the desired delay; most plugins will ignore
483 * @param cont continuation to call once the message has
484 * been transmitted (or if the transport is ready
485 * for the next transmission call; or if the
486 * peer disconnected...); can be NULL
487 * @param cont_cls closure for cont
488 * @return number of bytes used (on the physical network, with overheads);
489 * -1 on hard errors (i.e. address invalid); 0 is a legal value
490 * and does NOT mean that the message was not transmitted (DV)
493 http_client_plugin_send (void *cls,
495 const char *msgbuf, size_t msgbuf_size,
496 unsigned int priority,
497 struct GNUNET_TIME_Relative to,
498 GNUNET_TRANSPORT_TransmitContinuation cont,
501 struct HTTP_Client_Plugin *plugin = cls;
502 struct HTTP_Message *msg;
505 /* lookup if session is really existing */
506 if (GNUNET_YES != client_exist_session (plugin, s))
509 return GNUNET_SYSERR;
512 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
513 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
515 msgbuf_size, GNUNET_i2s (&s->target));
517 /* create new message and schedule */
518 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
520 msg->size = msgbuf_size;
522 msg->buf = (char *) &msg[1];
523 msg->transmit_cont = cont;
524 msg->transmit_cont_cls = cont_cls;
525 memcpy (msg->buf, msgbuf, msgbuf_size);
526 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
528 GNUNET_asprintf (&stat_txt,
529 "# bytes currently in %s_client buffers",
531 GNUNET_STATISTICS_update (plugin->env->stats,
532 stat_txt, msgbuf_size, GNUNET_NO);
533 GNUNET_free (stat_txt);
535 if (GNUNET_YES == s->put_tmp_disconnecting)
537 /* PUT connection is currently getting disconnected */
538 s->put_reconnect_required = GNUNET_YES;
539 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
540 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
544 else if (GNUNET_YES == s->put_paused)
546 /* PUT connection was paused, unpause */
547 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
548 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
549 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
550 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
551 "Session %p/connection %p: unpausing connection\n",
553 s->put_paused = GNUNET_NO;
554 if (NULL != s->client_put)
555 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
557 else if (GNUNET_YES == s->put_tmp_disconnected)
559 /* PUT connection was disconnected, reconnect */
560 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
561 "Session %p: Reconnecting PUT connection\n",
563 s->put_tmp_disconnected = GNUNET_NO;
564 GNUNET_break (s->client_put == NULL);
565 if (GNUNET_SYSERR == client_connect_put (s))
567 return GNUNET_SYSERR;
571 client_schedule (s->plugin, GNUNET_YES);
579 * @param s the session to delete
582 client_delete_session (struct Session *s)
584 struct HTTP_Client_Plugin *plugin = s->plugin;
585 struct HTTP_Message *pos;
586 struct HTTP_Message *next;
588 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
590 GNUNET_SCHEDULER_cancel (s->timeout_task);
591 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
593 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
595 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
596 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
599 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
602 while (NULL != (pos = next))
605 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
606 if (pos->transmit_cont != NULL)
607 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
608 pos->size, pos->pos + s->overhead);
613 if (s->msg_tk != NULL)
615 GNUNET_SERVER_mst_destroy (s->msg_tk);
618 GNUNET_HELLO_address_free (s->address);
619 GNUNET_free (s->url);
625 * Disconnect a session
627 * @param cls the `struct HTTP_Client_Plugin`
629 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
632 http_client_session_disconnect (void *cls,
635 struct HTTP_Client_Plugin *plugin = cls;
636 struct HTTP_Message *msg;
637 struct HTTP_Message *t;
641 if (GNUNET_YES != client_exist_session (plugin, s))
644 return GNUNET_SYSERR;
647 if (NULL != s->client_put)
649 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
650 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
651 s, s->client_put, GNUNET_i2s (&s->target));
653 /* remove curl handle from multi handle */
654 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
655 if (mret != CURLM_OK)
657 /* clean up easy handle, handle is now invalid and free'd */
661 curl_easy_cleanup (s->client_put);
662 s->client_put = NULL;
666 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
668 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
669 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
672 if (NULL != s->client_get)
674 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
675 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
677 GNUNET_i2s (&s->target));
678 /* remove curl handle from multi handle */
679 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
680 if (mret != CURLM_OK)
682 /* clean up easy handle, handle is now invalid and free'd */
686 curl_easy_cleanup (s->client_get);
687 s->client_get = NULL;
694 if (NULL != msg->transmit_cont)
695 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
696 msg->size, msg->pos + s->overhead);
698 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
703 GNUNET_assert (plugin->cur_connections >= 2);
704 plugin->cur_connections -= 2;
705 GNUNET_STATISTICS_set (plugin->env->stats,
706 HTTP_STAT_STR_CONNECTIONS,
707 plugin->cur_connections,
709 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
710 "Session %p: notifying transport about ending session\n",s);
712 plugin->env->session_end (plugin->env->cls, s->address, s);
713 client_delete_session (s);
715 /* Re-schedule since handles have changed */
716 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
718 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
719 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
721 client_schedule (plugin, GNUNET_YES);
728 * Function that is called to get the keepalive factor.
729 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
730 * calculate the interval between keepalive packets.
732 * @param cls closure with the `struct Plugin`
733 * @return keepalive factor
736 http_client_query_keepalive_factor (void *cls)
743 * Function that can be used to force the plugin to disconnect
744 * from the given peer and cancel all previous transmissions
745 * (and their continuationc).
748 * @param target peer from which to disconnect
751 http_client_peer_disconnect (void *cls,
752 const struct GNUNET_PeerIdentity *target)
754 struct HTTP_Client_Plugin *plugin = cls;
755 struct Session *next = NULL;
756 struct Session *pos = NULL;
758 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
759 "Transport tells me to disconnect `%s'\n",
760 GNUNET_i2s (target));
763 while (NULL != (pos = next))
766 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
768 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
769 "Disconnecting session %p to `%pos'\n",
770 pos, GNUNET_i2s (target));
771 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (plugin,
779 * Check if a sessions exists for an specific address
781 * @param plugin the plugin
782 * @param address the address
783 * @return the session or NULL
785 static struct Session *
786 client_lookup_session (struct HTTP_Client_Plugin *plugin,
787 const struct GNUNET_HELLO_Address *address)
791 for (pos = plugin->head; NULL != pos; pos = pos->next)
793 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
794 (0 == GNUNET_HELLO_address_cmp(address, pos->address)))
802 client_put_disconnect (void *cls,
803 const struct GNUNET_SCHEDULER_TaskContext *tc)
805 struct Session *s = cls;
807 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
808 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
809 "Session %p/connection %p: will be disconnected due to no activity\n",
811 s->put_paused = GNUNET_NO;
812 s->put_tmp_disconnecting = GNUNET_YES;
813 if (NULL != s->client_put)
814 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
815 client_schedule (s->plugin, GNUNET_YES);
820 * Callback method used with libcurl
821 * Method is called when libcurl needs to read data during sending
823 * @param stream pointer where to write data
824 * @param size size of an individual element
825 * @param nmemb count of elements that can be written to the buffer
826 * @param cls our `struct Session`
827 * @return bytes written to stream, returning 0 will terminate connection!
830 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
832 struct Session *s = cls;
833 struct HTTP_Client_Plugin *plugin = s->plugin;
834 struct HTTP_Message *msg = s->msg_head;
838 if (GNUNET_YES != client_exist_session (plugin, s))
843 if (GNUNET_YES == s->put_tmp_disconnecting)
846 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
847 "Session %p/connection %p: disconnect due to inactivity\n",
854 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
855 "Session %p/connection %p: nothing to send, suspending\n",
857 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
858 s->put_paused = GNUNET_YES;
859 return CURL_READFUNC_PAUSE;
862 GNUNET_assert (msg->pos < msg->size);
863 /* calculate how much fits in buffer */
864 len = GNUNET_MIN (msg->size - msg->pos,
866 memcpy (stream, &msg->buf[msg->pos], len);
868 if (msg->pos == msg->size)
870 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
871 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
872 s, s->client_put, msg->size, msg->pos);
873 /* Calling transmit continuation */
874 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
875 if (NULL != msg->transmit_cont)
876 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
877 msg->size, msg->size + s->overhead);
882 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
883 GNUNET_STATISTICS_update (plugin->env->stats,
884 stat_txt, -len, GNUNET_NO);
885 GNUNET_free (stat_txt);
887 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
888 GNUNET_STATISTICS_update (plugin->env->stats,
889 stat_txt, len, GNUNET_NO);
890 GNUNET_free (stat_txt);
896 * Wake up a curl handle which was suspended
898 * @param cls the session
899 * @param tc task context
902 client_wake_up (void *cls,
903 const struct GNUNET_SCHEDULER_TaskContext *tc)
905 struct Session *s = cls;
906 struct HTTP_Client_Plugin *p = s->plugin;
908 if (GNUNET_YES != client_exist_session (p, s))
913 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
914 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
916 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
917 "Session %p/connection %p: Waking up GET handle\n",
920 s->put_paused = GNUNET_NO;
921 if (NULL != s->client_get)
922 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
928 * Callback for message stream tokenizer
930 * @param cls the session
931 * @param client not used
932 * @param message the message received
933 * @return always #GNUNET_OK
936 client_receive_mst_cb (void *cls, void *client,
937 const struct GNUNET_MessageHeader *message)
939 struct Session *s = cls;
940 struct HTTP_Client_Plugin *plugin;
941 struct GNUNET_TIME_Relative delay;
942 struct GNUNET_ATS_Information atsi;
946 if (GNUNET_YES != client_exist_session (plugin, s))
952 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
953 atsi.value = s->ats_address_network_type;
954 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
956 delay = s->plugin->env->receive (plugin->env->cls, s->address, s, message);
957 plugin->env->update_address_metrics (plugin->env->cls,
961 GNUNET_asprintf (&stat_txt,
962 "# bytes received via %s_client",
964 GNUNET_STATISTICS_update (plugin->env->stats,
965 stat_txt, ntohs(message->size), GNUNET_NO);
966 GNUNET_free (stat_txt);
969 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
971 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
973 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
974 "Client: peer `%s' address `%s' next read delayed for %s\n",
975 GNUNET_i2s (&s->target),
976 http_common_plugin_address_to_string (NULL,
977 s->plugin->protocol, s->address->address,
978 s->address->address_length),
979 GNUNET_STRINGS_relative_time_to_string (delay,
982 client_reschedule_session_timeout (s);
988 * Callback method used with libcurl when data for a PUT connection are
989 * received. We do not expect data here, so we just dismiss it
991 * @param stream pointer where to write data
992 * @param size size of an individual element
993 * @param nmemb count of elements that can be written to the buffer
994 * @param cls destination pointer, passed to the libcurl handle
995 * @return bytes read from stream
998 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
1000 return size * nmemb;
1005 * Callback method used with libcurl when data for a GET connection are
1006 * received. Forward to MST
1008 * @param stream pointer where to write data
1009 * @param size size of an individual element
1010 * @param nmemb count of elements that can be written to the buffer
1011 * @param cls destination pointer, passed to the libcurl handle
1012 * @return bytes read from stream
1015 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
1017 struct Session *s = cls;
1018 struct GNUNET_TIME_Absolute now;
1019 size_t len = size * nmemb;
1021 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1022 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
1024 len, GNUNET_i2s (&s->target));
1025 now = GNUNET_TIME_absolute_get ();
1026 if (now.abs_value_us < s->next_receive.abs_value_us)
1028 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1029 struct GNUNET_TIME_Relative delta =
1030 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1031 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1032 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1034 GNUNET_STRINGS_relative_time_to_string (delta,
1036 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1038 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1039 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1041 s->recv_wakeup_task =
1042 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1043 return CURL_WRITEFUNC_PAUSE;
1045 if (NULL == s->msg_tk)
1046 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1047 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1053 * Task performing curl operations
1055 * @param cls plugin as closure
1056 * @param tc gnunet scheduler task context
1059 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1063 * Function setting up file descriptors and scheduling task to run
1065 * @param plugin the plugin as closure
1066 * @param now schedule task in 1ms, regardless of what curl may say
1067 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1070 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1076 struct GNUNET_NETWORK_FDSet *grs;
1077 struct GNUNET_NETWORK_FDSet *gws;
1080 struct GNUNET_TIME_Relative timeout;
1082 /* Cancel previous scheduled task */
1083 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1085 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1086 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1092 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1093 if (mret != CURLM_OK)
1095 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1096 "curl_multi_fdset", __FILE__, __LINE__,
1097 curl_multi_strerror (mret));
1098 return GNUNET_SYSERR;
1100 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1102 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1104 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1105 if (now == GNUNET_YES)
1106 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1108 if (mret != CURLM_OK)
1110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1111 "curl_multi_timeout", __FILE__, __LINE__,
1112 curl_multi_strerror (mret));
1113 return GNUNET_SYSERR;
1116 grs = GNUNET_NETWORK_fdset_create ();
1117 gws = GNUNET_NETWORK_fdset_create ();
1118 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1119 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1121 plugin->client_perform_task =
1122 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1124 &client_run, plugin);
1125 GNUNET_NETWORK_fdset_destroy (gws);
1126 GNUNET_NETWORK_fdset_destroy (grs);
1132 * Task performing curl operations
1134 * @param cls plugin as closure
1135 * @param tc gnunet scheduler task context
1138 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1140 struct HTTP_Client_Plugin *plugin = cls;
1142 long http_statuscode;
1145 GNUNET_assert (cls != NULL);
1147 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1148 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1154 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1159 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1161 CURL *easy_h = msg->easy_handle;
1162 struct Session *s = NULL;
1163 char *d = (char *) s;
1168 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1169 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1171 curl_easy_strerror (msg->data.result), running);
1175 GNUNET_assert (CURLE_OK ==
1176 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1177 s = (struct Session *) d;
1179 if (GNUNET_YES != client_exist_session(plugin, s))
1185 GNUNET_assert (s != NULL);
1186 if (msg->msg == CURLMSG_DONE)
1188 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1189 if (easy_h == s->client_put)
1191 if ((0 != msg->data.result) || (http_statuscode != 200))
1193 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1194 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1195 s, msg->easy_handle, GNUNET_i2s (&s->target),
1198 curl_easy_strerror (msg->data.result));
1201 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1202 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1203 s, msg->easy_handle, GNUNET_i2s (&s->target));
1204 if (s->client_get == NULL)
1206 /* Disconnect other transmission direction and tell transport */
1208 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1209 curl_easy_cleanup (easy_h);
1210 s->put_tmp_disconnecting = GNUNET_NO;
1211 s->put_tmp_disconnected = GNUNET_YES;
1212 s->client_put = NULL;
1213 s->put.easyhandle = NULL;
1217 * Handling a rare case:
1218 * plugin_send was called during temporary put disconnect,
1219 * reconnect required after connection was disconnected
1221 if (GNUNET_YES == s->put_reconnect_required)
1223 s->put_reconnect_required = GNUNET_NO;
1224 if (GNUNET_SYSERR == client_connect_put(s))
1226 GNUNET_break (s->client_put == NULL);
1227 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1231 if (easy_h == s->client_get)
1233 if ((0 != msg->data.result) || (http_statuscode != 200))
1235 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1236 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1237 s, msg->easy_handle, GNUNET_i2s (&s->target),
1240 curl_easy_strerror (msg->data.result));
1244 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1245 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1246 s, msg->easy_handle, GNUNET_i2s (&s->target));
1247 /* Disconnect other transmission direction and tell transport */
1248 s->get.easyhandle = NULL;
1250 http_client_session_disconnect (plugin, s);
1255 while (mret == CURLM_CALL_MULTI_PERFORM);
1256 client_schedule (plugin, GNUNET_NO);
1261 * Connect GET connection for a session
1263 * @param s the session to connect
1264 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1267 client_connect_get (struct Session *s)
1271 /* create get connection */
1272 s->client_get = curl_easy_init ();
1274 s->get.easyhandle = s->client_get;
1276 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1277 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1278 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1281 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1283 struct HttpAddress *ha;
1284 ha = (struct HttpAddress *) s->address->address;
1286 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1287 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1289 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1290 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1294 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1295 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1298 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1299 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1301 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1302 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1305 if (s->plugin->proxy_hostname != NULL)
1307 curl_easy_setopt (s->client_get, CURLOPT_PROXY, s->plugin->proxy_hostname);
1308 curl_easy_setopt (s->client_get, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1309 if (NULL != s->plugin->proxy_username)
1310 curl_easy_setopt (s->client_get, CURLOPT_PROXYUSERNAME,
1311 s->plugin->proxy_username);
1312 if (NULL != s->plugin->proxy_password)
1313 curl_easy_setopt (s->client_get, CURLOPT_PROXYPASSWORD,
1314 s->plugin->proxy_password);
1315 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1316 curl_easy_setopt (s->client_get, CURLOPT_HTTPPROXYTUNNEL,
1317 s->plugin->proxy_use_httpproxytunnel);
1320 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1321 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1322 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1323 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1324 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1325 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1326 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1327 /* No timeout by default, timeout done with session timeout */
1328 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1329 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1330 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1331 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1332 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1333 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1334 #if CURL_TCP_NODELAY
1335 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1337 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1339 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1340 if (mret != CURLM_OK)
1342 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1343 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1345 curl_multi_strerror (mret));
1346 curl_easy_cleanup (s->client_get);
1347 s->client_get = NULL;
1349 s->get.easyhandle = NULL;
1351 return GNUNET_SYSERR;
1359 * Connect a HTTP put connection
1361 * @param s the session to connect
1362 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1365 client_connect_put (struct Session *s)
1369 /* create put connection */
1370 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1371 "Session %p : Init PUT handle\n", s);
1372 s->client_put = curl_easy_init ();
1374 s->put.easyhandle = s->client_put;
1376 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1377 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1378 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1381 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1383 struct HttpAddress *ha;
1384 ha = (struct HttpAddress *) s->address->address;
1386 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1387 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1389 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1390 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1394 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1395 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1398 curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1399 curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1401 curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1402 curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1404 if (s->plugin->proxy_hostname != NULL)
1406 curl_easy_setopt (s->client_put, CURLOPT_PROXY, s->plugin->proxy_hostname);
1407 curl_easy_setopt (s->client_put, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1408 if (NULL != s->plugin->proxy_username)
1409 curl_easy_setopt (s->client_put, CURLOPT_PROXYUSERNAME,
1410 s->plugin->proxy_username);
1411 if (NULL != s->plugin->proxy_password)
1412 curl_easy_setopt (s->client_put, CURLOPT_PROXYPASSWORD,
1413 s->plugin->proxy_password);
1414 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1415 curl_easy_setopt (s->client_put, CURLOPT_HTTPPROXYTUNNEL,
1416 s->plugin->proxy_use_httpproxytunnel);
1419 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1420 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1421 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1422 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1423 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1424 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1425 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1426 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1427 /* No timeout by default, timeout done with session timeout */
1428 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1429 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1430 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1431 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1432 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1433 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1434 #if CURL_TCP_NODELAY
1435 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1437 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1438 if (mret != CURLM_OK)
1440 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1441 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1443 curl_multi_strerror (mret));
1444 curl_easy_cleanup (s->client_put);
1445 s->client_put = NULL;
1446 s->put.easyhandle = NULL;
1448 s->put_tmp_disconnected = GNUNET_YES;
1449 return GNUNET_SYSERR;
1451 s->put_tmp_disconnected = GNUNET_NO;
1457 * Connect both PUT and GET connection for a session
1459 * @param s the session to connect
1460 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1463 client_connect (struct Session *s)
1466 struct HTTP_Client_Plugin *plugin = s->plugin;
1467 int res = GNUNET_OK;
1470 if (NULL == http_common_plugin_address_to_string (NULL,
1471 plugin->protocol, s->address->address, s->address->address_length))
1473 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1475 "Invalid address peer `%s'\n",
1476 GNUNET_i2s (&s->target));
1477 return GNUNET_SYSERR;
1480 GNUNET_asprintf (&s->url, "%s/%s;%u",
1481 http_common_plugin_address_to_url (NULL, s->address->address,
1482 s->address->address_length),
1483 GNUNET_i2s_full (plugin->env->my_identity),
1488 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1489 "Initiating outbound session peer `%s' using address `%s'\n",
1490 GNUNET_i2s (&s->target), s->url);
1492 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1493 (GNUNET_SYSERR == client_connect_put (s)))
1495 plugin->env->session_end (plugin->env->cls, s->address, s);
1496 client_delete_session (s);
1497 return GNUNET_SYSERR;
1500 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1502 "Session %p: connected with connections GET %p and PUT %p\n",
1503 s, s->client_get, s->client_put);
1505 /* Perform connect */
1506 plugin->cur_connections += 2;
1507 GNUNET_STATISTICS_set (plugin->env->stats,
1508 HTTP_STAT_STR_CONNECTIONS,
1509 plugin->cur_connections,
1512 /* Re-schedule since handles have changed */
1513 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1515 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1516 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1518 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1524 * Function obtain the network type for a session
1526 * @param cls closure (`struct Plugin*`)
1527 * @param session the session
1528 * @return the network type
1530 static enum GNUNET_ATS_Network_Type
1531 http_client_get_network (void *cls,
1532 struct Session *session)
1534 return ntohl (session->ats_address_network_type);
1539 * Session was idle, so disconnect it
1541 * @param cls the `struct Session` of the idle session
1542 * @param tc scheduler context
1545 client_session_timeout (void *cls,
1546 const struct GNUNET_SCHEDULER_TaskContext *tc)
1548 struct Session *s = cls;
1550 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1551 GNUNET_log (TIMEOUT_LOG,
1552 "Session %p was idle for %s, disconnecting\n",
1554 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1557 /* call session destroy function */
1558 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (s->plugin,
1564 * Creates a new outbound session the transport service will use to
1565 * send data to the peer
1567 * @param cls the plugin
1568 * @param address the address
1569 * @return the session or NULL of max connections exceeded
1571 static struct Session *
1572 http_client_plugin_get_session (void *cls,
1573 const struct GNUNET_HELLO_Address *address)
1575 struct HTTP_Client_Plugin *plugin = cls;
1576 struct Session * s = NULL;
1577 struct sockaddr *sa;
1578 struct GNUNET_ATS_Information ats;
1582 GNUNET_assert (address->address != NULL);
1584 /* find existing session */
1585 s = client_lookup_session (plugin, address);
1589 if (plugin->max_connections <= plugin->cur_connections)
1591 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1592 "Maximum number of connections (%u) reached: "
1593 "cannot connect to peer `%s'\n",
1594 plugin->max_connections,
1595 GNUNET_i2s (&address->peer));
1599 /* Determine network location */
1600 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1601 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1602 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1603 if (GNUNET_SYSERR == res)
1607 else if (GNUNET_YES == res)
1609 GNUNET_assert (NULL != sa);
1610 if (AF_INET == sa->sa_family)
1612 salen = sizeof (struct sockaddr_in);
1614 else if (AF_INET6 == sa->sa_family)
1616 salen = sizeof (struct sockaddr_in6);
1618 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1619 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1622 else if (GNUNET_NO == res)
1624 /* Cannot convert to sockaddr -> is external hostname */
1625 ats.value = htonl (GNUNET_ATS_NET_WAN);
1627 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1633 s = GNUNET_new (struct Session);
1634 s->target = address->peer;
1636 s->address = GNUNET_HELLO_address_copy (address);
1637 s->ats_address_network_type = ats.value;
1638 s->put_paused = GNUNET_NO;
1639 s->put_tmp_disconnecting = GNUNET_NO;
1640 s->put_tmp_disconnected = GNUNET_NO;
1641 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1642 &client_session_timeout,
1644 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1645 "Created new session %p for `%s' address `%s''\n",
1646 s, http_common_plugin_address_to_string (NULL,
1647 plugin->protocol, s->address->address,
1648 s->address->address_length),
1649 GNUNET_i2s (&s->target));
1651 /* add new session */
1652 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1654 /* initiate new connection */
1655 if (GNUNET_SYSERR == client_connect (s))
1657 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1658 "Cannot connect to peer `%s' address `%s''\n",
1659 http_common_plugin_address_to_string (NULL,
1660 plugin->protocol, s->address->address,
1661 s->address->address_length),
1662 GNUNET_i2s (&s->target));
1663 client_delete_session (s);
1671 * Setup http_client plugin
1673 * @param plugin the plugin handle
1674 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1677 client_start (struct HTTP_Client_Plugin *plugin)
1679 curl_global_init (CURL_GLOBAL_ALL);
1680 plugin->curl_multi_handle = curl_multi_init ();
1682 if (NULL == plugin->curl_multi_handle)
1684 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1685 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1687 return GNUNET_SYSERR;
1694 * Increment session timeout due to activity for session s
1696 * param s the session
1699 client_reschedule_session_timeout (struct Session *s)
1701 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1702 GNUNET_SCHEDULER_cancel (s->timeout_task);
1703 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1704 &client_session_timeout,
1706 GNUNET_log (TIMEOUT_LOG,
1707 "Timeout rescheduled for session %p set to %s\n",
1709 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1715 * Another peer has suggested an address for this
1716 * peer and transport plugin. Check that this could be a valid
1717 * address. If so, consider adding it to the list
1720 * @param cls closure with the `struct Plugin`
1721 * @param addr pointer to the address
1722 * @param addrlen length of @a addr
1723 * @return #GNUNET_OK if this is a plausible address for this peer
1724 * and transport; always returns #GNUNET_NO (this is the client!)
1727 http_client_plugin_address_suggested (void *cls,
1731 /* struct Plugin *plugin = cls; */
1733 /* A HTTP/S client does not have any valid address so:*/
1739 * Exit point from the plugin.
1741 * @param cls api as closure
1745 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1747 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1748 struct HTTP_Client_Plugin *plugin = api->cls;
1749 struct Session *pos;
1750 struct Session *next;
1752 if (NULL == api->cls)
1759 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1760 _("Shutting down plugin `%s'\n"),
1764 next = plugin->head;
1765 while (NULL != (pos = next))
1768 http_client_session_disconnect (plugin, pos);
1770 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1772 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1773 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1777 if (NULL != plugin->curl_multi_handle)
1779 curl_multi_cleanup (plugin->curl_multi_handle);
1780 plugin->curl_multi_handle = NULL;
1782 curl_global_cleanup ();
1784 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1785 _("Shutdown for plugin `%s' complete\n"),
1788 GNUNET_free_non_null (plugin->proxy_hostname);
1789 GNUNET_free_non_null (plugin->proxy_username);
1790 GNUNET_free_non_null (plugin->proxy_password);
1792 GNUNET_free (plugin);
1801 * @param plugin the plugin handle
1802 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1805 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1807 unsigned long long max_connections;
1811 /* Optional parameters */
1812 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1814 "MAX_CONNECTIONS", &max_connections))
1815 max_connections = 128;
1816 plugin->max_connections = max_connections;
1818 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1819 _("Maximum number of connections is %u\n"),
1820 plugin->max_connections);
1822 /* Read proxy configuration */
1823 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1824 plugin->name, "PROXY", &plugin->proxy_hostname))
1826 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1827 "Found proxy host: `%s'\n",
1828 plugin->proxy_hostname);
1829 /* proxy username */
1830 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1831 plugin->name, "PROXY_USERNAME", &plugin->proxy_username))
1833 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1834 "Found proxy username name: `%s'\n",
1835 plugin->proxy_username);
1838 /* proxy password */
1839 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1840 plugin->name, "PROXY_PASSWORD", &plugin->proxy_password))
1842 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1843 "Found proxy password name: `%s'\n",
1844 plugin->proxy_password);
1848 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1849 plugin->name, "PROXY_TYPE", &proxy_type))
1851 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
1853 if (0 == strcmp(proxy_type, "HTTP"))
1854 plugin->proxytype = CURLPROXY_HTTP;
1855 else if (0 == strcmp(proxy_type, "SOCKS4"))
1856 plugin->proxytype = CURLPROXY_SOCKS4;
1857 else if (0 == strcmp(proxy_type, "SOCKS5"))
1858 plugin->proxytype = CURLPROXY_SOCKS5;
1859 else if (0 == strcmp(proxy_type, "SOCKS4A"))
1860 plugin->proxytype = CURLPROXY_SOCKS4A;
1861 else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
1862 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
1865 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1866 _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
1869 GNUNET_free (proxy_type);
1870 GNUNET_free (plugin->proxy_hostname);
1871 plugin->proxy_hostname = NULL;
1872 GNUNET_free_non_null (plugin->proxy_username);
1873 plugin->proxy_username = NULL;
1874 GNUNET_free_non_null (plugin->proxy_password);
1875 plugin->proxy_password = NULL;
1877 return GNUNET_SYSERR;
1880 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1881 "Found proxy type: `%s'\n", proxy_type);
1884 /* proxy http tunneling */
1885 if (GNUNET_SYSERR == (plugin->proxy_use_httpproxytunnel == GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
1886 plugin->name, "PROXY_HTTP_TUNNELING")))
1887 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
1889 GNUNET_free_non_null (proxy_type);
1898 http_plugin_address_to_string (void *cls,
1902 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1907 http_client_plugin_update_session_timeout (void *cls,
1908 const struct GNUNET_PeerIdentity *peer,
1909 struct Session *session)
1911 struct HTTP_Client_Plugin *plugin = cls;
1913 /* lookup if session is really existing */
1914 if (GNUNET_YES != client_exist_session (plugin, session))
1919 client_reschedule_session_timeout (session);
1923 * Entry point for the plugin.
1926 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1928 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1929 struct GNUNET_TRANSPORT_PluginFunctions *api;
1930 struct HTTP_Client_Plugin *plugin;
1932 if (NULL == env->receive)
1934 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1935 initialze the plugin or the API */
1936 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1938 api->address_to_string = &http_plugin_address_to_string;
1939 api->string_to_address = &http_common_plugin_string_to_address;
1940 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1944 plugin = GNUNET_new (struct HTTP_Client_Plugin);
1946 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1948 api->send = &http_client_plugin_send;
1949 api->disconnect_session = &http_client_session_disconnect;
1950 api->query_keepalive_factor = &http_client_query_keepalive_factor;
1951 api->disconnect_peer = &http_client_peer_disconnect;
1952 api->check_address = &http_client_plugin_address_suggested;
1953 api->get_session = &http_client_plugin_get_session;
1954 api->address_to_string = &http_plugin_address_to_string;
1955 api->string_to_address = &http_common_plugin_string_to_address;
1956 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1957 api->get_network = &http_client_get_network;
1958 api->update_session_timeout = &http_client_plugin_update_session_timeout;
1961 plugin->name = "transport-https_client";
1962 plugin->protocol = "https";
1964 plugin->name = "transport-http_client";
1965 plugin->protocol = "http";
1967 plugin->last_tag = 1;
1968 plugin->options = 0; /* Setup options */
1970 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1972 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1977 if (GNUNET_SYSERR == client_start (plugin))
1979 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1985 /* end of plugin_transport_http_client.c */