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_Session * ps = stream;
1067 size_t len = size * nmemb;
1068 long http_result = 0;
1071 /* Getting last http result code */
1072 if (ps->recv_connected==GNUNET_NO)
1074 GNUNET_assert(NULL!=ps);
1075 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1076 if (CURLE_OK == res)
1078 if (http_result == 200)
1080 ps->recv_connected = GNUNET_YES;
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connected\n",ps);
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_Session * ps = ptr;
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes recieved\n",ps, size*nmemb);
1182 GNUNET_SERVER_mst_receive(ps->msgtok, ps, 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_Session *ps = NULL;
1326 struct HTTP_PeerContext *pc = 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 *) &ps);
1349 GNUNET_assert ( ps != NULL );
1350 pc = ps->peercontext;
1351 GNUNET_assert ( pc != NULL );
1356 if ( (msg->data.result != CURLE_OK) &&
1357 (msg->data.result != CURLE_GOT_NOTHING) )
1359 /* sending msg failed*/
1360 if (msg->easy_handle == ps->send_endpoint)
1362 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1363 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1365 GNUNET_i2s(&pc->identity),
1366 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1367 "curl_multi_perform",
1368 curl_easy_strerror (msg->data.result));
1370 ps->send_connected = GNUNET_NO;
1371 curl_easy_cleanup(ps->send_endpoint);
1372 ps->send_endpoint=NULL;
1373 if (( NULL != ps->pending_msgs_tail) && ( NULL != ps->pending_msgs_tail->transmit_cont))
1374 ps->pending_msgs_tail->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1376 /* GET connection failed */
1377 if (msg->easy_handle == ps->recv_endpoint)
1379 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1380 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1382 GNUNET_i2s(&pc->identity),
1383 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1384 "curl_multi_perform",
1385 curl_easy_strerror (msg->data.result));
1386 ps->recv_connected = GNUNET_NO;
1387 curl_easy_cleanup(ps->recv_endpoint);
1388 ps->recv_endpoint=NULL;
1393 if (msg->easy_handle == ps->send_endpoint)
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(&pc->identity),
1400 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1403 /* Calling transmit continuation */
1404 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
1406 /* HTTP 1xx : Last message before here was informational */
1407 if ((http_result >=100) && (http_result < 200))
1408 ps->pending_msgs_tail->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&pc->identity,GNUNET_OK);
1409 /* HTTP 2xx: successful operations */
1410 if ((http_result >=200) && (http_result < 300))
1411 ps->pending_msgs_tail->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&pc->identity,GNUNET_OK);
1412 /* HTTP 3xx..5xx: error */
1413 if ((http_result >=300) && (http_result < 600))
1414 ps->pending_msgs_tail->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1416 ps->send_connected = GNUNET_NO;
1417 curl_easy_cleanup(ps->send_endpoint);
1418 ps->send_endpoint =NULL;
1420 if (msg->easy_handle == ps->recv_endpoint)
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(&pc->identity),
1426 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1429 ps->recv_connected = GNUNET_NO;
1430 curl_easy_cleanup(ps->recv_endpoint);
1431 ps->recv_endpoint=NULL;
1434 if (ps->pending_msgs_tail != NULL)
1436 if (ps->pending_msgs_tail->pos>0)
1437 remove_http_message(ps, ps->pending_msgs_tail);
1445 while ( (running > 0) );
1447 handles_last_run = running;
1449 while (mret == CURLM_CALL_MULTI_PERFORM);
1450 send_schedule(plugin, cls);
1455 * Function setting up file descriptors and scheduling task to run
1456 * @param ses session to send data to
1457 * @return bytes sent to peer
1459 static size_t send_schedule(void *cls, struct Session* ses )
1461 struct Plugin *plugin = cls;
1466 struct GNUNET_NETWORK_FDSet *grs;
1467 struct GNUNET_NETWORK_FDSet *gws;
1471 GNUNET_assert(cls !=NULL);
1476 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1477 if (mret != CURLM_OK)
1479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1480 _("%s failed at %s:%d: `%s'\n"),
1481 "curl_multi_fdset", __FILE__, __LINE__,
1482 curl_multi_strerror (mret));
1485 mret = curl_multi_timeout (plugin->multi_handle, &to);
1486 if (mret != CURLM_OK)
1488 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1489 _("%s failed at %s:%d: `%s'\n"),
1490 "curl_multi_timeout", __FILE__, __LINE__,
1491 curl_multi_strerror (mret));
1495 grs = GNUNET_NETWORK_fdset_create ();
1496 gws = GNUNET_NETWORK_fdset_create ();
1497 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1498 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1499 plugin->http_server_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1500 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1501 GNUNET_SCHEDULER_NO_TASK,
1502 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1507 GNUNET_NETWORK_fdset_destroy (gws);
1508 GNUNET_NETWORK_fdset_destroy (grs);
1510 /* FIXME: return bytes REALLY sent */
1516 * Function that can be used by the transport service to transmit
1517 * a message using the plugin. Note that in the case of a
1518 * peer disconnecting, the continuation MUST be called
1519 * prior to the disconnect notification itself. This function
1520 * will be called with this peer's HELLO message to initiate
1521 * a fresh connection to another peer.
1523 * @param cls closure
1524 * @param target who should receive this message
1525 * @param msgbuf the message to transmit
1526 * @param msgbuf_size number of bytes in 'msgbuf'
1527 * @param priority how important is the message (most plugins will
1528 * ignore message priority and just FIFO)
1529 * @param timeout how long to wait at most for the transmission (does not
1530 * require plugins to discard the message after the timeout,
1531 * just advisory for the desired delay; most plugins will ignore
1533 * @param session which session must be used (or NULL for "any")
1534 * @param addr the address to use (can be NULL if the plugin
1535 * is "on its own" (i.e. re-use existing TCP connection))
1536 * @param addrlen length of the address in bytes
1537 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1538 * GNUNET_NO means the plugin may use any other address and
1539 * GNUNET_SYSERR means that only reliable existing
1540 * bi-directional connections should be used (regardless
1542 * @param cont continuation to call once the message has
1543 * been transmitted (or if the transport is ready
1544 * for the next transmission call; or if the
1545 * peer disconnected...); can be NULL
1546 * @param cont_cls closure for cont
1547 * @return number of bytes used (on the physical network, with overheads);
1548 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1549 * and does NOT mean that the message was not transmitted (DV)
1552 http_plugin_send (void *cls,
1553 const struct GNUNET_PeerIdentity *target,
1556 unsigned int priority,
1557 struct GNUNET_TIME_Relative to,
1558 struct Session *session,
1562 GNUNET_TRANSPORT_TransmitContinuation cont,
1565 struct Plugin *plugin = cls;
1566 //struct Session *cs;
1567 struct HTTP_Message *msg;
1568 //struct HTTP_Connection *con;
1571 struct HTTP_PeerContext * pc;
1572 struct HTTP_Session * ps;
1574 GNUNET_assert(cls !=NULL);
1575 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
1577 /* get session from hashmap */
1578 //cs = session_get(plugin, target);
1579 //con = session_check_outbound_address(plugin, cs, addr, addrlen);
1582 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: CREATING NEW PEER CONTEXT\n");
1587 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1588 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1589 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: PEER CONTEXT FOUND\n");
1595 ps = get_HTTP_Session(plugin, pc, addr, addrlen);
1596 /* session not existing, but address forced -> creating new session */
1597 if ((ps==NULL) && (force_address == GNUNET_YES))
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: CREATING NEW SESSION %s\n",http_plugin_address_to_string(NULL, addr, addrlen));
1600 ps = GNUNET_malloc(sizeof (struct HTTP_Session));
1601 ps->addr = GNUNET_malloc(addrlen);
1602 memcpy(ps->addr,addr,addrlen);
1603 ps->addrlen = addrlen;
1604 ps->direction=OUTBOUND;
1605 ps->recv_connected = GNUNET_NO;
1606 ps->send_connected = GNUNET_NO;
1607 ps->pending_msgs_head = NULL;
1608 ps->pending_msgs_tail = NULL;
1609 ps->url = create_url (plugin, ps->addr, ps->addrlen);
1610 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1612 /* session not existing, address not forced -> looking for other session */
1613 if ((ps==NULL) && (force_address == GNUNET_NO))
1615 /* FIXME: CREATING SESSION, SHOULD CHOOSE EXISTING */
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: CREATING NEW SESSION %s\n",http_plugin_address_to_string(NULL, addr, addrlen));
1617 ps = GNUNET_malloc(sizeof (struct HTTP_Session));
1618 ps->addr = GNUNET_malloc(addrlen);
1619 memcpy(ps->addr,addr,addrlen);
1620 ps->addrlen = addrlen;
1621 ps->direction=OUTBOUND;
1622 ps->recv_connected = GNUNET_NO;
1623 ps->send_connected = GNUNET_NO;
1624 ps->pending_msgs_head = NULL;
1625 ps->pending_msgs_tail = NULL;
1626 ps->url = create_url (plugin, ps->addr, ps->addrlen);
1627 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1629 if ((ps==NULL) && (force_address == GNUNET_SYSERR))
1631 /* FIXME: CREATING SESSION, SHOULD CHOOSE EXISTING */
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"SEND: CREATING NEW SESSION %s\n",http_plugin_address_to_string(NULL, addr, addrlen));
1633 ps = GNUNET_malloc(sizeof (struct HTTP_Session));
1634 ps->addr = GNUNET_malloc(addrlen);
1635 memcpy(ps->addr,addr,addrlen);
1636 ps->addrlen = addrlen;
1637 ps->direction=OUTBOUND;
1638 ps->recv_connected = GNUNET_NO;
1639 ps->send_connected = GNUNET_NO;
1640 ps->pending_msgs_head = NULL;
1641 ps->pending_msgs_tail = NULL;
1642 ps->url = create_url (plugin, ps->addr, ps->addrlen);
1643 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1646 char * force = GNUNET_malloc(30);
1647 if (force_address == GNUNET_YES)
1648 strcpy(force,"forced addr.");
1649 if (force_address == GNUNET_NO)
1650 strcpy(force,"any addr.");
1651 if (force_address == GNUNET_SYSERR)
1652 strcpy(force,"reliable bi-direc. address addr.");
1653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' %s (%s), session: %X\n",
1655 GNUNET_i2s(&pc->identity),
1657 http_plugin_address_to_string(NULL, addr, addrlen),
1662 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1664 msg->size = msgbuf_size;
1666 msg->buf = (char *) &msg[1];
1667 msg->transmit_cont = cont;
1668 msg->transmit_cont_cls = cont_cls;
1669 memcpy (msg->buf,msgbuf, msgbuf_size);
1670 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1672 return send_check_connections (plugin, session, ps);
1678 * Function that can be used to force the plugin to disconnect
1679 * from the given peer and cancel all previous transmissions
1680 * (and their continuationc).
1682 * @param cls closure
1683 * @param target peer from which to disconnect
1686 http_plugin_disconnect (void *cls,
1687 const struct GNUNET_PeerIdentity *target)
1689 struct Plugin *plugin = cls;
1690 struct HTTP_Connection *con;
1693 /* get session from hashmap */
1694 cs = session_get(plugin, target);
1695 con = cs->outbound_connections_head;
1699 if (con->put_curl_handle!=NULL)
1700 curl_easy_cleanup(con->put_curl_handle);
1701 con->put_curl_handle=NULL;
1702 con->put_connected = GNUNET_NO;
1703 while (con->pending_msgs_head!=NULL)
1705 remove_http_message(con, con->pending_msgs_head);
1713 * Convert the transports address to a nice, human-readable
1716 * @param cls closure
1717 * @param type name of the transport that generated the address
1718 * @param addr one of the addresses of the host, NULL for the last address
1719 * the specific address format depends on the transport
1720 * @param addrlen length of the address
1721 * @param numeric should (IP) addresses be displayed in numeric form?
1722 * @param timeout after how long should we give up?
1723 * @param asc function to call on each string
1724 * @param asc_cls closure for asc
1727 http_plugin_address_pretty_printer (void *cls,
1732 struct GNUNET_TIME_Relative timeout,
1733 GNUNET_TRANSPORT_AddressStringCallback
1736 const struct IPv4HttpAddress *t4;
1737 const struct IPv6HttpAddress *t6;
1738 struct sockaddr_in a4;
1739 struct sockaddr_in6 a6;
1745 GNUNET_assert(cls !=NULL);
1746 if (addrlen == sizeof (struct IPv6HttpAddress))
1748 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1750 a6.sin6_addr = t6->ipv6_addr;
1751 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1752 port = ntohs(t6->u6_port);
1754 else if (addrlen == sizeof (struct IPv4HttpAddress))
1756 address = GNUNET_malloc (INET_ADDRSTRLEN);
1758 a4.sin_addr.s_addr = t4->ipv4_addr;
1759 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1760 port = ntohs(t4->u_port);
1764 /* invalid address */
1765 GNUNET_break_op (0);
1766 asc (asc_cls, NULL);
1769 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1770 GNUNET_free (address);
1771 GNUNET_assert(res != 0);
1779 * Another peer has suggested an address for this
1780 * peer and transport plugin. Check that this could be a valid
1781 * address. If so, consider adding it to the list
1784 * @param cls closure
1785 * @param addr pointer to the address
1786 * @param addrlen length of addr
1787 * @return GNUNET_OK if this is a plausible address for this peer
1791 http_plugin_address_suggested (void *cls,
1792 const void *addr, size_t addrlen)
1794 struct Plugin *plugin = cls;
1795 struct IPv4HttpAddress *v4;
1796 struct IPv6HttpAddress *v6;
1799 GNUNET_assert(cls !=NULL);
1800 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1801 (addrlen != sizeof (struct IPv6HttpAddress)))
1803 return GNUNET_SYSERR;
1805 if (addrlen == sizeof (struct IPv4HttpAddress))
1807 v4 = (struct IPv4HttpAddress *) addr;
1808 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1810 return GNUNET_SYSERR;
1812 port = ntohs (v4->u_port);
1813 if (port != plugin->port_inbound)
1815 return GNUNET_SYSERR;
1820 v6 = (struct IPv6HttpAddress *) addr;
1821 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1823 return GNUNET_SYSERR;
1825 port = ntohs (v6->u6_port);
1826 if (port != plugin->port_inbound)
1828 return GNUNET_SYSERR;
1836 * Function called for a quick conversion of the binary address to
1837 * a numeric address. Note that the caller must not free the
1838 * address and that the next call to this function is allowed
1839 * to override the address again.
1841 * @param cls closure
1842 * @param addr binary address
1843 * @param addrlen length of the address
1844 * @return string representing the same address
1847 http_plugin_address_to_string (void *cls,
1851 const struct IPv4HttpAddress *t4;
1852 const struct IPv6HttpAddress *t6;
1853 struct sockaddr_in a4;
1854 struct sockaddr_in6 a6;
1860 if (addrlen == sizeof (struct IPv6HttpAddress))
1862 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1864 a6.sin6_addr = t6->ipv6_addr;
1865 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1866 port = ntohs(t6->u6_port);
1868 else if (addrlen == sizeof (struct IPv4HttpAddress))
1870 address = GNUNET_malloc (INET_ADDRSTRLEN);
1872 a4.sin_addr.s_addr = t4->ipv4_addr;
1873 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1874 port = ntohs(t4->u_port);
1878 /* invalid address */
1881 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
1882 GNUNET_free (address);
1883 GNUNET_assert(res != 0);
1888 * Add the IP of our network interface to the list of
1889 * our external IP addresses.
1891 * @param cls the 'struct Plugin*'
1892 * @param name name of the interface
1893 * @param isDefault do we think this may be our default interface
1894 * @param addr address of the interface
1895 * @param addrlen number of bytes in addr
1896 * @return GNUNET_OK to continue iterating
1899 process_interfaces (void *cls,
1902 const struct sockaddr *addr, socklen_t addrlen)
1904 struct Plugin *plugin = cls;
1905 struct IPv4HttpAddress * t4;
1906 struct IPv6HttpAddress * t6;
1909 GNUNET_assert(cls !=NULL);
1910 af = addr->sa_family;
1913 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
1914 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1916 /* skip loopback addresses */
1919 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1920 t4->u_port = htons (plugin->port_inbound);
1921 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
1924 else if (af == AF_INET6)
1926 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
1927 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1929 /* skip link local addresses */
1932 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1934 /* skip loopback addresses */
1937 memcpy (&t6->ipv6_addr,
1938 &((struct sockaddr_in6 *) addr)->sin6_addr,
1939 sizeof (struct in6_addr));
1940 t6->u6_port = htons (plugin->port_inbound);
1941 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
1945 int hashMapFreeIterator (void *cls, const GNUNET_HashCode *key, void *value)
1947 struct Session * cs = value;
1948 struct HTTP_Connection * con = cs->outbound_connections_head;
1949 struct HTTP_Connection * tmp_con = cs->outbound_connections_head;
1950 struct HTTP_Message * msg = NULL;
1951 struct HTTP_Message * tmp_msg = NULL;
1953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session for peer `%s'\n",GNUNET_i2s(&cs->identity));
1955 /* freeing connections */
1958 GNUNET_free(con->url);
1959 if (con->put_curl_handle!=NULL)
1960 curl_easy_cleanup(con->put_curl_handle);
1961 con->put_curl_handle = NULL;
1962 msg = con->pending_msgs_head;
1973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"All sessions freed \n");
1980 * Exit point from the plugin.
1983 libgnunet_plugin_transport_http_done (void *cls)
1985 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1986 struct Plugin *plugin = api->cls;
1989 GNUNET_assert(cls !=NULL);
1992 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1994 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1995 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1998 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2000 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2001 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2004 if ( plugin->http_server_task_send != GNUNET_SCHEDULER_NO_TASK)
2006 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_send);
2007 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
2010 if (plugin->http_server_daemon_v4 != NULL)
2012 MHD_stop_daemon (plugin->http_server_daemon_v4);
2013 plugin->http_server_daemon_v4 = NULL;
2015 if (plugin->http_server_daemon_v6 != NULL)
2017 MHD_stop_daemon (plugin->http_server_daemon_v6);
2018 plugin->http_server_daemon_v6 = NULL;
2021 /* free all sessions */
2022 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
2023 &hashMapFreeIterator,
2026 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2027 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2029 mret = curl_multi_cleanup(plugin->multi_handle);
2030 if ( CURLM_OK != mret)
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
2033 GNUNET_free (plugin);
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2041 * Entry point for the plugin.
2044 libgnunet_plugin_transport_http_init (void *cls)
2046 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2047 struct Plugin *plugin;
2048 struct GNUNET_TRANSPORT_PluginFunctions *api;
2049 struct GNUNET_TIME_Relative gn_timeout;
2050 long long unsigned int port;
2052 GNUNET_assert(cls !=NULL);
2053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
2055 plugin = GNUNET_malloc (sizeof (struct Plugin));
2057 plugin->sessions = NULL;
2059 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2061 api->send = &http_plugin_send;
2062 api->disconnect = &http_plugin_disconnect;
2063 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2064 api->check_address = &http_plugin_address_suggested;
2065 api->address_to_string = &http_plugin_address_to_string;
2067 /* Hashing our identity to use it in URLs */
2068 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2070 /* Reading port number from config file */
2072 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2078 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2081 ("Require valid port number for transport plugin `%s' in configuration!\n"),
2083 libgnunet_plugin_transport_http_done (api);
2086 GNUNET_assert ((port > 0) && (port <= 65535));
2087 plugin->port_inbound = port;
2088 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2089 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->http_server_daemon_v6 == NULL) && (port != 0))
2091 plugin->http_server_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
2093 &acceptPolicyCallback,
2094 plugin , &accessHandlerCallback, plugin,
2095 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
2096 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
2097 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
2098 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2099 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
2101 plugin->http_server_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
2103 &acceptPolicyCallback,
2104 plugin , &accessHandlerCallback, plugin,
2105 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
2106 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
2107 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
2108 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2109 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
2112 if (plugin->http_server_daemon_v4 != NULL)
2113 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2114 if (plugin->http_server_daemon_v6 != NULL)
2115 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2117 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
2119 else if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
2124 libgnunet_plugin_transport_http_done (api);
2128 /* Initializing cURL */
2129 curl_global_init(CURL_GLOBAL_ALL);
2130 plugin->multi_handle = curl_multi_init();
2132 if ( NULL == plugin->multi_handle )
2134 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2136 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2138 libgnunet_plugin_transport_http_done (api);
2142 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
2143 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2144 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2149 /* end of plugin_transport_http.c */