(no commit message)
[oweals/gnunet.git] / src / transport / plugin_transport_http.c
index 3421169eea87aaa49b912029d31d3d8822bbd3a6..f71acf4b0cbb0c703748bc67a60a900962747a9c 100644 (file)
@@ -42,7 +42,7 @@
 
 #define DEBUG_CURL GNUNET_NO
 #define DEBUG_HTTP GNUNET_NO
-#define DEBUG_CONNECTIONS GNUNET_NO
+#define DEBUG_CONNECTIONS GNUNET_YES
 
 #define INBOUND GNUNET_NO
 #define OUTBOUND GNUNET_YES
@@ -153,6 +153,9 @@ struct HTTP_Message
 
 struct HTTP_PeerContext
 {
+  /**
+   * peer's identity
+   */
   struct GNUNET_PeerIdentity identity;
 
   /**
@@ -160,8 +163,22 @@ struct HTTP_PeerContext
    */
   struct Plugin *plugin;
 
+  /**
+   * Linked list of connections with this peer
+   * head
+   */
   struct Session * head;
+
+  /**
+   * Linked list of connections with this peer
+   * tail
+   */
   struct Session * tail;
+
+  /**
+   * id for next session
+   */
+  size_t session_id_counter;
 };
 
 
@@ -256,15 +273,23 @@ struct Session
    */
   unsigned int recv_force_disconnect;
 
+  /**
+   * id for next session
+   * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
+   */
+  size_t session_id;
+
   /**
    * entity managing sending data
-   * outbound session: pointer to curl easy handle
+   * outbound session: CURL *
+   * inbound session: mhd_connection *
    */
   void * send_endpoint;
 
   /**
    * entity managing recieving data
-   * outbound session: pointer to curl easy handle
+   * outbound session: CURL *
+   * inbound session: mhd_connection *
    */
   void * recv_endpoint;
 };
@@ -337,16 +362,16 @@ http_plugin_address_to_string (void *cls,
                                    const void *addr,
                                    size_t addrlen);
 
-static char * create_url(void * cls, const void * addr, size_t addrlen)
+static char * create_url(void * cls, const void * addr, size_t addrlen, size_t id)
 {
   struct Plugin *plugin = cls;
   char *url = NULL;
 
   GNUNET_assert ((addr!=NULL) && (addrlen != 0));
   GNUNET_asprintf(&url,
-                  "http://%s/%s",
+                  "http://%s/%s;%u",
                   http_plugin_address_to_string(NULL, addr, addrlen),
-                  (char *) (&plugin->my_ascii_hash_ident));
+                  (char *) (&plugin->my_ascii_hash_ident),id);
 
   return url;
 }
@@ -357,14 +382,46 @@ static char * create_url(void * cls, const void * addr, size_t addrlen)
  * @param msg message to remove
  * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
  */
-
-static int remove_http_message(struct Session * ps, struct HTTP_Message * msg)
+static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
 {
   GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
   GNUNET_free(msg);
   return GNUNET_OK;
 }
 
