(no commit message)
[oweals/gnunet.git] / src / transport / plugin_transport_http.c
index 923c2bcf0e689a67f481088d19f73678e14a31fc..7984529343dd4381b8a54a5b311ed3c0b18eb5f6 100644 (file)
 #include "gnunet_transport_service.h"
 #include "gnunet_resolver_service.h"
 #include "plugin_transport.h"
+#include "gnunet_os_lib.h"
 #include "microhttpd.h"
 #include <curl/curl.h>
 
+
+#define DEBUG_CURL GNUNET_CURL
 #define DEBUG_HTTP GNUNET_NO
 
 /**
  */
 #define HTTP_CONNECT_TIMEOUT 30
 
+/**
+ * Timeout for a http connect
+ */
+#define HTTP_MESSAGE_INITIAL_BUFFERSIZE GNUNET_SERVER_MAX_MESSAGE_SIZE
+
 
 /**
  * Encapsulation of all of the state of the plugin.
  */
 struct Plugin;
 
+/**
+ * Network format for IPv4 addresses.
+ */
+struct IPv4HttpAddress
+{
+  /**
+   * IPv4 address, in network byte order.
+   */
+  uint32_t ipv4_addr;
+
+  /**
+   * Port number, in network byte order.
+   */
+  uint16_t u_port;
+
+};
+
+
+/**
+ * Network format for IPv6 addresses.
+ */
+struct IPv6HttpAddress
+{
+  /**
+   * IPv6 address.
+   */
+  struct in6_addr ipv6_addr;
+
+  /**
+   * Port number, in network byte order.
+   */
+  uint16_t u6_port;
+
+};
+
+
+
 /**
  *  Message to send using http
  */
@@ -92,9 +137,21 @@ struct HTTP_Message
   /**
    * amount of data to sent
    */
-  size_t size;
-
   size_t len;
+
+  char * dest_url;
+
+  /**
+   * Continuation function to call once the transmission buffer
+   * has again space available.  NULL if there is no
+   * continuation to call.
+   */
+  GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
+
+  /**
+   * Closure for transmit_cont.
+   */
+  void *transmit_cont_cls;
 };
 
 
@@ -114,18 +171,6 @@ struct Session
    */
   struct Plugin *plugin;
 
-  /**
-   * Continuation function to call once the transmission buffer
-   * has again space available.  NULL if there is no
-   * continuation to call.
-   */
-  GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
-
-  /**
-   * Closure for transmit_cont.
-   */
-  void *transmit_cont_cls;
-
   /**
    * To whom are we talking to (set to our identity
    * if we are still waiting for the welcome message)
@@ -145,7 +190,12 @@ struct Session
   /**
    * Sender's ip address to distinguish between incoming connections
    */
-  struct sockaddr_in * addr;
+  struct sockaddr_in * addr_inbound;
+
+  /**
+   * Sender's ip address recieved by transport
+   */
+  struct sockaddr_in * addr_outbound;
 
   /**
    * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
@@ -179,6 +229,11 @@ struct Session
    */
   unsigned int is_put_in_progress;
 
+  /**
+   * Is there a HTTP/PUT in progress?
+   */
+  unsigned int is_bad_request;
+
   /**
    * Encoded hash
    */
@@ -186,6 +241,8 @@ struct Session
 
   struct HTTP_Message * pending_outbound_msg;;
 
+  struct HTTP_Message * pending_inbound_msg;
+
   CURL *curl_handle;
 };
 
@@ -204,6 +261,8 @@ struct Plugin
    */
   struct GNUNET_SERVICE_Context *service;
 
+  unsigned int port_inbound;
+
   /**
    * List of open sessions.
    */
@@ -238,7 +297,7 @@ static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
 
 
 /**
- * Our primary task for http daemon handling IPv6 connections
+ * The task sending data
  */
 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
 
@@ -289,41 +348,81 @@ static struct Session * find_session_by_pi( const struct GNUNET_PeerIdentity *pe
   return NULL;
 }
 
+/**
+ * Finds a http session in our linked list using libcurl handle as a key
+ * Needed when sending data with libcurl to differentiate between sessions
+ * @param handle peeridentity
+ * @return http session corresponding to peer identity
+ */
+static struct Session * find_session_by_curlhandle( CURL* handle )
+{
+  struct Session * cur;
+
+  cur = plugin->sessions;
+  while (cur != NULL)
+  {
+    if ( handle == cur->curl_handle )
+      return cur;
+    cur = plugin->sessions->next;
+  }
+  return NULL;
+}
+
 /**
  * Create a new session
  *
- * @param address address the peer is using
- * @peer  peer identity
+ * @param address address the peer is using inbound
+ * @param address address the peer is using outbound
+ * @param peer identity
  * @return created session object
  */
