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
155 struct HttpAddress *addr;
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 * My options to be included in the address
285 * Maximum number of sockets the plugin can use
286 * Each http inbound /outbound connections are two connections
288 unsigned int max_connections;
291 * Current number of sockets the plugin can use
292 * Each http inbound /outbound connections are two connections
294 unsigned int cur_connections;
297 * Last used unique HTTP connection tag
314 CURLM *curl_multi_handle;
319 GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
324 * Encapsulation of all of the state of the plugin.
326 struct HTTP_Client_Plugin *p;
330 * Start session timeout for a session
331 * @param s the session
334 client_start_session_timeout (struct Session *s);
338 * Increment session timeout due to activity for a session
339 * @param s the session
342 client_reschedule_session_timeout (struct Session *s);
346 * Cancel timeout for a session
347 * @param s the session
350 client_stop_session_timeout (struct Session *s);
354 * Function setting up file descriptors and scheduling task to run
356 * @param plugin plugin as closure
357 * @param now schedule task in 1ms, regardless of what curl may say
358 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
361 client_schedule (struct HTTP_Client_Plugin *plugin, int now);
365 * Connect a HTTP put connection
367 * @param s the session to connect
368 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for success
371 client_connect_put (struct Session *s);
375 * Does a session s exists?
377 * @param plugin the plugin
378 * @param s desired session
379 * @return GNUNET_YES or GNUNET_NO
382 client_exist_session (struct HTTP_Client_Plugin *plugin, struct Session *s)
384 struct Session * head;
386 GNUNET_assert (NULL != plugin);
387 GNUNET_assert (NULL != s);
389 for (head = plugin->head; head != NULL; head = head->next)
401 * @param curl the curl easy handle
402 * @param type message type
403 * @param data data to log, NOT a 0-terminated string
404 * @param size data length
405 * @param cls the closure
409 client_log (CURL *curl, curl_infotype type,
410 const char *data, size_t size, void *cls)
412 struct ConnectionHandle *ch = cls;
413 const char *ttype = "UNSPECIFIED";
415 if ((type == CURLINFO_TEXT) || (type == CURLINFO_HEADER_IN) || (type == CURLINFO_HEADER_OUT))
423 case CURLINFO_HEADER_IN:
426 case CURLINFO_HEADER_OUT:
427 ttype = "HEADER_OUT";
430 GNUNET_assert (NULL != ch);
431 GNUNET_assert (NULL != ch->easyhandle);
432 GNUNET_assert (NULL != ch->s);
433 ch->s->overhead += size;
436 ttype = "UNSPECIFIED";
440 memcpy (text, data, size);
441 if (text[size - 1] == '\n')
446 text[size + 1] = '\0';
449 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https_client",
450 "Connection %p %s: %s", ch->easyhandle, ttype, text);
452 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http_client",
453 "Connection %p %s: %s", ch->easyhandle, ttype, text);
461 * Function that can be used by the transport service to transmit
462 * a message using the plugin. Note that in the case of a
463 * peer disconnecting, the continuation MUST be called
464 * prior to the disconnect notification itself. This function
465 * will be called with this peer's HELLO message to initiate
466 * a fresh connection to another peer.
469 * @param s which session must be used
470 * @param msgbuf the message to transmit
471 * @param msgbuf_size number of bytes in 'msgbuf'
472 * @param priority how important is the message (most plugins will
473 * ignore message priority and just FIFO)
474 * @param to how long to wait at most for the transmission (does not
475 * require plugins to discard the message after the timeout,
476 * just advisory for the desired delay; most plugins will ignore
478 * @param cont continuation to call once the message has
479 * been transmitted (or if the transport is ready
480 * for the next transmission call; or if the
481 * peer disconnected...); can be NULL
482 * @param cont_cls closure for cont
483 * @return number of bytes used (on the physical network, with overheads);
484 * -1 on hard errors (i.e. address invalid); 0 is a legal value
485 * and does NOT mean that the message was not transmitted (DV)
488 http_client_plugin_send (void *cls,
490 const char *msgbuf, size_t msgbuf_size,
491 unsigned int priority,
492 struct GNUNET_TIME_Relative to,
493 GNUNET_TRANSPORT_TransmitContinuation cont,
496 struct HTTP_Client_Plugin *plugin = cls;
497 struct HTTP_Message *msg;
500 GNUNET_assert (plugin != NULL);
501 GNUNET_assert (s != NULL);
503 /* lookup if session is really existing */
504 if (GNUNET_YES != client_exist_session (plugin, s))
507 return GNUNET_SYSERR;
510 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
511 "Session %p/connection %p: Sending message with %u to peer `%s' \n",
513 msgbuf_size, GNUNET_i2s (&s->target));
515 /* create new message and schedule */
516 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
518 msg->size = msgbuf_size;
520 msg->buf = (char *) &msg[1];
521 msg->transmit_cont = cont;
522 msg->transmit_cont_cls = cont_cls;
523 memcpy (msg->buf, msgbuf, msgbuf_size);
524 GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg);
526 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
527 GNUNET_STATISTICS_update (plugin->env->stats,
528 stat_txt, msgbuf_size, GNUNET_NO);
530 GNUNET_free (stat_txt);
532 if (GNUNET_YES == s->put_tmp_disconnecting)
534 /* PUT connection is currently getting disconnected */
535 s->put_reconnect_required = GNUNET_YES;
536 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
537 "Session %p/connection %jp: currently disconnecting, reconnecting immediately\n",
541 else if (GNUNET_YES == s->put_paused)
543 /* PUT connection was paused, unpause */
544 GNUNET_assert (s->put_disconnect_task != GNUNET_SCHEDULER_NO_TASK);
545 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
546 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
547 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
548 "Session %p/connection %p: unpausing connection\n",
550 s->put_paused = GNUNET_NO;
551 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
553 else if (GNUNET_YES == s->put_tmp_disconnected)
555 /* PUT connection was disconnected, reconnect */
556 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
557 "Session %p: Reconnecting PUT connection\n",
559 s->put_tmp_disconnected = GNUNET_NO;
560 GNUNET_break (s->client_put == NULL);
561 if (GNUNET_SYSERR == client_connect_put (s))
563 return GNUNET_SYSERR;
567 client_schedule (s->plugin, GNUNET_YES);
568 client_reschedule_session_timeout (s);
576 * @param s the session to delete
579 client_delete_session (struct Session *s)
581 struct HTTP_Client_Plugin *plugin = s->plugin;
582 struct HTTP_Message *pos;
583 struct HTTP_Message *next;
585 client_stop_session_timeout (s);
587 if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
589 GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
590 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
593 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
596 while (NULL != (pos = next))
599 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
600 if (pos->transmit_cont != NULL)
601 pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
602 pos->size, pos->pos + s->overhead);
607 if (s->msg_tk != NULL)
609 GNUNET_SERVER_mst_destroy (s->msg_tk);
612 GNUNET_free (s->addr);
613 GNUNET_free (s->url);
620 * Disconnect a session
623 * @return GNUNET_OK on success, GNUNET_SYSERR on error
626 client_disconnect (struct Session *s)
628 struct HTTP_Client_Plugin *plugin = s->plugin;
629 struct HTTP_Message *msg;
630 struct HTTP_Message *t;
634 if (GNUNET_YES != client_exist_session (plugin, s))
637 return GNUNET_SYSERR;
640 if (s->client_put != NULL)
642 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
643 "Session %p/connection %p: disconnecting PUT connection to peer `%s'\n",
644 s, s->client_put, GNUNET_i2s (&s->target));
646 /* remove curl handle from multi handle */
647 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_put);
648 if (mret != CURLM_OK)
650 /* clean up easy handle, handle is now invalid and free'd */
654 curl_easy_cleanup (s->client_put);
655 s->client_put = NULL;
659 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
661 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
662 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
665 if (s->client_get != NULL)
667 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
668 "Session %p/connection %p: disconnecting GET connection to peer `%s'\n",
669 s, s->client_get, GNUNET_i2s (&s->target));
671 /* remove curl handle from multi handle */
672 mret = curl_multi_remove_handle (plugin->curl_multi_handle, s->client_get);
673 if (mret != CURLM_OK)
675 /* clean up easy handle, handle is now invalid and free'd */
679 curl_easy_cleanup (s->client_get);
680 s->client_get = NULL;
687 if (NULL != msg->transmit_cont)
688 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
689 msg->size, msg->pos + s->overhead);
691 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
696 GNUNET_assert (plugin->cur_connections >= 2);
697 plugin->cur_connections -= 2;
698 GNUNET_STATISTICS_set (plugin->env->stats,
699 "# HTTP client sessions",
700 plugin->cur_connections,
703 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
704 "Session %p: notifying transport about ending session\n",s);
706 plugin->env->session_end (plugin->env->cls, &s->target, s);
707 client_delete_session (s);
709 /* Re-schedule since handles have changed */
710 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
712 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
713 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
715 client_schedule (plugin, GNUNET_YES);
722 * Function that can be used to force the plugin to disconnect
723 * from the given peer and cancel all previous transmissions
724 * (and their continuationc).
727 * @param target peer from which to disconnect
730 http_client_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
732 struct HTTP_Client_Plugin *plugin = cls;
733 struct Session *next = NULL;
734 struct Session *pos = NULL;
736 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
737 "Transport tells me to disconnect `%s'\n",
738 GNUNET_i2s (target));
741 while (NULL != (pos = next))
744 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
746 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
747 "Disconnecting session %p to `%pos'\n",
748 pos, GNUNET_i2s (target));
749 GNUNET_assert (GNUNET_OK == client_disconnect (pos));
756 * Check if a sessions exists for an specific address
758 * @param plugin the plugin
759 * @param address the address
760 * @return the session or NULL
762 static struct Session *
763 client_lookup_session (struct HTTP_Client_Plugin *plugin,
764 const struct GNUNET_HELLO_Address *address)
768 for (pos = plugin->head; NULL != pos; pos = pos->next)
769 if ((0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
770 (address->address_length == pos->addrlen) &&
771 (0 == memcmp (address->address, pos->addr, pos->addrlen)))
777 client_put_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
779 struct Session *s = cls;
780 s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
781 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
782 "Session %p/connection %p: will be disconnected due to no activity\n",
784 s->put_paused = GNUNET_NO;
785 s->put_tmp_disconnecting = GNUNET_YES;
786 curl_easy_pause (s->client_put, CURLPAUSE_CONT);
787 client_schedule (s->plugin, GNUNET_YES);
793 * Callback method used with libcurl
794 * Method is called when libcurl needs to read data during sending
796 * @param stream pointer where to write data
797 * @param size size of an individual element
798 * @param nmemb count of elements that can be written to the buffer
799 * @param cls source pointer, passed to the libcurl handle
800 * @return bytes written to stream, returning 0 will terminate connection!
803 client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
805 struct Session *s = cls;
806 struct HTTP_Client_Plugin *plugin = s->plugin;
807 struct HTTP_Message *msg = s->msg_head;
811 if (GNUNET_YES != client_exist_session (plugin, s))
816 if (GNUNET_YES == s->put_tmp_disconnecting)
819 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
820 "Session %p/connection %p: disconnect due to inactivity\n",
827 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
828 "Session %p/connection %p: nothing to send, suspending\n",
830 s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT, &client_put_disconnect, s);
831 s->put_paused = GNUNET_YES;
832 return CURL_READFUNC_PAUSE;
835 GNUNET_assert (msg->pos < msg->size);
836 /* calculate how much fits in buffer */
837 len = GNUNET_MIN (msg->size - msg->pos,
839 memcpy (stream, &msg->buf[msg->pos], len);
841 if (msg->pos == msg->size)
843 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
844 "Session %p/connection %p: sent message with %u bytes sent, removing message from queue\n",
845 s, s->client_put, msg->size, msg->pos);
846 /* Calling transmit continuation */
847 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
848 if (NULL != msg->transmit_cont)
849 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
850 msg->size, msg->size + s->overhead);
855 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_client buffers", plugin->protocol);
856 GNUNET_STATISTICS_update (plugin->env->stats,
857 stat_txt, -len, GNUNET_NO);
858 GNUNET_free (stat_txt);
860 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_client", plugin->protocol);
861 GNUNET_STATISTICS_update (plugin->env->stats,
862 stat_txt, len, GNUNET_NO);
863 GNUNET_free (stat_txt);
865 client_reschedule_session_timeout (s);
871 * Wake up a curl handle which was suspended
873 * @param cls the session
874 * @param tc task context
877 client_wake_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
879 struct Session *s = cls;
881 if (GNUNET_YES != client_exist_session(p, s))
886 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
887 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
889 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
890 "Session %p/connection %p: Waking up GET handle\n", s, s->client_get);
891 if (s->client_get != NULL)
892 curl_easy_pause (s->client_get, CURLPAUSE_CONT);
897 * Callback for message stream tokenizer
899 * @param cls the session
900 * @param client not used
901 * @param message the message received
902 * @return always GNUNET_OK
905 client_receive_mst_cb (void *cls, void *client,
906 const struct GNUNET_MessageHeader *message)
908 struct Session *s = cls;
909 struct HTTP_Client_Plugin *plugin;
910 struct GNUNET_TIME_Relative delay;
911 struct GNUNET_ATS_Information atsi;
913 if (GNUNET_YES != client_exist_session(p, s))
920 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
921 atsi.value = s->ats_address_network_type;
922 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
924 delay = s->plugin->env->receive (plugin->env->cls, &s->target, message,
925 s, (const char *) s->addr, s->addrlen);
927 plugin->env->update_address_metrics (plugin->env->cls,
934 GNUNET_asprintf (&stat_txt, "# bytes received via %s_client", plugin->protocol);
935 GNUNET_STATISTICS_update (plugin->env->stats,
936 stat_txt, ntohs(message->size), GNUNET_NO);
937 GNUNET_free (stat_txt);
940 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
942 if (GNUNET_TIME_absolute_get ().abs_value < s->next_receive.abs_value)
945 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
946 "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
947 GNUNET_i2s (&s->target),
948 http_common_plugin_address_to_string (NULL, s->plugin->protocol, s->addr, s->addrlen),
951 client_reschedule_session_timeout (s);
957 * Callback method used with libcurl when data for a PUT connection are
958 * received. We do not expect data here, so we just dismiss it
960 * @param stream pointer where to write data
961 * @param size size of an individual element
962 * @param nmemb count of elements that can be written to the buffer
963 * @param cls destination pointer, passed to the libcurl handle
964 * @return bytes read from stream
967 client_receive_put (void *stream, size_t size, size_t nmemb, void *cls)
974 * Callback method used with libcurl when data for a GET connection are
975 * received. Forward to MST
977 * @param stream pointer where to write data
978 * @param size size of an individual element
979 * @param nmemb count of elements that can be written to the buffer
980 * @param cls destination pointer, passed to the libcurl handle
981 * @return bytes read from stream
984 client_receive (void *stream, size_t size, size_t nmemb, void *cls)
986 struct Session *s = cls;
987 struct GNUNET_TIME_Absolute now;
988 size_t len = size * nmemb;
990 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
991 "Session %p / connection %p: Received %u bytes from peer `%s'\n",
993 len, GNUNET_i2s (&s->target));
994 now = GNUNET_TIME_absolute_get ();
995 if (now.abs_value < s->next_receive.abs_value)
997 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
998 struct GNUNET_TIME_Relative delta =
999 GNUNET_TIME_absolute_get_difference (now, s->next_receive);
1000 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1001 "Session %p / connection %p: No inbound bandwidth available! Next read was delayed for %llu ms\n",
1002 s, s->client_get, delta.rel_value);
1003 if (s->recv_wakeup_task != GNUNET_SCHEDULER_NO_TASK)
1005 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
1006 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
1008 s->recv_wakeup_task =
1009 GNUNET_SCHEDULER_add_delayed (delta, &client_wake_up, s);
1010 return CURL_WRITEFUNC_PAUSE;
1012 if (NULL == s->msg_tk)
1013 s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb, s);
1014 GNUNET_SERVER_mst_receive (s->msg_tk, s, stream, len, GNUNET_NO, GNUNET_NO);
1020 * Task performing curl operations
1022 * @param cls plugin as closure
1023 * @param tc gnunet scheduler task context
1026 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1030 * Function setting up file descriptors and scheduling task to run
1032 * @param plugin the plugin as closure
1033 * @param now schedule task in 1ms, regardless of what curl may say
1034 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1037 client_schedule (struct HTTP_Client_Plugin *plugin, int now)
1043 struct GNUNET_NETWORK_FDSet *grs;
1044 struct GNUNET_NETWORK_FDSet *gws;
1047 struct GNUNET_TIME_Relative timeout;
1049 /* Cancel previous scheduled task */
1050 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1052 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1053 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1059 mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
1060 if (mret != CURLM_OK)
1062 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1063 "curl_multi_fdset", __FILE__, __LINE__,
1064 curl_multi_strerror (mret));
1065 return GNUNET_SYSERR;
1067 mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
1069 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1071 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1072 if (now == GNUNET_YES)
1073 timeout = GNUNET_TIME_UNIT_MILLISECONDS;
1075 if (mret != CURLM_OK)
1077 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
1078 "curl_multi_timeout", __FILE__, __LINE__,
1079 curl_multi_strerror (mret));
1080 return GNUNET_SYSERR;
1083 grs = GNUNET_NETWORK_fdset_create ();
1084 gws = GNUNET_NETWORK_fdset_create ();
1085 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1086 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1088 plugin->client_perform_task =
1089 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1091 &client_run, plugin);
1092 GNUNET_NETWORK_fdset_destroy (gws);
1093 GNUNET_NETWORK_fdset_destroy (grs);
1099 * Task performing curl operations
1101 * @param cls plugin as closure
1102 * @param tc gnunet scheduler task context
1105 client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1107 struct HTTP_Client_Plugin *plugin = cls;
1109 long http_statuscode;
1112 GNUNET_assert (cls != NULL);
1114 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1115 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1121 mret = curl_multi_perform (plugin->curl_multi_handle, &running);
1126 while ((msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
1128 CURL *easy_h = msg->easy_handle;
1129 struct Session *s = NULL;
1130 char *d = (char *) s;
1135 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1136 "Client: connection to ended with reason %i: `%s', %i handles running\n",
1138 curl_easy_strerror (msg->data.result), running);
1142 GNUNET_assert (CURLE_OK ==
1143 curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
1144 s = (struct Session *) d;
1146 if (GNUNET_YES != client_exist_session(plugin, s))
1152 GNUNET_assert (s != NULL);
1153 if (msg->msg == CURLMSG_DONE)
1155 curl_easy_getinfo (easy_h, CURLINFO_RESPONSE_CODE, &http_statuscode);
1156 if (easy_h == s->client_put)
1158 if ((0 != msg->data.result) || (http_statuscode != 200))
1160 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1161 "Session %p/connection %p: PUT connection to `%s' ended with status %i reason %i: `%s'\n",
1162 s, msg->easy_handle, GNUNET_i2s (&s->target),
1165 curl_easy_strerror (msg->data.result));
1168 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1169 "Session %p/connection %p: PUT connection to `%s' ended normal\n",
1170 s, msg->easy_handle, GNUNET_i2s (&s->target));
1171 if (s->client_get == NULL)
1173 /* Disconnect other transmission direction and tell transport */
1175 curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
1176 curl_easy_cleanup (easy_h);
1177 s->put_tmp_disconnecting = GNUNET_NO;
1178 s->put_tmp_disconnected = GNUNET_YES;
1179 s->client_put = NULL;
1180 s->put.easyhandle = NULL;
1184 * Handling a rare case:
1185 * plugin_send was called during temporary put disconnect,
1186 * reconnect required after connection was disconnected
1188 if (GNUNET_YES == s->put_reconnect_required)
1190 s->put_reconnect_required = GNUNET_NO;
1191 if (GNUNET_SYSERR == client_connect_put(s))
1193 GNUNET_break (s->client_put == NULL);
1194 GNUNET_break (s->put_tmp_disconnected == GNUNET_NO);
1198 if (easy_h == s->client_get)
1200 if ((0 != msg->data.result) || (http_statuscode != 200))
1202 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1203 "Session %p/connection %p: GET connection to `%s' ended with status %i reason %i: `%s'\n",
1204 s, msg->easy_handle, GNUNET_i2s (&s->target),
1207 curl_easy_strerror (msg->data.result));
1211 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1212 "Session %p/connection %p: GET connection to `%s' ended normal\n",
1213 s, msg->easy_handle, GNUNET_i2s (&s->target));
1214 /* Disconnect other transmission direction and tell transport */
1215 s->get.easyhandle = NULL;
1217 client_disconnect (s);
1222 while (mret == CURLM_CALL_MULTI_PERFORM);
1223 client_schedule (plugin, GNUNET_NO);
1228 * Connect GET connection for a session
1230 * @param s the session to connect
1231 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1234 client_connect_get (struct Session *s)
1238 /* create get connection */
1239 s->client_get = curl_easy_init ();
1241 s->get.easyhandle = s->client_get;
1243 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
1244 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
1245 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, &s->get);
1248 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1249 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1250 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1252 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 1L);
1253 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 2L);
1257 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
1258 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
1260 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1261 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1263 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1264 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1267 curl_easy_setopt (s->client_get, CURLOPT_URL, s->url);
1268 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1269 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
1270 curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, client_send_cb);
1271 curl_easy_setopt (s->client_get, CURLOPT_READDATA, s);
1272 curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
1273 curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
1274 /* No timeout by default, timeout done with session timeout */
1275 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT, 0);
1276 curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
1277 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
1278 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1279 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
1280 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1281 #if CURL_TCP_NODELAY
1282 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1284 curl_easy_setopt (s->client_get, CURLOPT_FOLLOWLOCATION, 0);
1286 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_get);
1287 if (mret != CURLM_OK)
1289 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1290 "Session %p : Failed to add GET handle to multihandle: `%s'\n",
1291 s, curl_multi_strerror (mret));
1292 curl_easy_cleanup (s->client_get);
1293 s->client_get = NULL;
1295 s->get.easyhandle = NULL;
1297 return GNUNET_SYSERR;
1304 * Connect a HTTP put connection
1306 * @param s the session to connect
1307 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1310 client_connect_put (struct Session *s)
1313 /* create put connection */
1314 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1315 "Session %p : Init PUT handle \n", s);
1316 s->client_put = curl_easy_init ();
1318 s->put.easyhandle = s->client_put;
1320 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
1321 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
1322 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, &s->put);
1325 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1326 if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
1327 (ntohl (s->addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
1329 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 1L);
1330 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 2L);
1334 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
1335 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
1337 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
1338 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
1340 curl_easy_setopt (s->client_get, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
1341 curl_easy_setopt (s->client_get, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
1343 curl_easy_setopt (s->client_put, CURLOPT_URL, s->url);
1344 curl_easy_setopt (s->client_put, CURLOPT_UPLOAD, 1L);
1345 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &client_curl_header);
1346 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
1347 curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, client_send_cb);
1348 curl_easy_setopt (s->client_put, CURLOPT_READDATA, s);
1349 curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive_put);
1350 curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
1351 /* No timeout by default, timeout done with session timeout */
1352 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT, 0);
1353 curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
1354 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
1355 (long) HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value);
1356 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
1357 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1358 #if CURL_TCP_NODELAY
1359 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
1361 mret = curl_multi_add_handle (s->plugin->curl_multi_handle, s->client_put);
1362 if (mret != CURLM_OK)
1364 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
1365 "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
1366 s, curl_multi_strerror (mret));
1367 curl_easy_cleanup (s->client_put);
1368 s->client_put = NULL;
1369 s->put.easyhandle = NULL;
1371 s->put_tmp_disconnected = GNUNET_YES;
1372 return GNUNET_SYSERR;
1374 s->put_tmp_disconnected = GNUNET_NO;
1380 * Connect both PUT and GET connection for a session
1382 * @param s the session to connect
1383 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1386 client_connect (struct Session *s)
1389 struct HTTP_Client_Plugin *plugin = s->plugin;
1390 int res = GNUNET_OK;
1393 if (NULL == http_common_plugin_address_to_string (NULL, plugin->protocol, s->addr, s->addrlen))
1395 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1396 "Invalid address peer `%s'\n",
1397 GNUNET_i2s (&s->target));
1398 return GNUNET_SYSERR;
1401 GNUNET_asprintf (&s->url, "%s/%s;%u",
1402 http_common_plugin_address_to_url (NULL, s->addr, s->addrlen),
1403 GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),
1408 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1409 "Initiating outbound session peer `%s' using address `%s'\n",
1410 GNUNET_i2s (&s->target), s->url);
1412 if ((GNUNET_SYSERR == client_connect_get (s)) ||
1413 (GNUNET_SYSERR == client_connect_put (s)))
1416 return GNUNET_SYSERR;
1419 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1420 "Session %p: connected with connections GET %p and PUT %p\n",
1421 s, s->client_get, s->client_put);
1423 /* Perform connect */
1424 plugin->cur_connections += 2;
1425 GNUNET_STATISTICS_set (plugin->env->stats,
1426 "# HTTP client connections",
1427 plugin->cur_connections,
1430 /* Re-schedule since handles have changed */
1431 if (plugin->client_perform_task != GNUNET_SCHEDULER_NO_TASK)
1433 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1434 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1436 plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run, plugin);
1442 * Creates a new outbound session the transport service will use to send data to the
1445 * @param cls the plugin
1446 * @param address the address
1447 * @return the session or NULL of max connections exceeded
1449 static struct Session *
1450 http_client_plugin_get_session (void *cls,
1451 const struct GNUNET_HELLO_Address *address)
1453 struct HTTP_Client_Plugin *plugin = cls;
1454 struct Session * s = NULL;
1455 struct sockaddr *sa;
1456 struct GNUNET_ATS_Information ats;
1460 GNUNET_assert (plugin != NULL);
1461 GNUNET_assert (address != NULL);
1462 GNUNET_assert (address->address != NULL);
1464 /* find existing session */
1465 s = client_lookup_session (plugin, address);
1469 if (plugin->max_connections <= plugin->cur_connections)
1471 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1472 "Maximum number of connections (%u) reached: "
1473 "cannot connect to peer `%s'\n",
1474 plugin->max_connections,
1475 GNUNET_i2s (&address->peer));
1479 /* Determine network location */
1480 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1481 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
1482 sa = http_common_socket_from_address (address->address, address->address_length, &res);
1483 if (GNUNET_SYSERR == res)
1487 else if (GNUNET_YES == res)
1489 GNUNET_assert (NULL != sa);
1490 if (AF_INET == sa->sa_family)
1492 salen = sizeof (struct sockaddr_in);
1494 else if (AF_INET6 == sa->sa_family)
1496 salen = sizeof (struct sockaddr_in6);
1498 ats = plugin->env->get_address_type (plugin->env->cls, sa, salen);
1499 //fprintf (stderr, "Address %s is in %s\n", GNUNET_a2s (sa,salen), GNUNET_ATS_print_network_type(ntohl(ats.value)));
1502 else if (GNUNET_NO == res)
1504 ats.value = htonl (GNUNET_ATS_COST_WAN);
1507 if (GNUNET_ATS_NET_UNSPECIFIED == ntohl(ats.value))
1513 s = GNUNET_malloc (sizeof (struct Session));
1514 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1516 s->addr = GNUNET_malloc (address->address_length);
1517 memcpy (s->addr, address->address, address->address_length);
1518 s->addrlen = address->address_length;
1519 s->ats_address_network_type = ats.value;
1520 s->put_paused = GNUNET_NO;
1521 s->put_tmp_disconnecting = GNUNET_NO;
1522 s->put_tmp_disconnected = GNUNET_NO;
1523 client_start_session_timeout (s);
1525 /* add new session */
1526 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1528 /* initiate new connection */
1529 if (GNUNET_SYSERR == client_connect (s))
1531 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1532 "Cannot connect to peer `%s' address `%s''\n",
1533 http_common_plugin_address_to_string (NULL, plugin->name, s->addr, s->addrlen),
1534 GNUNET_i2s (&s->target));
1535 client_delete_session (s);
1543 * Setup http_client plugin
1545 * @param plugin the plugin handle
1546 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1549 client_start (struct HTTP_Client_Plugin *plugin)
1551 curl_global_init (CURL_GLOBAL_ALL);
1552 plugin->curl_multi_handle = curl_multi_init ();
1554 if (NULL == plugin->curl_multi_handle)
1556 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1557 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
1559 return GNUNET_SYSERR;
1565 * Session was idle, so disconnect it
1568 client_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1570 GNUNET_assert (NULL != cls);
1571 struct Session *s = cls;
1573 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1574 GNUNET_log (TIMEOUT_LOG,
1575 "Session %p was idle for %llu ms, disconnecting\n",
1576 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1578 /* call session destroy function */
1579 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1584 * Start session timeout for session s
1586 * @param s the session
1589 client_start_session_timeout (struct Session *s)
1592 GNUNET_assert (NULL != s);
1593 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1594 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1595 &client_session_timeout,
1597 GNUNET_log (TIMEOUT_LOG,
1598 "Timeout for session %p set to %llu ms\n",
1599 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1604 * Increment session timeout due to activity for session s
1606 * param s the session
1609 client_reschedule_session_timeout (struct Session *s)
1612 GNUNET_assert (NULL != s);
1613 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1615 GNUNET_SCHEDULER_cancel (s->timeout_task);
1616 s->timeout_task = GNUNET_SCHEDULER_add_delayed (CLIENT_SESSION_TIMEOUT,
1617 &client_session_timeout,
1619 GNUNET_log (TIMEOUT_LOG,
1620 "Timeout rescheduled for session %p set to %llu ms\n",
1621 s, (unsigned long long) CLIENT_SESSION_TIMEOUT.rel_value);
1626 * Cancel timeout due to activity for session s
1628 * param s the session
1631 client_stop_session_timeout (struct Session *s)
1633 GNUNET_assert (NULL != s);
1635 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1637 GNUNET_SCHEDULER_cancel (s->timeout_task);
1638 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1639 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
1645 * Another peer has suggested an address for this
1646 * peer and transport plugin. Check that this could be a valid
1647 * address. If so, consider adding it to the list
1650 * @param cls closure
1651 * @param addr pointer to the address
1652 * @param addrlen length of addr
1653 * @return GNUNET_OK if this is a plausible address for this peer
1657 http_client_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1659 /* struct Plugin *plugin = cls; */
1661 /* A HTTP/S client does not have any valid address so:*/
1667 * Exit point from the plugin.
1669 * @param cls api as closure
1673 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1675 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1676 struct HTTP_Client_Plugin *plugin = api->cls;
1677 struct Session *pos;
1678 struct Session *next;
1680 if (NULL == api->cls)
1687 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1688 _("Shutting down plugin `%s'\n"),
1692 next = plugin->head;
1693 while (NULL != (pos = next))
1696 client_disconnect (pos);
1698 if (GNUNET_SCHEDULER_NO_TASK != plugin->client_perform_task)
1700 GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
1701 plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
1705 if (NULL != plugin->curl_multi_handle)
1707 curl_multi_cleanup (plugin->curl_multi_handle);
1708 plugin->curl_multi_handle = NULL;
1710 curl_global_cleanup ();
1712 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1713 _("Shutdown for plugin `%s' complete\n"),
1716 GNUNET_free (plugin);
1725 * @param plugin the plugin handle
1726 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1729 client_configure_plugin (struct HTTP_Client_Plugin *plugin)
1731 unsigned long long max_connections;
1733 /* Optional parameters */
1734 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
1736 "MAX_CONNECTIONS", &max_connections))
1737 max_connections = 128;
1738 plugin->max_connections = max_connections;
1740 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1741 _("Maximum number of connections is %u\n"),
1742 plugin->max_connections);
1746 const char *http_plugin_address_to_string (void *cls,
1750 return http_common_plugin_address_to_string (cls, p->name, addr, addrlen);
1754 * Entry point for the plugin.
1757 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1759 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1760 struct GNUNET_TRANSPORT_PluginFunctions *api;
1761 struct HTTP_Client_Plugin *plugin;
1763 if (NULL == env->receive)
1765 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1766 initialze the plugin or the API */
1767 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1769 api->address_to_string = &http_plugin_address_to_string;
1770 api->string_to_address = &http_common_plugin_string_to_address;
1771 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1775 plugin = GNUNET_malloc (sizeof (struct HTTP_Client_Plugin));
1778 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1780 api->send = &http_client_plugin_send;
1781 api->disconnect = &http_client_plugin_disconnect;
1782 api->check_address = &http_client_plugin_address_suggested;
1783 api->get_session = &http_client_plugin_get_session;
1784 api->address_to_string = &http_plugin_address_to_string;
1785 api->string_to_address = &http_common_plugin_string_to_address;
1786 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
1790 plugin->name = "transport-https_client";
1791 plugin->protocol = "https";
1793 plugin->name = "transport-http_client";
1794 plugin->protocol = "http";
1796 plugin->last_tag = 1;
1797 plugin->options = 0; /* Setup options */
1799 if (GNUNET_SYSERR == client_configure_plugin (plugin))
1801 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1806 if (GNUNET_SYSERR == client_start (plugin))
1808 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
1814 /* end of plugin_transport_http_client.c */