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_NO
38 #include "gnunet_protocols.h"
39 #include "gnunet_common.h"
40 #include "gnunet_server_lib.h"
41 #include "gnunet_transport_plugin.h"
42 #include "plugin_transport_http_common.h"
43 #include <curl/curl.h>
47 * Encapsulation of all of the state of the plugin.
49 struct HTTP_Client_Plugin;
53 * Message to send using http
58 * next pointer for double linked list
60 struct HTTP_Message *next;
63 * previous pointer for double linked list
65 struct HTTP_Message *prev;
68 * buffer containing data to send
73 * amount of data already sent
83 * Continuation function to call once the transmission buffer
84 * has again space available. NULL if there is no
85 * continuation to call.
87 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
90 * Closure for transmit_cont.
92 void *transmit_cont_cls;
97 * Session handle for connections.
102 * To whom are we talking to (set to our identity
103 * if we are still waiting for the welcome message)
105 struct GNUNET_PeerIdentity target;
108 * Stored in a linked list.
110 struct Session *next;
113 * Stored in a linked list.
115 struct Session *prev;
128 * ATS network type in NBO
130 uint32_t ats_address_network_type;
133 * Pointer to the global plugin struct.
135 struct HTTP_Client_Plugin *plugin;
138 * Is client send handle paused since there are no data to send?
141 int client_put_paused;
144 * Was session given to transport service?
146 // int session_passed;
154 * Client receive handle
159 * next pointer for double linked list
161 struct HTTP_Message *msg_head;
164 * previous pointer for double linked list
166 struct HTTP_Message *msg_tail;
169 * Message stream tokenizer for incoming data
171 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
174 * Session timeout task
176 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
179 * Task to wake up client receive handle when receiving is allowed again
181 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
184 * Absolute time when to receive data again
185 * Used for receive throttling
187 struct GNUNET_TIME_Absolute next_receive;
192 * Encapsulation of all of the state of the plugin.
194 struct HTTP_Client_Plugin
199 struct GNUNET_TRANSPORT_PluginEnvironment *env;
202 * Linked list head of open sessions.
204 struct Session *head;
207 * Linked list tail of open sessions.
209 struct Session *tail;
222 * Maximum number of sockets the plugin can use
223 * Each http inbound /outbound connections are two connections
225 unsigned int max_connections;
228 * Current number of sockets the plugin can use
229 * Each http inbound /outbound connections are two connections
231 unsigned int cur_connections;
234 * Last used unique HTTP connection tag
251 CURLM *curl_multi_handle;
256 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
261 * Encapsulation of all of the state of the plugin.
263 struct HTTP_Client_Plugin *p;
267 * Start session timeout for a session
268 * @param s the session
271 client_start_session_timeout (struct Session *s);
275 * Increment session timeout due to activity for a session
276 * @param s the session
279 client_reschedule_session_timeout (struct Session *s);
283 * Cancel timeout for a session
284 * @param s the session
287 client_stop_session_timeout (struct Session *s);
291 * Function setting up file descriptors and scheduling task to run
293 * @param plugin plugin as closure
294 * @param now schedule task in 1ms, regardless of what curl may say
295 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
298 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
302 * Does a session s exists?
304 * @param plugin the plugin
305 * @param s desired session
306 * @return GNUNET_YES or GNUNET_NO
309 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
311 struct Session * head;
313 GNUNET_assert (NULL != plugin);
314 GNUNET_assert (NULL != s);
316 for (head = plugin->head; head != NULL; head = head->next)
326 * Function to log curl debug messages with GNUNET_log
329 * @param type curl_infotype
336 client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
339 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
347 case CURLINFO_HEADER_IN:
350 case CURLINFO_HEADER_OUT:
351 ttype = "HEADER_OUT";
357 memcpy (text, data, size);
358 if (text[size - 1] == '\n')
363 text[size + 1] = '\0';
366 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
367 "Connection %p %s: %s", cls, ttype, text);
369 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
370 "Connection %p %s: %s", cls, ttype, text);
379 * Function that can be used by the transport service to transmit
380 * a message using the plugin. Note that in the case of a
381 * peer disconnecting, the continuation MUST be called
382 * prior to the disconnect notification itself. This function
383 * will be called with this peer's HELLO message to initiate
384 * a fresh connection to another peer.
387 * @param session which session must be used
388 * @param msgbuf the message to transmit
389 * @param msgbuf_size number of bytes in 'msgbuf'
390 * @param priority how important is the message (most plugins will
391 * ignore message priority and just FIFO)
392 * @param to how long to wait at most for the transmission (does not
393 * require plugins to discard the message after the timeout,
394 * just advisory for the desired delay; most plugins will ignore
396 * @param cont continuation to call once the message has
397 * been transmitted (or if the transport is ready
398 * for the next transmission call; or if the
399 * peer disconnected...); can be NULL
400 * @param cont_cls closure for cont
401 * @return number of bytes used (on the physical network, with overheads);
402 * -1 on hard errors (i.e. address invalid); 0 is a legal value
403 * and does NOT mean that the message was not transmitted (DV)
406 http_client_plugin_send (void *cls,
407 struct Session *session,
408 const char *msgbuf, size_t msgbuf_size,
409 unsigned int priority,
410 struct GNUNET_TIME_Relative to,
411 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
413 struct HTTP_Client_Plugin *plugin = cls;
414 struct HTTP_Message *msg;
416 GNUNET_assert (plugin != NULL);
417 GNUNET_assert (session != NULL);
419 /* lookup if session is really existing */
420 if (GNUNET_YES != client_exist_session (plugin, session))
423 return GNUNET_SYSERR;
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
427 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
428 session, session->client_put,
429 msgbuf_size, GNUNET_i2s (&session->target));
431 /* create new message and schedule */
432 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
434 msg->size = msgbuf_size;
436 msg->buf = (char *) &msg[1];
437 msg->transmit_cont = cont;
438 msg->transmit_cont_cls = cont_cls;
439 memcpy (msg->buf, msgbuf, msgbuf_size);
440 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
442 if (session->client_put_paused == GNUNET_YES)
444 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
445 "Session %p/connection %p: unpausing connection\n",
446 session, session->client_put);
447 session->client_put_paused = GNUNET_NO;
448 curl_easy_pause (session->client_put, CURLPAUSE_CONT);
450 client_schedule (session->plugin, GNUNET_YES);
451 client_reschedule_session_timeout (session);
459 * @param s the session to delete
462 client_delete_session (struct Session *s)
464 struct HTTP_Client_Plugin *plugin = s->plugin;
465 struct HTTP_Message *pos;
466 struct HTTP_Message *next;
468 client_stop_session_timeout (s);
470 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
473 while (NULL != (pos = next))
476 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
477 if (pos->transmit_cont != NULL)
478 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR);
482 if (s->msg_tk != NULL)
484 GNUNET_SERVER_mst_destroy (s->msg_tk);
487 GNUNET_free (s->addr);
494 * Disconnect a session
497 * @return GNUNET_OK on success, GNUNET_SYSERR on error
500 client_disconnect (struct Session *s)
502 struct HTTP_Client_Plugin *plugin = s->plugin;
503 struct HTTP_Message *msg;
504 struct HTTP_Message *t;
508 if (GNUNET_YES != client_exist_session (plugin, s))
511 return GNUNET_SYSERR;
514 if (s->client_put != NULL)
516 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
517 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
518 s, s->client_put, GNUNET_i2s (&s->target));
520 /* remove curl handle from multi handle */
521 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
522 if (mret != CURLM_OK)
524 /* clean up easy handle, handle is now invalid and free'd */
528 curl_easy_cleanup (s->client_put);
529 s->client_put = NULL;
533 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
535 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
536 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
539 if (s->client_get != NULL)
541 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
542 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
543 s, s->client_get, GNUNET_i2s (&s->target));
545 /* remove curl handle from multi handle */
546 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
547 if (mret != CURLM_OK)
549 /* clean up easy handle, handle is now invalid and free'd */
553 curl_easy_cleanup (s->client_get);
554 s->client_get = NULL;
561 if (NULL != msg->transmit_cont)
562 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
563 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
568 GNUNET_assert (plugin->cur_connections >= 2);
569 plugin->cur_connections -= 2;
570 GNUNET_STATISTICS_set (plugin->env->stats,
571 "# HTTP client sessions",
572 plugin->cur_connections,
575 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
576 "Session %p: notifying transport about ending session\n",s);
578 plugin->env->session_end (plugin->env->cls, &s->target, s);
579 client_delete_session (s);
581 /* Re-schedule since handles have changed */
582 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
584 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
585 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
587 client_schedule (plugin, GNUNET_YES);
594 * Function that can be used to force the plugin to disconnect
595 * from the given peer and cancel all previous transmissions
596 * (and their continuationc).
599 * @param target peer from which to disconnect
602 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
604 struct HTTP_Client_Plugin *plugin = cls;
605 struct Session *next = NULL;
606 struct Session *pos = NULL;
608 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
609 "Transport tells me to disconnect `%s'\n",
610 GNUNET_i2s (target));
613 while (NULL != (pos = next))
616 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
618 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
619 "Disconnecting session %p to `%pos'\n",
620 pos, GNUNET_i2s (target));
621 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
628 static struct Session *
629 client_lookup_session (struct HTTP_Client_Plugin *plugin,
630 const struct GNUNET_HELLO_Address *address)
634 for (pos = plugin->head; NULL != pos; pos = pos->next)
635 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
636 (address->address_length == pos->addrlen) &&
637 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
644 * Callback method used with libcurl
645 * Method is called when libcurl needs to read data during sending
647 * @param stream pointer where to write data
648 * @param size size of an individual element
649 * @param nmemb count of elements that can be written to the buffer
650 * @param cls source pointer, passed to the libcurl handle
651 * @return bytes written to stream, returning 0 will terminate connection!
654 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
656 struct Session *s = cls;
657 struct HTTP_Client_Plugin *plugin = s->plugin;
658 struct HTTP_Message *msg = s->msg_head;
661 if (GNUNET_YES != client_exist_session (plugin, s))
668 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
669 "Session %p/connection %p: nothing to send, suspending\n",
671 s->client_put_paused = GNUNET_YES;
672 return CURL_READFUNC_PAUSE;
675 GNUNET_assert (msg->pos < msg->size);
676 /* calculate how much fits in buffer */
677 len = GNUNET_MIN (msg->size - msg->pos,
679 memcpy (stream, &msg->buf[msg->pos], len);
681 if (msg->pos == msg->size)
683 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
684 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
685 s, s->client_put, msg->size, msg->pos);
686 /* Calling transmit continuation */
687 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
688 if (NULL != msg->transmit_cont)
689 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
692 client_reschedule_session_timeout (s);
698 * Wake up a curl handle which was suspended
700 * @param cls the session
701 * @param tc task context
704 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
706 struct Session *s = cls;
708 if (GNUNET_YES != client_exist_session(p, s))
713 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
714 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
716 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
717 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
718 if (s->client_get != NULL)
719 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
724 * Callback for message stream tokenixer
726 * @param cls the session
727 * @param client not used
728 * @param message the message received
729 * @return always GNUNET_OK
732 client_receive_mst_cb (void *cls, void *client,
733 const struct GNUNET_MessageHeader *message)
735 struct Session *s = cls;
736 struct HTTP_Client_Plugin *plugin;
737 struct GNUNET_TIME_Relative delay;
738 struct GNUNET_ATS_Information atsi[2];
740 if (GNUNET_YES != client_exist_session(p, s))
748 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
749 atsi[0].value = htonl (1);
750 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
751 atsi[1].value = s->ats_address_network_type;
752 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
754 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
755 (const struct GNUNET_ATS_Information *) &atsi, 2,
756 s, s->addr, s->addrlen);
758 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
760 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
763 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
764 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
765 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
768 client_reschedule_session_timeout (s);
775 * Callback method used with libcurl
776 * Method is called when libcurl needs to write data during sending
778 * @param stream pointer where to write data
779 * @param size size of an individual element
780 * @param nmemb count of elements that can be written to the buffer
781 * @param cls destination pointer, passed to the libcurl handle
782 * @return bytes read from stream
785 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
787 struct Session *s = cls;
788 struct GNUNET_TIME_Absolute now;
789 size_t len = size * nmemb;
790 struct HTTP_Client_Plugin *plugin = s->plugin;
792 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
793 "Received %u bytes from peer `%s'\n", len,
794 GNUNET_i2s (&s->target));
795 now = GNUNET_TIME_absolute_get ();
796 if (now.abs_value < s->next_receive.abs_value)
798 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
799 struct GNUNET_TIME_Relative delta =
800 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
801 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
802 "No inbound bandwidth for session %p available! Next read was delayed for %llu ms\n",
804 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
806 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
807 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
809 s->recv_wakeup_task =
810 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
811 return CURLPAUSE_ALL;
813 if (NULL == s->msg_tk)
814 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
815 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
822 * Task performing curl operations
824 * @param cls plugin as closure
825 * @param tc gnunet scheduler task context
828 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
832 * Function setting up file descriptors and scheduling task to run
834 * @param plugin plugin as closure
835 * @param now schedule task in 1ms, regardless of what curl may say
836 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
839 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
845 struct GNUNET_NETWORK_FDSet *grs;
846 struct GNUNET_NETWORK_FDSet *gws;
849 struct GNUNET_TIME_Relative timeout;
851 /* Cancel previous scheduled task */
852 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
854 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
855 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
861 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
862 if (mret != CURLM_OK)
864 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
865 "curl_multi_fdset", __FILE__, __LINE__,
866 curl_multi_strerror (mret));
867 return GNUNET_SYSERR;
869 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
871 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
873 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
874 if (now == GNUNET_YES)
875 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
877 if (mret != CURLM_OK)
879 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
880 "curl_multi_timeout", __FILE__, __LINE__,
881 curl_multi_strerror (mret));
882 return GNUNET_SYSERR;
885 grs = GNUNET_NETWORK_fdset_create ();
886 gws = GNUNET_NETWORK_fdset_create ();
887 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
888 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
890 plugin->client_perform_task =
891 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
893 &client_run, plugin);
894 GNUNET_NETWORK_fdset_destroy (gws);
895 GNUNET_NETWORK_fdset_destroy (grs);
901 * Task performing curl operations
903 * @param cls plugin as closure
904 * @param tc gnunet scheduler task context
907 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
909 struct HTTP_Client_Plugin *plugin = cls;
913 GNUNET_assert (cls != NULL);
915 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
916 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
922 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
927 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
929 CURL *easy_h = msg->easy_handle;
930 struct Session *s = NULL;
931 char *d = (char *) s;
936 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
937 "Client: connection to ended with reason %i: `%s', %i handles running\n",
939 curl_easy_strerror (msg->data.result), running);
943 GNUNET_assert (CURLE_OK ==
944 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
945 s = (struct Session *) d;
947 if (GNUNET_YES != client_exist_session(plugin, s))
953 GNUNET_assert (s != NULL);
954 if (msg->msg == CURLMSG_DONE)
956 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
957 "Session %p/connection %p: connection to `%s' ended with reason %i: `%s'\n",
958 s, msg->easy_handle, GNUNET_i2s (&s->target),
960 curl_easy_strerror (msg->data.result));
962 /* Disconnect other transmission direction and tell transport */
963 client_disconnect (s);
967 while (mret == CURLM_CALL_MULTI_PERFORM);
968 client_schedule (plugin, GNUNET_NO);
972 client_connect (struct Session *s)
975 struct HTTP_Client_Plugin *plugin = s->plugin;
981 if (NULL == http_common_plugin_address_to_string (NULL, s->addr, s->addrlen))
983 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
984 "Invalid address peer `%s'\n",
985 GNUNET_i2s (&s->target));
986 return GNUNET_SYSERR;
989 GNUNET_asprintf (&url, "%s/%s;%u",
990 http_common_plugin_address_to_string (plugin, s->addr, s->addrlen),
991 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
994 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
995 "Initiating outbound session peer `%s' using address `%s'\n",
996 GNUNET_i2s (&s->target), url);
998 /* create get connection */
999 s->client_get = curl_easy_init ();
1001 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1002 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1003 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
1006 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1007 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1008 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1010 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
1011 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1012 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1013 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1014 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1015 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1016 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1017 /* No timeout by default, timeout done with session timeout */
1018 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1019 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1020 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1021 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1022 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1023 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1024 #if CURL_TCP_NODELAY
1025 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1028 /* create put connection */
1029 s->client_put = curl_easy_init ();
1031 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1032 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1033 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
1036 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1037 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1038 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1040 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
1041 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1043 struct curl_slist *m_headerlist;
1044 m_headerlist = NULL;
1045 m_headerlist = curl_slist_append(m_headerlist, "Transfer-Encoding: chunked");
1046 curl_easy_setopt(s->client_put, CURLOPT_HTTPHEADER, m_headerlist);*/
1047 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1048 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1049 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1050 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1051 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
1052 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1053 /* No timeout by default, timeout done with session timeout */
1054 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1055 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1056 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1057 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1058 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1059 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1060 #if CURL_TCP_NODELAY
1061 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1065 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_get);
1066 if (mret != CURLM_OK)
1068 curl_easy_cleanup (s->client_get);
1070 return GNUNET_SYSERR;
1073 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_put);
1074 if (mret != CURLM_OK)
1076 curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
1077 curl_easy_cleanup (s->client_get);
1078 curl_easy_cleanup (s->client_put);
1080 return GNUNET_SYSERR;
1083 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1084 "Session %p: connected with connections GET %p and PUT %p\n",
1085 s, s->client_get, s->client_put);
1087 /* Perform connect */
1088 plugin->cur_connections += 2;
1089 GNUNET_STATISTICS_set (plugin->env->stats,
1090 "# HTTP client connections",
1091 plugin->cur_connections,
1094 /* Re-schedule since handles have changed */
1095 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1097 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1098 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1100 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1106 * Creates a new outbound session the transport service will use to send data to the
1109 * @param cls the plugin
1110 * @param address the address
1111 * @return the session or NULL of max connections exceeded
1113 static struct Session *
1114 http_client_plugin_get_session (void *cls,
1115 const struct GNUNET_HELLO_Address *address)
1117 struct HTTP_Client_Plugin *plugin = cls;
1118 struct Session * s = NULL;
1119 struct sockaddr *sa;
1120 struct GNUNET_ATS_Information ats;
1124 GNUNET_assert (plugin != NULL);
1125 GNUNET_assert (address != NULL);
1126 GNUNET_assert (address->address != NULL);
1128 /* find existing session */
1129 s = client_lookup_session (plugin, address);
1133 if (plugin->max_connections <= plugin->cur_connections)
1135 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1136 "Maximum number of connections (%u) reached: "
1137 "cannot connect to peer `%s'\n",
1138 plugin->max_connections,
1139 GNUNET_i2s (&address->peer));
1143 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1144 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1145 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1147 if (GNUNET_SYSERR == res)
1151 else if (GNUNET_YES == res)
1153 GNUNET_assert (NULL != sa);
1154 if (AF_INET == sa->sa_family)
1156 salen = sizeof (struct sockaddr_in);
1158 else if (AF_INET == sa->sa_family)
1160 salen = sizeof (struct sockaddr_in6);
1162 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1165 else if (GNUNET_NO == res)
1167 ats.value = htonl (GNUNET_ATS_COST_WAN);
1170 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1176 s = GNUNET_malloc (sizeof (struct Session));
1177 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1179 s->addr = GNUNET_malloc (address->address_length);
1180 memcpy (s->addr, address->address, address->address_length);
1181 s->addrlen = address->address_length;
1182 s->ats_address_network_type = ats.value;
1184 client_start_session_timeout (s);
1186 /* add new session */
1187 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1189 /* initiate new connection */
1190 if (GNUNET_SYSERR == client_connect (s))
1192 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1193 "Cannot connect to peer `%s' address `%s''\n",
1194 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1195 GNUNET_i2s (&s->target));
1196 client_delete_session (s);
1204 * Setup http_client plugin
1206 * @param plugin the plugin handle
1207 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1210 client_start (struct HTTP_Client_Plugin *plugin)
1212 curl_global_init (CURL_GLOBAL_ALL);
1213 plugin->curl_multi_handle = curl_multi_init ();
1215 if (NULL == plugin->curl_multi_handle)
1217 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1218 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1220 return GNUNET_SYSERR;
1226 * Session was idle, so disconnect it
1229 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1231 GNUNET_assert (NULL != cls);
1232 struct Session *s = cls;
1234 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1235 GNUNET_log (TIMEOUT_LOG,
1236 "Session %p was idle for %llu ms, disconnecting\n",
1237 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1239 /* call session destroy function */
1240 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1245 * Start session timeout for session s
1247 * @param s the session
1250 client_start_session_timeout (struct Session *s)
1253 GNUNET_assert (NULL != s);
1254 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1255 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1256 &client_session_timeout,
1258 GNUNET_log (TIMEOUT_LOG,
1259 "Timeout for session %p set to %llu ms\n",
1260 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1265 * Increment session timeout due to activity for session s
1267 * param s the session
1270 client_reschedule_session_timeout (struct Session *s)
1273 GNUNET_assert (NULL != s);
1274 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1276 GNUNET_SCHEDULER_cancel (s->timeout_task);
1277 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1278 &client_session_timeout,
1280 GNUNET_log (TIMEOUT_LOG,
1281 "Timeout rescheduled for session %p set to %llu ms\n",
1282 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1287 * Cancel timeout due to activity for session s
1289 * param s the session
1292 client_stop_session_timeout (struct Session *s)
1294 GNUNET_assert (NULL != s);
1296 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1298 GNUNET_SCHEDULER_cancel (s->timeout_task);
1299 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1300 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1306 * Another peer has suggested an address for this
1307 * peer and transport plugin. Check that this could be a valid
1308 * address. If so, consider adding it to the list
1311 * @param cls closure
1312 * @param addr pointer to the address
1313 * @param addrlen length of addr
1314 * @return GNUNET_OK if this is a plausible address for this peer
1318 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1320 /* struct Plugin *plugin = cls; */
1322 /* A HTTP/S client does not have any valid address so:*/
1328 * Exit point from the plugin.
1330 * @param cls api as closure
1334 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1336 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1337 struct HTTP_Client_Plugin *plugin = api->cls;
1338 struct Session *pos;
1339 struct Session *next;
1341 if (NULL == api->cls)
1348 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1349 _("Shutting down plugin `%s'\n"),
1353 next = plugin->head;
1354 while (NULL != (pos = next))
1357 client_disconnect (pos);
1359 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1361 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1362 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1366 if (NULL != plugin->curl_multi_handle)
1368 curl_multi_cleanup (plugin->curl_multi_handle);
1369 plugin->curl_multi_handle = NULL;
1371 curl_global_cleanup ();
1373 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1374 _("Shutdown for plugin `%s' complete\n"),
1377 GNUNET_free (plugin);
1386 * @param plugin the plugin handle
1387 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1390 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1392 unsigned long long max_connections;
1394 /* Optional parameters */
1395 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1397 "MAX_CONNECTIONS", &max_connections))
1398 max_connections = 128;
1399 plugin->max_connections = max_connections;
1401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1402 _("Maximum number of connections is %u\n"),
1403 plugin->max_connections);
1408 * Entry point for the plugin.
1411 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1413 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1414 struct GNUNET_TRANSPORT_PluginFunctions *api;
1415 struct HTTP_Client_Plugin *plugin;
1417 if (NULL == env->receive)
1419 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1420 initialze the plugin or the API */
1421 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1423 api->address_to_string = &http_common_plugin_address_to_string;
1424 api->string_to_address = &http_common_plugin_string_to_address;
1425 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1429 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1432 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1434 api->send = &http_client_plugin_send;
1435 api->disconnect = &http_client_plugin_disconnect;
1436 api->check_address = &http_client_plugin_address_suggested;
1437 api->get_session = &http_client_plugin_get_session;
1438 api->address_to_string = &http_common_plugin_address_to_string;
1439 api->string_to_address = &http_common_plugin_string_to_address;
1440 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1444 plugin->name = "transport-https_client";
1445 plugin->protocol = "https";
1447 plugin->name = "transport-http_client";
1448 plugin->protocol = "http";
1450 plugin->last_tag = 1;
1452 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1454 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1459 if (GNUNET_SYSERR == client_start (plugin))
1461 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1467 /* end of plugin_transport_http_client.c */