fixing resource leaks
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.c
index 2cdc12766183bde80ffa03aa07193bcb93c57646..406acf6147430f1c9a4c1dfe48949e2ac108eb47 100644 (file)
@@ -472,12 +472,17 @@ free_address (struct ATS_Address *addr)
  * @param plugin_name plugin
  * @param plugin_addr address
  * @param plugin_addr_len address length
+ * @param local_address_info additional local info for the address
  * @param session_id session
  * @return the ATS_Address
  */
 static struct ATS_Address *
-create_address (const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
-    const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
+create_address (const struct GNUNET_PeerIdentity *peer,
+    const char *plugin_name,
+    const void *plugin_addr,
+    size_t plugin_addr_len,
+    uint32_t local_address_info,
+    uint32_t session_id)
 {
   struct ATS_Address *aa = NULL;
   int c1;
@@ -490,6 +495,7 @@ create_address (const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
   memcpy (&aa[1], plugin_addr, plugin_addr_len);
   aa->plugin = GNUNET_strdup (plugin_name);
   aa->session_id = session_id;
+  aa->local_address_info = local_address_info;
   aa->active = GNUNET_NO;
   aa->used = GNUNET_NO;
   aa->solver_information = NULL;
@@ -636,20 +642,25 @@ find_equivalent_address (struct GAS_Addresses_Handle *handle,
  * @param plugin_name transport plugin name
  * @param plugin_addr plugin address
  * @param plugin_addr_len length of the plugin address
+ * @param local_address_info the local address for the address
  * @param session_id session id, can be 0
  * @return an ATS_address or NULL
  */
 
 static struct ATS_Address *
 find_exact_address (struct GAS_Addresses_Handle *handle,
-    const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
-    const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
+    const struct GNUNET_PeerIdentity *peer,
+    const char *plugin_name,
+    const void *plugin_addr,
+    size_t plugin_addr_len,
+    uint32_t local_address_info,
+    uint32_t session_id)
 {
   struct ATS_Address *aa;
   struct ATS_Address *ea;
 
   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
-      session_id);
+      local_address_info, session_id);
 
   /* Get existing address or address with session == 0 */
   ea = find_equivalent_address (handle, peer, aa);
@@ -721,15 +732,21 @@ get_performance_info (struct ATS_Address *address, uint32_t type)
  * @param plugin_name transport plugin name
  * @param plugin_addr plugin address
  * @param plugin_addr_len length of the plugin address
+ * @param local_address_info the local address for the address
  * @param session_id session id, can be 0
  * @param atsi performance information for this address
  * @param atsi_count number of performance information contained
  */
 void
 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
-    const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
-    const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
-    const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
+    const struct GNUNET_PeerIdentity *peer,
+    const char *plugin_name,
+    const void *plugin_addr,
+    size_t plugin_addr_len,
+    uint32_t local_address_info,
+    uint32_t session_id,
+    const struct GNUNET_ATS_Information *atsi,
+    uint32_t atsi_count)
 {
   struct ATS_Address *new_address;
   struct ATS_Address *existing_address;
@@ -748,7 +765,7 @@ GAS_addresses_add (struct GAS_Addresses_Handle *handle,
   GNUNET_assert(NULL != handle->addresses);
 
   new_address = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
-      session_id);
+      local_address_info, session_id);
   atsi_delta = NULL;
   disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
       &atsi_delta_count);
@@ -885,15 +902,21 @@ GAS_addresses_add (struct GAS_Addresses_Handle *handle,
  * @param plugin_name transport plugin name
  * @param plugin_addr plugin address
  * @param plugin_addr_len length of the plugin address
+ * @param local_address_info the local address for the address
  * @param session_id session id, can be 0
  * @param atsi performance information for this address
  * @param atsi_count number of performance information contained
  */
 void
 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
-    const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
-    const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
-    const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
+    const struct GNUNET_PeerIdentity *peer,
+    const char *plugin_name,
+    const void *plugin_addr,
+    size_t plugin_addr_len,
+    uint32_t local_address_info,
+    uint32_t session_id,
+    const struct GNUNET_ATS_Information *atsi,
+    uint32_t atsi_count)
 {
   struct ATS_Address *aa;
   struct GNUNET_ATS_Information *atsi_delta;
@@ -908,7 +931,7 @@ GAS_addresses_update (struct GAS_Addresses_Handle *handle,
 
   /* Get existing address */
   aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
-      plugin_addr_len, session_id);
+      plugin_addr_len, local_address_info, session_id);
   if (aa == NULL )
     return;
   if (NULL == aa->solver_information)
@@ -1034,8 +1057,8 @@ destroy_by_session_id (void *cls,
       GNUNET_break(0);
       return GNUNET_OK;
     }
-
-    if (aa->addr_len == 0)
+    if (GNUNET_HELLO_ADDRESS_INFO_INBOUND ==
+        (aa->local_address_info && GNUNET_HELLO_ADDRESS_INFO_INBOUND))
     {
       /* Inbound connection died, delete full address */
       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
@@ -1076,12 +1099,17 @@ destroy_by_session_id (void *cls,
  * @param plugin_name transport plugin name
  * @param plugin_addr plugin address
  * @param plugin_addr_len length of the plugin address
+ * @param local_address_info the local address for the address
  * @param session_id session id, can be 0
  */
 void
 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
-    const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
-    const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
+    const struct GNUNET_PeerIdentity *peer,
+    const char *plugin_name,
+    const void *plugin_addr,
+    size_t plugin_addr_len,
+    uint32_t local_address_info,
+    uint32_t session_id)
 {
   struct ATS_Address *ea;
   struct DestroyContext dc;
@@ -1090,7 +1118,7 @@ GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
 
   /* Get existing address */
   ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
-      plugin_addr_len, session_id);
+      plugin_addr_len, local_address_info, session_id);
   if (ea == NULL )
   {
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
@@ -1106,7 +1134,7 @@ GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
   GNUNET_break(0 < strlen (plugin_name));
   dc.handle = handle;
   dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
-      session_id);
+      local_address_info, session_id);
 
   GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
                                              peer,
