From fb364c38673557da4882c7fce86449b26d320025 Mon Sep 17 00:00:00 2001 From: Matthias Wachs Date: Thu, 20 May 2010 11:12:26 +0000 Subject: [PATCH] --- src/transport/plugin_transport_http.c | 66 ++++++++++++++----- .../test_plugin_transport_data_http.conf | 2 +- src/transport/test_plugin_transport_http.c | 46 ++++++++++++- 3 files changed, 93 insertions(+), 21 deletions(-) diff --git a/src/transport/plugin_transport_http.c b/src/transport/plugin_transport_http.c index af2a19cde..22f06caca 100644 --- a/src/transport/plugin_transport_http.c +++ b/src/transport/plugin_transport_http.c @@ -45,7 +45,6 @@ */ #define HTTP_PUT_RESPONSE "Thank you!" - /** * After how long do we expire an address that we * learned from another peer if it is not reconfirmed @@ -53,6 +52,11 @@ */ #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6) +/** + * Page returned if request invalid + */ +#define HTTP_ERROR_RESPONSE "404 Not Found

Not Found

The requested URL was not found on this server.


" + /** * Encapsulation of all of the state of the plugin. @@ -183,7 +187,7 @@ static GNUNET_SCHEDULER_TaskIdentifier http_task_v6; /** - * Pl + * Information about this plugin */ static struct Plugin *plugin; @@ -197,6 +201,12 @@ static CURLM *multi_handle; */ static char * hostname; +/** + * Our ASCII encoded, hashed peer identity + * This string is used to distinguish between connections and is added to the urls + */ +static struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident; + /** * Finds a http session in our linked list using peer identity as a key @@ -309,10 +319,14 @@ static void requestCompletedCallback (void *cls, struct MHD_Connection * connect struct Session * cs; cs = *httpSessionCache; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection `%s' was terminated\n",cs->ip); - /* session set to inactive */ - cs->is_active = GNUNET_NO; - + if (cs != NULL) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&cs->sender)); + /* session set to inactive */ + cs->is_active = GNUNET_NO; + } + else + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Not accepted connection was terminated\n"); return; } @@ -351,10 +365,22 @@ accessHandlerCallback (void *cls, struct sockaddr_in *addrin; struct sockaddr_in6 *addrin6; char * address = NULL; + struct GNUNET_PeerIdentity pi_in; int res = GNUNET_NO; if ( NULL == *httpSessionCache) { + /* check url for peer identity */ + res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey)); + if ( GNUNET_SYSERR == res ) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident\n"); + response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO); + res = MHD_queue_response (session, MHD_HTTP_NOT_FOUND, response); + MHD_destroy_response (response); + return MHD_YES; + } + conn_info = MHD_get_connection_info(session, MHD_CONNECTION_INFO_CLIENT_ADDRESS ); /* Incoming IPv4 connection */ if ( AF_INET == conn_info->client_addr->sin_family) @@ -371,8 +397,7 @@ accessHandlerCallback (void *cls, inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN); } - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from `[%s]:%u'\n",method, address,conn_info->client_addr->sin_port); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from peer `%s' from `[%s]:%u'\n",method, GNUNET_h2s(&pi_in.hashPubKey),address,conn_info->client_addr->sin_port); /* find session for address */ cs = NULL; @@ -382,16 +407,15 @@ accessHandlerCallback (void *cls, while ( NULL != cs) { - /* FIXME: When are two connections equal? ip1 == ip2 or ip1:port1 == ip2:port2 ? - * Think about NAT, reuse connections... - */ - /* Comparison based on ip address */ - res = (0 == memcmp(&(conn_info->client_addr->sin_addr),&(cs->addr->sin_addr), sizeof (struct in_addr))) ? GNUNET_YES : GNUNET_NO; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"res is %u \n",res); + // res = (0 == memcmp(&(conn_info->client_addr->sin_addr),&(cs->addr->sin_addr), sizeof (struct in_addr))) ? GNUNET_YES : GNUNET_NO; + /* Comparison based on ip address, port number and address family */ - /* res = (0 == memcmp((conn_info->client_addr),(cs->addr), sizeof (struct sockaddr_in))) ? GNUNET_YES : GNUNET_NO; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"port1 is %u port2 is %u, res is %u \n",conn_info->client_addr->sin_port,cs->addr->sin_port,res); */ + // res = (0 == memcmp((conn_info->client_addr),(cs->addr), sizeof (struct sockaddr_in))) ? GNUNET_YES : GNUNET_NO; + + /* Comparison based on PeerIdentity */ + res = (0 == memcmp(&pi_in,&(cs->sender), sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_YES : GNUNET_NO; + if ( GNUNET_YES == res) { /* existing session for this address found */ @@ -408,9 +432,9 @@ accessHandlerCallback (void *cls, cs = GNUNET_malloc ( sizeof( struct Session) ); cs->addr = GNUNET_malloc ( sizeof (struct sockaddr_in) ); - cs->ip = address; memcpy(cs->addr, conn_info->client_addr, sizeof (struct sockaddr_in)); + memcpy(&cs->sender, &pi_in, sizeof (struct GNUNET_PeerIdentity)); cs->next = NULL; cs->is_active = GNUNET_YES; @@ -455,6 +479,11 @@ accessHandlerCallback (void *cls, if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) ) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url); + + response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO); + MHD_queue_response (session, MHD_HTTP_OK, response); + MHD_destroy_response (response); } return MHD_YES; @@ -807,6 +836,9 @@ libgnunet_plugin_transport_http_init (void *cls) hostname = GNUNET_RESOLVER_local_fqdn_get (); + /* Hashing our identity to use it in URLs */ + GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &my_ascii_hash_ident); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n"); /* Reading port number from config file */ if ((GNUNET_OK != diff --git a/src/transport/test_plugin_transport_data_http.conf b/src/transport/test_plugin_transport_data_http.conf index 13cc00388..8e4bbe033 100644 --- a/src/transport/test_plugin_transport_data_http.conf +++ b/src/transport/test_plugin_transport_data_http.conf @@ -1,6 +1,6 @@ [PATHS] DEFAULTCONFIG = test_plugin_transport_data_http.conf -SERVICEHOME = /tmp/test-gnunetd-transport-peer-1/ +SERVICEHOME = /tmp/test_plugin_transport_http/ [transport-http] PORT = 12389 diff --git a/src/transport/test_plugin_transport_http.c b/src/transport/test_plugin_transport_http.c index 9e11026f8..c2a2901d3 100644 --- a/src/transport/test_plugin_transport_http.c +++ b/src/transport/test_plugin_transport_http.c @@ -59,17 +59,21 @@ */ /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */ +/** + * Our public key. + */ +static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; + /** * Our identity. */ static struct GNUNET_PeerIdentity my_identity; -#if 0 /** * Our private key. */ static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key; -#endif + /** * Our scheduler. @@ -283,6 +287,8 @@ run (void *cls, char * libname; sched = s; cfg = c; + char *keyfile; + unsigned long long tneigh; /* settings up statistics */ /* stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg); @@ -295,6 +301,38 @@ run (void *cls, return ; }*/ + /* parse configuration */ + if ((GNUNET_OK != + GNUNET_CONFIGURATION_get_value_number (c, + "TRANSPORT", + "NEIGHBOUR_LIMIT", + &tneigh)) || + (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_filename (c, + "GNUNETD", + "HOSTKEY", &keyfile))) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ + ("Transport service is lacking key configuration settings. Exiting.\n")); + 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); + if (my_private_key == NULL) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ + ("Transport service could not access hostkey. Exiting.\n")); + GNUNET_SCHEDULER_shutdown (s); + return; + } + 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); + /* load plugins... */ setup_plugin_environment (); GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http"); @@ -368,7 +406,9 @@ main (int argc, char *const *argv) argv_prog, "test_plugin_transport_http", "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES; - GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http"); + /* FIXME: Please do not generate a key every time + * GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http"); + */ /* if (0 != PLIBC_KILL (pid, SIGTERM)) { -- 2.25.1