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
36 #define VERBOSE_CURL GNUNET_NO
39 #include "gnunet_protocols.h"
40 #include "gnunet_connection_lib.h"
41 #include "gnunet_server_lib.h"
42 #include "gnunet_service_lib.h"
43 #include "gnunet_statistics_service.h"
44 #include "gnunet_transport_service.h"
45 #include "gnunet_transport_plugin.h"
46 #include "plugin_transport_http_common.h"
47 #include <curl/curl.h>
51 * Encapsulation of all of the state of the plugin.
53 struct HTTP_Client_Plugin;
57 * Message to send using http
62 * next pointer for double linked list
64 struct HTTP_Message *next;
67 * previous pointer for double linked list
69 struct HTTP_Message *prev;
72 * buffer containing data to send
77 * amount of data already sent
87 * Continuation function to call once the transmission buffer
88 * has again space available. NULL if there is no
89 * continuation to call.
91 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
94 * Closure for transmit_cont.
96 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;
132 * ATS network type in NBO
134 uint32_t ats_address_network_type;
137 * Pointer to the global plugin struct.
139 struct HTTP_Client_Plugin *plugin;
142 * Is client send handle paused since there are no data to send?
145 int client_put_paused;
148 * Was session given to transport service?
158 * Client receive handle
163 * next pointer for double linked list
165 struct HTTP_Message *msg_head;
168 * previous pointer for double linked list
170 struct HTTP_Message *msg_tail;
173 * Message stream tokenizer for incoming data
175 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
178 * Session timeout task
180 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
183 * Task to wake up client receive handle when receiving is allowed again
185 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
188 * Absolute time when to receive data again
189 * Used for receive throttling
191 struct GNUNET_TIME_Absolute next_receive;
195 * Encapsulation of all of the state of the plugin.
197 struct HTTP_Client_Plugin
202 struct GNUNET_TRANSPORT_PluginEnvironment *env;
205 * Linked list head of open sessions.
207 struct Session *head;
210 * Linked list tail of open sessions.
212 struct Session *tail;
225 * Maximum number of sockets the plugin can use
226 * Each http inbound /outbound connections are two connections
228 unsigned int max_connections;
231 * Current number of sockets the plugin can use
232 * Each http inbound /outbound connections are two connections
234 unsigned int cur_connections;
237 * Last used unique HTTP connection tag
254 CURLM *curl_multi_handle;
259 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
263 * Encapsulation of all of the state of the plugin.
265 struct HTTP_Client_Plugin *p;
268 * Start session timeout
271 client_start_session_timeout (struct Session *s);
274 * Increment session timeout due to activity
277 client_reschedule_session_timeout (struct Session *s);
283 client_stop_session_timeout (struct Session *s);
286 * Function setting up file descriptors and scheduling task to run
288 * @param plugin plugin as closure
289 * @param now schedule task in 1ms, regardless of what curl may say
290 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
293 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
297 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
299 struct Session * head;
301 GNUNET_assert (NULL != plugin);
302 GNUNET_assert (NULL != s);
304 for (head = plugin->head; head != NULL; head = head->next)
314 * Function to log curl debug messages with GNUNET_log
316 * @param type curl_infotype
323 client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
325 if (type == CURLINFO_TEXT)
329 memcpy (text, data, size);
330 if (text[size - 1] == '\n')
335 text[size + 1] = '\0';
338 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
339 "Connection: %p - %s", cls, text);
341 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
342 "Connection %p: - %s", cls, text);
350 * Function that can be used by the transport service to transmit
351 * a message using the plugin. Note that in the case of a
352 * peer disconnecting, the continuation MUST be called
353 * prior to the disconnect notification itself. This function
354 * will be called with this peer's HELLO message to initiate
355 * a fresh connection to another peer.
358 * @param session which session must be used
359 * @param msgbuf the message to transmit
360 * @param msgbuf_size number of bytes in 'msgbuf'
361 * @param priority how important is the message (most plugins will
362 * ignore message priority and just FIFO)
363 * @param to how long to wait at most for the transmission (does not
364 * require plugins to discard the message after the timeout,
365 * just advisory for the desired delay; most plugins will ignore
367 * @param cont continuation to call once the message has
368 * been transmitted (or if the transport is ready
369 * for the next transmission call; or if the
370 * peer disconnected...); can be NULL
371 * @param cont_cls closure for cont
372 * @return number of bytes used (on the physical network, with overheads);
373 * -1 on hard errors (i.e. address invalid); 0 is a legal value
374 * and does NOT mean that the message was not transmitted (DV)
377 http_client_plugin_send (void *cls,
378 struct Session *session,
379 const char *msgbuf, size_t msgbuf_size,
380 unsigned int priority,
381 struct GNUNET_TIME_Relative to,
382 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
384 struct HTTP_Client_Plugin *plugin = cls;
385 struct HTTP_Message *msg;
387 GNUNET_assert (plugin != NULL);
388 GNUNET_assert (session != NULL);
390 /* lookup if session is really existing */
391 if (GNUNET_YES != client_exist_session (plugin, session))
394 return GNUNET_SYSERR;
397 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
398 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
399 session, session->client_put,
400 msgbuf_size, GNUNET_i2s (&session->target));
402 /* create new message and schedule */
403 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
405 msg->size = msgbuf_size;
407 msg->buf = (char *) &msg[1];
408 msg->transmit_cont = cont;
409 msg->transmit_cont_cls = cont_cls;
410 memcpy (msg->buf, msgbuf, msgbuf_size);
411 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
413 if (session->client_put_paused == GNUNET_YES)
415 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
416 "Session %p/connection %p: unpausing connection\n",
417 session, session->client_put);
418 session->client_put_paused = GNUNET_NO;
419 curl_easy_pause (session->client_put, CURLPAUSE_CONT);
421 client_schedule (session->plugin, GNUNET_YES);
422 client_reschedule_session_timeout (session);
428 client_delete_session (struct Session *s)
430 struct HTTP_Client_Plugin *plugin = s->plugin;
431 struct HTTP_Message *pos = s->msg_head;
432 struct HTTP_Message *next = NULL;
434 client_stop_session_timeout (s);
436 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
438 while (NULL != (pos = next))
441 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
442 if (pos->transmit_cont != NULL)
443 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR);
447 if (s->msg_tk != NULL)
449 GNUNET_SERVER_mst_destroy (s->msg_tk);
452 GNUNET_free (s->addr);
459 * Disconnect a session
462 * @return GNUNET_OK on success, GNUNET_SYSERR on error
465 client_disconnect (struct Session *s)
467 struct HTTP_Client_Plugin *plugin = s->plugin;
468 struct HTTP_Message *msg;
469 struct HTTP_Message *t;
473 if (GNUNET_YES != client_exist_session (plugin, s))
476 return GNUNET_SYSERR;
479 if (s->client_put != NULL)
481 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
482 "Session %p/connection %p: disconnecting PUT connectionto peer `%s'\n",
483 s, s->client_put, GNUNET_i2s (&s->target));
485 /* remove curl handle from multi handle */
486 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
487 if (mret != CURLM_OK)
489 /* clean up easy handle, handle is now invalid and free'd */
493 curl_easy_cleanup (s->client_put);
494 s->client_put = NULL;
498 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
500 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
501 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
504 if (s->client_get != NULL)
506 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
507 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
508 s, s->client_get, GNUNET_i2s (&s->target));
510 /* remove curl handle from multi handle */
511 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
512 if (mret != CURLM_OK)
514 /* clean up easy handle, handle is now invalid and free'd */
518 curl_easy_cleanup (s->client_get);
519 s->client_get = NULL;
526 if (NULL != msg->transmit_cont)
527 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
528 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
533 GNUNET_assert (plugin->cur_connections >= 2);
534 plugin->cur_connections -= 2;
535 GNUNET_STATISTICS_set (plugin->env->stats,
536 "# HTTP client sessions",
537 plugin->cur_connections,
540 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
541 "Session %p: notifying transport about ending session\n",s);
543 plugin->env->session_end (plugin->env->cls, &s->target, s);
544 client_delete_session (s);
546 /* Re-schedule since handles have changed */
547 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
549 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
550 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
552 client_schedule (plugin, GNUNET_YES);
559 * Function that can be used to force the plugin to disconnect
560 * from the given peer and cancel all previous transmissions
561 * (and their continuationc).
564 * @param target peer from which to disconnect
567 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
569 struct HTTP_Client_Plugin *plugin = cls;
570 struct Session *next = NULL;
571 struct Session *pos = NULL;
573 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
574 "Transport tells me to disconnect `%s'\n",
575 GNUNET_i2s (target));
578 while (NULL != (pos = next))
581 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
583 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
584 "Disconnecting session %p to `%pos'\n",
585 pos, GNUNET_i2s (target));
586 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
592 static struct Session *
593 client_lookup_session (struct HTTP_Client_Plugin *plugin,
594 const struct GNUNET_HELLO_Address *address)
598 for (pos = plugin->head; NULL != pos; pos = pos->next)
599 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
600 (address->address_length == pos->addrlen) &&
601 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
607 * Callback method used with libcurl
608 * Method is called when libcurl needs to read data during sending
610 * @param stream pointer where to write data
611 * @param size size of an individual element
612 * @param nmemb count of elements that can be written to the buffer
613 * @param cls source pointer, passed to the libcurl handle
614 * @return bytes written to stream, returning 0 will terminate connection!
617 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
619 struct Session *s = cls;
620 struct HTTP_Client_Plugin *plugin = s->plugin;
621 struct HTTP_Message *msg = s->msg_head;
624 if (GNUNET_YES != client_exist_session (plugin, s))
631 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
632 "Session %p/connection %p: nothing to send, suspending\n",
634 s->client_put_paused = GNUNET_YES;
635 return CURL_READFUNC_PAUSE;
638 GNUNET_assert (msg->pos < msg->size);
639 /* calculate how much fits in buffer */
640 len = GNUNET_MIN (msg->size - msg->pos,
642 memcpy (stream, &msg->buf[msg->pos], len);
644 if (msg->pos == msg->size)
646 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
647 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
648 s, s->client_put, msg->size, msg->pos);
649 /* Calling transmit continuation */
650 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
651 if (NULL != msg->transmit_cont)
652 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
655 client_reschedule_session_timeout (s);
661 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
663 struct Session *s = cls;
665 if (GNUNET_YES != client_exist_session(p, s))
670 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
671 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
673 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
674 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
675 if (s->client_get != NULL)
676 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
682 client_receive_mst_cb (void *cls, void *client,
683 const struct GNUNET_MessageHeader *message)
685 struct Session *s = cls;
686 struct HTTP_Client_Plugin *plugin;
687 struct GNUNET_TIME_Relative delay;
688 struct GNUNET_ATS_Information atsi[2];
690 if (GNUNET_YES != client_exist_session(p, s))
698 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
699 atsi[0].value = htonl (1);
700 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
701 atsi[1].value = s->ats_address_network_type;
702 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
704 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
705 (const struct GNUNET_ATS_Information *) &atsi, 2,
706 s, s->addr, s->addrlen);
708 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
710 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
713 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
714 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
715 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
718 client_reschedule_session_timeout (s);
725 * Callback method used with libcurl
726 * Method is called when libcurl needs to write data during sending
728 * @param stream pointer where to write data
729 * @param size size of an individual element
730 * @param nmemb count of elements that can be written to the buffer
731 * @param cls destination pointer, passed to the libcurl handle
732 * @return bytes read from stream
735 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
737 struct Session *s = cls;
738 struct GNUNET_TIME_Absolute now;
739 size_t len = size * nmemb;
740 struct HTTP_Client_Plugin *plugin = s->plugin;
742 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
743 "Received %u bytes from peer `%s'\n", len,
744 GNUNET_i2s (&s->target));
745 now = GNUNET_TIME_absolute_get ();
746 if (now.abs_value < s->next_receive.abs_value)
748 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
749 struct GNUNET_TIME_Relative delta =
750 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
751 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
752 "No inbound bandwidth for session %p available! Next read was delayed for %llu ms\n",
754 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
756 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
757 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
759 s->recv_wakeup_task =
760 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
761 return CURLPAUSE_ALL;
763 if (NULL == s->msg_tk)
764 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
765 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
771 * Task performing curl operations
773 * @param cls plugin as closure
774 * @param tc gnunet scheduler task context
777 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
780 * Function setting up file descriptors and scheduling task to run
782 * @param plugin plugin as closure
783 * @param now schedule task in 1ms, regardless of what curl may say
784 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
787 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
793 struct GNUNET_NETWORK_FDSet *grs;
794 struct GNUNET_NETWORK_FDSet *gws;
797 struct GNUNET_TIME_Relative timeout;
799 /* Cancel previous scheduled task */
800 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
802 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
803 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
809 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
810 if (mret != CURLM_OK)
812 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
813 "curl_multi_fdset", __FILE__, __LINE__,
814 curl_multi_strerror (mret));
815 return GNUNET_SYSERR;
817 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
819 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
821 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
822 if (now == GNUNET_YES)
823 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
825 if (mret != CURLM_OK)
827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
828 "curl_multi_timeout", __FILE__, __LINE__,
829 curl_multi_strerror (mret));
830 return GNUNET_SYSERR;
833 grs = GNUNET_NETWORK_fdset_create ();
834 gws = GNUNET_NETWORK_fdset_create ();
835 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
836 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
838 plugin->client_perform_task =
839 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
841 &client_run, plugin);
842 GNUNET_NETWORK_fdset_destroy (gws);
843 GNUNET_NETWORK_fdset_destroy (grs);
850 * Task performing curl operations
852 * @param cls plugin as closure
853 * @param tc gnunet scheduler task context
856 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
858 struct HTTP_Client_Plugin *plugin = cls;
862 GNUNET_assert (cls != NULL);
864 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
865 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
871 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
876 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
878 CURL *easy_h = msg->easy_handle;
879 struct Session *s = NULL;
880 char *d = (char *) s;
885 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
886 "Client: connection to ended with reason %i: `%s', %i handles running\n",
888 curl_easy_strerror (msg->data.result), running);
892 GNUNET_assert (CURLE_OK ==
893 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
894 s = (struct Session *) d;
896 if (GNUNET_YES != client_exist_session(plugin, s))
902 GNUNET_assert (s != NULL);
903 if (msg->msg == CURLMSG_DONE)
905 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
906 "Session %p/connection %p: connection to `%s' ended with reason %i: `%s'\n",
907 s, msg->easy_handle, GNUNET_i2s (&s->target),
909 curl_easy_strerror (msg->data.result));
911 /* Disconnect other transmission direction and tell transport */
912 client_disconnect (s);
916 while (mret == CURLM_CALL_MULTI_PERFORM);
917 client_schedule (plugin, GNUNET_NO);
922 client_connect (struct Session *s)
925 struct HTTP_Client_Plugin *plugin = s->plugin;
931 if (NULL == http_common_plugin_address_to_string (NULL, s->addr, s->addrlen))
933 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
934 "Invalid address peer `%s'\n",
935 GNUNET_i2s (&s->target));
936 return GNUNET_SYSERR;
939 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
940 "address peer `%s'\n",
941 http_common_plugin_address_to_string (plugin, s->addr, s->addrlen));
944 GNUNET_asprintf (&url, "%s/%s;%u",
945 http_common_plugin_address_to_string (plugin, s->addr, s->addrlen),
946 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
949 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
950 "Initiating outbound session peer `%s' using address `%s'\n",
951 GNUNET_i2s (&s->target), url);
953 /* create get connection */
954 s->client_get = curl_easy_init ();
956 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
957 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
958 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
961 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
962 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
963 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
965 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
966 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
967 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
968 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
969 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
970 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
971 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
972 /* No timeout by default, timeout done with session timeout */
973 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
974 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
975 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
976 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
977 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
978 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
980 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
983 /* create put connection */
984 s->client_put = curl_easy_init ();
986 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
987 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
988 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
991 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
992 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
993 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
995 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
996 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
997 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
998 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
999 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1000 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1001 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
1002 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1003 /* No timeout by default, timeout done with session timeout */
1004 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1005 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1006 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1007 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1008 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1009 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1010 #if CURL_TCP_NODELAY
1011 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1015 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_get);
1016 if (mret != CURLM_OK)
1018 curl_easy_cleanup (s->client_get);
1020 return GNUNET_SYSERR;
1023 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_put);
1024 if (mret != CURLM_OK)
1026 curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
1027 curl_easy_cleanup (s->client_get);
1028 curl_easy_cleanup (s->client_put);
1030 return GNUNET_SYSERR;
1033 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1034 "Session %p: connected with connections GET %p and PUT %p\n",
1035 s, s->client_get, s->client_put);
1037 /* Perform connect */
1038 plugin->cur_connections += 2;
1039 GNUNET_STATISTICS_set (plugin->env->stats,
1040 "# HTTP client connections",
1041 plugin->cur_connections,
1044 /* Re-schedule since handles have changed */
1045 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1047 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1048 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1050 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1056 * Creates a new outbound session the transport service will use to send data to the
1059 * @param cls the plugin
1060 * @param address the address
1061 * @return the session or NULL of max connections exceeded
1063 static struct Session *
1064 http_client_plugin_get_session (void *cls,
1065 const struct GNUNET_HELLO_Address *address)
1067 struct HTTP_Client_Plugin *plugin = cls;
1068 struct Session * s = NULL;
1069 struct sockaddr *sa;
1070 struct GNUNET_ATS_Information ats;
1074 GNUNET_assert (plugin != NULL);
1075 GNUNET_assert (address != NULL);
1076 GNUNET_assert (address->address != NULL);
1079 /* find existing session */
1080 s = client_lookup_session (plugin, address);
1084 if (plugin->max_connections <= plugin->cur_connections)
1086 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1087 "Maximum number of connections (%u) reached: "
1088 "cannot connect to peer `%s'\n",
1089 plugin->max_connections,
1090 GNUNET_i2s (&address->peer));
1094 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1095 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1096 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1100 if (GNUNET_SYSERR == res)
1104 else if (GNUNET_YES == res)
1106 GNUNET_assert (NULL != sa);
1107 if (AF_INET == sa->sa_family)
1109 salen = sizeof (struct sockaddr_in);
1111 else if (AF_INET == sa->sa_family)
1113 salen = sizeof (struct sockaddr_in6);
1115 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1118 else if (GNUNET_NO == res)
1120 ats.value = htonl (GNUNET_ATS_COST_WAN);
1123 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1129 s = GNUNET_malloc (sizeof (struct Session));
1130 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1132 s->addr = GNUNET_malloc (address->address_length);
1133 memcpy (s->addr, address->address, address->address_length);
1134 s->addrlen = address->address_length;
1135 s->ats_address_network_type = ats.value;
1137 client_start_session_timeout (s);
1139 /* add new session */
1140 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1142 /* initiate new connection */
1143 if (GNUNET_SYSERR == client_connect (s))
1145 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1146 "Cannot connect to peer `%s' address `%s''\n",
1147 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1148 GNUNET_i2s (&s->target));
1149 client_delete_session (s);
1156 client_start (struct HTTP_Client_Plugin *plugin)
1158 curl_global_init (CURL_GLOBAL_ALL);
1159 plugin->curl_multi_handle = curl_multi_init ();
1161 if (NULL == plugin->curl_multi_handle)
1163 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1164 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1166 return GNUNET_SYSERR;
1172 * Session was idle, so disconnect it
1175 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1177 GNUNET_assert (NULL != cls);
1178 struct Session *s = cls;
1180 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1181 GNUNET_log (TIMEOUT_LOG,
1182 "Session %p was idle for %llu ms, disconnecting\n",
1183 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1185 /* call session destroy function */
1186 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1190 * Start session timeout
1193 client_start_session_timeout (struct Session *s)
1196 GNUNET_assert (NULL != s);
1197 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1198 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1199 &client_session_timeout,
1201 GNUNET_log (TIMEOUT_LOG,
1202 "Timeout for session %p set to %llu ms\n",
1203 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1207 * Increment session timeout due to activity
1210 client_reschedule_session_timeout (struct Session *s)
1213 GNUNET_assert (NULL != s);
1214 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1216 GNUNET_SCHEDULER_cancel (s->timeout_task);
1217 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1218 &client_session_timeout,
1220 GNUNET_log (TIMEOUT_LOG,
1221 "Timeout rescheduled for session %p set to %llu ms\n",
1222 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1229 client_stop_session_timeout (struct Session *s)
1231 GNUNET_assert (NULL != s);
1233 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1235 GNUNET_SCHEDULER_cancel (s->timeout_task);
1236 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1237 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1243 * Another peer has suggested an address for this
1244 * peer and transport plugin. Check that this could be a valid
1245 * address. If so, consider adding it to the list
1248 * @param cls closure
1249 * @param addr pointer to the address
1250 * @param addrlen length of addr
1251 * @return GNUNET_OK if this is a plausible address for this peer
1255 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1257 /* struct Plugin *plugin = cls; */
1259 /* A HTTP/S client does not have any valid address so:*/
1264 * Exit point from the plugin.
1267 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1269 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1270 struct HTTP_Client_Plugin *plugin = api->cls;
1271 struct Session *pos;
1272 struct Session *next;
1274 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1275 _("Shutting down plugin `%s'\n"),
1278 if (NULL == api->cls)
1285 next = plugin->head;
1286 while (NULL != (pos = next))
1289 client_disconnect (pos);
1291 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1293 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1294 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1298 if (NULL != plugin->curl_multi_handle)
1300 curl_multi_cleanup (plugin->curl_multi_handle);
1301 plugin->curl_multi_handle = NULL;
1303 curl_global_cleanup ();
1305 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1306 _("Shutdown for plugin `%s' complete\n"),
1309 GNUNET_free (plugin);
1316 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1318 unsigned long long max_connections;
1320 /* Optional parameters */
1321 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1323 "MAX_CONNECTIONS", &max_connections))
1324 max_connections = 128;
1325 plugin->max_connections = max_connections;
1327 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1328 _("Maximum number of connections is %u\n"),
1329 plugin->max_connections);
1334 * Entry point for the plugin.
1337 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1339 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1340 struct GNUNET_TRANSPORT_PluginFunctions *api;
1341 struct HTTP_Client_Plugin *plugin;
1343 if (NULL == env->receive)
1345 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1346 initialze the plugin or the API */
1347 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1349 api->address_to_string = &http_common_plugin_address_to_string;
1350 api->string_to_address = &http_common_plugin_string_to_address;
1351 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1355 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1358 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1360 api->send = &http_client_plugin_send;
1361 api->disconnect = &http_client_plugin_disconnect;
1362 api->check_address = &http_client_plugin_address_suggested;
1363 api->get_session = &http_client_plugin_get_session;
1364 api->address_to_string = &http_common_plugin_address_to_string;
1365 api->string_to_address = &http_common_plugin_string_to_address;
1366 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1370 plugin->name = "transport-https_client";
1371 plugin->protocol = "https";
1373 plugin->name = "transport-http_client";
1374 plugin->protocol = "http";
1376 plugin->last_tag = 1;
1378 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1380 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1385 if (GNUNET_SYSERR == client_start (plugin))
1387 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1393 /* end of plugin_transport_http_client.c */