fix segfault
[oweals/gnunet.git] / src / namestore / gnunet-namestore.c
index a6b45586b434fe2a14a0b7f76262c6c6625483c8..c58d58fd1f50e685fcf702d33231e5b9b25f8e95 100644 (file)
      Boston, MA 02111-1307, USA.
 */
 /**
- * @file gnunet-gns.c
+ * @file gnunet-namestore.c
  * @brief command line tool to manipulate the local zone
  * @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
- * - add option to shorten name (lookup PKEY, then lookup name by zone,
- *   then possibly lookup PSEU for the zone and update our zone)
  */
 #include "platform.h"
 #include <gnunet_util_lib.h>
@@ -105,7 +102,7 @@ static char *typestring;
  * Desired expiration time.
  */
 static char *expirationstring;
-               
+
 
 /**
  * Task run on shutdown.  Cleans up everything.
@@ -193,7 +190,7 @@ del_continuation (void *cls,
  *               or the expiration time of the block in the namestore (even if there are zero
  *               records matching the desired record type)
  * @param name name that is being mapped (at most 255 characters long)
- * @param rd_count number of entries in 'rd' array
+ * @param rd_len number of entries in 'rd' array
  * @param rd array of records with data to store
  * @param signature signature of the record block, NULL if signature is unavailable (i.e. 
  *        because the user queried for a particular record type only)
@@ -207,6 +204,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;
@@ -215,7 +216,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);
 }
 
@@ -234,10 +253,8 @@ 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;
+  void *data = NULL;
+  size_t data_size = 0;
   struct GNUNET_TIME_Relative etime;
   struct GNUNET_NAMESTORE_RecordData rd;
 
@@ -286,7 +303,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"),
@@ -296,53 +314,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,
@@ -350,20 +326,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_GNS_TYPE_PKEY:
-      fprintf (stderr, _("Record type `%s' not implemented yet\n"), typestring);
-      GNUNET_SCHEDULER_shutdown ();
-      return;
-    case GNUNET_GNS_TYPE_PSEU:
-      data = value;
-      data_size = strlen (value);
-      break;
-    default:
-      GNUNET_assert (0);
-    }
   } else if (add | del)
   {
     fprintf (stderr,
@@ -429,7 +391,7 @@ run (void *cls, char *const *args, const char *cfgfile,
     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,
+    del_qe = GNUNET_NAMESTORE_record_remove (ns,
                                             zone_pkey,
                                             name,
                                             &rd,
@@ -444,11 +406,12 @@ run (void *cls, char *const *args, const char *cfgfile,
                                                     &display_record,
                                                     NULL);
   }
+  GNUNET_free_non_null (data);
 }
 
 
 /**
- * The main function for gnunet-gns.
+ * The main function for gnunet-namestore.
  *
  * @param argc number of arguments from the command line
  * @param argv command line arguments
@@ -460,7 +423,7 @@ main (int argc, char *const *argv)
   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},   
@@ -468,7 +431,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)"), 1,
      &GNUNET_GETOPT_set_string, &expirationstring},   
     {'n', "name", "NAME",
      gettext_noop ("name of the record to add/delete/display"), 1,
@@ -487,15 +450,15 @@ main (int argc, char *const *argv)
 
   int ret;
 
-  GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
+  GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
   ret =
       (GNUNET_OK ==
-       GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
-                           _("GNUnet GNS zone manipulation tool"), 
+       GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
+                           _("GNUnet zone manipulation tool"), 
                           options,
                            &run, NULL)) ? 0 : 1;
 
   return ret;
 }
 
-/* end of gnunet-gns.c */
+/* end of gnunet-namestore.c */