From: Matthias Wachs Date: Wed, 19 May 2010 14:19:49 +0000 (+0000) Subject: (no commit message) X-Git-Tag: initial-import-from-subversion-38251~21644 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=d9848e182469f43e59cb7fb9883ffbc008966b64;p=oweals%2Fgnunet.git --- diff --git a/src/transport/plugin_transport_http.c b/src/transport/plugin_transport_http.c index 8694765d6..59110c1be 100644 --- a/src/transport/plugin_transport_http.c +++ b/src/transport/plugin_transport_http.c @@ -31,6 +31,7 @@ #include "gnunet_service_lib.h" #include "gnunet_statistics_service.h" #include "gnunet_transport_service.h" +#include "gnunet_resolver_service.h" #include "plugin_transport.h" #include "microhttpd.h" #include @@ -103,11 +104,21 @@ struct Session */ char * ip; + /** + * Sender's ip address to distinguish between incoming connections + */ + struct sockaddr_in * addr; + /** * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)? */ unsigned int is_client; + /** + * Is the connection active (GNUNET_YES) or terminated (GNUNET_NO)? + */ + unsigned int is_active; + /** * At what time did we reset last_received last? */ @@ -182,11 +193,9 @@ static struct Plugin *plugin; static CURLM *multi_handle; /** - * session of current incoming connection + * Our hostname */ -static struct Session * current_session; - -static unsigned int locked; +static char * hostname; /** @@ -297,8 +306,13 @@ static struct Session * create_session_by_ip ( struct sockaddr_in * addr ) */ static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection was terminated\n"); - /* clean up session here*/ + 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; + return; } @@ -309,141 +323,107 @@ static int acceptPolicyCallback (void *cls, const struct sockaddr *addr, socklen_t addr_len) { + /* Every connection is accepted, nothing more to do here */ + 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) +{ + struct MHD_Response *response; + struct Session * cs; + struct Session * cs_temp; + const union MHD_ConnectionInfo * conn_info; struct sockaddr_in *addrin; struct sockaddr_in6 *addrin6; char * address = NULL; - struct Session * cs; - - if ( GNUNET_YES == locked ) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming connections not accepted, rejecting connection\n"); - return MHD_NO; - } - /* something went wrong since last attempt to connect, lost session to free */ - if ( NULL != current_session ) - { - GNUNET_free ( current_session->ip ); - GNUNET_free ( current_session ); - } - locked = GNUNET_YES; + conn_info = MHD_get_connection_info(session, MHD_CONNECTION_INFO_CLIENT_ADDRESS ); - if (addr->sa_family == AF_INET6) + /* Incoming IPv4 connection */ + if ( AF_INET == conn_info->client_addr->sin_family) { - address = GNUNET_malloc (INET6_ADDRSTRLEN); - addrin6 = (struct sockaddr_in6 *) addr; - inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming IPv6 connection from `%s'\n",address); - } - else if (addr->sa_family == AF_INET) - { - address = GNUNET_malloc(INET_ADDRSTRLEN); - addrin = (struct sockaddr_in *) addr; + address = GNUNET_malloc (INET_ADDRSTRLEN); + addrin = conn_info->client_addr; inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming IPv4 connection from `%s'\n",address); } - /* are there any socket types besides ipv4 and ipv6 we want to support? */ - else + /* Incoming IPv6 connection */ + if ( AF_INET6 == conn_info->client_addr->sin_family) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unsupported connection type for incoming connection \n"); - current_session = NULL; - locked = GNUNET_NO; - return MHD_NO; + address = GNUNET_malloc (INET6_ADDRSTRLEN); + addrin6 = (struct sockaddr_in6 *) conn_info->client_addr; + inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN); } - /* get existing session for this address */ + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from `%s'\n",method, address); + + /* find session for address */ cs = NULL; if (plugin->session_count > 0) { cs = plugin->sessions; while ( NULL != cs) { - if ( 0 == strcmp(address,cs->ip)) + if ( 0 == memcmp(conn_info->client_addr,cs->addr, sizeof (struct sockaddr_in))) { /* existing session for this address found */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session `%s' found\n",address); break; } cs = cs->next; } } - if (cs != NULL ) - { - /* session to this addresse already existing */ - current_session = cs; - } - else + if (cs == NULL ) { /* create new session object */ - current_session = GNUNET_malloc ( sizeof( struct Session) ); - current_session->ip = address; - current_session->next = NULL; - } - /* Every connection is accepted, nothing more to do here */ - return MHD_YES; -} - + cs = GNUNET_malloc ( sizeof( struct Session) ); + cs->ip = address; + cs->addr = conn_info->client_addr; + cs->next = NULL; + cs->is_active = GNUNET_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) -{ - struct MHD_Response *response; - struct Session * cs; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from `%s'\n",method, current_session->ip); - - /* Check if new or already known session */ - if ( NULL != current_session ) - { /* Insert session into linked list */ if ( plugin->sessions == NULL) { - plugin->sessions = current_session; + plugin->sessions = cs; plugin->session_count = 1; } - - cs = plugin->sessions; - while ( cs->next != NULL ) + cs_temp = plugin->sessions; + while ( cs_temp->next != NULL ) { - cs = cs->next; + cs_temp = cs_temp->next; } - - if (cs != current_session) + if (cs_temp != cs ) { - cs->next = current_session; + cs_temp->next = cs; plugin->session_count++; } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", address, plugin->session_count); + } - /* iter over list */ - cs = plugin->sessions; - while (cs!=NULL) - { - cs = cs->next; - } - /* Set closure */ - if (*httpSessionCache == NULL) - { - *httpSessionCache = current_session; - } + /* Set closure */ + if (*httpSessionCache == NULL) + { + *httpSessionCache = cs; } - /* Since connection is established, we can unlock */ - locked = GNUNET_NO; - current_session = NULL; /* Is it a PUT or a GET request */ if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) ) @@ -452,7 +432,6 @@ accessHandlerCallback (void *cls, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %lu \n",(*upload_data_size)); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"PUT Request: `%s'\n",upload_data); - /* FIXME: GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# PUT requests"), 1, GNUNET_NO); */ /* No data left */ *upload_data_size = 0; response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO); @@ -462,7 +441,6 @@ accessHandlerCallback (void *cls, if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) ) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n"); - //GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# GET requests"), 1, GNUNET_NO); } return MHD_YES; @@ -776,8 +754,10 @@ libgnunet_plugin_transport_http_done (void *cls) cs = plugin->sessions; while ( NULL != cs) { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session to `%s'\n",cs->ip); cs_next = cs->next; GNUNET_free (cs->ip); + GNUNET_free (cs->addr); GNUNET_free (cs); plugin->session_count--; cs = cs_next; @@ -811,6 +791,8 @@ libgnunet_plugin_transport_http_init (void *cls) api->check_address = &template_plugin_address_suggested; api->address_to_string = &template_plugin_address_to_string; + hostname = GNUNET_RESOLVER_local_fqdn_get (); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n"); /* Reading port number from config file */ if ((GNUNET_OK != @@ -852,8 +834,6 @@ libgnunet_plugin_transport_http_init (void *cls) MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL, MHD_OPTION_END); } - - locked = GNUNET_NO; if (http_daemon_v4 != NULL) http_task_v4 = http_daemon_prepare (http_daemon_v4); if (http_daemon_v6 != NULL)