(no commit message)
[oweals/gnunet.git] / src / transport / plugin_transport_http.c
index fc49cf287ca956f8629f3b3d5c896cdfe8a8893f..7984529343dd4381b8a54a5b311ed3c0b18eb5f6 100644 (file)
@@ -140,6 +140,18 @@ struct HTTP_Message
   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;
 };
 
 
@@ -159,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)
@@ -395,14 +395,12 @@ static struct Session * create_session (struct sockaddr_in *addr_in, struct sock
     {
       ses->ip = GNUNET_malloc (INET_ADDRSTRLEN);
       addrin = addr_in;
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Port %u \n", addrin->sin_port);
       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;
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Port %u \n", addrin6->sin6_port);
       inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr) ,ses->ip,INET6_ADDRSTRLEN);
     }
   }
@@ -799,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;
@@ -814,6 +813,7 @@ 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;
@@ -923,7 +923,6 @@ static size_t send_prepare(struct Session* session );
  */
 static ssize_t send_select_init (struct Session* ses )
 {
-  char * url;
   int bytes_sent = 0;
   CURLMcode mret;
   struct HTTP_Message * msg;
@@ -937,15 +936,12 @@ static ssize_t send_select_init (struct Session* ses )
   }
   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));
 
 
 #if DEBUG_CURL
   curl_easy_setopt(ses->curl_handle, CURLOPT_VERBOSE, 1L);
 #endif
-  curl_easy_setopt(ses->curl_handle, CURLOPT_URL, url);
+  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);
@@ -966,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;
 }
 
@@ -1015,8 +1010,8 @@ static void send_execute (void *cls,
                                __LINE__,
                                curl_easy_strerror (msg->data.result));
                     /* sending msg failed*/
-                    if ( NULL != cs->transmit_cont)
-                      cs->transmit_cont (cs->transmit_cont_cls,&cs->sender,GNUNET_SYSERR);
+                    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
                   {
@@ -1029,8 +1024,8 @@ static void send_execute (void *cls,
                     cs->curl_handle=NULL;
 
                     /* Calling transmit continuation  */
-                    if ( NULL != cs->transmit_cont)
-                      cs->transmit_cont (NULL,&cs->sender,GNUNET_OK);
+                    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 */
@@ -1151,7 +1146,7 @@ http_plugin_send (void *cls,
                       GNUNET_TRANSPORT_TransmitContinuation cont,
                       void *cont_cls)
 {
-  char address[INET6_ADDRSTRLEN];
+  char * address;
   struct Session* ses;
   struct Session* ses_temp;
   struct HTTP_Message * msg;
@@ -1169,7 +1164,6 @@ http_plugin_send (void *cls,
     /*FIXME: what is const void * really? Assuming struct sockaddr_in * ! */
     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)
@@ -1192,26 +1186,31 @@ http_plugin_send (void *cls,
 
   GNUNET_assert (addr!=NULL);
   unsigned int port;
-  if (addrlen == (sizeof (struct IPv4HttpAddress)))
+
+  /* setting url to send to */
+  if (force_address == GNUNET_YES)
+  {
+    if (addrlen == (sizeof (struct IPv4HttpAddress)))
     {
-      inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
-      port = ((struct IPv4HttpAddress *) addr)->u_port;
-      //port = ntohs(((struct in_addr *) addr)->);
+      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)))
+    else if (addrlen == (sizeof (struct IPv6HttpAddress)))
     {
-      inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
+      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;
+    else
+      {
+        GNUNET_break (0);
+        return -1;
     }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"ADDR `%s':%u \n",address,port);
+  }
 
-  ses->transmit_cont = cont;
-  ses->transmit_cont_cls = cont_cls;
   timeout = to;
   /* setting up message */
   msg = GNUNET_malloc (sizeof (struct HTTP_Message));
@@ -1219,6 +1218,9 @@ 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 */
@@ -1346,6 +1348,7 @@ http_plugin_address_suggested (void *cls,
 {
   struct IPv4HttpAddress *v4;
   struct IPv6HttpAddress *v6;
+  unsigned int port;
 
   if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
       (addrlen != sizeof (struct IPv6HttpAddress)))
@@ -1357,8 +1360,8 @@ http_plugin_address_suggested (void *cls,
     {
       v4 = (struct IPv4HttpAddress *) addr;
 
-      v4->u_port = ntohs (v4->u_port);
-      if (v4->u_port != plugin->port_inbound)
+      port = ntohs (v4->u_port);
+      if (port != plugin->port_inbound)
       {
         GNUNET_break_op (0);
         return GNUNET_SYSERR;
@@ -1372,8 +1375,8 @@ http_plugin_address_suggested (void *cls,
           GNUNET_break_op (0);
           return GNUNET_SYSERR;
         }
-      v6->u6_port = ntohs (v6->u6_port);
-      if (v6->u6_port != plugin->port_inbound)
+      port = ntohs (v6->u6_port);
+      if (port != plugin->port_inbound)
       {
         GNUNET_break_op (0);
         return GNUNET_SYSERR;