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;
432 struct HTTP_Message *next;
434 client_stop_session_timeout (s);
436 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
439 while (NULL != (pos = next))
442 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
443 if (pos->transmit_cont != NULL)
444 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR);
448 if (s->msg_tk != NULL)
450 GNUNET_SERVER_mst_destroy (s->msg_tk);
453 GNUNET_free (s->addr);
460 * Disconnect a session
463 * @return GNUNET_OK on success, GNUNET_SYSERR on error
466 client_disconnect (struct Session *s)
468 struct HTTP_Client_Plugin *plugin = s->plugin;
469 struct HTTP_Message *msg;
470 struct HTTP_Message *t;
474 if (GNUNET_YES != client_exist_session (plugin, s))
477 return GNUNET_SYSERR;
480 if (s->client_put != NULL)
482 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
483 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
484 s, s->client_put, GNUNET_i2s (&s->target));
486 /* remove curl handle from multi handle */
487 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
488 if (mret != CURLM_OK)
490 /* clean up easy handle, handle is now invalid and free'd */
494 curl_easy_cleanup (s->client_put);
495 s->client_put = NULL;
499 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
501 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
502 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
505 if (s->client_get != NULL)
507 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
508 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
509 s, s->client_get, GNUNET_i2s (&s->target));
511 /* remove curl handle from multi handle */
512 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
513 if (mret != CURLM_OK)
515 /* clean up easy handle, handle is now invalid and free'd */
519 curl_easy_cleanup (s->client_get);
520 s->client_get = NULL;
527 if (NULL != msg->transmit_cont)
528 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
529 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
534 GNUNET_assert (plugin->cur_connections >= 2);
535 plugin->cur_connections -= 2;
536 GNUNET_STATISTICS_set (plugin->env->stats,
537 "# HTTP client sessions",
538 plugin->cur_connections,
541 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
542 "Session %p: notifying transport about ending session\n",s);
544 plugin->env->session_end (plugin->env->cls, &s->target, s);
545 client_delete_session (s);
547 /* Re-schedule since handles have changed */
548 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
550 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
551 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
553 client_schedule (plugin, GNUNET_YES);
560 * Function that can be used to force the plugin to disconnect
561 * from the given peer and cancel all previous transmissions
562 * (and their continuationc).
565 * @param target peer from which to disconnect
568 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
570 struct HTTP_Client_Plugin *plugin = cls;
571 struct Session *next = NULL;
572 struct Session *pos = NULL;
574 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
575 "Transport tells me to disconnect `%s'\n",
576 GNUNET_i2s (target));
579 while (NULL != (pos = next))
582 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
584 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
585 "Disconnecting session %p to `%pos'\n",
586 pos, GNUNET_i2s (target));
587 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
593 static struct Session *
594 client_lookup_session (struct HTTP_Client_Plugin *plugin,
595 const struct GNUNET_HELLO_Address *address)
599 for (pos = plugin->head; NULL != pos; pos = pos->next)
600 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
601 (address->address_length == pos->addrlen) &&
602 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
608 * Callback method used with libcurl
609 * Method is called when libcurl needs to read data during sending
611 * @param stream pointer where to write data
612 * @param size size of an individual element
613 * @param nmemb count of elements that can be written to the buffer
614 * @param cls source pointer, passed to the libcurl handle
615 * @return bytes written to stream, returning 0 will terminate connection!
618 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
620 struct Session *s = cls;
621 struct HTTP_Client_Plugin *plugin = s->plugin;
622 struct HTTP_Message *msg = s->msg_head;
625 if (GNUNET_YES != client_exist_session (plugin, s))
632 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
633 "Session %p/connection %p: nothing to send, suspending\n",
635 s->client_put_paused = GNUNET_YES;
636 return CURL_READFUNC_PAUSE;
639 GNUNET_assert (msg->pos < msg->size);
640 /* calculate how much fits in buffer */
641 len = GNUNET_MIN (msg->size - msg->pos,
643 memcpy (stream, &msg->buf[msg->pos], len);
645 if (msg->pos == msg->size)
647 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
648 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
649 s, s->client_put, msg->size, msg->pos);
650 /* Calling transmit continuation */
651 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
652 if (NULL != msg->transmit_cont)
653 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
656 client_reschedule_session_timeout (s);
662 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
664 struct Session *s = cls;
666 if (GNUNET_YES != client_exist_session(p, s))
671 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
672 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
674 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
675 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
676 if (s->client_get != NULL)
677 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
683 client_receive_mst_cb (void *cls, void *client,
684 const struct GNUNET_MessageHeader *message)
686 struct Session *s = cls;
687 struct HTTP_Client_Plugin *plugin;
688 struct GNUNET_TIME_Relative delay;
689 struct GNUNET_ATS_Information atsi[2];
691 if (GNUNET_YES != client_exist_session(p, s))
699 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
700 atsi[0].value = htonl (1);
701 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
702 atsi[1].value = s->ats_address_network_type;
703 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
705 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
706 (const struct GNUNET_ATS_Information *) &atsi, 2,
707 s, s->addr, s->addrlen);
709 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
711 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
714 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
715 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
716 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
719 client_reschedule_session_timeout (s);
726 * Callback method used with libcurl
727 * Method is called when libcurl needs to write data during sending
729 * @param stream pointer where to write data
730 * @param size size of an individual element
731 * @param nmemb count of elements that can be written to the buffer
732 * @param cls destination pointer, passed to the libcurl handle
733 * @return bytes read from stream
736 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
738 struct Session *s = cls;
739 struct GNUNET_TIME_Absolute now;
740 size_t len = size * nmemb;
741 struct HTTP_Client_Plugin *plugin = s->plugin;
743 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
744 "Received %u bytes from peer `%s'\n", len,
745 GNUNET_i2s (&s->target));
746 now = GNUNET_TIME_absolute_get ();
747 if (now.abs_value < s->next_receive.abs_value)
749 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
750 struct GNUNET_TIME_Relative delta =
751 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
752 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
753 "No inbound bandwidth for session %p available! Next read was delayed for %llu ms\n",
755 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
757 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
758 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
760 s->recv_wakeup_task =
761 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
762 return CURLPAUSE_ALL;
764 if (NULL == s->msg_tk)
765 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
766 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
772 * Task performing curl operations
774 * @param cls plugin as closure
775 * @param tc gnunet scheduler task context
778 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
781 * Function setting up file descriptors and scheduling task to run
783 * @param plugin plugin as closure
784 * @param now schedule task in 1ms, regardless of what curl may say
785 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
788 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
794 struct GNUNET_NETWORK_FDSet *grs;
795 struct GNUNET_NETWORK_FDSet *gws;
798 struct GNUNET_TIME_Relative timeout;
800 /* Cancel previous scheduled task */
801 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
803 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
804 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
810 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
811 if (mret != CURLM_OK)
813 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
814 "curl_multi_fdset", __FILE__, __LINE__,
815 curl_multi_strerror (mret));
816 return GNUNET_SYSERR;
818 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
820 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
822 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
823 if (now == GNUNET_YES)
824 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
826 if (mret != CURLM_OK)
828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
829 "curl_multi_timeout", __FILE__, __LINE__,
830 curl_multi_strerror (mret));
831 return GNUNET_SYSERR;
834 grs = GNUNET_NETWORK_fdset_create ();
835 gws = GNUNET_NETWORK_fdset_create ();
836 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
837 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
839 plugin->client_perform_task =
840 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
842 &client_run, plugin);
843 GNUNET_NETWORK_fdset_destroy (gws);
844 GNUNET_NETWORK_fdset_destroy (grs);
851 * Task performing curl operations
853 * @param cls plugin as closure
854 * @param tc gnunet scheduler task context
857 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
859 struct HTTP_Client_Plugin *plugin = cls;
863 GNUNET_assert (cls != NULL);
865 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
866 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
872 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
877 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
879 CURL *easy_h = msg->easy_handle;
880 struct Session *s = NULL;
881 char *d = (char *) s;
886 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
887 "Client: connection to ended with reason %i: `%s', %i handles running\n",
889 curl_easy_strerror (msg->data.result), running);
893 GNUNET_assert (CURLE_OK ==
894 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
895 s = (struct Session *) d;
897 if (GNUNET_YES != client_exist_session(plugin, s))
903 GNUNET_assert (s != NULL);
904 if (msg->msg == CURLMSG_DONE)
906 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
907 "Session %p/connection %p: connection to `%s' ended with reason %i: `%s'\n",
908 s, msg->easy_handle, GNUNET_i2s (&s->target),
910 curl_easy_strerror (msg->data.result));
912 /* Disconnect other transmission direction and tell transport */
913 client_disconnect (s);
917 while (mret == CURLM_CALL_MULTI_PERFORM);
918 client_schedule (plugin, GNUNET_NO);
923 client_connect (struct Session *s)
926 struct HTTP_Client_Plugin *plugin = s->plugin;
932 if (NULL == http_common_plugin_address_to_string (NULL, s->addr, s->addrlen))
934 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
935 "Invalid address peer `%s'\n",
936 GNUNET_i2s (&s->target));
937 return GNUNET_SYSERR;
940 GNUNET_asprintf (&url, "%s/%s;%u",
941 http_common_plugin_address_to_string (plugin, s->addr, s->addrlen),
942 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
945 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
946 "Initiating outbound session peer `%s' using address `%s'\n",
947 GNUNET_i2s (&s->target), url);
949 /* create get connection */
950 s->client_get = curl_easy_init ();
952 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
953 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
954 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
957 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
958 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
959 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
961 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
962 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
963 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
964 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
965 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
966 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
967 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
968 /* No timeout by default, timeout done with session timeout */
969 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
970 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
971 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
972 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
973 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
974 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
976 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
979 /* create put connection */
980 s->client_put = curl_easy_init ();
982 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
983 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
984 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
987 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
988 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
989 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
991 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
992 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
993 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
994 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
995 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
996 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
997 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
998 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
999 /* No timeout by default, timeout done with session timeout */
1000 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1001 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1002 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1003 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1004 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1005 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1006 #if CURL_TCP_NODELAY
1007 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1011 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_get);
1012 if (mret != CURLM_OK)
1014 curl_easy_cleanup (s->client_get);
1016 return GNUNET_SYSERR;
1019 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_put);
1020 if (mret != CURLM_OK)
1022 curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
1023 curl_easy_cleanup (s->client_get);
1024 curl_easy_cleanup (s->client_put);
1026 return GNUNET_SYSERR;
1029 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1030 "Session %p: connected with connections GET %p and PUT %p\n",
1031 s, s->client_get, s->client_put);
1033 /* Perform connect */
1034 plugin->cur_connections += 2;
1035 GNUNET_STATISTICS_set (plugin->env->stats,
1036 "# HTTP client connections",
1037 plugin->cur_connections,
1040 /* Re-schedule since handles have changed */
1041 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1043 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1044 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1046 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1052 * Creates a new outbound session the transport service will use to send data to the
1055 * @param cls the plugin
1056 * @param address the address
1057 * @return the session or NULL of max connections exceeded
1059 static struct Session *
1060 http_client_plugin_get_session (void *cls,
1061 const struct GNUNET_HELLO_Address *address)
1063 struct HTTP_Client_Plugin *plugin = cls;
1064 struct Session * s = NULL;
1065 struct sockaddr *sa;
1066 struct GNUNET_ATS_Information ats;
1070 GNUNET_assert (plugin != NULL);
1071 GNUNET_assert (address != NULL);
1072 GNUNET_assert (address->address != NULL);
1075 /* find existing session */
1076 s = client_lookup_session (plugin, address);
1080 if (plugin->max_connections <= plugin->cur_connections)
1082 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1083 "Maximum number of connections (%u) reached: "
1084 "cannot connect to peer `%s'\n",
1085 plugin->max_connections,
1086 GNUNET_i2s (&address->peer));
1090 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1091 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1092 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1094 if (GNUNET_SYSERR == res)
1098 else if (GNUNET_YES == res)
1100 GNUNET_assert (NULL != sa);
1101 if (AF_INET == sa->sa_family)
1103 salen = sizeof (struct sockaddr_in);
1105 else if (AF_INET == sa->sa_family)
1107 salen = sizeof (struct sockaddr_in6);
1109 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1112 else if (GNUNET_NO == res)
1114 ats.value = htonl (GNUNET_ATS_COST_WAN);
1117 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1123 s = GNUNET_malloc (sizeof (struct Session));
1124 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1126 s->addr = GNUNET_malloc (address->address_length);
1127 memcpy (s->addr, address->address, address->address_length);
1128 s->addrlen = address->address_length;
1129 s->ats_address_network_type = ats.value;
1131 client_start_session_timeout (s);
1133 /* add new session */
1134 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1136 /* initiate new connection */
1137 if (GNUNET_SYSERR == client_connect (s))
1139 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1140 "Cannot connect to peer `%s' address `%s''\n",
1141 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1142 GNUNET_i2s (&s->target));
1143 client_delete_session (s);
1150 client_start (struct HTTP_Client_Plugin *plugin)
1152 curl_global_init (CURL_GLOBAL_ALL);
1153 plugin->curl_multi_handle = curl_multi_init ();
1155 if (NULL == plugin->curl_multi_handle)
1157 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1158 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1160 return GNUNET_SYSERR;
1166 * Session was idle, so disconnect it
1169 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1171 GNUNET_assert (NULL != cls);
1172 struct Session *s = cls;
1174 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1175 GNUNET_log (TIMEOUT_LOG,
1176 "Session %p was idle for %llu ms, disconnecting\n",
1177 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1179 /* call session destroy function */
1180 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1184 * Start session timeout
1187 client_start_session_timeout (struct Session *s)
1190 GNUNET_assert (NULL != s);
1191 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1192 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1193 &client_session_timeout,
1195 GNUNET_log (TIMEOUT_LOG,
1196 "Timeout for session %p set to %llu ms\n",
1197 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1201 * Increment session timeout due to activity
1204 client_reschedule_session_timeout (struct Session *s)
1207 GNUNET_assert (NULL != s);
1208 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1210 GNUNET_SCHEDULER_cancel (s->timeout_task);
1211 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1212 &client_session_timeout,
1214 GNUNET_log (TIMEOUT_LOG,
1215 "Timeout rescheduled for session %p set to %llu ms\n",
1216 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1223 client_stop_session_timeout (struct Session *s)
1225 GNUNET_assert (NULL != s);
1227 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1229 GNUNET_SCHEDULER_cancel (s->timeout_task);
1230 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1231 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1237 * Another peer has suggested an address for this
1238 * peer and transport plugin. Check that this could be a valid
1239 * address. If so, consider adding it to the list
1242 * @param cls closure
1243 * @param addr pointer to the address
1244 * @param addrlen length of addr
1245 * @return GNUNET_OK if this is a plausible address for this peer
1249 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1251 /* struct Plugin *plugin = cls; */
1253 /* A HTTP/S client does not have any valid address so:*/
1258 * Exit point from the plugin.
1261 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1263 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1264 struct HTTP_Client_Plugin *plugin = api->cls;
1265 struct Session *pos;
1266 struct Session *next;
1268 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1269 _("Shutting down plugin `%s'\n"),
1272 if (NULL == api->cls)
1279 next = plugin->head;
1280 while (NULL != (pos = next))
1283 client_disconnect (pos);
1285 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1287 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1288 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1292 if (NULL != plugin->curl_multi_handle)
1294 curl_multi_cleanup (plugin->curl_multi_handle);
1295 plugin->curl_multi_handle = NULL;
1297 curl_global_cleanup ();
1299 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1300 _("Shutdown for plugin `%s' complete\n"),
1303 GNUNET_free (plugin);
1310 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1312 unsigned long long max_connections;
1314 /* Optional parameters */
1315 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1317 "MAX_CONNECTIONS", &max_connections))
1318 max_connections = 128;
1319 plugin->max_connections = max_connections;
1321 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1322 _("Maximum number of connections is %u\n"),
1323 plugin->max_connections);
1328 * Entry point for the plugin.
1331 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1333 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1334 struct GNUNET_TRANSPORT_PluginFunctions *api;
1335 struct HTTP_Client_Plugin *plugin;
1337 if (NULL == env->receive)
1339 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1340 initialze the plugin or the API */
1341 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1343 api->address_to_string = &http_common_plugin_address_to_string;
1344 api->string_to_address = &http_common_plugin_string_to_address;
1345 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1349 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1352 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1354 api->send = &http_client_plugin_send;
1355 api->disconnect = &http_client_plugin_disconnect;
1356 api->check_address = &http_client_plugin_address_suggested;
1357 api->get_session = &http_client_plugin_get_session;
1358 api->address_to_string = &http_common_plugin_address_to_string;
1359 api->string_to_address = &http_common_plugin_string_to_address;
1360 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1364 plugin->name = "transport-https_client";
1365 plugin->protocol = "https";
1367 plugin->name = "transport-http_client";
1368 plugin->protocol = "http";
1370 plugin->last_tag = 1;
1372 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1374 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1379 if (GNUNET_SYSERR == client_start (plugin))
1381 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1387 /* end of plugin_transport_http_client.c */