+/**
+ * Removes a session from the linked list of sessions
+ * @param pc peer context
+ * @param ps session
+ * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
+ * @param call_msg_cont_result, result to call message continuations with
+ * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
+ */
+static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps,  int call_msg_cont, int call_msg_cont_result)
+{
+  struct HTTP_Message * msg;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: removing %s session with id %u\n", ps, (ps->direction == INBOUND) ? "inbound" : "outbound",ps->session_id);
+  GNUNET_free_non_null (ps->addr);
+  GNUNET_SERVER_mst_destroy (ps->msgtok);
+  GNUNET_free(ps->url);
+
+  msg = ps->pending_msgs_head;
+  while (msg!=NULL)
+  {
+    if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
+    {
+      msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
+    }
+    GNUNET_free(msg);
+    GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
+  }
+
+  GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
+  GNUNET_free(ps);
+  ps = NULL;
+  return GNUNET_OK;
+}
+
 static struct Session * get_Session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addr_len)
 {
   struct Session * cc = pc->head;
@@ -378,7 +435,9 @@ static struct Session * get_Session (void * cls, struct HTTP_PeerContext *pc, co
     {
       if (0 == memcmp(cc->addr, addr, addr_len))
       {
-        con = cc;
+        /* connection can not be used, since it is disconnected */
+        if ((cc->recv_force_disconnect==GNUNET_NO) && (cc->send_force_disconnect==GNUNET_NO))
+          con = cc;
         break;
       }
     }
@@ -392,17 +451,38 @@ static struct Session * get_Session (void * cls, struct HTTP_PeerContext *pc, co
 /**
  * Callback called by MHD when a connection is terminated
  */
-static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
+static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
 {
   struct Session * ps = *httpSessionCache;
   if (ps == NULL)
     return;
+  struct HTTP_PeerContext * pc = ps->peercontext;
 
+  if (connection==ps->recv_endpoint)
+  {
+#if DEBUG_CONNECTIONS
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
+#endif
+    ps->recv_active = GNUNET_NO;
+    ps->recv_connected = GNUNET_NO;
+    ps->recv_endpoint = NULL;
+  }
+  if (connection==ps->send_endpoint)
+  {
+
+    ps->send_active = GNUNET_NO;
+    ps->send_connected = GNUNET_NO;
+    ps->send_endpoint = NULL;
 #if DEBUG_CONNECTIONS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&ps->peercontext->identity));
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
 #endif
-  /* session set to inactive */
-  //ps-> = GNUNET_NO;
+  }
+
+  /* if both connections disconnected, remove session */
+  if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
+  {
+    remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
+  }
 }
 
 static void mhd_write_mst_cb (void *cls,
@@ -416,7 +496,8 @@ static void mhd_write_mst_cb (void *cls,
   GNUNET_assert(pc != NULL);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
+             "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
+             ps,
              ntohs(message->type),
               ntohs(message->size),
              GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
@@ -428,7 +509,7 @@ static void mhd_write_mst_cb (void *cls,
                            ps->addrlen);
 }
 
-static void curl_write_mst_cb  (void *cls,
+static void curl_receive_mst_cb  (void *cls,
                                 void *client,
                                 const struct GNUNET_MessageHeader *message)
 {
@@ -455,7 +536,7 @@ static void curl_write_mst_cb  (void *cls,
  * Check if ip is allowed to connect.
  */
 static int
-acceptPolicyCallback (void *cls,
+mhd_accept_cb (void *cls,
                       const struct sockaddr *addr, socklen_t addr_len)
 {
 #if 0
@@ -465,7 +546,7 @@ acceptPolicyCallback (void *cls,
   return MHD_YES;
 }
 
-int server_read_callback (void *cls, uint64_t pos, char *buf, int max)
+int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
 {
   int bytes_read = 0;
 
@@ -477,8 +558,7 @@ int server_read_callback (void *cls, uint64_t pos, char *buf, int max)
   GNUNET_assert (ps!=NULL);
   pc = ps->peercontext;
   msg = ps->pending_msgs_tail;
-
-  if (ps->send_force_disconnect)
+  if (ps->send_force_disconnect==GNUNET_YES)
   {
 #if DEBUG_CONNECTIONS
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
@@ -488,23 +568,30 @@ int server_read_callback (void *cls, uint64_t pos, char *buf, int max)
 
   if (msg!=NULL)
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"mhd_send_callback %X: queue msg size: %u, max %u pos %u msg->pos %u\n",ps,msg->size,max,pos,msg->pos);
     if ((msg->size-msg->pos) <= max)
-      {
-        memcpy(buf,&msg->buf[pos],(msg->size-msg->pos));
-        pos+=(msg->size-msg->pos);
-      }
-      else
-      {
-        memcpy(buf,&msg->buf[pos],max);
-        pos+=max;
-      }
+    {
+      memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
+      bytes_read = msg->size-msg->pos;
+      msg->pos+=(msg->size-msg->pos);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"mhd_send_callback %X: complete: size %u pos %u bytes read %u \n",ps,msg->size,msg->pos,bytes_read);
+    }
+    else
+    {
+      memcpy(buf,&msg->buf[msg->pos],max);
+      msg->pos+=max;
+      bytes_read = max;
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"mhd_send_callback %X: partial: size %u pos %u bytes read %u \n",ps,msg->size,msg->pos,bytes_read);
+    }
 
-      if (msg->pos==msg->size)
-      {
-        if (NULL!=msg->transmit_cont)
-          msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
-        res = remove_http_message(ps,msg);
-      }
+    if (msg->pos==msg->size)
+    {
+      struct GNUNET_MessageHeader * tmp = (struct GNUNET_MessageHeader *) msg->buf;
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"MHD SENT MESSAGE %u bytes msg->type %u msg->size %u\n", bytes_read, ntohs(tmp->type), ntohs(tmp->size));
+      if (NULL!=msg->transmit_cont)
+        msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
+      res = remove_http_message(ps,msg);
+    }
   }
   return bytes_read;
 }
@@ -517,7 +604,7 @@ int server_read_callback (void *cls, uint64_t pos, char *buf, int max)
  * already exists and create a new one if not.
  */
 static int
-accessHandlerCallback (void *cls,
+mdh_access_cb (void *cls,
                        struct MHD_Connection *mhd_connection,
                        const char *url,
                        const char *method,
@@ -534,12 +621,14 @@ accessHandlerCallback (void *cls,
 
   char address[INET6_ADDRSTRLEN+14];
   struct GNUNET_PeerIdentity pi_in;
+  size_t id_num = 0;
 
   struct IPv4HttpAddress ipv4addr;
   struct IPv6HttpAddress ipv6addr;
 
   struct HTTP_PeerContext *pc;
   struct Session *ps;
+  struct Session *ps_tmp;
 
   int res = GNUNET_NO;
   int send_error_to_client;
@@ -552,7 +641,20 @@ accessHandlerCallback (void *cls,
   if (NULL == *httpSessionCache)
   {
     /* check url for peer identity , if invalid send HTTP 404*/
-    res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
+    size_t len = strlen(&url[1]);
+    char * peer = GNUNET_malloc(104+1);
+
+    if ((len>104) && (url[104]==';'))
+    {
+        char * id = GNUNET_malloc((len-104)+1);
+        strcpy(id,&url[105]);
+        memcpy(peer,&url[1],103);
+        peer[103] = '\0';
+        id_num = strtoul ( id, NULL , 10);
+        GNUNET_free(id);
+    }
+    res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
+    GNUNET_free(peer);
     if ( GNUNET_SYSERR == res )
     {
       response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
@@ -580,6 +682,7 @@ accessHandlerCallback (void *cls,
     {
       pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
       pc->plugin = plugin;
+      pc->session_id_counter=1;
       memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
       GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
     }
@@ -594,7 +697,6 @@ accessHandlerCallback (void *cls,
       ipv4addr.u_port = addrin->sin_port;
       addr = &ipv4addr;
       addr_len = sizeof(struct IPv4HttpAddress);
-      //con = session_check_inbound_address (plugin, cs, (const void *) &ipv4addr, sizeof (struct IPv4HttpAddress));
     }
     /* Incoming IPv6 connection */
     if ( AF_INET6 == conn_info->client_addr->sin_family)
@@ -606,8 +708,24 @@ accessHandlerCallback (void *cls,
       addr = &ipv6addr;
       addr_len = sizeof(struct IPv6HttpAddress);
     }
-    /* Set closure and update current session*/
-    ps = get_Session(plugin, pc, addr, addr_len);
+
+
+    //ps = get_Session(plugin, pc, addr, addr_len);
+    ps = NULL;
+    /* only inbound sessions here */
+
+    ps_tmp = pc->head;
+    while (ps_tmp!=NULL)
+    {
+      if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
+      {
+        if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
+        ps=ps_tmp;
+        break;
+      }
+      ps_tmp=ps_tmp->next;
+    }
+
     if (ps==NULL)
     {
       ps = GNUNET_malloc(sizeof (struct Session));
@@ -622,7 +740,8 @@ accessHandlerCallback (void *cls,
       ps->recv_connected=GNUNET_NO;
       ps->recv_active=GNUNET_NO;
       ps->peercontext=pc;
-      ps->url = create_url (plugin, ps->addr, ps->addrlen);
+      ps->session_id =id_num;
+      ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
       GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
     }
 
@@ -630,7 +749,8 @@ accessHandlerCallback (void *cls,
     if (ps->msgtok==NULL)
       ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
 
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
+                ps,
                 method,
                 GNUNET_i2s(&pc->identity),
                 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
@@ -642,15 +762,19 @@ accessHandlerCallback (void *cls,
     if (ps->recv_force_disconnect)
     {
 #if DEBUG_CONNECTIONS
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound forced to disconnect\n",ps);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
 #endif
-      ps->recv_connected = GNUNET_NO;
       ps->recv_active = GNUNET_NO;
       return MHD_NO;
     }
     if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
     {
+      ps->recv_endpoint = mhd_connection;
+      ps->recv_connected = GNUNET_YES;
       ps->recv_active = GNUNET_YES;
+#if DEBUG_CONNECTIONS
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
+#endif
       return MHD_YES;
     }
 
@@ -682,16 +806,21 @@ accessHandlerCallback (void *cls,
     if (ps->send_force_disconnect)
     {
 #if DEBUG_CONNECTIONS
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was  forced to disconnect\n",ps);
 #endif
-      ps->send_connected = GNUNET_NO;
       ps->send_active = GNUNET_NO;
       return MHD_NO;
     }
-    response = MHD_create_response_from_callback(-1,32 * 1024, &server_read_callback, ps, NULL);
+    ps->send_connected = GNUNET_YES;
+    ps->send_active = GNUNET_YES;
+    ps->send_endpoint = mhd_connection;
+#if DEBUG_CONNECTIONS
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
+#endif
+    response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
     res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
     MHD_destroy_response (response);
-    return res;
+    return MHD_YES;
   }
   return MHD_NO;
 }
@@ -829,8 +958,7 @@ static void http_server_daemon_v6_run (void *cls,
  * @param con connection
  * @return bytes sent to peer
  */
-static ssize_t send_check_connections (void *cls, struct Session* ses , struct Session *ps);
-
+static ssize_t send_check_connections (void *cls, struct Session *ps);
 
 static size_t curl_get_header_function( void *ptr, size_t size, size_t nmemb, void *stream)
 {
@@ -855,7 +983,7 @@ static size_t curl_get_header_function( void *ptr, size_t size, size_t nmemb, vo
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
 #endif
         // Calling send_check_connections again since receive is established
-        send_check_connections (ps->peercontext->plugin, NULL, ps);
+        send_check_connections (ps->peercontext->plugin, ps);
       }
     }
   }
@@ -945,7 +1073,7 @@ static size_t curl_put_header_function( void *ptr, size_t size, size_t nmemb, vo
  * @param ptr source pointer, passed to the libcurl handle
  * @return bytes written to stream
  */
-static size_t send_curl_send_callback(void *stream, size_t size, size_t nmemb, void *ptr)
+static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
 {
   struct Session * ps = ptr;
   struct HTTP_Message * msg = ps->pending_msgs_tail;
@@ -1009,14 +1137,17 @@ static size_t send_curl_send_callback(void *stream, size_t size, size_t nmemb, v
 * @param ptr destination pointer, passed to the libcurl handle
 * @return bytes read from stream
 */
-static size_t send_curl_receive_callback( void *stream, size_t size, size_t nmemb, void *ptr)
+static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
 {
   struct Session * ps = ptr;
 #if DEBUG_CONNECTIONS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes recieved\n",ps, size*nmemb);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
 #endif
-  GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
 
+  struct GNUNET_MessageHeader * msg = stream;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes msg->type %u msg->size %u\n",ps, size*nmemb, ntohs(msg->type), ntohs(msg->size));
+
+  // GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
   return (size * nmemb);
 
 }
@@ -1038,7 +1169,7 @@ static size_t send_schedule(void *cls, struct Session* ses );
  * @param con connection
  * @return bytes sent to peer
  */
-static ssize_t send_check_connections (void *cls, struct Session* ses , struct Session *ps)
+static ssize_t send_check_connections (void *cls, struct Session *ps)
 {
   struct Plugin *plugin = cls;
   int bytes_sent = 0;
@@ -1063,9 +1194,9 @@ static ssize_t send_check_connections (void *cls, struct Session* ses , struct S
         curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
         curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_function);
         curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
-        curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, send_curl_send_callback);
+        curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
         curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
-        curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, send_curl_receive_callback);
+        curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
         curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
         curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
         curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
@@ -1082,8 +1213,6 @@ static ssize_t send_check_connections (void *cls, struct Session* ses , struct S
           return -1;
         }
         bytes_sent = send_schedule (plugin, NULL);
-        if (ps->msgtok != NULL)
-          ps->msgtok = GNUNET_SERVER_mst_create (&curl_write_mst_cb, ps);
 #if DEBUG_CONNECTIONS
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
 #endif
@@ -1133,9 +1262,9 @@ static ssize_t send_check_connections (void *cls, struct Session* ses , struct S
     curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
     curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_function);
     curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
-    curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, send_curl_send_callback);
+    curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
     curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
-    curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, send_curl_receive_callback);
+    curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
     curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
     curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
     curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
@@ -1299,11 +1428,8 @@ static void send_execute (void *cls,
                       ps->recv_endpoint=NULL;
                     }
                   }
-                  if (ps->pending_msgs_tail != NULL)
-                  {
-                    if (ps->pending_msgs_tail->pos>0)
-                      remove_http_message(ps, ps->pending_msgs_tail);
-                  }
+                  if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
+                    remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
                   return;
                 default:
                   break;
@@ -1434,10 +1560,25 @@ http_plugin_send (void *cls,
   struct HTTP_Message *msg;
 
   struct HTTP_PeerContext * pc;
-  struct Session * ps;
+  struct Session * ps = NULL;
+  struct Session * ps_tmp = NULL;
 
   GNUNET_assert(cls !=NULL);
-  GNUNET_assert ((addr!=NULL) && (addrlen != 0));
+
+  char * force = GNUNET_malloc(40);
+  if (force_address == GNUNET_YES)
+    strcpy(force,"forced addr.");
+  if (force_address == GNUNET_NO)
+    strcpy(force,"any addr.");
+  if (force_address == GNUNET_SYSERR)
+    strcpy(force,"reliable bi-direc. address addr.");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
+                                      msgbuf_size,
+                                      GNUNET_i2s(target),
+                                      force,
+                                      http_plugin_address_to_string(NULL, addr, addrlen),
+                                      session);
+  GNUNET_free(force);
 
   pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
   /* Peer unknown */
@@ -1445,78 +1586,100 @@ http_plugin_send (void *cls,
   {
     pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
     pc->plugin = plugin;
+    pc->session_id_counter=1;
     memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
     GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
   }
-  ps = get_Session(plugin, pc, addr, addrlen);
 
-  /* session not existing, but address forced -> creating new session */
-  if ((force_address == GNUNET_YES) && (ps==NULL))
+  /* Search for existing session using the passed address */
+  if  ((addr!=NULL) && (addrlen != 0))
   {
-    ps = GNUNET_malloc(sizeof (struct Session));
-    ps->addr = GNUNET_malloc(addrlen);
-    memcpy(ps->addr,addr,addrlen);
-    ps->addrlen = addrlen;
-    ps->direction=OUTBOUND;
-    ps->recv_connected = GNUNET_NO;
-    ps->send_connected = GNUNET_NO;
-    ps->pending_msgs_head = NULL;
-    ps->pending_msgs_tail = NULL;
-    ps->peercontext=pc;
-    ps->url = create_url (plugin, ps->addr, ps->addrlen);
-    GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
+    ps = get_Session(plugin, pc, addr, addrlen);
   }
-  /* session not existing, address not forced -> looking for other session */
-  if ((ps==NULL) && (force_address == GNUNET_NO))
-  {
-    //struct PeerContext * tmp = pc->head;
+  if (ps != NULL)
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Found existing connection to peer %s with given address, using %X\n", GNUNET_i2s(target), ps);
 
-
-    ps = GNUNET_malloc(sizeof (struct Session));
-    ps->addr = GNUNET_malloc(addrlen);
-    memcpy(ps->addr,addr,addrlen);
-    ps->addrlen = addrlen;
-    ps->direction=OUTBOUND;
-    ps->recv_connected = GNUNET_NO;
-    ps->send_connected = GNUNET_NO;
-    ps->pending_msgs_head = NULL;
-    ps->pending_msgs_tail = NULL;
-    ps->peercontext=pc;
-    ps->url = create_url (plugin, ps->addr, ps->addrlen);
-    GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
+  /* Search for existing session using the passed session */
+  if ((ps==NULL) && (force_address != GNUNET_YES))
+  {
+    ps_tmp = pc->head;
+    while (ps_tmp!=NULL)
+    {
+      if ((ps_tmp==session) && (ps_tmp->recv_force_disconnect==GNUNET_NO) && (ps_tmp->send_force_disconnect==GNUNET_NO) &&
+          (ps_tmp->recv_connected==GNUNET_YES) && (ps_tmp->send_connected==GNUNET_YES))
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Found existing connection to peer %s with given session, using inbound session %X\n", GNUNET_i2s(target), ps_tmp);
+        ps = ps_tmp;
+        break;
+      }
+      ps_tmp=ps_tmp->next;
+    }
   }
-  if ((ps==NULL) && (force_address == GNUNET_SYSERR))
+
+  /* session not existing, address not forced -> looking for other session */
+  if ((ps==NULL) && (force_address != GNUNET_YES))
   {
-    /* FIXME: CREATING SESSION, SHOULD CHOOSE EXISTING */
-    ps = GNUNET_malloc(sizeof (struct Session));
-    ps->addr = GNUNET_malloc(addrlen);
-    memcpy(ps->addr,addr,addrlen);
-    ps->addrlen = addrlen;
-    ps->direction=OUTBOUND;
-    ps->recv_connected = GNUNET_NO;
-    ps->send_connected = GNUNET_NO;
-    ps->pending_msgs_head = NULL;
-    ps->pending_msgs_tail = NULL;
-    ps->peercontext=pc;
-    ps->url = create_url (plugin, ps->addr, ps->addrlen);
-    GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection, but free to choose existing, searching for existing connection to peer %s\n", GNUNET_i2s(target));
+    /* Choosing different session to peer when possible */
+    struct Session * tmp = pc->head;
+    while (tmp!=NULL)
+    {
+      if ((tmp->recv_connected) && (tmp->send_connected) && (tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
+      {
+        ps = tmp;
+      }
+      tmp = tmp->next;
+    }
+    if (ps != NULL)
+     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection to peer %s, selected connection %X\n", GNUNET_i2s(target),ps);
+    else
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection to peer %s, no connection found\n", GNUNET_i2s(target));
   }
 
-  char * force = GNUNET_malloc(30);
-  if (force_address == GNUNET_YES)
-    strcpy(force,"forced addr.");
-  if (force_address == GNUNET_NO)
-    strcpy(force,"any addr.");
-  if (force_address == GNUNET_SYSERR)
-    strcpy(force,"reliable bi-direc. address addr.");
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' %s (%s), session: %X\n",
-                                      msgbuf_size,
-                                      GNUNET_i2s(&pc->identity),
-                                      force,
-                                      http_plugin_address_to_string(NULL, addr, addrlen),
-                                      ps);
+  /* session not existing, but address forced -> creating new session */
+  if ((ps==NULL) || ((ps==NULL) && (force_address == GNUNET_YES)))
+  {
+    if ((addr!=NULL) && (addrlen!=0))
+    {
+      if (force_address == GNUNET_YES)
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new connection to peer %s\n", GNUNET_i2s(target));
+      if (force_address != GNUNET_YES)
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new connection to peer %s\n", GNUNET_i2s(target));
+
+      ps = GNUNET_malloc(sizeof (struct Session));
+      if ((addrlen!=0) && (addr!=NULL))
+      {
+      ps->addr = GNUNET_malloc(addrlen);
+      memcpy(ps->addr,addr,addrlen);
+      ps->addrlen = addrlen;
+      }
+      else
+      {
+        ps->addr = NULL;
+        ps->addrlen = 0;
+      }
+      ps->direction=OUTBOUND;
+      ps->recv_connected = GNUNET_NO;
+      ps->recv_force_disconnect = GNUNET_NO;
+      ps->send_connected = GNUNET_NO;
+      ps->send_force_disconnect = GNUNET_NO;
+      ps->pending_msgs_head = NULL;
+      ps->pending_msgs_tail = NULL;
+      ps->peercontext=pc;
+      ps->session_id = pc->session_id_counter;
+      pc->session_id_counter++;
+      ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
+      if (ps->msgtok == NULL)
+        ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
+      GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
+    }
+    else
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing session & and no address given: no way to send this message to peer `%s'!\n", GNUNET_i2s(target));
+      return -1;
+    }
+  }
 
-  GNUNET_free(force);
   /* create msg */
   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
   msg->next = NULL;
@@ -1528,7 +1691,7 @@ http_plugin_send (void *cls,
   memcpy (msg->buf,msgbuf, msgbuf_size);
   GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
 
-  return send_check_connections (plugin, session, ps);
+  return send_check_connections (plugin, ps);
 }
 
 
@@ -1565,12 +1728,14 @@ http_plugin_disconnect (void *cls,
         curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
         curl_easy_cleanup(ps->send_endpoint);
         ps->send_endpoint=NULL;
+        ps->send_force_disconnect = GNUNET_YES;
       }
       if (ps->recv_endpoint!=NULL)
       {
        curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
        curl_easy_cleanup(ps->recv_endpoint);
        ps->recv_endpoint=NULL;
+       ps->recv_force_disconnect = GNUNET_YES;
       }
     }
     if (ps->direction==INBOUND)
@@ -1582,9 +1747,7 @@ http_plugin_disconnect (void *cls,
     {
       remove_http_message(ps, ps->pending_msgs_head);
     }
-    ps->recv_connected = GNUNET_NO;
     ps->recv_active = GNUNET_NO;
-    ps->send_connected = GNUNET_NO;
     ps->send_active = GNUNET_NO;
     ps=ps->next;
   }
@@ -1826,7 +1989,7 @@ process_interfaces (void *cls,
   return GNUNET_OK;
 }
 
-int peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
+int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
 {
   struct HTTP_PeerContext * pc = value;
   struct Session * ps = pc->head;
@@ -1840,7 +2003,7 @@ int peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
   {
     tmp = ps->next;
 
-    GNUNET_free(ps->addr);
+    GNUNET_free_non_null (ps->addr);
     GNUNET_free(ps->url);
     if (ps->msgtok != NULL)
       GNUNET_SERVER_mst_destroy (ps->msgtok);
@@ -1911,7 +2074,7 @@ libgnunet_plugin_transport_http_done (void *cls)
 
   /* free all peer information */
   GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
-                                         &peer_context_Iterator,
+                                         &remove_peer_context_Iterator,
                                          NULL);
   GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
 
@@ -1979,23 +2142,23 @@ libgnunet_plugin_transport_http_init (void *cls)
     {
     plugin->http_server_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
                                        port,
-                                       &acceptPolicyCallback,
-                                       plugin , &accessHandlerCallback, plugin,
+                                       &mhd_accept_cb,
+                                       plugin , &mdh_access_cb, plugin,
                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
                                        MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
-                                       MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
+                                       MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
                                        MHD_OPTION_END);
     plugin->http_server_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
                                        port,
-                                       &acceptPolicyCallback,
-                                       plugin , &accessHandlerCallback, plugin,
+                                       &mhd_accept_cb,
+                                       plugin , &mdh_access_cb, plugin,
                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
                                        MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
-                                       MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
+                                       MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
                                        MHD_OPTION_END);
     }
   if (plugin->http_server_daemon_v4 != NULL)