-never store NICKs anywhere but in '+', do not display nicks in gnunet-namestore
[oweals/gnunet.git] / src / namestore / gnunet-namestore.c
index 5becf0e6c9eb994f75c23d663369b754be34ccfe..e985e1b049d8b2c5356d0faacff0b3d1efcdece9 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
      This file is part of GNUnet.
      (C) 2012, 2013 Christian Grothoff (and other contributing authors)
@@ -31,6 +30,7 @@
 #include <gnunet_dnsparser_lib.h>
 #include <gnunet_identity_service.h>
 #include <gnunet_gnsrecord_lib.h>
+#include <gnunet_gns_service.h>
 #include <gnunet_namestore_service.h>
 
 
@@ -49,6 +49,16 @@ static struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
  */
 static struct GNUNET_IDENTITY_EgoLookup *el;
 
+/**
+ * Identity service handle
+ */
+static struct GNUNET_IDENTITY_Handle *idh;
+
+/**
+ * Obtain default ego
+ */
+struct GNUNET_IDENTITY_Operation *get_default;
+
 /**
  * Name of the ego controlling the zone.
  */
@@ -134,6 +144,11 @@ static char *typestring;
  */
 static char *expirationstring;
 
+/**
+ * Desired nick name.
+ */
+static char *nickstring;
+
 /**
  * Global return value
  */
@@ -190,6 +205,16 @@ static void
 do_shutdown (void *cls,
             const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  if (NULL != get_default)
+  {
+    GNUNET_IDENTITY_cancel (get_default);
+    get_default = NULL;
+  }
+  if (NULL != idh)
+  {
+    GNUNET_IDENTITY_disconnect (idh);
+    idh = NULL;
+  }
   if (NULL != el)
   {
     GNUNET_IDENTITY_ego_lookup_cancel (el);
@@ -280,6 +305,7 @@ add_continuation (void *cls,
     if (GNUNET_NO != success)
       ret = 1;
   }
+  ret = 0;
   test_finished ();
 }
 
@@ -358,6 +384,8 @@ display_record (void *cls,
           rname);
   for (i=0;i<rd_len;i++)
   {
+    if (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type)
+      continue;
     typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
     s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
                                          rd[i].data,
@@ -521,7 +549,7 @@ testservice_task (void *cls,
             "namestore");
     return;
   }
-  if (! (add|del|list|(NULL != uri)|(NULL != reverse_pkey)) )
+  if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
   {
     /* nothing more to be done */
     fprintf (stderr,
@@ -678,12 +706,8 @@ testservice_task (void *cls,
     char sname[64];
     struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
 
-    if ( (2 != (sscanf (uri,
-                        "gnunet://gns/%104s/%63s",
-                        sh,
-                        sname)) ) ||
-         (GNUNET_OK !=
-          GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
+    if ( (2 != (sscanf (uri, "gnunet://gns/%52s/%63s", sh, sname)) ) ||
+         (GNUNET_OK != GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
     {
       fprintf (stderr,
                _("Invalid URI `%s'\n"),
@@ -705,7 +729,8 @@ testservice_task (void *cls,
       rd.expiration_time = etime_abs.abs_value_us;
     else
       rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
-    if (1 != shadow)
+
+    if (1 == shadow)
       rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
     add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
                                                 &zone_pkey,
@@ -715,6 +740,20 @@ testservice_task (void *cls,
                                                 &add_continuation,
                                                 &add_qe_uri);
   }
+  if (NULL != nickstring)
+  {
+    if (0 == strlen(nickstring))
+    {
+      fprintf (stderr,
+               _("Invalid nick `%s'\n"),
+               nickstring);
+      GNUNET_SCHEDULER_shutdown ();
+      ret = 1;
+      return;
+    }
+    add_qe_uri = GNUNET_NAMESTORE_set_nick(ns, &zone_pkey, nickstring,
+        &add_continuation, &add_qe_uri);
+  }
   if (monitor)
   {
     zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
@@ -743,14 +782,18 @@ identity_cb (void *cls,
   el = NULL;
   if (NULL == ego)
   {
-    fprintf (stderr,
-            _("Ego `%s' not known to identity service\n"),
-            ego_name);
+    if (NULL != ego_name)
+    {
+      fprintf (stderr,
+               _("Ego `%s' not known to identity service\n"),
+               ego_name);
+    }
     GNUNET_SCHEDULER_shutdown ();
+    ret = -1;
     return;
   }
   zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
-  GNUNET_free (ego_name);
+  GNUNET_free_non_null (ego_name);
   ego_name = NULL;
   GNUNET_CLIENT_service_test ("namestore", cfg,
                              GNUNET_TIME_UNIT_SECONDS,
@@ -759,6 +802,72 @@ identity_cb (void *cls,
 }
 
 
+static void
+default_ego_cb (void *cls,
+    struct GNUNET_IDENTITY_Ego *ego,
+    void **ctx,
+    const char *name)
+{
+  get_default = NULL;
+  if (NULL == ego)
+  {
+    fprintf (stderr,
+             _("No default ego configured in identity service\n"));
+    GNUNET_SCHEDULER_shutdown ();
+    ret = -1;
+    return;
+  }
+  else
+  {
+    identity_cb (cls, ego);
+  }
+}
+
+static void
+id_connect_cb (void *cls,
+    struct GNUNET_IDENTITY_Ego *ego,
+    void **ctx,
+    const char *name)
+{
+  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+  if (NULL == ego)
+  {
+    get_default = GNUNET_IDENTITY_get (idh, "namestore",
+        &default_ego_cb, (void *) cfg);
+  }
+}
+
+
+static void
+testservice_id_task (void *cls, int result)
+{
+  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+  if (result != GNUNET_YES)
+  {
+    fprintf (stderr,
+             _("Identity service is not running\n"));
+    GNUNET_SCHEDULER_shutdown ();
+    ret = -1;
+    return;
+  }
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
+                                &do_shutdown, (void *) cfg);
+
+  if (NULL == ego_name)
+  {
+    idh = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, (void *) cfg);
+    if (NULL == idh)
+      fprintf (stderr, _("Cannot connect to identity service\n"));
+    ret = -1;
+    return;
+  }
+  el = GNUNET_IDENTITY_ego_lookup (cfg,
+                                   ego_name,
+                                   &identity_cb,
+                                   (void *) cfg);
+}
+
+
 /**
  * Main function that will be run.
  *
@@ -771,20 +880,13 @@ static void
 run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  if (NULL == ego_name)
-  {
-    fprintf (stderr,
-            _("You must specify which zone should be accessed\n"));
-    return;
-  }
   if ( (NULL != args[0]) && (NULL == uri) )
     uri = GNUNET_strdup (args[0]);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                &do_shutdown, NULL);
-  el = GNUNET_IDENTITY_ego_lookup (cfg,
-                                  ego_name,
-                                  &identity_cb,
-                                  (void *) cfg);
+
+  GNUNET_CLIENT_service_test ("identity", cfg,
+                              GNUNET_TIME_UNIT_SECONDS,
+                              &testservice_id_task,
+                              (void *) cfg);
 }
 
 
@@ -814,6 +916,9 @@ main (int argc, char *const *argv)
     {'e', "expiration", "TIME",
      gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
      &GNUNET_GETOPT_set_string, &expirationstring},
+    {'i', "nick", "NICKNAME",
+     gettext_noop ("set the desired nick name for the zone"), 1,
+     &GNUNET_GETOPT_set_string, &nickstring},
     {'m', "monitor", NULL,
      gettext_noop ("monitor changes in the namestore"), 0,
      &GNUNET_GETOPT_set_one, &monitor},