-more debug messages
[oweals/gnunet.git] / src / namestore / gnunet-namestore.c
index a80f880520d6aec3d66b48c70033c6c648dd8f9f..a79178abab261bc137645b6a130ae9b90c1f614d 100644 (file)
  * @author Christian Grothoff
  *
  * TODO:
- * - printing records
  * - allow users to set record options (not just 'RF_AUTHORITY')
  * - test
- * - parsing SOA, PTR and MX value specifications (and define format!)
  * - add options to list/lookup individual records
  */
 #include "platform.h"
@@ -34,6 +32,7 @@
 #include <gnunet_dnsparser_lib.h>
 #include <gnunet_namestore_service.h>
 
+
 /**
  * Handle to the namestore.
  */
@@ -42,7 +41,7 @@ static struct GNUNET_NAMESTORE_Handle *ns;
 /**
  * Hash of the public key of our zone.
  */
-static GNUNET_HashCode zone;
+static struct GNUNET_CRYPTO_ShortHashCode zone;
 
 /**
  * Private key for the our zone.
@@ -79,6 +78,16 @@ static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
  */
 static int del;
 
+/**
+ * Is record public
+ */
+static int public;
+
+/**
+ * Is record authority
+ */
+static int nonauthority;
+
 /**
  * Queue entry for the 'del' operation.
  */
@@ -103,7 +112,7 @@ static char *typestring;
  * Desired expiration time.
  */
 static char *expirationstring;
-               
+
 
 /**
  * Task run on shutdown.  Cleans up everything.
@@ -117,7 +126,7 @@ do_shutdown (void *cls,
 {
   if (NULL != ns)
   {
-    GNUNET_NAMESTORE_disconnect (ns, GNUNET_NO);
+    GNUNET_NAMESTORE_disconnect (ns);
     ns = NULL;
   }
   if (NULL != zone_pkey)
@@ -205,6 +214,10 @@ display_record (void *cls,
                const struct GNUNET_NAMESTORE_RecordData *rd,
                const struct GNUNET_CRYPTO_RsaSignature *signature)
 {
+  const char *typestring;
+  char *s;
+  unsigned int i;
+
   if (NULL == name)
   {
     list_it = NULL;
@@ -213,7 +226,25 @@ display_record (void *cls,
       GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  // FIXME: display record!
+  FPRINTF (stdout,
+          "%s:\n",
+          name);
+  for (i=0;i<rd_len;i++)
+  {
+    typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
+    s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
+                                         rd[i].data,
+                                         rd[i].data_size);
+    if (NULL == s)
+    {
+      FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
+              (unsigned int) rd[i].record_type);
+      continue;
+    }
+    FPRINTF (stdout, "\t%s: %s\n", typestring, s);
+    GNUNET_free (s);    
+  }
+  FPRINTF (stdout, "%s", "\n");
   GNUNET_NAMESTORE_zone_iterator_next (list_it);
 }
 
@@ -232,19 +263,26 @@ run (void *cls, char *const *args, const char *cfgfile,
 {
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
   uint32_t type;
-  const void *data;
-  size_t data_size;
-  struct in_addr value_a;
-  struct in6_addr value_aaaa;
-  struct GNUNET_TIME_Relative etime;
+  void *data = NULL;
+  size_t data_size = 0;
+  struct GNUNET_TIME_Relative etime_rel;
+  struct GNUNET_TIME_Absolute etime_abs;
+  int etime_is_rel = GNUNET_SYSERR;
   struct GNUNET_NAMESTORE_RecordData rd;
 
   if (NULL == keyfile)
   {
-    fprintf (stderr,
-            _("Option `%s' not given, but I need a zone key file!\n"),
-            "z");
-    return;
+      if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
+                                                 "ZONEKEY", &keyfile))
+      {
+        fprintf (stderr,
+                 _("Option `%s' not given, but I need a zone key file!\n"),
+                 "z");
+        return;
+      }
+      fprintf (stderr,
+               _("Using default zone file `%s'\n"),
+               keyfile);
   }
   zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
   GNUNET_free (keyfile);
@@ -252,6 +290,8 @@ run (void *cls, char *const *args, const char *cfgfile,
   if (! (add|del|list))
   {
     /* nothing more to be done */  
+    fprintf (stderr,
+             _("No options given\n"));
     GNUNET_CRYPTO_rsa_key_free (zone_pkey);
     zone_pkey = NULL;
     return; 
