fixing unchecked return values
[oweals/gnunet.git] / src / transport / plugin_transport_tcp.c
index 657043c24aa8ce6ff33a36b9325f899074e835ec..da38e7cc7b0ac13f3b0d89999cba231ccc99d6f1 100644 (file)
@@ -483,7 +483,7 @@ tcp_nat_port_map_callback (void *cls, int add_remove,
     t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
     t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
     arg = &t4;
-    args = sizeof(t4);
+    args = sizeof (t4);
     break;
   case AF_INET6:
     GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
@@ -493,13 +493,15 @@ tcp_nat_port_map_callback (void *cls, int add_remove,
     t6.options = htonl (myoptions);
     t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
     arg = &t6;
-    args = sizeof(t6);
+    args = sizeof (t6);
     break;
   default:
     GNUNET_break(0);
     return;
   }
   /* modify our published address list */
+  GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
+      (args == sizeof (struct IPv6TcpAddress)));
   address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
       PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
   plugin->env->notify_address (plugin->env->cls, add_remove, address);
@@ -549,12 +551,6 @@ tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
     memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
     sb = &a4;
     break;
-  case 0:
-  {
-    GNUNET_snprintf (rbuf, sizeof(rbuf), "%s",
-        TRANSPORT_SESSION_INBOUND_STRING);
-    return rbuf;
-  }
   default:
     LOG(GNUNET_ERROR_TYPE_WARNING, _("Unexpected address length: %u bytes\n"),
         (unsigned int ) addrlen);
@@ -779,8 +775,10 @@ tcp_disconnect_session (void *cls, struct Session *session)
     GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
     session->transmit_handle = NULL;
   }
+  plugin->env->unregister_quota_notification (plugin->env->cls,
+      &session->target, PLUGIN_NAME, session);
   session->plugin->env->session_end (session->plugin->env->cls,
-      &session->target, session);
+      session->address, session);
 
   if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
   {
@@ -929,6 +927,8 @@ create_session (struct Plugin *plugin,
     GNUNET_STATISTICS_update (plugin->env->stats,
         gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
   }
+  plugin->env->register_quota_notification (plugin->env->cls,
+      &address->peer, PLUGIN_NAME, session);
   session->timeout_task = GNUNET_SCHEDULER_add_delayed (
       GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
   return session;
@@ -1322,6 +1322,41 @@ tcp_plugin_update_session_timeout (void *cls,
   reschedule_session_timeout (session);
 }
 
+/**
+ * Task to signal the server that we can continue
+ * receiving from the TCP client now.
+ *
+ * @param cls the `struct Session*`
+ * @param tc task context (unused)
+ */
+static void
+delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct Session *session = cls;
+
+  session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
+  reschedule_session_timeout (session);
+
+  GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
+}
+
+static void tcp_plugin_update_inbound_delay (void *cls,
+                                      const struct GNUNET_PeerIdentity *peer,
+                                      struct Session *session,
+                                      struct GNUNET_TIME_Relative delay)
+{
+  if (GNUNET_SCHEDULER_NO_TASK == session->receive_delay_task)
+    return;
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
+      "New inbound delay %llu us\n",delay.rel_value_us);
+
+  GNUNET_SCHEDULER_cancel (session->receive_delay_task);
+  session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
+      &delayed_done, session);
+}
+
+
 /**
  * Create a new session to transmit data to the target
  * This session will used to send data to this peer and the plugin will
@@ -1735,16 +1770,11 @@ tcp_plugin_address_pretty_printer (void *cls, const char *type,
     sb = &a4;
     sbs = sizeof(a4);
   }
-  else if (0 == addrlen)
-  {
-    asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
-    asc (asc_cls, NULL );
-    return;
-  }
   else
   {
     /* invalid address */
-    GNUNET_break_op(0);
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+        "Trying to print invalid `%s' address with size %u\n", type, addrlen);
     asc (asc_cls, NULL );
     return;
   }
@@ -1940,22 +1970,24 @@ handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
   case AF_INET:
     s4 = vaddr;
     t4 = GNUNET_new (struct IPv4TcpAddress);
-    t4->options = 0;
+    t4->options = htonl(0);
     t4->t4_port = s4->sin_port;
     t4->ipv4_addr = s4->sin_addr.s_addr;
     session->address = GNUNET_HELLO_address_allocate (
         &tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t4,
-        sizeof(struct IPv4TcpAddress), GNUNET_HELLO_ADDRESS_INFO_NONE);
+        sizeof(struct IPv4TcpAddress),
+        GNUNET_HELLO_ADDRESS_INFO_NONE);
     break;
   case AF_INET6:
     s6 = vaddr;
     t6 = GNUNET_new (struct IPv6TcpAddress);
