2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
29 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
31 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
32 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
35 #define VERBOSE_CURL GNUNET_YES
37 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
39 #define ENABLE_PUT GNUNET_YES
40 #define ENABLE_GET GNUNET_YES
43 #include "gnunet_protocols.h"
44 #include "gnunet_common.h"
45 #include "gnunet_server_lib.h"
46 #include "gnunet_transport_plugin.h"
47 #include "plugin_transport_http_common.h"
48 #include <curl/curl.h>
52 * Encapsulation of all of the state of the plugin.
54 struct HTTP_Client_Plugin;
58 * Message to send using http
63 * next pointer for double linked list
65 struct HTTP_Message *next;
68 * previous pointer for double linked list
70 struct HTTP_Message *prev;
73 * buffer containing data to send
78 * amount of data already sent
88 * Continuation function to call once the transmission buffer
89 * has again space available. NULL if there is no
90 * continuation to call.
92 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
95 * Closure for transmit_cont.
97 void *transmit_cont_cls;
101 * Session handle for connections.
106 * To whom are we talking to (set to our identity
107 * if we are still waiting for the welcome message)
109 struct GNUNET_PeerIdentity target;
112 * Stored in a linked list.
114 struct Session *next;
117 * Stored in a linked list.
119 struct Session *prev;
122 * The URL to connect to
137 * ATS network type in NBO
139 uint32_t ats_address_network_type;
142 * Pointer to the global plugin struct.
144 struct HTTP_Client_Plugin *plugin;
147 * Was session given to transport service?
149 // int session_passed;
158 int put_tmp_disconnecting;
161 * Client receive handle
166 * next pointer for double linked list
168 struct HTTP_Message *msg_head;
171 * previous pointer for double linked list
173 struct HTTP_Message *msg_tail;
176 * Message stream tokenizer for incoming data
178 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
181 * Session timeout task
183 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
186 * Session timeout task
188 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
191 * Task to wake up client receive handle when receiving is allowed again
193 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
196 * Absolute time when to receive data again
197 * Used for receive throttling
199 struct GNUNET_TIME_Absolute next_receive;
204 * Encapsulation of all of the state of the plugin.
206 struct HTTP_Client_Plugin
211 struct GNUNET_TRANSPORT_PluginEnvironment *env;
214 * Linked list head of open sessions.
216 struct Session *head;
219 * Linked list tail of open sessions.
221 struct Session *tail;
234 * Maximum number of sockets the plugin can use
235 * Each http inbound /outbound connections are two connections
237 unsigned int max_connections;
240 * Current number of sockets the plugin can use
241 * Each http inbound /outbound connections are two connections
243 unsigned int cur_connections;
246 * Last used unique HTTP connection tag
263 CURLM *curl_multi_handle;
268 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
273 * Encapsulation of all of the state of the plugin.
275 struct HTTP_Client_Plugin *p;
279 * Start session timeout for a session
280 * @param s the session
283 client_start_session_timeout (struct Session *s);
287 * Increment session timeout due to activity for a session
288 * @param s the session
291 client_reschedule_session_timeout (struct Session *s);
295 * Cancel timeout for a session
296 * @param s the session
299 client_stop_session_timeout (struct Session *s);
303 * Function setting up file descriptors and scheduling task to run
305 * @param plugin plugin as closure
306 * @param now schedule task in 1ms, regardless of what curl may say
307 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
310 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
313 client_connect_put (struct Session *s);
316 * Does a session s exists?
318 * @param plugin the plugin
319 * @param s desired session
320 * @return GNUNET_YES or GNUNET_NO
323 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
325 struct Session * head;
327 GNUNET_assert (NULL != plugin);
328 GNUNET_assert (NULL != s);
330 for (head = plugin->head; head != NULL; head = head->next)
340 * Function to log curl debug messages with GNUNET_log
343 * @param type curl_infotype
350 client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
353 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
361 case CURLINFO_HEADER_IN:
364 case CURLINFO_HEADER_OUT:
365 ttype = "HEADER_OUT";
371 memcpy (text, data, size);
372 if (text[size - 1] == '\n')
377 text[size + 1] = '\0';
380 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
381 "Connection %p %s: %s", cls, ttype, text);
383 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
384 "Connection %p %s: %s", cls, ttype, text);
393 * Function that can be used by the transport service to transmit
394 * a message using the plugin. Note that in the case of a
395 * peer disconnecting, the continuation MUST be called
396 * prior to the disconnect notification itself. This function
397 * will be called with this peer's HELLO message to initiate
398 * a fresh connection to another peer.
401 * @param session which session must be used
402 * @param msgbuf the message to transmit
403 * @param msgbuf_size number of bytes in 'msgbuf'
404 * @param priority how important is the message (most plugins will
405 * ignore message priority and just FIFO)
406 * @param to how long to wait at most for the transmission (does not
407 * require plugins to discard the message after the timeout,
408 * just advisory for the desired delay; most plugins will ignore
410 * @param cont continuation to call once the message has
411 * been transmitted (or if the transport is ready
412 * for the next transmission call; or if the
413 * peer disconnected...); can be NULL
414 * @param cont_cls closure for cont
415 * @return number of bytes used (on the physical network, with overheads);
416 * -1 on hard errors (i.e. address invalid); 0 is a legal value
417 * and does NOT mean that the message was not transmitted (DV)
420 http_client_plugin_send (void *cls,
422 const char *msgbuf, size_t msgbuf_size,
423 unsigned int priority,
424 struct GNUNET_TIME_Relative to,
425 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
427 struct HTTP_Client_Plugin *plugin = cls;
428 struct HTTP_Message *msg;
430 GNUNET_assert (plugin != NULL);
431 GNUNET_assert (s != NULL);
433 /* lookup if session is really existing */
434 if (GNUNET_YES != client_exist_session (plugin, s))
437 return GNUNET_SYSERR;
440 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
441 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
443 msgbuf_size, GNUNET_i2s (&s->target));
445 /* create new message and schedule */
446 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
448 msg->size = msgbuf_size;
450 msg->buf = (char *) &msg[1];
451 msg->transmit_cont = cont;
452 msg->transmit_cont_cls = cont_cls;
453 memcpy (msg->buf, msgbuf, msgbuf_size);
454 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
456 if (GNUNET_YES == s->put_tmp_disconnecting)
458 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
459 "Session %p: Reconnecting PUT connection\n",
461 s->put_tmp_disconnecting = GNUNET_NO;
462 if (GNUNET_SYSERR == client_connect_put (s))
464 return GNUNET_SYSERR;
468 if (GNUNET_YES == s->put_paused)
470 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
471 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
472 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
473 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
474 "Session %p/connection %p: unpausing connection\n",
476 s->put_paused = GNUNET_NO;
477 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
479 client_schedule (s->plugin, GNUNET_YES);
480 client_reschedule_session_timeout (s);
488 * @param s the session to delete
491 client_delete_session (struct Session *s)
493 struct HTTP_Client_Plugin *plugin = s->plugin;
494 struct HTTP_Message *pos;
495 struct HTTP_Message *next;
497 client_stop_session_timeout (s);
499 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
501 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
502 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
505 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
508 while (NULL != (pos = next))
511 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
512 if (pos->transmit_cont != NULL)
513 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR);
517 if (s->msg_tk != NULL)
519 GNUNET_SERVER_mst_destroy (s->msg_tk);
522 GNUNET_free (s->addr);
523 GNUNET_free (s->url);
530 * Disconnect a session
533 * @return GNUNET_OK on success, GNUNET_SYSERR on error
536 client_disconnect (struct Session *s)
538 struct HTTP_Client_Plugin *plugin = s->plugin;
539 struct HTTP_Message *msg;
540 struct HTTP_Message *t;
544 if (GNUNET_YES != client_exist_session (plugin, s))
547 return GNUNET_SYSERR;
550 if (s->client_put != NULL)
552 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
553 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
554 s, s->client_put, GNUNET_i2s (&s->target));
556 /* remove curl handle from multi handle */
557 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
558 if (mret != CURLM_OK)
560 /* clean up easy handle, handle is now invalid and free'd */
564 curl_easy_cleanup (s->client_put);
565 s->client_put = NULL;
569 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
571 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
572 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
575 if (s->client_get != NULL)
577 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
578 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
579 s, s->client_get, GNUNET_i2s (&s->target));
581 /* remove curl handle from multi handle */
582 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
583 if (mret != CURLM_OK)
585 /* clean up easy handle, handle is now invalid and free'd */
589 curl_easy_cleanup (s->client_get);
590 s->client_get = NULL;
597 if (NULL != msg->transmit_cont)
598 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
599 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
604 GNUNET_assert (plugin->cur_connections >= 2);
605 plugin->cur_connections -= 2;
606 GNUNET_STATISTICS_set (plugin->env->stats,
607 "# HTTP client sessions",
608 plugin->cur_connections,
611 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
612 "Session %p: notifying transport about ending session\n",s);
614 plugin->env->session_end (plugin->env->cls, &s->target, s);
615 client_delete_session (s);
617 /* Re-schedule since handles have changed */
618 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
620 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
621 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
623 client_schedule (plugin, GNUNET_YES);
630 * Function that can be used to force the plugin to disconnect
631 * from the given peer and cancel all previous transmissions
632 * (and their continuationc).
635 * @param target peer from which to disconnect
638 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
640 struct HTTP_Client_Plugin *plugin = cls;
641 struct Session *next = NULL;
642 struct Session *pos = NULL;
644 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
645 "Transport tells me to disconnect `%s'\n",
646 GNUNET_i2s (target));
649 while (NULL != (pos = next))
652 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
654 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
655 "Disconnecting session %p to `%pos'\n",
656 pos, GNUNET_i2s (target));
657 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
664 static struct Session *
665 client_lookup_session (struct HTTP_Client_Plugin *plugin,
666 const struct GNUNET_HELLO_Address *address)
670 for (pos = plugin->head; NULL != pos; pos = pos->next)
671 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
672 (address->address_length == pos->addrlen) &&
673 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
679 client_put_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
681 struct Session *s = cls;
682 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
683 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
684 "Session %p/connection %p: will be disconnected due to no activity\n",
686 s->put_paused = GNUNET_NO;
687 s->put_tmp_disconnecting = GNUNET_YES;
688 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
689 client_schedule (s->plugin, GNUNET_YES);
695 * Callback method used with libcurl
696 * Method is called when libcurl needs to read data during sending
698 * @param stream pointer where to write data
699 * @param size size of an individual element
700 * @param nmemb count of elements that can be written to the buffer
701 * @param cls source pointer, passed to the libcurl handle
702 * @return bytes written to stream, returning 0 will terminate connection!
705 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
707 struct Session *s = cls;
708 struct HTTP_Client_Plugin *plugin = s->plugin;
709 struct HTTP_Message *msg = s->msg_head;
712 if (GNUNET_YES != client_exist_session (plugin, s))
717 if (GNUNET_YES == s->put_tmp_disconnecting)
720 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
721 "Session %p/connection %p: disconnect due to inactivity\n",
728 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
729 "Session %p/connection %p: nothing to send, suspending\n",
731 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
732 s->put_paused = GNUNET_YES;
733 return CURL_READFUNC_PAUSE;
736 GNUNET_assert (msg->pos < msg->size);
737 /* calculate how much fits in buffer */
738 len = GNUNET_MIN (msg->size - msg->pos,
740 memcpy (stream, &msg->buf[msg->pos], len);
742 if (msg->pos == msg->size)
744 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
745 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
746 s, s->client_put, msg->size, msg->pos);
747 /* Calling transmit continuation */
748 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
749 if (NULL != msg->transmit_cont)
750 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
753 client_reschedule_session_timeout (s);
759 * Wake up a curl handle which was suspended
761 * @param cls the session
762 * @param tc task context
765 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
767 struct Session *s = cls;
769 if (GNUNET_YES != client_exist_session(p, s))
774 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
775 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
777 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
778 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
779 if (s->client_get != NULL)
780 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
785 * Callback for message stream tokenizer
787 * @param cls the session
788 * @param client not used
789 * @param message the message received
790 * @return always GNUNET_OK
793 client_receive_mst_cb (void *cls, void *client,
794 const struct GNUNET_MessageHeader *message)
796 struct Session *s = cls;
797 struct HTTP_Client_Plugin *plugin;
798 struct GNUNET_TIME_Relative delay;
799 struct GNUNET_ATS_Information atsi[2];
800 if (GNUNET_YES != client_exist_session(p, s))
807 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
808 atsi[0].value = htonl (1);
809 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
810 atsi[1].value = s->ats_address_network_type;
811 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
813 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
814 (const struct GNUNET_ATS_Information *) &atsi, 2,
815 s, s->addr, s->addrlen);
818 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
820 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
823 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
824 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
825 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
828 client_reschedule_session_timeout (s);
834 * Callback method used with libcurl when data for a PUT connection are
835 * received. We do not expect data here, so we just dismiss it
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 destination pointer, passed to the libcurl handle
841 * @return bytes read from stream
844 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
851 * Callback method used with libcurl when data for a GET connection are
852 * received. Forward to MST
854 * @param stream pointer where to write data
855 * @param size size of an individual element
856 * @param nmemb count of elements that can be written to the buffer
857 * @param cls destination pointer, passed to the libcurl handle
858 * @return bytes read from stream
861 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
863 struct Session *s = cls;
864 struct GNUNET_TIME_Absolute now;
865 size_t len = size * nmemb;
866 struct HTTP_Client_Plugin *plugin = s->plugin;
868 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
869 "Received %u bytes from peer `%s'\n", len,
870 GNUNET_i2s (&s->target));
871 now = GNUNET_TIME_absolute_get ();
872 if (now.abs_value < s->next_receive.abs_value)
874 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
875 struct GNUNET_TIME_Relative delta =
876 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
877 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
878 "No inbound bandwidth for session %p available! Next read was delayed for %llu ms\n",
880 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
882 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
883 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
885 s->recv_wakeup_task =
886 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
887 return CURLPAUSE_ALL;
889 if (NULL == s->msg_tk)
890 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
891 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
897 * Task performing curl operations
899 * @param cls plugin as closure
900 * @param tc gnunet scheduler task context
903 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
907 * Function setting up file descriptors and scheduling task to run
909 * @param plugin plugin as closure
910 * @param now schedule task in 1ms, regardless of what curl may say
911 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
914 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
920 struct GNUNET_NETWORK_FDSet *grs;
921 struct GNUNET_NETWORK_FDSet *gws;
924 struct GNUNET_TIME_Relative timeout;
926 /* Cancel previous scheduled task */
927 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
929 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
930 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
936 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
937 if (mret != CURLM_OK)
939 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
940 "curl_multi_fdset", __FILE__, __LINE__,
941 curl_multi_strerror (mret));
942 return GNUNET_SYSERR;
944 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
946 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
948 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
949 if (now == GNUNET_YES)
950 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
952 if (mret != CURLM_OK)
954 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
955 "curl_multi_timeout", __FILE__, __LINE__,
956 curl_multi_strerror (mret));
957 return GNUNET_SYSERR;
960 grs = GNUNET_NETWORK_fdset_create ();
961 gws = GNUNET_NETWORK_fdset_create ();
962 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
963 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
965 plugin->client_perform_task =
966 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
968 &client_run, plugin);
969 GNUNET_NETWORK_fdset_destroy (gws);
970 GNUNET_NETWORK_fdset_destroy (grs);
976 * Task performing curl operations
978 * @param cls plugin as closure
979 * @param tc gnunet scheduler task context
982 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
984 struct HTTP_Client_Plugin *plugin = cls;
989 GNUNET_assert (cls != NULL);
991 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
992 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
998 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1003 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1005 CURL *easy_h = msg->easy_handle;
1006 struct Session *s = NULL;
1007 char *d = (char *) s;
1012 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1013 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1015 curl_easy_strerror (msg->data.result), running);
1019 GNUNET_assert (CURLE_OK ==
1020 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1021 s = (struct Session *) d;
1023 if (GNUNET_YES != client_exist_session(plugin, s))
1029 GNUNET_assert (s != NULL);
1030 if (msg->msg == CURLMSG_DONE)
1032 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1033 if (easy_h == s->client_put)
1035 if ((0 != msg->data.result) || (http_statuscode != 200))
1037 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1038 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1039 s, msg->easy_handle, GNUNET_i2s (&s->target),
1042 curl_easy_strerror (msg->data.result));
1045 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1046 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1047 s, msg->easy_handle, GNUNET_i2s (&s->target));
1048 if (s->client_get == NULL)
1050 /* Disconnect other transmission direction and tell transport */
1052 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1053 curl_easy_cleanup (easy_h);
1054 s->client_put = NULL;
1056 if (easy_h == s->client_get)
1058 if ((0 != msg->data.result) || (http_statuscode != 200))
1060 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1061 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1062 s, msg->easy_handle, GNUNET_i2s (&s->target),
1065 curl_easy_strerror (msg->data.result));
1069 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1070 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1071 s, msg->easy_handle, GNUNET_i2s (&s->target));
1072 /* Disconnect other transmission direction and tell transport */
1073 client_disconnect (s);
1078 while (mret == CURLM_CALL_MULTI_PERFORM);
1079 client_schedule (plugin, GNUNET_NO);
1083 client_connect_get (struct Session *s)
1086 /* create get connection */
1087 s->client_get = curl_easy_init ();
1089 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1090 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1091 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
1094 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1095 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1096 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1098 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1099 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1100 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1101 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1102 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1103 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1104 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1105 /* No timeout by default, timeout done with session timeout */
1106 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1107 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1108 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1109 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1110 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1111 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1112 #if CURL_TCP_NODELAY
1113 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1115 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1116 if (mret != CURLM_OK)
1118 curl_easy_cleanup (s->client_get);
1120 return GNUNET_SYSERR;
1127 client_connect_put (struct Session *s)
1130 /* create put connection */
1131 if (NULL == s->client_put)
1133 s->client_put = curl_easy_init ();
1136 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1137 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1138 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
1141 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1142 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1143 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1145 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1146 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1147 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1148 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1149 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1150 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1151 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1152 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1153 /* No timeout by default, timeout done with session timeout */
1154 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1155 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1156 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1157 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1158 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1159 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1160 #if CURL_TCP_NODELAY
1161 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1163 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1164 if (mret != CURLM_OK)
1166 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1167 "Failed to add curl handle to multihandle: `%s'\n",
1168 curl_multi_strerror (mret));
1170 curl_easy_cleanup (s->client_put);
1171 s->client_put = NULL;
1172 return GNUNET_SYSERR;
1178 client_connect (struct Session *s)
1181 struct HTTP_Client_Plugin *plugin = s->plugin;
1182 int res = GNUNET_OK;
1186 if (NULL == http_common_plugin_address_to_string (NULL, s->addr, s->addrlen))
1188 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1189 "Invalid address peer `%s'\n",
1190 GNUNET_i2s (&s->target));
1191 return GNUNET_SYSERR;
1194 GNUNET_asprintf (&s->url, "%s/%s;%u",
1195 http_common_plugin_address_to_string (plugin, s->addr, s->addrlen),
1196 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
1201 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1202 "Initiating outbound session peer `%s' using address `%s'\n",
1203 GNUNET_i2s (&s->target), s->url);
1205 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1206 (GNUNET_SYSERR == client_connect_put (s)))
1209 return GNUNET_SYSERR;
1212 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1213 "Session %p: connected with connections GET %p and PUT %p\n",
1214 s, s->client_get, s->client_put);
1216 /* Perform connect */
1217 plugin->cur_connections += 2;
1218 GNUNET_STATISTICS_set (plugin->env->stats,
1219 "# HTTP client connections",
1220 plugin->cur_connections,
1223 /* Re-schedule since handles have changed */
1224 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1226 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1227 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1229 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1235 * Creates a new outbound session the transport service will use to send data to the
1238 * @param cls the plugin
1239 * @param address the address
1240 * @return the session or NULL of max connections exceeded
1242 static struct Session *
1243 http_client_plugin_get_session (void *cls,
1244 const struct GNUNET_HELLO_Address *address)
1246 struct HTTP_Client_Plugin *plugin = cls;
1247 struct Session * s = NULL;
1248 struct sockaddr *sa;
1249 struct GNUNET_ATS_Information ats;
1253 GNUNET_assert (plugin != NULL);
1254 GNUNET_assert (address != NULL);
1255 GNUNET_assert (address->address != NULL);
1257 /* find existing session */
1258 s = client_lookup_session (plugin, address);
1262 if (plugin->max_connections <= plugin->cur_connections)
1264 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1265 "Maximum number of connections (%u) reached: "
1266 "cannot connect to peer `%s'\n",
1267 plugin->max_connections,
1268 GNUNET_i2s (&address->peer));
1272 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1273 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1274 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1276 if (GNUNET_SYSERR == res)
1280 else if (GNUNET_YES == res)
1282 GNUNET_assert (NULL != sa);
1283 if (AF_INET == sa->sa_family)
1285 salen = sizeof (struct sockaddr_in);
1287 else if (AF_INET == sa->sa_family)
1289 salen = sizeof (struct sockaddr_in6);
1291 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1294 else if (GNUNET_NO == res)
1296 ats.value = htonl (GNUNET_ATS_COST_WAN);
1299 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1305 s = GNUNET_malloc (sizeof (struct Session));
1306 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1308 s->addr = GNUNET_malloc (address->address_length);
1309 memcpy (s->addr, address->address, address->address_length);
1310 s->addrlen = address->address_length;
1311 s->ats_address_network_type = ats.value;
1312 s->put_paused = GNUNET_NO;
1313 s->put_tmp_disconnecting = GNUNET_NO;
1315 client_start_session_timeout (s);
1317 /* add new session */
1318 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1320 /* initiate new connection */
1321 if (GNUNET_SYSERR == client_connect (s))
1323 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1324 "Cannot connect to peer `%s' address `%s''\n",
1325 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1326 GNUNET_i2s (&s->target));
1327 client_delete_session (s);
1335 * Setup http_client plugin
1337 * @param plugin the plugin handle
1338 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1341 client_start (struct HTTP_Client_Plugin *plugin)
1343 curl_global_init (CURL_GLOBAL_ALL);
1344 plugin->curl_multi_handle = curl_multi_init ();
1346 if (NULL == plugin->curl_multi_handle)
1348 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1349 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1351 return GNUNET_SYSERR;
1357 * Session was idle, so disconnect it
1360 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1362 GNUNET_assert (NULL != cls);
1363 struct Session *s = cls;
1365 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1366 GNUNET_log (TIMEOUT_LOG,
1367 "Session %p was idle for %llu ms, disconnecting\n",
1368 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1370 /* call session destroy function */
1371 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1376 * Start session timeout for session s
1378 * @param s the session
1381 client_start_session_timeout (struct Session *s)
1384 GNUNET_assert (NULL != s);
1385 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1386 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1387 &client_session_timeout,
1389 GNUNET_log (TIMEOUT_LOG,
1390 "Timeout for session %p set to %llu ms\n",
1391 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1396 * Increment session timeout due to activity for session s
1398 * param s the session
1401 client_reschedule_session_timeout (struct Session *s)
1404 GNUNET_assert (NULL != s);
1405 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1407 GNUNET_SCHEDULER_cancel (s->timeout_task);
1408 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1409 &client_session_timeout,
1411 GNUNET_log (TIMEOUT_LOG,
1412 "Timeout rescheduled for session %p set to %llu ms\n",
1413 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1418 * Cancel timeout due to activity for session s
1420 * param s the session
1423 client_stop_session_timeout (struct Session *s)
1425 GNUNET_assert (NULL != s);
1427 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1429 GNUNET_SCHEDULER_cancel (s->timeout_task);
1430 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1431 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1437 * Another peer has suggested an address for this
1438 * peer and transport plugin. Check that this could be a valid
1439 * address. If so, consider adding it to the list
1442 * @param cls closure
1443 * @param addr pointer to the address
1444 * @param addrlen length of addr
1445 * @return GNUNET_OK if this is a plausible address for this peer
1449 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1451 /* struct Plugin *plugin = cls; */
1453 /* A HTTP/S client does not have any valid address so:*/
1459 * Exit point from the plugin.
1461 * @param cls api as closure
1465 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1467 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1468 struct HTTP_Client_Plugin *plugin = api->cls;
1469 struct Session *pos;
1470 struct Session *next;
1472 if (NULL == api->cls)
1479 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1480 _("Shutting down plugin `%s'\n"),
1484 next = plugin->head;
1485 while (NULL != (pos = next))
1488 client_disconnect (pos);
1490 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1492 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1493 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1497 if (NULL != plugin->curl_multi_handle)
1499 curl_multi_cleanup (plugin->curl_multi_handle);
1500 plugin->curl_multi_handle = NULL;
1502 curl_global_cleanup ();
1504 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1505 _("Shutdown for plugin `%s' complete\n"),
1508 GNUNET_free (plugin);
1517 * @param plugin the plugin handle
1518 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1521 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1523 unsigned long long max_connections;
1525 /* Optional parameters */
1526 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1528 "MAX_CONNECTIONS", &max_connections))
1529 max_connections = 128;
1530 plugin->max_connections = max_connections;
1532 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1533 _("Maximum number of connections is %u\n"),
1534 plugin->max_connections);
1539 * Entry point for the plugin.
1542 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1544 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1545 struct GNUNET_TRANSPORT_PluginFunctions *api;
1546 struct HTTP_Client_Plugin *plugin;
1548 if (NULL == env->receive)
1550 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1551 initialze the plugin or the API */
1552 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1554 api->address_to_string = &http_common_plugin_address_to_string;
1555 api->string_to_address = &http_common_plugin_string_to_address;
1556 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1560 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1563 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1565 api->send = &http_client_plugin_send;
1566 api->disconnect = &http_client_plugin_disconnect;
1567 api->check_address = &http_client_plugin_address_suggested;
1568 api->get_session = &http_client_plugin_get_session;
1569 api->address_to_string = &http_common_plugin_address_to_string;
1570 api->string_to_address = &http_common_plugin_string_to_address;
1571 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1575 plugin->name = "transport-https_client";
1576 plugin->protocol = "https";
1578 plugin->name = "transport-http_client";
1579 plugin->protocol = "http";
1581 plugin->last_tag = 1;
1583 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1585 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1590 if (GNUNET_SYSERR == client_start (plugin))
1592 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1598 /* end of plugin_transport_http_client.c */