2 This file is part of GNUnet
3 (C) 2002-2013 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 PLUGIN_NAME "https_client"
29 #define HTTP_STAT_STR_CONNECTIONS "# HTTPS client connections"
30 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
31 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
33 #define PLUGIN_NAME "http_client"
34 #define HTTP_STAT_STR_CONNECTIONS "# HTTP client connections"
35 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
36 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
39 #define VERBOSE_CURL GNUNET_YES
41 #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
43 #define ENABLE_PUT GNUNET_YES
44 #define ENABLE_GET GNUNET_YES
47 #include "gnunet_util_lib.h"
48 #include "gnunet_protocols.h"
49 #include "gnunet_transport_plugin.h"
50 #include "plugin_transport_http_common.h"
51 #include <curl/curl.h>
56 * Encapsulation of all of the state of the plugin.
58 struct HTTP_Client_Plugin;
62 * Message to send using http
67 * next pointer for double linked list
69 struct HTTP_Message *next;
72 * previous pointer for double linked list
74 struct HTTP_Message *prev;
77 * buffer containing data to send
82 * amount of data already sent
92 * Continuation function to call once the transmission buffer
93 * has again space available. NULL if there is no
94 * continuation to call.
96 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
99 * Closure for transmit_cont.
101 void *transmit_cont_cls;
106 * Session handle for connections.
112 * A connection handle
115 struct ConnectionHandle
118 * The curl easy handle
123 * The related session
130 * Session handle for connections.
135 * To whom are we talking to (set to our identity
136 * if we are still waiting for the welcome message)
138 struct GNUNET_PeerIdentity target;
141 * Stored in a linked list.
143 struct Session *next;
146 * Stored in a linked list.
148 struct Session *prev;
151 * The URL to connect to
158 struct HttpAddress *addr;
166 * ATS network type in NBO
168 uint32_t ats_address_network_type;
171 * Pointer to the global plugin struct.
173 struct HTTP_Client_Plugin *plugin;
180 struct ConnectionHandle put;
181 struct ConnectionHandle get;
184 * Is the client PUT handle currently paused
189 * Is the client PUT handle disconnect in progress?
191 int put_tmp_disconnecting;
194 * Is the client PUT handle temporarily disconnected?
196 int put_tmp_disconnected;
199 * We received data to send while disconnecting, reconnect immediately
201 int put_reconnect_required;
204 * Client receive handle
209 * Outbound overhead due to HTTP connection
210 * Add to next message of this session when calling callback
215 * next pointer for double linked list
217 struct HTTP_Message *msg_head;
220 * previous pointer for double linked list
222 struct HTTP_Message *msg_tail;
225 * Message stream tokenizer for incoming data
227 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
230 * Session timeout task
232 GNUNET_SCHEDULER_TaskIdentifier put_disconnect_task;
235 * Session timeout task
237 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
240 * Task to wake up client receive handle when receiving is allowed again
242 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
245 * Absolute time when to receive data again
246 * Used for receive throttling
248 struct GNUNET_TIME_Absolute next_receive;
253 * Encapsulation of all of the state of the plugin.
255 struct HTTP_Client_Plugin
260 struct GNUNET_TRANSPORT_PluginEnvironment *env;
263 * Linked list head of open sessions.
265 struct Session *head;
268 * Linked list tail of open sessions.
270 struct Session *tail;
283 * My options to be included in the address
288 * Maximum number of sockets the plugin can use
289 * Each http inbound /outbound connections are two connections
291 unsigned int max_connections;
294 * Current number of sockets the plugin can use
295 * Each http inbound /outbound connections are two connections
297 unsigned int cur_connections;
300 * Last used unique HTTP connection tag
317 CURLM *curl_multi_handle;
322 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
327 * Increment session timeout due to activity for a session
328 * @param s the session
331 client_reschedule_session_timeout (struct Session *s);
335 * Function setting up file descriptors and scheduling task to run
337 * @param plugin plugin as closure
338 * @param now schedule task in 1ms, regardless of what curl may say
339 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
342 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
346 * Connect a HTTP put connection
348 * @param s the session to connect
349 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
352 client_connect_put (struct Session *s);
356 * Does a session s exists?
358 * @param plugin the plugin
359 * @param s desired session
360 * @return #GNUNET_YES or #GNUNET_NO
363 client_exist_session (struct HTTP_Client_Plugin *plugin,
366 struct Session * head;
368 for (head = plugin->head; head != NULL; head = head->next)
378 * @param curl the curl easy handle
379 * @param type message type
380 * @param data data to log, NOT a 0-terminated string
381 * @param size data length
382 * @param cls the closure
386 client_log (CURL *curl, curl_infotype type,
387 const char *data, size_t size, void *cls)
389 struct ConnectionHandle *ch = cls;
390 const char *ttype = "UNSPECIFIED";
392 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
400 case CURLINFO_HEADER_IN:
403 case CURLINFO_HEADER_OUT:
404 ttype = "HEADER_OUT";
407 GNUNET_assert (NULL != ch);
408 GNUNET_assert (NULL != ch->easyhandle);
409 GNUNET_assert (NULL != ch->s);
410 ch->s->overhead += size;
413 ttype = "UNSPECIFIED";
417 memcpy (text, data, size);
418 if (text[size - 1] == '\n')
423 text[size + 1] = '\0';
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
427 "Connection %p %s: %s", ch->easyhandle, ttype, text);
429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
430 "Connection %p %s: %s", ch->easyhandle, ttype, text);
438 * Function that can be used by the transport service to transmit
439 * a message using the plugin. Note that in the case of a
440 * peer disconnecting, the continuation MUST be called
441 * prior to the disconnect notification itself. This function
442 * will be called with this peer's HELLO message to initiate
443 * a fresh connection to another peer.
446 * @param s which session must be used
447 * @param msgbuf the message to transmit
448 * @param msgbuf_size number of bytes in 'msgbuf'
449 * @param priority how important is the message (most plugins will
450 * ignore message priority and just FIFO)
451 * @param to how long to wait at most for the transmission (does not
452 * require plugins to discard the message after the timeout,
453 * just advisory for the desired delay; most plugins will ignore
455 * @param cont continuation to call once the message has
456 * been transmitted (or if the transport is ready
457 * for the next transmission call; or if the
458 * peer disconnected...); can be NULL
459 * @param cont_cls closure for cont
460 * @return number of bytes used (on the physical network, with overheads);
461 * -1 on hard errors (i.e. address invalid); 0 is a legal value
462 * and does NOT mean that the message was not transmitted (DV)
465 http_client_plugin_send (void *cls,
467 const char *msgbuf, size_t msgbuf_size,
468 unsigned int priority,
469 struct GNUNET_TIME_Relative to,
470 GNUNET_TRANSPORT_TransmitContinuation cont,
473 struct HTTP_Client_Plugin *plugin = cls;
474 struct HTTP_Message *msg;
477 GNUNET_assert (plugin != NULL);
478 GNUNET_assert (s != NULL);
480 /* lookup if session is really existing */
481 if (GNUNET_YES != client_exist_session (plugin, s))
484 return GNUNET_SYSERR;
487 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
488 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
490 msgbuf_size, GNUNET_i2s (&s->target));
492 /* create new message and schedule */
493 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
495 msg->size = msgbuf_size;
497 msg->buf = (char *) &msg[1];
498 msg->transmit_cont = cont;
499 msg->transmit_cont_cls = cont_cls;
500 memcpy (msg->buf, msgbuf, msgbuf_size);
501 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
503 GNUNET_asprintf (&stat_txt,
504 "# bytes currently in %s_client buffers",
506 GNUNET_STATISTICS_update (plugin->env->stats,
507 stat_txt, msgbuf_size, GNUNET_NO);
508 GNUNET_free (stat_txt);
510 if (GNUNET_YES == s->put_tmp_disconnecting)
512 /* PUT connection is currently getting disconnected */
513 s->put_reconnect_required = GNUNET_YES;
514 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
515 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
519 else if (GNUNET_YES == s->put_paused)
521 /* PUT connection was paused, unpause */
522 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
523 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
524 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
525 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
526 "Session %p/connection %p: unpausing connection\n",
528 s->put_paused = GNUNET_NO;
529 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
531 else if (GNUNET_YES == s->put_tmp_disconnected)
533 /* PUT connection was disconnected, reconnect */
534 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
535 "Session %p: Reconnecting PUT connection\n",
537 s->put_tmp_disconnected = GNUNET_NO;
538 GNUNET_break (s->client_put == NULL);
539 if (GNUNET_SYSERR == client_connect_put (s))
541 return GNUNET_SYSERR;
545 client_schedule (s->plugin, GNUNET_YES);
546 client_reschedule_session_timeout (s);
554 * @param s the session to delete
557 client_delete_session (struct Session *s)
559 struct HTTP_Client_Plugin *plugin = s->plugin;
560 struct HTTP_Message *pos;
561 struct HTTP_Message *next;
563 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
565 GNUNET_SCHEDULER_cancel (s->timeout_task);
566 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
568 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
570 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
571 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
574 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
577 while (NULL != (pos = next))
580 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
581 if (pos->transmit_cont != NULL)
582 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
583 pos->size, pos->pos + s->overhead);
588 if (s->msg_tk != NULL)
590 GNUNET_SERVER_mst_destroy (s->msg_tk);
593 GNUNET_free (s->addr);
594 GNUNET_free (s->url);
600 * Disconnect a session
602 * @param cls the `struct HTTP_Client_Plugin`
604 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
607 http_client_session_disconnect (void *cls,
610 struct HTTP_Client_Plugin *plugin = cls;
611 struct HTTP_Message *msg;
612 struct HTTP_Message *t;
616 if (GNUNET_YES != client_exist_session (plugin, s))
619 return GNUNET_SYSERR;
622 if (s->client_put != NULL)
624 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
625 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
626 s, s->client_put, GNUNET_i2s (&s->target));
628 /* remove curl handle from multi handle */
629 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
630 if (mret != CURLM_OK)
632 /* clean up easy handle, handle is now invalid and free'd */
636 curl_easy_cleanup (s->client_put);
637 s->client_put = NULL;
641 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
643 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
644 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
647 if (s->client_get != NULL)
649 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
650 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
651 s, s->client_get, GNUNET_i2s (&s->target));
653 /* remove curl handle from multi handle */
654 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
655 if (mret != CURLM_OK)
657 /* clean up easy handle, handle is now invalid and free'd */
661 curl_easy_cleanup (s->client_get);
662 s->client_get = NULL;
669 if (NULL != msg->transmit_cont)
670 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
671 msg->size, msg->pos + s->overhead);
673 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
678 GNUNET_assert (plugin->cur_connections >= 2);
679 plugin->cur_connections -= 2;
680 GNUNET_STATISTICS_set (plugin->env->stats,
681 HTTP_STAT_STR_CONNECTIONS,
682 plugin->cur_connections,
684 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
685 "Session %p: notifying transport about ending session\n",s);
687 plugin->env->session_end (plugin->env->cls, &s->target, s);
688 client_delete_session (s);
690 /* Re-schedule since handles have changed */
691 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
693 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
694 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
696 client_schedule (plugin, GNUNET_YES);
703 * Function that can be used to force the plugin to disconnect
704 * from the given peer and cancel all previous transmissions
705 * (and their continuationc).
708 * @param target peer from which to disconnect
711 http_client_peer_disconnect (void *cls,
712 const struct GNUNET_PeerIdentity *target)
714 struct HTTP_Client_Plugin *plugin = cls;
715 struct Session *next = NULL;
716 struct Session *pos = NULL;
718 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
719 "Transport tells me to disconnect `%s'\n",
720 GNUNET_i2s (target));
723 while (NULL != (pos = next))
726 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
728 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
729 "Disconnecting session %p to `%pos'\n",
730 pos, GNUNET_i2s (target));
731 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (plugin,
739 * Check if a sessions exists for an specific address
741 * @param plugin the plugin
742 * @param address the address
743 * @return the session or NULL
745 static struct Session *
746 client_lookup_session (struct HTTP_Client_Plugin *plugin,
747 const struct GNUNET_HELLO_Address *address)
751 for (pos = plugin->head; NULL != pos; pos = pos->next)
752 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
753 (address->address_length == pos->addrlen) &&
754 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
761 client_put_disconnect (void *cls,
762 const struct GNUNET_SCHEDULER_TaskContext *tc)
764 struct Session *s = cls;
766 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
767 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
768 "Session %p/connection %p: will be disconnected due to no activity\n",
770 s->put_paused = GNUNET_NO;
771 s->put_tmp_disconnecting = GNUNET_YES;
772 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
773 client_schedule (s->plugin, GNUNET_YES);
778 * Callback method used with libcurl
779 * Method is called when libcurl needs to read data during sending
781 * @param stream pointer where to write data
782 * @param size size of an individual element
783 * @param nmemb count of elements that can be written to the buffer
784 * @param cls our `struct Session`
785 * @return bytes written to stream, returning 0 will terminate connection!
788 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
790 struct Session *s = cls;
791 struct HTTP_Client_Plugin *plugin = s->plugin;
792 struct HTTP_Message *msg = s->msg_head;
796 if (GNUNET_YES != client_exist_session (plugin, s))
801 if (GNUNET_YES == s->put_tmp_disconnecting)
804 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
805 "Session %p/connection %p: disconnect due to inactivity\n",
812 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
813 "Session %p/connection %p: nothing to send, suspending\n",
815 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
816 s->put_paused = GNUNET_YES;
817 return CURL_READFUNC_PAUSE;
820 GNUNET_assert (msg->pos < msg->size);
821 /* calculate how much fits in buffer */
822 len = GNUNET_MIN (msg->size - msg->pos,
824 memcpy (stream, &msg->buf[msg->pos], len);
826 if (msg->pos == msg->size)
828 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
829 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
830 s, s->client_put, msg->size, msg->pos);
831 /* Calling transmit continuation */
832 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
833 if (NULL != msg->transmit_cont)
834 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
835 msg->size, msg->size + s->overhead);
840 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
841 GNUNET_STATISTICS_update (plugin->env->stats,
842 stat_txt, -len, GNUNET_NO);
843 GNUNET_free (stat_txt);
845 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
846 GNUNET_STATISTICS_update (plugin->env->stats,
847 stat_txt, len, GNUNET_NO);
848 GNUNET_free (stat_txt);
850 client_reschedule_session_timeout (s);
856 * Wake up a curl handle which was suspended
858 * @param cls the session
859 * @param tc task context
862 client_wake_up (void *cls,
863 const struct GNUNET_SCHEDULER_TaskContext *tc)
865 struct Session *s = cls;
866 struct HTTP_Client_Plugin *p = s->plugin;
868 if (GNUNET_YES != client_exist_session (p, s))
873 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
874 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
876 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
877 "Session %p/connection %p: Waking up GET handle\n",
880 if (s->client_get != NULL)
881 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
886 * Callback for message stream tokenizer
888 * @param cls the session
889 * @param client not used
890 * @param message the message received
891 * @return always #GNUNET_OK
894 client_receive_mst_cb (void *cls, void *client,
895 const struct GNUNET_MessageHeader *message)
897 struct Session *s = cls;
898 struct HTTP_Client_Plugin *plugin;
899 struct GNUNET_TIME_Relative delay;
900 struct GNUNET_ATS_Information atsi;
904 if (GNUNET_YES != client_exist_session (plugin, s))
910 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
911 atsi.value = s->ats_address_network_type;
912 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
913 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
914 s, (const char *) s->addr, s->addrlen);
916 plugin->env->update_address_metrics (plugin->env->cls,
923 GNUNET_asprintf (&stat_txt,
924 "# bytes received via %s_client",
926 GNUNET_STATISTICS_update (plugin->env->stats,
927 stat_txt, ntohs(message->size), GNUNET_NO);
928 GNUNET_free (stat_txt);
931 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
933 if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
935 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
936 "Client: peer `%s' address `%s' next read delayed for %s\n",
937 GNUNET_i2s (&s->target),
938 http_common_plugin_address_to_string (NULL,
940 s->addr, s->addrlen),
941 GNUNET_STRINGS_relative_time_to_string (delay,
944 client_reschedule_session_timeout (s);
950 * Callback method used with libcurl when data for a PUT connection are
951 * received. We do not expect data here, so we just dismiss it
953 * @param stream pointer where to write data
954 * @param size size of an individual element
955 * @param nmemb count of elements that can be written to the buffer
956 * @param cls destination pointer, passed to the libcurl handle
957 * @return bytes read from stream
960 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
967 * Callback method used with libcurl when data for a GET connection are
968 * received. Forward to MST
970 * @param stream pointer where to write data
971 * @param size size of an individual element
972 * @param nmemb count of elements that can be written to the buffer
973 * @param cls destination pointer, passed to the libcurl handle
974 * @return bytes read from stream
977 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
979 struct Session *s = cls;
980 struct GNUNET_TIME_Absolute now;
981 size_t len = size * nmemb;
983 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
984 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
986 len, GNUNET_i2s (&s->target));
987 now = GNUNET_TIME_absolute_get ();
988 if (now.abs_value_us < s->next_receive.abs_value_us)
990 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
991 struct GNUNET_TIME_Relative delta =
992 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
993 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
994 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %s\n",
996 GNUNET_STRINGS_relative_time_to_string (delta,
998 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1000 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1001 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1003 s->recv_wakeup_task =
1004 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1005 return CURL_WRITEFUNC_PAUSE;
1007 if (NULL == s->msg_tk)
1008 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1009 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1015 * Task performing curl operations
1017 * @param cls plugin as closure
1018 * @param tc gnunet scheduler task context
1021 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1025 * Function setting up file descriptors and scheduling task to run
1027 * @param plugin the plugin as closure
1028 * @param now schedule task in 1ms, regardless of what curl may say
1029 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1032 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1038 struct GNUNET_NETWORK_FDSet *grs;
1039 struct GNUNET_NETWORK_FDSet *gws;
1042 struct GNUNET_TIME_Relative timeout;
1044 /* Cancel previous scheduled task */
1045 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1047 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1048 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1054 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1055 if (mret != CURLM_OK)
1057 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1058 "curl_multi_fdset", __FILE__, __LINE__,
1059 curl_multi_strerror (mret));
1060 return GNUNET_SYSERR;
1062 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1064 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1066 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1067 if (now == GNUNET_YES)
1068 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1070 if (mret != CURLM_OK)
1072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1073 "curl_multi_timeout", __FILE__, __LINE__,
1074 curl_multi_strerror (mret));
1075 return GNUNET_SYSERR;
1078 grs = GNUNET_NETWORK_fdset_create ();
1079 gws = GNUNET_NETWORK_fdset_create ();
1080 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1081 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1083 plugin->client_perform_task =
1084 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1086 &client_run, plugin);
1087 GNUNET_NETWORK_fdset_destroy (gws);
1088 GNUNET_NETWORK_fdset_destroy (grs);
1094 * Task performing curl operations
1096 * @param cls plugin as closure
1097 * @param tc gnunet scheduler task context
1100 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1102 struct HTTP_Client_Plugin *plugin = cls;
1104 long http_statuscode;
1107 GNUNET_assert (cls != NULL);
1109 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1110 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1116 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1121 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1123 CURL *easy_h = msg->easy_handle;
1124 struct Session *s = NULL;
1125 char *d = (char *) s;
1130 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1131 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1133 curl_easy_strerror (msg->data.result), running);
1137 GNUNET_assert (CURLE_OK ==
1138 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1139 s = (struct Session *) d;
1141 if (GNUNET_YES != client_exist_session(plugin, s))
1147 GNUNET_assert (s != NULL);
1148 if (msg->msg == CURLMSG_DONE)
1150 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1151 if (easy_h == s->client_put)
1153 if ((0 != msg->data.result) || (http_statuscode != 200))
1155 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1156 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1157 s, msg->easy_handle, GNUNET_i2s (&s->target),
1160 curl_easy_strerror (msg->data.result));
1163 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1164 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1165 s, msg->easy_handle, GNUNET_i2s (&s->target));
1166 if (s->client_get == NULL)
1168 /* Disconnect other transmission direction and tell transport */
1170 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1171 curl_easy_cleanup (easy_h);
1172 s->put_tmp_disconnecting = GNUNET_NO;
1173 s->put_tmp_disconnected = GNUNET_YES;
1174 s->client_put = NULL;
1175 s->put.easyhandle = NULL;
1179 * Handling a rare case:
1180 * plugin_send was called during temporary put disconnect,
1181 * reconnect required after connection was disconnected
1183 if (GNUNET_YES == s->put_reconnect_required)
1185 s->put_reconnect_required = GNUNET_NO;
1186 if (GNUNET_SYSERR == client_connect_put(s))
1188 GNUNET_break (s->client_put == NULL);
1189 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1193 if (easy_h == s->client_get)
1195 if ((0 != msg->data.result) || (http_statuscode != 200))
1197 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1198 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1199 s, msg->easy_handle, GNUNET_i2s (&s->target),
1202 curl_easy_strerror (msg->data.result));
1206 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1207 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1208 s, msg->easy_handle, GNUNET_i2s (&s->target));
1209 /* Disconnect other transmission direction and tell transport */
1210 s->get.easyhandle = NULL;
1212 http_client_session_disconnect (plugin, s);
1217 while (mret == CURLM_CALL_MULTI_PERFORM);
1218 client_schedule (plugin, GNUNET_NO);
1223 * Connect GET connection for a session
1225 * @param s the session to connect
1226 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1229 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 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1245 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1247 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1248 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1252 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1253 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1255 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1256 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1258 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1259 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1262 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1263 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1264 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1265 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1266 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1267 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1268 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1269 /* No timeout by default, timeout done with session timeout */
1270 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1271 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1272 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1273 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1274 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1275 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1276 #if CURL_TCP_NODELAY
1277 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1279 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1281 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1282 if (mret != CURLM_OK)
1284 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1285 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1287 curl_multi_strerror (mret));
1288 curl_easy_cleanup (s->client_get);
1289 s->client_get = NULL;
1291 s->get.easyhandle = NULL;
1293 return GNUNET_SYSERR;
1301 * Connect a HTTP put connection
1303 * @param s the session to connect
1304 * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
1307 client_connect_put (struct Session *s)
1311 /* create put connection */
1312 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1313 "Session %p : Init PUT handle\n", s);
1314 s->client_put = curl_easy_init ();
1316 s->put.easyhandle = s->client_put;
1318 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1319 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1320 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1323 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1324 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1325 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1327 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1328 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1332 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1333 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1335 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1336 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1338 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1339 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1341 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1342 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1343 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1344 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1345 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1346 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1347 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1348 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1349 /* No timeout by default, timeout done with session timeout */
1350 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1351 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1352 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1353 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL);
1354 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1355 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1356 #if CURL_TCP_NODELAY
1357 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1359 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1360 if (mret != CURLM_OK)
1362 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1363 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1365 curl_multi_strerror (mret));
1366 curl_easy_cleanup (s->client_put);
1367 s->client_put = NULL;
1368 s->put.easyhandle = NULL;
1370 s->put_tmp_disconnected = GNUNET_YES;
1371 return GNUNET_SYSERR;
1373 s->put_tmp_disconnected = GNUNET_NO;
1379 * Connect both PUT and GET connection for a session
1381 * @param s the session to connect
1382 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1385 client_connect (struct Session *s)
1388 struct HTTP_Client_Plugin *plugin = s->plugin;
1389 int res = GNUNET_OK;
1392 if (NULL == http_common_plugin_address_to_string (NULL,
1394 s->addr, s->addrlen))
1396 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1398 "Invalid address peer `%s'\n",
1399 GNUNET_i2s (&s->target));
1400 return GNUNET_SYSERR;
1403 GNUNET_asprintf (&s->url, "%s/%s;%u",
1404 http_common_plugin_address_to_url (NULL, s->addr, s->addrlen),
1405 GNUNET_i2s_full (plugin->env->my_identity),
1410 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1411 "Initiating outbound session peer `%s' using address `%s'\n",
1412 GNUNET_i2s (&s->target), s->url);
1414 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1415 (GNUNET_SYSERR == client_connect_put (s)))
1418 return GNUNET_SYSERR;
1421 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1423 "Session %p: connected with connections GET %p and PUT %p\n",
1424 s, s->client_get, s->client_put);
1426 /* Perform connect */
1427 plugin->cur_connections += 2;
1428 GNUNET_STATISTICS_set (plugin->env->stats,
1429 HTTP_STAT_STR_CONNECTIONS,
1430 plugin->cur_connections,
1433 /* Re-schedule since handles have changed */
1434 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1436 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1437 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1439 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1445 * Function obtain the network type for a session
1447 * @param cls closure (`struct Plugin*`)
1448 * @param session the session
1449 * @return the network type
1451 static enum GNUNET_ATS_Network_Type
1452 http_client_get_network (void *cls,
1453 struct Session *session)
1455 return ntohl (session->ats_address_network_type);
1460 * Session was idle, so disconnect it
1462 * @param cls the `struct Session` of the idle session
1463 * @param tc scheduler context
1466 client_session_timeout (void *cls,
1467 const struct GNUNET_SCHEDULER_TaskContext *tc)
1469 struct Session *s = cls;
1471 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1472 GNUNET_log (TIMEOUT_LOG,
1473 "Session %p was idle for %s, disconnecting\n",
1475 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1478 /* call session destroy function */
1479 GNUNET_assert (GNUNET_OK == http_client_session_disconnect (s->plugin,
1485 * Creates a new outbound session the transport service will use to
1486 * send data to the peer
1488 * @param cls the plugin
1489 * @param address the address
1490 * @return the session or NULL of max connections exceeded
1492 static struct Session *
1493 http_client_plugin_get_session (void *cls,
1494 const struct GNUNET_HELLO_Address *address)
1496 struct HTTP_Client_Plugin *plugin = cls;
1497 struct Session * s = NULL;
1498 struct sockaddr *sa;
1499 struct GNUNET_ATS_Information ats;
1503 GNUNET_assert (address->address != NULL);
1505 /* find existing session */
1506 s = client_lookup_session (plugin, address);
1510 if (plugin->max_connections <= plugin->cur_connections)
1512 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1513 "Maximum number of connections (%u) reached: "
1514 "cannot connect to peer `%s'\n",
1515 plugin->max_connections,
1516 GNUNET_i2s (&address->peer));
1520 /* Determine network location */
1521 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1522 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1523 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1524 if (GNUNET_SYSERR == res)
1528 else if (GNUNET_YES == res)
1530 GNUNET_assert (NULL != sa);
1531 if (AF_INET == sa->sa_family)
1533 salen = sizeof (struct sockaddr_in);
1535 else if (AF_INET6 == sa->sa_family)
1537 salen = sizeof (struct sockaddr_in6);
1539 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1540 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1543 else if (GNUNET_NO == res)
1545 /* Cannot convert to sockaddr -> is external hostname */
1546 ats.value = htonl (GNUNET_ATS_NET_WAN);
1548 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl (ats.value))
1554 s = GNUNET_new (struct Session);
1555 s->target = address->peer;
1557 s->addr = GNUNET_malloc (address->address_length);
1558 memcpy (s->addr, address->address, address->address_length);
1559 s->addrlen = address->address_length;
1560 s->ats_address_network_type = ats.value;
1561 s->put_paused = GNUNET_NO;
1562 s->put_tmp_disconnecting = GNUNET_NO;
1563 s->put_tmp_disconnected = GNUNET_NO;
1564 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1565 &client_session_timeout,
1567 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1568 "Created new session %p for `%s' address `%s''\n",
1570 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1571 GNUNET_i2s (&s->target));
1573 /* add new session */
1574 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1576 /* initiate new connection */
1577 if (GNUNET_SYSERR == client_connect (s))
1579 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1580 "Cannot connect to peer `%s' address `%s''\n",
1581 http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen),
1582 GNUNET_i2s (&s->target));
1583 client_delete_session (s);
1591 * Setup http_client plugin
1593 * @param plugin the plugin handle
1594 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1597 client_start (struct HTTP_Client_Plugin *plugin)
1599 curl_global_init (CURL_GLOBAL_ALL);
1600 plugin->curl_multi_handle = curl_multi_init ();
1602 if (NULL == plugin->curl_multi_handle)
1604 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1605 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1607 return GNUNET_SYSERR;
1614 * Increment session timeout due to activity for session s
1616 * param s the session
1619 client_reschedule_session_timeout (struct Session *s)
1621 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1622 GNUNET_SCHEDULER_cancel (s->timeout_task);
1623 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
1624 &client_session_timeout,
1626 GNUNET_log (TIMEOUT_LOG,
1627 "Timeout rescheduled for session %p set to %s\n",
1629 GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
1635 * Another peer has suggested an address for this
1636 * peer and transport plugin. Check that this could be a valid
1637 * address. If so, consider adding it to the list
1640 * @param cls closure with the `struct Plugin`
1641 * @param addr pointer to the address
1642 * @param addrlen length of @a addr
1643 * @return #GNUNET_OK if this is a plausible address for this peer
1644 * and transport; always returns #GNUNET_NO (this is the client!)
1647 http_client_plugin_address_suggested (void *cls,
1651 /* struct Plugin *plugin = cls; */
1653 /* A HTTP/S client does not have any valid address so:*/
1659 * Exit point from the plugin.
1661 * @param cls api as closure
1665 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1667 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1668 struct HTTP_Client_Plugin *plugin = api->cls;
1669 struct Session *pos;
1670 struct Session *next;
1672 if (NULL == api->cls)
1679 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1680 _("Shutting down plugin `%s'\n"),
1684 next = plugin->head;
1685 while (NULL != (pos = next))
1688 http_client_session_disconnect (plugin, pos);
1690 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1692 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1693 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1697 if (NULL != plugin->curl_multi_handle)
1699 curl_multi_cleanup (plugin->curl_multi_handle);
1700 plugin->curl_multi_handle = NULL;
1702 curl_global_cleanup ();
1704 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1705 _("Shutdown for plugin `%s' complete\n"),
1708 GNUNET_free (plugin);
1717 * @param plugin the plugin handle
1718 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1721 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1723 unsigned long long max_connections;
1725 /* Optional parameters */
1726 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1728 "MAX_CONNECTIONS", &max_connections))
1729 max_connections = 128;
1730 plugin->max_connections = max_connections;
1732 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1733 _("Maximum number of connections is %u\n"),
1734 plugin->max_connections);
1740 http_plugin_address_to_string (void *cls,
1744 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
1749 * Entry point for the plugin.
1752 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1754 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1755 struct GNUNET_TRANSPORT_PluginFunctions *api;
1756 struct HTTP_Client_Plugin *plugin;
1758 if (NULL == env->receive)
1760 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1761 initialze the plugin or the API */
1762 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1764 api->address_to_string = &http_plugin_address_to_string;
1765 api->string_to_address = &http_common_plugin_string_to_address;
1766 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1770 plugin = GNUNET_new (struct HTTP_Client_Plugin);
1772 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1774 api->send = &http_client_plugin_send;
1775 api->disconnect_session = &http_client_session_disconnect;
1776 api->disconnect_peer = &http_client_peer_disconnect;
1777 api->check_address = &http_client_plugin_address_suggested;
1778 api->get_session = &http_client_plugin_get_session;
1779 api->address_to_string = &http_plugin_address_to_string;
1780 api->string_to_address = &http_common_plugin_string_to_address;
1781 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1782 api->get_network = &http_client_get_network;
1785 plugin->name = "transport-https_client";
1786 plugin->protocol = "https";
1788 plugin->name = "transport-http_client";
1789 plugin->protocol = "http";
1791 plugin->last_tag = 1;
1792 plugin->options = 0; /* Setup options */
1794 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1796 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1801 if (GNUNET_SYSERR == client_start (plugin))
1803 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1809 /* end of plugin_transport_http_client.c */