-
-static struct Session * create_session (struct sockaddr_in *address, const struct GNUNET_PeerIdentity *peer)
+static struct Session * create_session (struct sockaddr_in *addr_in, struct sockaddr_in *addr_out, const struct GNUNET_PeerIdentity *peer)
 {
   struct sockaddr_in  *addrin;
   struct sockaddr_in6 *addrin6;
-
   struct Session * ses = GNUNET_malloc ( sizeof( struct Session) );
-  ses->addr = GNUNET_malloc ( sizeof (struct sockaddr_in) );
+
+  ses->addr_outbound = GNUNET_malloc ( sizeof (struct sockaddr_in) );
+
 
   ses->next = NULL;
   ses->plugin = plugin;
-
-  memcpy(ses->addr, address, sizeof (struct sockaddr_in));
-  if ( AF_INET == address->sin_family)
+  if (NULL != addr_in)
   {
-    ses->ip = GNUNET_malloc (INET_ADDRSTRLEN);
-    addrin = address;
-    inet_ntop(addrin->sin_family,&(addrin->sin_addr),ses->ip,INET_ADDRSTRLEN);
+    ses->addr_inbound  = GNUNET_malloc ( sizeof (struct sockaddr_in) );
+    memcpy(ses->addr_inbound, addr_in, sizeof (struct sockaddr_in));
+    if ( AF_INET == addr_in->sin_family)
+    {
+      ses->ip = GNUNET_malloc (INET_ADDRSTRLEN);
+      addrin = addr_in;
+      inet_ntop(addrin->sin_family,&(addrin->sin_addr),ses->ip,INET_ADDRSTRLEN);
+    }
+    if ( AF_INET6 == addr_in->sin_family)
+    {
+      ses->ip = GNUNET_malloc (INET6_ADDRSTRLEN);
+      addrin6 = (struct sockaddr_in6 *) addr_in;
+      inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr) ,ses->ip,INET6_ADDRSTRLEN);
+    }
   }
-  if ( AF_INET6 == address->sin_family)
+  else
+    ses->addr_inbound = NULL;
+
+  if (NULL != addr_out)
   {
-    ses->ip = GNUNET_malloc (INET6_ADDRSTRLEN);
-    addrin6 = (struct sockaddr_in6 *) address;
-    inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr) ,ses->ip,INET6_ADDRSTRLEN);
+    ses->addr_outbound  = GNUNET_malloc ( sizeof (struct sockaddr_in) );
+    memcpy(ses->addr_outbound, addr_out, sizeof (struct sockaddr_in));
   }
+  else
+    ses->addr_outbound = NULL;
+
   memcpy(&ses->sender, peer, sizeof (struct GNUNET_PeerIdentity));
   GNUNET_CRYPTO_hash_to_enc(&ses->sender.hashPubKey,&(ses->hash));
   ses->is_active = GNUNET_NO;
+  ses->pending_inbound_msg = GNUNET_malloc( sizeof (struct HTTP_Message));
+  ses->pending_inbound_msg->buf = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE);
+  ses->pending_inbound_msg->len = GNUNET_SERVER_MAX_MESSAGE_SIZE;
+  ses->pending_inbound_msg->pos = 0;
+
 
   return ses;
 }
@@ -354,11 +453,11 @@ acceptPolicyCallback (void *cls,
                       const struct sockaddr *addr, socklen_t addr_len)
 {
   /* Every connection is accepted, nothing more to do here */
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connect!\n");
   return MHD_YES;
 }
 
 
+int serror;
 
 /**
  * Process GET or PUT request received via MHD.  For
@@ -385,12 +484,12 @@ accessHandlerCallback (void *cls,
   char * address = NULL;
   struct GNUNET_PeerIdentity pi_in;
   int res = GNUNET_NO;
-  size_t bytes_recv;
   struct GNUNET_MessageHeader *gn_msg;
   int send_error_to_client;
 
   gn_msg = NULL;
   send_error_to_client = GNUNET_NO;
+
   if ( NULL == *httpSessionCache)
   {
     /* check url for peer identity */
