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_YES
44 #define DEBUG_HTTP GNUNET_NO
47 * Text of the response sent back after the last bytes of a PUT
48 * request have been received (just to formally obey the HTTP
51 #define HTTP_PUT_RESPONSE "Thank you!"
54 * After how long do we expire an address that we
55 * learned from another peer if it is not reconfirmed
58 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
61 * Page returned if request invalid
63 #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>"
66 * Timeout for a http connect
68 #define HTTP_CONNECT_TIMEOUT 30
71 * Network format for IPv4 addresses.
73 struct IPv4HttpAddress
76 * IPv4 address, in network byte order.
78 uint32_t ipv4_addr GNUNET_PACKED;
81 * Port number, in network byte order.
83 uint16_t u_port GNUNET_PACKED;
89 * Network format for IPv6 addresses.
91 struct IPv6HttpAddress
96 struct in6_addr ipv6_addr GNUNET_PACKED;
99 * Port number, in network byte order.
101 uint16_t u6_port GNUNET_PACKED;
107 * Message to send using http
112 * next pointer for double linked list
114 struct HTTP_Message * next;
117 * previous pointer for double linked list
119 struct HTTP_Message * prev;
122 * buffer containing data to send
127 * amount of data already sent
137 * Continuation function to call once the transmission buffer
138 * has again space available. NULL if there is no
139 * continuation to call.
141 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
144 * Closure for transmit_cont.
146 void *transmit_cont_cls;
150 struct HTTP_PeerContext
152 struct GNUNET_PeerIdentity identity;
154 struct HTTP_Session * head;
155 struct HTTP_Session * tail;
161 struct HTTP_Session * next;
162 struct HTTP_Session * prev;
173 * Message queue for outbound messages
176 struct HTTP_Message * pending_msgs_head;
179 * Message queue for outbound messages
182 struct HTTP_Message * pending_msgs_tail;
185 * partner peer this connection belongs to
187 struct HTTP_PeerContext * peercontext;
190 * message stream tokenizer for incoming data
192 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
196 * outbound: GNUNET_YES
197 * inbound : GNUNET_NO
199 unsigned int direction;
202 * entity managing sending data
204 void * send_connection_endpoint;
207 * entity managing recieving data
209 void * recieve_connection_endpoint;
213 struct HTTP_Connection
215 struct HTTP_Connection * next;
216 struct HTTP_Connection * prev;
222 * Message queue for outbound messages
225 struct HTTP_Message * pending_msgs_head;
228 * Message queue for outbound messages
231 struct HTTP_Message * pending_msgs_tail;
239 * status of PUT connection
242 unsigned int put_connected;
245 * status of PUT connection
246 * if no data to send, connection is paused
248 unsigned int put_send_paused;
250 unsigned int get_connected;
253 * curl handle for sending data using HTTP/PUT
256 CURL *put_curl_handle;
259 * curl handle for recieving data using HTTP/GET transmission
262 CURL *get_curl_handle;
264 struct Session * session;
266 struct GNUNET_SERVER_MessageStreamTokenizer * get_msgtok;
269 struct HTTP_Connection_in
271 struct HTTP_Connection_in * next;
273 struct HTTP_Connection_in * prev;
278 unsigned int connected;
279 unsigned int send_paused;
281 struct GNUNET_SERVER_MessageStreamTokenizer * msgtok;
283 struct Session * session;
286 * Is there a HTTP/PUT in progress?
288 int is_put_in_progress;
291 * Is the http request invalid?
297 * Session handle for connections.
305 struct SessionHeader header;
308 * Stored in a linked list.
310 struct Session *next;
313 * Pointer to the global plugin struct.
315 struct Plugin *plugin;
318 * To whom are we talking to (set to our identity
319 * if we are still waiting for the welcome message)
321 struct GNUNET_PeerIdentity identity;
324 * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
329 * At what time did we reset last_received last?
331 struct GNUNET_TIME_Absolute last_quota_update;
334 * How many bytes have we received since the "last_quota_update"
337 uint64_t last_received;
340 * Number of bytes per ms that this peer is allowed
348 struct GNUNET_CRYPTO_HashAsciiEncoded hash;
350 struct HTTP_Connection *outbound_connections_head;
351 struct HTTP_Connection *outbound_connections_tail;
353 struct HTTP_Connection_in *inbound_connections_head;
354 struct HTTP_Connection_in *inbound_connections_tail;
358 * Encapsulation of all of the state of the plugin.
365 struct GNUNET_TRANSPORT_PluginEnvironment *env;
367 unsigned int port_inbound;
370 * Hashmap for all existing sessions.
372 struct GNUNET_CONTAINER_MultiHashMap *sessions;
374 struct GNUNET_CONTAINER_MultiHashMap *peers;
377 * Daemon for listening for new IPv4 connections.
379 struct MHD_Daemon *http_server_daemon_v4;
382 * Daemon for listening for new IPv6connections.
384 struct MHD_Daemon *http_server_daemon_v6;
387 * Our primary task for http daemon handling IPv4 connections
389 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
392 * Our primary task for http daemon handling IPv6 connections
394 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
397 * The task sending data
399 GNUNET_SCHEDULER_TaskIdentifier http_server_task_send;
404 CURLM * multi_handle;
407 * Our ASCII encoded, hashed peer identity
408 * This string is used to distinguish between connections and is added to the urls
410 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
415 * Function called for a quick conversion of the binary address to
416 * a numeric address. Note that the caller must not free the
417 * address and that the next call to this function is allowed
418 * to override the address again.
421 * @param addr binary address
422 * @param addrlen length of the address
423 * @return string representing the same address
426 http_plugin_address_to_string (void *cls,
431 * Create a new session
432 * @param cls plugin as closure
433 * @param addr_in address the peer is using inbound
434 * @param addr_len_in address length
435 * @param addr_out address the peer is using outbound
436 * @param addr_len_out address length
437 * @param peer identity
438 * @return created session object
440 static struct Session *
441 create_session (void * cls,
446 const struct GNUNET_PeerIdentity *peer)
448 struct Plugin *plugin = cls;
449 struct Session * cs = GNUNET_malloc ( sizeof( struct Session) );
451 GNUNET_assert(cls !=NULL);
453 memcpy(&cs->identity, peer, sizeof (struct GNUNET_PeerIdentity));
454 GNUNET_CRYPTO_hash_to_enc(&cs->identity.hashPubKey,&(cs->hash));
455 cs->outbound_connections_head = NULL;
456 cs->outbound_connections_tail = NULL;
461 * Check if session for this peer is already existing, otherwise create it
462 * @param cls the plugin used
463 * @param p peer to get session for
464 * @return session found or created
466 static struct Session * session_get (void * cls, const struct GNUNET_PeerIdentity *p)
468 struct Plugin *plugin = cls;
472 cs = GNUNET_CONTAINER_multihashmap_get (plugin->sessions, &p->hashPubKey);
475 cs = create_session(plugin, NULL, 0, NULL, 0, p);
476 res = GNUNET_CONTAINER_multihashmap_put ( plugin->sessions,
477 &cs->identity.hashPubKey,
479 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
480 if (res == GNUNET_OK)
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "New Session `%s' inserted\n", GNUNET_i2s(p));
487 static char * create_url(void * cls, const void * addr, size_t addrlen)
489 struct Plugin *plugin = cls;
492 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
493 GNUNET_asprintf(&url,
495 http_plugin_address_to_string(NULL, addr, addrlen),
496 (char *) (&plugin->my_ascii_hash_ident));
502 * Removes a message from the linked list of messages
503 * @param con connection to remove message from
504 * @param msg message to remove
505 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
508 static int remove_http_message(struct HTTP_Connection * con, struct HTTP_Message * msg)
510 GNUNET_CONTAINER_DLL_remove(con->pending_msgs_head,con->pending_msgs_tail,msg);
516 * Check if session already knows this address for a outbound connection to this peer
517 * If address not in session, add it to the session
518 * @param cls the plugin used
519 * @param cs the session
520 * @param addr address
521 * @param addr_len address length
522 * @return the found or created address
524 static struct HTTP_Connection * session_check_outbound_address (void * cls, struct Session *cs, const void * addr, size_t addr_len)
526 struct Plugin *plugin = cls;
527 struct HTTP_Connection * cc = cs->outbound_connections_head;
528 struct HTTP_Connection * con = NULL;
530 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
534 if (addr_len == cc->addrlen)
536 if (0 == memcmp(cc->addr, addr, addr_len))
547 con = GNUNET_malloc(sizeof(struct HTTP_Connection) + addr_len);
548 con->addrlen = addr_len;
550 con->url=create_url(plugin, addr, addr_len);
551 con->put_curl_handle = NULL;
552 con->put_connected = GNUNET_NO;
553 con->get_curl_handle = NULL;
554 con->get_connected = GNUNET_NO;
556 memcpy(con->addr, addr, addr_len);
557 GNUNET_CONTAINER_DLL_insert(cs->outbound_connections_head,cs->outbound_connections_tail,con);
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Created new connection %X to peer `%s'\n",con,GNUNET_i2s(&cs->identity));
563 static struct HTTP_Session * get_HTTP_Session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addr_len)
565 struct HTTP_Session * cc = pc->head;
566 struct HTTP_Session * con = NULL;
567 unsigned int count = 0;
569 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
572 if (addr_len == cc->addrlen)
574 if (0 == memcmp(cc->addr, addr, addr_len))
588 * Check if session already knows this address for a inbound connection to this peer
589 * If address not in session, add it to the session
590 * @param cls not needed, can be NULL
591 * @param cs the session
592 * @param addr address
593 * @param addr_len address length
594 * @return the found or created address
596 static struct HTTP_Connection_in * session_check_inbound_address (void * cls, struct Session *cs, const void * addr, size_t addr_len)
598 struct HTTP_Connection_in * cc = cs->inbound_connections_head;
599 struct HTTP_Connection_in * con = NULL;
601 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
605 if (addr_len == cc->addrlen)
607 if (0 == memcmp(cc->addr, addr, addr_len))
619 con = GNUNET_malloc(sizeof(struct HTTP_Connection_in) + addr_len);
620 con->addrlen = addr_len;
622 con->connected = GNUNET_NO;
624 memcpy(con->addr, addr, addr_len);
625 GNUNET_CONTAINER_DLL_insert(cs->inbound_connections_head,cs->inbound_connections_tail,con);
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X for inbound address %s (%s) was found\n",
630 GNUNET_i2s(&cs->identity),
631 http_plugin_address_to_string(NULL,con->addr,con->addrlen));
637 * Callback called by MHD when a connection is terminated
639 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
641 struct HTTP_Connection_in * con;
643 con = *httpSessionCache;
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&con->session->identity));
647 /* session set to inactive */
648 con->is_put_in_progress = GNUNET_NO;
649 con->is_bad_request = GNUNET_NO;
652 static void mhd_write_mst_cb (void *cls,
654 const struct GNUNET_MessageHeader *message)
656 struct HTTP_Connection_in * con = cls;
657 GNUNET_assert(con != NULL);
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
661 ntohs(message->type),
662 ntohs(message->size),
663 GNUNET_i2s(&(con->session->identity)),http_plugin_address_to_string(NULL,con->addr,con->addrlen));
665 con->session->plugin->env->receive (con->session->plugin->env->cls,
666 &con->session->identity,
667 message, 1, con->session,
672 static void curl_write_mst_cb (void *cls,
674 const struct GNUNET_MessageHeader *message)
676 struct HTTP_Connection * con = cls;
677 GNUNET_assert(con != NULL);
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
681 ntohs(message->type),
682 ntohs(message->size),
683 GNUNET_i2s(&(con->session->identity)),http_plugin_address_to_string(NULL,con->addr,con->addrlen));
685 con->session->plugin->env->receive (con->session->plugin->env->cls,
686 &con->session->identity,
687 message, 1, con->session,
694 * Check if ip is allowed to connect.
697 acceptPolicyCallback (void *cls,
698 const struct sockaddr *addr, socklen_t addr_len)
701 struct Plugin *plugin = cls;
703 /* Every connection is accepted, nothing more to do here */
707 int server_read_callback (void *cls, uint64_t pos, char *buf, int max)
711 struct HTTP_Connection * con = cls;
712 struct HTTP_Message * msg;
715 msg=con->pending_msgs_tail;
720 if ((msg->size-msg->pos) <= max)
722 memcpy(buf,&msg->buf[pos],(msg->size-msg->pos));
723 pos+=(msg->size-msg->pos);
727 memcpy(buf,&msg->buf[pos],max);
731 if (msg->pos==msg->size)
733 if (NULL != con->pending_msgs_tail->transmit_cont)
734 msg->transmit_cont (msg->transmit_cont_cls,&con->session->identity,GNUNET_SYSERR);
735 res = remove_http_message(con,msg);
744 * Process GET or PUT request received via MHD. For
745 * GET, queue response that will send back our pending
746 * messages. For PUT, process incoming data and send
747 * to GNUnet core. In either case, check if a session
748 * already exists and create a new one if not.
751 accessHandlerCallback (void *cls,
752 struct MHD_Connection *mhd_connection,
756 const char *upload_data,
757 size_t * upload_data_size, void **httpSessionCache)
759 struct Plugin *plugin = cls;
760 struct MHD_Response *response;
762 struct HTTP_Connection_in * con;
763 const union MHD_ConnectionInfo * conn_info;
764 struct sockaddr_in *addrin;
765 struct sockaddr_in6 *addrin6;
766 char address[INET6_ADDRSTRLEN+14];
767 struct GNUNET_PeerIdentity pi_in;
769 int send_error_to_client;
770 struct IPv4HttpAddress ipv4addr;
771 struct IPv6HttpAddress ipv6addr;
772 struct HTTP_PeerContext *pc;
773 struct HTTP_Session *ps;
777 GNUNET_assert(cls !=NULL);
778 send_error_to_client = GNUNET_NO;
779 if (NULL == *httpSessionCache)
781 /* check url for peer identity , if invalid send HTTP 404*/
782 res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
783 if ( GNUNET_SYSERR == res )
785 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
786 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
787 MHD_destroy_response (response);
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
797 con = *httpSessionCache;
801 if (NULL == *httpSessionCache)
803 /* get session for peer identity */
804 cs = session_get (plugin ,&pi_in);
806 /* get peer context */
807 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"RECV: CREATING NEW PEER CONTEXT\n");
812 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
813 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
814 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"RECV: PEER CONTEXT FOUND\n");
821 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
822 /* Incoming IPv4 connection */
823 if ( AF_INET == conn_info->client_addr->sin_family)
825 addrin = conn_info->client_addr;
826 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
827 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
828 ipv4addr.u_port = addrin->sin_port;
830 addr_len = sizeof(struct IPv4HttpAddress);
831 con = session_check_inbound_address (plugin, cs, (const void *) &ipv4addr, sizeof (struct IPv4HttpAddress));
833 /* Incoming IPv6 connection */
834 if ( AF_INET6 == conn_info->client_addr->sin_family)
836 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
837 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
838 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
839 ipv6addr.u6_port = addrin6->sin6_port;
841 addr_len = sizeof(struct IPv6HttpAddress);
842 con = session_check_inbound_address (plugin, cs, &ipv6addr, sizeof (struct IPv6HttpAddress));
844 /* Set closure and update current session*/
847 ps = get_HTTP_Session(plugin, pc, addr, addr_len);
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"RECV: CREATING NEW SESSION %s\n",http_plugin_address_to_string(NULL, addr, addr_len));
851 ps = GNUNET_malloc(sizeof (struct HTTP_Session));
852 ps->addr = GNUNET_malloc(addr_len);
853 memcpy(ps->addr,addr,addr_len);
854 ps->addrlen = addr_len;
855 ps->direction=GNUNET_NO;
856 ps->pending_msgs_head = NULL;
857 ps->pending_msgs_tail = NULL;
858 ps->url = create_url (plugin, ps->addr, ps->addrlen);
859 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"RECV: SESSION CONTEXT FOUND\n");
866 *httpSessionCache = con;
867 if (con->msgtok==NULL)
868 con->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, con);
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
872 GNUNET_i2s(&cs->identity),
873 http_plugin_address_to_string(NULL, con->addr, con->addrlen));
876 /* Is it a PUT or a GET request */
877 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
879 if ((*upload_data_size == 0) && (con->is_put_in_progress==GNUNET_NO))
881 con->is_put_in_progress = GNUNET_YES;
885 /* Transmission of all data complete */
886 if ((*upload_data_size == 0) && (con->is_put_in_progress == GNUNET_YES))
888 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
889 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
891 MHD_destroy_response (response);
894 con->is_put_in_progress = GNUNET_NO;
895 con->is_bad_request = GNUNET_NO;
900 if ((*upload_data_size > 0) && (con->is_put_in_progress == GNUNET_YES))
902 res = GNUNET_SERVER_mst_receive(con->msgtok, con, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
903 (*upload_data_size) = 0;
909 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
911 response = MHD_create_response_from_callback(-1,32 * 1024, &server_read_callback, con, NULL);
912 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
913 MHD_destroy_response (response);
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
917 GNUNET_i2s(&cs->identity),
918 http_plugin_address_to_string(NULL, con->addr, con->addrlen));
929 * Call MHD to process pending ipv4 requests and then go back
930 * and schedule the next run.
932 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
934 * Call MHD to process pending ipv6 requests and then go back
935 * and schedule the next run.
937 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
940 * Function that queries MHD's select sets and
941 * starts the task waiting for them.
943 static GNUNET_SCHEDULER_TaskIdentifier
944 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
946 struct Plugin *plugin = cls;
947 GNUNET_SCHEDULER_TaskIdentifier ret;
951 struct GNUNET_NETWORK_FDSet *wrs;
952 struct GNUNET_NETWORK_FDSet *wws;
953 struct GNUNET_NETWORK_FDSet *wes;
955 unsigned long long timeout;
957 struct GNUNET_TIME_Relative tv;
959 GNUNET_assert(cls !=NULL);
960 ret = GNUNET_SCHEDULER_NO_TASK;
964 wrs = GNUNET_NETWORK_fdset_create ();
965 wes = GNUNET_NETWORK_fdset_create ();
966 wws = GNUNET_NETWORK_fdset_create ();
968 GNUNET_assert (MHD_YES ==
969 MHD_get_fdset (daemon_handle,
974 haveto = MHD_get_timeout (daemon_handle, &timeout);
975 if (haveto == MHD_YES)
976 tv.value = (uint64_t) timeout;
978 tv = GNUNET_TIME_UNIT_FOREVER_REL;
979 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
980 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
981 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
982 if (daemon_handle == plugin->http_server_daemon_v4)
984 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
985 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
986 GNUNET_SCHEDULER_NO_TASK,
990 &http_server_daemon_v4_run,
993 if (daemon_handle == plugin->http_server_daemon_v6)
995 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
996 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
997 GNUNET_SCHEDULER_NO_TASK,
1001 &http_server_daemon_v6_run,
1004 GNUNET_NETWORK_fdset_destroy (wrs);
1005 GNUNET_NETWORK_fdset_destroy (wws);
1006 GNUNET_NETWORK_fdset_destroy (wes);
1011 * Call MHD to process pending requests and then go back
1012 * and schedule the next run.
1014 static void http_server_daemon_v4_run (void *cls,
1015 const struct GNUNET_SCHEDULER_TaskContext *tc)
1017 struct Plugin *plugin = cls;
1019 GNUNET_assert(cls !=NULL);
1020 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1021 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1023 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1026 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1027 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1033 * Call MHD to process pending requests and then go back
1034 * and schedule the next run.
1036 static void http_server_daemon_v6_run (void *cls,
1037 const struct GNUNET_SCHEDULER_TaskContext *tc)
1039 struct Plugin *plugin = cls;
1041 GNUNET_assert(cls !=NULL);
1042 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1043 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1045 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1048 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1049 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1054 static size_t curl_header_function( void *ptr, size_t size, size_t nmemb, void *stream)
1056 struct HTTP_Connection * con = stream;
1059 size_t len = size * nmemb;
1060 long http_result = 0;
1063 /* Getting last http result code */
1064 if (con->get_connected==GNUNET_NO)
1066 GNUNET_assert(NULL!=con);
1067 res = curl_easy_getinfo(con->get_curl_handle, CURLINFO_RESPONSE_CODE, &http_result);
1068 if (CURLE_OK == res)
1070 if (http_result == 200)
1072 con->get_connected = GNUNET_YES;
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connected\n",con);
1079 if ((size * nmemb) < SIZE_MAX)
1080 tmp = GNUNET_malloc (len+1);
1082 if ((tmp != NULL) && (len > 0))
1084 memcpy(tmp,ptr,len);
1087 if (tmp[len-2] == 13)
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s' %u \n",tmp, http_result);
1097 return size * nmemb;
1101 * Callback method used with libcurl
1102 * Method is called when libcurl needs to read data during sending
1103 * @param stream pointer where to write data
1104 * @param size size of an individual element
1105 * @param nmemb count of elements that can be written to the buffer
1106 * @param ptr source pointer, passed to the libcurl handle
1107 * @return bytes written to stream
1109 static size_t send_curl_read_callback(void *stream, size_t size, size_t nmemb, void *ptr)
1111 struct HTTP_Connection * con = ptr;
1112 struct HTTP_Message * msg = con->pending_msgs_tail;
1116 if (con->pending_msgs_tail == NULL)
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",con);
1119 con->put_send_paused = GNUNET_YES;
1120 return CURL_READFUNC_PAUSE;
1123 msg = con->pending_msgs_tail;
1125 if (msg->pos < msg->size)
1127 /* data fit in buffer */
1128 if ((msg->size - msg->pos) <= (size * nmemb))
1130 len = (msg->size - msg->pos);
1131 memcpy(stream, &msg->buf[msg->pos], len);
1138 memcpy(stream, &msg->buf[msg->pos], len);
1143 /* no data to send */
1149 if ( msg->pos == msg->size)
1151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",con, msg->pos);
1152 /* Calling transmit continuation */
1153 if (( NULL != con->pending_msgs_tail) && (NULL != con->pending_msgs_tail->transmit_cont))
1154 msg->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&(con->session)->identity,GNUNET_OK);
1155 remove_http_message(con, msg);
1161 * Callback method used with libcurl
1162 * Method is called when libcurl needs to write data during sending
1163 * @param stream pointer where to write data
1164 * @param size size of an individual element
1165 * @param nmemb count of elements that can be written to the buffer
1166 * @param ptr destination pointer, passed to the libcurl handle
1167 * @return bytes read from stream
1169 static size_t send_curl_write_callback( void *stream, size_t size, size_t nmemb, void *ptr)
1171 struct HTTP_Connection * con = ptr;
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes recieved\n",con, size*nmemb);
1174 GNUNET_SERVER_mst_receive(con->get_msgtok, con, stream,size*nmemb, GNUNET_NO, GNUNET_NO);
1176 return (size * nmemb);
1181 * Function setting up file descriptors and scheduling task to run
1182 * @param cls closure
1183 * @param ses session to send data to
1184 * @return bytes sent to peer
1186 static size_t send_schedule(void *cls, struct Session* ses );
1191 * Function setting up curl handle and selecting message to send
1193 * @param ses session to send data to
1194 * @param con connection
1195 * @return bytes sent to peer
1197 static ssize_t send_check_connections (void *cls, struct Session* ses , struct HTTP_Connection *con)
1199 struct Plugin *plugin = cls;
1202 struct HTTP_Message * msg;
1203 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1205 GNUNET_assert(cls !=NULL);
1207 if (con->get_connected == GNUNET_NO)
1209 if (con->get_curl_handle == NULL)
1211 con->get_curl_handle = curl_easy_init();
1213 curl_easy_setopt(con->get_curl_handle, CURLOPT_VERBOSE, 1L);
1215 curl_easy_setopt(con->get_curl_handle, CURLOPT_URL, con->url);
1216 //curl_easy_setopt(con->put_curl_handle, CURLOPT_PUT, 1L);
1217 curl_easy_setopt(con->get_curl_handle, CURLOPT_HEADERFUNCTION, &curl_header_function);
1218 curl_easy_setopt(con->get_curl_handle, CURLOPT_WRITEHEADER, con);
1219 curl_easy_setopt(con->get_curl_handle, CURLOPT_READFUNCTION, send_curl_read_callback);
1220 curl_easy_setopt(con->get_curl_handle, CURLOPT_READDATA, con);
1221 curl_easy_setopt(con->get_curl_handle, CURLOPT_WRITEFUNCTION, send_curl_write_callback);
1222 curl_easy_setopt(con->get_curl_handle, CURLOPT_WRITEDATA, con);
1223 curl_easy_setopt(con->get_curl_handle, CURLOPT_TIMEOUT, (long) timeout.value);
1224 curl_easy_setopt(con->get_curl_handle, CURLOPT_PRIVATE, con);
1225 curl_easy_setopt(con->get_curl_handle, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1226 curl_easy_setopt(con->get_curl_handle, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1228 mret = curl_multi_add_handle(plugin->multi_handle, con->get_curl_handle);
1229 if (mret != CURLM_OK)
1231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1232 _("%s failed at %s:%d: `%s'\n"),
1233 "curl_multi_add_handle", __FILE__, __LINE__,
1234 curl_multi_strerror (mret));
1238 if (con->get_msgtok != NULL)
1239 con->get_msgtok = GNUNET_SERVER_mst_create (&curl_write_mst_cb, con);
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",con);
1245 /* PUT already connected, no need to initiate connection */
1246 if ((con->put_connected == GNUNET_YES) && (con->put_curl_handle != NULL))
1248 if (con->put_send_paused == GNUNET_NO)
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",con);
1253 if (con->put_send_paused == GNUNET_YES)
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",con);
1256 curl_easy_pause(con->put_curl_handle,CURLPAUSE_CONT);
1257 con->put_send_paused=GNUNET_NO;
1262 /* not connected, initiate connection */
1263 if ( NULL == con->put_curl_handle)
1264 con->put_curl_handle = curl_easy_init();
1265 GNUNET_assert (con->put_curl_handle != NULL);
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",con);
1268 GNUNET_assert (NULL != con->pending_msgs_tail);
1269 msg = con->pending_msgs_tail;
1272 curl_easy_setopt(con->put_curl_handle, CURLOPT_VERBOSE, 1L);
1274 curl_easy_setopt(con->put_curl_handle, CURLOPT_URL, con->url);
1275 curl_easy_setopt(con->put_curl_handle, CURLOPT_PUT, 1L);
1276 curl_easy_setopt(con->put_curl_handle, CURLOPT_READFUNCTION, send_curl_read_callback);
1277 curl_easy_setopt(con->put_curl_handle, CURLOPT_READDATA, con);
1278 curl_easy_setopt(con->put_curl_handle, CURLOPT_WRITEFUNCTION, send_curl_write_callback);
1279 curl_easy_setopt(con->put_curl_handle, CURLOPT_READDATA, con);
1280 curl_easy_setopt(con->put_curl_handle, CURLOPT_TIMEOUT, (long) timeout.value);
1281 curl_easy_setopt(con->put_curl_handle, CURLOPT_PRIVATE, con);
1282 curl_easy_setopt(con->put_curl_handle, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1283 curl_easy_setopt(con->put_curl_handle, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1285 mret = curl_multi_add_handle(plugin->multi_handle, con->put_curl_handle);
1286 if (mret != CURLM_OK)
1288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1289 _("%s failed at %s:%d: `%s'\n"),
1290 "curl_multi_add_handle", __FILE__, __LINE__,
1291 curl_multi_strerror (mret));
1294 con->put_connected = GNUNET_YES;
1295 bytes_sent = send_schedule (plugin, ses);
1299 static void send_execute (void *cls,
1300 const struct GNUNET_SCHEDULER_TaskContext *tc)
1302 struct Plugin *plugin = cls;
1303 static unsigned int handles_last_run;
1305 struct CURLMsg *msg;
1307 struct HTTP_Connection * con = NULL;
1308 struct Session * cs = NULL;
1311 GNUNET_assert(cls !=NULL);
1312 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
1313 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1319 mret = curl_multi_perform (plugin->multi_handle, &running);
1320 if (running < handles_last_run)
1325 msg = curl_multi_info_read (plugin->multi_handle, &running);
1328 /* get session for affected curl handle */
1329 GNUNET_assert ( msg->easy_handle != NULL );
1330 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char *) &con);
1331 GNUNET_assert ( con != NULL );
1333 GNUNET_assert ( cs != NULL );
1338 if ( (msg->data.result != CURLE_OK) &&
1339 (msg->data.result != CURLE_GOT_NOTHING) )
1341 /* sending msg failed*/
1342 if (msg->easy_handle == con->put_curl_handle)
1344 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1345 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1347 GNUNET_i2s(&cs->identity),
1348 http_plugin_address_to_string(NULL, con->addr, con->addrlen),
1349 "curl_multi_perform",
1350 curl_easy_strerror (msg->data.result));
1352 con->put_connected = GNUNET_NO;
1353 curl_easy_cleanup(con->put_curl_handle);
1354 con->put_curl_handle=NULL;
1355 if (( NULL != con->pending_msgs_tail) && ( NULL != con->pending_msgs_tail->transmit_cont))
1356 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&con->session->identity,GNUNET_SYSERR);
1358 /* GET connection failed */
1359 if (msg->easy_handle == con->get_curl_handle)
1361 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1362 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1364 GNUNET_i2s(&cs->identity),
1365 http_plugin_address_to_string(NULL, con->addr, con->addrlen),
1366 "curl_multi_perform",
1367 curl_easy_strerror (msg->data.result));
1368 con->get_connected = GNUNET_NO;
1369 curl_easy_cleanup(con->get_curl_handle);
1370 con->get_curl_handle=NULL;
1375 if (msg->easy_handle == con->put_curl_handle)
1377 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1379 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1381 GNUNET_i2s(&cs->identity),
1382 http_plugin_address_to_string(NULL, con->addr, con->addrlen),
1385 /* Calling transmit continuation */
1386 if (( NULL != con->pending_msgs_tail) && (NULL != con->pending_msgs_tail->transmit_cont))
1388 /* HTTP 1xx : Last message before here was informational */
1389 if ((http_result >=100) && (http_result < 200))
1390 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_OK);
1391 /* HTTP 2xx: successful operations */
1392 if ((http_result >=200) && (http_result < 300))
1393 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_OK);
1394 /* HTTP 3xx..5xx: error */
1395 if ((http_result >=300) && (http_result < 600))
1396 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_SYSERR);
1398 curl_easy_cleanup(con->put_curl_handle);
1399 con->put_connected = GNUNET_NO;
1400 con->put_curl_handle=NULL;
1402 if (msg->easy_handle == con->get_curl_handle)
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1407 GNUNET_i2s(&cs->identity),
1408 http_plugin_address_to_string(NULL, con->addr, con->addrlen),
1411 con->get_connected = GNUNET_NO;
1412 curl_easy_cleanup(con->get_curl_handle);
1413 con->get_curl_handle=NULL;
1416 if (con->pending_msgs_tail != NULL)
1418 if (con->pending_msgs_tail->pos>0)
1419 remove_http_message(con, con->pending_msgs_tail);
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message could not be removed from session `%s'\n", GNUNET_i2s(&cs->identity));
1428 while ( (running > 0) );
1430 handles_last_run = running;
1432 while (mret == CURLM_CALL_MULTI_PERFORM);
1433 send_schedule(plugin, cls);
1438 * Function setting up file descriptors and scheduling task to run
1439 * @param ses session to send data to
1440 * @return bytes sent to peer
1442 static size_t send_schedule(void *cls, struct Session* ses )
1444 struct Plugin *plugin = cls;
1449 struct GNUNET_NETWORK_FDSet *grs;
1450 struct GNUNET_NETWORK_FDSet *gws;
1454 GNUNET_assert(cls !=NULL);
1459 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1460 if (mret != CURLM_OK)
1462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1463 _("%s failed at %s:%d: `%s'\n"),
1464 "curl_multi_fdset", __FILE__, __LINE__,
1465 curl_multi_strerror (mret));
1468 mret = curl_multi_timeout (plugin->multi_handle, &to);
1469 if (mret != CURLM_OK)
1471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1472 _("%s failed at %s:%d: `%s'\n"),
1473 "curl_multi_timeout", __FILE__, __LINE__,
1474 curl_multi_strerror (mret));
1478 grs = GNUNET_NETWORK_fdset_create ();
1479 gws = GNUNET_NETWORK_fdset_create ();
1480 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1481 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1482 plugin->http_server_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1483 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1484 GNUNET_SCHEDULER_NO_TASK,
1485 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1490 GNUNET_NETWORK_fdset_destroy (gws);
1491 GNUNET_NETWORK_fdset_destroy (grs);
1493 /* FIXME: return bytes REALLY sent */
1499 * Function that can be used by the transport service to transmit
1500 * a message using the plugin. Note that in the case of a
1501 * peer disconnecting, the continuation MUST be called
1502 * prior to the disconnect notification itself. This function
1503 * will be called with this peer's HELLO message to initiate
1504 * a fresh connection to another peer.
1506 * @param cls closure
1507 * @param target who should receive this message
1508 * @param msgbuf the message to transmit
1509 * @param msgbuf_size number of bytes in 'msgbuf'
1510 * @param priority how important is the message (most plugins will
1511 * ignore message priority and just FIFO)
1512 * @param timeout how long to wait at most for the transmission (does not
1513 * require plugins to discard the message after the timeout,
1514 * just advisory for the desired delay; most plugins will ignore
1516 * @param session which session must be used (or NULL for "any")
1517 * @param addr the address to use (can be NULL if the plugin
1518 * is "on its own" (i.e. re-use existing TCP connection))
1519 * @param addrlen length of the address in bytes
1520 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1521 * GNUNET_NO means the plugin may use any other address and
1522 * GNUNET_SYSERR means that only reliable existing
1523 * bi-directional connections should be used (regardless
1525 * @param cont continuation to call once the message has
1526 * been transmitted (or if the transport is ready
1527 * for the next transmission call; or if the
1528 * peer disconnected...); can be NULL
1529 * @param cont_cls closure for cont
1530 * @return number of bytes used (on the physical network, with overheads);
1531 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1532 * and does NOT mean that the message was not transmitted (DV)
1535 http_plugin_send (void *cls,
1536 const struct GNUNET_PeerIdentity *target,
1539 unsigned int priority,
1540 struct GNUNET_TIME_Relative to,
1541 struct Session *session,
1545 GNUNET_TRANSPORT_TransmitContinuation cont,
1548 struct Plugin *plugin = cls;
1550 struct HTTP_Message *msg;
1551 struct HTTP_Connection *con;
1554 struct HTTP_PeerContext * pc;
1555 struct HTTP_Session * ps;
1557 GNUNET_assert(cls !=NULL);
1558 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
1560 /* get session from hashmap */
1561 cs = session_get(plugin, target);
1562 con = session_check_outbound_address(plugin, cs, addr, addrlen);
1565 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: CREATING NEW PEER CONTEXT\n");
1570 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1571 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1572 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: PEER CONTEXT FOUND\n");
1578 ps = get_HTTP_Session(plugin, pc, addr, addrlen);
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: CREATING NEW SESSION %s\n",http_plugin_address_to_string(NULL, addr, addrlen));
1582 ps = GNUNET_malloc(sizeof (struct HTTP_Session));
1583 ps->addr = GNUNET_malloc(addrlen);
1584 memcpy(ps->addr,addr,addrlen);
1585 ps->addrlen = addrlen;
1586 ps->direction=GNUNET_YES;
1587 ps->pending_msgs_head = NULL;
1588 ps->pending_msgs_tail = NULL;
1589 ps->url = create_url (plugin, ps->addr, ps->addrlen);
1590 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: SESSION CONTEXT FOUND\n");
1597 char * force = GNUNET_malloc(30);
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' %s (%s), session: %X\n",
1606 GNUNET_i2s(&cs->identity),
1608 http_plugin_address_to_string(NULL, addr, addrlen),
1611 //GNUNET_free(force);
1613 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1615 msg->size = msgbuf_size;
1617 msg->buf = (char *) &msg[1];
1618 msg->transmit_cont = cont;
1619 msg->transmit_cont_cls = cont_cls;
1620 memcpy (msg->buf,msgbuf, msgbuf_size);
1622 /* must use this address */
1623 if (force_address == GNUNET_YES)
1625 /* enqueue in connection message queue */
1626 GNUNET_CONTAINER_DLL_insert(con->pending_msgs_head,con->pending_msgs_tail,msg);
1627 //GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1629 /* can use existing connection to send */
1632 /* enqueue in connection message queue */
1633 GNUNET_CONTAINER_DLL_insert(con->pending_msgs_head,con->pending_msgs_tail,msg);
1634 //GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1636 return send_check_connections (plugin, cs, con);
1642 * Function that can be used to force the plugin to disconnect
1643 * from the given peer and cancel all previous transmissions
1644 * (and their continuationc).
1646 * @param cls closure
1647 * @param target peer from which to disconnect
1650 http_plugin_disconnect (void *cls,
1651 const struct GNUNET_PeerIdentity *target)
1653 struct Plugin *plugin = cls;
1654 struct HTTP_Connection *con;
1657 /* get session from hashmap */
1658 cs = session_get(plugin, target);
1659 con = cs->outbound_connections_head;
1663 if (con->put_curl_handle!=NULL)
1664 curl_easy_cleanup(con->put_curl_handle);
1665 con->put_curl_handle=NULL;
1666 con->put_connected = GNUNET_NO;
1667 while (con->pending_msgs_head!=NULL)
1669 remove_http_message(con, con->pending_msgs_head);
1677 * Convert the transports address to a nice, human-readable
1680 * @param cls closure
1681 * @param type name of the transport that generated the address
1682 * @param addr one of the addresses of the host, NULL for the last address
1683 * the specific address format depends on the transport
1684 * @param addrlen length of the address
1685 * @param numeric should (IP) addresses be displayed in numeric form?
1686 * @param timeout after how long should we give up?
1687 * @param asc function to call on each string
1688 * @param asc_cls closure for asc
1691 http_plugin_address_pretty_printer (void *cls,
1696 struct GNUNET_TIME_Relative timeout,
1697 GNUNET_TRANSPORT_AddressStringCallback
1700 const struct IPv4HttpAddress *t4;
1701 const struct IPv6HttpAddress *t6;
1702 struct sockaddr_in a4;
1703 struct sockaddr_in6 a6;
1709 GNUNET_assert(cls !=NULL);
1710 if (addrlen == sizeof (struct IPv6HttpAddress))
1712 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1714 a6.sin6_addr = t6->ipv6_addr;
1715 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1716 port = ntohs(t6->u6_port);
1718 else if (addrlen == sizeof (struct IPv4HttpAddress))
1720 address = GNUNET_malloc (INET_ADDRSTRLEN);
1722 a4.sin_addr.s_addr = t4->ipv4_addr;
1723 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1724 port = ntohs(t4->u_port);
1728 /* invalid address */
1729 GNUNET_break_op (0);
1730 asc (asc_cls, NULL);
1733 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1734 GNUNET_free (address);
1735 GNUNET_assert(res != 0);
1743 * Another peer has suggested an address for this
1744 * peer and transport plugin. Check that this could be a valid
1745 * address. If so, consider adding it to the list
1748 * @param cls closure
1749 * @param addr pointer to the address
1750 * @param addrlen length of addr
1751 * @return GNUNET_OK if this is a plausible address for this peer
1755 http_plugin_address_suggested (void *cls,
1756 const void *addr, size_t addrlen)
1758 struct Plugin *plugin = cls;
1759 struct IPv4HttpAddress *v4;
1760 struct IPv6HttpAddress *v6;
1763 GNUNET_assert(cls !=NULL);
1764 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1765 (addrlen != sizeof (struct IPv6HttpAddress)))
1767 return GNUNET_SYSERR;
1769 if (addrlen == sizeof (struct IPv4HttpAddress))
1771 v4 = (struct IPv4HttpAddress *) addr;
1772 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1774 return GNUNET_SYSERR;
1776 port = ntohs (v4->u_port);
1777 if (port != plugin->port_inbound)
1779 return GNUNET_SYSERR;
1784 v6 = (struct IPv6HttpAddress *) addr;
1785 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1787 return GNUNET_SYSERR;
1789 port = ntohs (v6->u6_port);
1790 if (port != plugin->port_inbound)
1792 return GNUNET_SYSERR;
1800 * Function called for a quick conversion of the binary address to
1801 * a numeric address. Note that the caller must not free the
1802 * address and that the next call to this function is allowed
1803 * to override the address again.
1805 * @param cls closure
1806 * @param addr binary address
1807 * @param addrlen length of the address
1808 * @return string representing the same address
1811 http_plugin_address_to_string (void *cls,
1815 const struct IPv4HttpAddress *t4;
1816 const struct IPv6HttpAddress *t6;
1817 struct sockaddr_in a4;
1818 struct sockaddr_in6 a6;
1824 if (addrlen == sizeof (struct IPv6HttpAddress))
1826 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1828 a6.sin6_addr = t6->ipv6_addr;
1829 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1830 port = ntohs(t6->u6_port);
1832 else if (addrlen == sizeof (struct IPv4HttpAddress))
1834 address = GNUNET_malloc (INET_ADDRSTRLEN);
1836 a4.sin_addr.s_addr = t4->ipv4_addr;
1837 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1838 port = ntohs(t4->u_port);
1842 /* invalid address */
1845 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
1846 GNUNET_free (address);
1847 GNUNET_assert(res != 0);
1852 * Add the IP of our network interface to the list of
1853 * our external IP addresses.
1855 * @param cls the 'struct Plugin*'
1856 * @param name name of the interface
1857 * @param isDefault do we think this may be our default interface
1858 * @param addr address of the interface
1859 * @param addrlen number of bytes in addr
1860 * @return GNUNET_OK to continue iterating
1863 process_interfaces (void *cls,
1866 const struct sockaddr *addr, socklen_t addrlen)
1868 struct Plugin *plugin = cls;
1869 struct IPv4HttpAddress * t4;
1870 struct IPv6HttpAddress * t6;
1873 GNUNET_assert(cls !=NULL);
1874 af = addr->sa_family;
1877 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
1878 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1880 /* skip loopback addresses */
1883 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1884 t4->u_port = htons (plugin->port_inbound);
1885 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
1888 else if (af == AF_INET6)
1890 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
1891 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1893 /* skip link local addresses */
1896 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1898 /* skip loopback addresses */
1901 memcpy (&t6->ipv6_addr,
1902 &((struct sockaddr_in6 *) addr)->sin6_addr,
1903 sizeof (struct in6_addr));
1904 t6->u6_port = htons (plugin->port_inbound);
1905 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
1909 int hashMapFreeIterator (void *cls, const GNUNET_HashCode *key, void *value)
1911 struct Session * cs = value;
1912 struct HTTP_Connection * con = cs->outbound_connections_head;
1913 struct HTTP_Connection * tmp_con = cs->outbound_connections_head;
1914 struct HTTP_Message * msg = NULL;
1915 struct HTTP_Message * tmp_msg = NULL;
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session for peer `%s'\n",GNUNET_i2s(&cs->identity));
1919 /* freeing connections */
1922 GNUNET_free(con->url);
1923 if (con->put_curl_handle!=NULL)
1924 curl_easy_cleanup(con->put_curl_handle);
1925 con->put_curl_handle = NULL;
1926 msg = con->pending_msgs_head;
1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"All sessions freed \n");
1944 * Exit point from the plugin.
1947 libgnunet_plugin_transport_http_done (void *cls)
1949 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1950 struct Plugin *plugin = api->cls;
1953 GNUNET_assert(cls !=NULL);
1956 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1958 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1959 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1962 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1964 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1965 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1968 if ( plugin->http_server_task_send != GNUNET_SCHEDULER_NO_TASK)
1970 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_send);
1971 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
1974 if (plugin->http_server_daemon_v4 != NULL)
1976 MHD_stop_daemon (plugin->http_server_daemon_v4);
1977 plugin->http_server_daemon_v4 = NULL;
1979 if (plugin->http_server_daemon_v6 != NULL)
1981 MHD_stop_daemon (plugin->http_server_daemon_v6);
1982 plugin->http_server_daemon_v6 = NULL;
1985 /* free all sessions */
1986 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1987 &hashMapFreeIterator,
1990 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1991 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
1993 mret = curl_multi_cleanup(plugin->multi_handle);
1994 if ( CURLM_OK != mret)
1995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
1997 GNUNET_free (plugin);
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2005 * Entry point for the plugin.
2008 libgnunet_plugin_transport_http_init (void *cls)
2010 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2011 struct Plugin *plugin;
2012 struct GNUNET_TRANSPORT_PluginFunctions *api;
2013 struct GNUNET_TIME_Relative gn_timeout;
2014 long long unsigned int port;
2016 GNUNET_assert(cls !=NULL);
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
2019 plugin = GNUNET_malloc (sizeof (struct Plugin));
2021 plugin->sessions = NULL;
2023 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2025 api->send = &http_plugin_send;
2026 api->disconnect = &http_plugin_disconnect;
2027 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2028 api->check_address = &http_plugin_address_suggested;
2029 api->address_to_string = &http_plugin_address_to_string;
2031 /* Hashing our identity to use it in URLs */
2032 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2034 /* Reading port number from config file */
2036 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2042 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2045 ("Require valid port number for transport plugin `%s' in configuration!\n"),
2047 libgnunet_plugin_transport_http_done (api);
2050 GNUNET_assert ((port > 0) && (port <= 65535));
2051 plugin->port_inbound = port;
2052 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2053 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->http_server_daemon_v6 == NULL) && (port != 0))
2055 plugin->http_server_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
2057 &acceptPolicyCallback,
2058 plugin , &accessHandlerCallback, plugin,
2059 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
2060 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
2061 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
2062 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2063 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
2065 plugin->http_server_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
2067 &acceptPolicyCallback,
2068 plugin , &accessHandlerCallback, plugin,
2069 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
2070 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
2071 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
2072 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2073 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
2076 if (plugin->http_server_daemon_v4 != NULL)
2077 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2078 if (plugin->http_server_daemon_v6 != NULL)
2079 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2081 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
2083 else if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
2087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
2088 libgnunet_plugin_transport_http_done (api);
2092 /* Initializing cURL */
2093 curl_global_init(CURL_GLOBAL_ALL);
2094 plugin->multi_handle = curl_multi_init();
2096 if ( NULL == plugin->multi_handle )
2098 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2100 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2102 libgnunet_plugin_transport_http_done (api);
2106 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
2107 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2108 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2113 /* end of plugin_transport_http.c */