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;
154 * Client receive handle
159 * next pointer for double linked list
161 struct HTTP_Message *msg_head;
164 * previous pointer for double linked list
166 struct HTTP_Message *msg_tail;
169 * Message stream tokenizer for incoming data
171 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
174 * Session timeout task
176 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
179 * Task to wake up client receive handle when receiving is allowed again
181 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
184 * Absolute time when to receive data again
185 * Used for receive throttling
187 struct GNUNET_TIME_Absolute next_receive;
191 * Encapsulation of all of the state of the plugin.
193 struct HTTP_Client_Plugin
198 struct GNUNET_TRANSPORT_PluginEnvironment *env;
201 * Linked list head of open sessions.
203 struct Session *head;
206 * Linked list tail of open sessions.
208 struct Session *tail;
221 * Maximum number of sockets the plugin can use
222 * Each http inbound /outbound connections are two connections
224 unsigned int max_connections;
227 * Current number of sockets the plugin can use
228 * Each http inbound /outbound connections are two connections
230 unsigned int cur_connections;
233 * Last used unique HTTP connection tag
250 CURLM *curl_multi_handle;
255 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
259 * Encapsulation of all of the state of the plugin.
261 struct HTTP_Client_Plugin *p;
264 * Start session timeout
267 client_start_session_timeout (struct Session *s);
270 * Increment session timeout due to activity
273 client_reschedule_session_timeout (struct Session *s);
279 client_stop_session_timeout (struct Session *s);
283 * Function that can be used by the transport service to transmit
284 * a message using the plugin. Note that in the case of a
285 * peer disconnecting, the continuation MUST be called
286 * prior to the disconnect notification itself. This function
287 * will be called with this peer's HELLO message to initiate
288 * a fresh connection to another peer.
291 * @param session which session must be used
292 * @param msgbuf the message to transmit
293 * @param msgbuf_size number of bytes in 'msgbuf'
294 * @param priority how important is the message (most plugins will
295 * ignore message priority and just FIFO)
296 * @param to how long to wait at most for the transmission (does not
297 * require plugins to discard the message after the timeout,
298 * just advisory for the desired delay; most plugins will ignore
300 * @param cont continuation to call once the message has
301 * been transmitted (or if the transport is ready
302 * for the next transmission call; or if the
303 * peer disconnected...); can be NULL
304 * @param cont_cls closure for cont
305 * @return number of bytes used (on the physical network, with overheads);
306 * -1 on hard errors (i.e. address invalid); 0 is a legal value
307 * and does NOT mean that the message was not transmitted (DV)
310 http_client_plugin_send (void *cls,
311 struct Session *session,
312 const char *msgbuf, size_t msgbuf_size,
313 unsigned int priority,
314 struct GNUNET_TIME_Relative to,
315 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
317 struct HTTP_Client_Plugin *plugin = cls;
320 GNUNET_assert (plugin != NULL);
321 GNUNET_assert (session != NULL);
325 /* struct Plugin *plugin = cls; */
332 * Function that can be used to force the plugin to disconnect
333 * from the given peer and cancel all previous transmissions
334 * (and their continuationc).
337 * @param target peer from which to disconnect
340 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
342 // struct Plugin *plugin = cls;
347 static struct Session *
348 client_lookup_session (struct HTTP_Client_Plugin *plugin,
349 const struct GNUNET_HELLO_Address *address)
353 for (pos = plugin->head; NULL != pos; pos = pos->next)
354 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
355 (address->address_length == pos->addrlen) &&
356 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
362 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
364 struct Session * head;
366 GNUNET_assert (NULL != plugin);
367 GNUNET_assert (NULL != s);
369 for (head = plugin->head; head != NULL; head = head->next)
378 * Callback method used with libcurl
379 * Method is called when libcurl needs to read data during sending
381 * @param stream pointer where to write data
382 * @param size size of an individual element
383 * @param nmemb count of elements that can be written to the buffer
384 * @param cls source pointer, passed to the libcurl handle
385 * @return bytes written to stream, returning 0 will terminate connection!
388 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
390 struct Session *s = cls;
391 struct HTTP_Client_Plugin *plugin = s->plugin;
392 struct HTTP_Message *msg = s->msg_head;
395 if (GNUNET_YES != client_exist_session (plugin, s))
402 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
403 "Nothing to for session %p send! Suspending PUT handle!\n", s);
404 s->client_put_paused = GNUNET_YES;
405 return CURL_READFUNC_PAUSE;
408 GNUNET_assert (msg->pos < msg->size);
409 /* calculate how much fits in buffer */
410 len = GNUNET_MIN (msg->size - msg->pos,
412 memcpy (stream, &msg->buf[msg->pos], len);
414 if (msg->pos == msg->size)
416 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
417 "Session %p message with %u bytes sent, removing message from queue\n",
418 s, msg->size, msg->pos);
419 /* Calling transmit continuation */
420 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
421 if (NULL != msg->transmit_cont)
422 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
425 client_reschedule_session_timeout (s);
431 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
433 struct Session *s = cls;
435 if (GNUNET_YES != client_exist_session(p, s))
440 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
441 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
443 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
444 "Client: %p Waking up receive handle\n", s->client_get);
445 if (s->client_get != NULL)
446 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
452 client_receive_mst_cb (void *cls, void *client,
453 const struct GNUNET_MessageHeader *message)
455 struct Session *s = cls;
456 struct HTTP_Client_Plugin *plugin;
457 struct GNUNET_TIME_Relative delay;
458 struct GNUNET_ATS_Information atsi[2];
460 if (GNUNET_YES != client_exist_session(p, s))
468 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
469 atsi[0].value = htonl (1);
470 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
471 atsi[1].value = s->ats_address_network_type;
472 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
474 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
475 (const struct GNUNET_ATS_Information *) &atsi, 2,
476 s, s->addr, s->addrlen);
478 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
480 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
483 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
484 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
485 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
488 client_reschedule_session_timeout (s);
495 * Callback method used with libcurl
496 * Method is called when libcurl needs to write data during sending
498 * @param stream pointer where to write data
499 * @param size size of an individual element
500 * @param nmemb count of elements that can be written to the buffer
501 * @param cls destination pointer, passed to the libcurl handle
502 * @return bytes read from stream
505 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
509 struct Session *s = cls;
510 struct GNUNET_TIME_Absolute now;
511 size_t len = size * nmemb;
512 struct HTTP_Client_Plugin *plugin = s->plugin;
514 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
515 "Received %u bytes from peer `%s'\n", len,
516 GNUNET_i2s (&s->target));
517 now = GNUNET_TIME_absolute_get ();
518 if (now.abs_value < s->next_receive.abs_value)
520 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
521 struct GNUNET_TIME_Relative delta =
522 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
523 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
524 "No inbound bandwidth for session %p available! Next read was delayed for %llu ms\n",
526 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
528 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
529 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
531 s->recv_wakeup_task =
532 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
533 return CURLPAUSE_ALL;
535 if (NULL == s->msg_tk)
536 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
537 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
543 * Task performing curl operations
545 * @param cls plugin as closure
546 * @param tc gnunet scheduler task context
549 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
552 * Function setting up file descriptors and scheduling task to run
554 * @param plugin plugin as closure
555 * @param now schedule task in 1ms, regardless of what curl may say
556 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
559 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
565 struct GNUNET_NETWORK_FDSet *grs;
566 struct GNUNET_NETWORK_FDSet *gws;
569 struct GNUNET_TIME_Relative timeout;
571 /* Cancel previous scheduled task */
572 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
574 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
575 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
581 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
582 if (mret != CURLM_OK)
584 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
585 "curl_multi_fdset", __FILE__, __LINE__,
586 curl_multi_strerror (mret));
587 return GNUNET_SYSERR;
589 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
591 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
593 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
594 if (now == GNUNET_YES)
595 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
597 if (mret != CURLM_OK)
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
600 "curl_multi_timeout", __FILE__, __LINE__,
601 curl_multi_strerror (mret));
602 return GNUNET_SYSERR;
605 grs = GNUNET_NETWORK_fdset_create ();
606 gws = GNUNET_NETWORK_fdset_create ();
607 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
608 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
610 plugin->client_perform_task =
611 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
613 &client_run, plugin);
614 GNUNET_NETWORK_fdset_destroy (gws);
615 GNUNET_NETWORK_fdset_destroy (grs);
622 client_disconnect (struct Session *s)
627 struct HTTP_Client_Plugin *plugin = s->plugin;
628 struct HTTP_Message *msg;
629 struct HTTP_Message *t;
631 if (GNUNET_YES != client_exist_session (plugin, s))
634 return GNUNET_SYSERR;
637 if (s->client_put != NULL)
639 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
640 "Client: %p / %p Deleting outbound PUT session to peer `%s'\n",
641 s, s->client_put, GNUNET_i2s (&s->target));
643 /* remove curl handle from multi handle */
644 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
645 if (mret != CURLM_OK)
647 /* clean up easy handle, handle is now invalid and free'd */
651 curl_easy_cleanup (s->client_put);
652 s->client_put = NULL;
656 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
658 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
659 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
662 if (s->client_get != NULL)
664 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
665 "Client: %p / %p Deleting outbound GET session to peer `%s'\n",
667 s->client_get, GNUNET_i2s (&s->target));
669 /* remove curl handle from multi handle */
670 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
671 if (mret != CURLM_OK)
673 /* clean up easy handle, handle is now invalid and free'd */
677 curl_easy_cleanup (s->client_get);
678 s->client_get = NULL;
685 if (NULL != msg->transmit_cont)
686 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
687 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
692 plugin->cur_connections -= 2;
693 plugin->env->session_end (plugin->env->cls, &s->target, s);
695 GNUNET_assert (plugin->cur_connections > 0);
696 plugin->cur_connections --;
697 GNUNET_STATISTICS_set (plugin->env->stats,
698 "# HTTP client connections",
699 plugin->cur_connections,
702 /* Re-schedule since handles have changed */
703 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
705 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
706 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
708 client_schedule (plugin, GNUNET_YES);
715 * Task performing curl operations
717 * @param cls plugin as closure
718 * @param tc gnunet scheduler task context
721 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
724 struct HTTP_Client_Plugin *plugin = cls;
728 GNUNET_assert (cls != NULL);
730 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
731 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
737 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
742 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
744 CURL *easy_h = msg->easy_handle;
745 struct Session *s = NULL;
746 char *d = (char *) s;
751 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
752 "Client: connection to ended with reason %i: `%s', %i handles running\n",
754 curl_easy_strerror (msg->data.result), running);
758 GNUNET_assert (CURLE_OK ==
759 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
760 s = (struct Session *) d;
762 if (GNUNET_YES != client_exist_session(plugin, s))
768 GNUNET_assert (s != NULL);
769 if (msg->msg == CURLMSG_DONE)
771 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
772 "Client: %p connection to '%s' %s ended with reason %i: `%s'\n",
773 msg->easy_handle, GNUNET_i2s (&s->target),
774 http_common_plugin_address_to_string (NULL, s->addr,
777 curl_easy_strerror (msg->data.result));
779 /* Disconnect other transmission direction and tell transport */
780 client_disconnect (s);
784 while (mret == CURLM_CALL_MULTI_PERFORM);
785 client_schedule (plugin, GNUNET_NO);
790 client_connect (struct Session *s)
793 struct HTTP_Client_Plugin *plugin = s->plugin;
799 if (NULL == http_common_plugin_address_to_string (NULL, s->addr, s->addrlen))
801 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
802 "Invalid address peer `%s'\n",
803 GNUNET_i2s (&s->target));
804 return GNUNET_SYSERR;
807 GNUNET_asprintf (&url, "%s%s;%u",
808 http_common_plugin_address_to_string (plugin, s->addr, s->addrlen),
809 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
812 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
813 "Initiating outbound session peer `%s' using address `%s'\n",
814 GNUNET_i2s (&s->target), url);
816 /* create get connection */
817 s->client_get = curl_easy_init ();
819 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
820 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
821 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
824 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
825 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
826 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
828 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
829 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
830 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
831 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
832 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
833 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
834 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
835 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
836 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
837 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
838 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
839 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
840 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
841 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
843 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
846 /* create put connection */
847 s->client_put = curl_easy_init ();
849 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
850 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
851 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
854 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
855 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
856 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
858 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
859 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
860 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
861 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
862 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
863 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
864 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
865 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
866 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
867 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
868 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
869 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
870 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
871 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
872 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
874 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
878 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_get);
879 if (mret != CURLM_OK)
881 curl_easy_cleanup (s->client_get);
883 return GNUNET_SYSERR;
886 mret = curl_multi_add_handle (plugin->curl_multi_handle, s->client_put);
887 if (mret != CURLM_OK)
889 curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
890 curl_easy_cleanup (s->client_get);
891 curl_easy_cleanup (s->client_put);
893 return GNUNET_SYSERR;
896 /* Perform connect */
897 plugin->cur_connections += 2;
898 GNUNET_STATISTICS_set (plugin->env->stats,
899 "# HTTP client connections",
900 plugin->cur_connections,
903 /* Re-schedule since handles have changed */
904 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
906 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
907 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
909 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
914 client_delete_session (struct Session *s)
916 struct HTTP_Client_Plugin *plugin = s->plugin;
917 struct HTTP_Message *pos = s->msg_head;
918 struct HTTP_Message *next = NULL;
920 client_stop_session_timeout (s);
922 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
924 while (NULL != (pos = next))
927 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
928 if (pos->transmit_cont != NULL)
929 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR);
933 if (s->msg_tk != NULL)
935 GNUNET_SERVER_mst_destroy (s->msg_tk);
938 GNUNET_free (s->addr);
944 * Creates a new outbound session the transport service will use to send data to the
947 * @param cls the plugin
948 * @param address the address
949 * @return the session or NULL of max connections exceeded
951 static struct Session *
952 http_client_plugin_get_session (void *cls,
953 const struct GNUNET_HELLO_Address *address)
955 struct HTTP_Client_Plugin *plugin = cls;
956 struct Session * s = NULL;
958 GNUNET_assert (plugin != NULL);
959 GNUNET_assert (address != NULL);
960 GNUNET_assert (address->address != NULL);
963 /* find existing session */
964 s = client_lookup_session (plugin, address);
968 if (plugin->max_connections <= plugin->cur_connections)
970 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
971 "Maximum number of connections (%u) reached: "
972 "cannot connect to peer `%s'\n",
973 plugin->max_connections,
974 GNUNET_i2s (&address->peer));
978 s = GNUNET_malloc (sizeof (struct Session));
979 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
981 s->addr = GNUNET_malloc (address->address_length);
982 memcpy (s->addr, address->address, address->address_length);
983 s->addrlen = address->address_length;
984 //s->ats_address_network_type = ats.value;
987 client_start_session_timeout (s);
989 /* add new session */
990 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
992 /* initiate new connection */
993 if (GNUNET_SYSERR == client_connect (s))
995 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
996 "Cannot connect to peer `%s' address `%s''\n",
997 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
998 GNUNET_i2s (&s->target));
999 client_delete_session (s);
1006 client_start (struct HTTP_Client_Plugin *plugin)
1008 curl_global_init (CURL_GLOBAL_ALL);
1009 plugin->curl_multi_handle = curl_multi_init ();
1011 if (NULL == plugin->curl_multi_handle)
1013 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1014 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1016 return GNUNET_SYSERR;
1022 * Session was idle, so disconnect it
1025 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1027 GNUNET_assert (NULL != cls);
1028 struct Session *s = cls;
1030 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1031 GNUNET_log (TIMEOUT_LOG,
1032 "Session %p was idle for %llu ms, disconnecting\n",
1033 s, (unsigned long long) TIMEOUT.rel_value);
1035 /* call session destroy function */
1036 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1040 * Start session timeout
1043 client_start_session_timeout (struct Session *s)
1046 GNUNET_assert (NULL != s);
1047 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1048 s->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1049 &client_session_timeout,
1051 GNUNET_log (TIMEOUT_LOG,
1052 "Timeout for session %p set to %llu ms\n",
1053 s, (unsigned long long) TIMEOUT.rel_value);
1058 * Increment session timeout due to activity
1061 client_reschedule_session_timeout (struct Session *s)
1064 GNUNET_assert (NULL != s);
1065 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1067 GNUNET_SCHEDULER_cancel (s->timeout_task);
1068 s->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1069 &client_session_timeout,
1071 GNUNET_log (TIMEOUT_LOG,
1072 "Timeout rescheduled for session %p set to %llu ms\n",
1073 s, (unsigned long long) TIMEOUT.rel_value);
1081 client_stop_session_timeout (struct Session *s)
1083 GNUNET_assert (NULL != s);
1085 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1087 GNUNET_SCHEDULER_cancel (s->timeout_task);
1088 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1089 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1095 * Another peer has suggested an address for this
1096 * peer and transport plugin. Check that this could be a valid
1097 * address. If so, consider adding it to the list
1100 * @param cls closure
1101 * @param addr pointer to the address
1102 * @param addrlen length of addr
1103 * @return GNUNET_OK if this is a plausible address for this peer
1107 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1109 /* struct Plugin *plugin = cls; */
1111 /* A HTTP/S client does not have any valid address so:*/
1116 * Exit point from the plugin.
1119 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1121 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1122 struct HTTP_Client_Plugin *plugin = api->cls;
1124 if (NULL == api->cls)
1130 if (NULL != plugin->curl_multi_handle)
1132 curl_multi_cleanup (plugin->curl_multi_handle);
1133 plugin->curl_multi_handle = NULL;
1135 curl_global_cleanup ();
1137 GNUNET_free (plugin);
1144 * Entry point for the plugin.
1147 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1149 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1150 struct GNUNET_TRANSPORT_PluginFunctions *api;
1151 struct HTTP_Client_Plugin *plugin;
1153 if (NULL == env->receive)
1155 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1156 initialze the plugin or the API */
1157 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1159 api->address_to_string = &http_common_plugin_address_to_string;
1160 api->string_to_address = &http_common_plugin_string_to_address;
1161 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1165 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1168 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1170 api->send = &http_client_plugin_send;
1171 api->disconnect = &http_client_plugin_disconnect;
1172 api->check_address = &http_client_plugin_address_suggested;
1173 api->get_session = &http_client_plugin_get_session;
1176 plugin->name = "transport-https_client";
1177 plugin->protocol = "https";
1179 plugin->name = "transport-http_client";
1180 plugin->protocol = "http";
1184 if (GNUNET_SYSERR == client_start (plugin))
1186 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1192 /* end of plugin_transport_http_client.c */