- fix for 601 assertion
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.c
index c5923a0997d8087b05d045cb784f84a974b85cad..aff7cede1056f855aa4dea075632a03b1ae8d7cb 100644 (file)
@@ -66,6 +66,8 @@ static unsigned int active_addr_count;
 
 static int ats_mode;
 
+static int running;
+
 
 static void
 send_bw_notification (struct ATS_Address *aa)
@@ -339,6 +341,11 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
   struct ATS_Address *old;
   uint32_t i;
 
+  if (GNUNET_NO == running)
+    return;
+
+  GNUNET_assert (NULL != addresses);
+
   aa = create_address (peer,
                        plugin_name,
                        plugin_addr, plugin_addr_len,
@@ -510,6 +517,9 @@ GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
 {
   struct ATS_Address *aa;
 
+  if (GNUNET_NO == running)
+    return;
+
   GNUNET_break (0 < strlen (plugin_name));
   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
 
@@ -566,7 +576,7 @@ find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
 }
 
 
-void
+int
 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
                       const char *plugin_name, const void *plugin_addr,
                       size_t plugin_addr_len, uint32_t session_id, int in_use)
@@ -580,19 +590,34 @@ GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
   struct ATS_Address *aa;
   struct ATS_Address *old;
 
+  if (GNUNET_NO == running)
+    return GNUNET_SYSERR;
 
-  aa = create_address(peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
+  aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
   old = find_exact_address (peer, aa);
   free_address (aa);
 
-  GNUNET_assert (old != NULL);
-  GNUNET_assert (old->used != in_use);
+  if (NULL == old)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  if (old->used == in_use)
+  {
+    GNUNET_break (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Address in use called multiple times for peer `%s': %s -> %s \n",
+                GNUNET_i2s (peer),
+                (GNUNET_NO == old->used) ? "NO" : "YES",
+                (GNUNET_NO == in_use) ? "NO" : "YES");
+    return GNUNET_SYSERR;
+  }
   old->used = in_use;
-
 #if HAVE_LIBGLPK
   if (ats_mode == MLP)
      GAS_mlp_address_update (mlp, addresses, old);
 #endif
+  return GNUNET_OK;
 }
 
 
@@ -615,7 +640,7 @@ void request_address_mlp (const struct GNUNET_PeerIdentity *peer)
 
   if (aa == NULL)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
     return;
   }
@@ -648,7 +673,7 @@ void request_address_simple (const struct GNUNET_PeerIdentity *peer)
                                               &find_address_it, &aa);
   if (aa == NULL)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
     return;
   }
@@ -677,6 +702,9 @@ void request_address_simple (const struct GNUNET_PeerIdentity *peer)
 void
 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
 {
+  if (GNUNET_NO == running)
+    return;
+
   if (ats_mode == SIMPLE)
   {
     request_address_simple (peer);
@@ -695,6 +723,8 @@ GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
                                  enum GNUNET_ATS_PreferenceKind kind,
                                  float score)
 {
+  if (GNUNET_NO == running)
+    return;
 #if HAVE_LIBGLPK
   if (ats_mode == MLP)
     GAS_mlp_address_change_preference (mlp, peer, kind, score);
@@ -713,42 +743,86 @@ void
 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
                     const struct GNUNET_STATISTICS_Handle *stats)
 {
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
-                                                      "WAN_QUOTA_IN",
-                                                      &wan_quota_in));
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
-                                                      "WAN_QUOTA_OUT",
-                                                      &wan_quota_out));
-
-  switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP"))
+  int mode;
+
+  char *quota_wan_in_str;
+  char *quota_wan_out_str;
+
+  running = GNUNET_NO;
+
+  addresses = GNUNET_CONTAINER_multihashmap_create (128);
+  GNUNET_assert (NULL != addresses);
+
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_wan_in_str))
   {
-       /* MLP = YES */
-       case GNUNET_YES:
+    if (0 == strcmp(quota_wan_in_str, "unlimited") ||
+        (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &wan_quota_in)))
+      wan_quota_in = (UINT32_MAX) /10;
+
+    GNUNET_free (quota_wan_in_str);
+    quota_wan_in_str = NULL;
+  }
+  else
+  {
+    wan_quota_in = (UINT32_MAX) /10;
+  }
+
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_wan_out_str))
+  {
+    if (0 == strcmp(quota_wan_out_str, "unlimited") ||
+        (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &wan_quota_out)))
+      wan_quota_out = (UINT32_MAX) /10;
+
+    GNUNET_free (quota_wan_out_str);
+    quota_wan_out_str = NULL;
+  }
+  else
+  {
+    wan_quota_out = (UINT32_MAX) /10;
+  }
+
+  mode = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP mode %u", mode);
+  switch (mode)
+  {
+    /* MLP = YES */
+    case GNUNET_YES:
 #if HAVE_LIBGLPK
-          ats_mode = MLP;
-          /* Init the MLP solver with default values */
-          mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
-          break;
+      ats_mode = MLP;
+      /* Init the MLP solver with default values */
+      mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
+      if (NULL == mlp)
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode\n");
+        GNUNET_STATISTICS_update (GSA_stats, "MLP mode enabled", 0, GNUNET_NO);
+        break;
+      }
+      else
+      {
+        GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 1, GNUNET_NO);
+        break;
+      }
 #else
-          GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
-          ats_mode = SIMPLE;
-          break;
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
+      GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
+      ats_mode = SIMPLE;
+      break;
 #endif
-       /* MLP = NO */
-       case GNUNET_NO:
-               ats_mode = SIMPLE;
-               break;
-       /* No configuration value */
-       case GNUNET_SYSERR:
-               ats_mode = SIMPLE;
-               break;
-       default:
-               break;
+    /* MLP = NO */
+    case GNUNET_NO:
+      GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
+      ats_mode = SIMPLE;
+      break;
+    /* No configuration value */
+    case GNUNET_SYSERR:
+      GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
+      ats_mode = SIMPLE;
+      break;
+    default:
+      break;
   }
-
-  addresses = GNUNET_CONTAINER_multihashmap_create (128);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started with %s mode\n", (SIMPLE == ats_mode) ? "SIMPLE" : "MLP");
+  running = GNUNET_YES;
 }
 
 
@@ -773,6 +847,9 @@ free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
 void
 GAS_addresses_destroy_all ()
 {
+  if (GNUNET_NO == running)
+    return;
+
   if (addresses != NULL)
     GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
   GNUNET_assert (active_addr_count == 0);
@@ -785,6 +862,7 @@ GAS_addresses_destroy_all ()
 void
 GAS_addresses_done ()
 {
+  running = GNUNET_NO;
   GAS_addresses_destroy_all ();
   GNUNET_CONTAINER_multihashmap_destroy (addresses);
   addresses = NULL;