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
46 #define INBOUND GNUNET_NO
47 #define OUTBOUND GNUNET_YES
50 * Text of the response sent back after the last bytes of a PUT
51 * request have been received (just to formally obey the HTTP
54 #define HTTP_PUT_RESPONSE "Thank you!"
57 * After how long do we expire an address that we
58 * learned from another peer if it is not reconfirmed
61 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
64 * Page returned if request invalid
66 #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>"
69 * Timeout for a http connect
71 #define HTTP_CONNECT_TIMEOUT 30
74 * Network format for IPv4 addresses.
76 struct IPv4HttpAddress
79 * IPv4 address, in network byte order.
81 uint32_t ipv4_addr GNUNET_PACKED;
84 * Port number, in network byte order.
86 uint16_t u_port GNUNET_PACKED;
92 * Network format for IPv6 addresses.
94 struct IPv6HttpAddress
99 struct in6_addr ipv6_addr GNUNET_PACKED;
102 * Port number, in network byte order.
104 uint16_t u6_port GNUNET_PACKED;
110 * Message to send using http
115 * next pointer for double linked list
117 struct HTTP_Message * next;
120 * previous pointer for double linked list
122 struct HTTP_Message * prev;
125 * buffer containing data to send
130 * amount of data already sent
140 * Continuation function to call once the transmission buffer
141 * has again space available. NULL if there is no
142 * continuation to call.
144 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
147 * Closure for transmit_cont.
149 void *transmit_cont_cls;
153 struct HTTP_PeerContext
155 struct GNUNET_PeerIdentity identity;
157 struct HTTP_Session * head;
158 struct HTTP_Session * tail;
164 struct HTTP_Session * next;
165 struct HTTP_Session * prev;
176 * Message queue for outbound messages
179 struct HTTP_Message * pending_msgs_head;
182 * Message queue for outbound messages
185 struct HTTP_Message * pending_msgs_tail;
188 * partner peer this connection belongs to
190 struct HTTP_PeerContext * peercontext;
193 * message stream tokenizer for incoming data
195 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
199 * outbound: GNUNET_YES
200 * inbound : GNUNET_NO
202 unsigned int direction;
204 unsigned int send_connected;
205 unsigned int send_active;
206 unsigned int recv_connected;
207 unsigned int recv_active;
210 * entity managing sending data
212 void * send_endpoint;
215 * entity managing recieving data
217 void * recv_endpoint;
221 struct HTTP_Connection
223 struct HTTP_Connection * next;
224 struct HTTP_Connection * prev;
230 * Message queue for outbound messages
233 struct HTTP_Message * pending_msgs_head;
236 * Message queue for outbound messages
239 struct HTTP_Message * pending_msgs_tail;
247 * status of PUT connection
250 unsigned int put_connected;
253 * status of PUT connection
254 * if no data to send, connection is paused
256 unsigned int put_send_paused;
258 unsigned int get_connected;
261 * curl handle for sending data using HTTP/PUT
264 CURL *put_curl_handle;
267 * curl handle for recieving data using HTTP/GET transmission
270 CURL *get_curl_handle;
272 struct Session * session;
274 struct GNUNET_SERVER_MessageStreamTokenizer * get_msgtok;
277 struct HTTP_Connection_in
279 struct HTTP_Connection_in * next;
281 struct HTTP_Connection_in * prev;
286 unsigned int connected;
287 unsigned int send_paused;
289 struct GNUNET_SERVER_MessageStreamTokenizer * msgtok;
291 struct Session * session;
294 * Is there a HTTP/PUT in progress?
296 int is_put_in_progress;
299 * Is the http request invalid?
305 * Session handle for connections.
313 struct SessionHeader header;
316 * Stored in a linked list.
318 struct Session *next;
321 * Pointer to the global plugin struct.
323 struct Plugin *plugin;
326 * To whom are we talking to (set to our identity
327 * if we are still waiting for the welcome message)
329 struct GNUNET_PeerIdentity identity;
332 * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
337 * At what time did we reset last_received last?
339 struct GNUNET_TIME_Absolute last_quota_update;
342 * How many bytes have we received since the "last_quota_update"
345 uint64_t last_received;
348 * Number of bytes per ms that this peer is allowed
356 struct GNUNET_CRYPTO_HashAsciiEncoded hash;
358 struct HTTP_Connection *outbound_connections_head;
359 struct HTTP_Connection *outbound_connections_tail;
361 struct HTTP_Connection_in *inbound_connections_head;
362 struct HTTP_Connection_in *inbound_connections_tail;
366 * Encapsulation of all of the state of the plugin.
373 struct GNUNET_TRANSPORT_PluginEnvironment *env;
375 unsigned int port_inbound;
378 * Hashmap for all existing sessions.
380 struct GNUNET_CONTAINER_MultiHashMap *sessions;
382 struct GNUNET_CONTAINER_MultiHashMap *peers;
385 * Daemon for listening for new IPv4 connections.
387 struct MHD_Daemon *http_server_daemon_v4;
390 * Daemon for listening for new IPv6connections.
392 struct MHD_Daemon *http_server_daemon_v6;
395 * Our primary task for http daemon handling IPv4 connections
397 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
400 * Our primary task for http daemon handling IPv6 connections
402 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
405 * The task sending data
407 GNUNET_SCHEDULER_TaskIdentifier http_server_task_send;
412 CURLM * multi_handle;
415 * Our ASCII encoded, hashed peer identity
416 * This string is used to distinguish between connections and is added to the urls
418 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
423 * Function called for a quick conversion of the binary address to
424 * a numeric address. Note that the caller must not free the
425 * address and that the next call to this function is allowed
426 * to override the address again.
429 * @param addr binary address
430 * @param addrlen length of the address
431 * @return string representing the same address
434 http_plugin_address_to_string (void *cls,
439 * Create a new session
440 * @param cls plugin as closure
441 * @param addr_in address the peer is using inbound
442 * @param addr_len_in address length
443 * @param addr_out address the peer is using outbound
444 * @param addr_len_out address length
445 * @param peer identity
446 * @return created session object
448 static struct Session *
449 create_session (void * cls,
454 const struct GNUNET_PeerIdentity *peer)
456 struct Plugin *plugin = cls;
457 struct Session * cs = GNUNET_malloc ( sizeof( struct Session) );
459 GNUNET_assert(cls !=NULL);
461 memcpy(&cs->identity, peer, sizeof (struct GNUNET_PeerIdentity));
462 GNUNET_CRYPTO_hash_to_enc(&cs->identity.hashPubKey,&(cs->hash));
463 cs->outbound_connections_head = NULL;
464 cs->outbound_connections_tail = NULL;
469 * Check if session for this peer is already existing, otherwise create it
470 * @param cls the plugin used
471 * @param p peer to get session for
472 * @return session found or created
474 static struct Session * session_get (void * cls, const struct GNUNET_PeerIdentity *p)
476 struct Plugin *plugin = cls;
480 cs = GNUNET_CONTAINER_multihashmap_get (plugin->sessions, &p->hashPubKey);
483 cs = create_session(plugin, NULL, 0, NULL, 0, p);
484 res = GNUNET_CONTAINER_multihashmap_put ( plugin->sessions,
485 &cs->identity.hashPubKey,
487 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
488 if (res == GNUNET_OK)
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "New Session `%s' inserted\n", GNUNET_i2s(p));
495 static char * create_url(void * cls, const void * addr, size_t addrlen)
497 struct Plugin *plugin = cls;
500 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
501 GNUNET_asprintf(&url,
503 http_plugin_address_to_string(NULL, addr, addrlen),
504 (char *) (&plugin->my_ascii_hash_ident));
510 * Removes a message from the linked list of messages
511 * @param con connection to remove message from
512 * @param msg message to remove
513 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
516 static int remove_http_message(struct HTTP_Session * ps, struct HTTP_Message * msg)
518 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
524 * Check if session already knows this address for a outbound connection to this peer
525 * If address not in session, add it to the session
526 * @param cls the plugin used
527 * @param cs the session
528 * @param addr address
529 * @param addr_len address length
530 * @return the found or created address
532 static struct HTTP_Connection * session_check_outbound_address (void * cls, struct Session *cs, const void * addr, size_t addr_len)
534 struct Plugin *plugin = cls;
535 struct HTTP_Connection * cc = cs->outbound_connections_head;
536 struct HTTP_Connection * con = NULL;
538 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
542 if (addr_len == cc->addrlen)
544 if (0 == memcmp(cc->addr, addr, addr_len))
555 con = GNUNET_malloc(sizeof(struct HTTP_Connection) + addr_len);
556 con->addrlen = addr_len;
558 con->url=create_url(plugin, addr, addr_len);
559 con->put_curl_handle = NULL;
560 con->put_connected = GNUNET_NO;
561 con->get_curl_handle = NULL;
562 con->get_connected = GNUNET_NO;
564 memcpy(con->addr, addr, addr_len);
565 GNUNET_CONTAINER_DLL_insert(cs->outbound_connections_head,cs->outbound_connections_tail,con);
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Created new connection %X to peer `%s'\n",con,GNUNET_i2s(&cs->identity));
571 static struct HTTP_Session * get_HTTP_Session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addr_len)
573 struct HTTP_Session * cc = pc->head;
574 struct HTTP_Session * con = NULL;
575 unsigned int count = 0;
577 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
580 if (addr_len == cc->addrlen)
582 if (0 == memcmp(cc->addr, addr, addr_len))
596 * Check if session already knows this address for a inbound connection to this peer
597 * If address not in session, add it to the session
598 * @param cls not needed, can be NULL
599 * @param cs the session
600 * @param addr address
601 * @param addr_len address length
602 * @return the found or created address
604 static struct HTTP_Connection_in * session_check_inbound_address (void * cls, struct Session *cs, const void * addr, size_t addr_len)
606 struct HTTP_Connection_in * cc = cs->inbound_connections_head;
607 struct HTTP_Connection_in * con = NULL;
609 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
613 if (addr_len == cc->addrlen)
615 if (0 == memcmp(cc->addr, addr, addr_len))
627 con = GNUNET_malloc(sizeof(struct HTTP_Connection_in) + addr_len);
628 con->addrlen = addr_len;
630 con->connected = GNUNET_NO;
632 memcpy(con->addr, addr, addr_len);
633 GNUNET_CONTAINER_DLL_insert(cs->inbound_connections_head,cs->inbound_connections_tail,con);
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X for inbound address %s (%s) was found\n",
638 GNUNET_i2s(&cs->identity),
639 http_plugin_address_to_string(NULL,con->addr,con->addrlen));
645 * Callback called by MHD when a connection is terminated
647 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
649 struct HTTP_Connection_in * con;
651 con = *httpSessionCache;
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&con->session->identity));
655 /* session set to inactive */
656 con->is_put_in_progress = GNUNET_NO;
657 con->is_bad_request = GNUNET_NO;
660 static void mhd_write_mst_cb (void *cls,
662 const struct GNUNET_MessageHeader *message)
664 struct HTTP_Connection_in * con = cls;
665 GNUNET_assert(con != NULL);
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
669 ntohs(message->type),
670 ntohs(message->size),
671 GNUNET_i2s(&(con->session->identity)),http_plugin_address_to_string(NULL,con->addr,con->addrlen));
673 con->session->plugin->env->receive (con->session->plugin->env->cls,
674 &con->session->identity,
675 message, 1, con->session,
680 static void curl_write_mst_cb (void *cls,
682 const struct GNUNET_MessageHeader *message)
684 struct HTTP_Connection * con = cls;
685 GNUNET_assert(con != NULL);
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
689 ntohs(message->type),
690 ntohs(message->size),
691 GNUNET_i2s(&(con->session->identity)),http_plugin_address_to_string(NULL,con->addr,con->addrlen));
693 con->session->plugin->env->receive (con->session->plugin->env->cls,
694 &con->session->identity,
695 message, 1, con->session,
702 * Check if ip is allowed to connect.
705 acceptPolicyCallback (void *cls,
706 const struct sockaddr *addr, socklen_t addr_len)
709 struct Plugin *plugin = cls;
711 /* Every connection is accepted, nothing more to do here */
715 int server_read_callback (void *cls, uint64_t pos, char *buf, int max)
719 struct HTTP_Connection * con = cls;
720 struct HTTP_Message * msg;
723 msg=con->pending_msgs_tail;
728 if ((msg->size-msg->pos) <= max)
730 memcpy(buf,&msg->buf[pos],(msg->size-msg->pos));
731 pos+=(msg->size-msg->pos);
735 memcpy(buf,&msg->buf[pos],max);
739 if (msg->pos==msg->size)
741 if (NULL != con->pending_msgs_tail->transmit_cont)
742 msg->transmit_cont (msg->transmit_cont_cls,&con->session->identity,GNUNET_SYSERR);
743 res = remove_http_message(con,msg);
752 * Process GET or PUT request received via MHD. For
753 * GET, queue response that will send back our pending
754 * messages. For PUT, process incoming data and send
755 * to GNUnet core. In either case, check if a session
756 * already exists and create a new one if not.
759 accessHandlerCallback (void *cls,
760 struct MHD_Connection *mhd_connection,
764 const char *upload_data,
765 size_t * upload_data_size, void **httpSessionCache)
767 struct Plugin *plugin = cls;
768 struct MHD_Response *response;
770 struct HTTP_Connection_in * con;
771 const union MHD_ConnectionInfo * conn_info;
772 struct sockaddr_in *addrin;
773 struct sockaddr_in6 *addrin6;
774 char address[INET6_ADDRSTRLEN+14];
775 struct GNUNET_PeerIdentity pi_in;
777 int send_error_to_client;
778 struct IPv4HttpAddress ipv4addr;
779 struct IPv6HttpAddress ipv6addr;
780 struct HTTP_PeerContext *pc;
781 struct HTTP_Session *ps;
785 GNUNET_assert(cls !=NULL);
786 send_error_to_client = GNUNET_NO;
787 if (NULL == *httpSessionCache)
789 /* check url for peer identity , if invalid send HTTP 404*/
790 res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
791 if ( GNUNET_SYSERR == res )
793 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
794 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
795 MHD_destroy_response (response);
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
805 con = *httpSessionCache;
809 if (NULL == *httpSessionCache)
811 /* get session for peer identity */
812 cs = session_get (plugin ,&pi_in);
814 /* get peer context */
815 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"RECV: CREATING NEW PEER CONTEXT\n");
820 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
821 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
822 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"RECV: PEER CONTEXT FOUND\n");
829 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
830 /* Incoming IPv4 connection */
831 if ( AF_INET == conn_info->client_addr->sin_family)
833 addrin = conn_info->client_addr;
834 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
835 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
836 ipv4addr.u_port = addrin->sin_port;
838 addr_len = sizeof(struct IPv4HttpAddress);
839 con = session_check_inbound_address (plugin, cs, (const void *) &ipv4addr, sizeof (struct IPv4HttpAddress));
841 /* Incoming IPv6 connection */
842 if ( AF_INET6 == conn_info->client_addr->sin_family)
844 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
845 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
846 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
847 ipv6addr.u6_port = addrin6->sin6_port;
849 addr_len = sizeof(struct IPv6HttpAddress);
850 con = session_check_inbound_address (plugin, cs, &ipv6addr, sizeof (struct IPv6HttpAddress));
852 /* Set closure and update current session*/
855 ps = get_HTTP_Session(plugin, pc, addr, addr_len);
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"RECV: CREATING NEW SESSION %s\n",http_plugin_address_to_string(NULL, addr, addr_len));
859 ps = GNUNET_malloc(sizeof (struct HTTP_Session));
860 ps->addr = GNUNET_malloc(addr_len);
861 memcpy(ps->addr,addr,addr_len);
862 ps->addrlen = addr_len;
863 ps->direction=GNUNET_NO;
864 ps->pending_msgs_head = NULL;
865 ps->pending_msgs_tail = NULL;
866 ps->url = create_url (plugin, ps->addr, ps->addrlen);
867 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"RECV: SESSION CONTEXT FOUND\n");
874 *httpSessionCache = con;
875 if (con->msgtok==NULL)
876 con->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, con);
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
880 GNUNET_i2s(&cs->identity),
881 http_plugin_address_to_string(NULL, con->addr, con->addrlen));
884 /* Is it a PUT or a GET request */
885 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
887 if ((*upload_data_size == 0) && (con->is_put_in_progress==GNUNET_NO))
889 con->is_put_in_progress = GNUNET_YES;
893 /* Transmission of all data complete */
894 if ((*upload_data_size == 0) && (con->is_put_in_progress == GNUNET_YES))
896 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
897 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
899 MHD_destroy_response (response);
902 con->is_put_in_progress = GNUNET_NO;
903 con->is_bad_request = GNUNET_NO;
908 if ((*upload_data_size > 0) && (con->is_put_in_progress == GNUNET_YES))
910 res = GNUNET_SERVER_mst_receive(con->msgtok, con, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
911 (*upload_data_size) = 0;
917 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
919 response = MHD_create_response_from_callback(-1,32 * 1024, &server_read_callback, con, NULL);
920 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
921 MHD_destroy_response (response);
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
925 GNUNET_i2s(&cs->identity),
926 http_plugin_address_to_string(NULL, con->addr, con->addrlen));
937 * Call MHD to process pending ipv4 requests and then go back
938 * and schedule the next run.
940 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
942 * Call MHD to process pending ipv6 requests and then go back
943 * and schedule the next run.
945 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
948 * Function that queries MHD's select sets and
949 * starts the task waiting for them.
951 static GNUNET_SCHEDULER_TaskIdentifier
952 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
954 struct Plugin *plugin = cls;
955 GNUNET_SCHEDULER_TaskIdentifier ret;
959 struct GNUNET_NETWORK_FDSet *wrs;
960 struct GNUNET_NETWORK_FDSet *wws;
961 struct GNUNET_NETWORK_FDSet *wes;
963 unsigned long long timeout;
965 struct GNUNET_TIME_Relative tv;
967 GNUNET_assert(cls !=NULL);
968 ret = GNUNET_SCHEDULER_NO_TASK;
972 wrs = GNUNET_NETWORK_fdset_create ();
973 wes = GNUNET_NETWORK_fdset_create ();
974 wws = GNUNET_NETWORK_fdset_create ();
976 GNUNET_assert (MHD_YES ==
977 MHD_get_fdset (daemon_handle,
982 haveto = MHD_get_timeout (daemon_handle, &timeout);
983 if (haveto == MHD_YES)
984 tv.value = (uint64_t) timeout;
986 tv = GNUNET_TIME_UNIT_FOREVER_REL;
987 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
988 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
989 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
990 if (daemon_handle == plugin->http_server_daemon_v4)
992 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
993 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
994 GNUNET_SCHEDULER_NO_TASK,
998 &http_server_daemon_v4_run,
1001 if (daemon_handle == plugin->http_server_daemon_v6)
1003 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1004 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1005 GNUNET_SCHEDULER_NO_TASK,
1009 &http_server_daemon_v6_run,
1012 GNUNET_NETWORK_fdset_destroy (wrs);
1013 GNUNET_NETWORK_fdset_destroy (wws);
1014 GNUNET_NETWORK_fdset_destroy (wes);
1019 * Call MHD to process pending requests and then go back
1020 * and schedule the next run.
1022 static void http_server_daemon_v4_run (void *cls,
1023 const struct GNUNET_SCHEDULER_TaskContext *tc)
1025 struct Plugin *plugin = cls;
1027 GNUNET_assert(cls !=NULL);
1028 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1029 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1031 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1034 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1035 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1041 * Call MHD to process pending requests and then go back
1042 * and schedule the next run.
1044 static void http_server_daemon_v6_run (void *cls,
1045 const struct GNUNET_SCHEDULER_TaskContext *tc)
1047 struct Plugin *plugin = cls;
1049 GNUNET_assert(cls !=NULL);
1050 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1051 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1053 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1056 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1057 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1062 static size_t curl_header_function( void *ptr, size_t size, size_t nmemb, void *stream)
1064 struct HTTP_Connection * con = stream;
1067 size_t len = size * nmemb;
1068 long http_result = 0;
1071 /* Getting last http result code */
1072 if (con->get_connected==GNUNET_NO)
1074 GNUNET_assert(NULL!=con);
1075 res = curl_easy_getinfo(con->get_curl_handle, CURLINFO_RESPONSE_CODE, &http_result);
1076 if (CURLE_OK == res)
1078 if (http_result == 200)
1080 con->get_connected = GNUNET_YES;
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connected\n",con);
1087 if ((size * nmemb) < SIZE_MAX)
1088 tmp = GNUNET_malloc (len+1);
1090 if ((tmp != NULL) && (len > 0))
1092 memcpy(tmp,ptr,len);
1095 if (tmp[len-2] == 13)
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s' %u \n",tmp, http_result);
1105 return size * nmemb;
1109 * Callback method used with libcurl
1110 * Method is called when libcurl needs to read data during sending
1111 * @param stream pointer where to write data
1112 * @param size size of an individual element
1113 * @param nmemb count of elements that can be written to the buffer
1114 * @param ptr source pointer, passed to the libcurl handle
1115 * @return bytes written to stream
1117 static size_t send_curl_read_callback(void *stream, size_t size, size_t nmemb, void *ptr)
1119 struct HTTP_Session * ps = ptr;
1120 struct HTTP_Message * msg = ps->pending_msgs_tail;
1124 if (ps->pending_msgs_tail == NULL)
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1127 ps->send_active = GNUNET_NO;
1128 return CURL_READFUNC_PAUSE;
1131 msg = ps->pending_msgs_tail;
1133 if (msg->pos < msg->size)
1135 /* data fit in buffer */
1136 if ((msg->size - msg->pos) <= (size * nmemb))
1138 len = (msg->size - msg->pos);
1139 memcpy(stream, &msg->buf[msg->pos], len);
1146 memcpy(stream, &msg->buf[msg->pos], len);
1151 /* no data to send */
1157 if ( msg->pos == msg->size)
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1160 /* Calling transmit continuation */
1161 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
1162 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1163 remove_http_message(ps, msg);
1169 * Callback method used with libcurl
1170 * Method is called when libcurl needs to write data during sending
1171 * @param stream pointer where to write data
1172 * @param size size of an individual element
1173 * @param nmemb count of elements that can be written to the buffer
1174 * @param ptr destination pointer, passed to the libcurl handle
1175 * @return bytes read from stream
1177 static size_t send_curl_write_callback( void *stream, size_t size, size_t nmemb, void *ptr)
1179 struct HTTP_Connection * con = ptr;
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes recieved\n",con, size*nmemb);
1182 GNUNET_SERVER_mst_receive(con->get_msgtok, con, stream,size*nmemb, GNUNET_NO, GNUNET_NO);
1184 return (size * nmemb);
1189 * Function setting up file descriptors and scheduling task to run
1190 * @param cls closure
1191 * @param ses session to send data to
1192 * @return bytes sent to peer
1194 static size_t send_schedule(void *cls, struct Session* ses );
1199 * Function setting up curl handle and selecting message to send
1201 * @param ses session to send data to
1202 * @param con connection
1203 * @return bytes sent to peer
1205 static ssize_t send_check_connections (void *cls, struct Session* ses , struct HTTP_Session *ps)
1207 struct Plugin *plugin = cls;
1210 struct HTTP_Message * msg;
1211 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1213 GNUNET_assert(cls !=NULL);
1215 if (ps->direction == OUTBOUND)
1217 /* Check if session is connected to receive data, otherwise connect to peer */
1218 if (ps->recv_connected == GNUNET_NO)
1220 if (ps->recv_endpoint == NULL)
1222 ps->recv_endpoint = curl_easy_init();
1224 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1226 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1227 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_header_function);
1228 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1229 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, send_curl_read_callback);
1230 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1231 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, send_curl_write_callback);
1232 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1233 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1234 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1235 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1236 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1238 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1239 if (mret != CURLM_OK)
1241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1242 _("%s failed at %s:%d: `%s'\n"),
1243 "curl_multi_add_handle", __FILE__, __LINE__,
1244 curl_multi_strerror (mret));
1248 if (ps->msgtok != NULL)
1249 ps->msgtok = GNUNET_SERVER_mst_create (&curl_write_mst_cb, ps);
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1254 /* Check if session is connected to send data, otherwise connect to peer */
1255 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1257 if (ps->send_connected == GNUNET_NO)
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1262 if (ps->send_active == GNUNET_NO)
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1265 curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT);
1266 ps->send_active=GNUNET_YES;
1271 /* not connected, initiate connection */
1272 if ( NULL == ps->send_endpoint)
1273 ps->send_endpoint = curl_easy_init();
1274 GNUNET_assert (ps->send_endpoint != NULL);
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1277 GNUNET_assert (NULL != ps->pending_msgs_tail);
1278 msg = ps->pending_msgs_tail;
1281 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1283 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1284 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1285 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, send_curl_read_callback);
1286 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1287 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, send_curl_write_callback);
1288 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1289 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1290 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1291 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1292 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1294 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1295 if (mret != CURLM_OK)
1297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1298 _("%s failed at %s:%d: `%s'\n"),
1299 "curl_multi_add_handle", __FILE__, __LINE__,
1300 curl_multi_strerror (mret));
1303 ps->send_connected = GNUNET_YES;
1304 bytes_sent = send_schedule (plugin, NULL);
1307 if (ps->direction == INBOUND)
1309 GNUNET_assert (NULL != ps->pending_msgs_tail);
1310 msg = ps->pending_msgs_tail;
1311 if ((ps->recv_connected==GNUNET_YES) && (ps->recv_connected==GNUNET_YES))
1312 bytes_sent = msg->size;
1317 static void send_execute (void *cls,
1318 const struct GNUNET_SCHEDULER_TaskContext *tc)
1320 struct Plugin *plugin = cls;
1321 static unsigned int handles_last_run;
1323 struct CURLMsg *msg;
1325 struct HTTP_Connection * con = NULL;
1326 struct Session * cs = NULL;
1329 GNUNET_assert(cls !=NULL);
1330 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
1331 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1337 mret = curl_multi_perform (plugin->multi_handle, &running);
1338 if (running < handles_last_run)
1343 msg = curl_multi_info_read (plugin->multi_handle, &running);
1346 /* get session for affected curl handle */
1347 GNUNET_assert ( msg->easy_handle != NULL );
1348 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char *) &con);
1349 GNUNET_assert ( con != NULL );
1351 GNUNET_assert ( cs != NULL );
1356 if ( (msg->data.result != CURLE_OK) &&
1357 (msg->data.result != CURLE_GOT_NOTHING) )
1359 /* sending msg failed*/
1360 if (msg->easy_handle == con->put_curl_handle)
1362 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1363 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1365 GNUNET_i2s(&cs->identity),
1366 http_plugin_address_to_string(NULL, con->addr, con->addrlen),
1367 "curl_multi_perform",
1368 curl_easy_strerror (msg->data.result));
1370 con->put_connected = GNUNET_NO;
1371 curl_easy_cleanup(con->put_curl_handle);
1372 con->put_curl_handle=NULL;
1373 if (( NULL != con->pending_msgs_tail) && ( NULL != con->pending_msgs_tail->transmit_cont))
1374 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&con->session->identity,GNUNET_SYSERR);
1376 /* GET connection failed */
1377 if (msg->easy_handle == con->get_curl_handle)
1379 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1380 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1382 GNUNET_i2s(&cs->identity),
1383 http_plugin_address_to_string(NULL, con->addr, con->addrlen),
1384 "curl_multi_perform",
1385 curl_easy_strerror (msg->data.result));
1386 con->get_connected = GNUNET_NO;
1387 curl_easy_cleanup(con->get_curl_handle);
1388 con->get_curl_handle=NULL;
1393 if (msg->easy_handle == con->put_curl_handle)
1395 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1399 GNUNET_i2s(&cs->identity),
1400 http_plugin_address_to_string(NULL, con->addr, con->addrlen),
1403 /* Calling transmit continuation */
1404 if (( NULL != con->pending_msgs_tail) && (NULL != con->pending_msgs_tail->transmit_cont))
1406 /* HTTP 1xx : Last message before here was informational */
1407 if ((http_result >=100) && (http_result < 200))
1408 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_OK);
1409 /* HTTP 2xx: successful operations */
1410 if ((http_result >=200) && (http_result < 300))
1411 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_OK);
1412 /* HTTP 3xx..5xx: error */
1413 if ((http_result >=300) && (http_result < 600))
1414 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_SYSERR);
1416 curl_easy_cleanup(con->put_curl_handle);
1417 con->put_connected = GNUNET_NO;
1418 con->put_curl_handle=NULL;
1420 if (msg->easy_handle == con->get_curl_handle)
1422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1423 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1425 GNUNET_i2s(&cs->identity),
1426 http_plugin_address_to_string(NULL, con->addr, con->addrlen),
1429 con->get_connected = GNUNET_NO;
1430 curl_easy_cleanup(con->get_curl_handle);
1431 con->get_curl_handle=NULL;
1434 if (con->pending_msgs_tail != NULL)
1436 if (con->pending_msgs_tail->pos>0)
1437 remove_http_message(con, con->pending_msgs_tail);
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message could not be removed from session `%s'\n", GNUNET_i2s(&cs->identity));
1446 while ( (running > 0) );
1448 handles_last_run = running;
1450 while (mret == CURLM_CALL_MULTI_PERFORM);
1451 send_schedule(plugin, cls);
1456 * Function setting up file descriptors and scheduling task to run
1457 * @param ses session to send data to
1458 * @return bytes sent to peer
1460 static size_t send_schedule(void *cls, struct Session* ses )
1462 struct Plugin *plugin = cls;
1467 struct GNUNET_NETWORK_FDSet *grs;
1468 struct GNUNET_NETWORK_FDSet *gws;
1472 GNUNET_assert(cls !=NULL);
1477 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1478 if (mret != CURLM_OK)
1480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1481 _("%s failed at %s:%d: `%s'\n"),
1482 "curl_multi_fdset", __FILE__, __LINE__,
1483 curl_multi_strerror (mret));
1486 mret = curl_multi_timeout (plugin->multi_handle, &to);
1487 if (mret != CURLM_OK)
1489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1490 _("%s failed at %s:%d: `%s'\n"),
1491 "curl_multi_timeout", __FILE__, __LINE__,
1492 curl_multi_strerror (mret));
1496 grs = GNUNET_NETWORK_fdset_create ();
1497 gws = GNUNET_NETWORK_fdset_create ();
1498 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1499 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1500 plugin->http_server_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1501 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1502 GNUNET_SCHEDULER_NO_TASK,
1503 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1508 GNUNET_NETWORK_fdset_destroy (gws);
1509 GNUNET_NETWORK_fdset_destroy (grs);
1511 /* FIXME: return bytes REALLY sent */
1517 * Function that can be used by the transport service to transmit
1518 * a message using the plugin. Note that in the case of a
1519 * peer disconnecting, the continuation MUST be called
1520 * prior to the disconnect notification itself. This function
1521 * will be called with this peer's HELLO message to initiate
1522 * a fresh connection to another peer.
1524 * @param cls closure
1525 * @param target who should receive this message
1526 * @param msgbuf the message to transmit
1527 * @param msgbuf_size number of bytes in 'msgbuf'
1528 * @param priority how important is the message (most plugins will
1529 * ignore message priority and just FIFO)
1530 * @param timeout how long to wait at most for the transmission (does not
1531 * require plugins to discard the message after the timeout,
1532 * just advisory for the desired delay; most plugins will ignore
1534 * @param session which session must be used (or NULL for "any")
1535 * @param addr the address to use (can be NULL if the plugin
1536 * is "on its own" (i.e. re-use existing TCP connection))
1537 * @param addrlen length of the address in bytes
1538 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1539 * GNUNET_NO means the plugin may use any other address and
1540 * GNUNET_SYSERR means that only reliable existing
1541 * bi-directional connections should be used (regardless
1543 * @param cont continuation to call once the message has
1544 * been transmitted (or if the transport is ready
1545 * for the next transmission call; or if the
1546 * peer disconnected...); can be NULL
1547 * @param cont_cls closure for cont
1548 * @return number of bytes used (on the physical network, with overheads);
1549 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1550 * and does NOT mean that the message was not transmitted (DV)
1553 http_plugin_send (void *cls,
1554 const struct GNUNET_PeerIdentity *target,
1557 unsigned int priority,
1558 struct GNUNET_TIME_Relative to,
1559 struct Session *session,
1563 GNUNET_TRANSPORT_TransmitContinuation cont,
1566 struct Plugin *plugin = cls;
1567 //struct Session *cs;
1568 struct HTTP_Message *msg;
1569 //struct HTTP_Connection *con;
1572 struct HTTP_PeerContext * pc;
1573 struct HTTP_Session * ps;
1575 GNUNET_assert(cls !=NULL);
1576 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
1578 /* get session from hashmap */
1579 //cs = session_get(plugin, target);
1580 //con = session_check_outbound_address(plugin, cs, addr, addrlen);
1583 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: CREATING NEW PEER CONTEXT\n");
1588 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1589 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1590 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: PEER CONTEXT FOUND\n");
1596 ps = get_HTTP_Session(plugin, pc, addr, addrlen);
1597 /* session not existing, but address forced -> creating new session */
1598 if ((ps==NULL) && (force_address == GNUNET_YES))
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: CREATING NEW SESSION %s\n",http_plugin_address_to_string(NULL, addr, addrlen));
1601 ps = GNUNET_malloc(sizeof (struct HTTP_Session));
1602 ps->addr = GNUNET_malloc(addrlen);
1603 memcpy(ps->addr,addr,addrlen);
1604 ps->addrlen = addrlen;
1605 ps->direction=OUTBOUND;
1606 ps->recv_connected = GNUNET_NO;
1607 ps->send_connected = GNUNET_NO;
1608 ps->pending_msgs_head = NULL;
1609 ps->pending_msgs_tail = NULL;
1610 ps->url = create_url (plugin, ps->addr, ps->addrlen);
1611 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1613 /* session not existing, address not forced -> looking for other session */
1614 if ((ps==NULL) && (force_address == GNUNET_NO))
1616 /* FIXME: CREATING SESSION, SHOULD CHOOSE EXISTING */
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: CREATING NEW SESSION %s\n",http_plugin_address_to_string(NULL, addr, addrlen));
1618 ps = GNUNET_malloc(sizeof (struct HTTP_Session));
1619 ps->addr = GNUNET_malloc(addrlen);
1620 memcpy(ps->addr,addr,addrlen);
1621 ps->addrlen = addrlen;
1622 ps->direction=OUTBOUND;
1623 ps->recv_connected = GNUNET_NO;
1624 ps->send_connected = GNUNET_NO;
1625 ps->pending_msgs_head = NULL;
1626 ps->pending_msgs_tail = NULL;
1627 ps->url = create_url (plugin, ps->addr, ps->addrlen);
1628 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1630 if ((ps==NULL) && (force_address == GNUNET_SYSERR))
1632 /* FIXME: CREATING SESSION, SHOULD CHOOSE EXISTING */
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: CREATING NEW SESSION %s\n",http_plugin_address_to_string(NULL, addr, addrlen));
1634 ps = GNUNET_malloc(sizeof (struct HTTP_Session));
1635 ps->addr = GNUNET_malloc(addrlen);
1636 memcpy(ps->addr,addr,addrlen);
1637 ps->addrlen = addrlen;
1638 ps->direction=OUTBOUND;
1639 ps->recv_connected = GNUNET_NO;
1640 ps->send_connected = GNUNET_NO;
1641 ps->pending_msgs_head = NULL;
1642 ps->pending_msgs_tail = NULL;
1643 ps->url = create_url (plugin, ps->addr, ps->addrlen);
1644 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1647 char * force = GNUNET_malloc(30);
1648 if (force_address == GNUNET_YES)
1649 strcpy(force,"forced addr.");
1650 if (force_address == GNUNET_NO)
1651 strcpy(force,"any addr.");
1652 if (force_address == GNUNET_SYSERR)
1653 strcpy(force,"reliable bi-direc. address addr.");
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' %s (%s), session: %X\n",
1656 GNUNET_i2s(&pc->identity),
1658 http_plugin_address_to_string(NULL, addr, addrlen),
1663 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1665 msg->size = msgbuf_size;
1667 msg->buf = (char *) &msg[1];
1668 msg->transmit_cont = cont;
1669 msg->transmit_cont_cls = cont_cls;
1670 memcpy (msg->buf,msgbuf, msgbuf_size);
1671 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1673 return send_check_connections (plugin, session, ps);
1679 * Function that can be used to force the plugin to disconnect
1680 * from the given peer and cancel all previous transmissions
1681 * (and their continuationc).
1683 * @param cls closure
1684 * @param target peer from which to disconnect
1687 http_plugin_disconnect (void *cls,
1688 const struct GNUNET_PeerIdentity *target)
1690 struct Plugin *plugin = cls;
1691 struct HTTP_Connection *con;
1694 /* get session from hashmap */
1695 cs = session_get(plugin, target);
1696 con = cs->outbound_connections_head;
1700 if (con->put_curl_handle!=NULL)
1701 curl_easy_cleanup(con->put_curl_handle);
1702 con->put_curl_handle=NULL;
1703 con->put_connected = GNUNET_NO;
1704 while (con->pending_msgs_head!=NULL)
1706 remove_http_message(con, con->pending_msgs_head);
1714 * Convert the transports address to a nice, human-readable
1717 * @param cls closure
1718 * @param type name of the transport that generated the address
1719 * @param addr one of the addresses of the host, NULL for the last address
1720 * the specific address format depends on the transport
1721 * @param addrlen length of the address
1722 * @param numeric should (IP) addresses be displayed in numeric form?
1723 * @param timeout after how long should we give up?
1724 * @param asc function to call on each string
1725 * @param asc_cls closure for asc
1728 http_plugin_address_pretty_printer (void *cls,
1733 struct GNUNET_TIME_Relative timeout,
1734 GNUNET_TRANSPORT_AddressStringCallback
1737 const struct IPv4HttpAddress *t4;
1738 const struct IPv6HttpAddress *t6;
1739 struct sockaddr_in a4;
1740 struct sockaddr_in6 a6;
1746 GNUNET_assert(cls !=NULL);
1747 if (addrlen == sizeof (struct IPv6HttpAddress))
1749 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1751 a6.sin6_addr = t6->ipv6_addr;
1752 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1753 port = ntohs(t6->u6_port);
1755 else if (addrlen == sizeof (struct IPv4HttpAddress))
1757 address = GNUNET_malloc (INET_ADDRSTRLEN);
1759 a4.sin_addr.s_addr = t4->ipv4_addr;
1760 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1761 port = ntohs(t4->u_port);
1765 /* invalid address */
1766 GNUNET_break_op (0);
1767 asc (asc_cls, NULL);
1770 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1771 GNUNET_free (address);
1772 GNUNET_assert(res != 0);
1780 * Another peer has suggested an address for this
1781 * peer and transport plugin. Check that this could be a valid
1782 * address. If so, consider adding it to the list
1785 * @param cls closure
1786 * @param addr pointer to the address
1787 * @param addrlen length of addr
1788 * @return GNUNET_OK if this is a plausible address for this peer
1792 http_plugin_address_suggested (void *cls,
1793 const void *addr, size_t addrlen)
1795 struct Plugin *plugin = cls;
1796 struct IPv4HttpAddress *v4;
1797 struct IPv6HttpAddress *v6;
1800 GNUNET_assert(cls !=NULL);
1801 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1802 (addrlen != sizeof (struct IPv6HttpAddress)))
1804 return GNUNET_SYSERR;
1806 if (addrlen == sizeof (struct IPv4HttpAddress))
1808 v4 = (struct IPv4HttpAddress *) addr;
1809 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1811 return GNUNET_SYSERR;
1813 port = ntohs (v4->u_port);
1814 if (port != plugin->port_inbound)
1816 return GNUNET_SYSERR;
1821 v6 = (struct IPv6HttpAddress *) addr;
1822 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1824 return GNUNET_SYSERR;
1826 port = ntohs (v6->u6_port);
1827 if (port != plugin->port_inbound)
1829 return GNUNET_SYSERR;
1837 * Function called for a quick conversion of the binary address to
1838 * a numeric address. Note that the caller must not free the
1839 * address and that the next call to this function is allowed
1840 * to override the address again.
1842 * @param cls closure
1843 * @param addr binary address
1844 * @param addrlen length of the address
1845 * @return string representing the same address
1848 http_plugin_address_to_string (void *cls,
1852 const struct IPv4HttpAddress *t4;
1853 const struct IPv6HttpAddress *t6;
1854 struct sockaddr_in a4;
1855 struct sockaddr_in6 a6;
1861 if (addrlen == sizeof (struct IPv6HttpAddress))
1863 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1865 a6.sin6_addr = t6->ipv6_addr;
1866 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1867 port = ntohs(t6->u6_port);
1869 else if (addrlen == sizeof (struct IPv4HttpAddress))
1871 address = GNUNET_malloc (INET_ADDRSTRLEN);
1873 a4.sin_addr.s_addr = t4->ipv4_addr;
1874 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1875 port = ntohs(t4->u_port);
1879 /* invalid address */
1882 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
1883 GNUNET_free (address);
1884 GNUNET_assert(res != 0);
1889 * Add the IP of our network interface to the list of
1890 * our external IP addresses.
1892 * @param cls the 'struct Plugin*'
1893 * @param name name of the interface
1894 * @param isDefault do we think this may be our default interface
1895 * @param addr address of the interface
1896 * @param addrlen number of bytes in addr
1897 * @return GNUNET_OK to continue iterating
1900 process_interfaces (void *cls,
1903 const struct sockaddr *addr, socklen_t addrlen)
1905 struct Plugin *plugin = cls;
1906 struct IPv4HttpAddress * t4;
1907 struct IPv6HttpAddress * t6;
1910 GNUNET_assert(cls !=NULL);
1911 af = addr->sa_family;
1914 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
1915 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1917 /* skip loopback addresses */
1920 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1921 t4->u_port = htons (plugin->port_inbound);
1922 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
1925 else if (af == AF_INET6)
1927 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
1928 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1930 /* skip link local addresses */
1933 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1935 /* skip loopback addresses */
1938 memcpy (&t6->ipv6_addr,
1939 &((struct sockaddr_in6 *) addr)->sin6_addr,
1940 sizeof (struct in6_addr));
1941 t6->u6_port = htons (plugin->port_inbound);
1942 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
1946 int hashMapFreeIterator (void *cls, const GNUNET_HashCode *key, void *value)
1948 struct Session * cs = value;
1949 struct HTTP_Connection * con = cs->outbound_connections_head;
1950 struct HTTP_Connection * tmp_con = cs->outbound_connections_head;
1951 struct HTTP_Message * msg = NULL;
1952 struct HTTP_Message * tmp_msg = NULL;
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session for peer `%s'\n",GNUNET_i2s(&cs->identity));
1956 /* freeing connections */
1959 GNUNET_free(con->url);
1960 if (con->put_curl_handle!=NULL)
1961 curl_easy_cleanup(con->put_curl_handle);
1962 con->put_curl_handle = NULL;
1963 msg = con->pending_msgs_head;
1974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"All sessions freed \n");
1981 * Exit point from the plugin.
1984 libgnunet_plugin_transport_http_done (void *cls)
1986 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1987 struct Plugin *plugin = api->cls;
1990 GNUNET_assert(cls !=NULL);
1993 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1995 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1996 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1999 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2001 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2002 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2005 if ( plugin->http_server_task_send != GNUNET_SCHEDULER_NO_TASK)
2007 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_send);
2008 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
2011 if (plugin->http_server_daemon_v4 != NULL)
2013 MHD_stop_daemon (plugin->http_server_daemon_v4);
2014 plugin->http_server_daemon_v4 = NULL;
2016 if (plugin->http_server_daemon_v6 != NULL)
2018 MHD_stop_daemon (plugin->http_server_daemon_v6);
2019 plugin->http_server_daemon_v6 = NULL;
2022 /* free all sessions */
2023 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
2024 &hashMapFreeIterator,
2027 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2028 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2030 mret = curl_multi_cleanup(plugin->multi_handle);
2031 if ( CURLM_OK != mret)
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
2034 GNUNET_free (plugin);
2036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2042 * Entry point for the plugin.
2045 libgnunet_plugin_transport_http_init (void *cls)
2047 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2048 struct Plugin *plugin;
2049 struct GNUNET_TRANSPORT_PluginFunctions *api;
2050 struct GNUNET_TIME_Relative gn_timeout;
2051 long long unsigned int port;
2053 GNUNET_assert(cls !=NULL);
2054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
2056 plugin = GNUNET_malloc (sizeof (struct Plugin));
2058 plugin->sessions = NULL;
2060 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2062 api->send = &http_plugin_send;
2063 api->disconnect = &http_plugin_disconnect;
2064 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2065 api->check_address = &http_plugin_address_suggested;
2066 api->address_to_string = &http_plugin_address_to_string;
2068 /* Hashing our identity to use it in URLs */
2069 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2071 /* Reading port number from config file */
2073 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2079 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2082 ("Require valid port number for transport plugin `%s' in configuration!\n"),
2084 libgnunet_plugin_transport_http_done (api);
2087 GNUNET_assert ((port > 0) && (port <= 65535));
2088 plugin->port_inbound = port;
2089 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2090 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->http_server_daemon_v6 == NULL) && (port != 0))
2092 plugin->http_server_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
2094 &acceptPolicyCallback,
2095 plugin , &accessHandlerCallback, plugin,
2096 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
2097 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
2098 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
2099 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2100 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
2102 plugin->http_server_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
2104 &acceptPolicyCallback,
2105 plugin , &accessHandlerCallback, plugin,
2106 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
2107 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
2108 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
2109 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2110 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
2113 if (plugin->http_server_daemon_v4 != NULL)
2114 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2115 if (plugin->http_server_daemon_v6 != NULL)
2116 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2118 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
2120 else if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
2124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
2125 libgnunet_plugin_transport_http_done (api);
2129 /* Initializing cURL */
2130 curl_global_init(CURL_GLOBAL_ALL);
2131 plugin->multi_handle = curl_multi_init();
2133 if ( NULL == plugin->multi_handle )
2135 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2137 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2139 libgnunet_plugin_transport_http_done (api);
2143 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
2144 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2145 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2150 /* end of plugin_transport_http.c */