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>
54 #define LOG(kind,...) GNUNET_log_from(kind, PLUGIN_NAME, __VA_ARGS__)
57 * Encapsulation of all of the state of the plugin.
59 struct HTTP_Client_Plugin;
63 * Message to send using http
68 * next pointer for double linked list
70 struct HTTP_Message *next;
73 * previous pointer for double linked list
75 struct HTTP_Message *prev;
78 * buffer containing data to send
83 * amount of data already sent
93 * Continuation function to call once the transmission buffer
94 * has again space available. NULL if there is no
95 * continuation to call.
97 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
100 * Closure for @e transmit_cont.
102 void *transmit_cont_cls;
107 * Session handle for connections.
113 * A connection handle
116 struct ConnectionHandle
119 * The curl easy handle
124 * The related session
131 * Session handle for connections.
136 * To whom are we talking to (set to our identity
137 * if we are still waiting for the welcome message)
139 struct GNUNET_PeerIdentity target;
142 * Stored in a linked list.
144 struct Session *next;
147 * Stored in a linked list.
149 struct Session *prev;
152 * The URL to connect to
159 struct GNUNET_HELLO_Address *address;
162 * Pointer to the global plugin struct.
164 struct HTTP_Client_Plugin *plugin;
172 * Handle for the HTTP PUT request.
174 struct ConnectionHandle put;
177 * Handle for the HTTP GET request.
179 struct ConnectionHandle get;
182 * Client receive handle
186 * next pointer for double linked list
188 struct HTTP_Message *msg_head;
191 * previous pointer for double linked list
193 struct HTTP_Message *msg_tail;
196 * Message stream tokenizer for incoming data
198 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
201 * Session timeout task
203 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
206 * Session timeout task
208 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
211 * Task to wake up client receive handle when receiving is allowed again
213 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
216 * Absolute time when to receive data again
217 * Used for receive throttling
219 struct GNUNET_TIME_Absolute next_receive;
222 * Outbound overhead due to HTTP connection
223 * Add to next message of this session when calling callback
228 * ATS network type in NBO
230 uint32_t ats_address_network_type;
233 * Is the client PUT handle currently paused
238 * Is the client PUT handle disconnect in progress?
240 int put_tmp_disconnecting;
243 * Is the client PUT handle temporarily disconnected?
245 int put_tmp_disconnected;
248 * We received data to send while disconnecting, reconnect immediately
250 int put_reconnect_required;
255 * Encapsulation of all of the state of the plugin.
257 struct HTTP_Client_Plugin
262 struct GNUNET_TRANSPORT_PluginEnvironment *env;
265 * Linked list head of open sessions.
267 struct Session *head;
270 * Linked list tail of open sessions.
272 struct Session *tail;
285 * Proxy configuration: hostname or ip of the proxy server
287 char *proxy_hostname;
290 * Username for the proxy server
292 char *proxy_username;
295 * Password for the proxy server
297 char *proxy_password;
302 CURLM *curl_multi_handle;
307 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
310 * Type of proxy server:
312 * Valid values as supported by curl:
313 * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
314 * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
316 curl_proxytype proxytype;
319 * Use proxy tunneling:
320 * Tunnel all operations through a given HTTP instead of have the proxy
321 * evaluate the HTTP request
323 * Default: GNUNET_NO, GNUNET_yes experimental
325 int proxy_use_httpproxytunnel;
328 * My options to be included in the address
333 * Maximum number of sockets the plugin can use
334 * Each http inbound /outbound connections are two connections
336 unsigned int max_connections;
339 * Current number of sockets the plugin can use
340 * Each http inbound /outbound connections are two connections
342 unsigned int cur_connections;
345 * Last used unique HTTP connection tag
363 * Increment session timeout due to activity for a session
364 * @param s the session
367 client_reschedule_session_timeout (struct Session *s);
371 * Function setting up file descriptors and scheduling task to run
373 * @param plugin plugin as closure
374 * @param now schedule task in 1ms, regardless of what curl may say
375 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
378 client_schedule (struct HTTP_Client_Plugin *plugin,
383 * Connect a HTTP put connection
385 * @param s the session to connect
386 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
389 client_connect_put (struct Session *s);
393 * Does a session s exists?
395 * @param plugin the plugin
396 * @param s desired session
397 * @return #GNUNET_YES or #GNUNET_NO
400 client_exist_session (struct HTTP_Client_Plugin *plugin,
403 struct Session * head;
405 for (head = plugin->head; head != NULL; head = head->next)
415 * @param curl the curl easy handle
416 * @param type message type
417 * @param data data to log, NOT a 0-terminated string
418 * @param size data length
419 * @param cls the closure
423 client_log (CURL *curl,
429 struct ConnectionHandle *ch = cls;
430 const char *ttype = "UNSPECIFIED";
433 if (! ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT)))
440 case CURLINFO_HEADER_IN:
443 case CURLINFO_HEADER_OUT:
444 ttype = "HEADER_OUT";
446 GNUNET_assert (NULL != ch);
447 GNUNET_assert (NULL != ch->easyhandle);
448 GNUNET_assert (NULL != ch->s);
449 ch->s->overhead += size;
452 ttype = "UNSPECIFIED";
455 memcpy (text, data, size);
456 if (text[size - 1] == '\n')
463 text[size + 1] = '\0';
465 LOG (GNUNET_ERROR_TYPE_DEBUG,
466 "Connection %p %s: %s",
475 * Function that can be used by the transport service to transmit
476 * a message using the plugin. Note that in the case of a
477 * peer disconnecting, the continuation MUST be called
478 * prior to the disconnect notification itself. This function
479 * will be called with this peer's HELLO message to initiate
480 * a fresh connection to another peer.
483 * @param s which session must be used
484 * @param msgbuf the message to transmit
485 * @param msgbuf_size number of bytes in @a msgbuf
486 * @param priority how important is the message (most plugins will
487 * ignore message priority and just FIFO)
488 * @param to how long to wait at most for the transmission (does not
489 * require plugins to discard the message after the timeout,
490 * just advisory for the desired delay; most plugins will ignore
492 * @param cont continuation to call once the message has
493 * been transmitted (or if the transport is ready
494 * for the next transmission call; or if the
495 * peer disconnected...); can be NULL
496 * @param cont_cls closure for cont
497 * @return number of bytes used (on the physical network, with overheads);
498 * -1 on hard errors (i.e. address invalid); 0 is a legal value
499 * and does NOT mean that the message was not transmitted (DV)
502 http_client_plugin_send (void *cls,
506 unsigned int priority,
507 struct GNUNET_TIME_Relative to,
508 GNUNET_TRANSPORT_TransmitContinuation cont,
511 struct HTTP_Client_Plugin *plugin = cls;
512 struct HTTP_Message *msg;
515 /* lookup if session is really existing */
516 if (GNUNET_YES != client_exist_session (plugin, s))
519 return GNUNET_SYSERR;
522 LOG (GNUNET_ERROR_TYPE_DEBUG,
523 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
525 msgbuf_size, GNUNET_i2s (&s->target));
527 /* create new message and schedule */
528 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
530 msg->size = msgbuf_size;
532 msg->buf = (char *) &msg[1];
533 msg->transmit_cont = cont;
534 msg->transmit_cont_cls = cont_cls;
535 memcpy (msg->buf, msgbuf, msgbuf_size);
536 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
538 GNUNET_asprintf (&stat_txt,
539 "# bytes currently in %s_client buffers",
541 GNUNET_STATISTICS_update (plugin->env->stats,
542 stat_txt, msgbuf_size, GNUNET_NO);
543 GNUNET_free (stat_txt);
545 if (GNUNET_YES == s->put_tmp_disconnecting)
547 /* PUT connection is currently getting disconnected */
548 s->put_reconnect_required = GNUNET_YES;
549 LOG (GNUNET_ERROR_TYPE_DEBUG,
550 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
555 if (GNUNET_YES == s->put_paused)
557 /* PUT connection was paused, unpause */
558 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
559 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
560 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
561 LOG (GNUNET_ERROR_TYPE_DEBUG,
562 "Session %p/connection %p: unpausing connection\n",
564 s->put_paused = GNUNET_NO;
565 if (NULL != s->client_put)
566 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
568 else if (GNUNET_YES == s->put_tmp_disconnected)
570 /* PUT connection was disconnected, reconnect */
571 LOG (GNUNET_ERROR_TYPE_DEBUG,
572 "Session %p: Reconnecting PUT connection\n",
574 s->put_tmp_disconnected = GNUNET_NO;
575 GNUNET_break (s->client_put == NULL);
576 if (GNUNET_SYSERR == client_connect_put (s))
578 return GNUNET_SYSERR;
582 client_schedule (s->plugin, GNUNET_YES);
590 * @param s the session to delete
593 client_delete_session (struct Session *s)
595 struct HTTP_Client_Plugin *plugin = s->plugin;
596 struct HTTP_Message *pos;
597 struct HTTP_Message *next;
599 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
601 GNUNET_SCHEDULER_cancel (s->timeout_task);
602 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
604 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
606 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
607 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
610 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
613 while (NULL != (pos = next))
616 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
617 if (pos->transmit_cont != NULL)
618 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
619 pos->size, pos->pos + s->overhead);
624 if (s->msg_tk != NULL)
626 GNUNET_SERVER_mst_destroy (s->msg_tk);
629 GNUNET_HELLO_address_free (s->address);
630 GNUNET_free (s->url);
636 * Disconnect a session
638 * @param cls the `struct HTTP_Client_Plugin *`
640 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
643 http_client_session_disconnect (void *cls,
646 struct HTTP_Client_Plugin *plugin = cls;
647 struct HTTP_Message *msg;
648 struct HTTP_Message *t;
652 if (GNUNET_YES != client_exist_session (plugin, s))
655 return GNUNET_SYSERR;
658 if (NULL != s->client_put)
660 LOG (GNUNET_ERROR_TYPE_DEBUG,
661 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
662 s, s->client_put, GNUNET_i2s (&s->target));
664 /* remove curl handle from multi handle */
665 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
666 if (mret != CURLM_OK)
668 /* clean up easy handle, handle is now invalid and free'd */
672 curl_easy_cleanup (s->client_put);
673 s->client_put = NULL;
677 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
679 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
680 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
683 if (NULL != s->client_get)
685 LOG (GNUNET_ERROR_TYPE_DEBUG,
686 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
688 GNUNET_i2s (&s->target));
689 /* remove curl handle from multi handle */
690 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
691 if (mret != CURLM_OK)
693 /* clean up easy handle, handle is now invalid and free'd */
697 curl_easy_cleanup (s->client_get);
698 s->client_get = NULL;
705 if (NULL != msg->transmit_cont)
706 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
707 msg->size, msg->pos + s->overhead);
709 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
714 GNUNET_assert (plugin->cur_connections >= 2);
715 plugin->cur_connections -= 2;
716 GNUNET_STATISTICS_set (plugin->env->stats,
717 HTTP_STAT_STR_CONNECTIONS,
718 plugin->cur_connections,
720 LOG (GNUNET_ERROR_TYPE_DEBUG,
721 "Session %p: notifying transport about ending session\n",s);
723 plugin->env->session_end (plugin->env->cls, s->address, s);
724 client_delete_session (s);
726 /* Re-schedule since handles have changed */
727 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
729 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
730 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
732 client_schedule (plugin, GNUNET_YES);
739 * Function that is called to get the keepalive factor.
740 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
741 * calculate the interval between keepalive packets.
743 * @param cls closure with the `struct Plugin`
744 * @return keepalive factor
747 http_client_query_keepalive_factor (void *cls)
754 * Function that can be used to force the plugin to disconnect
755 * from the given peer and cancel all previous transmissions
756 * (and their continuationc).
759 * @param target peer from which to disconnect
762 http_client_peer_disconnect (void *cls,
763 const struct GNUNET_PeerIdentity *target)
765 struct HTTP_Client_Plugin *plugin = cls;
766 struct Session *next = NULL;
767 struct Session *pos = NULL;
769 LOG (GNUNET_ERROR_TYPE_DEBUG,
770 "Transport tells me to disconnect `%s'\n",
771 GNUNET_i2s (target));
774 while (NULL != (pos = next))
777 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
779 LOG (GNUNET_ERROR_TYPE_DEBUG,
780 "Disconnecting session %p to `%pos'\n",
781 pos, GNUNET_i2s (target));
782 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (plugin,
790 * Check if a sessions exists for an specific address
792 * @param plugin the plugin
793 * @param address the address
794 * @return the session or NULL
796 static struct Session *
797 client_lookup_session (struct HTTP_Client_Plugin *plugin,
798 const struct GNUNET_HELLO_Address *address)
802 for (pos = plugin->head; NULL != pos; pos = pos->next)
804 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
805 (0 == GNUNET_HELLO_address_cmp(address, pos->address)))
816 client_put_disconnect (void *cls,
817 const struct GNUNET_SCHEDULER_TaskContext *tc)
819 struct Session *s = cls;
821 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
822 LOG (GNUNET_ERROR_TYPE_DEBUG,
823 "Session %p/connection %p: will be disconnected due to no activity\n",
825 s->put_paused = GNUNET_NO;
826 s->put_tmp_disconnecting = GNUNET_YES;
827 if (NULL != s->client_put)
828 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
829 client_schedule (s->plugin, GNUNET_YES);
834 * Callback method used with libcurl
835 * Method is called when libcurl needs to read data during sending
837 * @param stream pointer where to write data
838 * @param size size of an individual element
839 * @param nmemb count of elements that can be written to the buffer
840 * @param cls our `struct Session`
841 * @return bytes written to stream, returning 0 will terminate connection!
844 client_send_cb (void *stream,
849 struct Session *s = cls;
850 struct HTTP_Client_Plugin *plugin = s->plugin;
851 struct HTTP_Message *msg = s->msg_head;
855 if (GNUNET_YES != client_exist_session (plugin, s))
860 if (GNUNET_YES == s->put_tmp_disconnecting)
862 LOG (GNUNET_ERROR_TYPE_DEBUG,
863 "Session %p/connection %p: disconnect due to inactivity\n",
870 LOG (GNUNET_ERROR_TYPE_DEBUG,
871 "Session %p/connection %p: nothing to send, suspending\n",
873 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
874 s->put_paused = GNUNET_YES;
875 return CURL_READFUNC_PAUSE;
878 GNUNET_assert (msg->pos < msg->size);
879 /* calculate how much fits in buffer */
880 len = GNUNET_MIN (msg->size - msg->pos,
882 memcpy (stream, &msg->buf[msg->pos], len);
884 if (msg->pos == msg->size)
886 LOG (GNUNET_ERROR_TYPE_DEBUG,
887 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
888 s, s->client_put, msg->size, msg->pos);
889 /* Calling transmit continuation */
890 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
891 if (NULL != msg->transmit_cont)
892 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
893 msg->size, msg->size + s->overhead);
898 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
899 GNUNET_STATISTICS_update (plugin->env->stats,
900 stat_txt, -len, GNUNET_NO);
901 GNUNET_free (stat_txt);
903 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
904 GNUNET_STATISTICS_update (plugin->env->stats,
905 stat_txt, len, GNUNET_NO);
906 GNUNET_free (stat_txt);
912 * Wake up a curl handle which was suspended
914 * @param cls the session
915 * @param tc task context
918 client_wake_up (void *cls,
919 const struct GNUNET_SCHEDULER_TaskContext *tc)
921 struct Session *s = cls;
922 struct HTTP_Client_Plugin *p = s->plugin;
924 if (GNUNET_YES != client_exist_session (p, s))
929 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
930 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
932 LOG (GNUNET_ERROR_TYPE_DEBUG,
933 "Session %p/connection %p: Waking up GET handle\n",
936 s->put_paused = GNUNET_NO;
937 if (NULL != s->client_get)
938 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
944 * Callback for message stream tokenizer
946 * @param cls the session
947 * @param client not used
948 * @param message the message received
949 * @return always #GNUNET_OK
952 client_receive_mst_cb (void *cls, void *client,
953 const struct GNUNET_MessageHeader *message)
955 struct Session *s = cls;
956 struct HTTP_Client_Plugin *plugin;
957 struct GNUNET_TIME_Relative delay;
958 struct GNUNET_ATS_Information atsi;
962 if (GNUNET_YES != client_exist_session (plugin, s))
968 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
969 atsi.value = s->ats_address_network_type;
970 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
972 delay = s->plugin->env->receive (plugin->env->cls, s->address, s, message);
973 plugin->env->update_address_metrics (plugin->env->cls,
977 GNUNET_asprintf (&stat_txt,
978 "# bytes received via %s_client",
980 GNUNET_STATISTICS_update (plugin->env->stats,
981 stat_txt, ntohs(message->size), GNUNET_NO);
982 GNUNET_free (stat_txt);
985 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
987 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
989 LOG (GNUNET_ERROR_TYPE_DEBUG,
990 "Client: peer `%s' address `%s' next read delayed for %s\n",
991 GNUNET_i2s (&s->target),
992 http_common_plugin_address_to_string (NULL,
993 s->plugin->protocol, s->address->address,
994 s->address->address_length),
995 GNUNET_STRINGS_relative_time_to_string (delay,
998 client_reschedule_session_timeout (s);
1004 * Callback method used with libcurl when data for a PUT connection are
1005 * received. We do not expect data here, so we just discard it.
1007 * @param stream pointer where to write data
1008 * @param size size of an individual element
1009 * @param nmemb count of elements that can be written to the buffer
1010 * @param cls destination pointer, passed to the libcurl handle
1011 * @return bytes read from stream
1014 client_receive_put (void *stream,
1019 return size * nmemb;
1024 * Callback method used with libcurl when data for a GET connection are
1025 * received. Forward to MST
1027 * @param stream pointer where to write data
1028 * @param size size of an individual element
1029 * @param nmemb count of elements that can be written to the buffer
1030 * @param cls destination pointer, passed to the libcurl handle
1031 * @return bytes read from stream
1034 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
1036 struct Session *s = cls;
1037 struct GNUNET_TIME_Absolute now;
1038 size_t len = size * nmemb;
1040 LOG (GNUNET_ERROR_TYPE_DEBUG,
1041 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
1043 len, GNUNET_i2s (&s->target));
1044 now = GNUNET_TIME_absolute_get ();
1045 if (now.abs_value_us < s->next_receive.abs_value_us)
1047 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1048 struct GNUNET_TIME_Relative delta =
1049 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG,
1051 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
1053 GNUNET_STRINGS_relative_time_to_string (delta,
1055 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1057 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1058 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1060 s->recv_wakeup_task =
1061 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1062 return CURL_WRITEFUNC_PAUSE;
1064 if (NULL == s->msg_tk)
1065 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1066 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1072 * Task performing curl operations
1074 * @param cls plugin as closure
1075 * @param tc gnunet scheduler task context
1078 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1082 * Function setting up file descriptors and scheduling task to run
1084 * @param plugin the plugin as closure
1085 * @param now schedule task in 1ms, regardless of what curl may say
1086 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1089 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1095 struct GNUNET_NETWORK_FDSet *grs;
1096 struct GNUNET_NETWORK_FDSet *gws;
1099 struct GNUNET_TIME_Relative timeout;
1101 /* Cancel previous scheduled task */
1102 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1104 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1105 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1111 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1112 if (mret != CURLM_OK)
1114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1115 "curl_multi_fdset", __FILE__, __LINE__,
1116 curl_multi_strerror (mret));
1117 return GNUNET_SYSERR;
1119 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1121 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1123 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1124 if (now == GNUNET_YES)
1125 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1127 if (mret != CURLM_OK)
1129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1130 "curl_multi_timeout", __FILE__, __LINE__,
1131 curl_multi_strerror (mret));
1132 return GNUNET_SYSERR;
1135 grs = GNUNET_NETWORK_fdset_create ();
1136 gws = GNUNET_NETWORK_fdset_create ();
1137 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1138 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1140 plugin->client_perform_task =
1141 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1143 &client_run, plugin);
1144 GNUNET_NETWORK_fdset_destroy (gws);
1145 GNUNET_NETWORK_fdset_destroy (grs);
1151 * Task performing curl operations
1153 * @param cls plugin as closure
1154 * @param tc gnunet scheduler task context
1157 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1159 struct HTTP_Client_Plugin *plugin = cls;
1161 long http_statuscode;
1164 GNUNET_assert (cls != NULL);
1166 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1167 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1173 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1178 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1180 CURL *easy_h = msg->easy_handle;
1181 struct Session *s = NULL;
1182 char *d = (char *) s;
1187 LOG (GNUNET_ERROR_TYPE_DEBUG,
1188 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1190 curl_easy_strerror (msg->data.result), running);
1194 GNUNET_assert (CURLE_OK ==
1195 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1196 s = (struct Session *) d;
1198 if (GNUNET_YES != client_exist_session(plugin, s))
1204 GNUNET_assert (s != NULL);
1205 if (msg->msg == CURLMSG_DONE)
1207 GNUNET_break (CURLE_OK == curl_easy_getinfo (easy_h,
1208 CURLINFO_RESPONSE_CODE, &http_statuscode));
1209 if (easy_h == s->client_put)
1211 if ((0 != msg->data.result) || (http_statuscode != 200))
1213 LOG (GNUNET_ERROR_TYPE_DEBUG,
1214 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1215 s, msg->easy_handle,
1216 GNUNET_i2s (&s->target),
1219 curl_easy_strerror (msg->data.result));
1222 LOG (GNUNET_ERROR_TYPE_DEBUG,
1223 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1224 s, msg->easy_handle,
1225 GNUNET_i2s (&s->target));
1226 if (NULL == s->client_get)
1228 /* Disconnect other transmission direction and tell transport */
1231 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1232 curl_easy_cleanup (easy_h);
1233 s->put_tmp_disconnecting = GNUNET_NO;
1234 s->put_tmp_disconnected = GNUNET_YES;
1235 s->client_put = NULL;
1236 s->put.easyhandle = NULL;
1240 * Handling a rare case:
1241 * plugin_send was called during temporary put disconnect,
1242 * reconnect required after connection was disconnected
1244 if (GNUNET_YES == s->put_reconnect_required)
1246 s->put_reconnect_required = GNUNET_NO;
1247 if (GNUNET_SYSERR == client_connect_put(s))
1249 GNUNET_break (s->client_put == NULL);
1250 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1254 if (easy_h == s->client_get)
1256 if ((0 != msg->data.result) || (http_statuscode != 200))
1258 LOG (GNUNET_ERROR_TYPE_DEBUG,
1259 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1262 GNUNET_i2s (&s->target),
1265 curl_easy_strerror (msg->data.result));
1269 LOG (GNUNET_ERROR_TYPE_DEBUG,
1270 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1273 GNUNET_i2s (&s->target));
1274 /* Disconnect other transmission direction and tell transport */
1275 s->get.easyhandle = NULL;
1277 http_client_session_disconnect (plugin, s);
1282 while (mret == CURLM_CALL_MULTI_PERFORM);
1283 client_schedule (plugin, GNUNET_NO);
1288 * Connect GET connection for a session
1290 * @param s the session to connect
1291 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1294 client_connect_get (struct Session *s)
1298 /* create get connection */
1299 s->client_get = curl_easy_init ();
1301 s->get.easyhandle = s->client_get;
1303 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1304 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1305 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1308 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1310 struct HttpAddress *ha;
1311 ha = (struct HttpAddress *) s->address->address;
1313 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1314 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1316 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1317 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1321 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1322 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1325 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1326 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1328 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1329 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1332 if (s->plugin->proxy_hostname != NULL)
1334 curl_easy_setopt (s->client_get, CURLOPT_PROXY, s->plugin->proxy_hostname);
1335 curl_easy_setopt (s->client_get, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1336 if (NULL != s->plugin->proxy_username)
1337 curl_easy_setopt (s->client_get, CURLOPT_PROXYUSERNAME,
1338 s->plugin->proxy_username);
1339 if (NULL != s->plugin->proxy_password)
1340 curl_easy_setopt (s->client_get, CURLOPT_PROXYPASSWORD,
1341 s->plugin->proxy_password);
1342 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1343 curl_easy_setopt (s->client_get, CURLOPT_HTTPPROXYTUNNEL,
1344 s->plugin->proxy_use_httpproxytunnel);
1347 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1348 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1349 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1350 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1351 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1352 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1353 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1354 /* No timeout by default, timeout done with session timeout */
1355 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1356 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1357 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1358 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1359 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1360 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1361 #if CURL_TCP_NODELAY
1362 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1364 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1366 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1367 if (mret != CURLM_OK)
1369 LOG (GNUNET_ERROR_TYPE_ERROR,
1370 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1372 curl_multi_strerror (mret));
1373 curl_easy_cleanup (s->client_get);
1374 s->client_get = NULL;
1376 s->get.easyhandle = NULL;
1378 return GNUNET_SYSERR;
1386 * Connect a HTTP put connection
1388 * @param s the session to connect
1389 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1392 client_connect_put (struct Session *s)
1396 /* create put connection */
1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1398 "Session %p: Init PUT handle\n", s);
1399 s->client_put = curl_easy_init ();
1401 s->put.easyhandle = s->client_put;
1403 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1404 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1405 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1408 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1410 struct HttpAddress *ha;
1411 ha = (struct HttpAddress *) s->address->address;
1413 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1414 (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1416 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1417 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1421 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1422 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1425 curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1426 curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1428 curl_easy_setopt (s->client_put, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1429 curl_easy_setopt (s->client_put, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1431 if (s->plugin->proxy_hostname != NULL)
1433 curl_easy_setopt (s->client_put, CURLOPT_PROXY, s->plugin->proxy_hostname);
1434 curl_easy_setopt (s->client_put, CURLOPT_PROXYTYPE, s->plugin->proxytype);
1435 if (NULL != s->plugin->proxy_username)
1436 curl_easy_setopt (s->client_put, CURLOPT_PROXYUSERNAME,
1437 s->plugin->proxy_username);
1438 if (NULL != s->plugin->proxy_password)
1439 curl_easy_setopt (s->client_put, CURLOPT_PROXYPASSWORD,
1440 s->plugin->proxy_password);
1441 if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
1442 curl_easy_setopt (s->client_put, CURLOPT_HTTPPROXYTUNNEL,
1443 s->plugin->proxy_use_httpproxytunnel);
1446 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1447 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1448 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1449 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1450 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1451 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1452 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1453 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1454 /* No timeout by default, timeout done with session timeout */
1455 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1456 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1457 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1458 (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
1459 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1460 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1461 #if CURL_TCP_NODELAY
1462 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1464 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1465 if (mret != CURLM_OK)
1467 LOG (GNUNET_ERROR_TYPE_ERROR,
1468 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1470 curl_multi_strerror (mret));
1471 curl_easy_cleanup (s->client_put);
1472 s->client_put = NULL;
1473 s->put.easyhandle = NULL;
1475 s->put_tmp_disconnected = GNUNET_YES;
1476 return GNUNET_SYSERR;
1478 s->put_tmp_disconnected = GNUNET_NO;
1484 * Connect both PUT and GET connection for a session
1486 * @param s the session to connect
1487 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1490 client_connect (struct Session *s)
1493 struct HTTP_Client_Plugin *plugin = s->plugin;
1494 int res = GNUNET_OK;
1497 if (NULL == http_common_plugin_address_to_string (NULL,
1498 plugin->protocol, s->address->address, s->address->address_length))
1500 LOG (GNUNET_ERROR_TYPE_DEBUG,
1501 "Invalid address peer `%s'\n",
1502 GNUNET_i2s (&s->target));
1503 return GNUNET_SYSERR;
1506 GNUNET_asprintf (&s->url, "%s/%s;%u",
1507 http_common_plugin_address_to_url (NULL, s->address->address,
1508 s->address->address_length),
1509 GNUNET_i2s_full (plugin->env->my_identity),
1513 LOG (GNUNET_ERROR_TYPE_DEBUG,
1514 "Initiating outbound session peer `%s' using address `%s'\n",
1515 GNUNET_i2s (&s->target), s->url);
1517 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1518 (GNUNET_SYSERR == client_connect_put (s)))
1520 plugin->env->session_end (plugin->env->cls, s->address, s);
1521 client_delete_session (s);
1522 return GNUNET_SYSERR;
1525 LOG (GNUNET_ERROR_TYPE_DEBUG,
1526 "Session %p: connected with connections GET %p and PUT %p\n",
1530 /* Perform connect */
1531 plugin->cur_connections += 2;
1532 GNUNET_STATISTICS_set (plugin->env->stats,
1533 HTTP_STAT_STR_CONNECTIONS,
1534 plugin->cur_connections,
1537 /* Re-schedule since handles have changed */
1538 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1540 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1541 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1543 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1549 * Function obtain the network type for a session
1551 * @param cls closure (`struct Plugin*`)
1552 * @param session the session
1553 * @return the network type
1555 static enum GNUNET_ATS_Network_Type
1556 http_client_get_network (void *cls,
1557 struct Session *session)
1559 return ntohl (session->ats_address_network_type);
1564 * Session was idle, so disconnect it
1566 * @param cls the `struct Session` of the idle session
1567 * @param tc scheduler context
1570 client_session_timeout (void *cls,
1571 const struct GNUNET_SCHEDULER_TaskContext *tc)
1573 struct Session *s = cls;
1575 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1577 "Session %p was idle for %s, disconnecting\n",
1579 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1582 /* call session destroy function */
1583 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (s->plugin,
1589 * Creates a new outbound session the transport service will use to
1590 * send data to the peer
1592 * @param cls the plugin
1593 * @param address the address
1594 * @return the session or NULL of max connections exceeded
1596 static struct Session *
1597 http_client_plugin_get_session (void *cls,
1598 const struct GNUNET_HELLO_Address *address)
1600 struct HTTP_Client_Plugin *plugin = cls;
1601 struct Session * s = NULL;
1602 struct sockaddr *sa;
1603 struct GNUNET_ATS_Information ats;
1607 GNUNET_assert (address->address != NULL);
1609 /* find existing session */
1610 s = client_lookup_session (plugin, address);
1614 if (plugin->max_connections <= plugin->cur_connections)
1616 LOG (GNUNET_ERROR_TYPE_WARNING,
1617 "Maximum number of connections (%u) reached: "
1618 "cannot connect to peer `%s'\n",
1619 plugin->max_connections,
1620 GNUNET_i2s (&address->peer));
1624 /* Determine network location */
1625 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1626 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1627 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1628 if (GNUNET_SYSERR == res)
1632 else if (GNUNET_YES == res)
1634 GNUNET_assert (NULL != sa);
1635 if (AF_INET == sa->sa_family)
1637 salen = sizeof (struct sockaddr_in);
1639 else if (AF_INET6 == sa->sa_family)
1641 salen = sizeof (struct sockaddr_in6);
1643 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1644 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1647 else if (GNUNET_NO == res)
1649 /* Cannot convert to sockaddr -> is external hostname */
1650 ats.value = htonl (GNUNET_ATS_NET_WAN);
1652 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1658 s = GNUNET_new (struct Session);
1659 s->target = address->peer;
1661 s->address = GNUNET_HELLO_address_copy (address);
1662 s->ats_address_network_type = ats.value;
1663 s->put_paused = GNUNET_NO;
1664 s->put_tmp_disconnecting = GNUNET_NO;
1665 s->put_tmp_disconnected = GNUNET_NO;
1666 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1667 &client_session_timeout,
1669 LOG (GNUNET_ERROR_TYPE_DEBUG,
1670 "Created new session %p for `%s' address `%s''\n",
1671 s, http_common_plugin_address_to_string (NULL,
1672 plugin->protocol, s->address->address,
1673 s->address->address_length),
1674 GNUNET_i2s (&s->target));
1676 /* add new session */
1677 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1679 /* initiate new connection */
1680 if (GNUNET_SYSERR == client_connect (s))
1682 LOG (GNUNET_ERROR_TYPE_ERROR,
1683 "Cannot connect to peer `%s' address `%s''\n",
1684 http_common_plugin_address_to_string (NULL,
1685 plugin->protocol, s->address->address,
1686 s->address->address_length),
1687 GNUNET_i2s (&s->target));
1688 client_delete_session (s);
1696 * Setup http_client plugin
1698 * @param plugin the plugin handle
1699 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1702 client_start (struct HTTP_Client_Plugin *plugin)
1704 curl_global_init (CURL_GLOBAL_ALL);
1705 plugin->curl_multi_handle = curl_multi_init ();
1707 if (NULL == plugin->curl_multi_handle)
1709 LOG (GNUNET_ERROR_TYPE_ERROR,
1710 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1712 return GNUNET_SYSERR;
1719 * Increment session timeout due to activity for session s
1721 * param s the session
1724 client_reschedule_session_timeout (struct Session *s)
1726 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1727 GNUNET_SCHEDULER_cancel (s->timeout_task);
1728 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1729 &client_session_timeout,
1732 "Timeout rescheduled for session %p set to %s\n",
1734 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1740 * Another peer has suggested an address for this
1741 * peer and transport plugin. Check that this could be a valid
1742 * address. If so, consider adding it to the list
1745 * @param cls closure with the `struct Plugin`
1746 * @param addr pointer to the address
1747 * @param addrlen length of @a addr
1748 * @return #GNUNET_OK if this is a plausible address for this peer
1749 * and transport; always returns #GNUNET_NO (this is the client!)
1752 http_client_plugin_address_suggested (void *cls,
1756 /* struct Plugin *plugin = cls; */
1758 /* A HTTP/S client does not have any valid address so:*/
1764 * Exit point from the plugin.
1766 * @param cls api as closure
1770 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1772 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1773 struct HTTP_Client_Plugin *plugin = api->cls;
1774 struct Session *pos;
1775 struct Session *next;
1777 if (NULL == api->cls)
1784 LOG (GNUNET_ERROR_TYPE_DEBUG,
1785 _("Shutting down plugin `%s'\n"),
1788 next = plugin->head;
1789 while (NULL != (pos = next))
1792 http_client_session_disconnect (plugin, pos);
1794 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1796 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1797 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1801 if (NULL != plugin->curl_multi_handle)
1803 curl_multi_cleanup (plugin->curl_multi_handle);
1804 plugin->curl_multi_handle = NULL;
1806 curl_global_cleanup ();
1808 LOG (GNUNET_ERROR_TYPE_DEBUG,
1809 _("Shutdown for plugin `%s' complete\n"),
1812 GNUNET_free_non_null (plugin->proxy_hostname);
1813 GNUNET_free_non_null (plugin->proxy_username);
1814 GNUNET_free_non_null (plugin->proxy_password);
1816 GNUNET_free (plugin);
1825 * @param plugin the plugin handle
1826 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1829 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1831 unsigned long long max_connections;
1835 /* Optional parameters */
1836 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1838 "MAX_CONNECTIONS", &max_connections))
1839 max_connections = 128;
1840 plugin->max_connections = max_connections;
1842 LOG (GNUNET_ERROR_TYPE_DEBUG,
1843 _("Maximum number of connections is %u\n"),
1844 plugin->max_connections);
1846 /* Read proxy configuration */
1847 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1848 plugin->name, "PROXY", &plugin->proxy_hostname))
1850 LOG (GNUNET_ERROR_TYPE_DEBUG,
1851 "Found proxy host: `%s'\n",
1852 plugin->proxy_hostname);
1853 /* proxy username */
1855 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1858 &plugin->proxy_username))
1860 LOG (GNUNET_ERROR_TYPE_DEBUG,
1861 "Found proxy username name: `%s'\n",
1862 plugin->proxy_username);
1865 /* proxy password */
1866 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1867 plugin->name, "PROXY_PASSWORD", &plugin->proxy_password))
1869 LOG (GNUNET_ERROR_TYPE_DEBUG,
1870 "Found proxy password name: `%s'\n",
1871 plugin->proxy_password);
1875 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1876 plugin->name, "PROXY_TYPE", &proxy_type))
1878 GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
1880 if (0 == strcmp(proxy_type, "HTTP"))
1881 plugin->proxytype = CURLPROXY_HTTP;
1882 else if (0 == strcmp(proxy_type, "SOCKS4"))
1883 plugin->proxytype = CURLPROXY_SOCKS4;
1884 else if (0 == strcmp(proxy_type, "SOCKS5"))
1885 plugin->proxytype = CURLPROXY_SOCKS5;
1886 else if (0 == strcmp(proxy_type, "SOCKS4A"))
1887 plugin->proxytype = CURLPROXY_SOCKS4A;
1888 else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
1889 plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
1892 LOG (GNUNET_ERROR_TYPE_ERROR,
1893 _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
1896 GNUNET_free (proxy_type);
1897 GNUNET_free (plugin->proxy_hostname);
1898 plugin->proxy_hostname = NULL;
1899 GNUNET_free_non_null (plugin->proxy_username);
1900 plugin->proxy_username = NULL;
1901 GNUNET_free_non_null (plugin->proxy_password);
1902 plugin->proxy_password = NULL;
1904 return GNUNET_SYSERR;
1907 LOG (GNUNET_ERROR_TYPE_DEBUG,
1908 "Found proxy type: `%s'\n",
1912 /* proxy http tunneling */
1913 if (GNUNET_SYSERR == (plugin->proxy_use_httpproxytunnel = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
1914 plugin->name, "PROXY_HTTP_TUNNELING")))
1915 plugin->proxy_use_httpproxytunnel = GNUNET_NO;
1917 GNUNET_free_non_null (proxy_type);
1926 http_plugin_address_to_string (void *cls,
1930 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1935 http_client_plugin_update_session_timeout (void *cls,
1936 const struct GNUNET_PeerIdentity *peer,
1937 struct Session *session)
1939 struct HTTP_Client_Plugin *plugin = cls;
1941 /* lookup if session is really existing */
1942 if (GNUNET_YES != client_exist_session (plugin, session))
1947 client_reschedule_session_timeout (session);
1951 * Entry point for the plugin.
1954 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1956 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1957 struct GNUNET_TRANSPORT_PluginFunctions *api;
1958 struct HTTP_Client_Plugin *plugin;
1960 if (NULL == env->receive)
1962 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1963 initialze the plugin or the API */
1964 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1966 api->address_to_string = &http_plugin_address_to_string;
1967 api->string_to_address = &http_common_plugin_string_to_address;
1968 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1972 plugin = GNUNET_new (struct HTTP_Client_Plugin);
1974 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1976 api->send = &http_client_plugin_send;
1977 api->disconnect_session = &http_client_session_disconnect;
1978 api->query_keepalive_factor = &http_client_query_keepalive_factor;
1979 api->disconnect_peer = &http_client_peer_disconnect;
1980 api->check_address = &http_client_plugin_address_suggested;
1981 api->get_session = &http_client_plugin_get_session;
1982 api->address_to_string = &http_plugin_address_to_string;
1983 api->string_to_address = &http_common_plugin_string_to_address;
1984 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1985 api->get_network = &http_client_get_network;
1986 api->update_session_timeout = &http_client_plugin_update_session_timeout;
1989 plugin->name = "transport-https_client";
1990 plugin->protocol = "https";
1992 plugin->name = "transport-http_client";
1993 plugin->protocol = "http";
1995 plugin->last_tag = 1;
1996 plugin->options = 0; /* Setup options */
1998 if (GNUNET_SYSERR == client_configure_plugin (plugin))
2000 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2005 if (GNUNET_SYSERR == client_start (plugin))
2007 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2013 /* end of plugin_transport_http_client.c */