@@ -1131,6 +1159,7 @@ GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
  * @param plugin_name transport plugin name
  * @param plugin_addr plugin address
  * @param plugin_addr_len length of the plugin address
+ * @param local_address_info the local address for the address
  * @param session_id session id, can be 0
  * @param in_use GNUNET_YES if GNUNET_NO
  * @return GNUNET_SYSERR on failure (address unknown ...)
@@ -1138,7 +1167,9 @@ GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
 int
 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
-    const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
+    const void *plugin_addr, size_t plugin_addr_len,
+    uint32_t local_address_info,
+    uint32_t session_id,
     int in_use)
 {
   struct ATS_Address *ea;
@@ -1149,7 +1180,7 @@ GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
     return GNUNET_SYSERR;
 
   ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
-      plugin_addr_len, session_id);
+      plugin_addr_len, local_address_info, session_id);
   if (NULL == ea)
   {
     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
@@ -1261,7 +1292,8 @@ GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
       aa, GNUNET_i2s (peer));
 
   GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
-      aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
+      aa->addr_len, aa->local_address_info, aa->session_id,
+      aa->atsi, aa->atsi_count,
       aa->assigned_bw_out, aa->assigned_bw_in);
 
   aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
@@ -1323,9 +1355,10 @@ GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
 }
 
 
-
 static int
-eval_count_active_it (void *cls, const struct GNUNET_PeerIdentity *id, void *obj)
+eval_count_active_it (void *cls,
+                      const struct GNUNET_PeerIdentity *id,
+                      void *obj)
 {
   int *request_fulfilled = cls;
   struct ATS_Address *addr = obj;
@@ -1343,7 +1376,8 @@ eval_count_active_it (void *cls, const struct GNUNET_PeerIdentity *id, void *obj
 /**
  * Summary context
  */
-struct SummaryContext {
+struct SummaryContext
+{
   /**
    * Sum of the utilized inbound bandwidth per network
    */
@@ -1364,11 +1398,10 @@ struct SummaryContext {
 static int
 eval_sum_bw_used (void *cls, const struct GNUNET_PeerIdentity *id, void *obj)
 {
+  struct SummaryContext *ctx = cls;
   struct ATS_Address *addr = obj;
   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
   int net;
-  struct SummaryContext  *ctx = cls;
-
   int c;
 
   if (GNUNET_YES == addr->active)
@@ -1383,22 +1416,26 @@ eval_sum_bw_used (void *cls, const struct GNUNET_PeerIdentity *id, void *obj)
         ctx->bandwidth_out_assigned[c] += ntohl (addr->assigned_bw_out.value__);
       }
     }
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Active address in  %s with (in/out) %llu/%llu Bps\n",
-        GNUNET_ATS_print_network_type(net),
-        ntohl (addr->assigned_bw_in.value__),
-        ntohl (addr->assigned_bw_out.value__));
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "Active address in  %s with (in/out) %u/%u Bps\n",
+                GNUNET_ATS_print_network_type (net),
+                (unsigned int) ntohl (addr->assigned_bw_in.value__),
+                (unsigned int) ntohl (addr->assigned_bw_out.value__));
   }
   return GNUNET_OK;
 }
 
