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_YES
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
75 * Network format for IPv4 addresses.
77 struct IPv4HttpAddress
80 * IPv4 address, in network byte order.
82 uint32_t ipv4_addr GNUNET_PACKED;
85 * Port number, in network byte order.
87 uint16_t u_port GNUNET_PACKED;
93 * Network format for IPv6 addresses.
95 struct IPv6HttpAddress
100 struct in6_addr ipv6_addr GNUNET_PACKED;
103 * Port number, in network byte order.
105 uint16_t u6_port GNUNET_PACKED;
111 * Message to send using http
116 * next pointer for double linked list
118 struct HTTP_Message * next;
121 * previous pointer for double linked list
123 struct HTTP_Message * prev;
126 * buffer containing data to send
131 * amount of data already sent
141 * Continuation function to call once the transmission buffer
142 * has again space available. NULL if there is no
143 * continuation to call.
145 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
148 * Closure for transmit_cont.
150 void *transmit_cont_cls;
154 struct HTTP_PeerContext
159 struct GNUNET_PeerIdentity identity;
162 * Pointer to the global plugin struct.
164 struct Plugin *plugin;
167 * Linked list of connections with this peer
170 struct Session * head;
173 * Linked list of connections with this peer
176 struct Session * tail;
179 * id for next session
181 size_t session_id_counter;
190 struct SessionHeader header;
193 * next session in linked list
195 struct Session * next;
198 * previous session in linked list
200 struct Session * prev;
203 * address of this session
218 * Message queue for outbound messages
221 struct HTTP_Message * pending_msgs_head;
224 * Message queue for outbound messages
227 struct HTTP_Message * pending_msgs_tail;
230 * partner peer this connection belongs to
232 struct HTTP_PeerContext * peercontext;
235 * message stream tokenizer for incoming data
237 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
241 * outbound: OUTBOUND (GNUNET_YES)
242 * inbound : INBOUND (GNUNET_NO)
244 unsigned int direction;
247 * is session connected to send data?
249 unsigned int send_connected;
252 * is send connection active?
254 unsigned int send_active;
257 * connection disconnect forced (e.g. from transport)
259 unsigned int send_force_disconnect;
262 * is session connected to receive data?
264 unsigned int recv_connected;
267 * is receive connection active?
269 unsigned int recv_active;
272 * connection disconnect forced (e.g. from transport)
274 unsigned int recv_force_disconnect;
277 * id for next session
278 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
283 * entity managing sending data
284 * outbound session: CURL *
285 * inbound session: mhd_connection *
287 void * send_endpoint;
290 * entity managing recieving data
291 * outbound session: CURL *
292 * inbound session: mhd_connection *
294 void * recv_endpoint;
298 * Encapsulation of all of the state of the plugin.
305 struct GNUNET_TRANSPORT_PluginEnvironment *env;
307 unsigned int port_inbound;
309 struct GNUNET_CONTAINER_MultiHashMap *peers;
312 * Daemon for listening for new IPv4 connections.
314 struct MHD_Daemon *http_server_daemon_v4;
317 * Daemon for listening for new IPv6connections.
319 struct MHD_Daemon *http_server_daemon_v6;
322 * Our primary task for http daemon handling IPv4 connections
324 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
327 * Our primary task for http daemon handling IPv6 connections
329 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
332 * The task sending data
334 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
339 CURLM * multi_handle;
342 * Our ASCII encoded, hashed peer identity
343 * This string is used to distinguish between connections and is added to the urls
345 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
350 * Function called for a quick conversion of the binary address to
351 * a numeric address. Note that the caller must not free the
352 * address and that the next call to this function is allowed
353 * to override the address again.
356 * @param addr binary address
357 * @param addrlen length of the address
358 * @return string representing the same address
361 http_plugin_address_to_string (void *cls,
365 static char * create_url(void * cls, const void * addr, size_t addrlen, size_t id)
367 struct Plugin *plugin = cls;
370 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
371 GNUNET_asprintf(&url,
373 http_plugin_address_to_string(NULL, addr, addrlen),
374 (char *) (&plugin->my_ascii_hash_ident),id);
380 * Removes a message from the linked list of messages
381 * @param con connection to remove message from
382 * @param msg message to remove
383 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
385 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
387 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
393 * Removes a session from the linked list of sessions
394 * @param pc peer context
396 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
397 * @param call_msg_cont_result, result to call message continuations with
398 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
400 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
402 struct HTTP_Message * msg;
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: removing %s session with id %u\n", ps, (ps->direction == INBOUND) ? "inbound" : "outbound",ps->session_id);
404 GNUNET_free_non_null (ps->addr);
405 GNUNET_SERVER_mst_destroy (ps->msgtok);
406 GNUNET_free(ps->url);
408 msg = ps->pending_msgs_head;
411 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
413 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
416 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
419 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
425 static struct Session * get_Session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addr_len)
427 struct Session * cc = pc->head;
428 struct Session * con = NULL;
429 unsigned int count = 0;
431 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
434 if (addr_len == cc->addrlen)
436 if (0 == memcmp(cc->addr, addr, addr_len))
438 /* connection can not be used, since it is disconnected */
439 if ((cc->recv_force_disconnect==GNUNET_NO) && (cc->send_force_disconnect==GNUNET_NO))
452 * Callback called by MHD when a connection is terminated
454 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
456 struct Session * ps = *httpSessionCache;
459 struct HTTP_PeerContext * pc = ps->peercontext;
461 if (connection==ps->recv_endpoint)
463 #if DEBUG_CONNECTIONS
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
466 ps->recv_active = GNUNET_NO;
467 ps->recv_connected = GNUNET_NO;
468 ps->recv_endpoint = NULL;
470 if (connection==ps->send_endpoint)
473 ps->send_active = GNUNET_NO;
474 ps->send_connected = GNUNET_NO;
475 ps->send_endpoint = NULL;
476 #if DEBUG_CONNECTIONS
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
481 /* if both connections disconnected, remove session */
482 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
484 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
488 static void mhd_write_mst_cb (void *cls,
490 const struct GNUNET_MessageHeader *message)
493 struct Session *ps = cls;
494 struct HTTP_PeerContext *pc = ps->peercontext;
495 GNUNET_assert(ps != NULL);
496 GNUNET_assert(pc != NULL);
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
501 ntohs(message->type),
502 ntohs(message->size),
503 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
505 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
512 static void curl_receive_mst_cb (void *cls,
514 const struct GNUNET_MessageHeader *message)
516 struct Session *ps = cls;
517 struct HTTP_PeerContext *pc = ps->peercontext;
518 GNUNET_assert(ps != NULL);
519 GNUNET_assert(pc != NULL);
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
523 ntohs(message->type),
524 ntohs(message->size),
525 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
527 pc->plugin->env->receive (pc->plugin->env->cls,
536 * Check if ip is allowed to connect.
539 mhd_accept_cb (void *cls,
540 const struct sockaddr *addr, socklen_t addr_len)
543 struct Plugin *plugin = cls;
545 /* Every connection is accepted, nothing more to do here */
549 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
553 struct Session * ps = cls;
554 struct HTTP_PeerContext * pc;
555 struct HTTP_Message * msg;
558 GNUNET_assert (ps!=NULL);
559 pc = ps->peercontext;
560 msg = ps->pending_msgs_tail;
561 if (ps->send_force_disconnect==GNUNET_YES)
563 #if DEBUG_CONNECTIONS
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
571 if ((msg->size-msg->pos) <= max)
573 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
574 bytes_read = msg->size-msg->pos;
575 msg->pos+=(msg->size-msg->pos);
579 memcpy(buf,&msg->buf[msg->pos],max);
584 if (msg->pos==msg->size)
586 if (NULL!=msg->transmit_cont)
587 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
588 res = remove_http_message(ps,msg);
595 * Process GET or PUT request received via MHD. For
596 * GET, queue response that will send back our pending
597 * messages. For PUT, process incoming data and send
598 * to GNUnet core. In either case, check if a session
599 * already exists and create a new one if not.
602 mdh_access_cb (void *cls,
603 struct MHD_Connection *mhd_connection,
607 const char *upload_data,
608 size_t * upload_data_size, void **httpSessionCache)
610 struct Plugin *plugin = cls;
611 struct MHD_Response *response;
612 const union MHD_ConnectionInfo * conn_info;
614 struct sockaddr_in *addrin;
615 struct sockaddr_in6 *addrin6;
617 char address[INET6_ADDRSTRLEN+14];
618 struct GNUNET_PeerIdentity pi_in;
621 struct IPv4HttpAddress ipv4addr;
622 struct IPv6HttpAddress ipv6addr;
624 struct HTTP_PeerContext *pc;
626 struct Session *ps_tmp;
629 int send_error_to_client;
633 GNUNET_assert(cls !=NULL);
634 send_error_to_client = GNUNET_NO;
636 if (NULL == *httpSessionCache)
638 /* check url for peer identity , if invalid send HTTP 404*/
639 size_t len = strlen(&url[1]);
640 char * peer = GNUNET_malloc(104+1);
642 if ((len>104) && (url[104]==';'))
644 char * id = GNUNET_malloc((len-104)+1);
645 strcpy(id,&url[105]);
646 memcpy(peer,&url[1],103);
648 id_num = strtoul ( id, NULL , 10);
651 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
653 if ( GNUNET_SYSERR == res )
655 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
656 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
657 MHD_destroy_response (response);
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
667 ps = *httpSessionCache;
668 pc = ps->peercontext;
671 if (NULL == *httpSessionCache)
673 /* get peer context */
674 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
678 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
680 pc->session_id_counter=1;
681 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
682 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
685 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
686 /* Incoming IPv4 connection */
687 if ( AF_INET == conn_info->client_addr->sin_family)
689 addrin = conn_info->client_addr;
690 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
691 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
692 ipv4addr.u_port = addrin->sin_port;
694 addr_len = sizeof(struct IPv4HttpAddress);
696 /* Incoming IPv6 connection */
697 if ( AF_INET6 == conn_info->client_addr->sin_family)
699 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
700 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
701 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
702 ipv6addr.u6_port = addrin6->sin6_port;
704 addr_len = sizeof(struct IPv6HttpAddress);
708 //ps = get_Session(plugin, pc, addr, addr_len);
710 /* only inbound sessions here */
715 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
717 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
726 ps = GNUNET_malloc(sizeof (struct Session));
727 ps->addr = GNUNET_malloc(addr_len);
728 memcpy(ps->addr,addr,addr_len);
729 ps->addrlen = addr_len;
730 ps->direction=INBOUND;
731 ps->pending_msgs_head = NULL;
732 ps->pending_msgs_tail = NULL;
733 ps->send_connected=GNUNET_NO;
734 ps->send_active=GNUNET_NO;
735 ps->recv_connected=GNUNET_NO;
736 ps->recv_active=GNUNET_NO;
738 ps->session_id =id_num;
739 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
740 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
743 *httpSessionCache = ps;
744 if (ps->msgtok==NULL)
745 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
750 GNUNET_i2s(&pc->identity),
751 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
754 /* Is it a PUT or a GET request */
755 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
757 if (ps->recv_force_disconnect)
759 #if DEBUG_CONNECTIONS
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
762 ps->recv_active = GNUNET_NO;
765 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
767 ps->recv_endpoint = mhd_connection;
768 ps->recv_connected = GNUNET_YES;
769 ps->recv_active = GNUNET_YES;
770 #if DEBUG_CONNECTIONS
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
776 /* Transmission of all data complete */
777 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
779 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
780 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
781 #if DEBUG_CONNECTIONS
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
784 MHD_destroy_response (response);
785 ps->recv_active=GNUNET_NO;
790 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
792 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
793 (*upload_data_size) = 0;
799 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
801 if (ps->send_force_disconnect)
803 #if DEBUG_CONNECTIONS
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
806 ps->send_active = GNUNET_NO;
809 ps->send_connected = GNUNET_YES;
810 ps->send_active = GNUNET_YES;
811 ps->send_endpoint = mhd_connection;
812 #if DEBUG_CONNECTIONS
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
815 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
816 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
817 MHD_destroy_response (response);
825 * Call MHD to process pending ipv4 requests and then go back
826 * and schedule the next run.
828 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
830 * Call MHD to process pending ipv6 requests and then go back
831 * and schedule the next run.
833 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
836 * Function that queries MHD's select sets and
837 * starts the task waiting for them.
839 static GNUNET_SCHEDULER_TaskIdentifier
840 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
842 struct Plugin *plugin = cls;
843 GNUNET_SCHEDULER_TaskIdentifier ret;
847 struct GNUNET_NETWORK_FDSet *wrs;
848 struct GNUNET_NETWORK_FDSet *wws;
849 struct GNUNET_NETWORK_FDSet *wes;
851 unsigned long long timeout;
853 struct GNUNET_TIME_Relative tv;
855 GNUNET_assert(cls !=NULL);
856 ret = GNUNET_SCHEDULER_NO_TASK;
860 wrs = GNUNET_NETWORK_fdset_create ();
861 wes = GNUNET_NETWORK_fdset_create ();
862 wws = GNUNET_NETWORK_fdset_create ();
864 GNUNET_assert (MHD_YES ==
865 MHD_get_fdset (daemon_handle,
870 haveto = MHD_get_timeout (daemon_handle, &timeout);
871 if (haveto == MHD_YES)
872 tv.value = (uint64_t) timeout;
874 tv = GNUNET_TIME_UNIT_FOREVER_REL;
875 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
876 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
877 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
878 if (daemon_handle == plugin->http_server_daemon_v4)
880 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
881 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
882 GNUNET_SCHEDULER_NO_TASK,
886 &http_server_daemon_v4_run,
889 if (daemon_handle == plugin->http_server_daemon_v6)
891 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
892 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
893 GNUNET_SCHEDULER_NO_TASK,
897 &http_server_daemon_v6_run,
900 GNUNET_NETWORK_fdset_destroy (wrs);
901 GNUNET_NETWORK_fdset_destroy (wws);
902 GNUNET_NETWORK_fdset_destroy (wes);
907 * Call MHD to process pending requests and then go back
908 * and schedule the next run.
910 static void http_server_daemon_v4_run (void *cls,
911 const struct GNUNET_SCHEDULER_TaskContext *tc)
913 struct Plugin *plugin = cls;
915 GNUNET_assert(cls !=NULL);
916 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
917 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
919 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
922 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
923 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
929 * Call MHD to process pending requests and then go back
930 * and schedule the next run.
932 static void http_server_daemon_v6_run (void *cls,
933 const struct GNUNET_SCHEDULER_TaskContext *tc)
935 struct Plugin *plugin = cls;
937 GNUNET_assert(cls !=NULL);
938 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
939 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
941 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
944 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
945 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
950 * Function setting up curl handle and selecting message to send
952 * @param ses session to send data to
953 * @param con connection
954 * @return bytes sent to peer
956 static ssize_t send_check_connections (void *cls, struct Session *ps);
958 static size_t curl_get_header_function( void *ptr, size_t size, size_t nmemb, void *stream)
960 struct Session * ps = stream;
963 size_t len = size * nmemb;
964 long http_result = 0;
966 /* Getting last http result code */
967 if (ps->recv_connected==GNUNET_NO)
969 GNUNET_assert(NULL!=ps);
970 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
973 if (http_result == 200)
975 ps->recv_connected = GNUNET_YES;
976 ps->recv_active = GNUNET_YES;
977 #if DEBUG_CONNECTIONS
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
980 // Calling send_check_connections again since receive is established
981 send_check_connections (ps->peercontext->plugin, ps);
987 if ((size * nmemb) < SIZE_MAX)
988 tmp = GNUNET_malloc (len+1);
990 if ((tmp != NULL) && (len > 0))
995 if (tmp[len-2] == 13)
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s' %u \n",tmp, http_result);
1005 return size * nmemb;
1008 static size_t curl_put_header_function( void *ptr, size_t size, size_t nmemb, void *stream)
1010 struct Session * ps = stream;
1013 size_t len = size * nmemb;
1014 long http_result = 0;
1017 /* Getting last http result code */
1018 GNUNET_assert(NULL!=ps);
1019 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1020 if (CURLE_OK == res)
1022 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1024 ps->send_connected = GNUNET_YES;
1025 ps->send_active = GNUNET_YES;
1026 #if DEBUG_CONNECTIONS
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1030 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1032 ps->send_connected = GNUNET_NO;
1033 ps->send_active = GNUNET_NO;
1034 #if DEBUG_CONNECTIONS
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1041 if ((size * nmemb) < SIZE_MAX)
1042 tmp = GNUNET_malloc (len+1);
1044 if ((tmp != NULL) && (len > 0))
1046 memcpy(tmp,ptr,len);
1049 if (tmp[len-2] == 13)
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s' %u \n",tmp, http_result);
1059 return size * nmemb;
1063 * Callback method used with libcurl
1064 * Method is called when libcurl needs to read data during sending
1065 * @param stream pointer where to write data
1066 * @param size size of an individual element
1067 * @param nmemb count of elements that can be written to the buffer
1068 * @param ptr source pointer, passed to the libcurl handle
1069 * @return bytes written to stream
1071 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1073 struct Session * ps = ptr;
1074 struct HTTP_Message * msg = ps->pending_msgs_tail;
1078 if (ps->pending_msgs_tail == NULL)
1080 #if DEBUG_CONNECTIONS
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1083 ps->send_active = GNUNET_NO;
1084 return CURL_READFUNC_PAUSE;
1087 msg = ps->pending_msgs_tail;
1089 if (msg->pos < msg->size)
1091 /* data fit in buffer */
1092 if ((msg->size - msg->pos) <= (size * nmemb))
1094 len = (msg->size - msg->pos);
1095 memcpy(stream, &msg->buf[msg->pos], len);
1102 memcpy(stream, &msg->buf[msg->pos], len);
1107 /* no data to send */
1113 if ( msg->pos == msg->size)
1115 #if DEBUG_CONNECTIONS
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1118 /* Calling transmit continuation */
1119 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
1120 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1121 remove_http_message(ps, msg);
1127 * Callback method used with libcurl
1128 * Method is called when libcurl needs to write data during sending
1129 * @param stream pointer where to write data
1130 * @param size size of an individual element
1131 * @param nmemb count of elements that can be written to the buffer
1132 * @param ptr destination pointer, passed to the libcurl handle
1133 * @return bytes read from stream
1135 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1137 struct Session * ps = ptr;
1138 #if DEBUG_CONNECTIONS
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1141 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1142 return (size * nmemb);
1147 * Function setting up file descriptors and scheduling task to run
1148 * @param cls closure
1149 * @param ses session to send data to
1152 static int curl_schedule(void *cls, struct Session* ses );
1157 * Function setting up curl handle and selecting message to send
1159 * @param ses session to send data to
1160 * @param con connection
1161 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1163 static ssize_t send_check_connections (void *cls, struct Session *ps)
1165 struct Plugin *plugin = cls;
1167 struct HTTP_Message * msg;
1168 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1170 GNUNET_assert(cls !=NULL);
1172 if (ps->direction == OUTBOUND)
1174 /* RECV DIRECTION */
1175 /* Check if session is connected to receive data, otherwise connect to peer */
1176 if (ps->recv_connected == GNUNET_NO)
1178 if (ps->recv_endpoint == NULL)
1180 ps->recv_endpoint = curl_easy_init();
1182 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1184 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1185 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_function);
1186 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1187 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1188 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1189 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1190 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1191 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1192 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1193 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1194 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1196 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1197 if (mret != CURLM_OK)
1199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1200 _("%s failed at %s:%d: `%s'\n"),
1201 "curl_multi_add_handle", __FILE__, __LINE__,
1202 curl_multi_strerror (mret));
1203 return GNUNET_SYSERR;
1205 if (curl_schedule (plugin, NULL) == GNUNET_SYSERR)
1206 return GNUNET_SYSERR;
1207 #if DEBUG_CONNECTIONS
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1213 /* waiting for receive direction */
1214 if (ps->recv_connected==GNUNET_NO)
1217 /* SEND DIRECTION */
1218 /* Check if session is connected to send data, otherwise connect to peer */
1219 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1221 if (ps->send_active == GNUNET_YES)
1223 #if DEBUG_CONNECTIONS
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1228 if (ps->send_active == GNUNET_NO)
1230 #if DEBUG_CONNECTIONS
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1233 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1235 ps->send_active=GNUNET_YES;
1239 return GNUNET_SYSERR;
1242 /* not connected, initiate connection */
1243 if ((ps->send_connected==GNUNET_NO) && (NULL == ps->send_endpoint))
1244 ps->send_endpoint = curl_easy_init();
1245 GNUNET_assert (ps->send_endpoint != NULL);
1246 GNUNET_assert (NULL != ps->pending_msgs_tail);
1247 #if DEBUG_CONNECTIONS
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1250 ps->send_active = GNUNET_NO;
1251 msg = ps->pending_msgs_tail;
1254 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1256 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1257 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1258 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_function);
1259 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1260 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1261 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1262 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1263 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1264 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1265 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1266 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1267 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1269 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1270 if (mret != CURLM_OK)
1272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1273 _("%s failed at %s:%d: `%s'\n"),
1274 "curl_multi_add_handle", __FILE__, __LINE__,
1275 curl_multi_strerror (mret));
1276 return GNUNET_SYSERR;
1278 if (curl_schedule (plugin, NULL) == GNUNET_SYSERR)
1279 return GNUNET_SYSERR;
1282 if (ps->direction == INBOUND)
1284 GNUNET_assert (NULL != ps->pending_msgs_tail);
1285 msg = ps->pending_msgs_tail;
1286 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES))
1289 return GNUNET_SYSERR;
1292 static void curl_perform (void *cls,
1293 const struct GNUNET_SCHEDULER_TaskContext *tc)
1295 struct Plugin *plugin = cls;
1296 static unsigned int handles_last_run;
1298 struct CURLMsg *msg;
1300 struct Session *ps = NULL;
1301 struct HTTP_PeerContext *pc = NULL;
1302 struct HTTP_Message * cur_msg = NULL;
1305 GNUNET_assert(cls !=NULL);
1307 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1308 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1314 mret = curl_multi_perform (plugin->multi_handle, &running);
1315 if (running < handles_last_run)
1320 msg = curl_multi_info_read (plugin->multi_handle, &running);
1323 /* get session for affected curl handle */
1324 GNUNET_assert ( msg->easy_handle != NULL );
1325 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char *) &ps);
1326 GNUNET_assert ( ps != NULL );
1327 pc = ps->peercontext;
1328 GNUNET_assert ( pc != NULL );
1333 if ( (msg->data.result != CURLE_OK) &&
1334 (msg->data.result != CURLE_GOT_NOTHING) )
1336 /* sending msg failed*/
1337 if (msg->easy_handle == ps->send_endpoint)
1339 #if DEBUG_CONNECTIONS
1340 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1341 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1343 GNUNET_i2s(&pc->identity),
1344 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1345 "curl_multi_perform",
1346 curl_easy_strerror (msg->data.result));
1348 ps->send_connected = GNUNET_NO;
1349 ps->send_active = GNUNET_NO;
1350 curl_multi_remove_handle(plugin->multi_handle,msg->easy_handle);
1351 curl_easy_cleanup(ps->send_endpoint);
1352 ps->send_endpoint=NULL;
1353 cur_msg = ps->pending_msgs_tail;
1354 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1355 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1357 /* GET connection failed */
1358 if (msg->easy_handle == ps->recv_endpoint)
1360 #if DEBUG_CONNECTIONS
1361 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1362 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1364 GNUNET_i2s(&pc->identity),
1365 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1366 "curl_multi_perform",
1367 curl_easy_strerror (msg->data.result));
1369 ps->recv_connected = GNUNET_NO;
1370 ps->recv_active = GNUNET_NO;
1371 curl_multi_remove_handle(plugin->multi_handle,msg->easy_handle);
1372 curl_easy_cleanup(ps->recv_endpoint);
1373 ps->recv_endpoint=NULL;
1378 if (msg->easy_handle == ps->send_endpoint)
1380 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1381 #if DEBUG_CONNECTIONS
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1385 GNUNET_i2s(&pc->identity),
1386 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1389 /* Calling transmit continuation */
1390 cur_msg = ps->pending_msgs_tail;
1391 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1393 /* HTTP 1xx : Last message before here was informational */
1394 if ((http_result >=100) && (http_result < 200))
1395 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1396 /* HTTP 2xx: successful operations */
1397 if ((http_result >=200) && (http_result < 300))
1398 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1399 /* HTTP 3xx..5xx: error */
1400 if ((http_result >=300) && (http_result < 600))
1401 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1403 ps->send_connected = GNUNET_NO;
1404 ps->send_active = GNUNET_NO;
1405 curl_multi_remove_handle(plugin->multi_handle,msg->easy_handle);
1406 curl_easy_cleanup(ps->send_endpoint);
1407 ps->send_endpoint =NULL;
1409 if (msg->easy_handle == ps->recv_endpoint)
1411 #if DEBUG_CONNECTIONS
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1413 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1415 GNUNET_i2s(&pc->identity),
1416 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1419 ps->recv_connected = GNUNET_NO;
1420 ps->recv_active = GNUNET_NO;
1421 curl_multi_remove_handle(plugin->multi_handle,msg->easy_handle);
1422 curl_easy_cleanup(ps->recv_endpoint);
1423 ps->recv_endpoint=NULL;
1426 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1427 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1434 while ( (running > 0) );
1436 handles_last_run = running;
1438 while (mret == CURLM_CALL_MULTI_PERFORM);
1439 curl_schedule(plugin, cls);
1444 * Function setting up file descriptors and scheduling task to run
1445 * @param ses session to send data to
1446 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1448 static int curl_schedule(void *cls, struct Session* ses )
1450 struct Plugin *plugin = cls;
1455 struct GNUNET_NETWORK_FDSet *grs;
1456 struct GNUNET_NETWORK_FDSet *gws;
1460 GNUNET_assert(cls !=NULL);
1465 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1466 if (mret != CURLM_OK)
1468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1469 _("%s failed at %s:%d: `%s'\n"),
1470 "curl_multi_fdset", __FILE__, __LINE__,
1471 curl_multi_strerror (mret));
1472 return GNUNET_SYSERR;
1474 mret = curl_multi_timeout (plugin->multi_handle, &to);
1475 if (mret != CURLM_OK)
1477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1478 _("%s failed at %s:%d: `%s'\n"),
1479 "curl_multi_timeout", __FILE__, __LINE__,
1480 curl_multi_strerror (mret));
1481 return GNUNET_SYSERR;
1484 grs = GNUNET_NETWORK_fdset_create ();
1485 gws = GNUNET_NETWORK_fdset_create ();
1486 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1487 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1488 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1489 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1490 GNUNET_SCHEDULER_NO_TASK,
1491 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1496 GNUNET_NETWORK_fdset_destroy (gws);
1497 GNUNET_NETWORK_fdset_destroy (grs);
1503 * Function that can be used by the transport service to transmit
1504 * a message using the plugin. Note that in the case of a
1505 * peer disconnecting, the continuation MUST be called
1506 * prior to the disconnect notification itself. This function
1507 * will be called with this peer's HELLO message to initiate
1508 * a fresh connection to another peer.
1510 * @param cls closure
1511 * @param target who should receive this message
1512 * @param msgbuf the message to transmit
1513 * @param msgbuf_size number of bytes in 'msgbuf'
1514 * @param priority how important is the message (most plugins will
1515 * ignore message priority and just FIFO)
1516 * @param timeout how long to wait at most for the transmission (does not
1517 * require plugins to discard the message after the timeout,
1518 * just advisory for the desired delay; most plugins will ignore
1520 * @param session which session must be used (or NULL for "any")
1521 * @param addr the address to use (can be NULL if the plugin
1522 * is "on its own" (i.e. re-use existing TCP connection))
1523 * @param addrlen length of the address in bytes
1524 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1525 * GNUNET_NO means the plugin may use any other address and
1526 * GNUNET_SYSERR means that only reliable existing
1527 * bi-directional connections should be used (regardless
1529 * @param cont continuation to call once the message has
1530 * been transmitted (or if the transport is ready
1531 * for the next transmission call; or if the
1532 * peer disconnected...); can be NULL
1533 * @param cont_cls closure for cont
1534 * @return number of bytes used (on the physical network, with overheads);
1535 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1536 * and does NOT mean that the message was not transmitted (DV)
1539 http_plugin_send (void *cls,
1540 const struct GNUNET_PeerIdentity *target,
1543 unsigned int priority,
1544 struct GNUNET_TIME_Relative to,
1545 struct Session *session,
1549 GNUNET_TRANSPORT_TransmitContinuation cont,
1552 struct Plugin *plugin = cls;
1553 struct HTTP_Message *msg;
1555 struct HTTP_PeerContext * pc;
1556 struct Session * ps = NULL;
1557 struct Session * ps_tmp = NULL;
1559 GNUNET_assert(cls !=NULL);
1561 char * force = GNUNET_malloc(40);
1562 if (force_address == GNUNET_YES)
1563 strcpy(force,"forced addr.");
1564 if (force_address == GNUNET_NO)
1565 strcpy(force,"any addr.");
1566 if (force_address == GNUNET_SYSERR)
1567 strcpy(force,"reliable bi-direc. address addr.");
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1572 http_plugin_address_to_string(NULL, addr, addrlen),
1576 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1580 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1581 pc->plugin = plugin;
1582 pc->session_id_counter=1;
1583 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1584 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1587 /* Search for existing session using the passed address */
1588 if ((addr!=NULL) && (addrlen != 0))
1590 ps = get_Session(plugin, pc, addr, addrlen);
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Found existing connection to peer %s with given address, using %X\n", GNUNET_i2s(target), ps);
1595 /* Search for existing session using the passed session */
1596 if ((ps==NULL) && (force_address != GNUNET_YES))
1599 while (ps_tmp!=NULL)
1601 if ((ps_tmp==session) && (ps_tmp->recv_force_disconnect==GNUNET_NO) && (ps_tmp->send_force_disconnect==GNUNET_NO) &&
1602 (ps_tmp->recv_connected==GNUNET_YES) && (ps_tmp->send_connected==GNUNET_YES))
1604 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);
1608 ps_tmp=ps_tmp->next;
1612 /* session not existing, address not forced -> looking for other session */
1613 if ((ps==NULL) && (force_address != GNUNET_YES))
1615 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));
1616 /* Choosing different session to peer when possible */
1617 struct Session * tmp = pc->head;
1620 if ((tmp->recv_connected) && (tmp->send_connected) && (tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection to peer %s, selected connection %X\n", GNUNET_i2s(target),ps);
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection to peer %s, no connection found\n", GNUNET_i2s(target));
1632 /* session not existing, but address forced -> creating new session */
1633 if ((ps==NULL) || ((ps==NULL) && (force_address == GNUNET_YES)))
1635 if ((addr!=NULL) && (addrlen!=0))
1637 if (force_address == GNUNET_YES)
1638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new connection to peer %s\n", GNUNET_i2s(target));
1639 if (force_address != GNUNET_YES)
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new connection to peer %s\n", GNUNET_i2s(target));
1642 ps = GNUNET_malloc(sizeof (struct Session));
1643 if ((addrlen!=0) && (addr!=NULL))
1645 ps->addr = GNUNET_malloc(addrlen);
1646 memcpy(ps->addr,addr,addrlen);
1647 ps->addrlen = addrlen;
1654 ps->direction=OUTBOUND;
1655 ps->recv_connected = GNUNET_NO;
1656 ps->recv_force_disconnect = GNUNET_NO;
1657 ps->send_connected = GNUNET_NO;
1658 ps->send_force_disconnect = GNUNET_NO;
1659 ps->pending_msgs_head = NULL;
1660 ps->pending_msgs_tail = NULL;
1662 ps->session_id = pc->session_id_counter;
1663 pc->session_id_counter++;
1664 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1665 if (ps->msgtok == NULL)
1666 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
1667 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1671 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));
1677 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1679 msg->size = msgbuf_size;
1681 msg->buf = (char *) &msg[1];
1682 msg->transmit_cont = cont;
1683 msg->transmit_cont_cls = cont_cls;
1684 memcpy (msg->buf,msgbuf, msgbuf_size);
1685 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1687 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
1690 return GNUNET_SYSERR;
1696 * Function that can be used to force the plugin to disconnect
1697 * from the given peer and cancel all previous transmissions
1698 * (and their continuationc).
1700 * @param cls closure
1701 * @param target peer from which to disconnect
1704 http_plugin_disconnect (void *cls,
1705 const struct GNUNET_PeerIdentity *target)
1708 struct Plugin *plugin = cls;
1709 struct HTTP_PeerContext *pc = NULL;
1710 struct Session *ps = NULL;
1711 //struct Session *tmp = NULL;
1713 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1721 if (ps->direction==OUTBOUND)
1723 if (ps->send_endpoint!=NULL)
1725 //curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1726 //curl_easy_cleanup(ps->send_endpoint);
1727 //ps->send_endpoint=NULL;
1728 ps->send_force_disconnect = GNUNET_YES;
1730 if (ps->recv_endpoint!=NULL)
1732 //curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1733 //curl_easy_cleanup(ps->recv_endpoint);
1734 //ps->recv_endpoint=NULL;
1735 ps->recv_force_disconnect = GNUNET_YES;
1739 if (ps->direction==INBOUND)
1741 ps->recv_force_disconnect = GNUNET_YES;
1742 ps->send_force_disconnect = GNUNET_YES;
1745 while (ps->pending_msgs_head!=NULL)
1747 remove_http_message(ps, ps->pending_msgs_head);
1749 ps->recv_active = GNUNET_NO;
1750 ps->send_active = GNUNET_NO;
1757 * Convert the transports address to a nice, human-readable
1760 * @param cls closure
1761 * @param type name of the transport that generated the address
1762 * @param addr one of the addresses of the host, NULL for the last address
1763 * the specific address format depends on the transport
1764 * @param addrlen length of the address
1765 * @param numeric should (IP) addresses be displayed in numeric form?
1766 * @param timeout after how long should we give up?
1767 * @param asc function to call on each string
1768 * @param asc_cls closure for asc
1771 http_plugin_address_pretty_printer (void *cls,
1776 struct GNUNET_TIME_Relative timeout,
1777 GNUNET_TRANSPORT_AddressStringCallback
1780 const struct IPv4HttpAddress *t4;
1781 const struct IPv6HttpAddress *t6;
1782 struct sockaddr_in a4;
1783 struct sockaddr_in6 a6;
1789 GNUNET_assert(cls !=NULL);
1790 if (addrlen == sizeof (struct IPv6HttpAddress))
1792 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1794 a6.sin6_addr = t6->ipv6_addr;
1795 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1796 port = ntohs(t6->u6_port);
1798 else if (addrlen == sizeof (struct IPv4HttpAddress))
1800 address = GNUNET_malloc (INET_ADDRSTRLEN);
1802 a4.sin_addr.s_addr = t4->ipv4_addr;
1803 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1804 port = ntohs(t4->u_port);
1808 /* invalid address */
1809 GNUNET_break_op (0);
1810 asc (asc_cls, NULL);
1813 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1814 GNUNET_free (address);
1815 GNUNET_assert(res != 0);
1823 * Another peer has suggested an address for this
1824 * peer and transport plugin. Check that this could be a valid
1825 * address. If so, consider adding it to the list
1828 * @param cls closure
1829 * @param addr pointer to the address
1830 * @param addrlen length of addr
1831 * @return GNUNET_OK if this is a plausible address for this peer
1835 http_plugin_address_suggested (void *cls,
1836 const void *addr, size_t addrlen)
1838 struct Plugin *plugin = cls;
1839 struct IPv4HttpAddress *v4;
1840 struct IPv6HttpAddress *v6;
1843 GNUNET_assert(cls !=NULL);
1844 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1845 (addrlen != sizeof (struct IPv6HttpAddress)))
1847 return GNUNET_SYSERR;
1849 if (addrlen == sizeof (struct IPv4HttpAddress))
1851 v4 = (struct IPv4HttpAddress *) addr;
1852 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1854 return GNUNET_SYSERR;
1856 port = ntohs (v4->u_port);
1857 if (port != plugin->port_inbound)
1859 return GNUNET_SYSERR;
1862 if (addrlen == sizeof (struct IPv6HttpAddress))
1864 v6 = (struct IPv6HttpAddress *) addr;
1865 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1867 return GNUNET_SYSERR;
1869 port = ntohs (v6->u6_port);
1870 if (port != plugin->port_inbound)
1872 return GNUNET_SYSERR;
1881 * Function called for a quick conversion of the binary address to
1882 * a numeric address. Note that the caller must not free the
1883 * address and that the next call to this function is allowed
1884 * to override the address again.
1886 * @param cls closure
1887 * @param addr binary address
1888 * @param addrlen length of the address
1889 * @return string representing the same address
1892 http_plugin_address_to_string (void *cls,
1896 const struct IPv4HttpAddress *t4;
1897 const struct IPv6HttpAddress *t6;
1898 struct sockaddr_in a4;
1899 struct sockaddr_in6 a6;
1905 if (addrlen == sizeof (struct IPv6HttpAddress))
1907 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1909 a6.sin6_addr = t6->ipv6_addr;
1910 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1911 port = ntohs(t6->u6_port);
1913 else if (addrlen == sizeof (struct IPv4HttpAddress))
1915 address = GNUNET_malloc (INET_ADDRSTRLEN);
1917 a4.sin_addr.s_addr = t4->ipv4_addr;
1918 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1919 port = ntohs(t4->u_port);
1923 /* invalid address */
1926 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
1927 GNUNET_free (address);
1928 GNUNET_assert(res != 0);
1933 * Add the IP of our network interface to the list of
1934 * our external IP addresses.
1936 * @param cls the 'struct Plugin*'
1937 * @param name name of the interface
1938 * @param isDefault do we think this may be our default interface
1939 * @param addr address of the interface
1940 * @param addrlen number of bytes in addr
1941 * @return GNUNET_OK to continue iterating
1944 process_interfaces (void *cls,
1947 const struct sockaddr *addr, socklen_t addrlen)
1949 struct Plugin *plugin = cls;
1950 struct IPv4HttpAddress * t4;
1951 struct IPv6HttpAddress * t6;
1954 GNUNET_assert(cls !=NULL);
1955 af = addr->sa_family;
1958 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
1959 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1961 /* skip loopback addresses */
1964 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1965 t4->u_port = htons (plugin->port_inbound);
1966 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
1969 else if (af == AF_INET6)
1971 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
1972 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1974 /* skip link local addresses */
1977 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1979 /* skip loopback addresses */
1982 memcpy (&t6->ipv6_addr,
1983 &((struct sockaddr_in6 *) addr)->sin6_addr,
1984 sizeof (struct in6_addr));
1985 t6->u6_port = htons (plugin->port_inbound);
1986 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
1991 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
1993 struct HTTP_PeerContext * pc = value;
1994 struct Session * ps = pc->head;
1995 struct Session * tmp = NULL;
1996 struct HTTP_Message * msg = NULL;
1997 struct HTTP_Message * msg_tmp = NULL;
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
2005 GNUNET_free_non_null (ps->addr);
2006 GNUNET_free(ps->url);
2007 if (ps->msgtok != NULL)
2008 GNUNET_SERVER_mst_destroy (ps->msgtok);
2010 msg = ps->pending_msgs_head;
2013 msg_tmp = msg->next;
2017 if (ps->direction==OUTBOUND)
2019 if (ps->send_endpoint!=NULL)
2020 curl_easy_cleanup(ps->send_endpoint);
2021 if (ps->recv_endpoint!=NULL)
2022 curl_easy_cleanup(ps->recv_endpoint);
2034 * Exit point from the plugin.
2037 libgnunet_plugin_transport_http_done (void *cls)
2039 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2040 struct Plugin *plugin = api->cls;
2043 GNUNET_assert(cls !=NULL);
2045 if (plugin->http_server_daemon_v4 != NULL)
2047 MHD_stop_daemon (plugin->http_server_daemon_v4);
2048 plugin->http_server_daemon_v4 = NULL;
2050 if (plugin->http_server_daemon_v6 != NULL)
2052 MHD_stop_daemon (plugin->http_server_daemon_v6);
2053 plugin->http_server_daemon_v6 = NULL;
2058 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2060 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2061 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2064 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2066 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2067 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2070 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2072 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2073 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2076 /* free all peer information */
2077 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2078 &remove_peer_context_Iterator,
2080 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2082 mret = curl_multi_cleanup(plugin->multi_handle);
2083 if ( CURLM_OK != mret)
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
2086 GNUNET_free (plugin);
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2094 * Entry point for the plugin.
2097 libgnunet_plugin_transport_http_init (void *cls)
2099 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2100 struct Plugin *plugin;
2101 struct GNUNET_TRANSPORT_PluginFunctions *api;
2102 struct GNUNET_TIME_Relative gn_timeout;
2103 long long unsigned int port;
2105 GNUNET_assert(cls !=NULL);
2106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
2108 plugin = GNUNET_malloc (sizeof (struct Plugin));
2110 plugin->peers = NULL;
2112 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2114 api->send = &http_plugin_send;
2115 api->disconnect = &http_plugin_disconnect;
2116 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2117 api->check_address = &http_plugin_address_suggested;
2118 api->address_to_string = &http_plugin_address_to_string;
2120 /* Hashing our identity to use it in URLs */
2121 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2123 /* Reading port number from config file */
2125 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2131 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2134 ("Require valid port number for transport plugin `%s' in configuration!\n"),
2136 libgnunet_plugin_transport_http_done (api);
2139 GNUNET_assert ((port > 0) && (port <= 65535));
2140 plugin->port_inbound = port;
2141 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2142 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->http_server_daemon_v6 == NULL) && (port != 0))
2144 plugin->http_server_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
2147 plugin , &mdh_access_cb, plugin,
2148 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
2149 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
2150 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
2151 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2152 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2154 plugin->http_server_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
2157 plugin , &mdh_access_cb, plugin,
2158 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
2159 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
2160 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
2161 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2162 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2165 if (plugin->http_server_daemon_v4 != NULL)
2166 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2167 if (plugin->http_server_daemon_v6 != NULL)
2168 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2170 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
2172 else if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
2177 libgnunet_plugin_transport_http_done (api);
2181 /* Initializing cURL */
2182 curl_global_init(CURL_GLOBAL_ALL);
2183 plugin->multi_handle = curl_multi_init();
2185 if ( NULL == plugin->multi_handle )
2187 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2189 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2191 libgnunet_plugin_transport_http_done (api);
2195 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2196 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2201 /* end of plugin_transport_http.c */