@@ -449,7 +548,7 @@ accessHandlerCallback (void *cls,
     if (cs == NULL )
     {
       /* create new session object */
-      cs = create_session(conn_info->client_addr, &pi_in);
+      cs = create_session(conn_info->client_addr, NULL, &pi_in);
 
       /* Insert session into linked list */
       if ( plugin->sessions == NULL)
@@ -474,7 +573,7 @@ accessHandlerCallback (void *cls,
     {
       *httpSessionCache = cs;
     }
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`[%s]:%u')\n",method, GNUNET_i2s(&cs->sender),cs->ip,cs->addr->sin_port);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`[%s]:%u')\n",method, GNUNET_i2s(&cs->sender),cs->ip,cs->addr_inbound->sin_port);
   }
   else
   {
@@ -486,63 +585,94 @@ accessHandlerCallback (void *cls,
     /* New  */
     if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_NO))
     {
+      if (cs->pending_inbound_msg->pos !=0 )
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                    _("Incoming message from peer `%s', while existing message with %u bytes was not forwarded to transport'\n"),
+                    GNUNET_i2s(&cs->sender), cs->pending_inbound_msg->pos);
+        cs->pending_inbound_msg->pos = 0;
+      }
       /* not yet ready */
       cs->is_put_in_progress = GNUNET_YES;
+      cs->is_bad_request = GNUNET_NO;
       cs->is_active = GNUNET_YES;
       return MHD_YES;
     }
-    if ( *upload_data_size > 0 )
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"PUT URL: `%s'\n",url);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"PUT Request: %lu bytes: `%s' \n", (*upload_data_size), upload_data);
-      /* No data left */
-      bytes_recv = *upload_data_size ;
-      *upload_data_size = 0;
 
-      /* checking size */
-      if (bytes_recv < sizeof (struct GNUNET_MessageHeader))
+    if ((*upload_data_size > 0) && (cs->is_bad_request != GNUNET_YES))
+    {
+      if ((*upload_data_size + cs->pending_inbound_msg->pos < cs->pending_inbound_msg->len) && (*upload_data_size + cs->pending_inbound_msg->pos <= GNUNET_SERVER_MAX_MESSAGE_SIZE))
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Message too small, is %u bytes, has to be at least %u '\n",bytes_recv, sizeof(struct GNUNET_MessageHeader));
-        send_error_to_client = GNUNET_YES;
+        /* copy uploaded data to buffer */
+        memcpy(&cs->pending_inbound_msg->buf[cs->pending_inbound_msg->pos],upload_data,*upload_data_size);
+        cs->pending_inbound_msg->pos += *upload_data_size;
+        *upload_data_size = 0;
+        return MHD_YES;
       }
-
-      if ( bytes_recv > GNUNET_SERVER_MAX_MESSAGE_SIZE)
+      else
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Message too big, is %u bytes, maximum %u '\n",bytes_recv, GNUNET_SERVER_MAX_MESSAGE_SIZE);
-        send_error_to_client = GNUNET_YES;
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"%u bytes not added to message of %u bytes, message to big\n",*upload_data_size, cs->pending_inbound_msg->pos);
+        cs->is_bad_request = GNUNET_YES;
+        /* (*upload_data_size) bytes not processed */
+        return MHD_YES;
       }
+    }
 
-      struct GNUNET_MessageHeader * gn_msg = GNUNET_malloc (bytes_recv);
-      memcpy (gn_msg,upload_data,bytes_recv);
+    if ((cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_YES))
+    {
+      *upload_data_size = 0;
+      response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
+      res = MHD_queue_response (session, MHD_HTTP_REQUEST_ENTITY_TOO_LARGE, response);
+      if (res == MHD_YES)
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 413 ENTITY TOO LARGE as PUT Response\n");
+        cs->is_bad_request = GNUNET_NO;
+        cs->is_put_in_progress =GNUNET_NO;
+      }
+      MHD_destroy_response (response);
+      return MHD_YES;
+    }
 
-      if ( ntohs(gn_msg->size) != bytes_recv )
+    if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_NO))
+    {
+      send_error_to_client = GNUNET_YES;
+      struct GNUNET_MessageHeader * gn_msg = NULL;
+      /*check message and forward here */
+      /* checking size */
+      if (cs->pending_inbound_msg->pos >= sizeof (struct GNUNET_MessageHeader))
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Message has incorrect size, is %u bytes vs %u recieved'\n",ntohs(gn_msg->size) , bytes_recv);
-        send_error_to_client = GNUNET_YES;
+        gn_msg = GNUNET_malloc (cs->pending_inbound_msg->pos);
+        memcpy (gn_msg,cs->pending_inbound_msg->buf,cs->pending_inbound_msg->pos);
+
+        if ((ntohs(gn_msg->size) == cs->pending_inbound_msg->pos))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved GNUnet message type %u size %u and payload %u \n",ntohs (gn_msg->type), ntohs (gn_msg->size), ntohs (gn_msg->size)-sizeof(struct GNUNET_MessageHeader));
+          /* forwarding message to transport */
+          plugin->env->receive(plugin->env, &(cs->sender), gn_msg, 1, cs , cs->ip, strlen(cs->ip) );
+          send_error_to_client = GNUNET_NO;
+        }
       }
 
