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 HTTP_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
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)
313 * Function that can be used by the transport service to transmit
314 * a message using the plugin. Note that in the case of a
315 * peer disconnecting, the continuation MUST be called
316 * prior to the disconnect notification itself. This function
317 * will be called with this peer's HELLO message to initiate
318 * a fresh connection to another peer.
321 * @param session which session must be used
322 * @param msgbuf the message to transmit
323 * @param msgbuf_size number of bytes in 'msgbuf'
324 * @param priority how important is the message (most plugins will
325 * ignore message priority and just FIFO)
326 * @param to how long to wait at most for the transmission (does not
327 * require plugins to discard the message after the timeout,
328 * just advisory for the desired delay; most plugins will ignore
330 * @param cont continuation to call once the message has
331 * been transmitted (or if the transport is ready
332 * for the next transmission call; or if the
333 * peer disconnected...); can be NULL
334 * @param cont_cls closure for cont
335 * @return number of bytes used (on the physical network, with overheads);
336 * -1 on hard errors (i.e. address invalid); 0 is a legal value
337 * and does NOT mean that the message was not transmitted (DV)
340 http_client_plugin_send (void *cls,
341 struct Session *session,
342 const char *msgbuf, size_t msgbuf_size,
343 unsigned int priority,
344 struct GNUNET_TIME_Relative to,
345 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
347 struct HTTP_Client_Plugin *plugin = cls;
348 struct HTTP_Message *msg;
351 GNUNET_assert (plugin != NULL);
352 GNUNET_assert (session != NULL);
354 /* lookup if session is really existing */
355 if (GNUNET_YES != client_exist_session (plugin, session))
358 return GNUNET_SYSERR;
361 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
362 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
363 session, session->client_put,
364 msgbuf_size, GNUNET_i2s (&session->target));
366 /* create new message and schedule */
367 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
369 msg->size = msgbuf_size;
371 msg->buf = (char *) &msg[1];
372 msg->transmit_cont = cont;
373 msg->transmit_cont_cls = cont_cls;
374 memcpy (msg->buf, msgbuf, msgbuf_size);
375 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
377 if (session->client_put_paused == GNUNET_YES)
379 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
380 "Session %p/connection %p: unpausing connection\n",
381 session, session->client_put);
382 session->client_put_paused = GNUNET_NO;
383 curl_easy_pause (session->client_put, CURLPAUSE_CONT);
385 client_schedule (session->plugin, GNUNET_YES);
386 client_reschedule_session_timeout (session);
393 client_delete_session (struct Session *s)
395 struct HTTP_Client_Plugin *plugin = s->plugin;
396 struct HTTP_Message *pos = s->msg_head;
397 struct HTTP_Message *next = NULL;
399 client_stop_session_timeout (s);
401 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
403 while (NULL != (pos = next))
406 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
407 if (pos->transmit_cont != NULL)
408 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR);
412 if (s->msg_tk != NULL)
414 GNUNET_SERVER_mst_destroy (s->msg_tk);
417 GNUNET_free (s->addr);
424 * Disconnect a session
427 * @return GNUNET_OK on success, GNUNET_SYSERR on error
430 client_disconnect (struct Session *s)
432 struct HTTP_Client_Plugin *plugin = s->plugin;
433 struct HTTP_Message *msg;
434 struct HTTP_Message *t;
438 if (GNUNET_YES != client_exist_session(plugin, s))
441 return GNUNET_SYSERR;
444 if (s->client_put != NULL)
446 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
447 "Session %p/connection %p: disconnecting PUT connectionto peer `%s'\n",
448 s, s->client_put, GNUNET_i2s (&s->target));
450 /* remove curl handle from multi handle */
451 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
452 if (mret != CURLM_OK)
454 /* clean up easy handle, handle is now invalid and free'd */
458 curl_easy_cleanup (s->client_put);
459 s->client_put = NULL;
463 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
465 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
466 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
469 if (s->client_get != NULL)
471 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
472 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
473 s, s->client_get, GNUNET_i2s (&s->target));
475 /* remove curl handle from multi handle */
476 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
477 if (mret != CURLM_OK)
479 /* clean up easy handle, handle is now invalid and free'd */
483 curl_easy_cleanup (s->client_get);
484 s->client_get = NULL;
491 if (NULL != msg->transmit_cont)
492 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
493 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
498 GNUNET_assert (plugin->cur_connections >= 2);
499 plugin->cur_connections -= 2;
500 GNUNET_STATISTICS_set (plugin->env->stats,
501 "# HTTP client sessions",
502 plugin->cur_connections,
505 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
506 "Session %p: notifying transport about ending session\n",s);
508 plugin->env->session_end (plugin->env->cls, &s->target, s);
509 client_delete_session (s);
511 /* Re-schedule since handles have changed */
512 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
514 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
515 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
517 client_schedule (plugin, GNUNET_YES);
524 * Function that can be used to force the plugin to disconnect
525 * from the given peer and cancel all previous transmissions
526 * (and their continuationc).
529 * @param target peer from which to disconnect
532 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
534 struct HTTP_Client_Plugin *plugin = cls;
535 struct Session *next = NULL;
536 struct Session *pos = NULL;
538 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
539 "Transport tells me to disconnect `%s'\n",
540 GNUNET_i2s (target));
543 while (NULL != (pos = next))
546 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
548 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
549 "Disconnecting session %p to `%pos'\n",
550 pos, GNUNET_i2s (target));
551 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
557 static struct Session *
558 client_lookup_session (struct HTTP_Client_Plugin *plugin,
559 const struct GNUNET_HELLO_Address *address)
563 for (pos = plugin->head; NULL != pos; pos = pos->next)
564 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
565 (address->address_length == pos->addrlen) &&
566 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
572 * Callback method used with libcurl
573 * Method is called when libcurl needs to read data during sending
575 * @param stream pointer where to write data
576 * @param size size of an individual element
577 * @param nmemb count of elements that can be written to the buffer
578 * @param cls source pointer, passed to the libcurl handle
579 * @return bytes written to stream, returning 0 will terminate connection!
582 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
584 struct Session *s = cls;
585 struct HTTP_Client_Plugin *plugin = s->plugin;
586 struct HTTP_Message *msg = s->msg_head;
589 if (GNUNET_YES != client_exist_session (plugin, s))
596 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
597 "Session %p/connection %p: nothing to send, suspending\n",
599 s->client_put_paused = GNUNET_YES;
600 return CURL_READFUNC_PAUSE;
603 GNUNET_assert (msg->pos < msg->size);
604 /* calculate how much fits in buffer */
605 len = GNUNET_MIN (msg->size - msg->pos,
607 memcpy (stream, &msg->buf[msg->pos], len);
609 if (msg->pos == msg->size)
611 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
612 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
613 s, s->client_put, msg->size, msg->pos);
614 /* Calling transmit continuation */
615 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
616 if (NULL != msg->transmit_cont)
617 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
620 client_reschedule_session_timeout (s);
626 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
628 struct Session *s = cls;
630 if (GNUNET_YES != client_exist_session(p, s))
635 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
636 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
638 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
639 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
640 if (s->client_get != NULL)
641 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
647 client_receive_mst_cb (void *cls, void *client,
648 const struct GNUNET_MessageHeader *message)
650 struct Session *s = cls;
651 struct HTTP_Client_Plugin *plugin;
652 struct GNUNET_TIME_Relative delay;
653 struct GNUNET_ATS_Information atsi[2];
655 if (GNUNET_YES != client_exist_session(p, s))
663 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
664 atsi[0].value = htonl (1);
665 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
666 atsi[1].value = s->ats_address_network_type;
667 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
669 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
670 (const struct GNUNET_ATS_Information *) &atsi, 2,
671 s, s->addr, s->addrlen);
673 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
675 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
678 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
679 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
680 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
683 client_reschedule_session_timeout (s);
690 * Callback method used with libcurl
691 * Method is called when libcurl needs to write data during sending
693 * @param stream pointer where to write data
694 * @param size size of an individual element
695 * @param nmemb count of elements that can be written to the buffer
696 * @param cls destination pointer, passed to the libcurl handle
697 * @return bytes read from stream
700 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
704 struct Session *s = cls;
705 struct GNUNET_TIME_Absolute now;
706 size_t len = size * nmemb;
707 struct HTTP_Client_Plugin *plugin = s->plugin;
709 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
710 "Received %u bytes from peer `%s'\n", len,
711 GNUNET_i2s (&s->target));
712 now = GNUNET_TIME_absolute_get ();
713 if (now.abs_value < s->next_receive.abs_value)
715 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
716 struct GNUNET_TIME_Relative delta =
717 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
718 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
719 "No inbound bandwidth for session %p available! Next read was delayed for %llu ms\n",
721 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
723 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
724 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
726 s->recv_wakeup_task =
727 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
728 return CURLPAUSE_ALL;
730 if (NULL == s->msg_tk)
731 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
732 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
738 * Task performing curl operations
740 * @param cls plugin as closure
741 * @param tc gnunet scheduler task context
744 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
747 * Function setting up file descriptors and scheduling task to run
749 * @param plugin plugin as closure
750 * @param now schedule task in 1ms, regardless of what curl may say
751 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
754 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
760 struct GNUNET_NETWORK_FDSet *grs;
761 struct GNUNET_NETWORK_FDSet *gws;
764 struct GNUNET_TIME_Relative timeout;
766 /* Cancel previous scheduled task */
767 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
769 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
770 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
776 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
777 if (mret != CURLM_OK)
779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
780 "curl_multi_fdset", __FILE__, __LINE__,
781 curl_multi_strerror (mret));
782 return GNUNET_SYSERR;
784 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
786 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
788 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
789 if (now == GNUNET_YES)
790 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
792 if (mret != CURLM_OK)
794 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
795 "curl_multi_timeout", __FILE__, __LINE__,
796 curl_multi_strerror (mret));
797 return GNUNET_SYSERR;
800 grs = GNUNET_NETWORK_fdset_create ();
801 gws = GNUNET_NETWORK_fdset_create ();
802 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
803 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
805 plugin->client_perform_task =
806 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
808 &client_run, plugin);
809 GNUNET_NETWORK_fdset_destroy (gws);
810 GNUNET_NETWORK_fdset_destroy (grs);
817 * Task performing curl operations
819 * @param cls plugin as closure
820 * @param tc gnunet scheduler task context
823 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
825 struct HTTP_Client_Plugin *plugin = cls;
829 GNUNET_assert (cls != NULL);
831 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
832 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
838 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
843 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
845 CURL *easy_h = msg->easy_handle;
846 struct Session *s = NULL;
847 char *d = (char *) s;
852 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
853 "Client: connection to ended with reason %i: `%s', %i handles running\n",
855 curl_easy_strerror (msg->data.result), running);
859 GNUNET_assert (CURLE_OK ==
860 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
861 s = (struct Session *) d;
863 if (GNUNET_YES != client_exist_session(plugin, s))
869 GNUNET_assert (s != NULL);
870 if (msg->msg == CURLMSG_DONE)
872 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
873 "Session %p/connection %p: connection to `%s' ended with reason %i: `%s'\n",
874 s, msg->easy_handle, GNUNET_i2s (&s->target),
876 curl_easy_strerror (msg->data.result));
878 /* Disconnect other transmission direction and tell transport */
879 client_disconnect (s);
883 while (mret == CURLM_CALL_MULTI_PERFORM);
884 client_schedule (plugin, GNUNET_NO);
889 client_connect (struct Session *s)
892 struct HTTP_Client_Plugin *plugin = s->plugin;
898 if (NULL == http_common_plugin_address_to_string (NULL, s->addr, s->addrlen))
900 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
901 "Invalid address peer `%s'\n",
902 GNUNET_i2s (&s->target));
903 return GNUNET_SYSERR;
906 GNUNET_asprintf (&url, "%s/%s;%u",
907 http_common_plugin_address_to_string (plugin, s->addr, s->addrlen),
908 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
911 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
912 "Initiating outbound session peer `%s' using address `%s'\n",
913 GNUNET_i2s (&s->target), url);
915 /* create get connection */
916 s->client_get = curl_easy_init ();
918 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
919 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
920 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
923 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
924 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
925 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
927 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
928 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
929 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
930 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
931 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
932 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
933 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
934 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
935 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
936 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
937 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
938 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
939 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
940 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
942 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
945 /* create put connection */
946 s->client_put = curl_easy_init ();
948 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
949 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
950 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
953 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
954 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
955 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
957 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
958 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
959 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
960 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
961 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
962 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
963 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
964 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
965 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
966 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
967 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
968 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
969 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
970 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
971 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
973 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
977 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_get);
978 if (mret != CURLM_OK)
980 curl_easy_cleanup (s->client_get);
982 return GNUNET_SYSERR;
985 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_put);
986 if (mret != CURLM_OK)
988 curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
989 curl_easy_cleanup (s->client_get);
990 curl_easy_cleanup (s->client_put);
992 return GNUNET_SYSERR;
995 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
996 "Session %p: connected with connections GET %p and PUT %p\n",
997 s, s->client_get, s->client_put);
999 /* Perform connect */
1000 plugin->cur_connections += 2;
1001 GNUNET_STATISTICS_set (plugin->env->stats,
1002 "# HTTP client connections",
1003 plugin->cur_connections,
1006 /* Re-schedule since handles have changed */
1007 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1009 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1010 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1012 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1018 * Creates a new outbound session the transport service will use to send data to the
1021 * @param cls the plugin
1022 * @param address the address
1023 * @return the session or NULL of max connections exceeded
1025 static struct Session *
1026 http_client_plugin_get_session (void *cls,
1027 const struct GNUNET_HELLO_Address *address)
1029 struct HTTP_Client_Plugin *plugin = cls;
1030 struct Session * s = NULL;
1032 GNUNET_assert (plugin != NULL);
1033 GNUNET_assert (address != NULL);
1034 GNUNET_assert (address->address != NULL);
1037 /* find existing session */
1038 s = client_lookup_session (plugin, address);
1042 if (plugin->max_connections <= plugin->cur_connections)
1044 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1045 "Maximum number of connections (%u) reached: "
1046 "cannot connect to peer `%s'\n",
1047 plugin->max_connections,
1048 GNUNET_i2s (&address->peer));
1052 s = GNUNET_malloc (sizeof (struct Session));
1053 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1055 s->addr = GNUNET_malloc (address->address_length);
1056 memcpy (s->addr, address->address, address->address_length);
1057 s->addrlen = address->address_length;
1058 //s->ats_address_network_type = ats.value;
1061 client_start_session_timeout (s);
1063 /* add new session */
1064 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1066 /* initiate new connection */
1067 if (GNUNET_SYSERR == client_connect (s))
1069 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1070 "Cannot connect to peer `%s' address `%s''\n",
1071 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1072 GNUNET_i2s (&s->target));
1073 client_delete_session (s);
1080 client_start (struct HTTP_Client_Plugin *plugin)
1082 curl_global_init (CURL_GLOBAL_ALL);
1083 plugin->curl_multi_handle = curl_multi_init ();
1085 if (NULL == plugin->curl_multi_handle)
1087 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1088 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1090 return GNUNET_SYSERR;
1096 * Session was idle, so disconnect it
1099 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1101 GNUNET_assert (NULL != cls);
1102 struct Session *s = cls;
1104 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1105 GNUNET_log (TIMEOUT_LOG,
1106 "Session %p was idle for %llu ms, disconnecting\n",
1107 s, (unsigned long long) TIMEOUT.rel_value);
1109 /* call session destroy function */
1110 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1114 * Start session timeout
1117 client_start_session_timeout (struct Session *s)
1120 GNUNET_assert (NULL != s);
1121 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1122 s->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1123 &client_session_timeout,
1125 GNUNET_log (TIMEOUT_LOG,
1126 "Timeout for session %p set to %llu ms\n",
1127 s, (unsigned long long) TIMEOUT.rel_value);
1131 * Increment session timeout due to activity
1134 client_reschedule_session_timeout (struct Session *s)
1137 GNUNET_assert (NULL != s);
1138 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1140 GNUNET_SCHEDULER_cancel (s->timeout_task);
1141 s->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1142 &client_session_timeout,
1144 GNUNET_log (TIMEOUT_LOG,
1145 "Timeout rescheduled for session %p set to %llu ms\n",
1146 s, (unsigned long long) TIMEOUT.rel_value);
1153 client_stop_session_timeout (struct Session *s)
1155 GNUNET_assert (NULL != s);
1157 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1159 GNUNET_SCHEDULER_cancel (s->timeout_task);
1160 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1161 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1167 * Another peer has suggested an address for this
1168 * peer and transport plugin. Check that this could be a valid
1169 * address. If so, consider adding it to the list
1172 * @param cls closure
1173 * @param addr pointer to the address
1174 * @param addrlen length of addr
1175 * @return GNUNET_OK if this is a plausible address for this peer
1179 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1181 /* struct Plugin *plugin = cls; */
1183 /* A HTTP/S client does not have any valid address so:*/
1188 * Exit point from the plugin.
1191 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1193 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1194 struct HTTP_Client_Plugin *plugin = api->cls;
1195 struct Session *pos;
1196 struct Session *next;
1198 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1199 _("Shutting down plugin `%s'\n"),
1202 next = plugin->head;
1203 while (NULL != (pos = next))
1206 GNUNET_CONTAINER_DLL_remove( plugin->head, plugin->tail, pos);
1207 client_disconnect (pos);
1210 if (NULL == api->cls)
1216 if (NULL != plugin->curl_multi_handle)
1218 curl_multi_cleanup (plugin->curl_multi_handle);
1219 plugin->curl_multi_handle = NULL;
1221 curl_global_cleanup ();
1223 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1224 _("Shutdown for plugin `%s' complete\n"),
1227 GNUNET_free (plugin);
1234 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1236 unsigned long long max_connections;
1238 /* Optional parameters */
1239 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1241 "MAX_CONNECTIONS", &max_connections))
1242 max_connections = 128;
1243 plugin->max_connections = max_connections;
1245 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1246 _("Maximum number of connections is %u\n"),
1247 plugin->max_connections);
1252 * Entry point for the plugin.
1255 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1257 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1258 struct GNUNET_TRANSPORT_PluginFunctions *api;
1259 struct HTTP_Client_Plugin *plugin;
1261 if (NULL == env->receive)
1263 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1264 initialze the plugin or the API */
1265 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1267 api->address_to_string = &http_common_plugin_address_to_string;
1268 api->string_to_address = &http_common_plugin_string_to_address;
1269 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1273 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1276 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1278 api->send = &http_client_plugin_send;
1279 api->disconnect = &http_client_plugin_disconnect;
1280 api->check_address = &http_client_plugin_address_suggested;
1281 api->get_session = &http_client_plugin_get_session;
1282 api->address_to_string = &http_common_plugin_address_to_string;
1283 api->string_to_address = &http_common_plugin_string_to_address;
1284 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1288 plugin->name = "transport-https_client";
1289 plugin->protocol = "https";
1291 plugin->name = "transport-http_client";
1292 plugin->protocol = "http";
1294 plugin->last_tag = 1;
1296 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1298 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1303 if (GNUNET_SYSERR == client_start (plugin))
1305 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1311 /* end of plugin_transport_http_client.c */