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
35 #define VERBOSE_CURL GNUNET_YES
37 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
39 #define ENABLE_PUT GNUNET_YES
40 #define ENABLE_GET GNUNET_YES
43 #include "gnunet_protocols.h"
44 #include "gnunet_common.h"
45 #include "gnunet_server_lib.h"
46 #include "gnunet_transport_plugin.h"
47 #include "plugin_transport_http_common.h"
48 #include <curl/curl.h>
53 * Encapsulation of all of the state of the plugin.
55 struct HTTP_Client_Plugin;
59 * Message to send using http
64 * next pointer for double linked list
66 struct HTTP_Message *next;
69 * previous pointer for double linked list
71 struct HTTP_Message *prev;
74 * buffer containing data to send
79 * amount of data already sent
89 * Continuation function to call once the transmission buffer
90 * has again space available. NULL if there is no
91 * continuation to call.
93 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
96 * Closure for transmit_cont.
98 void *transmit_cont_cls;
103 * Session handle for connections.
108 * A connection handle
111 struct ConnectionHandle
114 * The curl easy handle
119 * The related session
127 * Session handle for connections.
132 * To whom are we talking to (set to our identity
133 * if we are still waiting for the welcome message)
135 struct GNUNET_PeerIdentity target;
138 * Stored in a linked list.
140 struct Session *next;
143 * Stored in a linked list.
145 struct Session *prev;
148 * The URL to connect to
163 * ATS network type in NBO
165 uint32_t ats_address_network_type;
168 * Pointer to the global plugin struct.
170 struct HTTP_Client_Plugin *plugin;
177 struct ConnectionHandle put;
178 struct ConnectionHandle get;
181 * Is the client PUT handle currently paused
186 * Is the client PUT handle disconnect in progress?
188 int put_tmp_disconnecting;
191 * Is the client PUT handle temporarily disconnected?
193 int put_tmp_disconnected;
196 * We received data to send while disconnecting, reconnect immediately
198 int put_reconnect_required;
201 * Client receive handle
206 * Outbound overhead due to HTTP connection
207 * Add to next message of this session when calling callback
212 * next pointer for double linked list
214 struct HTTP_Message *msg_head;
217 * previous pointer for double linked list
219 struct HTTP_Message *msg_tail;
222 * Message stream tokenizer for incoming data
224 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
227 * Session timeout task
229 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
232 * Session timeout task
234 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
237 * Task to wake up client receive handle when receiving is allowed again
239 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
242 * Absolute time when to receive data again
243 * Used for receive throttling
245 struct GNUNET_TIME_Absolute next_receive;
250 * Encapsulation of all of the state of the plugin.
252 struct HTTP_Client_Plugin
257 struct GNUNET_TRANSPORT_PluginEnvironment *env;
260 * Linked list head of open sessions.
262 struct Session *head;
265 * Linked list tail of open sessions.
267 struct Session *tail;
280 * Maximum number of sockets the plugin can use
281 * Each http inbound /outbound connections are two connections
283 unsigned int max_connections;
286 * Current number of sockets the plugin can use
287 * Each http inbound /outbound connections are two connections
289 unsigned int cur_connections;
292 * Last used unique HTTP connection tag
309 CURLM *curl_multi_handle;
314 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
319 * Encapsulation of all of the state of the plugin.
321 struct HTTP_Client_Plugin *p;
325 * Start session timeout for a session
326 * @param s the session
329 client_start_session_timeout (struct Session *s);
333 * Increment session timeout due to activity for a session
334 * @param s the session
337 client_reschedule_session_timeout (struct Session *s);
341 * Cancel timeout for a session
342 * @param s the session
345 client_stop_session_timeout (struct Session *s);
349 * Function setting up file descriptors and scheduling task to run
351 * @param plugin plugin as closure
352 * @param now schedule task in 1ms, regardless of what curl may say
353 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
356 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
360 * Connect a HTTP put connection
362 * @param s the session to connect
363 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for success
366 client_connect_put (struct Session *s);
370 * Does a session s exists?
372 * @param plugin the plugin
373 * @param s desired session
374 * @return GNUNET_YES or GNUNET_NO
377 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
379 struct Session * head;
381 GNUNET_assert (NULL != plugin);
382 GNUNET_assert (NULL != s);
384 for (head = plugin->head; head != NULL; head = head->next)
396 * @param curl the curl easy handle
397 * @param type message type
398 * @param data data to log, NOT a 0-terminated string
399 * @param size data length
400 * @param cls the closure
404 client_log (CURL *curl, curl_infotype type,
405 const char *data, size_t size, void *cls)
407 struct ConnectionHandle *ch = cls;
408 const char *ttype = "UNSPECIFIED";
410 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
418 case CURLINFO_HEADER_IN:
421 case CURLINFO_HEADER_OUT:
422 ttype = "HEADER_OUT";
425 GNUNET_assert (NULL != ch);
426 GNUNET_assert (NULL != ch->easyhandle);
427 GNUNET_assert (NULL != ch->s);
428 ch->s->overhead += size;
431 ttype = "UNSPECIFIED";
435 memcpy (text, data, size);
436 if (text[size - 1] == '\n')
441 text[size + 1] = '\0';
444 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
445 "Connection %p %s: %s", ch->easyhandle, ttype, text);
447 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
448 "Connection %p %s: %s", ch->easyhandle, ttype, text);
456 * Function that can be used by the transport service to transmit
457 * a message using the plugin. Note that in the case of a
458 * peer disconnecting, the continuation MUST be called
459 * prior to the disconnect notification itself. This function
460 * will be called with this peer's HELLO message to initiate
461 * a fresh connection to another peer.
464 * @param s which session must be used
465 * @param msgbuf the message to transmit
466 * @param msgbuf_size number of bytes in 'msgbuf'
467 * @param priority how important is the message (most plugins will
468 * ignore message priority and just FIFO)
469 * @param to how long to wait at most for the transmission (does not
470 * require plugins to discard the message after the timeout,
471 * just advisory for the desired delay; most plugins will ignore
473 * @param cont continuation to call once the message has
474 * been transmitted (or if the transport is ready
475 * for the next transmission call; or if the
476 * peer disconnected...); can be NULL
477 * @param cont_cls closure for cont
478 * @return number of bytes used (on the physical network, with overheads);
479 * -1 on hard errors (i.e. address invalid); 0 is a legal value
480 * and does NOT mean that the message was not transmitted (DV)
483 http_client_plugin_send (void *cls,
485 const char *msgbuf, size_t msgbuf_size,
486 unsigned int priority,
487 struct GNUNET_TIME_Relative to,
488 GNUNET_TRANSPORT_TransmitContinuation cont,
491 struct HTTP_Client_Plugin *plugin = cls;
492 struct HTTP_Message *msg;
495 GNUNET_assert (plugin != NULL);
496 GNUNET_assert (s != NULL);
498 /* lookup if session is really existing */
499 if (GNUNET_YES != client_exist_session (plugin, s))
502 return GNUNET_SYSERR;
505 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
506 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
508 msgbuf_size, GNUNET_i2s (&s->target));
510 /* create new message and schedule */
511 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
513 msg->size = msgbuf_size;
515 msg->buf = (char *) &msg[1];
516 msg->transmit_cont = cont;
517 msg->transmit_cont_cls = cont_cls;
518 memcpy (msg->buf, msgbuf, msgbuf_size);
519 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
521 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
522 GNUNET_STATISTICS_update (plugin->env->stats,
523 stat_txt, msgbuf_size, GNUNET_NO);
525 GNUNET_free (stat_txt);
527 if (GNUNET_YES == s->put_tmp_disconnecting)
529 /* PUT connection is currently getting disconnected */
530 s->put_reconnect_required = GNUNET_YES;
531 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
532 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
536 else if (GNUNET_YES == s->put_paused)
538 /* PUT connection was paused, unpause */
539 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
540 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
541 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
542 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
543 "Session %p/connection %p: unpausing connection\n",
545 s->put_paused = GNUNET_NO;
546 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
548 else if (GNUNET_YES == s->put_tmp_disconnected)
550 /* PUT connection was disconnected, reconnect */
551 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
552 "Session %p: Reconnecting PUT connection\n",
554 s->put_tmp_disconnected = GNUNET_NO;
555 GNUNET_break (s->client_put == NULL);
556 if (GNUNET_SYSERR == client_connect_put (s))
558 return GNUNET_SYSERR;
562 client_schedule (s->plugin, GNUNET_YES);
563 client_reschedule_session_timeout (s);
571 * @param s the session to delete
574 client_delete_session (struct Session *s)
576 struct HTTP_Client_Plugin *plugin = s->plugin;
577 struct HTTP_Message *pos;
578 struct HTTP_Message *next;
580 client_stop_session_timeout (s);
582 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
584 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
585 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
588 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
591 while (NULL != (pos = next))
594 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
595 if (pos->transmit_cont != NULL)
596 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
597 pos->size, pos->pos + s->overhead);
602 if (s->msg_tk != NULL)
604 GNUNET_SERVER_mst_destroy (s->msg_tk);
607 GNUNET_free (s->addr);
608 GNUNET_free (s->url);
615 * Disconnect a session
618 * @return GNUNET_OK on success, GNUNET_SYSERR on error
621 client_disconnect (struct Session *s)
623 struct HTTP_Client_Plugin *plugin = s->plugin;
624 struct HTTP_Message *msg;
625 struct HTTP_Message *t;
629 if (GNUNET_YES != client_exist_session (plugin, s))
632 return GNUNET_SYSERR;
635 if (s->client_put != NULL)
637 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
638 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
639 s, s->client_put, GNUNET_i2s (&s->target));
641 /* remove curl handle from multi handle */
642 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
643 if (mret != CURLM_OK)
645 /* clean up easy handle, handle is now invalid and free'd */
649 curl_easy_cleanup (s->client_put);
650 s->client_put = NULL;
654 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
656 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
657 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
660 if (s->client_get != NULL)
662 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
663 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
664 s, s->client_get, GNUNET_i2s (&s->target));
666 /* remove curl handle from multi handle */
667 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
668 if (mret != CURLM_OK)
670 /* clean up easy handle, handle is now invalid and free'd */
674 curl_easy_cleanup (s->client_get);
675 s->client_get = NULL;
682 if (NULL != msg->transmit_cont)
683 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
684 msg->size, msg->pos + s->overhead);
686 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
691 GNUNET_assert (plugin->cur_connections >= 2);
692 plugin->cur_connections -= 2;
693 GNUNET_STATISTICS_set (plugin->env->stats,
694 "# HTTP client sessions",
695 plugin->cur_connections,
698 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
699 "Session %p: notifying transport about ending session\n",s);
701 plugin->env->session_end (plugin->env->cls, &s->target, s);
702 client_delete_session (s);
704 /* Re-schedule since handles have changed */
705 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
707 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
708 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
710 client_schedule (plugin, GNUNET_YES);
717 * Function that can be used to force the plugin to disconnect
718 * from the given peer and cancel all previous transmissions
719 * (and their continuationc).
722 * @param target peer from which to disconnect
725 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
727 struct HTTP_Client_Plugin *plugin = cls;
728 struct Session *next = NULL;
729 struct Session *pos = NULL;
731 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
732 "Transport tells me to disconnect `%s'\n",
733 GNUNET_i2s (target));
736 while (NULL != (pos = next))
739 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
741 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
742 "Disconnecting session %p to `%pos'\n",
743 pos, GNUNET_i2s (target));
744 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
751 * Check if a sessions exists for an specific address
753 * @param plugin the plugin
754 * @param address the address
755 * @return the session or NULL
757 static struct Session *
758 client_lookup_session (struct HTTP_Client_Plugin *plugin,
759 const struct GNUNET_HELLO_Address *address)
763 for (pos = plugin->head; NULL != pos; pos = pos->next)
764 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
765 (address->address_length == pos->addrlen) &&
766 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
772 client_put_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
774 struct Session *s = cls;
775 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
776 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
777 "Session %p/connection %p: will be disconnected due to no activity\n",
779 s->put_paused = GNUNET_NO;
780 s->put_tmp_disconnecting = GNUNET_YES;
781 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
782 client_schedule (s->plugin, GNUNET_YES);
788 * Callback method used with libcurl
789 * Method is called when libcurl needs to read data during sending
791 * @param stream pointer where to write data
792 * @param size size of an individual element
793 * @param nmemb count of elements that can be written to the buffer
794 * @param cls source pointer, passed to the libcurl handle
795 * @return bytes written to stream, returning 0 will terminate connection!
798 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
800 struct Session *s = cls;
801 struct HTTP_Client_Plugin *plugin = s->plugin;
802 struct HTTP_Message *msg = s->msg_head;
806 if (GNUNET_YES != client_exist_session (plugin, s))
811 if (GNUNET_YES == s->put_tmp_disconnecting)
814 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
815 "Session %p/connection %p: disconnect due to inactivity\n",
822 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
823 "Session %p/connection %p: nothing to send, suspending\n",
825 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
826 s->put_paused = GNUNET_YES;
827 return CURL_READFUNC_PAUSE;
830 GNUNET_assert (msg->pos < msg->size);
831 /* calculate how much fits in buffer */
832 len = GNUNET_MIN (msg->size - msg->pos,
834 memcpy (stream, &msg->buf[msg->pos], len);
836 if (msg->pos == msg->size)
838 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
839 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
840 s, s->client_put, msg->size, msg->pos);
841 /* Calling transmit continuation */
842 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
843 if (NULL != msg->transmit_cont)
844 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
845 msg->size, msg->size + s->overhead);
850 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
851 GNUNET_STATISTICS_update (plugin->env->stats,
852 stat_txt, -len, GNUNET_NO);
853 GNUNET_free (stat_txt);
855 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
856 GNUNET_STATISTICS_update (plugin->env->stats,
857 stat_txt, len, GNUNET_NO);
858 GNUNET_free (stat_txt);
860 client_reschedule_session_timeout (s);
866 * Wake up a curl handle which was suspended
868 * @param cls the session
869 * @param tc task context
872 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
874 struct Session *s = cls;
876 if (GNUNET_YES != client_exist_session(p, s))
881 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
882 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
884 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
885 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
886 if (s->client_get != NULL)
887 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
892 * Callback for message stream tokenizer
894 * @param cls the session
895 * @param client not used
896 * @param message the message received
897 * @return always GNUNET_OK
900 client_receive_mst_cb (void *cls, void *client,
901 const struct GNUNET_MessageHeader *message)
903 struct Session *s = cls;
904 struct HTTP_Client_Plugin *plugin;
905 struct GNUNET_TIME_Relative delay;
906 struct GNUNET_ATS_Information atsi[2];
908 if (GNUNET_YES != client_exist_session(p, s))
915 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
916 atsi[0].value = htonl (1);
917 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
918 atsi[1].value = s->ats_address_network_type;
919 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
921 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
922 s, s->addr, s->addrlen);
924 plugin->env->update_address_metrics (plugin->env->cls,
929 (struct GNUNET_ATS_Information *) &atsi, 2);
931 GNUNET_asprintf (&stat_txt, "# bytes received via %s_client", plugin->protocol);
932 GNUNET_STATISTICS_update (plugin->env->stats,
933 stat_txt, ntohs(message->size), GNUNET_NO);
934 GNUNET_free (stat_txt);
937 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
939 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
942 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
943 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
944 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen),
947 client_reschedule_session_timeout (s);
953 * Callback method used with libcurl when data for a PUT connection are
954 * received. We do not expect data here, so we just dismiss it
956 * @param stream pointer where to write data
957 * @param size size of an individual element
958 * @param nmemb count of elements that can be written to the buffer
959 * @param cls destination pointer, passed to the libcurl handle
960 * @return bytes read from stream
963 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
970 * Callback method used with libcurl when data for a GET connection are
971 * received. Forward to MST
973 * @param stream pointer where to write data
974 * @param size size of an individual element
975 * @param nmemb count of elements that can be written to the buffer
976 * @param cls destination pointer, passed to the libcurl handle
977 * @return bytes read from stream
980 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
982 struct Session *s = cls;
983 struct GNUNET_TIME_Absolute now;
984 size_t len = size * nmemb;
986 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
987 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
989 len, GNUNET_i2s (&s->target));
990 now = GNUNET_TIME_absolute_get ();
991 if (now.abs_value < s->next_receive.abs_value)
993 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
994 struct GNUNET_TIME_Relative delta =
995 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
996 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
997 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %llu ms\n",
998 s, s->client_get, delta.rel_value);
999 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1001 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1002 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1004 s->recv_wakeup_task =
1005 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1006 return CURL_WRITEFUNC_PAUSE;
1008 if (NULL == s->msg_tk)
1009 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1010 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1016 * Task performing curl operations
1018 * @param cls plugin as closure
1019 * @param tc gnunet scheduler task context
1022 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1026 * Function setting up file descriptors and scheduling task to run
1028 * @param plugin the plugin as closure
1029 * @param now schedule task in 1ms, regardless of what curl may say
1030 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1033 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1039 struct GNUNET_NETWORK_FDSet *grs;
1040 struct GNUNET_NETWORK_FDSet *gws;
1043 struct GNUNET_TIME_Relative timeout;
1045 /* Cancel previous scheduled task */
1046 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1048 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1049 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1055 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1056 if (mret != CURLM_OK)
1058 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1059 "curl_multi_fdset", __FILE__, __LINE__,
1060 curl_multi_strerror (mret));
1061 return GNUNET_SYSERR;
1063 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1065 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1067 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1068 if (now == GNUNET_YES)
1069 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1071 if (mret != CURLM_OK)
1073 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1074 "curl_multi_timeout", __FILE__, __LINE__,
1075 curl_multi_strerror (mret));
1076 return GNUNET_SYSERR;
1079 grs = GNUNET_NETWORK_fdset_create ();
1080 gws = GNUNET_NETWORK_fdset_create ();
1081 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1082 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1084 plugin->client_perform_task =
1085 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1087 &client_run, plugin);
1088 GNUNET_NETWORK_fdset_destroy (gws);
1089 GNUNET_NETWORK_fdset_destroy (grs);
1095 * Task performing curl operations
1097 * @param cls plugin as closure
1098 * @param tc gnunet scheduler task context
1101 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1103 struct HTTP_Client_Plugin *plugin = cls;
1105 long http_statuscode;
1108 GNUNET_assert (cls != NULL);
1110 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1111 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1117 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1122 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1124 CURL *easy_h = msg->easy_handle;
1125 struct Session *s = NULL;
1126 char *d = (char *) s;
1131 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1132 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1134 curl_easy_strerror (msg->data.result), running);
1138 GNUNET_assert (CURLE_OK ==
1139 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1140 s = (struct Session *) d;
1142 if (GNUNET_YES != client_exist_session(plugin, s))
1148 GNUNET_assert (s != NULL);
1149 if (msg->msg == CURLMSG_DONE)
1151 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1152 if (easy_h == s->client_put)
1154 if ((0 != msg->data.result) || (http_statuscode != 200))
1156 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1157 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1158 s, msg->easy_handle, GNUNET_i2s (&s->target),
1161 curl_easy_strerror (msg->data.result));
1164 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1165 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1166 s, msg->easy_handle, GNUNET_i2s (&s->target));
1167 if (s->client_get == NULL)
1169 /* Disconnect other transmission direction and tell transport */
1171 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1172 curl_easy_cleanup (easy_h);
1173 s->put_tmp_disconnecting = GNUNET_NO;
1174 s->put_tmp_disconnected = GNUNET_YES;
1175 s->client_put = NULL;
1176 s->put.easyhandle = NULL;
1180 * Handling a rare case:
1181 * plugin_send was called during temporary put disconnect,
1182 * reconnect required after connection was disconnected
1184 if (GNUNET_YES == s->put_reconnect_required)
1186 s->put_reconnect_required = GNUNET_NO;
1187 if (GNUNET_SYSERR == client_connect_put(s))
1189 GNUNET_break (s->client_put == NULL);
1190 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1194 if (easy_h == s->client_get)
1196 if ((0 != msg->data.result) || (http_statuscode != 200))
1198 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1199 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1200 s, msg->easy_handle, GNUNET_i2s (&s->target),
1203 curl_easy_strerror (msg->data.result));
1207 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1208 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1209 s, msg->easy_handle, GNUNET_i2s (&s->target));
1210 /* Disconnect other transmission direction and tell transport */
1211 s->get.easyhandle = NULL;
1213 client_disconnect (s);
1218 while (mret == CURLM_CALL_MULTI_PERFORM);
1219 client_schedule (plugin, GNUNET_NO);
1224 * Connect GET connection for a session
1226 * @param s the session to connect
1227 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1230 client_connect_get (struct Session *s)
1233 /* create get connection */
1234 s->client_get = curl_easy_init ();
1236 s->get.easyhandle = s->client_get;
1238 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1239 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1240 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1243 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1244 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1245 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1246 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1247 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1249 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1250 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1253 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1254 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1255 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1256 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1257 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1258 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1259 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1260 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1261 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1262 /* No timeout by default, timeout done with session timeout */
1263 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1264 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1265 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1266 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1267 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1268 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1269 #if CURL_TCP_NODELAY
1270 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1272 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1274 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1275 if (mret != CURLM_OK)
1277 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1278 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1279 s, curl_multi_strerror (mret));
1280 curl_easy_cleanup (s->client_get);
1281 s->client_get = NULL;
1283 s->get.easyhandle = NULL;
1285 return GNUNET_SYSERR;
1292 * Connect a HTTP put connection
1294 * @param s the session to connect
1295 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1298 client_connect_put (struct Session *s)
1301 /* create put connection */
1302 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1303 "Session %p : Init PUT handle \n", s);
1304 s->client_put = curl_easy_init ();
1306 s->put.easyhandle = s->client_put;
1308 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1309 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1310 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1313 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1314 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1315 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1316 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1317 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1319 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1320 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1322 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1323 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1324 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1325 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1326 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1327 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1328 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1329 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1330 /* No timeout by default, timeout done with session timeout */
1331 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1332 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1333 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1334 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1335 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1336 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1337 #if CURL_TCP_NODELAY
1338 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1340 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1341 if (mret != CURLM_OK)
1343 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1344 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1345 s, curl_multi_strerror (mret));
1346 curl_easy_cleanup (s->client_put);
1347 s->client_put = NULL;
1348 s->put.easyhandle = NULL;
1350 s->put_tmp_disconnected = GNUNET_YES;
1351 return GNUNET_SYSERR;
1353 s->put_tmp_disconnected = GNUNET_NO;
1359 * Connect both PUT and GET connection for a session
1361 * @param s the session to connect
1362 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1365 client_connect (struct Session *s)
1368 struct HTTP_Client_Plugin *plugin = s->plugin;
1369 int res = GNUNET_OK;
1373 if (NULL == http_common_plugin_address_to_string (NULL, s->addr, s->addrlen))
1375 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1376 "Invalid address peer `%s'\n",
1377 GNUNET_i2s (&s->target));
1378 return GNUNET_SYSERR;
1381 GNUNET_asprintf (&s->url, "%s/%s;%u",
1382 http_common_plugin_address_to_string (plugin, s->addr, s->addrlen),
1383 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
1388 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1389 "Initiating outbound session peer `%s' using address `%s'\n",
1390 GNUNET_i2s (&s->target), s->url);
1392 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1393 (GNUNET_SYSERR == client_connect_put (s)))
1396 return GNUNET_SYSERR;
1399 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1400 "Session %p: connected with connections GET %p and PUT %p\n",
1401 s, s->client_get, s->client_put);
1403 /* Perform connect */
1404 plugin->cur_connections += 2;
1405 GNUNET_STATISTICS_set (plugin->env->stats,
1406 "# HTTP client connections",
1407 plugin->cur_connections,
1410 /* Re-schedule since handles have changed */
1411 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1413 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1414 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1416 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1422 * Creates a new outbound session the transport service will use to send data to the
1425 * @param cls the plugin
1426 * @param address the address
1427 * @return the session or NULL of max connections exceeded
1429 static struct Session *
1430 http_client_plugin_get_session (void *cls,
1431 const struct GNUNET_HELLO_Address *address)
1433 struct HTTP_Client_Plugin *plugin = cls;
1434 struct Session * s = NULL;
1435 struct sockaddr *sa;
1436 struct GNUNET_ATS_Information ats;
1440 GNUNET_assert (plugin != NULL);
1441 GNUNET_assert (address != NULL);
1442 GNUNET_assert (address->address != NULL);
1444 /* find existing session */
1445 s = client_lookup_session (plugin, address);
1449 if (plugin->max_connections <= plugin->cur_connections)
1451 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1452 "Maximum number of connections (%u) reached: "
1453 "cannot connect to peer `%s'\n",
1454 plugin->max_connections,
1455 GNUNET_i2s (&address->peer));
1459 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1460 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1461 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1463 if (GNUNET_SYSERR == res)
1467 else if (GNUNET_YES == res)
1469 GNUNET_assert (NULL != sa);
1470 if (AF_INET == sa->sa_family)
1472 salen = sizeof (struct sockaddr_in);
1474 else if (AF_INET6 == sa->sa_family)
1476 salen = sizeof (struct sockaddr_in6);
1478 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1479 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1482 else if (GNUNET_NO == res)
1484 ats.value = htonl (GNUNET_ATS_COST_WAN);
1487 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1493 s = GNUNET_malloc (sizeof (struct Session));
1494 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1496 s->addr = GNUNET_malloc (address->address_length);
1497 memcpy (s->addr, address->address, address->address_length);
1498 s->addrlen = address->address_length;
1499 s->ats_address_network_type = ats.value;
1500 s->put_paused = GNUNET_NO;
1501 s->put_tmp_disconnecting = GNUNET_NO;
1502 s->put_tmp_disconnected = GNUNET_NO;
1503 client_start_session_timeout (s);
1505 /* add new session */
1506 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1508 /* initiate new connection */
1509 if (GNUNET_SYSERR == client_connect (s))
1511 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1512 "Cannot connect to peer `%s' address `%s''\n",
1513 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1514 GNUNET_i2s (&s->target));
1515 client_delete_session (s);
1523 * Setup http_client plugin
1525 * @param plugin the plugin handle
1526 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1529 client_start (struct HTTP_Client_Plugin *plugin)
1531 curl_global_init (CURL_GLOBAL_ALL);
1532 plugin->curl_multi_handle = curl_multi_init ();
1534 if (NULL == plugin->curl_multi_handle)
1536 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1537 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1539 return GNUNET_SYSERR;
1545 * Session was idle, so disconnect it
1548 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1550 GNUNET_assert (NULL != cls);
1551 struct Session *s = cls;
1553 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1554 GNUNET_log (TIMEOUT_LOG,
1555 "Session %p was idle for %llu ms, disconnecting\n",
1556 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1558 /* call session destroy function */
1559 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1564 * Start session timeout for session s
1566 * @param s the session
1569 client_start_session_timeout (struct Session *s)
1572 GNUNET_assert (NULL != s);
1573 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1574 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1575 &client_session_timeout,
1577 GNUNET_log (TIMEOUT_LOG,
1578 "Timeout for session %p set to %llu ms\n",
1579 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1584 * Increment session timeout due to activity for session s
1586 * param s the session
1589 client_reschedule_session_timeout (struct Session *s)
1592 GNUNET_assert (NULL != s);
1593 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1595 GNUNET_SCHEDULER_cancel (s->timeout_task);
1596 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1597 &client_session_timeout,
1599 GNUNET_log (TIMEOUT_LOG,
1600 "Timeout rescheduled for session %p set to %llu ms\n",
1601 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1606 * Cancel timeout due to activity for session s
1608 * param s the session
1611 client_stop_session_timeout (struct Session *s)
1613 GNUNET_assert (NULL != s);
1615 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1617 GNUNET_SCHEDULER_cancel (s->timeout_task);
1618 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1619 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1625 * Another peer has suggested an address for this
1626 * peer and transport plugin. Check that this could be a valid
1627 * address. If so, consider adding it to the list
1630 * @param cls closure
1631 * @param addr pointer to the address
1632 * @param addrlen length of addr
1633 * @return GNUNET_OK if this is a plausible address for this peer
1637 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1639 /* struct Plugin *plugin = cls; */
1641 /* A HTTP/S client does not have any valid address so:*/
1647 * Exit point from the plugin.
1649 * @param cls api as closure
1653 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1655 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1656 struct HTTP_Client_Plugin *plugin = api->cls;
1657 struct Session *pos;
1658 struct Session *next;
1660 if (NULL == api->cls)
1667 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1668 _("Shutting down plugin `%s'\n"),
1672 next = plugin->head;
1673 while (NULL != (pos = next))
1676 client_disconnect (pos);
1678 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1680 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1681 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1685 if (NULL != plugin->curl_multi_handle)
1687 curl_multi_cleanup (plugin->curl_multi_handle);
1688 plugin->curl_multi_handle = NULL;
1690 curl_global_cleanup ();
1692 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1693 _("Shutdown for plugin `%s' complete\n"),
1696 GNUNET_free (plugin);
1705 * @param plugin the plugin handle
1706 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1709 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1711 unsigned long long max_connections;
1713 /* Optional parameters */
1714 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1716 "MAX_CONNECTIONS", &max_connections))
1717 max_connections = 128;
1718 plugin->max_connections = max_connections;
1720 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1721 _("Maximum number of connections is %u\n"),
1722 plugin->max_connections);
1727 * Entry point for the plugin.
1730 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1732 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1733 struct GNUNET_TRANSPORT_PluginFunctions *api;
1734 struct HTTP_Client_Plugin *plugin;
1736 if (NULL == env->receive)
1738 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1739 initialze the plugin or the API */
1740 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1742 api->address_to_string = &http_common_plugin_address_to_string;
1743 api->string_to_address = &http_common_plugin_string_to_address;
1744 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1748 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1751 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1753 api->send = &http_client_plugin_send;
1754 api->disconnect = &http_client_plugin_disconnect;
1755 api->check_address = &http_client_plugin_address_suggested;
1756 api->get_session = &http_client_plugin_get_session;
1757 api->address_to_string = &http_common_plugin_address_to_string;
1758 api->string_to_address = &http_common_plugin_string_to_address;
1759 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1763 plugin->name = "transport-https_client";
1764 plugin->protocol = "https";
1766 plugin->name = "transport-http_client";
1767 plugin->protocol = "http";
1769 plugin->last_tag = 1;
1771 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1773 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1778 if (GNUNET_SYSERR == client_start (plugin))
1780 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1786 /* end of plugin_transport_http_client.c */