-      if ( GNUNET_YES == send_error_to_client)
+      if (send_error_to_client == GNUNET_NO)
+      {
+        response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
+        res = MHD_queue_response (session, MHD_HTTP_OK, response);
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
+        MHD_destroy_response (response);
+      }
+      else
       {
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved malformed message with %u bytes\n", cs->pending_inbound_msg->pos);
         response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
         res = MHD_queue_response (session, MHD_HTTP_BAD_REQUEST, response);
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 400 BAD REQUEST as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
         MHD_destroy_response (response);
-        GNUNET_free (gn_msg);
-        return MHD_NO;
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 400 BAD REQUEST as PUT Response\n");
       }
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved GNUnet message type %u size %u and payload %u \n",ntohs (gn_msg->type), ntohs (gn_msg->size), ntohs (gn_msg->size)-sizeof(struct GNUNET_MessageHeader));
 
-      /* forwarding message to transport */
-      plugin->env->receive(plugin->env, &(cs->sender), gn_msg, 1, cs , cs->ip, strlen(cs->ip) );
-      return MHD_YES;
-    }
-    if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_YES))
-    {
+      GNUNET_free_non_null (gn_msg);
       cs->is_put_in_progress = GNUNET_NO;
-      response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
-      res = MHD_queue_response (session, MHD_HTTP_OK, response);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
-      MHD_destroy_response (response);
+      cs->is_bad_request = GNUNET_NO;
+      cs->pending_inbound_msg->pos = 0;
       return res;
     }
   }
@@ -623,9 +753,8 @@ http_daemon_prepare (struct MHD_Daemon *daemon_handle)
  * Call MHD to process pending requests and then go back
  * and schedule the next run.
  */
