more code
[oweals/gnunet.git] / src / transport / gnunet-service-transport.c
index bf43008bbbf1ad1865a6d1f20c2b518da88af465..677d0033c96a953d72f4862654367312a4cbd6a2 100644 (file)
@@ -90,12 +90,16 @@ static int connections;
  * @param ats performance information (unused)
  * @param ats_count number of records in ats (unused)
  * @param address the address
+ * @param bandwidth_in inbound quota in NBO
+ * @param bandwidth_out outbound quota in NBO
  */
 static void
 transmit_our_hello (void *cls, const struct GNUNET_PeerIdentity *target,
                     const struct GNUNET_ATS_Information *ats,
                     uint32_t ats_count,
-                    const struct GNUNET_HELLO_Address *address)
+                    const struct GNUNET_HELLO_Address *address,
+                    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
+                    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
 {
   const struct GNUNET_MessageHeader *hello = cls;
 
@@ -179,6 +183,7 @@ process_payload (const struct GNUNET_PeerIdentity *peer,
       htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
   memcpy (&ap[ats_count + 1], message, ntohs (message->size));
 
+  GNUNET_ATS_address_add (GST_ats, address, session, ap, ats_count + 1);
   GNUNET_ATS_address_update (GST_ats, address, session, ap, ats_count + 1);
   GST_clients_broadcast (&im->header, GNUNET_YES);
 
@@ -230,10 +235,12 @@ plugin_env_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
   if (NULL == message)
     goto end;
   type = ntohs (message->type);
-#if DEBUG_TRANSPORT
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Message with type %u from peer `%s'\n", type, GNUNET_i2s (peer));
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Message with type %u\n", type);
-#endif
+  GNUNET_STATISTICS_update (GST_stats,
+                        gettext_noop
+                        ("# bytes total received"),
+                            ntohs (message->size), GNUNET_NO);
 
   switch (type)
   {
@@ -241,21 +248,17 @@ plugin_env_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
     GST_validation_handle_hello (message);
     return ret;
   case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
-#if DEBUG_TRANSPORT
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
                 "Processing `%s' from `%s'\n", "PING",
                 (sender_address !=
                  NULL) ? GST_plugins_a2s (&address) : "<inbound>");
-#endif
     GST_validation_handle_ping (peer, message, &address, session);
     break;
   case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
-#if DEBUG_TRANSPORT
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
                 "Processing `%s' from `%s'\n", "PONG",
                 (sender_address !=
                  NULL) ? GST_plugins_a2s (&address) : "<inbound>");
-#endif
     GST_validation_handle_pong (peer, message);
     break;
   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT:
@@ -267,8 +270,8 @@ plugin_env_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
                                        ats_count);
     break;
   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
-    GST_neighbours_handle_ack (message, peer, &address, session, ats,
-                               ats_count);
+    GST_neighbours_handle_session_ack (message, peer, &address, session, ats,
+                                       ats_count);
     break;
   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
     GST_neighbours_handle_disconnect_message (peer, message);
@@ -281,20 +284,17 @@ plugin_env_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
     break;
   default:
     /* should be payload */
+    GNUNET_STATISTICS_update (GST_stats,
+                              gettext_noop
+                              ("# bytes payload received"),
+                              ntohs (message->size), GNUNET_NO);
     ret = process_payload (peer, &address, session, message, ats, ats_count);
     break;
   }
 end:
-#if 1
-  /* FIXME: this should not be needed, and not sure it's good to have it, but without
-   * this connections seem to go extra-slow */
-  GNUNET_ATS_address_update (GST_ats, &address, session, ats, ats_count);
-#endif
-#if DEBUG_TRANSPORT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Allowing receive from peer %s to continue in %llu ms\n",
               GNUNET_i2s (peer), (unsigned long long) ret.rel_value);
-#endif
   return ret;
 }
 
@@ -309,16 +309,17 @@ end:
  * @param addr one of the addresses of the host
  *        the specific address format depends on the transport
  * @param addrlen length of the address
+ * @param dest_plugin destination plugin to use this address with
  */
 static void
 plugin_env_address_change_notification (void *cls, int add_remove,
-                                        const void *addr, size_t addrlen)
+                                        const void *addr, size_t addrlen,
+                                        const char *dest_plugin)
 {
-  const char *plugin_name = cls;
   struct GNUNET_HELLO_Address address;
 
   address.peer = GST_my_identity;
-  address.transport_name = plugin_name;
+  address.transport_name = dest_plugin;
   address.address = addr;
   address.address_length = addrlen;
   GST_hello_modify_addresses (add_remove, &address);
@@ -346,10 +347,8 @@ plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
   struct GNUNET_HELLO_Address address;
 
   GNUNET_assert (strlen (transport_name) > 0);
-#if DEBUG_TRANSPORT
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %p to peer `%s' ended \n",
               session, GNUNET_i2s (peer));
-#endif
   if (NULL != session)
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
                      "transport-ats",
@@ -360,6 +359,8 @@ plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
   address.address_length = 0;
   address.transport_name = transport_name;
   GST_neighbours_session_terminated (peer, session);
+
+  /* Tell ATS that session has ended */
   GNUNET_ATS_address_destroyed (GST_ats, &address, session);
 }
 
@@ -373,7 +374,7 @@ plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param addrlen length of the address
  * @return ATS Information containing the network type
  */
-static const struct GNUNET_ATS_Information
+static struct GNUNET_ATS_Information
 plugin_env_address_to_type (void *cls,
                             const struct sockaddr *addr,
                             size_t addrlen)
@@ -430,15 +431,12 @@ ats_request_address_change (void *cls,
   /* ATS tells me to disconnect from peer */
   if ((bw_in == 0) && (bw_out == 0))
   {
-#if DEBUG_TRANSPORT
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "ATS tells me to disconnect from peer `%s'\n",
                 GNUNET_i2s (&address->peer));
-#endif
     GST_neighbours_force_disconnect (&address->peer);
     return;
   }
-  /* will never return GNUNET_YES since connection is to be established */
   GST_neighbours_switch_to_address (&address->peer, address, session, ats,
                                          ats_count, bandwidth_in,
                                          bandwidth_out);
@@ -453,12 +451,16 @@ ats_request_address_change (void *cls,
  * @param peer the peer that connected
  * @param ats performance data
  * @param ats_count number of entries in ats
+ * @param bandwidth_in inbound bandwidth in NBO
+ * @param bandwidth_out outbound bandwidth in NBO
  */
 static void
 neighbours_connect_notification (void *cls,
                                  const struct GNUNET_PeerIdentity *peer,
                                  const struct GNUNET_ATS_Information *ats,
-                                 uint32_t ats_count)
+                                 uint32_t ats_count,
+                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
+                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
 {
   size_t len =
       sizeof (struct ConnectInfoMessage) +
@@ -476,6 +478,8 @@ neighbours_connect_notification (void *cls,
   connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
   connect_msg->ats_count = htonl (ats_count);
   connect_msg->id = *peer;
+  connect_msg->quota_in = bandwidth_in;
+  connect_msg->quota_out = bandwidth_out;
   ap = (struct GNUNET_ATS_Information *) &connect_msg[1];
   memcpy (ap, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
   GST_clients_broadcast (&connect_msg->header, GNUNET_NO);