Do not crash if key is NULL.
[oweals/gnunet.git] / src / fs / gnunet-service-fs.c
index 72fa662c1c78fb1c76ec7631d5f05a71e6a6b5db..b824e6dc64105e0f028d1faa633d7a00e1c64676 100644 (file)
@@ -43,7 +43,7 @@
 #include "gnunet-service-fs_pr.h"
 #include "gnunet-service-fs_push.h"
 #include "gnunet-service-fs_put.h"
-#include "gnunet-service-fs_mesh.h"
+#include "gnunet-service-fs_cadet.h"
 #include "fs.h"
 
 /**
@@ -202,7 +202,7 @@ GSF_update_datastore_delay_ (struct GNUNET_TIME_Absolute start)
   struct GNUNET_TIME_Relative delay;
 
   delay = GNUNET_TIME_absolute_get_duration (start);
-  GNUNET_LOAD_update (datastore_get_load, delay.rel_value);
+  GNUNET_LOAD_update (datastore_get_load, delay.rel_value_us);
 }
 
 
@@ -248,29 +248,35 @@ update_latencies (void *cls,
                  int active,
                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
-                 const struct GNUNET_ATS_Information *ats, 
+                 const struct GNUNET_ATS_Information *ats,
                  uint32_t ats_count)
 {
   unsigned int i;
   struct GNUNET_TIME_Relative latency;
 
+  if (NULL == address)
+  {
+    /* ATS service temporarily disconnected */
+    return;
+  }
+
   if (GNUNET_YES != active)
-       return;
+    return;
   for (i = 0; i < ats_count; i++)
   {
     if (GNUNET_ATS_QUALITY_NET_DELAY != ntohl (ats[i].type))
       continue;
-    latency.rel_value = ntohl (ats[i].value);
+    latency.rel_value_us = ntohl (ats[i].value);
     GSF_update_peer_latency_ (&address->peer,
                              latency);
-    GSF_avg_latency.rel_value =
-      (GSF_avg_latency.rel_value * 31 +
+    GSF_avg_latency.rel_value_us =
+      (GSF_avg_latency.rel_value_us * 31 +
        GNUNET_MIN (5000, ntohl (ats[i].value))) / 32;
     GNUNET_STATISTICS_set (GSF_stats,
                           gettext_noop
                           ("# running average P2P latency (ms)"),
-                          GSF_avg_latency.rel_value, GNUNET_NO);
-    break;    
+                          GSF_avg_latency.rel_value_us / 1000LL, GNUNET_NO);
+    break;
   }
 }
 
@@ -412,11 +418,11 @@ start_p2p_processing (void *cls, struct GSF_PendingRequest *pr,
     {
     case GNUNET_BLOCK_TYPE_FS_DBLOCK:
     case GNUNET_BLOCK_TYPE_FS_IBLOCK:
-      /* the above block types MAY be available via 'mesh' */
+      /* the above block types MAY be available via 'cadet' */
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Considering mesh-based download for block\n");
-      GSF_mesh_lookup_ (pr);
-      break; 
+                 "Considering cadet-based download for block\n");
+      GSF_cadet_lookup_ (pr);
+      break;
     case GNUNET_BLOCK_TYPE_FS_UBLOCK:
       /* the above block types are in the DHT */
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -475,7 +481,8 @@ handle_start_search (void *cls, struct GNUNET_SERVER_Client *client,
 static void
 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GSF_mesh_stop ();
+  GSF_cadet_stop_client ();
+  GSF_cadet_stop_server ();
   if (NULL != GSF_core)
   {
     GNUNET_CORE_disconnect (GSF_core);
@@ -544,6 +551,21 @@ consider_peer_for_forwarding (void *cls, const struct GNUNET_HashCode * key,
 }
 
 
+/**
+ * Function called after the creation of a connected peer record is complete.
+ *
+ * @param cls closure (unused)
+ * @param cp handle to the newly created connected peer record
+ */
+static void
+connected_peer_cb (void *cls, struct GSF_ConnectedPeer *cp)
+{
+  if (NULL == cp)
+    return;
+  GSF_iterate_pending_requests_ (&consider_peer_for_forwarding, cp);
+}
+
+
 /**
  * Method called whenever a given peer connects.
  *
@@ -553,14 +575,9 @@ consider_peer_for_forwarding (void *cls, const struct GNUNET_HashCode * key,
 static void
 peer_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
 {
-  struct GSF_ConnectedPeer *cp;
-
   if (0 == memcmp (&my_id, peer, sizeof (struct GNUNET_PeerIdentity)))
     return;
-  cp = GSF_peer_connect_handler_ (peer);
-  if (NULL == cp)
-    return;
-  GSF_iterate_pending_requests_ (&consider_peer_for_forwarding, cp);
+  GSF_peer_connect_handler_ (peer, &connected_peer_cb, NULL);
 }
 
 
@@ -572,11 +589,10 @@ peer_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
  * directly (which should work if you are authorized...).
  *
  * @param cls closure
- * @param server handle to the server, NULL if we failed
  * @param my_identity ID of this peer, NULL if we failed
  */
 static void
-peer_init_handler (void *cls, struct GNUNET_CORE_Handle *server,
+peer_init_handler (void *cls,
                    const struct GNUNET_PeerIdentity *my_identity)
 {
   my_id = *my_identity;
@@ -625,7 +641,7 @@ main_init (struct GNUNET_SERVER_Handle *server,
   anon_p2p_off = (GNUNET_YES ==
                  GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
                                                        "fs",
-                                                       "DISABLE_ANON_TRANSFER"));  
+                                                       "DISABLE_ANON_TRANSFER"));
   GSF_core =
       GNUNET_CORE_connect (GSF_cfg, NULL, &peer_init_handler,
                            &peer_connect_handler, &GSF_peer_disconnect_handler_,
@@ -646,7 +662,8 @@ main_init (struct GNUNET_SERVER_Handle *server,
       GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
                                     NULL);
   datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
-  GSF_mesh_start ();
+  GSF_cadet_start_server ();
+  GSF_cadet_start_client ();
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
                                 NULL);
   return GNUNET_OK;
@@ -687,7 +704,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   GSF_rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
   GSF_stats = GNUNET_STATISTICS_create ("fs", cfg);
   block_cfg = GNUNET_CONFIGURATION_create ();
-  GNUNET_CONFIGURATION_set_value_string (block_cfg, "block", "PLUGINS", "fs");
   GSF_block_ctx = GNUNET_BLOCK_context_create (block_cfg);
   GNUNET_assert (NULL != GSF_block_ctx);
   GSF_dht = GNUNET_DHT_connect (cfg, FS_DHT_HT_SIZE);