From e079d8ca1ecc5e31ab5fdb44c140876f31f5c94e Mon Sep 17 00:00:00 2001 From: Matthias Wachs Date: Mon, 3 May 2010 15:06:36 +0000 Subject: [PATCH] --- src/transport/Makefile.am | 16 ++- src/transport/plugin_transport_http.c | 127 +++++++++++++++++- .../test_plugin_transport_data_http.conf | 28 +--- src/transport/test_plugin_transport_http.c | 15 ++- .../test_transport_api_http_peer1.conf | 3 +- .../test_transport_api_http_peer2.conf | 3 +- 6 files changed, 157 insertions(+), 35 deletions(-) diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am index 1cb61c0a9..c6f2bc2ff 100644 --- a/src/transport/Makefile.am +++ b/src/transport/Makefile.am @@ -6,6 +6,12 @@ if MINGW WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols endif +if HAVE_MHD + GN_LIBMHD = -lmicrohttpd + HTTP_PLUGIN_LA = libgnunet_plugin_transport_http.la + HTTP_PLGUIN_CHECK = test_plugin_transport_http +endif + if USE_COVERAGE AM_CFLAGS = --coverage -O0 endif @@ -69,8 +75,8 @@ plugin_LTLIBRARIES = \ libgnunet_plugin_transport_tcp.la \ libgnunet_plugin_transport_udp.la \ libgnunet_plugin_transport_udp_nat.la \ + $(HTTP_PLUGIN_LA) \ libgnunet_plugin_transport_template.la -# libgnunet_plugin_transport_http.la # TODO: add http, nat, etc. libgnunet_plugin_transport_tcp_la_SOURCES = \ @@ -110,6 +116,7 @@ libgnunet_plugin_transport_udp_nat_la_LIBADD = \ libgnunet_plugin_transport_udp_nat_la_LDFLAGS = \ $(GN_PLUGIN_LDFLAGS) +if HAVE_MHD libgnunet_plugin_transport_http_la_SOURCES = \ plugin_transport_http.c libgnunet_plugin_transport_http_la_LIBADD = \ @@ -118,14 +125,15 @@ libgnunet_plugin_transport_http_la_LIBADD = \ $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \ $(top_builddir)/src/util/libgnunetutil.la libgnunet_plugin_transport_http_la_LDFLAGS = \ + $(GN_LIBMHD) \ $(GN_PLUGIN_LDFLAGS) - +endif check_PROGRAMS = \ test_transport_api_tcp \ test_transport_api_udp \ - test_transport_api_udp_nat \ - test_plugin_transport_http + $(HTTP_PLGUIN_CHECK) \ + test_transport_api_udp_nat # test_transport_api_http \ # TODO: add tests for http, nat, etc. diff --git a/src/transport/plugin_transport_http.c b/src/transport/plugin_transport_http.c index a0711660b..7ede6da3c 100644 --- a/src/transport/plugin_transport_http.c +++ b/src/transport/plugin_transport_http.c @@ -32,8 +32,10 @@ #include "gnunet_statistics_service.h" #include "gnunet_transport_service.h" #include "plugin_transport.h" +#include "microhttpd.h" -#define DEBUG_HTTP GNUNET_YES +#define VERBOSE GNUNET_YES +#define DEBUG GNUNET_YES /** * After how long do we expire an address that we @@ -42,6 +44,7 @@ */ #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6) +#define HTTP_TIMEOUT 600 /** * Encapsulation of all of the state of the plugin. @@ -129,6 +132,11 @@ struct Plugin }; +/** + * Daemon for listening for new connections. + */ +static struct MHD_Daemon *http_daemon; + /** * Function that can be used by the transport service to transmit * a message using the plugin. @@ -247,6 +255,46 @@ http_plugin_address_suggested (void *cls, return GNUNET_OK; } +/** + * Check if we are allowed to connect to the given IP. + */ +static int +acceptPolicyCallback (void *cls, + const struct sockaddr *addr, socklen_t addr_len) +{ + return MHD_YES; +} + +/** + * Process GET or PUT request received via MHD. For + * GET, queue response that will send back our pending + * messages. For PUT, process incoming data and send + * to GNUnet core. In either case, check if a session + * already exists and create a new one if not. + */ +static int +accessHandlerCallback (void *cls, + struct MHD_Connection *session, + const char *url, + const char *method, + const char *version, + const char *upload_data, + size_t * upload_data_size, void **httpSessionCache) +{ + return MHD_YES; +} + +/** + * MHD is done handling a request. Cleanup + * the respective transport state. + */ +static void +requestCompletedCallback (void *unused, + struct MHD_Connection *session, + void **httpSessionCache) +{ + +} /** * Entry point for the plugin. @@ -257,6 +305,8 @@ libgnunet_plugin_transport_http_init (void *cls) struct GNUNET_TRANSPORT_PluginEnvironment *env = cls; struct GNUNET_TRANSPORT_PluginFunctions *api; struct Plugin *plugin; + long long unsigned int port; + int use_ipv6; plugin = GNUNET_malloc (sizeof (struct Plugin)); plugin->env = env; @@ -267,7 +317,73 @@ libgnunet_plugin_transport_http_init (void *cls) api->disconnect = &http_plugin_disconnect; api->address_pretty_printer = &http_plugin_address_pretty_printer; api->check_address = &http_plugin_address_suggested; - return api; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n"); + /* Reading port number from config file */ + if ((GNUNET_OK != + GNUNET_CONFIGURATION_get_value_number (env->cfg, + "transport-http", + "PORT", + &port)) || + (port > 65535) ) + { + GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, + "http", + _ + ("Require valid port number for service `%s' in configuration!\n"), + "transport-http"); + return NULL; + } + use_ipv6 = GNUNET_YES; + use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-http","USE_IPV6"); + if ((http_daemon == NULL) && (port != 0)) + { + if ( use_ipv6 == GNUNET_YES) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon could not started, http plugin not working\n"); + http_daemon = MHD_start_daemon (MHD_USE_IPv6, + port, + &acceptPolicyCallback, + NULL, &accessHandlerCallback, NULL, + MHD_OPTION_CONNECTION_TIMEOUT, + (unsigned int) HTTP_TIMEOUT, + MHD_OPTION_CONNECTION_MEMORY_LIMIT, + (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE, + MHD_OPTION_CONNECTION_LIMIT, + (unsigned int) 128, + MHD_OPTION_PER_IP_CONNECTION_LIMIT, + (unsigned int) 8, + MHD_OPTION_NOTIFY_COMPLETED, + &requestCompletedCallback, NULL, + MHD_OPTION_END); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 disabled\n",port); + http_daemon = MHD_start_daemon (MHD_NO_FLAG, + port, + &acceptPolicyCallback, + NULL, &accessHandlerCallback, NULL, + MHD_OPTION_CONNECTION_TIMEOUT, + (unsigned int) HTTP_TIMEOUT, + MHD_OPTION_CONNECTION_MEMORY_LIMIT, + (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE, + MHD_OPTION_CONNECTION_LIMIT, + (unsigned int) 128, + MHD_OPTION_PER_IP_CONNECTION_LIMIT, + (unsigned int) 8, + MHD_OPTION_NOTIFY_COMPLETED, + &requestCompletedCallback, NULL, + MHD_OPTION_END); + } + } + if ( NULL != http_daemon ) + return api; + else + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Starting MHD on port %u with IPv6 disabled\n",port); + return NULL; + } } @@ -280,6 +396,13 @@ libgnunet_plugin_transport_http_done (void *cls) struct GNUNET_TRANSPORT_PluginFunctions *api = cls; struct Plugin *plugin = api->cls; + if (http_daemon != NULL) + { + MHD_stop_daemon (http_daemon); + http_daemon = NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n"); + GNUNET_free (plugin); GNUNET_free (api); return NULL; diff --git a/src/transport/test_plugin_transport_data_http.conf b/src/transport/test_plugin_transport_data_http.conf index f794c8f87..77f4bdbbb 100644 --- a/src/transport/test_plugin_transport_data_http.conf +++ b/src/transport/test_plugin_transport_data_http.conf @@ -1,24 +1,8 @@ [PATHS] -SERVICEHOME = /tmp/test-gnunetd-plugin-transport_http/ +DEFAULTCONFIG = test_transport_api_tcp_peer1.conf +SERVICEHOME = /tmp/test-gnunetd-transport-peer-1/ -[resolver] -PORT = 2364 - -[transport] -PORT = 2365 -PLUGINS = http - -[arm] -PORT = 2366 - -[statistics] -PORT = 2367 - -[transport-udp] -PORT = 2368 - -[peerinfo] -PORT = 2369 - -[testing] -WEAKRANDOM = YES +[transport-http] +PORT = 12389 +DEBUG = YES +USE_IPV6 = NO \ No newline at end of file diff --git a/src/transport/test_plugin_transport_http.c b/src/transport/test_plugin_transport_http.c index f8b5e4dba..76e4f64f6 100644 --- a/src/transport/test_plugin_transport_http.c +++ b/src/transport/test_plugin_transport_http.c @@ -36,7 +36,8 @@ #include "plugin_transport.h" #include "transport.h" -#define VERBOSE GNUNET_NO +#define VERBOSE GNUNET_YES +#define DEBUG GNUNET_YES /** * How long until we give up on transmitting the message? @@ -124,7 +125,7 @@ static void unload_plugins (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) { GNUNET_assert (NULL == - GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_udp", + GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api)); if (my_private_key != NULL) GNUNET_CRYPTO_rsa_key_free (my_private_key); @@ -143,7 +144,6 @@ static void test_validation () { struct sockaddr_in soaddr; - memset (&soaddr, 0, sizeof (soaddr)); #if HAVE_SOCKADDR_IN_SIN_LEN soaddr.sin_len = sizeof (soaddr); @@ -207,6 +207,7 @@ run (void *cls, GNUNET_SCHEDULER_shutdown (s); return; } + /* max_connect_per_transport = (uint32_t) tneigh; my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile); GNUNET_free (keyfile); @@ -220,14 +221,18 @@ run (void *cls, } GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key); GNUNET_CRYPTO_hash (&my_public_key, - sizeof (my_public_key), &my_identity.hashPubKey); + sizeof (my_public_key), &my_identity.hashPubKey);*/ /* load plugins... */ setup_plugin_environment (); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading http transport plugin\n")); + GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http"); api = GNUNET_PLUGIN_load (libname, &env); + if (api != NULL ) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Loading transport plugin for http `%s' successful\n",libname); + GNUNET_free (libname); if (api == NULL) { diff --git a/src/transport/test_transport_api_http_peer1.conf b/src/transport/test_transport_api_http_peer1.conf index 5fe8864e2..2f9e3425d 100644 --- a/src/transport/test_transport_api_http_peer1.conf +++ b/src/transport/test_transport_api_http_peer1.conf @@ -1,5 +1,6 @@ [transport-http] -PORT = 12368 +#PORT = 12368 +DEBUG=YES [fs] ALLOW_SHUTDOWN = YES diff --git a/src/transport/test_transport_api_http_peer2.conf b/src/transport/test_transport_api_http_peer2.conf index 2ca26fddd..44bb71f26 100644 --- a/src/transport/test_transport_api_http_peer2.conf +++ b/src/transport/test_transport_api_http_peer2.conf @@ -1,5 +1,6 @@ [transport-http] PORT = 22368 +DEBUG = YES [fs] ALLOW_SHUTDOWN = YES @@ -49,7 +50,7 @@ MINIMUM-FRIENDS = 0 [transport] PLUGINS = http -#DEBUG = YES +DEBUG = YES # PREFIX = ALLOW_SHUTDOWN = YES ACCEPT_FROM6 = ::1; -- 2.25.1