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_YES
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)
326 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
334 case CURLINFO_HEADER_IN:
337 case CURLINFO_HEADER_OUT:
338 ttype = "HEADER_OUT";
344 memcpy (text, data, size);
345 if (text[size - 1] == '\n')
350 text[size + 1] = '\0';
353 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
354 "Connection %p %s: %s", cls, ttype, text);
356 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
357 "Connection %p %s: %s", cls, ttype, text);
365 * Function that can be used by the transport service to transmit
366 * a message using the plugin. Note that in the case of a
367 * peer disconnecting, the continuation MUST be called
368 * prior to the disconnect notification itself. This function
369 * will be called with this peer's HELLO message to initiate
370 * a fresh connection to another peer.
373 * @param session which session must be used
374 * @param msgbuf the message to transmit
375 * @param msgbuf_size number of bytes in 'msgbuf'
376 * @param priority how important is the message (most plugins will
377 * ignore message priority and just FIFO)
378 * @param to how long to wait at most for the transmission (does not
379 * require plugins to discard the message after the timeout,
380 * just advisory for the desired delay; most plugins will ignore
382 * @param cont continuation to call once the message has
383 * been transmitted (or if the transport is ready
384 * for the next transmission call; or if the
385 * peer disconnected...); can be NULL
386 * @param cont_cls closure for cont
387 * @return number of bytes used (on the physical network, with overheads);
388 * -1 on hard errors (i.e. address invalid); 0 is a legal value
389 * and does NOT mean that the message was not transmitted (DV)
392 http_client_plugin_send (void *cls,
393 struct Session *session,
394 const char *msgbuf, size_t msgbuf_size,
395 unsigned int priority,
396 struct GNUNET_TIME_Relative to,
397 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
399 struct HTTP_Client_Plugin *plugin = cls;
400 struct HTTP_Message *msg;
402 GNUNET_assert (plugin != NULL);
403 GNUNET_assert (session != NULL);
405 /* lookup if session is really existing */
406 if (GNUNET_YES != client_exist_session (plugin, session))
409 return GNUNET_SYSERR;
412 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
413 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
414 session, session->client_put,
415 msgbuf_size, GNUNET_i2s (&session->target));
417 /* create new message and schedule */
418 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
420 msg->size = msgbuf_size;
422 msg->buf = (char *) &msg[1];
423 msg->transmit_cont = cont;
424 msg->transmit_cont_cls = cont_cls;
425 memcpy (msg->buf, msgbuf, msgbuf_size);
426 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
428 if (session->client_put_paused == GNUNET_YES)
430 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
431 "Session %p/connection %p: unpausing connection\n",
432 session, session->client_put);
433 session->client_put_paused = GNUNET_NO;
434 curl_easy_pause (session->client_put, CURLPAUSE_CONT);
436 client_schedule (session->plugin, GNUNET_YES);
437 client_reschedule_session_timeout (session);
443 client_delete_session (struct Session *s)
445 struct HTTP_Client_Plugin *plugin = s->plugin;
446 struct HTTP_Message *pos;
447 struct HTTP_Message *next;
449 client_stop_session_timeout (s);
451 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
454 while (NULL != (pos = next))
457 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
458 if (pos->transmit_cont != NULL)
459 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR);
463 if (s->msg_tk != NULL)
465 GNUNET_SERVER_mst_destroy (s->msg_tk);
468 GNUNET_free (s->addr);
475 * Disconnect a session
478 * @return GNUNET_OK on success, GNUNET_SYSERR on error
481 client_disconnect (struct Session *s)
483 struct HTTP_Client_Plugin *plugin = s->plugin;
484 struct HTTP_Message *msg;
485 struct HTTP_Message *t;
489 if (GNUNET_YES != client_exist_session (plugin, s))
492 return GNUNET_SYSERR;
495 if (s->client_put != NULL)
497 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
498 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
499 s, s->client_put, GNUNET_i2s (&s->target));
501 /* remove curl handle from multi handle */
502 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
503 if (mret != CURLM_OK)
505 /* clean up easy handle, handle is now invalid and free'd */
509 curl_easy_cleanup (s->client_put);
510 s->client_put = NULL;
514 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
516 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
517 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
520 if (s->client_get != NULL)
522 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
523 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
524 s, s->client_get, GNUNET_i2s (&s->target));
526 /* remove curl handle from multi handle */
527 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
528 if (mret != CURLM_OK)
530 /* clean up easy handle, handle is now invalid and free'd */
534 curl_easy_cleanup (s->client_get);
535 s->client_get = NULL;
542 if (NULL != msg->transmit_cont)
543 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
544 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
549 GNUNET_assert (plugin->cur_connections >= 2);
550 plugin->cur_connections -= 2;
551 GNUNET_STATISTICS_set (plugin->env->stats,
552 "# HTTP client sessions",
553 plugin->cur_connections,
556 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
557 "Session %p: notifying transport about ending session\n",s);
559 plugin->env->session_end (plugin->env->cls, &s->target, s);
560 client_delete_session (s);
562 /* Re-schedule since handles have changed */
563 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
565 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
566 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
568 client_schedule (plugin, GNUNET_YES);
575 * Function that can be used to force the plugin to disconnect
576 * from the given peer and cancel all previous transmissions
577 * (and their continuationc).
580 * @param target peer from which to disconnect
583 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
585 struct HTTP_Client_Plugin *plugin = cls;
586 struct Session *next = NULL;
587 struct Session *pos = NULL;
589 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
590 "Transport tells me to disconnect `%s'\n",
591 GNUNET_i2s (target));
594 while (NULL != (pos = next))
597 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
600 "Disconnecting session %p to `%pos'\n",
601 pos, GNUNET_i2s (target));
602 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
608 static struct Session *
609 client_lookup_session (struct HTTP_Client_Plugin *plugin,
610 const struct GNUNET_HELLO_Address *address)
614 for (pos = plugin->head; NULL != pos; pos = pos->next)
615 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
616 (address->address_length == pos->addrlen) &&
617 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
623 * Callback method used with libcurl
624 * Method is called when libcurl needs to read data during sending
626 * @param stream pointer where to write data
627 * @param size size of an individual element
628 * @param nmemb count of elements that can be written to the buffer
629 * @param cls source pointer, passed to the libcurl handle
630 * @return bytes written to stream, returning 0 will terminate connection!
633 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
635 struct Session *s = cls;
636 struct HTTP_Client_Plugin *plugin = s->plugin;
637 struct HTTP_Message *msg = s->msg_head;
640 if (GNUNET_YES != client_exist_session (plugin, s))
647 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
648 "Session %p/connection %p: nothing to send, suspending\n",
650 s->client_put_paused = GNUNET_YES;
651 return CURL_READFUNC_PAUSE;
654 GNUNET_assert (msg->pos < msg->size);
655 /* calculate how much fits in buffer */
656 len = GNUNET_MIN (msg->size - msg->pos,
658 memcpy (stream, &msg->buf[msg->pos], len);
660 if (msg->pos == msg->size)
662 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
663 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
664 s, s->client_put, msg->size, msg->pos);
665 /* Calling transmit continuation */
666 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
667 if (NULL != msg->transmit_cont)
668 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
671 client_reschedule_session_timeout (s);
677 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
679 struct Session *s = cls;
681 if (GNUNET_YES != client_exist_session(p, s))
686 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
687 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
689 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
690 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
691 if (s->client_get != NULL)
692 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
698 client_receive_mst_cb (void *cls, void *client,
699 const struct GNUNET_MessageHeader *message)
701 struct Session *s = cls;
702 struct HTTP_Client_Plugin *plugin;
703 struct GNUNET_TIME_Relative delay;
704 struct GNUNET_ATS_Information atsi[2];
706 if (GNUNET_YES != client_exist_session(p, s))
714 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
715 atsi[0].value = htonl (1);
716 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
717 atsi[1].value = s->ats_address_network_type;
718 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
720 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
721 (const struct GNUNET_ATS_Information *) &atsi, 2,
722 s, s->addr, s->addrlen);
724 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
726 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
729 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
730 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
731 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
734 client_reschedule_session_timeout (s);
741 * Callback method used with libcurl
742 * Method is called when libcurl needs to write data during sending
744 * @param stream pointer where to write data
745 * @param size size of an individual element
746 * @param nmemb count of elements that can be written to the buffer
747 * @param cls destination pointer, passed to the libcurl handle
748 * @return bytes read from stream
751 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
753 struct Session *s = cls;
754 struct GNUNET_TIME_Absolute now;
755 size_t len = size * nmemb;
756 struct HTTP_Client_Plugin *plugin = s->plugin;
758 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
759 "Received %u bytes from peer `%s'\n", len,
760 GNUNET_i2s (&s->target));
761 now = GNUNET_TIME_absolute_get ();
762 if (now.abs_value < s->next_receive.abs_value)
764 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
765 struct GNUNET_TIME_Relative delta =
766 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
767 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
768 "No inbound bandwidth for session %p available! Next read was delayed for %llu ms\n",
770 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
772 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
773 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
775 s->recv_wakeup_task =
776 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
777 return CURLPAUSE_ALL;
779 if (NULL == s->msg_tk)
780 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
781 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
787 * Task performing curl operations
789 * @param cls plugin as closure
790 * @param tc gnunet scheduler task context
793 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
796 * Function setting up file descriptors and scheduling task to run
798 * @param plugin plugin as closure
799 * @param now schedule task in 1ms, regardless of what curl may say
800 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
803 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
809 struct GNUNET_NETWORK_FDSet *grs;
810 struct GNUNET_NETWORK_FDSet *gws;
813 struct GNUNET_TIME_Relative timeout;
815 /* Cancel previous scheduled task */
816 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
818 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
819 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
825 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
826 if (mret != CURLM_OK)
828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
829 "curl_multi_fdset", __FILE__, __LINE__,
830 curl_multi_strerror (mret));
831 return GNUNET_SYSERR;
833 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
835 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
837 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
838 if (now == GNUNET_YES)
839 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
841 if (mret != CURLM_OK)
843 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
844 "curl_multi_timeout", __FILE__, __LINE__,
845 curl_multi_strerror (mret));
846 return GNUNET_SYSERR;
849 grs = GNUNET_NETWORK_fdset_create ();
850 gws = GNUNET_NETWORK_fdset_create ();
851 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
852 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
854 plugin->client_perform_task =
855 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
857 &client_run, plugin);
858 GNUNET_NETWORK_fdset_destroy (gws);
859 GNUNET_NETWORK_fdset_destroy (grs);
866 * Task performing curl operations
868 * @param cls plugin as closure
869 * @param tc gnunet scheduler task context
872 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
874 struct HTTP_Client_Plugin *plugin = cls;
878 GNUNET_assert (cls != NULL);
880 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
881 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
887 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
892 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
894 CURL *easy_h = msg->easy_handle;
895 struct Session *s = NULL;
896 char *d = (char *) s;
901 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
902 "Client: connection to ended with reason %i: `%s', %i handles running\n",
904 curl_easy_strerror (msg->data.result), running);
908 GNUNET_assert (CURLE_OK ==
909 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
910 s = (struct Session *) d;
912 if (GNUNET_YES != client_exist_session(plugin, s))
918 GNUNET_assert (s != NULL);
919 if (msg->msg == CURLMSG_DONE)
921 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
922 "Session %p/connection %p: connection to `%s' ended with reason %i: `%s'\n",
923 s, msg->easy_handle, GNUNET_i2s (&s->target),
925 curl_easy_strerror (msg->data.result));
927 /* Disconnect other transmission direction and tell transport */
928 client_disconnect (s);
932 while (mret == CURLM_CALL_MULTI_PERFORM);
933 client_schedule (plugin, GNUNET_NO);
937 client_connect (struct Session *s)
940 struct HTTP_Client_Plugin *plugin = s->plugin;
946 if (NULL == http_common_plugin_address_to_string (NULL, s->addr, s->addrlen))
948 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
949 "Invalid address peer `%s'\n",
950 GNUNET_i2s (&s->target));
951 return GNUNET_SYSERR;
954 GNUNET_asprintf (&url, "%s/%s;%u",
955 http_common_plugin_address_to_string (plugin, s->addr, s->addrlen),
956 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
959 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
960 "Initiating outbound session peer `%s' using address `%s'\n",
961 GNUNET_i2s (&s->target), url);
963 /* create get connection */
964 s->client_get = curl_easy_init ();
966 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
967 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
968 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
971 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
972 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
973 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
975 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
976 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
977 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
978 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
979 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
980 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
981 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
982 /* No timeout by default, timeout done with session timeout */
983 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
984 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
985 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
986 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
987 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
988 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
990 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
993 /* create put connection */
994 s->client_put = curl_easy_init ();
996 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
997 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
998 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
1001 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1002 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1003 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1005 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
1006 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1008 struct curl_slist *m_headerlist;
1009 m_headerlist = NULL;
1010 m_headerlist = curl_slist_append(m_headerlist, "Transfer-Encoding: chunked");
1011 curl_easy_setopt(s->client_put, CURLOPT_HTTPHEADER, m_headerlist);*/
1012 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1013 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1014 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1015 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1016 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
1017 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1018 /* No timeout by default, timeout done with session timeout */
1019 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1020 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1021 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1022 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1023 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1024 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1025 #if CURL_TCP_NODELAY
1026 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1030 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_get);
1031 if (mret != CURLM_OK)
1033 curl_easy_cleanup (s->client_get);
1035 return GNUNET_SYSERR;
1038 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_put);
1039 if (mret != CURLM_OK)
1041 curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
1042 curl_easy_cleanup (s->client_get);
1043 curl_easy_cleanup (s->client_put);
1045 return GNUNET_SYSERR;
1048 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1049 "Session %p: connected with connections GET %p and PUT %p\n",
1050 s, s->client_get, s->client_put);
1052 /* Perform connect */
1053 plugin->cur_connections += 2;
1054 GNUNET_STATISTICS_set (plugin->env->stats,
1055 "# HTTP client connections",
1056 plugin->cur_connections,
1059 /* Re-schedule since handles have changed */
1060 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1062 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1063 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1065 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1071 * Creates a new outbound session the transport service will use to send data to the
1074 * @param cls the plugin
1075 * @param address the address
1076 * @return the session or NULL of max connections exceeded
1078 static struct Session *
1079 http_client_plugin_get_session (void *cls,
1080 const struct GNUNET_HELLO_Address *address)
1082 struct HTTP_Client_Plugin *plugin = cls;
1083 struct Session * s = NULL;
1084 struct sockaddr *sa;
1085 struct GNUNET_ATS_Information ats;
1089 GNUNET_assert (plugin != NULL);
1090 GNUNET_assert (address != NULL);
1091 GNUNET_assert (address->address != NULL);
1094 /* find existing session */
1095 s = client_lookup_session (plugin, address);
1099 if (plugin->max_connections <= plugin->cur_connections)
1101 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1102 "Maximum number of connections (%u) reached: "
1103 "cannot connect to peer `%s'\n",
1104 plugin->max_connections,
1105 GNUNET_i2s (&address->peer));
1109 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1110 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1111 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1113 if (GNUNET_SYSERR == res)
1117 else if (GNUNET_YES == res)
1119 GNUNET_assert (NULL != sa);
1120 if (AF_INET == sa->sa_family)
1122 salen = sizeof (struct sockaddr_in);
1124 else if (AF_INET == sa->sa_family)
1126 salen = sizeof (struct sockaddr_in6);
1128 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1131 else if (GNUNET_NO == res)
1133 ats.value = htonl (GNUNET_ATS_COST_WAN);
1136 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1142 s = GNUNET_malloc (sizeof (struct Session));
1143 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1145 s->addr = GNUNET_malloc (address->address_length);
1146 memcpy (s->addr, address->address, address->address_length);
1147 s->addrlen = address->address_length;
1148 s->ats_address_network_type = ats.value;
1150 client_start_session_timeout (s);
1152 /* add new session */
1153 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1155 /* initiate new connection */
1156 if (GNUNET_SYSERR == client_connect (s))
1158 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1159 "Cannot connect to peer `%s' address `%s''\n",
1160 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1161 GNUNET_i2s (&s->target));
1162 client_delete_session (s);
1169 client_start (struct HTTP_Client_Plugin *plugin)
1171 curl_global_init (CURL_GLOBAL_ALL);
1172 plugin->curl_multi_handle = curl_multi_init ();
1174 if (NULL == plugin->curl_multi_handle)
1176 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1177 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1179 return GNUNET_SYSERR;
1185 * Session was idle, so disconnect it
1188 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1190 GNUNET_assert (NULL != cls);
1191 struct Session *s = cls;
1193 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1194 GNUNET_log (TIMEOUT_LOG,
1195 "Session %p was idle for %llu ms, disconnecting\n",
1196 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1198 /* call session destroy function */
1199 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1203 * Start session timeout
1206 client_start_session_timeout (struct Session *s)
1209 GNUNET_assert (NULL != s);
1210 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == 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 for session %p set to %llu ms\n",
1216 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1220 * Increment session timeout due to activity
1223 client_reschedule_session_timeout (struct Session *s)
1226 GNUNET_assert (NULL != s);
1227 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1229 GNUNET_SCHEDULER_cancel (s->timeout_task);
1230 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1231 &client_session_timeout,
1233 GNUNET_log (TIMEOUT_LOG,
1234 "Timeout rescheduled for session %p set to %llu ms\n",
1235 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1242 client_stop_session_timeout (struct Session *s)
1244 GNUNET_assert (NULL != s);
1246 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1248 GNUNET_SCHEDULER_cancel (s->timeout_task);
1249 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1250 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1256 * Another peer has suggested an address for this
1257 * peer and transport plugin. Check that this could be a valid
1258 * address. If so, consider adding it to the list
1261 * @param cls closure
1262 * @param addr pointer to the address
1263 * @param addrlen length of addr
1264 * @return GNUNET_OK if this is a plausible address for this peer
1268 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1270 /* struct Plugin *plugin = cls; */
1272 /* A HTTP/S client does not have any valid address so:*/
1277 * Exit point from the plugin.
1280 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1282 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1283 struct HTTP_Client_Plugin *plugin = api->cls;
1284 struct Session *pos;
1285 struct Session *next;
1287 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1288 _("Shutting down plugin `%s'\n"),
1291 if (NULL == api->cls)
1298 next = plugin->head;
1299 while (NULL != (pos = next))
1302 client_disconnect (pos);
1304 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1306 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1307 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1311 if (NULL != plugin->curl_multi_handle)
1313 curl_multi_cleanup (plugin->curl_multi_handle);
1314 plugin->curl_multi_handle = NULL;
1316 curl_global_cleanup ();
1318 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1319 _("Shutdown for plugin `%s' complete\n"),
1322 GNUNET_free (plugin);
1329 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1331 unsigned long long max_connections;
1333 /* Optional parameters */
1334 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1336 "MAX_CONNECTIONS", &max_connections))
1337 max_connections = 128;
1338 plugin->max_connections = max_connections;
1340 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1341 _("Maximum number of connections is %u\n"),
1342 plugin->max_connections);
1347 * Entry point for the plugin.
1350 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1352 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1353 struct GNUNET_TRANSPORT_PluginFunctions *api;
1354 struct HTTP_Client_Plugin *plugin;
1356 if (NULL == env->receive)
1358 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1359 initialze the plugin or the API */
1360 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1362 api->address_to_string = &http_common_plugin_address_to_string;
1363 api->string_to_address = &http_common_plugin_string_to_address;
1364 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1368 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1371 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1373 api->send = &http_client_plugin_send;
1374 api->disconnect = &http_client_plugin_disconnect;
1375 api->check_address = &http_client_plugin_address_suggested;
1376 api->get_session = &http_client_plugin_get_session;
1377 api->address_to_string = &http_common_plugin_address_to_string;
1378 api->string_to_address = &http_common_plugin_string_to_address;
1379 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1383 plugin->name = "transport-https_client";
1384 plugin->protocol = "https";
1386 plugin->name = "transport-http_client";
1387 plugin->protocol = "http";
1389 plugin->last_tag = 1;
1391 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1393 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1398 if (GNUNET_SYSERR == client_start (plugin))
1400 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1406 /* end of plugin_transport_http_client.c */