-static void
-http_daemon_run (void *cls,
-            const struct GNUNET_SCHEDULER_TaskContext *tc)
+static void http_daemon_run (void *cls,
+                             const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct MHD_Daemon *daemon_handle = cls;
 
@@ -638,8 +767,6 @@ http_daemon_run (void *cls,
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
 
-
-
   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
   if (daemon_handle == http_daemon_v4)
     http_task_v4 = http_daemon_prepare (daemon_handle);
@@ -650,6 +777,9 @@ http_daemon_run (void *cls,
 
 /**
  * Removes a message from the linked list of messages
+ * @param ses session to remove message from
+ * @param msg message to remove
+ * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
  */
 
 static int remove_http_message(struct Session * ses, struct HTTP_Message * msg)
@@ -667,6 +797,7 @@ static int remove_http_message(struct Session * ses, struct HTTP_Message * msg)
   {
     ses->pending_outbound_msg = cur->next;
     GNUNET_free (cur->buf);
+    GNUNET_free (cur->dest_url);
     GNUNET_free (cur);
     cur = NULL;
     return GNUNET_OK;
@@ -682,12 +813,60 @@ static int remove_http_message(struct Session * ses, struct HTTP_Message * msg)
 
   cur->next = cur->next->next;
   GNUNET_free (cur->next->buf);
+  GNUNET_free (cur->next->dest_url);
   GNUNET_free (cur->next);
   cur->next = NULL;
   return GNUNET_OK;
 }
 
 
+static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
+{
+  char * tmp;
+  unsigned int len = size * nmemb;
+
+  tmp = GNUNET_malloc (  len+1 );
+  memcpy(tmp,ptr,len);
+  if (tmp[len-2] == 13)
+    tmp[len-2]= '\0';
+#if DEBUG_CURL
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
+#endif
+  /*
+  if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
+  {
+    res->http_result_code=100;
+  }
+  if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
+  {
+    res->http_result_code=200;
+  }
+  if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
+  {
+    res->http_result_code=400;
+  }
+  if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
+  {
+    res->http_result_code=404;
+  }
+  if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
+  {
+    res->http_result_code=413;
+  }
+   */
+  GNUNET_free (tmp);
+  return size * nmemb;
+}
+
+/**
+ * Callback method used with libcurl
+ * Method is called when libcurl needs to read data during sending
+ * @param stream pointer where to write data
+ * @param size size of an individual element
+ * @param nmemb count of elements that can be written to the buffer
+ * @param ptr source pointer, passed to the libcurl handle
+ * @return bytes written to stream
+ */
 static size_t send_read_callback(void *stream, size_t size, size_t nmemb, void *ptr)
 {
   struct Session * ses = ptr;
@@ -708,12 +887,20 @@ static size_t send_read_callback(void *stream, size_t size, size_t nmemb, void *
   return bytes_sent;
 }
 
-
-static size_t send_write_callback( void *ptr, size_t size, size_t nmemb, void *stream)
+/**
+* Callback method used with libcurl
+* Method is called when libcurl needs to write data during sending
+* @param stream pointer where to write data
+* @param size size of an individual element
+* @param nmemb count of elements that can be written to the buffer
+* @param ptr destination pointer, passed to the libcurl handle
+* @return bytes read from stream
+*/
+static size_t send_write_callback( void *stream, size_t size, size_t nmemb, void *ptr)
 {
   char * data = malloc(size*nmemb +1);
 
-  memcpy( data, ptr, size*nmemb);
+  memcpy( data, stream, size*nmemb);
   data[size*nmemb] = '\0';
   /* Just a dummy print for the response recieved for the PUT message */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Recieved %u bytes: `%s' \n", size * nmemb, data);
@@ -722,31 +909,47 @@ static size_t send_write_callback( void *ptr, size_t size, size_t nmemb, void *s
 
 }
 
+/**
+ * Function setting up file descriptors and scheduling task to run
+ * @param session session to send data to
+ * @return bytes sent to peer
+ */
 static size_t send_prepare(struct Session* session );
 
-static ssize_t send_select_init (struct Session* ses  )
+/**
+ * Function setting up curl handle and selecting message to send
+ * @param ses session to send data to
+ * @return bytes sent to peer
+ */
+static ssize_t send_select_init (struct Session* ses )
 {
-  char * url;
   int bytes_sent = 0;
   CURLMcode mret;
   struct HTTP_Message * msg;
 
-  /* FIFO selection, send oldest msg first, perhaps priority here?  */
+  if ( NULL == ses->curl_handle)
+    ses->curl_handle = curl_easy_init();
+  if( NULL == ses->curl_handle)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
+    return -1;
+  }
   msg = ses->pending_outbound_msg;
 
-  url = GNUNET_malloc( 7 + strlen(ses->ip) + 7 + strlen ((char *) &(ses->hash)) + 1);
-  /* FIXME: use correct port number */
-  GNUNET_asprintf(&url,"http://%s:%u/%s",ses->ip,12389, (char *) &(ses->hash));
 
-  /* curl_easy_setopt(ses->curl_handle, CURLOPT_VERBOSE, 1L); */
-  curl_easy_setopt(ses->curl_handle, CURLOPT_URL, url);
+
+#if DEBUG_CURL
+  curl_easy_setopt(ses->curl_handle, CURLOPT_VERBOSE, 1L);
+#endif
+  curl_easy_setopt(ses->curl_handle, CURLOPT_URL, msg->dest_url);
   curl_easy_setopt(ses->curl_handle, CURLOPT_PUT, 1L);
+  curl_easy_setopt(ses->curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
   curl_easy_setopt(ses->curl_handle, CURLOPT_READFUNCTION, send_read_callback);
   curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
   curl_easy_setopt(ses->curl_handle, CURLOPT_WRITEFUNCTION, send_write_callback);
   curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
   curl_easy_setopt(ses->curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
-  curl_easy_setopt(ses->curl_handle, CURLOPT_TIMEOUT, (timeout.value / 1000 ));
+  curl_easy_setopt(ses->curl_handle, CURLOPT_TIMEOUT, (long) (timeout.value / 1000 ));
   curl_easy_setopt(ses->curl_handle, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
 
   mret = curl_multi_add_handle(multi_handle, ses->curl_handle);
@@ -759,7 +962,6 @@ static ssize_t send_select_init (struct Session* ses  )
     return -1;
   }
   bytes_sent = send_prepare (ses );
-  GNUNET_free ( url );
   return bytes_sent;
 }
 
@@ -769,7 +971,7 @@ static void send_execute (void *cls,
   int running;
   struct CURLMsg *msg;
   CURLMcode mret;
-  struct Session * cs = cls;
+  struct Session * cs = NULL;
 
   http_task_send = GNUNET_SCHEDULER_NO_TASK;
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
@@ -779,7 +981,6 @@ static void send_execute (void *cls,
     {
       running = 0;
       mret = curl_multi_perform (multi_handle, &running);
-      //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"send_execute %u\n", running);
       if (running == 0)
         {
           do
@@ -789,11 +990,18 @@ static void send_execute (void *cls,
               GNUNET_break (msg != NULL);
               if (msg == NULL)
                 break;
+              /* get session for affected curl handle */
+              GNUNET_assert ( msg->easy_handle != NULL );
+              cs = find_session_by_curlhandle (msg->easy_handle);
+              GNUNET_assert ( cs != NULL );
               switch (msg->msg)
                 {
+
                 case CURLMSG_DONE:
                   if ( (msg->data.result != CURLE_OK) &&
                        (msg->data.result != CURLE_GOT_NOTHING) )
+                    {
+
                     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
                                _("%s failed for `%s' at %s:%d: `%s'\n"),
                                "curl_multi_perform",
@@ -801,8 +1009,12 @@ static void send_execute (void *cls,
                                __FILE__,
                                __LINE__,
                                curl_easy_strerror (msg->data.result));
+                    /* sending msg failed*/
+                    if ( NULL != cs->pending_outbound_msg->transmit_cont)
+                      cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_SYSERR);
+                    }
                   else
-                    {
+                  {
                     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                                 "Send to %s completed.\n", cs->ip);
                     if (GNUNET_OK != remove_http_message(cs, cs->pending_outbound_msg))
@@ -811,13 +1023,17 @@ static void send_execute (void *cls,
                     curl_easy_cleanup(cs->curl_handle);
                     cs->curl_handle=NULL;
 
+                    /* Calling transmit continuation  */
+                    if ( NULL != cs->pending_outbound_msg->transmit_cont)
+                      cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_OK);
+
+
+                    /* send pending messages */
                     if (cs->pending_outbound_msg != NULL)
+                    {
                       send_select_init (cs);
-
-                    /* Calling transmit continuation  */
-                    if ( NULL != cs->transmit_cont)
-                      cs->transmit_cont (NULL,&cs->sender,GNUNET_OK);
                     }
+                  }
                   return;
                 default:
                   break;
@@ -832,6 +1048,11 @@ static void send_execute (void *cls,
 }
 
 
+/**
+ * Function setting up file descriptors and scheduling task to run
+ * @param ses session to send data to
+ * @return bytes sent to peer
+ */
 static size_t send_prepare(struct Session* session )
 {
   fd_set rs;
@@ -843,7 +1064,6 @@ static size_t send_prepare(struct Session* session )
   long to;
   CURLMcode mret;
 
-//  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"send_prepare\n");
   max = -1;
   FD_ZERO (&rs);
   FD_ZERO (&ws);
@@ -895,7 +1115,7 @@ static size_t send_prepare(struct Session* session )
  * @param priority how important is the message
  * @param msgbuf the message to transmit
  * @param msgbuf_size number of bytes in 'msgbuf'
- * @param timeout when should we time out
+ * @param to when should we time out
  * @param session which session must be used (or NULL for "any")
  * @param addr the address to use (can be NULL if the plugin
  *                is "on its own" (i.e. re-use existing TCP connection))
@@ -926,14 +1146,13 @@ http_plugin_send (void *cls,
                       GNUNET_TRANSPORT_TransmitContinuation cont,
                       void *cont_cls)
 {
+  char * address;
   struct Session* ses;
   struct Session* ses_temp;
   struct HTTP_Message * msg;
   struct HTTP_Message * tmp;
   int bytes_sent = 0;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport told plugin to send to peer `%s'\n",GNUNET_i2s(target));
-
   /* find session for peer */
   ses = find_session_by_pi (target);
   if (NULL != ses )
@@ -943,9 +1162,8 @@ http_plugin_send (void *cls,
     /* create new session object */
 
     /*FIXME: what is const void * really? Assuming struct sockaddr_in * ! */
-    ses = create_session((struct sockaddr_in *) addr, target);
+    ses = create_session(NULL, (struct sockaddr_in *) addr, target);
     ses->is_active = GNUNET_YES;
-    ses->transmit_cont = cont;
 
     /* Insert session into linked list */
     if ( plugin->sessions == NULL)
@@ -966,15 +1184,33 @@ http_plugin_send (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", GNUNET_i2s(target), plugin->session_count);
   }
 
-  ses->curl_handle = curl_easy_init();
-  if( NULL == ses->curl_handle)
+  GNUNET_assert (addr!=NULL);
+  unsigned int port;
+
+  /* setting url to send to */
+  if (force_address == GNUNET_YES)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
-    return -1;
+    if (addrlen == (sizeof (struct IPv4HttpAddress)))
+    {
+      address = GNUNET_malloc(INET_ADDRSTRLEN + 14 + strlen ((const char *) (&ses->hash)));
+      inet_ntop(AF_INET,&((struct IPv4HttpAddress *) addr)->ipv4_addr,address,INET_ADDRSTRLEN);
+      port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
+      GNUNET_asprintf(&address,"http://%s:%u/%s",address,port, (char *) (&ses->hash));
+    }
+    else if (addrlen == (sizeof (struct IPv6HttpAddress)))
+    {
+      address = GNUNET_malloc(INET6_ADDRSTRLEN + 14 + strlen ((const char *) (&ses->hash)));
+      inet_ntop(AF_INET6, &((struct IPv6HttpAddress *) addr)->ipv6_addr,address,INET6_ADDRSTRLEN);
+      port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
+      GNUNET_asprintf(&address,"http://%s:%u/%s",address,port,(char *) (&ses->hash));
+    }
+    else
+      {
+        GNUNET_break (0);
+        return -1;
+    }
   }
 
-  if ( NULL != cont)
-    ses->transmit_cont = cont;
   timeout = to;
   /* setting up message */
   msg = GNUNET_malloc (sizeof (struct HTTP_Message));
@@ -982,10 +1218,12 @@ http_plugin_send (void *cls,
   msg->len = msgbuf_size;
   msg->pos = 0;
   msg->buf = GNUNET_malloc (msgbuf_size);
+  msg->dest_url = address;
+  msg->transmit_cont = cont;
+  msg->transmit_cont_cls = cont_cls;
   memcpy (msg->buf,msgbuf, msgbuf_size);
 
   /* insert created message in list of pending messages */
-
   if (ses->pending_outbound_msg == NULL)
   {
     ses->pending_outbound_msg = msg;
@@ -996,11 +1234,16 @@ http_plugin_send (void *cls,
     tmp = tmp->next;
   }
   if ( tmp != msg)
+  {
     tmp->next = msg;
+  }
 
-  bytes_sent = send_select_init (ses);
-  return bytes_sent;
-
+  if (msg == ses->pending_outbound_msg)
+  {
+    bytes_sent = send_select_init (ses);
+    return bytes_sent;
+  }
+  return msgbuf_size;
 }
 
 
@@ -1047,8 +1290,42 @@ http_plugin_address_pretty_printer (void *cls,
                                         GNUNET_TRANSPORT_AddressStringCallback
                                         asc, void *asc_cls)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_address_pretty_printer\n");
-  asc (asc_cls, NULL);
+  const struct IPv4HttpAddress *t4;
+  const struct IPv6HttpAddress *t6;
+  struct sockaddr_in a4;
+  struct sockaddr_in6 a6;
+  char * address;
+  char * ret;
+  unsigned int port;
+
+  if (addrlen == sizeof (struct IPv6HttpAddress))
+    {
+      address = GNUNET_malloc (INET6_ADDRSTRLEN);
+      t6 = addr;
+      a6.sin6_addr = t6->ipv6_addr;
+      inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
+      port = ntohs(t6->u6_port);
+    }
+  else if (addrlen == sizeof (struct IPv4HttpAddress))
+    {
+      address = GNUNET_malloc (INET_ADDRSTRLEN);
+      t4 = addr;
+      a4.sin_addr.s_addr =  t4->ipv4_addr;
+      inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
+      port = ntohs(t4->u_port);
+    }
+  else
+    {
+      /* invalid address */
+      GNUNET_break_op (0);
+      asc (asc_cls, NULL);
+      return;
+    }
+
+  ret = GNUNET_malloc(strlen(address) +14);
+  GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
+  GNUNET_free (address);
+  asc (asc_cls, ret);
 }
 
 
@@ -1069,11 +1346,43 @@ static int
 http_plugin_address_suggested (void *cls,
                                   void *addr, size_t addrlen)
 {
-  /* struct Plugin *plugin = cls; */
+  struct IPv4HttpAddress *v4;
+  struct IPv6HttpAddress *v6;
+  unsigned int port;
 
-  /* check if the address is plausible; if so,
-     add it to our list! */
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_address_suggested\n");
+  if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
+      (addrlen != sizeof (struct IPv6HttpAddress)))
+    {
+      GNUNET_break_op (0);
+      return GNUNET_SYSERR;
+    }
+  if (addrlen == sizeof (struct IPv4HttpAddress))
+    {
+      v4 = (struct IPv4HttpAddress *) addr;
+
+      port = ntohs (v4->u_port);
+      if (port != plugin->port_inbound)
+      {
+        GNUNET_break_op (0);
+        return GNUNET_SYSERR;
+      }
+    }
+  else
+    {
+      v6 = (struct IPv6HttpAddress *) addr;
+      if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
+        {
+          GNUNET_break_op (0);
+          return GNUNET_SYSERR;
+        }
+      port = ntohs (v6->u6_port);
+      if (port != plugin->port_inbound)
+      {
+        GNUNET_break_op (0);
+        return GNUNET_SYSERR;
+      }
+
+    }
   return GNUNET_OK;
 }
 
@@ -1094,9 +1403,107 @@ http_plugin_address_to_string (void *cls,
                                    const void *addr,
                                    size_t addrlen)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_address_to_string\n");
-  GNUNET_break (0);
-  return NULL;
+  const struct IPv4HttpAddress *t4;
+  const struct IPv6HttpAddress *t6;
+  struct sockaddr_in a4;
+  struct sockaddr_in6 a6;
+  char * address;
+  char * ret;
+  unsigned int port;
+
+  if (addrlen == sizeof (struct IPv6HttpAddress))
+    {
+      address = GNUNET_malloc (INET6_ADDRSTRLEN);
+      t6 = addr;
+      a6.sin6_addr = t6->ipv6_addr;
+      inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
+      port = ntohs(t6->u6_port);
+    }
+  else if (addrlen == sizeof (struct IPv4HttpAddress))
+    {
+      address = GNUNET_malloc (INET_ADDRSTRLEN);
+      t4 = addr;
+      a4.sin_addr.s_addr =  t4->ipv4_addr;
+      inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
+      port = ntohs(t4->u_port);
+    }
+  else
+    {
+      /* invalid address */
+      return NULL;
+    }
+
+  ret = GNUNET_malloc(strlen(address) +6);
+  GNUNET_asprintf(&ret,"%s:%u",address,port);
+  GNUNET_free (address);
+  return ret;
+}
+
+/**
+ * Add the IP of our network interface to the list of
+ * our external IP addresses.
+ *
+ * @param cls the 'struct Plugin*'
+ * @param name name of the interface
+ * @param isDefault do we think this may be our default interface
+ * @param addr address of the interface
+ * @param addrlen number of bytes in addr
+ * @return GNUNET_OK to continue iterating
+ */
+static int
+process_interfaces (void *cls,
+                    const char *name,
+                    int isDefault,
+                    const struct sockaddr *addr, socklen_t addrlen)
+{
+  struct IPv4HttpAddress t4;
+  struct IPv6HttpAddress t6;
+  int af;
+  void *arg;
+  uint16_t args;
+
+
+
+  af = addr->sa_family;
+  if (af == AF_INET)
+    {
+      if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
+      {
+        /* skip loopback addresses */
+        return GNUNET_OK;
+      }
+      t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
+      t4.u_port = htons (plugin->port_inbound);
+      arg = &t4;
+      args = sizeof (t4);
+    }
+  else if (af == AF_INET6)
+    {
+      if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
+        {
+          /* skip link local addresses */
+          return GNUNET_OK;
+        }
+      if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
+        {
+          /* skip loopback addresses */
+          return GNUNET_OK;
+        }
+      memcpy (&t6.ipv6_addr,
+              &((struct sockaddr_in6 *) addr)->sin6_addr,
+              sizeof (struct in6_addr));
+      t6.u6_port = htons (plugin->port_inbound);
+      arg = &t6;
+      args = sizeof (t6);
+    }
+  else
+    {
+      GNUNET_break (0);
+      return GNUNET_OK;
+    }
+  plugin->env->notify_address(plugin->env->cls,"http",arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
+
+  return GNUNET_OK;
 }
 
 /**
@@ -1167,9 +1574,11 @@ libgnunet_plugin_transport_http_done (void *cls)
          GNUNET_free (cur);
          cur = tmp;
       }
-
+      GNUNET_free (cs->pending_inbound_msg->buf);
+      GNUNET_free (cs->pending_inbound_msg);
       GNUNET_free (cs->ip);
-      GNUNET_free (cs->addr);
+      GNUNET_free (cs->addr_inbound);
+      GNUNET_free_non_null (cs->addr_outbound);
       GNUNET_free (cs);
       plugin->session_count--;
       cs = cs_next;
@@ -1244,7 +1653,7 @@ libgnunet_plugin_transport_http_init (void *cls)
       libgnunet_plugin_transport_http_done (api);
       return NULL;
     }
-
+  plugin->port_inbound = port;
   gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
   timeout = ( gn_timeout.value / 1000);
   if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
@@ -1282,6 +1691,7 @@ libgnunet_plugin_transport_http_init (void *cls)
   if (http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
 
+
   /* Initializing cURL */
   multi_handle = curl_multi_init();
   if ( NULL == multi_handle )
@@ -1293,6 +1703,9 @@ libgnunet_plugin_transport_http_init (void *cls)
     libgnunet_plugin_transport_http_done (api);
     return NULL;
   }
+
+  GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
+
   return api;
 }