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;
158 * Pointer to the global plugin struct.
160 struct Plugin *plugin;
162 struct Session * head;
163 struct Session * tail;
172 struct SessionHeader header;
175 * next session in linked list
177 struct Session * next;
180 * previous session in linked list
182 struct Session * prev;
185 * address of this session
200 * Message queue for outbound messages
203 struct HTTP_Message * pending_msgs_head;
206 * Message queue for outbound messages
209 struct HTTP_Message * pending_msgs_tail;
212 * partner peer this connection belongs to
214 struct HTTP_PeerContext * peercontext;
217 * message stream tokenizer for incoming data
219 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
223 * outbound: OUTBOUND (GNUNET_YES)
224 * inbound : INBOUND (GNUNET_NO)
226 unsigned int direction;
229 * is session connected to send data?
231 unsigned int send_connected;
234 * is send connection active?
236 unsigned int send_active;
239 * is session connected to receive data?
241 unsigned int recv_connected;
244 * is receive connection active?
246 unsigned int recv_active;
249 * entity managing sending data
250 * outbound session: pointer to curl easy handle
252 void * send_endpoint;
255 * entity managing recieving data
256 * outbound session: pointer to curl easy handle
258 void * recv_endpoint;
262 * Encapsulation of all of the state of the plugin.
269 struct GNUNET_TRANSPORT_PluginEnvironment *env;
271 unsigned int port_inbound;
273 struct GNUNET_CONTAINER_MultiHashMap *peers;
276 * Daemon for listening for new IPv4 connections.
278 struct MHD_Daemon *http_server_daemon_v4;
281 * Daemon for listening for new IPv6connections.
283 struct MHD_Daemon *http_server_daemon_v6;
286 * Our primary task for http daemon handling IPv4 connections
288 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
291 * Our primary task for http daemon handling IPv6 connections
293 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
296 * The task sending data
298 GNUNET_SCHEDULER_TaskIdentifier http_server_task_send;
303 CURLM * multi_handle;
306 * Our ASCII encoded, hashed peer identity
307 * This string is used to distinguish between connections and is added to the urls
309 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
314 * Function called for a quick conversion of the binary address to
315 * a numeric address. Note that the caller must not free the
316 * address and that the next call to this function is allowed
317 * to override the address again.
320 * @param addr binary address
321 * @param addrlen length of the address
322 * @return string representing the same address
325 http_plugin_address_to_string (void *cls,
329 static char * create_url(void * cls, const void * addr, size_t addrlen)
331 struct Plugin *plugin = cls;
334 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
335 GNUNET_asprintf(&url,
337 http_plugin_address_to_string(NULL, addr, addrlen),
338 (char *) (&plugin->my_ascii_hash_ident));
344 * Removes a message from the linked list of messages
345 * @param con connection to remove message from
346 * @param msg message to remove
347 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
350 static int remove_http_message(struct Session * ps, struct HTTP_Message * msg)
352 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
357 static struct Session * get_Session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addr_len)
359 struct Session * cc = pc->head;
360 struct Session * con = NULL;
361 unsigned int count = 0;
363 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
366 if (addr_len == cc->addrlen)
368 if (0 == memcmp(cc->addr, addr, addr_len))
382 * Callback called by MHD when a connection is terminated
384 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
386 struct Session * ps = *httpSessionCache;
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&ps->peercontext->identity));
390 /* session set to inactive */
392 //con->is_bad_request = GNUNET_NO;
395 static void mhd_write_mst_cb (void *cls,
397 const struct GNUNET_MessageHeader *message)
400 struct Session *ps = cls;
401 struct HTTP_PeerContext *pc = ps->peercontext;
402 GNUNET_assert(ps != NULL);
403 GNUNET_assert(pc != NULL);
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
407 ntohs(message->type),
408 ntohs(message->size),
409 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
411 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
419 static void curl_write_mst_cb (void *cls,
421 const struct GNUNET_MessageHeader *message)
423 struct Session *ps = cls;
424 struct HTTP_PeerContext *pc = ps->peercontext;
425 GNUNET_assert(ps != NULL);
426 GNUNET_assert(pc != NULL);
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
430 ntohs(message->type),
431 ntohs(message->size),
432 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
434 pc->plugin->env->receive (pc->plugin->env->cls,
444 * Check if ip is allowed to connect.
447 acceptPolicyCallback (void *cls,
448 const struct sockaddr *addr, socklen_t addr_len)
451 struct Plugin *plugin = cls;
453 /* Every connection is accepted, nothing more to do here */
457 int server_read_callback (void *cls, uint64_t pos, char *buf, int max)
461 struct Session * ps = cls;
462 struct HTTP_PeerContext * pc;
463 struct HTTP_Message * msg;
466 GNUNET_assert (ps!=NULL);
467 pc = ps->peercontext;
468 msg = ps->pending_msgs_tail;
472 if ((msg->size-msg->pos) <= max)
474 memcpy(buf,&msg->buf[pos],(msg->size-msg->pos));
475 pos+=(msg->size-msg->pos);
479 memcpy(buf,&msg->buf[pos],max);
483 if (msg->pos==msg->size)
485 if (NULL!=msg->transmit_cont)
486 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
487 res = remove_http_message(ps,msg);
494 * Process GET or PUT request received via MHD. For
495 * GET, queue response that will send back our pending
496 * messages. For PUT, process incoming data and send
497 * to GNUnet core. In either case, check if a session
498 * already exists and create a new one if not.
501 accessHandlerCallback (void *cls,
502 struct MHD_Connection *mhd_connection,
506 const char *upload_data,
507 size_t * upload_data_size, void **httpSessionCache)
509 struct Plugin *plugin = cls;
510 struct MHD_Response *response;
511 const union MHD_ConnectionInfo * conn_info;
513 struct sockaddr_in *addrin;
514 struct sockaddr_in6 *addrin6;
516 char address[INET6_ADDRSTRLEN+14];
517 struct GNUNET_PeerIdentity pi_in;
519 struct IPv4HttpAddress ipv4addr;
520 struct IPv6HttpAddress ipv6addr;
522 struct HTTP_PeerContext *pc;
526 int send_error_to_client;
530 GNUNET_assert(cls !=NULL);
531 send_error_to_client = GNUNET_NO;
533 if (NULL == *httpSessionCache)
535 /* check url for peer identity , if invalid send HTTP 404*/
536 res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
537 if ( GNUNET_SYSERR == res )
539 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
540 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
541 MHD_destroy_response (response);
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
551 ps = *httpSessionCache;
552 pc = ps->peercontext;
555 if (NULL == *httpSessionCache)
557 /* get peer context */
558 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
562 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
564 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
565 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
568 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
569 /* Incoming IPv4 connection */
570 if ( AF_INET == conn_info->client_addr->sin_family)
572 addrin = conn_info->client_addr;
573 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
574 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
575 ipv4addr.u_port = addrin->sin_port;
577 addr_len = sizeof(struct IPv4HttpAddress);
578 //con = session_check_inbound_address (plugin, cs, (const void *) &ipv4addr, sizeof (struct IPv4HttpAddress));
580 /* Incoming IPv6 connection */
581 if ( AF_INET6 == conn_info->client_addr->sin_family)
583 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
584 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
585 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
586 ipv6addr.u6_port = addrin6->sin6_port;
588 addr_len = sizeof(struct IPv6HttpAddress);
590 /* Set closure and update current session*/
591 ps = get_Session(plugin, pc, addr, addr_len);
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"RECV: CREATING NEW SESSION %s\n",http_plugin_address_to_string(NULL, addr, addr_len));
595 ps = GNUNET_malloc(sizeof (struct Session));
596 ps->addr = GNUNET_malloc(addr_len);
597 memcpy(ps->addr,addr,addr_len);
598 ps->addrlen = addr_len;
599 ps->direction=INBOUND;
600 ps->pending_msgs_head = NULL;
601 ps->pending_msgs_tail = NULL;
602 ps->send_connected=GNUNET_NO;
603 ps->send_active=GNUNET_NO;
604 ps->recv_connected=GNUNET_NO;
605 ps->recv_active=GNUNET_NO;
607 ps->url = create_url (plugin, ps->addr, ps->addrlen);
608 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"RECV: SESSION CONTEXT FOUND\n");
615 *httpSessionCache = ps;
616 if (ps->msgtok==NULL)
617 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
621 GNUNET_i2s(&pc->identity),
622 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
625 /* Is it a PUT or a GET request */
626 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
628 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
630 ps->recv_active = GNUNET_YES;
634 /* Transmission of all data complete */
635 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
637 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
638 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
640 MHD_destroy_response (response);
641 ps->recv_active=GNUNET_NO;
646 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
648 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
649 (*upload_data_size) = 0;
655 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
657 response = MHD_create_response_from_callback(-1,32 * 1024, &server_read_callback, ps, NULL);
658 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
659 MHD_destroy_response (response);
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
663 GNUNET_i2s(&pc->identity),
664 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
672 * Call MHD to process pending ipv4 requests and then go back
673 * and schedule the next run.
675 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
677 * Call MHD to process pending ipv6 requests and then go back
678 * and schedule the next run.
680 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
683 * Function that queries MHD's select sets and
684 * starts the task waiting for them.
686 static GNUNET_SCHEDULER_TaskIdentifier
687 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
689 struct Plugin *plugin = cls;
690 GNUNET_SCHEDULER_TaskIdentifier ret;
694 struct GNUNET_NETWORK_FDSet *wrs;
695 struct GNUNET_NETWORK_FDSet *wws;
696 struct GNUNET_NETWORK_FDSet *wes;
698 unsigned long long timeout;
700 struct GNUNET_TIME_Relative tv;
702 GNUNET_assert(cls !=NULL);
703 ret = GNUNET_SCHEDULER_NO_TASK;
707 wrs = GNUNET_NETWORK_fdset_create ();
708 wes = GNUNET_NETWORK_fdset_create ();
709 wws = GNUNET_NETWORK_fdset_create ();
711 GNUNET_assert (MHD_YES ==
712 MHD_get_fdset (daemon_handle,
717 haveto = MHD_get_timeout (daemon_handle, &timeout);
718 if (haveto == MHD_YES)
719 tv.value = (uint64_t) timeout;
721 tv = GNUNET_TIME_UNIT_FOREVER_REL;
722 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
723 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
724 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
725 if (daemon_handle == plugin->http_server_daemon_v4)
727 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
728 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
729 GNUNET_SCHEDULER_NO_TASK,
733 &http_server_daemon_v4_run,
736 if (daemon_handle == plugin->http_server_daemon_v6)
738 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
739 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
740 GNUNET_SCHEDULER_NO_TASK,
744 &http_server_daemon_v6_run,
747 GNUNET_NETWORK_fdset_destroy (wrs);
748 GNUNET_NETWORK_fdset_destroy (wws);
749 GNUNET_NETWORK_fdset_destroy (wes);
754 * Call MHD to process pending requests and then go back
755 * and schedule the next run.
757 static void http_server_daemon_v4_run (void *cls,
758 const struct GNUNET_SCHEDULER_TaskContext *tc)
760 struct Plugin *plugin = cls;
762 GNUNET_assert(cls !=NULL);
763 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
764 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
766 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
769 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
770 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
776 * Call MHD to process pending requests and then go back
777 * and schedule the next run.
779 static void http_server_daemon_v6_run (void *cls,
780 const struct GNUNET_SCHEDULER_TaskContext *tc)
782 struct Plugin *plugin = cls;
784 GNUNET_assert(cls !=NULL);
785 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
786 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
788 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
791 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
792 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
797 * Function setting up curl handle and selecting message to send
799 * @param ses session to send data to
800 * @param con connection
801 * @return bytes sent to peer
803 static ssize_t send_check_connections (void *cls, struct Session* ses , struct Session *ps);
806 static size_t curl_get_header_function( void *ptr, size_t size, size_t nmemb, void *stream)
808 struct Session * ps = stream;
811 size_t len = size * nmemb;
812 long http_result = 0;
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: GET HEADER FUNC\n",ps);
815 /* Getting last http result code */
816 if (ps->recv_connected==GNUNET_NO)
818 GNUNET_assert(NULL!=ps);
819 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
822 if (http_result == 200)
824 ps->recv_connected = GNUNET_YES;
825 ps->recv_active = GNUNET_YES;
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
827 // Calling send_check_connections again since receive is established
828 send_check_connections (ps->peercontext->plugin, NULL, ps);
834 if ((size * nmemb) < SIZE_MAX)
835 tmp = GNUNET_malloc (len+1);
837 if ((tmp != NULL) && (len > 0))
842 if (tmp[len-2] == 13)
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s' %u \n",tmp, http_result);
855 static size_t curl_put_header_function( void *ptr, size_t size, size_t nmemb, void *stream)
857 struct Session * ps = stream;
860 size_t len = size * nmemb;
861 long http_result = 0;
864 /* Getting last http result code */
865 GNUNET_assert(NULL!=ps);
866 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
869 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
871 ps->send_connected = GNUNET_YES;
872 ps->send_active = GNUNET_YES;
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
875 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
877 ps->send_connected = GNUNET_NO;
878 ps->send_active = GNUNET_NO;
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
884 if ((size * nmemb) < SIZE_MAX)
885 tmp = GNUNET_malloc (len+1);
887 if ((tmp != NULL) && (len > 0))
892 if (tmp[len-2] == 13)
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s' %u \n",tmp, http_result);
906 * Callback method used with libcurl
907 * Method is called when libcurl needs to read data during sending
908 * @param stream pointer where to write data
909 * @param size size of an individual element
910 * @param nmemb count of elements that can be written to the buffer
911 * @param ptr source pointer, passed to the libcurl handle
912 * @return bytes written to stream
914 static size_t send_curl_send_callback(void *stream, size_t size, size_t nmemb, void *ptr)
916 struct Session * ps = ptr;
917 struct HTTP_Message * msg = ps->pending_msgs_tail;
921 if (ps->pending_msgs_tail == NULL)
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
924 ps->send_active = GNUNET_NO;
925 return CURL_READFUNC_PAUSE;
928 msg = ps->pending_msgs_tail;
930 if (msg->pos < msg->size)
932 /* data fit in buffer */
933 if ((msg->size - msg->pos) <= (size * nmemb))
935 len = (msg->size - msg->pos);
936 memcpy(stream, &msg->buf[msg->pos], len);
943 memcpy(stream, &msg->buf[msg->pos], len);
948 /* no data to send */
954 if ( msg->pos == msg->size)
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
957 /* Calling transmit continuation */
958 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
959 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
960 remove_http_message(ps, msg);
966 * Callback method used with libcurl
967 * Method is called when libcurl needs to write data during sending
968 * @param stream pointer where to write data
969 * @param size size of an individual element
970 * @param nmemb count of elements that can be written to the buffer
971 * @param ptr destination pointer, passed to the libcurl handle
972 * @return bytes read from stream
974 static size_t send_curl_receive_callback( void *stream, size_t size, size_t nmemb, void *ptr)
976 struct Session * ps = ptr;
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes recieved\n",ps, size*nmemb);
979 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
981 return (size * nmemb);
986 * Function setting up file descriptors and scheduling task to run
988 * @param ses session to send data to
989 * @return bytes sent to peer
991 static size_t send_schedule(void *cls, struct Session* ses );
996 * Function setting up curl handle and selecting message to send
998 * @param ses session to send data to
999 * @param con connection
1000 * @return bytes sent to peer
1002 static ssize_t send_check_connections (void *cls, struct Session* ses , struct Session *ps)
1004 struct Plugin *plugin = cls;
1007 struct HTTP_Message * msg;
1008 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1010 GNUNET_assert(cls !=NULL);
1012 if (ps->direction == OUTBOUND)
1014 /* RECV DIRECTION */
1015 /* Check if session is connected to receive data, otherwise connect to peer */
1016 if (ps->recv_connected == GNUNET_NO)
1018 if (ps->recv_endpoint == NULL)
1020 ps->recv_endpoint = curl_easy_init();
1022 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1024 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1025 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_function);
1026 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1027 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, send_curl_send_callback);
1028 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1029 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, send_curl_receive_callback);
1030 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1031 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1032 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1033 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1034 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1036 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1037 if (mret != CURLM_OK)
1039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1040 _("%s failed at %s:%d: `%s'\n"),
1041 "curl_multi_add_handle", __FILE__, __LINE__,
1042 curl_multi_strerror (mret));
1045 bytes_sent = send_schedule (plugin, NULL);
1046 if (ps->msgtok != NULL)
1047 ps->msgtok = GNUNET_SERVER_mst_create (&curl_write_mst_cb, ps);
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1052 /* waiting for receive direction */
1053 if (ps->recv_connected==GNUNET_NO)
1056 /* SEND DIRECTION */
1057 /* Check if session is connected to send data, otherwise connect to peer */
1058 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1060 if (ps->send_active == GNUNET_YES)
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1065 if (ps->send_active == GNUNET_NO)
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1068 curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT);
1069 ps->send_active=GNUNET_YES;
1073 /* not connected, initiate connection */
1074 if ((ps->send_connected==GNUNET_NO) && (NULL == ps->send_endpoint))
1075 ps->send_endpoint = curl_easy_init();
1076 GNUNET_assert (ps->send_endpoint != NULL);
1077 GNUNET_assert (NULL != ps->pending_msgs_tail);
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1079 ps->send_active = GNUNET_NO;
1080 msg = ps->pending_msgs_tail;
1083 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1085 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1086 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1087 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_function);
1088 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1089 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, send_curl_send_callback);
1090 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1091 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, send_curl_receive_callback);
1092 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1093 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1094 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1095 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1096 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1098 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1099 if (mret != CURLM_OK)
1101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1102 _("%s failed at %s:%d: `%s'\n"),
1103 "curl_multi_add_handle", __FILE__, __LINE__,
1104 curl_multi_strerror (mret));
1107 bytes_sent = send_schedule (plugin, NULL);
1110 if (ps->direction == INBOUND)
1112 GNUNET_assert (NULL != ps->pending_msgs_tail);
1113 msg = ps->pending_msgs_tail;
1114 if ((ps->recv_connected==GNUNET_YES) && (ps->recv_connected==GNUNET_YES))
1115 bytes_sent = msg->size;
1121 static void send_execute (void *cls,
1122 const struct GNUNET_SCHEDULER_TaskContext *tc)
1124 struct Plugin *plugin = cls;
1125 static unsigned int handles_last_run;
1127 struct CURLMsg *msg;
1129 struct Session *ps = NULL;
1130 struct HTTP_PeerContext *pc = NULL;
1131 struct HTTP_Message * cur_msg = NULL;
1134 GNUNET_assert(cls !=NULL);
1135 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
1136 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1142 mret = curl_multi_perform (plugin->multi_handle, &running);
1143 if (running < handles_last_run)
1148 msg = curl_multi_info_read (plugin->multi_handle, &running);
1151 /* get session for affected curl handle */
1152 GNUNET_assert ( msg->easy_handle != NULL );
1153 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char *) &ps);
1154 GNUNET_assert ( ps != NULL );
1155 pc = ps->peercontext;
1156 GNUNET_assert ( pc != NULL );
1161 if ( (msg->data.result != CURLE_OK) &&
1162 (msg->data.result != CURLE_GOT_NOTHING) )
1164 /* sending msg failed*/
1165 if (msg->easy_handle == ps->send_endpoint)
1167 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1168 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1170 GNUNET_i2s(&pc->identity),
1171 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1172 "curl_multi_perform",
1173 curl_easy_strerror (msg->data.result));
1175 ps->send_connected = GNUNET_NO;
1176 ps->send_active = GNUNET_NO;
1177 curl_multi_remove_handle(plugin->multi_handle,msg->easy_handle);
1178 curl_easy_cleanup(ps->send_endpoint);
1179 ps->send_endpoint=NULL;
1180 cur_msg = ps->pending_msgs_tail;
1181 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1182 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1184 /* GET connection failed */
1185 if (msg->easy_handle == ps->recv_endpoint)
1187 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1188 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1190 GNUNET_i2s(&pc->identity),
1191 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1192 "curl_multi_perform",
1193 curl_easy_strerror (msg->data.result));
1194 ps->recv_connected = GNUNET_NO;
1195 ps->recv_active = GNUNET_NO;
1196 curl_multi_remove_handle(plugin->multi_handle,msg->easy_handle);
1197 curl_easy_cleanup(ps->recv_endpoint);
1198 ps->recv_endpoint=NULL;
1203 if (msg->easy_handle == ps->send_endpoint)
1205 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1209 GNUNET_i2s(&pc->identity),
1210 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1213 /* Calling transmit continuation */
1214 cur_msg = ps->pending_msgs_tail;
1215 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1217 /* HTTP 1xx : Last message before here was informational */
1218 if ((http_result >=100) && (http_result < 200))
1219 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1220 /* HTTP 2xx: successful operations */
1221 if ((http_result >=200) && (http_result < 300))
1222 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1223 /* HTTP 3xx..5xx: error */
1224 if ((http_result >=300) && (http_result < 600))
1225 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1227 ps->send_connected = GNUNET_NO;
1228 ps->send_active = GNUNET_NO;
1229 curl_multi_remove_handle(plugin->multi_handle,msg->easy_handle);
1230 curl_easy_cleanup(ps->send_endpoint);
1231 ps->send_endpoint =NULL;
1233 if (msg->easy_handle == ps->recv_endpoint)
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1236 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1238 GNUNET_i2s(&pc->identity),
1239 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1242 ps->recv_connected = GNUNET_NO;
1243 ps->recv_active = GNUNET_NO;
1244 curl_multi_remove_handle(plugin->multi_handle,msg->easy_handle);
1245 curl_easy_cleanup(ps->recv_endpoint);
1246 ps->recv_endpoint=NULL;
1249 if (ps->pending_msgs_tail != NULL)
1251 if (ps->pending_msgs_tail->pos>0)
1252 remove_http_message(ps, ps->pending_msgs_tail);
1260 while ( (running > 0) );
1262 handles_last_run = running;
1264 while (mret == CURLM_CALL_MULTI_PERFORM);
1265 send_schedule(plugin, cls);
1270 * Function setting up file descriptors and scheduling task to run
1271 * @param ses session to send data to
1272 * @return bytes sent to peer
1274 static size_t send_schedule(void *cls, struct Session* ses )
1276 struct Plugin *plugin = cls;
1281 struct GNUNET_NETWORK_FDSet *grs;
1282 struct GNUNET_NETWORK_FDSet *gws;
1286 GNUNET_assert(cls !=NULL);
1291 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1292 if (mret != CURLM_OK)
1294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1295 _("%s failed at %s:%d: `%s'\n"),
1296 "curl_multi_fdset", __FILE__, __LINE__,
1297 curl_multi_strerror (mret));
1300 mret = curl_multi_timeout (plugin->multi_handle, &to);
1301 if (mret != CURLM_OK)
1303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1304 _("%s failed at %s:%d: `%s'\n"),
1305 "curl_multi_timeout", __FILE__, __LINE__,
1306 curl_multi_strerror (mret));
1310 grs = GNUNET_NETWORK_fdset_create ();
1311 gws = GNUNET_NETWORK_fdset_create ();
1312 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1313 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1314 plugin->http_server_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1315 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1316 GNUNET_SCHEDULER_NO_TASK,
1317 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1322 GNUNET_NETWORK_fdset_destroy (gws);
1323 GNUNET_NETWORK_fdset_destroy (grs);
1325 /* FIXME: return bytes REALLY sent */
1331 * Function that can be used by the transport service to transmit
1332 * a message using the plugin. Note that in the case of a
1333 * peer disconnecting, the continuation MUST be called
1334 * prior to the disconnect notification itself. This function
1335 * will be called with this peer's HELLO message to initiate
1336 * a fresh connection to another peer.
1338 * @param cls closure
1339 * @param target who should receive this message
1340 * @param msgbuf the message to transmit
1341 * @param msgbuf_size number of bytes in 'msgbuf'
1342 * @param priority how important is the message (most plugins will
1343 * ignore message priority and just FIFO)
1344 * @param timeout how long to wait at most for the transmission (does not
1345 * require plugins to discard the message after the timeout,
1346 * just advisory for the desired delay; most plugins will ignore
1348 * @param session which session must be used (or NULL for "any")
1349 * @param addr the address to use (can be NULL if the plugin
1350 * is "on its own" (i.e. re-use existing TCP connection))
1351 * @param addrlen length of the address in bytes
1352 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1353 * GNUNET_NO means the plugin may use any other address and
1354 * GNUNET_SYSERR means that only reliable existing
1355 * bi-directional connections should be used (regardless
1357 * @param cont continuation to call once the message has
1358 * been transmitted (or if the transport is ready
1359 * for the next transmission call; or if the
1360 * peer disconnected...); can be NULL
1361 * @param cont_cls closure for cont
1362 * @return number of bytes used (on the physical network, with overheads);
1363 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1364 * and does NOT mean that the message was not transmitted (DV)
1367 http_plugin_send (void *cls,
1368 const struct GNUNET_PeerIdentity *target,
1371 unsigned int priority,
1372 struct GNUNET_TIME_Relative to,
1373 struct Session *session,
1377 GNUNET_TRANSPORT_TransmitContinuation cont,
1380 struct Plugin *plugin = cls;
1381 struct HTTP_Message *msg;
1383 struct HTTP_PeerContext * pc;
1384 struct Session * ps;
1386 GNUNET_assert(cls !=NULL);
1387 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
1389 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1393 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1394 pc->plugin = plugin;
1395 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1396 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1398 ps = get_Session(plugin, pc, addr, addrlen);
1399 /* session not existing, but address forced -> creating new session */
1400 if ((ps==NULL) && (force_address == GNUNET_YES))
1402 ps = GNUNET_malloc(sizeof (struct Session));
1403 ps->addr = GNUNET_malloc(addrlen);
1404 memcpy(ps->addr,addr,addrlen);
1405 ps->addrlen = addrlen;
1406 ps->direction=OUTBOUND;
1407 ps->recv_connected = GNUNET_NO;
1408 ps->send_connected = GNUNET_NO;
1409 ps->pending_msgs_head = NULL;
1410 ps->pending_msgs_tail = NULL;
1412 ps->url = create_url (plugin, ps->addr, ps->addrlen);
1413 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1415 /* session not existing, address not forced -> looking for other session */
1416 if ((ps==NULL) && (force_address == GNUNET_NO))
1418 /* FIXME: CREATING SESSION, SHOULD CHOOSE EXISTING */
1419 ps = GNUNET_malloc(sizeof (struct Session));
1420 ps->addr = GNUNET_malloc(addrlen);
1421 memcpy(ps->addr,addr,addrlen);
1422 ps->addrlen = addrlen;
1423 ps->direction=OUTBOUND;
1424 ps->recv_connected = GNUNET_NO;
1425 ps->send_connected = GNUNET_NO;
1426 ps->pending_msgs_head = NULL;
1427 ps->pending_msgs_tail = NULL;
1429 ps->url = create_url (plugin, ps->addr, ps->addrlen);
1430 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1432 if ((ps==NULL) && (force_address == GNUNET_SYSERR))
1434 /* FIXME: CREATING SESSION, SHOULD CHOOSE EXISTING */
1435 ps = GNUNET_malloc(sizeof (struct Session));
1436 ps->addr = GNUNET_malloc(addrlen);
1437 memcpy(ps->addr,addr,addrlen);
1438 ps->addrlen = addrlen;
1439 ps->direction=OUTBOUND;
1440 ps->recv_connected = GNUNET_NO;
1441 ps->send_connected = GNUNET_NO;
1442 ps->pending_msgs_head = NULL;
1443 ps->pending_msgs_tail = NULL;
1445 ps->url = create_url (plugin, ps->addr, ps->addrlen);
1446 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1449 char * force = GNUNET_malloc(30);
1450 if (force_address == GNUNET_YES)
1451 strcpy(force,"forced addr.");
1452 if (force_address == GNUNET_NO)
1453 strcpy(force,"any addr.");
1454 if (force_address == GNUNET_SYSERR)
1455 strcpy(force,"reliable bi-direc. address addr.");
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' %s (%s), session: %X\n",
1458 GNUNET_i2s(&pc->identity),
1460 http_plugin_address_to_string(NULL, addr, addrlen),
1465 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1467 msg->size = msgbuf_size;
1469 msg->buf = (char *) &msg[1];
1470 msg->transmit_cont = cont;
1471 msg->transmit_cont_cls = cont_cls;
1472 memcpy (msg->buf,msgbuf, msgbuf_size);
1473 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1475 return send_check_connections (plugin, session, ps);
1481 * Function that can be used to force the plugin to disconnect
1482 * from the given peer and cancel all previous transmissions
1483 * (and their continuationc).
1485 * @param cls closure
1486 * @param target peer from which to disconnect
1489 http_plugin_disconnect (void *cls,
1490 const struct GNUNET_PeerIdentity *target)
1493 struct Plugin *plugin = cls;
1494 struct HTTP_PeerContext *pc = NULL;
1495 struct Session *ps = NULL;
1497 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1505 if (ps->direction==OUTBOUND)
1507 if (ps->send_endpoint!=NULL)
1509 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1510 curl_easy_cleanup(ps->send_endpoint);
1511 ps->send_endpoint=NULL;
1513 if (ps->recv_endpoint!=NULL)
1515 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1516 curl_easy_cleanup(ps->recv_endpoint);
1517 ps->recv_endpoint=NULL;
1520 if (ps->direction==INBOUND)
1524 while (ps->pending_msgs_head!=NULL)
1526 remove_http_message(ps, ps->pending_msgs_head);
1528 ps->recv_connected = GNUNET_NO;
1529 ps->recv_active = GNUNET_NO;
1530 ps->send_connected = GNUNET_NO;
1531 ps->send_active = GNUNET_NO;
1538 * Convert the transports address to a nice, human-readable
1541 * @param cls closure
1542 * @param type name of the transport that generated the address
1543 * @param addr one of the addresses of the host, NULL for the last address
1544 * the specific address format depends on the transport
1545 * @param addrlen length of the address
1546 * @param numeric should (IP) addresses be displayed in numeric form?
1547 * @param timeout after how long should we give up?
1548 * @param asc function to call on each string
1549 * @param asc_cls closure for asc
1552 http_plugin_address_pretty_printer (void *cls,
1557 struct GNUNET_TIME_Relative timeout,
1558 GNUNET_TRANSPORT_AddressStringCallback
1561 const struct IPv4HttpAddress *t4;
1562 const struct IPv6HttpAddress *t6;
1563 struct sockaddr_in a4;
1564 struct sockaddr_in6 a6;
1570 GNUNET_assert(cls !=NULL);
1571 if (addrlen == sizeof (struct IPv6HttpAddress))
1573 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1575 a6.sin6_addr = t6->ipv6_addr;
1576 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1577 port = ntohs(t6->u6_port);
1579 else if (addrlen == sizeof (struct IPv4HttpAddress))
1581 address = GNUNET_malloc (INET_ADDRSTRLEN);
1583 a4.sin_addr.s_addr = t4->ipv4_addr;
1584 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1585 port = ntohs(t4->u_port);
1589 /* invalid address */
1590 GNUNET_break_op (0);
1591 asc (asc_cls, NULL);
1594 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1595 GNUNET_free (address);
1596 GNUNET_assert(res != 0);
1604 * Another peer has suggested an address for this
1605 * peer and transport plugin. Check that this could be a valid
1606 * address. If so, consider adding it to the list
1609 * @param cls closure
1610 * @param addr pointer to the address
1611 * @param addrlen length of addr
1612 * @return GNUNET_OK if this is a plausible address for this peer
1616 http_plugin_address_suggested (void *cls,
1617 const void *addr, size_t addrlen)
1619 struct Plugin *plugin = cls;
1620 struct IPv4HttpAddress *v4;
1621 struct IPv6HttpAddress *v6;
1624 GNUNET_assert(cls !=NULL);
1625 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1626 (addrlen != sizeof (struct IPv6HttpAddress)))
1628 return GNUNET_SYSERR;
1630 if (addrlen == sizeof (struct IPv4HttpAddress))
1632 v4 = (struct IPv4HttpAddress *) addr;
1633 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1635 return GNUNET_SYSERR;
1637 port = ntohs (v4->u_port);
1638 if (port != plugin->port_inbound)
1640 return GNUNET_SYSERR;
1645 v6 = (struct IPv6HttpAddress *) addr;
1646 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1648 return GNUNET_SYSERR;
1650 port = ntohs (v6->u6_port);
1651 if (port != plugin->port_inbound)
1653 return GNUNET_SYSERR;
1661 * Function called for a quick conversion of the binary address to
1662 * a numeric address. Note that the caller must not free the
1663 * address and that the next call to this function is allowed
1664 * to override the address again.
1666 * @param cls closure
1667 * @param addr binary address
1668 * @param addrlen length of the address
1669 * @return string representing the same address
1672 http_plugin_address_to_string (void *cls,
1676 const struct IPv4HttpAddress *t4;
1677 const struct IPv6HttpAddress *t6;
1678 struct sockaddr_in a4;
1679 struct sockaddr_in6 a6;
1685 if (addrlen == sizeof (struct IPv6HttpAddress))
1687 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1689 a6.sin6_addr = t6->ipv6_addr;
1690 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1691 port = ntohs(t6->u6_port);
1693 else if (addrlen == sizeof (struct IPv4HttpAddress))
1695 address = GNUNET_malloc (INET_ADDRSTRLEN);
1697 a4.sin_addr.s_addr = t4->ipv4_addr;
1698 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1699 port = ntohs(t4->u_port);
1703 /* invalid address */
1706 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
1707 GNUNET_free (address);
1708 GNUNET_assert(res != 0);
1713 * Add the IP of our network interface to the list of
1714 * our external IP addresses.
1716 * @param cls the 'struct Plugin*'
1717 * @param name name of the interface
1718 * @param isDefault do we think this may be our default interface
1719 * @param addr address of the interface
1720 * @param addrlen number of bytes in addr
1721 * @return GNUNET_OK to continue iterating
1724 process_interfaces (void *cls,
1727 const struct sockaddr *addr, socklen_t addrlen)
1729 struct Plugin *plugin = cls;
1730 struct IPv4HttpAddress * t4;
1731 struct IPv6HttpAddress * t6;
1734 GNUNET_assert(cls !=NULL);
1735 af = addr->sa_family;
1738 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
1739 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1741 /* skip loopback addresses */
1744 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1745 t4->u_port = htons (plugin->port_inbound);
1746 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
1749 else if (af == AF_INET6)
1751 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
1752 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1754 /* skip link local addresses */
1757 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1759 /* skip loopback addresses */
1762 memcpy (&t6->ipv6_addr,
1763 &((struct sockaddr_in6 *) addr)->sin6_addr,
1764 sizeof (struct in6_addr));
1765 t6->u6_port = htons (plugin->port_inbound);
1766 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
1771 int peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
1773 struct HTTP_PeerContext * pc = value;
1774 struct Session * ps = pc->head;
1775 struct Session * tmp = NULL;
1776 struct HTTP_Message * msg = NULL;
1777 struct HTTP_Message * msg_tmp = NULL;
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
1785 GNUNET_free(ps->addr);
1786 GNUNET_free(ps->url);
1787 if (ps->msgtok != NULL)
1788 GNUNET_SERVER_mst_destroy (ps->msgtok);
1790 msg = ps->pending_msgs_head;
1793 msg_tmp = msg->next;
1797 if (ps->direction==OUTBOUND)
1799 if (ps->send_endpoint!=NULL)
1800 curl_easy_cleanup(ps->send_endpoint);
1801 if (ps->recv_endpoint!=NULL)
1802 curl_easy_cleanup(ps->recv_endpoint);
1814 * Exit point from the plugin.
1817 libgnunet_plugin_transport_http_done (void *cls)
1819 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1820 struct Plugin *plugin = api->cls;
1823 GNUNET_assert(cls !=NULL);
1825 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1827 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1828 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1831 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1833 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1834 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1837 if ( plugin->http_server_task_send != GNUNET_SCHEDULER_NO_TASK)
1839 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_send);
1840 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
1843 if (plugin->http_server_daemon_v4 != NULL)
1845 MHD_stop_daemon (plugin->http_server_daemon_v4);
1846 plugin->http_server_daemon_v4 = NULL;
1848 if (plugin->http_server_daemon_v6 != NULL)
1850 MHD_stop_daemon (plugin->http_server_daemon_v6);
1851 plugin->http_server_daemon_v6 = NULL;
1854 /* free all peer information */
1855 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
1856 &peer_context_Iterator,
1858 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
1860 mret = curl_multi_cleanup(plugin->multi_handle);
1861 if ( CURLM_OK != mret)
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
1864 GNUNET_free (plugin);
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
1872 * Entry point for the plugin.
1875 libgnunet_plugin_transport_http_init (void *cls)
1877 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1878 struct Plugin *plugin;
1879 struct GNUNET_TRANSPORT_PluginFunctions *api;
1880 struct GNUNET_TIME_Relative gn_timeout;
1881 long long unsigned int port;
1883 GNUNET_assert(cls !=NULL);
1884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
1886 plugin = GNUNET_malloc (sizeof (struct Plugin));
1888 plugin->peers = NULL;
1890 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1892 api->send = &http_plugin_send;
1893 api->disconnect = &http_plugin_disconnect;
1894 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1895 api->check_address = &http_plugin_address_suggested;
1896 api->address_to_string = &http_plugin_address_to_string;
1898 /* Hashing our identity to use it in URLs */
1899 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
1901 /* Reading port number from config file */
1903 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1909 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1912 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1914 libgnunet_plugin_transport_http_done (api);
1917 GNUNET_assert ((port > 0) && (port <= 65535));
1918 plugin->port_inbound = port;
1919 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1920 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->http_server_daemon_v6 == NULL) && (port != 0))
1922 plugin->http_server_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
1924 &acceptPolicyCallback,
1925 plugin , &accessHandlerCallback, plugin,
1926 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1927 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1928 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
1929 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1930 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1932 plugin->http_server_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
1934 &acceptPolicyCallback,
1935 plugin , &accessHandlerCallback, plugin,
1936 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1937 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1938 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
1939 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1940 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1943 if (plugin->http_server_daemon_v4 != NULL)
1944 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1945 if (plugin->http_server_daemon_v6 != NULL)
1946 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1948 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
1950 else if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
1955 libgnunet_plugin_transport_http_done (api);
1959 /* Initializing cURL */
1960 curl_global_init(CURL_GLOBAL_ALL);
1961 plugin->multi_handle = curl_multi_init();
1963 if ( NULL == plugin->multi_handle )
1965 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1967 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
1969 libgnunet_plugin_transport_http_done (api);
1973 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
1974 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1979 /* end of plugin_transport_http.c */