-fix time assertion introduce in last patch
[oweals/gnunet.git] / src / topology / gnunet-daemon-topology.c
index 5b59995a9f5f8d7e73bcbefeea831f943f752f82..ba78f25dda985756c4b4be040eed2032240ae625 100644 (file)
 
 /**
  * @file topology/gnunet-daemon-topology.c
- * @brief code for maintaining the mesh topology
+ * @brief code for maintaining the cadet topology
  * @author Christian Grothoff
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
+#include "gnunet_friends_lib.h"
 #include "gnunet_constants.h"
 #include "gnunet_core_service.h"
 #include "gnunet_protocols.h"
@@ -61,7 +62,7 @@
 
 /**
  * For how long do we blacklist anyone under any cirumstances at most after a failed connection
- * attempt?  This is the absolute maximum, regardless of what the calculation based on 
+ * attempt?  This is the absolute maximum, regardless of what the calculation based on
  * exponential back-off returns.
  */
 #define GREYLIST_AFTER_ATTEMPT_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
@@ -482,7 +483,7 @@ make_peer (const struct GNUNET_PeerIdentity *peer,
 {
   struct Peer *ret;
 
-  ret = GNUNET_malloc (sizeof (struct Peer));
+  ret = GNUNET_new (struct Peer);
   ret->pid = *peer;
   ret->is_friend = is_friend;
   if (hello != NULL)
@@ -640,7 +641,8 @@ schedule_next_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     /* now! */
     pl->hello_req =
-        GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES, 0,
+        GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES,
+                                           GNUNET_CORE_PRIO_BEST_EFFORT,
                                            GNUNET_CONSTANTS_SERVICE_TIMEOUT,
                                            &pl->pid, next_want,
                                            &hello_advertising_ready, pl);
@@ -981,7 +983,7 @@ process_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param my_id ID of this peer, NULL if we failed
  */
 static void
-core_init (void *cls, 
+core_init (void *cls,
            const struct GNUNET_PeerIdentity *my_id)
 {
   if (NULL == my_id)
@@ -993,127 +995,72 @@ core_init (void *cls,
     return;
   }
   my_identity = *my_id;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-             "I am peer `%s'\n", 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "I am peer `%s'\n",
              GNUNET_i2s (my_id));
-  peerinfo_notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO, 
+  peerinfo_notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO,
                                            &process_peer, NULL);
 }
 
 
 /**
- * Read the friends file.
+ * Process friend found in FRIENDS file.
+ *
+ * @param cls pointer to an `unsigned int` to be incremented per friend found
+ * @param pid identity of the friend
  */
 static void
-read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
+handle_friend (void *cls,
+               const struct GNUNET_PeerIdentity *pid)
 {
-  char *fn;
-  char *data;
-  size_t pos;
-  size_t start;
-  struct GNUNET_PeerIdentity pid;
-  uint64_t fsize;
-  unsigned int entries_found;
+  unsigned int *entries_found = cls;
   struct Peer *fl;
 
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
-  {
-    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
-                              "topology", "FRIENDS");
-    return;
-  }
-  if ( (GNUNET_OK != GNUNET_DISK_file_test (fn)) &&
-       (GNUNET_OK != GNUNET_DISK_fn_write (fn, NULL, 0,
-                                          GNUNET_DISK_PERM_USER_READ |
-                                          GNUNET_DISK_PERM_USER_WRITE)) )
-      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
-  if (GNUNET_OK != GNUNET_DISK_file_size (fn,
-      &fsize, GNUNET_NO, GNUNET_YES))
-  {
-    if ((friends_only) || (minimum_friend_count > 0))
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  _("Could not read friends list `%s'\n"), fn);
-    GNUNET_free (fn);
-    return;
-  }
-  if (0 == fsize)
+  if (0 == memcmp (pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-               _("Friends file `%s' is empty.\n"),
-                fn);
-    GNUNET_free (fn);
-    return;
-  }
-  data = GNUNET_malloc_large (fsize);
-  if (NULL == data)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Failed to read friends list from `%s': out of memory\n"),
-                fn);
-    GNUNET_free (fn);
-    return;
-  }
-  if (fsize != GNUNET_DISK_fn_read (fn, data, fsize))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Failed to read friends list from `%s'\n"), fn);
-    GNUNET_free (fn);
-    GNUNET_free (data);
+                _("Found myself `%s' in friend list (useless, ignored)\n"),
+                GNUNET_i2s (pid));
     return;
   }
+  (*entries_found)++;
+  fl = make_peer (pid, NULL, GNUNET_YES);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              _("Found friend `%s' in configuration\n"),
+              GNUNET_i2s (&fl->pid));
+}
+
+
+/**
+ * Read the friends file.
+ */
+static void
+read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+  unsigned int entries_found;
+
   entries_found = 0;
-  start = 0;
-  pos = 0;
-  while (pos < fsize)
+  if (GNUNET_OK !=
+      GNUNET_FRIENDS_parse (cfg,
+                            &handle_friend,
+                            &entries_found))
   {
-    while ((pos < fsize) && isspace ((unsigned char) data[pos]))
-      pos++;
-    if (GNUNET_OK !=
-        GNUNET_CRYPTO_ecc_public_sign_key_from_string (&data[start],
-                                                      pos - start,
-                                                      &pid.public_key))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  _("Syntax error in topology specification at offset %llu, skipping bytes `%.*s'.\n"),
-                  (unsigned long long) pos,
-                 (int) (pos - start),
-                 &data[start]);
-      pos++;
-      start = pos;
-      continue;
-    }
-    pos++;
-    start = pos;
-    if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
-    {
+    if ((friends_only) || (minimum_friend_count > 0))
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Found myself `%s' in friend list (useless, ignored)\n"),
-                 GNUNET_i2s (&pid));
-      continue;
-    }
-    entries_found++;
-    fl = make_peer (&pid, NULL, GNUNET_YES);
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-               _("Found friend `%s' in configuration\n"),
-               GNUNET_i2s (&fl->pid));
+                  _("Encountered errors parsing friends list!\n"));
   }
-  GNUNET_free (data);
-  GNUNET_free (fn);
   GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
                             entries_found, GNUNET_NO);
   if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _
-                ("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
+                _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
   }
   if ((minimum_friend_count > target_connection_count) &&
       (friends_only == GNUNET_NO))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _
-                ("More friendly connections required than target total number of connections.\n"));
+                _("More friendly connections required than target total number of connections.\n"));
   }
 }
 
@@ -1262,7 +1209,9 @@ cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param c configuration
  */
 static void
-run (void *cls, char *const *args, const char *cfgfile,
+run (void *cls,
+     char *const *args,
+     const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
   static struct GNUNET_CORE_MessageHandler handlers[] = {
@@ -1296,8 +1245,12 @@ run (void *cls, char *const *args, const char *cfgfile,
     blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
   transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
   handle =
-      GNUNET_CORE_connect (cfg, NULL, &core_init, &connect_notify,
-                           &disconnect_notify, NULL, GNUNET_NO, NULL, GNUNET_NO,
+      GNUNET_CORE_connect (cfg, NULL,
+                           &core_init,
+                           &connect_notify,
+                           &disconnect_notify,
+                           NULL, GNUNET_NO,
+                           NULL, GNUNET_NO,
                            handlers);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
                                 NULL);
@@ -1340,7 +1293,7 @@ main (int argc, char *const *argv)
       (GNUNET_OK ==
        GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",
                            _
-                           ("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
+                           ("GNUnet topology control (maintaining P2P cadet and F2F constraints)"),
                            options, &run, NULL)) ? 0 : 1;
   GNUNET_free ((void*) argv);
   return ret;