gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r11435 - gnunet/src/transport


From: gnunet
Subject: [GNUnet-SVN] r11435 - gnunet/src/transport
Date: Wed, 19 May 2010 16:19:49 +0200

Author: wachs
Date: 2010-05-19 16:19:49 +0200 (Wed, 19 May 2010)
New Revision: 11435

Modified:
   gnunet/src/transport/plugin_transport_http.c
Log:


Modified: gnunet/src/transport/plugin_transport_http.c
===================================================================
--- gnunet/src/transport/plugin_transport_http.c        2010-05-19 12:15:42 UTC 
(rev 11434)
+++ gnunet/src/transport/plugin_transport_http.c        2010-05-19 14:19:49 UTC 
(rev 11435)
@@ -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 <curl/curl.h>
@@ -104,11 +105,21 @@
   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?
    */
   struct GNUNET_TIME_Absolute last_quota_update;
@@ -182,13 +193,11 @@
 static CURLM *multi_handle;
 
 /**
- * session of current incoming connection
+ * Our hostname
  */
-static struct Session  * current_session;
+static char * hostname;
 
-static unsigned int locked;
 
-
 /**
  * Finds a http session in our linked list using peer identity as a key
  * @param peer peeridentity
@@ -297,8 +306,13 @@
  */
 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 @@
 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 )
+  conn_info = MHD_get_connection_info(session, 
MHD_CONNECTION_INFO_CLIENT_ADDRESS );
+
+  /* Incoming IPv4 connection */
+  if ( AF_INET == conn_info->client_addr->sin_family)
   {
-    GNUNET_free ( current_session->ip );
-    GNUNET_free ( current_session );
+    address = GNUNET_malloc (INET_ADDRSTRLEN);
+    addrin = conn_info->client_addr;
+    inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
   }
-
-  locked = GNUNET_YES;
-
-  if (addr->sa_family == AF_INET6)
+  /* Incoming IPv6 connection */
+  if ( AF_INET6 == conn_info->client_addr->sin_family)
   {
     address = GNUNET_malloc (INET6_ADDRSTRLEN);
-    addrin6 = (struct sockaddr_in6 *) addr;
+    addrin6 = (struct sockaddr_in6 *) conn_info->client_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;
-    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
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unsupported connection type for 
incoming connection \n");
-    current_session = NULL;
-    locked = GNUNET_NO;
-    return MHD_NO;
-  }
 
-  /* 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 )
+  if (cs == NULL )
   {
-    /* session to this addresse already existing */
-    current_session = cs;
-  }
-  else
-  {
     /* 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 @@
     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 @@
   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 @@
   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 @@
   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 @@
                                        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)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]