+
 /**
  * Summary context
  */
-struct RelativityContext {
+struct RelativityContext
+{
 
   struct GAS_Addresses_Handle *ah;
 };
 
+
 static int
 find_active_address (void *cls, const struct GNUNET_PeerIdentity *id, void *obj)
 {
@@ -2044,32 +2081,36 @@ bandwidth_changed_cb (void *cls, struct ATS_Address *address)
   }
   if (NULL == cur)
   {
-    GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Nobody is interested in peer `%s' :(\n",
-        GNUNET_i2s (&address->peer));
+    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+               "Nobody is interested in peer `%s' :(\n",
+               GNUNET_i2s (&address->peer));
     return;
   }
 
   if ((0 == ntohl (address->assigned_bw_in.value__))
       && (0 == ntohl (address->assigned_bw_out.value__)))
   {
-    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
-        "Telling transport to disconnect peer `%s'\n",
-        GNUNET_i2s (&address->peer));
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+               "Telling transport to disconnect peer `%s'\n",
+                GNUNET_i2s (&address->peer));
   }
   else
   {
     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
-        "Sending bandwidth update for peer `%s': %llu %llu\n",
-        GNUNET_i2s (&address->peer), address->assigned_bw_out,
-        address->assigned_bw_out);
+               "Sending bandwidth update for peer `%s': %u %u\n",
+               GNUNET_i2s (&address->peer),
+               (unsigned int) ntohl (address->assigned_bw_out.value__),
+               (unsigned int) ntohl (address->assigned_bw_out.value__));
   }
 
   /* *Notify scheduling clients about suggestion */
   GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
-      address->addr, address->addr_len, address->session_id, address->atsi,
+      address->addr, address->addr_len, address->local_address_info,
+      address->session_id, address->atsi,
       address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
 }
 
+
 /**
  * Initialize address subsystem. The addresses subsystem manages the addresses
  * known and current performance information. It has a solver component
@@ -2101,7 +2142,6 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   GNUNET_assert(NULL != ah->addresses);
 
   /* Figure out configured solution method */
-  plugin_short = NULL;
   if (GNUNET_SYSERR
       == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
   {
@@ -2114,34 +2154,25 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
     for (c = 0; c < strlen (mode_str); c++)
       mode_str[c] = toupper (mode_str[c]);
     if (0 == strcmp (mode_str, "PROPORTIONAL"))
-    {
       ah->ats_mode = MODE_PROPORTIONAL;
-      plugin_short = "proportional";
-    }
     else if (0 == strcmp (mode_str, "MLP"))
     {
       ah->ats_mode = MODE_MLP;
-      plugin_short = "mlp";
 #if !HAVE_LIBGLPK
       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
           "Assignment method `%s' configured, but GLPK is not available, please install \n",
           mode_str);
       ah->ats_mode = MODE_PROPORTIONAL;
-      plugin_short = "proportional";
 #endif
     }
     else if (0 == strcmp (mode_str, "RIL"))
-    {
       ah->ats_mode = MODE_RIL;
-      plugin_short = "ril";
-    }
     else
     {
       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
           "Invalid resource assignment method `%s' configured, using proportional approach\n",
           mode_str);
       ah->ats_mode = MODE_PROPORTIONAL;
-      plugin_short = "proportional";
     }
     GNUNET_free(mode_str);
   }
@@ -2168,6 +2199,20 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
     ah->env.in_quota[c] = quotas_in[c];
   }
 
+  switch (ah->ats_mode) {
+    case MODE_PROPORTIONAL:
+      plugin_short = "proportional";
+      break;
+    case MODE_MLP:
+      plugin_short = "mlp";
+      break;
+    case MODE_RIL:
+      plugin_short = "ril";
+      break;
+    default:
+      plugin_short = NULL;
+      break;
+  }
   GNUNET_asprintf (&ah->plugin, "libgnunet_plugin_ats_%s", plugin_short);
   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s '`%s'\n"), plugin_short, ah->plugin);
   if  (NULL == (ah->solver = GNUNET_PLUGIN_load (ah->plugin, &ah->env)))