-    t6->options = 0;
+    t6->options = htonl(0);
     t6->t6_port = s6->sin6_port;
     memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
     session->address = GNUNET_HELLO_address_allocate (
-        &tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t6,
-        sizeof(struct IPv6TcpAddress), GNUNET_HELLO_ADDRESS_INFO_NONE);
+        &tcp_nat_probe->clientIdentity,
+        PLUGIN_NAME, &t6, sizeof(struct IPv6TcpAddress),
+        GNUNET_HELLO_ADDRESS_INFO_NONE);
     break;
   default:
     GNUNET_break_op(0);
@@ -1999,6 +2031,7 @@ handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
   const struct sockaddr_in6 *s6;
   struct GNUNET_ATS_Information ats;
 
+
   if (0 == memcmp (&wm->clientIdentity, plugin->env->my_identity,
           sizeof(struct GNUNET_PeerIdentity)))
   {
@@ -2015,6 +2048,7 @@ handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_break_op(0);
     return;
   }
+
   LOG(GNUNET_ERROR_TYPE_DEBUG, "Received %s message from `%4s' %p\n", "WELCOME",
       GNUNET_i2s (&wm->clientIdentity), client);
   GNUNET_STATISTICS_update (plugin->env->stats,
@@ -2061,25 +2095,37 @@ handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
             PLUGIN_NAME, &t6, sizeof (t6),
             GNUNET_HELLO_ADDRESS_INFO_INBOUND);
       }
+      else
+      {
+        GNUNET_break (0);
+        GNUNET_free_non_null (vaddr);
+        return;
+      }
       session = create_session (plugin, address, client, GNUNET_NO);
       GNUNET_HELLO_address_free (address);
       ats = plugin->env->get_address_type (plugin->env->cls, vaddr, alen);
       session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
           ats.value);
-      LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new%s session %p for peer `%s'\n",
-          GNUNET_HELLO_address_check_option (session->address, GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? " inbound" : "",
-          session,
-          tcp_address_to_string(NULL, (void *) session->address->address,session->address->address_length));
+      LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new%s session %p for peer `%s' client %p \n",
+          GNUNET_HELLO_address_check_option (session->address,
+              GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? " inbound" : "", session,
+          tcp_address_to_string(NULL, (void *) session->address->address,
+              session->address->address_length),
+          client);
       GNUNET_free(vaddr);
       GNUNET_SERVER_client_set_user_context(session->client, session);
       GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
           session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+
+      /* Notify transport and ATS about new session */
+      plugin->env->session_start (NULL, session->address, session, &ats, 1);
     }
     else
     {
       LOG(GNUNET_ERROR_TYPE_DEBUG,
           "Did not obtain TCP socket address for incoming connection\n");
       GNUNET_break(0);
+      return;
     }
   }
 
@@ -2093,32 +2139,12 @@ handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
   session->last_activity = GNUNET_TIME_absolute_get ();
   session->expecting_welcome = GNUNET_NO;
 
-  /* Notify transport and ATS about new session */
-  plugin->env->session_start (NULL, session->address, session, &ats, 1);
-
   process_pending_messages (session);
   GNUNET_SERVER_client_set_timeout (client,
       GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
-/**
- * Task to signal the server that we can continue
- * receiving from the TCP client now.
- *
- * @param cls the `struct Session*`
- * @param tc task context (unused)
- */
-static void
-delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct Session *session = cls;
-
-  session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
-  reschedule_session_timeout (session);
-
-  GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
-}
 
 /**
  * We've received data for this peer via TCP.  Unbox,
@@ -2344,7 +2370,11 @@ try_connection_reversal (void *cls, const struct sockaddr *addr,
 static enum GNUNET_ATS_Network_Type
 tcp_get_network (void *cls, struct Session *session)
 {
-  GNUNET_assert(NULL != session);
+  struct Plugin * plugin = cls;
+  GNUNET_assert (NULL != plugin);
+  GNUNET_assert (NULL != session);
+  if (GNUNET_SYSERR == find_session (plugin,session))
+    return GNUNET_ATS_NET_UNSPECIFIED;
   return session->ats_address_network_type;
 }
 
@@ -2479,6 +2509,7 @@ else
   api->string_to_address = &tcp_string_to_address;
   api->get_network = &tcp_get_network;
   api->update_session_timeout = &tcp_plugin_update_session_timeout;
+  api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
   plugin->service = service;
   if (NULL != service)
   {