(no commit message)
[oweals/gnunet.git] / src / transport / gnunet-service-transport.c
index d1ef887fd11d61128473ef6fb4dcef80f82fb5cc..4ce6b4321f5aa866be83d3d509a02092edf82753 100644 (file)
@@ -73,7 +73,7 @@ struct GNUNET_CRYPTO_RsaPrivateKey *GST_my_private_key;
 /**
  * ATS handle.
  */
-struct GNUNET_ATS_Handle *GST_ats;
+struct GNUNET_ATS_SchedulingHandle *GST_ats;
 
 
 /**
@@ -83,16 +83,22 @@ struct GNUNET_ATS_Handle *GST_ats;
  * @param target a connected neighbour
  * @param ats performance information (unused)
  * @param ats_count number of records in ats (unused)
+ * @param transport plugin
+ * @param addr address
+ * @param addrlen address length
  */
 static void
 transmit_our_hello (void *cls, const struct GNUNET_PeerIdentity *target,
                     const struct GNUNET_TRANSPORT_ATS_Information *ats,
-                    uint32_t ats_count)
+                    uint32_t ats_count,
+                    const char * transport,
+                    const void * addr,
+                    size_t addrlen)
 {
   const struct GNUNET_MessageHeader *hello = cls;
 
   GST_neighbours_send (target, (const char *) hello, ntohs (hello->size),
-                       GST_HELLO_ADDRESS_EXPIRATION, NULL, NULL);
+                       GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION, NULL, NULL);
 }
 
 
@@ -173,7 +179,7 @@ plugin_env_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
     {
     case GNUNET_MESSAGE_TYPE_HELLO:
       GST_validation_handle_hello (message);
-      break;
+      return ret;
     case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
 #if DEBUG_TRANSPORT
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
@@ -205,8 +211,8 @@ plugin_env_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
        * * way to communicate with this peer (other peer switched transport) */
       break;
     case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
-      /* TODO: do some validation to prevent an attacker from sending
-       * a fake disconnect message... */
+      /* FIXME: do some validation to prevent an attacker from sending
+       * a fake disconnect message... */         
       GST_neighbours_force_disconnect (peer);
       break;
     case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
@@ -232,7 +238,6 @@ plugin_env_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
         im->ats_count = htonl (0);
         memcpy (&(im->peer), peer, sizeof (struct GNUNET_PeerIdentity));
         memcpy (&im[1], message, ntohs (message->size));
-
         GST_clients_broadcast ((const struct GNUNET_MessageHeader *) im,
                                GNUNET_YES);
 
@@ -241,13 +246,18 @@ plugin_env_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
       break;
     }
   }
-
-  GNUNET_assert ((ats_count > 0) && (ats != NULL));
-
-  GNUNET_ATS_address_update (GST_ats, peer, GNUNET_TIME_absolute_get (),        /* valid at least until right now... */
-                             plugin_name, session, sender_address,
-                             sender_address_len, ats, ats_count);
-
+  /*
+     FIXME: this gives an address that might not have been validated to
+     ATS for 'selection', which is probably not what we want; this 
+     might be particularly wrong (as in, possibly hiding bugs with address
+     validation) as 'GNUNET_ATS_address_update' currently ignores
+     the expiration given.
+  */
+  if ((ats_count > 0) && (ats != NULL))
+    GNUNET_ATS_address_update (GST_ats, peer,
+                               plugin_name, sender_address, sender_address_len,
+                               session,
+                               ats, ats_count);
   return ret;
 }
 
@@ -290,6 +300,12 @@ static void
 plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
                         struct Session *session)
 {
+#if DEBUG_TRANSPORT
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Session %X to peer `%s' ended \n",
+              session, GNUNET_i2s (peer));
+#endif
+  GNUNET_ATS_address_destroyed(GST_ats, peer, NULL, NULL, 0, session);
   GST_neighbours_session_terminated (peer, session);
 }
 
@@ -307,17 +323,37 @@ plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param session session to use (if available)
  * @param plugin_addr address to use (if available)
  * @param plugin_addr_len number of bytes in addr
- * @param bandwidth assigned outbound bandwidth for the connection
+ * @param bandwidth_out assigned outbound bandwidth for the connection
+ * @param bandwidth_in assigned inbound bandwidth for the connection
  */
 static void
 ats_request_address_change (void *cls, const struct GNUNET_PeerIdentity *peer,
-                            const char *plugin_name, struct Session *session,
+                            const char *plugin_name,
                             const void *plugin_addr, size_t plugin_addr_len,
-                            struct GNUNET_BANDWIDTH_Value32NBO bandwidth)
+                            struct Session *session,
+                            struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
+                            struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
 {
   GST_neighbours_switch_to_address (peer, plugin_name, plugin_addr,
                                     plugin_addr_len, session, NULL, 0);
-  GST_neighbours_set_incoming_quota (peer, bandwidth);
+  GST_neighbours_set_incoming_quota (peer, bandwidth_in);
+
+#if DEBUG_TRANSPORT
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
+              ntohl (bandwidth_out.value__), GNUNET_i2s (peer));
+#endif
+  struct QuotaSetMessage msg;
+  msg.header.size = htons (sizeof (struct QuotaSetMessage));
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
+  msg.quota = bandwidth_out;
+  msg.peer = (*peer);
+  GST_clients_broadcast ((struct GNUNET_MessageHeader *) &msg, GNUNET_NO);
+
+#if DEBUG_TRANSPORT
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting inbound quota of %u for peer `%s' to \n",
+              ntohl (bandwidth_in.value__), GNUNET_i2s (peer));
+#endif
+  GST_neighbours_set_incoming_quota (peer, bandwidth_in);
 }
 
 
@@ -328,7 +364,7 @@ ats_request_address_change (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param cls closure
  * @param peer the peer that connected
  * @param ats performance data
- * @param ats_count number of entries in ats (excluding 0-termination)
+ * @param ats_count number of entries in ats
  */
 static void
 neighbours_connect_notification (void *cls,
@@ -339,16 +375,13 @@ neighbours_connect_notification (void *cls,
   char buf[sizeof (struct ConnectInfoMessage) +
            ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)];
   struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
-  struct GNUNET_TRANSPORT_ATS_Information *atsm = &connect_msg->ats;
 
   connect_msg->header.size = htons (sizeof (buf));
   connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
   connect_msg->ats_count = htonl (ats_count);
   connect_msg->id = *peer;
-  memcpy (&connect_msg->ats, ats,
+  memcpy (&connect_msg->ats, &connect_msg->ats,
           ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
-  atsm[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
-  atsm[ats_count].value = htonl (0);
   GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
 }
 
@@ -387,7 +420,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GST_validation_stop ();
   GST_plugins_unload ();
   GST_neighbours_stop ();
-  GNUNET_ATS_shutdown (GST_ats);
+  GNUNET_ATS_scheduling_done (GST_ats);
   GST_ats = NULL;
   GST_clients_stop ();
   GST_blacklist_stop ();
@@ -466,7 +499,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   GST_plugins_load (&plugin_env_receive_callback,
                     &plugin_env_address_change_notification,
                     &plugin_env_session_end);
-  GST_ats = GNUNET_ATS_init (GST_cfg, &ats_request_address_change, NULL);
+  GST_ats = GNUNET_ATS_scheduling_init (GST_cfg, &ats_request_address_change, NULL);
   GST_neighbours_start (NULL, &neighbours_connect_notification,
                         &neighbours_disconnect_notification);
   GST_clients_start (server);