@@ -264,7 +304,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   GNUNET_CRYPTO_rsa_key_get_public (zone_pkey,
                                    &pub);
-  GNUNET_CRYPTO_hash (&pub, sizeof (pub), &zone);
+  GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &zone);
 
   ns = GNUNET_NAMESTORE_connect (cfg);
   if (NULL == ns)
@@ -284,7 +324,8 @@ run (void *cls, char *const *args, const char *cfgfile,
     fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
     GNUNET_SCHEDULER_shutdown ();
     return;
-  } else if (add | del)
+  }
+  if ((NULL == typestring) && (add | del))
   {
     fprintf (stderr,
             _("Missing option `%s' for operation `%s'\n"),
@@ -294,53 +335,11 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   if (NULL != value)
   {
-    switch (type)
-    {
-    case 0:
-      fprintf (stderr, _("Need a record type to interpret value `%s'\n"), value);
-      GNUNET_SCHEDULER_shutdown ();
-      break;
-    case GNUNET_DNSPARSER_TYPE_A:
-      if (1 != inet_pton (AF_INET, value, &value_a))
-      {
-       fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"), 
-                value,
-                typestring);
-       GNUNET_SCHEDULER_shutdown ();
-       return;
-      }
-      data = &value_a;
-      data_size = sizeof (value_a);
-      break;
-    case GNUNET_DNSPARSER_TYPE_NS:
-      data = value;
-      data_size = strlen (value);
-      break;
-    case GNUNET_DNSPARSER_TYPE_CNAME:
-      data = value;
-      data_size = strlen (value);
-      break;
-    case GNUNET_DNSPARSER_TYPE_SOA:
-      // FIXME
-      fprintf (stderr, _("Record type `%s' not implemented yet\n"), typestring);
-      GNUNET_SCHEDULER_shutdown ();
-      return;
-    case GNUNET_DNSPARSER_TYPE_PTR:
-      // FIXME
-      fprintf (stderr, _("Record type `%s' not implemented yet\n"), typestring);
-      GNUNET_SCHEDULER_shutdown ();
-      return;
-    case GNUNET_DNSPARSER_TYPE_MX:
-      // FIXME
-      fprintf (stderr, _("Record type `%s' not implemented yet\n"), typestring);
-      GNUNET_SCHEDULER_shutdown ();
-      return;
-    case GNUNET_DNSPARSER_TYPE_TXT:
-      data = value;
-      data_size = strlen (value);
-      break;
-    case GNUNET_DNSPARSER_TYPE_AAAA:
-      if (1 != inet_pton (AF_INET6, value, &value_aaaa))
+    if (GNUNET_OK !=
+       GNUNET_NAMESTORE_string_to_value (type,
+                                         value,
+                                         &data,
+                                         &data_size))
       {
        fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"), 
                 value,
@@ -348,20 +347,6 @@ run (void *cls, char *const *args, const char *cfgfile,
        GNUNET_SCHEDULER_shutdown ();
        return;
       }
-      data = &value_aaaa;
-      data_size = sizeof (value_aaaa);
-      break;
-    case GNUNET_NAMESTORE_TYPE_PKEY:
-      fprintf (stderr, _("Record type `%s' not implemented yet\n"), typestring);
-      GNUNET_SCHEDULER_shutdown ();
-      return;
-    case GNUNET_NAMESTORE_TYPE_PSEU:
-      data = value;
-      data_size = strlen (value);
-      break;
-    default:
-      GNUNET_assert (0);
-    }
   } else if (add | del)
   {
     fprintf (stderr,
@@ -372,9 +357,24 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   if (NULL != expirationstring)
   {
-    if (GNUNET_OK !=
-       GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
-                                              &etime))
+    if (0 == strcmp (expirationstring, "never"))
+    {
+      etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
+      etime_is_rel = GNUNET_NO;
+    }
+    else if (GNUNET_OK ==
+            GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
+                                                   &etime_rel))
+    {
+      etime_is_rel = GNUNET_YES;
+    }
+    else if (GNUNET_OK == 
+            GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
+                                                   &etime_abs))
+    {
+      etime_is_rel = GNUNET_NO;
+    }
+    else
     {
       fprintf (stderr,
               _("Invalid time format `%s'\n"),
@@ -382,11 +382,12 @@ run (void *cls, char *const *args, const char *cfgfile,
       GNUNET_SCHEDULER_shutdown ();
       return;     
     }
-  } else if (add | del)
+  } 
+  else if (add)
   {
     fprintf (stderr,
             _("Missing option `%s' for operation `%s'\n"),
-            "-e", _("add/del"));
+            "-e", _("add"));
     GNUNET_SCHEDULER_shutdown ();
     return;     
   }
@@ -403,8 +404,25 @@ run (void *cls, char *const *args, const char *cfgfile,
     rd.data = data;
     rd.data_size = data_size;
     rd.record_type = type;
-    rd.expiration = GNUNET_TIME_relative_to_absolute (etime);
-    rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY; // FIXME: not always...
+    if (GNUNET_YES == etime_is_rel)
+    {
+      rd.expiration_time = etime_rel.rel_value;
+      rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
+    }
+    else if (GNUNET_NO == etime_is_rel)
+      rd.expiration_time = etime_abs.abs_value;
+    else
+    {
+      fprintf (stderr,
+              _("No valid expiration time for operation `%s'\n"),
+              _("add"));
+      GNUNET_SCHEDULER_shutdown ();
+      return;
+    }
+    if (1 != nonauthority)
+      rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
+    if (1 != public)
+      rd.flags |= GNUNET_NAMESTORE_RF_PRIVATE;
     add_qe = GNUNET_NAMESTORE_record_create (ns,
                                             zone_pkey,
                                             name,
@@ -425,9 +443,9 @@ run (void *cls, char *const *args, const char *cfgfile,
     rd.data = data;
     rd.data_size = data_size;
     rd.record_type = type;
-    rd.expiration = GNUNET_TIME_relative_to_absolute (etime);
-    rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY; // FIXME: not always...
-    del_qe = GNUNET_NAMESTORE_record_create (ns,
+    rd.expiration_time = 0;
+    rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
+    del_qe = GNUNET_NAMESTORE_record_remove (ns,
                                             zone_pkey,
                                             name,
                                             &rd,
@@ -436,12 +454,22 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   if (list)
   {
+    uint32_t must_not_flags = 0;
+
+    if (1 == nonauthority) /* List non-authority records */
+      must_not_flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
+
+    if (1 == public)
+      must_not_flags |= GNUNET_NAMESTORE_RF_PRIVATE;
+
     list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
                                                     &zone,
-                                                    0, 0,
+                                                    0,
+                                                    must_not_flags,
                                                     &display_record,
                                                     NULL);
   }
+  GNUNET_free_non_null (data);
 }
 
 
@@ -455,10 +483,13 @@ run (void *cls, char *const *args, const char *cfgfile,
 int
 main (int argc, char *const *argv)
 {
+  nonauthority = -1;
+  public = -1;
+
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
     {'a', "add", NULL,
      gettext_noop ("add record"), 0,
-     &GNUNET_GETOPT_set_one, &add},   
+     &GNUNET_GETOPT_set_one, &add},
     {'d', "delete", NULL,
      gettext_noop ("delete record"), 0,
      &GNUNET_GETOPT_set_one, &del},   
@@ -466,7 +497,7 @@ main (int argc, char *const *argv)
      gettext_noop ("display records"), 0,
      &GNUNET_GETOPT_set_one, &list},   
     {'e', "expiration", "TIME",
-     gettext_noop ("expiration time to use (for adding only)"), 1,
+     gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
      &GNUNET_GETOPT_set_string, &expirationstring},   
     {'n', "name", "NAME",
      gettext_noop ("name of the record to add/delete/display"), 1,
@@ -477,6 +508,12 @@ main (int argc, char *const *argv)
     {'V', "value", "VALUE",
      gettext_noop ("value of the record to add/delete"), 1,
      &GNUNET_GETOPT_set_string, &value},   
+    {'p', "public", NULL,
+     gettext_noop ("create or list public record"), 0,
+     &GNUNET_GETOPT_set_one, &public},
+    {'N', "non-authority", NULL,
+     gettext_noop ("create or list non-authority record"), 0,
+     &GNUNET_GETOPT_set_one, &nonauthority},
     {'z', "zonekey", "FILENAME",
      gettext_noop ("filename with the zone key"), 1,
      &GNUNET_GETOPT_set_string, &keyfile},   
@@ -485,6 +522,9 @@ main (int argc, char *const *argv)
 
   int ret;
 
+  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+    return 2;
+
   GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
   ret =
       (GNUNET_OK ==