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 2, 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 Implementation of the HTTP transport service
24 * @author Matthias Wachs
28 #include "gnunet_protocols.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_service_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_transport_service.h"
34 #include "plugin_transport.h"
35 #include "microhttpd.h"
36 #include <curl/curl.h>
38 #define VERBOSE GNUNET_YES
39 #define DEBUG GNUNET_YES
42 * After how long do we expire an address that we
43 * learned from another peer if it is not reconfirmed
46 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
48 #define HTTP_TIMEOUT 600
50 #define CURL_EASY_SETOPT(c, a, b) do { ret = curl_easy_setopt(c, a, b); if (ret != CURLE_OK) GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("%s failed at %s:%d: `%s'\n"), "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret)); } while (0);
53 * Text of the response sent back after the last bytes of a PUT
54 * request have been received (just to formally obey the HTTP
57 #define HTTP_PUT_RESPONSE "Thank you!"
60 * Encapsulation of all of the state of the plugin.
66 * Session handle for connections.
72 * Stored in a linked list.
77 * Pointer to the global plugin struct.
79 struct Plugin *plugin;
82 * The client (used to identify this connection)
87 * Continuation function to call once the transmission buffer
88 * has again space available. NULL if there is no
89 * continuation to call.
91 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
94 * Closure for transmit_cont.
96 void *transmit_cont_cls;
99 * To whom are we talking to (set to our identity
100 * if we are still waiting for the welcome message)
102 struct GNUNET_PeerIdentity sender;
105 * At what time did we reset last_received last?
107 struct GNUNET_TIME_Absolute last_quota_update;
110 * How many bytes have we received since the "last_quota_update"
113 uint64_t last_received;
116 * Number of bytes per ms that this peer is allowed
124 * Encapsulation of all of the state of the plugin.
131 struct GNUNET_TRANSPORT_PluginEnvironment *env;
134 * Handle to the network service.
136 struct GNUNET_SERVICE_Context *service;
139 * List of open sessions.
141 struct Session *sessions;
144 static struct Plugin *plugin;
147 * Daemon for listening for new IPv4 connections.
149 static struct MHD_Daemon *http_daemon_v4;
152 * Daemon for listening for new IPv6connections.
154 static struct MHD_Daemon *http_daemon_v6;
157 * Our primary task for http daemon handling IPv4 connections
159 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
162 * Our primary task for http daemon handling IPv6 connections
164 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
167 * ID of the task downloading the hostlist
169 static GNUNET_SCHEDULER_TaskIdentifier ti_download;
171 /* static int running; */
174 * Curl multi for managing client operations.
176 static CURLM *curl_multi;
179 static char * get_url( const struct GNUNET_PeerIdentity * target)
181 return strdup("http://localhost:12389");
185 static size_t curl_read_function( void *ptr, size_t size, size_t nmemb, void *stream)
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl read function\n");
188 // strcpy ("Testmessa")
194 * Task that is run when we are ready to receive more data from the hostlist
197 * @param cls closure, unused
198 * @param tc task context, unused
201 task_download (void *cls,
202 const struct GNUNET_SCHEDULER_TaskContext *tc)
204 ti_download = GNUNET_SCHEDULER_NO_TASK;
205 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Download!!!\n");
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "Logging shutdown\n");
212 GNUNET_STATISTICS_set(plugin->env->stats,"shutdown",2, GNUNET_NO);
215 * Ask CURL for the select set and then schedule the
216 * receiving task with the scheduler.
226 struct GNUNET_NETWORK_FDSet *grs;
227 struct GNUNET_NETWORK_FDSet *gws;
229 struct GNUNET_TIME_Relative rtime;
235 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
236 if (mret != CURLM_OK)
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 _("%s failed at %s:%d: `%s'\n"),
240 "curl_multi_fdset", __FILE__, __LINE__,
241 curl_multi_strerror (mret));
245 mret = curl_multi_timeout (curl_multi, &timeout);
246 if (mret != CURLM_OK)
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 _("%s failed at %s:%d: `%s'\n"),
250 "curl_multi_timeout", __FILE__, __LINE__,
251 curl_multi_strerror (mret));
256 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0 );
257 /*rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
258 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
260 grs = GNUNET_NETWORK_fdset_create ();
261 gws = GNUNET_NETWORK_fdset_create ();
262 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
263 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Scheduling http plugin send operation using cURL\n");
266 ti_download = GNUNET_SCHEDULER_add_select (plugin->env->sched,
267 GNUNET_SCHEDULER_PRIORITY_HIGH,
268 GNUNET_SCHEDULER_NO_TASK,
274 GNUNET_NETWORK_fdset_destroy (gws);
275 GNUNET_NETWORK_fdset_destroy (grs);
279 * Function that can be used by the transport service to transmit
280 * a message using the plugin.
283 * @param target who should receive this message
284 * @param priority how important is the message
285 * @param msgbuf the message to transmit
286 * @param msgbuf_size number of bytes in 'msgbuf'
287 * @param timeout when should we time out
288 * @param session which session must be used (or NULL for "any")
289 * @param addr the address to use (can be NULL if the plugin
290 * is "on its own" (i.e. re-use existing TCP connection))
291 * @param addrlen length of the address in bytes
292 * @param force_address GNUNET_YES if the plugin MUST use the given address,
293 * otherwise the plugin may use other addresses or
294 * existing connections (if available)
295 * @param cont continuation to call once the message has
296 * been transmitted (or if the transport is ready
297 * for the next transmission call; or if the
298 * peer disconnected...)
299 * @param cont_cls closure for cont
300 * @return number of bytes used (on the physical network, with overheads);
301 * -1 on hard errors (i.e. address invalid); 0 is a legal value
302 * and does NOT mean that the message was not transmitted (DV)
305 http_plugin_send (void *cls,
306 const struct GNUNET_PeerIdentity * target,
309 unsigned int priority,
310 struct GNUNET_TIME_Relative timeout,
311 struct Session *session,
315 GNUNET_TRANSPORT_TransmitContinuation cont,
318 char * peer_url = get_url( target );
324 /* struct Plugin *plugin = cls; */
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending %u bytes (`%s')'\n",msgbuf_size, msgbuf);
327 /* Insert code to send using cURL */
328 curl = curl_easy_init ();
330 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
331 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
334 CURL_EASY_SETOPT (curl, CURLOPT_UPLOAD, 1L);
335 CURL_EASY_SETOPT (curl, CURLOPT_PUT, 1L);
336 CURL_EASY_SETOPT (curl, CURLOPT_READDATA, msgbuf);
338 CURL_EASY_SETOPT (curl, CURLOPT_URL, peer_url);
342 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Peer URL is not correct\n");
346 CURL_EASY_SETOPT (curl,
349 CURL_EASY_SETOPT (curl,
352 CURL_EASY_SETOPT (curl,
354 GNUNET_SERVER_MAX_MESSAGE_SIZE);
355 if (0 == strncmp (peer_url, "http", 4))
356 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
357 CURL_EASY_SETOPT (curl,
358 CURLOPT_CONNECTTIMEOUT,
360 CURL_EASY_SETOPT (curl,
364 CURL_EASY_SETOPT (curl, CURLOPT_URL, "http://www.tum.de/");
365 curl_multi = curl_multi_init ();
366 if (curl_multi == NULL)
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "curl multi is not correct\n");
374 mret = curl_multi_add_handle (curl_multi, curl);
375 if (mret != CURLM_OK)
377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378 _("%s failed at %s:%d: `%s'\n"),
379 "curl_multi_add_handle", __FILE__, __LINE__,
380 curl_multi_strerror (mret));
381 mret = curl_multi_cleanup (curl_multi);
382 if (mret != CURLM_OK)
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384 _("%s failed at %s:%d: `%s'\n"),
385 "curl_multi_cleanup", __FILE__, __LINE__,
386 curl_multi_strerror (mret));
394 GNUNET_free(peer_url);
396 bytes_sent = msgbuf_size;
404 * Function that can be used to force the plugin to disconnect
405 * from the given peer and cancel all previous transmissions
406 * (and their continuationc).
409 * @param target peer from which to disconnect
412 http_plugin_disconnect (void *cls,
413 const struct GNUNET_PeerIdentity *target)
415 // struct Plugin *plugin = cls;
422 * Convert the transports address to a nice, human-readable
426 * @param type name of the transport that generated the address
427 * @param addr one of the addresses of the host, NULL for the last address
428 * the specific address format depends on the transport
429 * @param addrlen length of the address
430 * @param numeric should (IP) addresses be displayed in numeric form?
431 * @param timeout after how long should we give up?
432 * @param asc function to call on each string
433 * @param asc_cls closure for asc
436 http_plugin_address_pretty_printer (void *cls,
441 struct GNUNET_TIME_Relative timeout,
442 GNUNET_TRANSPORT_AddressStringCallback
451 * Another peer has suggested an address for this
452 * peer and transport plugin. Check that this could be a valid
453 * address. If so, consider adding it to the list
457 * @param addr pointer to the address
458 * @param addrlen length of addr
459 * @return GNUNET_OK if this is a plausible address for this peer
463 http_plugin_address_suggested (void *cls,
464 void *addr, size_t addrlen)
466 /* struct Plugin *plugin = cls; */
468 /* check if the address is plausible; if so,
469 add it to our list! */
474 * Check if we are allowed to connect to the given IP.
477 acceptPolicyCallback (void *cls,
478 const struct sockaddr *addr, socklen_t addr_len)
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming connection \n");
481 /* Currently all incoming connections are accepted, so nothing to do here */
486 * Process GET or PUT request received via MHD. For
487 * GET, queue response that will send back our pending
488 * messages. For PUT, process incoming data and send
489 * to GNUnet core. In either case, check if a session
490 * already exists and create a new one if not.
493 accessHandlerCallback (void *cls,
494 struct MHD_Connection *session,
498 const char *upload_data,
499 size_t * upload_data_size, void **httpSessionCache)
501 struct MHD_Response *response;
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from \n",method);
505 /* Find out if session exists, otherwise create one */
507 /* Is it a PUT or a GET request */
508 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
510 /* PUT method here */
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %u \n",upload_data_size);
512 GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# PUT requests"), 1, GNUNET_NO);
514 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request with size\n");
517 GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# GET requests"), 1, GNUNET_NO);
520 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
521 HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
522 MHD_queue_response (session, MHD_HTTP_OK, response);
523 MHD_destroy_response (response);
529 * Function that queries MHD's select sets and
530 * starts the task waiting for them.
532 static GNUNET_SCHEDULER_TaskIdentifier prepare_daemon (struct MHD_Daemon *daemon_handle);
534 * Call MHD to process pending requests and then go back
535 * and schedule the next run.
538 run_daemon (void *cls,
539 const struct GNUNET_SCHEDULER_TaskContext *tc)
541 struct MHD_Daemon *daemon_handle = cls;
543 if (daemon_handle == http_daemon_v4)
544 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
546 if (daemon_handle == http_daemon_v6)
547 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
549 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
552 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
553 if (daemon_handle == http_daemon_v4)
554 http_task_v4 = prepare_daemon (daemon_handle);
555 if (daemon_handle == http_daemon_v6)
556 http_task_v6 = prepare_daemon (daemon_handle);
561 * Function that queries MHD's select sets and
562 * starts the task waiting for them.
564 static GNUNET_SCHEDULER_TaskIdentifier
565 prepare_daemon (struct MHD_Daemon *daemon_handle)
567 GNUNET_SCHEDULER_TaskIdentifier ret;
571 struct GNUNET_NETWORK_FDSet *wrs;
572 struct GNUNET_NETWORK_FDSet *wws;
573 struct GNUNET_NETWORK_FDSet *wes;
575 unsigned long long timeout;
577 struct GNUNET_TIME_Relative tv;
582 wrs = GNUNET_NETWORK_fdset_create ();
583 wes = GNUNET_NETWORK_fdset_create ();
584 wws = GNUNET_NETWORK_fdset_create ();
586 GNUNET_assert (MHD_YES ==
587 MHD_get_fdset (daemon_handle,
592 haveto = MHD_get_timeout (daemon_handle, &timeout);
593 if (haveto == MHD_YES)
594 tv.value = (uint64_t) timeout;
596 tv = GNUNET_TIME_UNIT_FOREVER_REL;
597 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
598 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
599 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
600 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
601 GNUNET_SCHEDULER_PRIORITY_HIGH,
602 GNUNET_SCHEDULER_NO_TASK,
608 GNUNET_NETWORK_fdset_destroy (wrs);
609 GNUNET_NETWORK_fdset_destroy (wws);
610 GNUNET_NETWORK_fdset_destroy (wes);
615 * Exit point from the plugin.
618 libgnunet_plugin_transport_http_done (void *cls)
620 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
621 struct Plugin *plugin = api->cls;
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
625 if ( ti_download != GNUNET_SCHEDULER_NO_TASK)
627 GNUNET_SCHEDULER_cancel(plugin->env->sched, ti_download);
628 ti_download = GNUNET_SCHEDULER_NO_TASK;
631 if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
633 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
634 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
637 if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
639 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
640 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
643 if (http_daemon_v4 != NULL)
645 MHD_stop_daemon (http_daemon_v4);
646 http_daemon_v4 = NULL;
648 if (http_daemon_v6 != NULL)
650 MHD_stop_daemon (http_daemon_v6);
651 http_daemon_v6 = NULL;
655 if ( NULL != curl_multi)
657 curl_multi_cleanup (curl_multi);
660 GNUNET_free (plugin);
666 * Entry point for the plugin.
669 libgnunet_plugin_transport_http_init (void *cls)
671 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
672 struct GNUNET_TRANSPORT_PluginFunctions *api;
673 long long unsigned int port;
675 plugin = GNUNET_malloc (sizeof (struct Plugin));
678 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
680 api->send = &http_plugin_send;
681 api->disconnect = &http_plugin_disconnect;
682 api->address_pretty_printer = &http_plugin_address_pretty_printer;
683 api->check_address = &http_plugin_address_suggested;
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
687 /* Reading port number from config file */
689 GNUNET_CONFIGURATION_get_value_number (env->cfg,
695 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
698 ("Require valid port number for service `%s' in configuration!\n"),
700 libgnunet_plugin_transport_http_done (api);
703 if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
705 http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
707 &acceptPolicyCallback,
708 NULL, &accessHandlerCallback, NULL,
709 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
710 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
711 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
712 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
714 http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
716 &acceptPolicyCallback,
717 NULL, &accessHandlerCallback, NULL,
718 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
719 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
720 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
721 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
725 curl_multi = curl_multi_init ();
727 if (http_daemon_v4 != NULL)
728 http_task_v4 = prepare_daemon (http_daemon_v4);
729 if (http_daemon_v6 != NULL)
730 http_task_v6 = prepare_daemon (http_daemon_v6);
732 if ((http_daemon_v4 == NULL) || (http_daemon_v6 != NULL))
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u\n",port);
736 if (NULL == plugin->env->stats)
738 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
739 _("Failed to retrieve statistics handle\n"));
740 libgnunet_plugin_transport_http_done (api);
744 GNUNET_STATISTICS_set ( env->stats, "# PUT requests", 0, GNUNET_NO);
745 GNUNET_STATISTICS_set ( env->stats, "# GET requests", 0, GNUNET_NO);
747 if ( ((NULL == http_daemon_v4) && (NULL == http_daemon_v6)) || (NULL == curl_multi))
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Initializing http plugin failed\n");
750 libgnunet_plugin_transport_http_done (api);
757 /* end of plugin_transport_http.c */