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;
102 * Session handle for connections.
107 * To whom are we talking to (set to our identity
108 * if we are still waiting for the welcome message)
110 struct GNUNET_PeerIdentity target;
113 * Stored in a linked list.
115 struct Session *next;
118 * Stored in a linked list.
120 struct Session *prev;
123 * The URL to connect to
138 * ATS network type in NBO
140 uint32_t ats_address_network_type;
143 * Pointer to the global plugin struct.
145 struct HTTP_Client_Plugin *plugin;
148 * Is client send handle paused since there are no data to send?
151 int client_put_paused;
154 * Is client send handle to be disconnected due to inactivity
156 int client_put_disconnect;
159 * Was session given to transport service?
161 // int session_passed;
169 * Client receive handle
174 * next pointer for double linked list
176 struct HTTP_Message *msg_head;
179 * previous pointer for double linked list
181 struct HTTP_Message *msg_tail;
184 * Message stream tokenizer for incoming data
186 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
189 * Session timeout task
191 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
194 * Session timeout task
196 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
199 * Task to wake up client receive handle when receiving is allowed again
201 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
204 * Absolute time when to receive data again
205 * Used for receive throttling
207 struct GNUNET_TIME_Absolute next_receive;
212 * Encapsulation of all of the state of the plugin.
214 struct HTTP_Client_Plugin
219 struct GNUNET_TRANSPORT_PluginEnvironment *env;
222 * Linked list head of open sessions.
224 struct Session *head;
227 * Linked list tail of open sessions.
229 struct Session *tail;
242 * Maximum number of sockets the plugin can use
243 * Each http inbound /outbound connections are two connections
245 unsigned int max_connections;
248 * Current number of sockets the plugin can use
249 * Each http inbound /outbound connections are two connections
251 unsigned int cur_connections;
254 * Last used unique HTTP connection tag
271 CURLM *curl_multi_handle;
276 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
281 * Encapsulation of all of the state of the plugin.
283 struct HTTP_Client_Plugin *p;
287 * Start session timeout for a session
288 * @param s the session
291 client_start_session_timeout (struct Session *s);
295 * Increment session timeout due to activity for a session
296 * @param s the session
299 client_reschedule_session_timeout (struct Session *s);
303 * Cancel timeout for a session
304 * @param s the session
307 client_stop_session_timeout (struct Session *s);
311 * Function setting up file descriptors and scheduling task to run
313 * @param plugin plugin as closure
314 * @param now schedule task in 1ms, regardless of what curl may say
315 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
318 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
321 client_connect_put (struct Session *s);
324 * Does a session s exists?
326 * @param plugin the plugin
327 * @param s desired session
328 * @return GNUNET_YES or GNUNET_NO
331 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
333 struct Session * head;
335 GNUNET_assert (NULL != plugin);
336 GNUNET_assert (NULL != s);
338 for (head = plugin->head; head != NULL; head = head->next)
348 * Function to log curl debug messages with GNUNET_log
351 * @param type curl_infotype
358 client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
361 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
369 case CURLINFO_HEADER_IN:
372 case CURLINFO_HEADER_OUT:
373 ttype = "HEADER_OUT";
379 memcpy (text, data, size);
380 if (text[size - 1] == '\n')
385 text[size + 1] = '\0';
388 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
389 "Connection %p %s: %s", cls, ttype, text);
391 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
392 "Connection %p %s: %s", cls, ttype, text);
401 * Function that can be used by the transport service to transmit
402 * a message using the plugin. Note that in the case of a
403 * peer disconnecting, the continuation MUST be called
404 * prior to the disconnect notification itself. This function
405 * will be called with this peer's HELLO message to initiate
406 * a fresh connection to another peer.
409 * @param session which session must be used
410 * @param msgbuf the message to transmit
411 * @param msgbuf_size number of bytes in 'msgbuf'
412 * @param priority how important is the message (most plugins will
413 * ignore message priority and just FIFO)
414 * @param to how long to wait at most for the transmission (does not
415 * require plugins to discard the message after the timeout,
416 * just advisory for the desired delay; most plugins will ignore
418 * @param cont continuation to call once the message has
419 * been transmitted (or if the transport is ready
420 * for the next transmission call; or if the
421 * peer disconnected...); can be NULL
422 * @param cont_cls closure for cont
423 * @return number of bytes used (on the physical network, with overheads);
424 * -1 on hard errors (i.e. address invalid); 0 is a legal value
425 * and does NOT mean that the message was not transmitted (DV)
428 http_client_plugin_send (void *cls,
429 struct Session *session,
430 const char *msgbuf, size_t msgbuf_size,
431 unsigned int priority,
432 struct GNUNET_TIME_Relative to,
433 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
435 struct HTTP_Client_Plugin *plugin = cls;
436 struct HTTP_Message *msg;
438 GNUNET_assert (plugin != NULL);
439 GNUNET_assert (session != NULL);
441 /* lookup if session is really existing */
442 if (GNUNET_YES != client_exist_session (plugin, session))
445 return GNUNET_SYSERR;
448 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
449 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
450 session, session->client_put,
451 msgbuf_size, GNUNET_i2s (&session->target));
453 /* create new message and schedule */
454 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
456 msg->size = msgbuf_size;
458 msg->buf = (char *) &msg[1];
459 msg->transmit_cont = cont;
460 msg->transmit_cont_cls = cont_cls;
461 memcpy (msg->buf, msgbuf, msgbuf_size);
462 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
464 if (GNUNET_YES == session->client_put_disconnect)
466 session->client_put_disconnect = GNUNET_NO;
467 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
468 "Session %p: Reconnecting PUT connection\n",
470 client_connect_put (session);
473 if (GNUNET_YES == session->client_put_paused)
475 GNUNET_assert (session->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
476 GNUNET_SCHEDULER_cancel (session->put_disconnect_task);
477 session->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
478 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
479 "Session %p/connection %p: unpausing connection\n",
480 session, session->client_put);
481 session->client_put_paused = GNUNET_NO;
482 curl_easy_pause (session->client_put, CURLPAUSE_CONT);
484 client_schedule (session->plugin, GNUNET_YES);
485 client_reschedule_session_timeout (session);
493 * @param s the session to delete
496 client_delete_session (struct Session *s)
498 struct HTTP_Client_Plugin *plugin = s->plugin;
499 struct HTTP_Message *pos;
500 struct HTTP_Message *next;
502 client_stop_session_timeout (s);
504 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
506 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
507 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
510 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
513 while (NULL != (pos = next))
516 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
517 if (pos->transmit_cont != NULL)
518 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR);
522 if (s->msg_tk != NULL)
524 GNUNET_SERVER_mst_destroy (s->msg_tk);
527 GNUNET_free (s->addr);
528 GNUNET_free (s->url);
535 * Disconnect a session
538 * @return GNUNET_OK on success, GNUNET_SYSERR on error
541 client_disconnect (struct Session *s)
543 struct HTTP_Client_Plugin *plugin = s->plugin;
544 struct HTTP_Message *msg;
545 struct HTTP_Message *t;
549 if (GNUNET_YES != client_exist_session (plugin, s))
552 return GNUNET_SYSERR;
555 if (s->client_put != NULL)
557 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
558 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
559 s, s->client_put, GNUNET_i2s (&s->target));
561 /* remove curl handle from multi handle */
562 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
563 if (mret != CURLM_OK)
565 /* clean up easy handle, handle is now invalid and free'd */
569 curl_easy_cleanup (s->client_put);
570 s->client_put = NULL;
574 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
576 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
577 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
580 if (s->client_get != NULL)
582 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
583 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
584 s, s->client_get, GNUNET_i2s (&s->target));
586 /* remove curl handle from multi handle */
587 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
588 if (mret != CURLM_OK)
590 /* clean up easy handle, handle is now invalid and free'd */
594 curl_easy_cleanup (s->client_get);
595 s->client_get = NULL;
602 if (NULL != msg->transmit_cont)
603 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
604 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
609 GNUNET_assert (plugin->cur_connections >= 2);
610 plugin->cur_connections -= 2;
611 GNUNET_STATISTICS_set (plugin->env->stats,
612 "# HTTP client sessions",
613 plugin->cur_connections,
616 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
617 "Session %p: notifying transport about ending session\n",s);
619 plugin->env->session_end (plugin->env->cls, &s->target, s);
620 client_delete_session (s);
622 /* Re-schedule since handles have changed */
623 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
625 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
626 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
628 client_schedule (plugin, GNUNET_YES);
635 * Function that can be used to force the plugin to disconnect
636 * from the given peer and cancel all previous transmissions
637 * (and their continuationc).
640 * @param target peer from which to disconnect
643 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
645 struct HTTP_Client_Plugin *plugin = cls;
646 struct Session *next = NULL;
647 struct Session *pos = NULL;
649 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
650 "Transport tells me to disconnect `%s'\n",
651 GNUNET_i2s (target));
654 while (NULL != (pos = next))
657 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
659 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
660 "Disconnecting session %p to `%pos'\n",
661 pos, GNUNET_i2s (target));
662 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
669 static struct Session *
670 client_lookup_session (struct HTTP_Client_Plugin *plugin,
671 const struct GNUNET_HELLO_Address *address)
675 for (pos = plugin->head; NULL != pos; pos = pos->next)
676 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
677 (address->address_length == pos->addrlen) &&
678 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
684 client_put_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
686 struct Session *s = cls;
687 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
688 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
689 "Session %p/connection %p: will be disconnected due to no activity\n",
691 s->client_put_disconnect = GNUNET_YES;
692 s->client_put_paused = GNUNET_NO;
693 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
694 client_schedule (s->plugin, GNUNET_YES);
700 * Callback method used with libcurl
701 * Method is called when libcurl needs to read data during sending
703 * @param stream pointer where to write data
704 * @param size size of an individual element
705 * @param nmemb count of elements that can be written to the buffer
706 * @param cls source pointer, passed to the libcurl handle
707 * @return bytes written to stream, returning 0 will terminate connection!
710 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
712 struct Session *s = cls;
713 struct HTTP_Client_Plugin *plugin = s->plugin;
714 struct HTTP_Message *msg = s->msg_head;
717 if (GNUNET_YES != client_exist_session (plugin, s))
722 if (GNUNET_YES == s->client_put_disconnect)
724 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
725 "Session %p/connection %p: disconnect due to inactivity\n",
732 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
733 "Session %p/connection %p: nothing to send, suspending\n",
735 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
736 s->client_put_disconnect = GNUNET_NO;
737 s->client_put_paused = GNUNET_YES;
738 return CURL_READFUNC_PAUSE;
741 GNUNET_assert (msg->pos < msg->size);
742 /* calculate how much fits in buffer */
743 len = GNUNET_MIN (msg->size - msg->pos,
745 memcpy (stream, &msg->buf[msg->pos], len);
747 if (msg->pos == msg->size)
749 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
750 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
751 s, s->client_put, msg->size, msg->pos);
752 /* Calling transmit continuation */
753 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
754 if (NULL != msg->transmit_cont)
755 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
758 client_reschedule_session_timeout (s);
764 * Wake up a curl handle which was suspended
766 * @param cls the session
767 * @param tc task context
770 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
772 struct Session *s = cls;
774 if (GNUNET_YES != client_exist_session(p, s))
779 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
780 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
782 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
783 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
784 if (s->client_get != NULL)
785 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
790 * Callback for message stream tokenizer
792 * @param cls the session
793 * @param client not used
794 * @param message the message received
795 * @return always GNUNET_OK
798 client_receive_mst_cb (void *cls, void *client,
799 const struct GNUNET_MessageHeader *message)
801 struct Session *s = cls;
802 struct HTTP_Client_Plugin *plugin;
803 struct GNUNET_TIME_Relative delay;
804 struct GNUNET_ATS_Information atsi[2];
805 if (GNUNET_YES != client_exist_session(p, s))
812 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
813 atsi[0].value = htonl (1);
814 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
815 atsi[1].value = s->ats_address_network_type;
816 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
818 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
819 (const struct GNUNET_ATS_Information *) &atsi, 2,
820 s, s->addr, s->addrlen);
823 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
825 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
828 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
829 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
830 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
833 client_reschedule_session_timeout (s);
839 * Callback method used with libcurl when data for a PUT connection are
840 * received. We do not expect data here, so we just dismiss it
842 * @param stream pointer where to write data
843 * @param size size of an individual element
844 * @param nmemb count of elements that can be written to the buffer
845 * @param cls destination pointer, passed to the libcurl handle
846 * @return bytes read from stream
849 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
856 * Callback method used with libcurl when data for a GET connection are
857 * received. Forward to MST
859 * @param stream pointer where to write data
860 * @param size size of an individual element
861 * @param nmemb count of elements that can be written to the buffer
862 * @param cls destination pointer, passed to the libcurl handle
863 * @return bytes read from stream
866 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
868 struct Session *s = cls;
869 struct GNUNET_TIME_Absolute now;
870 size_t len = size * nmemb;
871 struct HTTP_Client_Plugin *plugin = s->plugin;
873 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
874 "Received %u bytes from peer `%s'\n", len,
875 GNUNET_i2s (&s->target));
876 now = GNUNET_TIME_absolute_get ();
877 if (now.abs_value < s->next_receive.abs_value)
879 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
880 struct GNUNET_TIME_Relative delta =
881 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
882 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
883 "No inbound bandwidth for session %p available! Next read was delayed for %llu ms\n",
885 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
887 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
888 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
890 s->recv_wakeup_task =
891 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
892 return CURLPAUSE_ALL;
894 if (NULL == s->msg_tk)
895 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
896 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
902 * Task performing curl operations
904 * @param cls plugin as closure
905 * @param tc gnunet scheduler task context
908 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
912 * Function setting up file descriptors and scheduling task to run
914 * @param plugin plugin as closure
915 * @param now schedule task in 1ms, regardless of what curl may say
916 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
919 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
925 struct GNUNET_NETWORK_FDSet *grs;
926 struct GNUNET_NETWORK_FDSet *gws;
929 struct GNUNET_TIME_Relative timeout;
931 /* Cancel previous scheduled task */
932 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
934 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
935 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
941 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
942 if (mret != CURLM_OK)
944 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
945 "curl_multi_fdset", __FILE__, __LINE__,
946 curl_multi_strerror (mret));
947 return GNUNET_SYSERR;
949 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
951 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
953 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
954 if (now == GNUNET_YES)
955 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
957 if (mret != CURLM_OK)
959 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
960 "curl_multi_timeout", __FILE__, __LINE__,
961 curl_multi_strerror (mret));
962 return GNUNET_SYSERR;
965 grs = GNUNET_NETWORK_fdset_create ();
966 gws = GNUNET_NETWORK_fdset_create ();
967 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
968 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
970 plugin->client_perform_task =
971 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
973 &client_run, plugin);
974 GNUNET_NETWORK_fdset_destroy (gws);
975 GNUNET_NETWORK_fdset_destroy (grs);
981 * Task performing curl operations
983 * @param cls plugin as closure
984 * @param tc gnunet scheduler task context
987 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
989 struct HTTP_Client_Plugin *plugin = cls;
994 GNUNET_assert (cls != NULL);
996 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
997 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1003 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1008 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1010 CURL *easy_h = msg->easy_handle;
1011 struct Session *s = NULL;
1012 char *d = (char *) s;
1017 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1018 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1020 curl_easy_strerror (msg->data.result), running);
1024 GNUNET_assert (CURLE_OK ==
1025 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1026 s = (struct Session *) d;
1028 if (GNUNET_YES != client_exist_session(plugin, s))
1034 GNUNET_assert (s != NULL);
1035 if (msg->msg == CURLMSG_DONE)
1037 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1038 if (easy_h == s->client_put)
1040 if ((0 != msg->data.result) || (http_statuscode != 200))
1042 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1043 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1044 s, msg->easy_handle, GNUNET_i2s (&s->target),
1047 curl_easy_strerror (msg->data.result));
1050 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1051 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1052 s, msg->easy_handle, GNUNET_i2s (&s->target));
1053 if (s->client_get == NULL)
1055 /* Disconnect other transmission direction and tell transport */
1057 curl_multi_remove_handle(plugin->curl_multi_handle, easy_h);
1058 curl_easy_cleanup(easy_h);
1059 s->client_put = NULL;
1061 if (easy_h == s->client_get)
1063 if ((0 != msg->data.result) || (http_statuscode != 200))
1065 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1066 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1067 s, msg->easy_handle, GNUNET_i2s (&s->target),
1070 curl_easy_strerror (msg->data.result));
1074 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1075 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1076 s, msg->easy_handle, GNUNET_i2s (&s->target));
1077 /* Disconnect other transmission direction and tell transport */
1078 client_disconnect (s);
1083 while (mret == CURLM_CALL_MULTI_PERFORM);
1084 client_schedule (plugin, GNUNET_NO);
1088 client_connect_get (struct Session *s)
1091 /* create get connection */
1092 s->client_get = curl_easy_init ();
1094 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1095 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1096 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
1099 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1100 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1101 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1103 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1104 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1105 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1106 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1107 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1108 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1109 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1110 /* No timeout by default, timeout done with session timeout */
1111 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1112 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1113 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1114 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1115 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1116 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1117 #if CURL_TCP_NODELAY
1118 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1120 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1121 if (mret != CURLM_OK)
1123 curl_easy_cleanup (s->client_get);
1125 return GNUNET_SYSERR;
1132 client_connect_put (struct Session *s)
1135 /* create put connection */
1136 if (NULL == s->client_put)
1137 s->client_put = curl_easy_init ();
1139 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1140 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1141 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
1144 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1145 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1146 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1148 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1149 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1151 struct curl_slist *m_headerlist;
1152 m_headerlist = NULL;
1153 m_headerlist = curl_slist_append(m_headerlist, "Transfer-Encoding: chunked");
1154 curl_easy_setopt(s->client_put, CURLOPT_HTTPHEADER, m_headerlist);*/
1155 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1156 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1157 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1158 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1159 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1160 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1161 /* No timeout by default, timeout done with session timeout */
1162 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1163 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1164 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1165 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1166 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1167 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1168 #if CURL_TCP_NODELAY
1169 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1172 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1173 if (mret != CURLM_OK)
1175 curl_easy_cleanup (s->client_put);
1177 return GNUNET_SYSERR;
1183 client_connect (struct Session *s)
1186 struct HTTP_Client_Plugin *plugin = s->plugin;
1187 int res = GNUNET_OK;
1191 if (NULL == http_common_plugin_address_to_string (NULL, s->addr, s->addrlen))
1193 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1194 "Invalid address peer `%s'\n",
1195 GNUNET_i2s (&s->target));
1196 return GNUNET_SYSERR;
1199 GNUNET_asprintf (&s->url, "%s/%s;%u",
1200 http_common_plugin_address_to_string (plugin, s->addr, s->addrlen),
1201 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
1206 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1207 "Initiating outbound session peer `%s' using address `%s'\n",
1208 GNUNET_i2s (&s->target), s->url);
1210 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1211 (GNUNET_SYSERR == client_connect_put (s)))
1214 return GNUNET_SYSERR;
1217 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1218 "Session %p: connected with connections GET %p and PUT %p\n",
1219 s, s->client_get, s->client_put);
1221 /* Perform connect */
1222 plugin->cur_connections += 2;
1223 GNUNET_STATISTICS_set (plugin->env->stats,
1224 "# HTTP client connections",
1225 plugin->cur_connections,
1228 /* Re-schedule since handles have changed */
1229 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1231 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1232 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1234 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1240 * Creates a new outbound session the transport service will use to send data to the
1243 * @param cls the plugin
1244 * @param address the address
1245 * @return the session or NULL of max connections exceeded
1247 static struct Session *
1248 http_client_plugin_get_session (void *cls,
1249 const struct GNUNET_HELLO_Address *address)
1251 struct HTTP_Client_Plugin *plugin = cls;
1252 struct Session * s = NULL;
1253 struct sockaddr *sa;
1254 struct GNUNET_ATS_Information ats;
1258 GNUNET_assert (plugin != NULL);
1259 GNUNET_assert (address != NULL);
1260 GNUNET_assert (address->address != NULL);
1262 /* find existing session */
1263 s = client_lookup_session (plugin, address);
1267 if (plugin->max_connections <= plugin->cur_connections)
1269 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1270 "Maximum number of connections (%u) reached: "
1271 "cannot connect to peer `%s'\n",
1272 plugin->max_connections,
1273 GNUNET_i2s (&address->peer));
1277 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1278 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1279 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1281 if (GNUNET_SYSERR == res)
1285 else if (GNUNET_YES == res)
1287 GNUNET_assert (NULL != sa);
1288 if (AF_INET == sa->sa_family)
1290 salen = sizeof (struct sockaddr_in);
1292 else if (AF_INET == sa->sa_family)
1294 salen = sizeof (struct sockaddr_in6);
1296 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1299 else if (GNUNET_NO == res)
1301 ats.value = htonl (GNUNET_ATS_COST_WAN);
1304 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1310 s = GNUNET_malloc (sizeof (struct Session));
1311 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1313 s->addr = GNUNET_malloc (address->address_length);
1314 memcpy (s->addr, address->address, address->address_length);
1315 s->addrlen = address->address_length;
1316 s->ats_address_network_type = ats.value;
1318 client_start_session_timeout (s);
1320 /* add new session */
1321 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1323 /* initiate new connection */
1324 if (GNUNET_SYSERR == client_connect (s))
1326 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1327 "Cannot connect to peer `%s' address `%s''\n",
1328 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1329 GNUNET_i2s (&s->target));
1330 client_delete_session (s);
1338 * Setup http_client plugin
1340 * @param plugin the plugin handle
1341 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1344 client_start (struct HTTP_Client_Plugin *plugin)
1346 curl_global_init (CURL_GLOBAL_ALL);
1347 plugin->curl_multi_handle = curl_multi_init ();
1349 if (NULL == plugin->curl_multi_handle)
1351 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1352 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1354 return GNUNET_SYSERR;
1360 * Session was idle, so disconnect it
1363 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1365 GNUNET_assert (NULL != cls);
1366 struct Session *s = cls;
1368 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1369 GNUNET_log (TIMEOUT_LOG,
1370 "Session %p was idle for %llu ms, disconnecting\n",
1371 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1373 /* call session destroy function */
1374 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1379 * Start session timeout for session s
1381 * @param s the session
1384 client_start_session_timeout (struct Session *s)
1387 GNUNET_assert (NULL != s);
1388 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1389 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1390 &client_session_timeout,
1392 GNUNET_log (TIMEOUT_LOG,
1393 "Timeout for session %p set to %llu ms\n",
1394 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1399 * Increment session timeout due to activity for session s
1401 * param s the session
1404 client_reschedule_session_timeout (struct Session *s)
1407 GNUNET_assert (NULL != s);
1408 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1410 GNUNET_SCHEDULER_cancel (s->timeout_task);
1411 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1412 &client_session_timeout,
1414 GNUNET_log (TIMEOUT_LOG,
1415 "Timeout rescheduled for session %p set to %llu ms\n",
1416 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1421 * Cancel timeout due to activity for session s
1423 * param s the session
1426 client_stop_session_timeout (struct Session *s)
1428 GNUNET_assert (NULL != s);
1430 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1432 GNUNET_SCHEDULER_cancel (s->timeout_task);
1433 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1434 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1440 * Another peer has suggested an address for this
1441 * peer and transport plugin. Check that this could be a valid
1442 * address. If so, consider adding it to the list
1445 * @param cls closure
1446 * @param addr pointer to the address
1447 * @param addrlen length of addr
1448 * @return GNUNET_OK if this is a plausible address for this peer
1452 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1454 /* struct Plugin *plugin = cls; */
1456 /* A HTTP/S client does not have any valid address so:*/
1462 * Exit point from the plugin.
1464 * @param cls api as closure
1468 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1470 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1471 struct HTTP_Client_Plugin *plugin = api->cls;
1472 struct Session *pos;
1473 struct Session *next;
1475 if (NULL == api->cls)
1482 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1483 _("Shutting down plugin `%s'\n"),
1487 next = plugin->head;
1488 while (NULL != (pos = next))
1491 client_disconnect (pos);
1493 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1495 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1496 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1500 if (NULL != plugin->curl_multi_handle)
1502 curl_multi_cleanup (plugin->curl_multi_handle);
1503 plugin->curl_multi_handle = NULL;
1505 curl_global_cleanup ();
1507 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1508 _("Shutdown for plugin `%s' complete\n"),
1511 GNUNET_free (plugin);
1520 * @param plugin the plugin handle
1521 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1524 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1526 unsigned long long max_connections;
1528 /* Optional parameters */
1529 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1531 "MAX_CONNECTIONS", &max_connections))
1532 max_connections = 128;
1533 plugin->max_connections = max_connections;
1535 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1536 _("Maximum number of connections is %u\n"),
1537 plugin->max_connections);
1542 * Entry point for the plugin.
1545 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1547 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1548 struct GNUNET_TRANSPORT_PluginFunctions *api;
1549 struct HTTP_Client_Plugin *plugin;
1551 if (NULL == env->receive)
1553 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1554 initialze the plugin or the API */
1555 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1557 api->address_to_string = &http_common_plugin_address_to_string;
1558 api->string_to_address = &http_common_plugin_string_to_address;
1559 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1563 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1566 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1568 api->send = &http_client_plugin_send;
1569 api->disconnect = &http_client_plugin_disconnect;
1570 api->check_address = &http_client_plugin_address_suggested;
1571 api->get_session = &http_client_plugin_get_session;
1572 api->address_to_string = &http_common_plugin_address_to_string;
1573 api->string_to_address = &http_common_plugin_string_to_address;
1574 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1578 plugin->name = "transport-https_client";
1579 plugin->protocol = "https";
1581 plugin->name = "transport-http_client";
1582 plugin->protocol = "http";
1584 plugin->last_tag = 1;
1586 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1588 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1593 if (GNUNET_SYSERR == client_start (plugin))
1595 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1601 /* end of plugin_transport_http_client.c */