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.c
23 * @brief http transport service plugin
24 * @author Matthias Wachs
28 #include "gnunet_constants.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_connection_lib.h"
31 #include "gnunet_service_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_container_lib.h"
37 #include "plugin_transport.h"
38 #include "gnunet_os_lib.h"
39 #include "microhttpd.h"
40 #include <curl/curl.h>
43 #define DEBUG_CURL GNUNET_NO
44 #define DEBUG_HTTP GNUNET_NO
45 #define DEBUG_CONNECTIONS GNUNET_NO
47 #define INBOUND GNUNET_NO
48 #define OUTBOUND GNUNET_YES
51 * Text of the response sent back after the last bytes of a PUT
52 * request have been received (just to formally obey the HTTP
55 #define HTTP_PUT_RESPONSE "Thank you!"
58 * After how long do we expire an address that we
59 * learned from another peer if it is not reconfirmed
62 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
65 * Page returned if request invalid
67 #define HTTP_ERROR_RESPONSE "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>Not Found</H1>The requested URL was not found on this server.<P><HR><ADDRESS></ADDRESS></BODY></HTML>"
70 * Timeout for a http connect
72 #define HTTP_CONNECT_TIMEOUT 30
76 * Network format for IPv4 addresses.
78 struct IPv4HttpAddress
81 * IPv4 address, in network byte order.
83 uint32_t ipv4_addr GNUNET_PACKED;
86 * Port number, in network byte order.
88 uint16_t u_port GNUNET_PACKED;
94 * Network format for IPv6 addresses.
96 struct IPv6HttpAddress
101 struct in6_addr ipv6_addr GNUNET_PACKED;
104 * Port number, in network byte order.
106 uint16_t u6_port GNUNET_PACKED;
112 * Message to send using http
117 * next pointer for double linked list
119 struct HTTP_Message * next;
122 * previous pointer for double linked list
124 struct HTTP_Message * prev;
127 * buffer containing data to send
132 * amount of data already sent
142 * Continuation function to call once the transmission buffer
143 * has again space available. NULL if there is no
144 * continuation to call.
146 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
149 * Closure for transmit_cont.
151 void *transmit_cont_cls;
155 struct HTTP_PeerContext
160 struct GNUNET_PeerIdentity identity;
163 * Pointer to the global plugin struct.
165 struct Plugin *plugin;
168 * Linked list of connections with this peer
171 struct Session * head;
174 * Linked list of connections with this peer
177 struct Session * tail;
180 * id for next session
182 size_t session_id_counter;
191 struct SessionHeader header;
194 * next session in linked list
196 struct Session * next;
199 * previous session in linked list
201 struct Session * prev;
204 * address of this session
219 * Message queue for outbound messages
222 struct HTTP_Message * pending_msgs_head;
225 * Message queue for outbound messages
228 struct HTTP_Message * pending_msgs_tail;
231 * partner peer this connection belongs to
233 struct HTTP_PeerContext * peercontext;
236 * message stream tokenizer for incoming data
238 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
242 * outbound: OUTBOUND (GNUNET_YES)
243 * inbound : INBOUND (GNUNET_NO)
245 unsigned int direction;
248 * is session connected to send data?
250 unsigned int send_connected;
253 * is send connection active?
255 unsigned int send_active;
258 * connection disconnect forced (e.g. from transport)
260 unsigned int send_force_disconnect;
263 * is session connected to receive data?
265 unsigned int recv_connected;
268 * is receive connection active?
270 unsigned int recv_active;
273 * connection disconnect forced (e.g. from transport)
275 unsigned int recv_force_disconnect;
278 * id for next session
279 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
284 * entity managing sending data
285 * outbound session: CURL *
286 * inbound session: mhd_connection *
288 void * send_endpoint;
291 * entity managing recieving data
292 * outbound session: CURL *
293 * inbound session: mhd_connection *
295 void * recv_endpoint;
299 * Encapsulation of all of the state of the plugin.
306 struct GNUNET_TRANSPORT_PluginEnvironment *env;
309 * Handle for reporting statistics.
311 struct GNUNET_STATISTICS_Handle *stats;
313 unsigned int port_inbound;
315 struct GNUNET_CONTAINER_MultiHashMap *peers;
318 * Daemon for listening for new IPv4 connections.
320 struct MHD_Daemon *http_server_daemon_v4;
323 * Daemon for listening for new IPv6connections.
325 struct MHD_Daemon *http_server_daemon_v6;
328 * Our primary task for http daemon handling IPv4 connections
330 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
333 * Our primary task for http daemon handling IPv6 connections
335 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
338 * The task sending data
340 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
345 CURLM * multi_handle;
348 * Our ASCII encoded, hashed peer identity
349 * This string is used to distinguish between connections and is added to the urls
351 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
356 * Function called for a quick conversion of the binary address to
357 * a numeric address. Note that the caller must not free the
358 * address and that the next call to this function is allowed
359 * to override the address again.
362 * @param addr binary address
363 * @param addrlen length of the address
364 * @return string representing the same address
367 http_plugin_address_to_string (void *cls,
371 static char * create_url(void * cls, const void * addr, size_t addrlen, size_t id)
373 struct Plugin *plugin = cls;
376 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
377 GNUNET_asprintf(&url,
379 http_plugin_address_to_string(NULL, addr, addrlen),
380 (char *) (&plugin->my_ascii_hash_ident),id);
386 * Removes a message from the linked list of messages
387 * @param con connection to remove message from
388 * @param msg message to remove
389 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
391 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
393 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
399 * Removes a session from the linked list of sessions
400 * @param pc peer context
402 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
403 * @param call_msg_cont_result, result to call message continuations with
404 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
406 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
408 struct HTTP_Message * msg;
409 struct Plugin * plugin = ps->peercontext->plugin;
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: removing %s session %X with id %u\n", ps, (ps->direction == INBOUND) ? "inbound" : "outbound", ps, ps->session_id);
412 plugin->env->session_end(plugin, &pc->identity, ps);
414 GNUNET_free_non_null (ps->addr);
415 GNUNET_SERVER_mst_destroy (ps->msgtok);
416 GNUNET_free(ps->url);
418 msg = ps->pending_msgs_head;
421 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
423 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
426 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
427 msg = ps->pending_msgs_head;
430 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
436 static struct Session * get_Session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addr_len)
438 struct Session * cc = pc->head;
439 struct Session * con = NULL;
440 unsigned int count = 0;
442 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
445 if (addr_len == cc->addrlen)
447 if (0 == memcmp(cc->addr, addr, addr_len))
449 /* connection can not be used, since it is disconnected */
450 if ((cc->recv_force_disconnect==GNUNET_NO) && (cc->send_force_disconnect==GNUNET_NO))
463 * Callback called by MHD when a connection is terminated
465 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
467 struct Session * ps = *httpSessionCache;
470 struct HTTP_PeerContext * pc = ps->peercontext;
472 if (connection==ps->recv_endpoint)
474 #if DEBUG_CONNECTIONS
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
477 ps->recv_active = GNUNET_NO;
478 ps->recv_connected = GNUNET_NO;
479 ps->recv_endpoint = NULL;
481 if (connection==ps->send_endpoint)
484 ps->send_active = GNUNET_NO;
485 ps->send_connected = GNUNET_NO;
486 ps->send_endpoint = NULL;
487 #if DEBUG_CONNECTIONS
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
492 /* if both connections disconnected, remove session */
493 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
495 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
499 static void mhd_write_mst_cb (void *cls,
501 const struct GNUNET_MessageHeader *message)
504 struct Session *ps = cls;
505 struct HTTP_PeerContext *pc = ps->peercontext;
506 GNUNET_assert(ps != NULL);
507 GNUNET_assert(pc != NULL);
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
512 ntohs(message->type),
513 ntohs(message->size),
514 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
516 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
523 static void curl_receive_mst_cb (void *cls,
525 const struct GNUNET_MessageHeader *message)
527 struct Session *ps = cls;
528 struct HTTP_PeerContext *pc = ps->peercontext;
529 GNUNET_assert(ps != NULL);
530 GNUNET_assert(pc != NULL);
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
535 ntohs(message->type),
536 ntohs(message->size),
537 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
539 pc->plugin->env->receive (pc->plugin->env->cls,
548 * Check if ip is allowed to connect.
551 mhd_accept_cb (void *cls,
552 const struct sockaddr *addr, socklen_t addr_len)
555 struct Plugin *plugin = cls;
557 /* Every connection is accepted, nothing more to do here */
561 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
565 struct Session * ps = cls;
566 struct HTTP_PeerContext * pc;
567 struct HTTP_Message * msg;
569 GNUNET_assert (ps!=NULL);
570 pc = ps->peercontext;
571 msg = ps->pending_msgs_tail;
572 if (ps->send_force_disconnect==GNUNET_YES)
574 #if DEBUG_CONNECTIONS
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
582 if ((msg->size-msg->pos) <= max)
584 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
585 bytes_read = msg->size-msg->pos;
586 msg->pos+=(msg->size-msg->pos);
590 memcpy(buf,&msg->buf[msg->pos],max);
595 if (msg->pos==msg->size)
597 if (NULL!=msg->transmit_cont)
598 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
599 remove_http_message(ps,msg);
606 * Process GET or PUT request received via MHD. For
607 * GET, queue response that will send back our pending
608 * messages. For PUT, process incoming data and send
609 * to GNUnet core. In either case, check if a session
610 * already exists and create a new one if not.
613 mdh_access_cb (void *cls,
614 struct MHD_Connection *mhd_connection,
618 const char *upload_data,
619 size_t * upload_data_size, void **httpSessionCache)
621 struct Plugin *plugin = cls;
622 struct MHD_Response *response;
623 const union MHD_ConnectionInfo * conn_info;
625 struct sockaddr_in *addrin;
626 struct sockaddr_in6 *addrin6;
628 char address[INET6_ADDRSTRLEN+14];
629 struct GNUNET_PeerIdentity pi_in;
632 struct IPv4HttpAddress ipv4addr;
633 struct IPv6HttpAddress ipv6addr;
635 struct HTTP_PeerContext *pc;
636 struct Session *ps = NULL;
637 struct Session *ps_tmp = NULL;
640 int send_error_to_client;
644 GNUNET_assert(cls !=NULL);
645 send_error_to_client = GNUNET_NO;
647 if (NULL == *httpSessionCache)
649 /* check url for peer identity , if invalid send HTTP 404*/
650 size_t len = strlen(&url[1]);
651 char * peer = GNUNET_malloc(104+1);
653 if ((len>104) && (url[104]==';'))
655 char * id = GNUNET_malloc((len-104)+1);
656 strcpy(id,&url[105]);
657 memcpy(peer,&url[1],103);
659 id_num = strtoul ( id, NULL , 10);
662 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
664 if ( GNUNET_SYSERR == res )
666 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
667 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
668 MHD_destroy_response (response);
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
678 ps = *httpSessionCache;
679 pc = ps->peercontext;
682 if (NULL == *httpSessionCache)
684 /* get peer context */
685 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
689 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
691 pc->session_id_counter=1;
692 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
693 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
696 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
697 /* Incoming IPv4 connection */
698 if ( AF_INET == conn_info->client_addr->sin_family)
700 addrin = conn_info->client_addr;
701 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
702 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
703 ipv4addr.u_port = addrin->sin_port;
705 addr_len = sizeof(struct IPv4HttpAddress);
707 /* Incoming IPv6 connection */
708 if ( AF_INET6 == conn_info->client_addr->sin_family)
710 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
711 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
712 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
713 ipv6addr.u6_port = addrin6->sin6_port;
715 addr_len = sizeof(struct IPv6HttpAddress);
719 //ps = get_Session(plugin, pc, addr, addr_len);
721 /* only inbound sessions here */
726 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
728 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
737 ps = GNUNET_malloc(sizeof (struct Session));
738 ps->addr = GNUNET_malloc(addr_len);
739 memcpy(ps->addr,addr,addr_len);
740 ps->addrlen = addr_len;
741 ps->direction=INBOUND;
742 ps->pending_msgs_head = NULL;
743 ps->pending_msgs_tail = NULL;
744 ps->send_connected=GNUNET_NO;
745 ps->send_active=GNUNET_NO;
746 ps->recv_connected=GNUNET_NO;
747 ps->recv_active=GNUNET_NO;
749 ps->session_id =id_num;
750 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
751 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
754 *httpSessionCache = ps;
755 if (ps->msgtok==NULL)
756 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
761 GNUNET_i2s(&pc->identity),
762 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
765 /* Is it a PUT or a GET request */
766 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
768 if (ps->recv_force_disconnect == GNUNET_YES)
770 #if DEBUG_CONNECTIONS
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
773 ps->recv_active = GNUNET_NO;
776 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
778 ps->recv_endpoint = mhd_connection;
779 ps->recv_connected = GNUNET_YES;
780 ps->recv_active = GNUNET_YES;
781 ps->recv_force_disconnect = GNUNET_NO;
782 #if DEBUG_CONNECTIONS
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
788 /* Transmission of all data complete */
789 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
791 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
792 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
793 #if DEBUG_CONNECTIONS
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
796 MHD_destroy_response (response);
797 ps->recv_active=GNUNET_NO;
802 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
804 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
805 (*upload_data_size) = 0;
811 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
813 if (ps->send_force_disconnect == GNUNET_YES)
815 #if DEBUG_CONNECTIONS
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
818 ps->send_active = GNUNET_NO;
821 ps->send_connected = GNUNET_YES;
822 ps->send_active = GNUNET_YES;
823 ps->send_endpoint = mhd_connection;
824 ps->send_force_disconnect = GNUNET_NO;
825 #if DEBUG_CONNECTIONS
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
828 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
829 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
830 MHD_destroy_response (response);
838 * Call MHD to process pending ipv4 requests and then go back
839 * and schedule the next run.
841 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
843 * Call MHD to process pending ipv6 requests and then go back
844 * and schedule the next run.
846 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
849 * Function that queries MHD's select sets and
850 * starts the task waiting for them.
852 static GNUNET_SCHEDULER_TaskIdentifier
853 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
855 struct Plugin *plugin = cls;
856 GNUNET_SCHEDULER_TaskIdentifier ret;
860 struct GNUNET_NETWORK_FDSet *wrs;
861 struct GNUNET_NETWORK_FDSet *wws;
862 struct GNUNET_NETWORK_FDSet *wes;
864 unsigned long long timeout;
866 struct GNUNET_TIME_Relative tv;
868 GNUNET_assert(cls !=NULL);
869 ret = GNUNET_SCHEDULER_NO_TASK;
873 wrs = GNUNET_NETWORK_fdset_create ();
874 wes = GNUNET_NETWORK_fdset_create ();
875 wws = GNUNET_NETWORK_fdset_create ();
877 GNUNET_assert (MHD_YES ==
878 MHD_get_fdset (daemon_handle,
883 haveto = MHD_get_timeout (daemon_handle, &timeout);
884 if (haveto == MHD_YES)
885 tv.value = (uint64_t) timeout;
887 tv = GNUNET_TIME_UNIT_FOREVER_REL;
888 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
889 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
890 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
891 if (daemon_handle == plugin->http_server_daemon_v4)
893 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
895 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
896 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
899 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
900 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
901 GNUNET_SCHEDULER_NO_TASK,
905 &http_server_daemon_v4_run,
908 if (daemon_handle == plugin->http_server_daemon_v6)
910 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
912 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
913 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
916 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
917 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
918 GNUNET_SCHEDULER_NO_TASK,
922 &http_server_daemon_v6_run,
925 GNUNET_NETWORK_fdset_destroy (wrs);
926 GNUNET_NETWORK_fdset_destroy (wws);
927 GNUNET_NETWORK_fdset_destroy (wes);
932 * Call MHD to process pending requests and then go back
933 * and schedule the next run.
935 static void http_server_daemon_v4_run (void *cls,
936 const struct GNUNET_SCHEDULER_TaskContext *tc)
938 struct Plugin *plugin = cls;
940 GNUNET_assert(cls !=NULL);
941 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
943 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
946 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
947 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
953 * Call MHD to process pending requests and then go back
954 * and schedule the next run.
956 static void http_server_daemon_v6_run (void *cls,
957 const struct GNUNET_SCHEDULER_TaskContext *tc)
959 struct Plugin *plugin = cls;
961 GNUNET_assert(cls !=NULL);
962 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
964 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
967 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
968 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
973 * Function setting up curl handle and selecting message to send
975 * @param ses session to send data to
976 * @param con connection
977 * @return bytes sent to peer
979 static ssize_t send_check_connections (void *cls, struct Session *ps);
981 static size_t curl_get_header_function( void *ptr, size_t size, size_t nmemb, void *stream)
983 struct Session * ps = stream;
986 size_t len = size * nmemb;
987 long http_result = 0;
989 /* Getting last http result code */
990 if (ps->recv_connected==GNUNET_NO)
992 GNUNET_assert(NULL!=ps);
993 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
996 if (http_result == 200)
998 ps->recv_connected = GNUNET_YES;
999 ps->recv_active = GNUNET_YES;
1000 #if DEBUG_CONNECTIONS
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1003 // Calling send_check_connections again since receive is established
1004 send_check_connections (ps->peercontext->plugin, ps);
1010 if ((size * nmemb) < SIZE_MAX)
1011 tmp = GNUNET_malloc (len+1);
1013 if ((tmp != NULL) && (len > 0))
1015 memcpy(tmp,ptr,len);
1018 if (tmp[len-2] == 13)
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s' %u \n",tmp, http_result);
1028 return size * nmemb;
1031 static size_t curl_put_header_function( void *ptr, size_t size, size_t nmemb, void *stream)
1033 struct Session * ps = stream;
1036 size_t len = size * nmemb;
1037 long http_result = 0;
1040 /* Getting last http result code */
1041 GNUNET_assert(NULL!=ps);
1042 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1043 if (CURLE_OK == res)
1045 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1047 ps->send_connected = GNUNET_YES;
1048 ps->send_active = GNUNET_YES;
1049 #if DEBUG_CONNECTIONS
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1053 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1055 ps->send_connected = GNUNET_NO;
1056 ps->send_active = GNUNET_NO;
1057 #if DEBUG_CONNECTIONS
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1064 if ((size * nmemb) < SIZE_MAX)
1065 tmp = GNUNET_malloc (len+1);
1067 if ((tmp != NULL) && (len > 0))
1069 memcpy(tmp,ptr,len);
1072 if (tmp[len-2] == 13)
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s' %u \n",tmp, http_result);
1082 return size * nmemb;
1086 * Callback method used with libcurl
1087 * Method is called when libcurl needs to read data during sending
1088 * @param stream pointer where to write data
1089 * @param size size of an individual element
1090 * @param nmemb count of elements that can be written to the buffer
1091 * @param ptr source pointer, passed to the libcurl handle
1092 * @return bytes written to stream
1094 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1096 struct Session * ps = ptr;
1097 struct HTTP_Message * msg = ps->pending_msgs_tail;
1101 if (ps->send_active == GNUNET_NO)
1102 return CURL_READFUNC_PAUSE;
1105 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1107 #if DEBUG_CONNECTIONS
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1110 ps->send_active = GNUNET_NO;
1111 return CURL_READFUNC_PAUSE;
1114 msg = ps->pending_msgs_tail;
1116 if (msg->pos < msg->size)
1118 /* data fit in buffer */
1119 if ((msg->size - msg->pos) <= (size * nmemb))
1121 len = (msg->size - msg->pos);
1122 memcpy(stream, &msg->buf[msg->pos], len);
1129 memcpy(stream, &msg->buf[msg->pos], len);
1134 /* no data to send */
1140 if ( msg->pos == msg->size)
1142 #if DEBUG_CONNECTIONS
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1145 /* Calling transmit continuation */
1146 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
1147 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1148 remove_http_message(ps, msg);
1154 * Callback method used with libcurl
1155 * Method is called when libcurl needs to write data during sending
1156 * @param stream pointer where to write data
1157 * @param size size of an individual element
1158 * @param nmemb count of elements that can be written to the buffer
1159 * @param ptr destination pointer, passed to the libcurl handle
1160 * @return bytes read from stream
1162 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1164 struct Session * ps = ptr;
1165 #if DEBUG_CONNECTIONS
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1168 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1169 return (size * nmemb);
1174 * Function setting up file descriptors and scheduling task to run
1175 * @param cls closure
1176 * @param ses session to send data to
1179 static int curl_schedule(void *cls );
1184 * Function setting up curl handle and selecting message to send
1186 * @param ses session to send data to
1187 * @param con connection
1188 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1190 static ssize_t send_check_connections (void *cls, struct Session *ps)
1192 struct Plugin *plugin = cls;
1194 struct HTTP_Message * msg;
1196 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1198 GNUNET_assert(cls !=NULL);
1200 if (ps->direction == OUTBOUND)
1202 /* RECV DIRECTION */
1203 /* Check if session is connected to receive data, otherwise connect to peer */
1204 if (ps->recv_connected == GNUNET_NO)
1206 if (ps->recv_endpoint == NULL)
1208 ps->recv_endpoint = curl_easy_init();
1210 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1212 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1213 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_function);
1214 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1215 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1216 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1217 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1218 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1219 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1220 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1221 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1222 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1224 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1225 if (mret != CURLM_OK)
1227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1228 _("%s failed at %s:%d: `%s'\n"),
1229 "curl_multi_add_handle", __FILE__, __LINE__,
1230 curl_multi_strerror (mret));
1231 return GNUNET_SYSERR;
1233 if (curl_schedule (plugin) == GNUNET_SYSERR)
1234 return GNUNET_SYSERR;
1235 #if DEBUG_CONNECTIONS
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1241 /* waiting for receive direction */
1242 if (ps->recv_connected==GNUNET_NO)
1245 /* SEND DIRECTION */
1246 /* Check if session is connected to send data, otherwise connect to peer */
1247 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1249 if (ps->send_active == GNUNET_YES)
1251 #if DEBUG_CONNECTIONS
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1256 if (ps->send_active == GNUNET_NO)
1258 #if DEBUG_CONNECTIONS
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1261 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1263 ps->send_active=GNUNET_YES;
1267 return GNUNET_SYSERR;
1270 /* not connected, initiate connection */
1271 if ((ps->send_connected==GNUNET_NO) && (NULL == ps->send_endpoint))
1272 ps->send_endpoint = curl_easy_init();
1273 GNUNET_assert (ps->send_endpoint != NULL);
1274 GNUNET_assert (NULL != ps->pending_msgs_tail);
1275 #if DEBUG_CONNECTIONS
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1278 ps->send_active = GNUNET_NO;
1279 msg = ps->pending_msgs_tail;
1282 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1284 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1285 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1286 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_function);
1287 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1288 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1289 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1290 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1291 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1292 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1293 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1294 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1295 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1297 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1298 if (mret != CURLM_OK)
1300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1301 _("%s failed at %s:%d: `%s'\n"),
1302 "curl_multi_add_handle", __FILE__, __LINE__,
1303 curl_multi_strerror (mret));
1304 return GNUNET_SYSERR;
1306 if (curl_schedule (plugin) == GNUNET_SYSERR)
1307 return GNUNET_SYSERR;
1310 if (ps->direction == INBOUND)
1312 GNUNET_assert (NULL != ps->pending_msgs_tail);
1313 msg = ps->pending_msgs_tail;
1314 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES))
1317 return GNUNET_SYSERR;
1320 static void curl_perform (void *cls,
1321 const struct GNUNET_SCHEDULER_TaskContext *tc)
1323 struct Plugin *plugin = cls;
1324 static unsigned int handles_last_run;
1326 struct CURLMsg *msg;
1328 struct Session *ps = NULL;
1329 struct HTTP_PeerContext *pc = NULL;
1330 struct HTTP_Message * cur_msg = NULL;
1333 GNUNET_assert(cls !=NULL);
1335 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1336 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1342 mret = curl_multi_perform (plugin->multi_handle, &running);
1343 if ((running < handles_last_run) && (running>0))
1348 msg = curl_multi_info_read (plugin->multi_handle, &running);
1351 /* get session for affected curl handle */
1352 GNUNET_assert ( msg->easy_handle != NULL );
1353 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char *) &ps);
1354 GNUNET_assert ( ps != NULL );
1355 pc = ps->peercontext;
1356 GNUNET_assert ( pc != NULL );
1361 if ( (msg->data.result != CURLE_OK) &&
1362 (msg->data.result != CURLE_GOT_NOTHING) )
1364 /* sending msg failed*/
1365 if (msg->easy_handle == ps->send_endpoint)
1367 #if DEBUG_CONNECTIONS
1368 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1369 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1371 GNUNET_i2s(&pc->identity),
1372 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1373 "curl_multi_perform",
1374 curl_easy_strerror (msg->data.result));
1376 ps->send_connected = GNUNET_NO;
1377 ps->send_active = GNUNET_NO;
1378 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1379 curl_easy_cleanup(ps->send_endpoint);
1380 ps->send_endpoint=NULL;
1381 cur_msg = ps->pending_msgs_tail;
1382 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1383 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1385 /* GET connection failed */
1386 if (msg->easy_handle == ps->recv_endpoint)
1388 #if DEBUG_CONNECTIONS
1389 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1390 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1392 GNUNET_i2s(&pc->identity),
1393 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1394 "curl_multi_perform",
1395 curl_easy_strerror (msg->data.result));
1397 ps->recv_connected = GNUNET_NO;
1398 ps->recv_active = GNUNET_NO;
1399 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1400 curl_easy_cleanup(ps->recv_endpoint);
1401 ps->recv_endpoint=NULL;
1406 if (msg->easy_handle == ps->send_endpoint)
1408 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1409 #if DEBUG_CONNECTIONS
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1411 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1413 GNUNET_i2s(&pc->identity),
1414 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1417 /* Calling transmit continuation */
1418 cur_msg = ps->pending_msgs_tail;
1419 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1421 /* HTTP 1xx : Last message before here was informational */
1422 if ((http_result >=100) && (http_result < 200))
1423 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1424 /* HTTP 2xx: successful operations */
1425 if ((http_result >=200) && (http_result < 300))
1426 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1427 /* HTTP 3xx..5xx: error */
1428 if ((http_result >=300) && (http_result < 600))
1429 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1431 ps->send_connected = GNUNET_NO;
1432 ps->send_active = GNUNET_NO;
1433 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1434 curl_easy_cleanup(ps->send_endpoint);
1435 ps->send_endpoint =NULL;
1437 if (msg->easy_handle == ps->recv_endpoint)
1439 #if DEBUG_CONNECTIONS
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1441 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1443 GNUNET_i2s(&pc->identity),
1444 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1447 ps->recv_connected = GNUNET_NO;
1448 ps->recv_active = GNUNET_NO;
1449 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1450 curl_easy_cleanup(ps->recv_endpoint);
1451 ps->recv_endpoint=NULL;
1454 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1455 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1462 while ( (running > 0) );
1464 handles_last_run = running;
1466 while (mret == CURLM_CALL_MULTI_PERFORM);
1467 curl_schedule(plugin);
1472 * Function setting up file descriptors and scheduling task to run
1473 * @param ses session to send data to
1474 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1476 static int curl_schedule(void *cls )
1478 struct Plugin *plugin = cls;
1483 struct GNUNET_NETWORK_FDSet *grs;
1484 struct GNUNET_NETWORK_FDSet *gws;
1488 GNUNET_assert(cls !=NULL);
1490 /* Cancel previous scheduled task */
1491 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1493 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1494 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1501 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1502 if (mret != CURLM_OK)
1504 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1505 _("%s failed at %s:%d: `%s'\n"),
1506 "curl_multi_fdset", __FILE__, __LINE__,
1507 curl_multi_strerror (mret));
1508 return GNUNET_SYSERR;
1510 mret = curl_multi_timeout (plugin->multi_handle, &to);
1511 if (mret != CURLM_OK)
1513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1514 _("%s failed at %s:%d: `%s'\n"),
1515 "curl_multi_timeout", __FILE__, __LINE__,
1516 curl_multi_strerror (mret));
1517 return GNUNET_SYSERR;
1520 grs = GNUNET_NETWORK_fdset_create ();
1521 gws = GNUNET_NETWORK_fdset_create ();
1522 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1523 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1524 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1525 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1526 GNUNET_SCHEDULER_NO_TASK,
1527 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1532 GNUNET_NETWORK_fdset_destroy (gws);
1533 GNUNET_NETWORK_fdset_destroy (grs);
1539 * Function that can be used by the transport service to transmit
1540 * a message using the plugin. Note that in the case of a
1541 * peer disconnecting, the continuation MUST be called
1542 * prior to the disconnect notification itself. This function
1543 * will be called with this peer's HELLO message to initiate
1544 * a fresh connection to another peer.
1546 * @param cls closure
1547 * @param target who should receive this message
1548 * @param msgbuf the message to transmit
1549 * @param msgbuf_size number of bytes in 'msgbuf'
1550 * @param priority how important is the message (most plugins will
1551 * ignore message priority and just FIFO)
1552 * @param timeout how long to wait at most for the transmission (does not
1553 * require plugins to discard the message after the timeout,
1554 * just advisory for the desired delay; most plugins will ignore
1556 * @param session which session must be used (or NULL for "any")
1557 * @param addr the address to use (can be NULL if the plugin
1558 * is "on its own" (i.e. re-use existing TCP connection))
1559 * @param addrlen length of the address in bytes
1560 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1561 * GNUNET_NO means the plugin may use any other address and
1562 * GNUNET_SYSERR means that only reliable existing
1563 * bi-directional connections should be used (regardless
1565 * @param cont continuation to call once the message has
1566 * been transmitted (or if the transport is ready
1567 * for the next transmission call; or if the
1568 * peer disconnected...); can be NULL
1569 * @param cont_cls closure for cont
1570 * @return number of bytes used (on the physical network, with overheads);
1571 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1572 * and does NOT mean that the message was not transmitted (DV)
1575 http_plugin_send (void *cls,
1576 const struct GNUNET_PeerIdentity *target,
1579 unsigned int priority,
1580 struct GNUNET_TIME_Relative to,
1581 struct Session *session,
1585 GNUNET_TRANSPORT_TransmitContinuation cont,
1588 struct Plugin *plugin = cls;
1589 struct HTTP_Message *msg;
1591 struct HTTP_PeerContext * pc;
1592 struct Session * ps = NULL;
1593 struct Session * ps_tmp = NULL;
1595 GNUNET_assert(cls !=NULL);
1597 char * force = GNUNET_malloc(40);
1598 if (force_address == GNUNET_YES)
1599 strcpy(force,"forced addr.");
1600 if (force_address == GNUNET_NO)
1601 strcpy(force,"any addr.");
1602 if (force_address == GNUNET_SYSERR)
1603 strcpy(force,"reliable bi-direc. address addr.");
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1608 http_plugin_address_to_string(NULL, addr, addrlen),
1612 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1616 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1617 pc->plugin = plugin;
1618 pc->session_id_counter=1;
1619 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1620 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1623 /* Search for existing session using the passed address */
1624 if ((addr!=NULL) && (addrlen != 0))
1626 ps = get_Session(plugin, pc, addr, addrlen);
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Found existing connection to peer %s with given address, using %X\n", GNUNET_i2s(target), ps);
1631 /* Search for existing session using the passed session */
1632 if ((ps==NULL) && (force_address != GNUNET_YES))
1635 while (ps_tmp!=NULL)
1637 if ((ps_tmp==session) && (ps_tmp->recv_force_disconnect==GNUNET_NO) && (ps_tmp->send_force_disconnect==GNUNET_NO) &&
1638 (ps_tmp->recv_connected==GNUNET_YES) && (ps_tmp->send_connected==GNUNET_YES))
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Found existing connection to peer %s with given session, using inbound session %X\n", GNUNET_i2s(target), ps_tmp);
1644 ps_tmp=ps_tmp->next;
1648 /* session not existing, address not forced -> looking for other session */
1649 if ((ps==NULL) && (force_address != GNUNET_YES))
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection, but free to choose existing, searching for existing connection to peer %s\n", GNUNET_i2s(target));
1652 /* Choosing different session to peer when possible */
1653 struct Session * tmp = pc->head;
1656 if ((tmp->recv_connected) && (tmp->send_connected) && (tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection to peer %s, selected connection %X\n", GNUNET_i2s(target),ps);
1665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection to peer %s, no connection found\n", GNUNET_i2s(target));
1668 /* session not existing, but address forced -> creating new session */
1669 if ((ps==NULL) || ((ps==NULL) && (force_address == GNUNET_YES)))
1671 if ((addr!=NULL) && (addrlen!=0))
1673 if (force_address == GNUNET_YES)
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new connection to peer %s\n", GNUNET_i2s(target));
1675 if (force_address != GNUNET_YES)
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new connection to peer %s\n", GNUNET_i2s(target));
1678 ps = GNUNET_malloc(sizeof (struct Session));
1679 if ((addrlen!=0) && (addr!=NULL))
1681 ps->addr = GNUNET_malloc(addrlen);
1682 memcpy(ps->addr,addr,addrlen);
1683 ps->addrlen = addrlen;
1690 ps->direction=OUTBOUND;
1691 ps->recv_connected = GNUNET_NO;
1692 ps->recv_force_disconnect = GNUNET_NO;
1693 ps->send_connected = GNUNET_NO;
1694 ps->send_force_disconnect = GNUNET_NO;
1695 ps->pending_msgs_head = NULL;
1696 ps->pending_msgs_tail = NULL;
1698 ps->session_id = pc->session_id_counter;
1699 pc->session_id_counter++;
1700 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1701 if (ps->msgtok == NULL)
1702 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
1703 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing session & and no address given: no way to send this message to peer `%s'!\n", GNUNET_i2s(target));
1708 return GNUNET_SYSERR;
1713 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1715 msg->size = msgbuf_size;
1717 msg->buf = (char *) &msg[1];
1718 msg->transmit_cont = cont;
1719 msg->transmit_cont_cls = cont_cls;
1720 memcpy (msg->buf,msgbuf, msgbuf_size);
1721 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1723 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
1726 return GNUNET_SYSERR;
1732 * Function that can be used to force the plugin to disconnect
1733 * from the given peer and cancel all previous transmissions
1734 * (and their continuationc).
1736 * @param cls closure
1737 * @param target peer from which to disconnect
1740 http_plugin_disconnect (void *cls,
1741 const struct GNUNET_PeerIdentity *target)
1745 struct Plugin *plugin = cls;
1746 struct HTTP_PeerContext *pc = NULL;
1747 struct Session *ps = NULL;
1748 //struct Session *tmp = NULL;
1750 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1757 /* Telling transport that session is getting disconnected */
1758 plugin->env->session_end(plugin, target, ps);
1759 if (ps->direction==OUTBOUND)
1761 if (ps->send_endpoint!=NULL)
1763 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
1764 //curl_easy_cleanup(ps->send_endpoint);
1765 //ps->send_endpoint=NULL;
1766 ps->send_force_disconnect = GNUNET_YES;
1768 if (ps->recv_endpoint!=NULL)
1770 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
1771 //curl_easy_cleanup(ps->recv_endpoint);
1772 //ps->recv_endpoint=NULL;
1773 ps->recv_force_disconnect = GNUNET_YES;
1777 if (ps->direction==INBOUND)
1779 ps->recv_force_disconnect = GNUNET_YES;
1780 ps->send_force_disconnect = GNUNET_YES;
1783 while (ps->pending_msgs_head!=NULL)
1785 remove_http_message(ps, ps->pending_msgs_head);
1787 ps->recv_active = GNUNET_NO;
1788 ps->send_active = GNUNET_NO;
1795 * Convert the transports address to a nice, human-readable
1798 * @param cls closure
1799 * @param type name of the transport that generated the address
1800 * @param addr one of the addresses of the host, NULL for the last address
1801 * the specific address format depends on the transport
1802 * @param addrlen length of the address
1803 * @param numeric should (IP) addresses be displayed in numeric form?
1804 * @param timeout after how long should we give up?
1805 * @param asc function to call on each string
1806 * @param asc_cls closure for asc
1809 http_plugin_address_pretty_printer (void *cls,
1814 struct GNUNET_TIME_Relative timeout,
1815 GNUNET_TRANSPORT_AddressStringCallback
1818 const struct IPv4HttpAddress *t4;
1819 const struct IPv6HttpAddress *t6;
1820 struct sockaddr_in a4;
1821 struct sockaddr_in6 a6;
1827 GNUNET_assert(cls !=NULL);
1828 if (addrlen == sizeof (struct IPv6HttpAddress))
1830 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1832 a6.sin6_addr = t6->ipv6_addr;
1833 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1834 port = ntohs(t6->u6_port);
1836 else if (addrlen == sizeof (struct IPv4HttpAddress))
1838 address = GNUNET_malloc (INET_ADDRSTRLEN);
1840 a4.sin_addr.s_addr = t4->ipv4_addr;
1841 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1842 port = ntohs(t4->u_port);
1846 /* invalid address */
1847 GNUNET_break_op (0);
1848 asc (asc_cls, NULL);
1851 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1852 GNUNET_free (address);
1853 GNUNET_assert(res != 0);
1861 * Another peer has suggested an address for this
1862 * peer and transport plugin. Check that this could be a valid
1863 * address. If so, consider adding it to the list
1866 * @param cls closure
1867 * @param addr pointer to the address
1868 * @param addrlen length of addr
1869 * @return GNUNET_OK if this is a plausible address for this peer
1873 http_plugin_address_suggested (void *cls,
1874 const void *addr, size_t addrlen)
1876 struct Plugin *plugin = cls;
1877 struct IPv4HttpAddress *v4;
1878 struct IPv6HttpAddress *v6;
1881 GNUNET_assert(cls !=NULL);
1882 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1883 (addrlen != sizeof (struct IPv6HttpAddress)))
1885 return GNUNET_SYSERR;
1887 if (addrlen == sizeof (struct IPv4HttpAddress))
1889 v4 = (struct IPv4HttpAddress *) addr;
1890 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1892 return GNUNET_SYSERR;
1894 port = ntohs (v4->u_port);
1895 if (port != plugin->port_inbound)
1897 return GNUNET_SYSERR;
1900 if (addrlen == sizeof (struct IPv6HttpAddress))
1902 v6 = (struct IPv6HttpAddress *) addr;
1903 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1905 return GNUNET_SYSERR;
1907 port = ntohs (v6->u6_port);
1908 if (port != plugin->port_inbound)
1910 return GNUNET_SYSERR;
1919 * Function called for a quick conversion of the binary address to
1920 * a numeric address. Note that the caller must not free the
1921 * address and that the next call to this function is allowed
1922 * to override the address again.
1924 * @param cls closure
1925 * @param addr binary address
1926 * @param addrlen length of the address
1927 * @return string representing the same address
1930 http_plugin_address_to_string (void *cls,
1934 const struct IPv4HttpAddress *t4;
1935 const struct IPv6HttpAddress *t6;
1936 struct sockaddr_in a4;
1937 struct sockaddr_in6 a6;
1943 if (addrlen == sizeof (struct IPv6HttpAddress))
1945 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1947 a6.sin6_addr = t6->ipv6_addr;
1948 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1949 port = ntohs(t6->u6_port);
1951 else if (addrlen == sizeof (struct IPv4HttpAddress))
1953 address = GNUNET_malloc (INET_ADDRSTRLEN);
1955 a4.sin_addr.s_addr = t4->ipv4_addr;
1956 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1957 port = ntohs(t4->u_port);
1961 /* invalid address */
1964 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
1965 GNUNET_free (address);
1966 GNUNET_assert(res != 0);
1971 * Add the IP of our network interface to the list of
1972 * our external IP addresses.
1974 * @param cls the 'struct Plugin*'
1975 * @param name name of the interface
1976 * @param isDefault do we think this may be our default interface
1977 * @param addr address of the interface
1978 * @param addrlen number of bytes in addr
1979 * @return GNUNET_OK to continue iterating
1982 process_interfaces (void *cls,
1985 const struct sockaddr *addr, socklen_t addrlen)
1987 struct Plugin *plugin = cls;
1988 struct IPv4HttpAddress * t4;
1989 struct IPv6HttpAddress * t6;
1992 GNUNET_assert(cls !=NULL);
1993 af = addr->sa_family;
1996 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
1997 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1999 /* skip loopback addresses */
2002 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2003 t4->u_port = htons (plugin->port_inbound);
2004 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
2007 else if (af == AF_INET6)
2009 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
2010 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
2012 /* skip link local addresses */
2015 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
2017 /* skip loopback addresses */
2020 memcpy (&t6->ipv6_addr,
2021 &((struct sockaddr_in6 *) addr)->sin6_addr,
2022 sizeof (struct in6_addr));
2023 t6->u6_port = htons (plugin->port_inbound);
2024 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
2030 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
2032 struct Plugin *plugin = cls;
2033 struct HTTP_PeerContext * pc = value;
2034 struct Session * ps = pc->head;
2035 struct Session * tmp = NULL;
2036 struct HTTP_Message * msg = NULL;
2037 struct HTTP_Message * msg_tmp = NULL;
2039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
2043 plugin->env->session_end(plugin, &pc->identity, ps);
2046 GNUNET_free_non_null (ps->addr);
2047 GNUNET_free(ps->url);
2048 if (ps->msgtok != NULL)
2049 GNUNET_SERVER_mst_destroy (ps->msgtok);
2051 msg = ps->pending_msgs_head;
2054 msg_tmp = msg->next;
2058 if (ps->direction==OUTBOUND)
2060 if (ps->send_endpoint!=NULL)
2061 curl_easy_cleanup(ps->send_endpoint);
2062 if (ps->recv_endpoint!=NULL)
2063 curl_easy_cleanup(ps->recv_endpoint);
2075 * Exit point from the plugin.
2078 libgnunet_plugin_transport_http_done (void *cls)
2080 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2081 struct Plugin *plugin = api->cls;
2083 GNUNET_assert(cls !=NULL);
2085 if (plugin->http_server_daemon_v4 != NULL)
2087 MHD_stop_daemon (plugin->http_server_daemon_v4);
2088 plugin->http_server_daemon_v4 = NULL;
2090 if (plugin->http_server_daemon_v6 != NULL)
2092 MHD_stop_daemon (plugin->http_server_daemon_v6);
2093 plugin->http_server_daemon_v6 = NULL;
2096 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2098 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2099 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2102 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2104 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2105 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2109 /* free all peer information */
2110 if (plugin->peers!=NULL)
2112 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2113 &remove_peer_context_Iterator,
2115 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2117 if (plugin->multi_handle!=NULL)
2119 mret = curl_multi_cleanup(plugin->multi_handle);
2120 if ( CURLM_OK != mret)
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2122 plugin->multi_handle = NULL;
2124 curl_global_cleanup();
2126 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2128 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2129 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2132 GNUNET_free (plugin);
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2140 * Entry point for the plugin.
2143 libgnunet_plugin_transport_http_init (void *cls)
2145 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2146 struct Plugin *plugin;
2147 struct GNUNET_TRANSPORT_PluginFunctions *api;
2148 struct GNUNET_TIME_Relative gn_timeout;
2149 long long unsigned int port;
2151 GNUNET_assert(cls !=NULL);
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
2154 plugin = GNUNET_malloc (sizeof (struct Plugin));
2155 plugin->stats = env->stats;
2157 plugin->peers = NULL;
2159 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2161 api->send = &http_plugin_send;
2162 api->disconnect = &http_plugin_disconnect;
2163 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2164 api->check_address = &http_plugin_address_suggested;
2165 api->address_to_string = &http_plugin_address_to_string;
2167 /* Hashing our identity to use it in URLs */
2168 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2170 /* Reading port number from config file */
2172 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2178 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2181 ("Require valid port number for transport plugin `%s' in configuration!\n"),
2183 libgnunet_plugin_transport_http_done (api);
2186 GNUNET_assert ((port > 0) && (port <= 65535));
2187 plugin->port_inbound = port;
2188 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2189 unsigned int timeout = (gn_timeout.value) / 1000;
2190 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->http_server_daemon_v6 == NULL) && (port != 0))
2192 plugin->http_server_daemon_v6 = MHD_start_daemon (
2199 plugin , &mdh_access_cb, plugin,
2200 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2201 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2202 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2203 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2204 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2206 plugin->http_server_daemon_v4 = MHD_start_daemon (
2213 plugin , &mdh_access_cb, plugin,
2214 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2215 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2216 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2217 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2218 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2221 if (plugin->http_server_daemon_v4 != NULL)
2222 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2223 if (plugin->http_server_daemon_v6 != NULL)
2224 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2226 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
2228 else if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
2232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
2233 libgnunet_plugin_transport_http_done (api);
2237 /* Initializing cURL */
2238 curl_global_init(CURL_GLOBAL_ALL);
2239 plugin->multi_handle = curl_multi_init();
2241 if ( NULL == plugin->multi_handle )
2243 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2245 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2247 libgnunet_plugin_transport_http_done (api);
2251 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2252 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2257 /* end of plugin_transport_http.c */