-additional namestore conversion APIs added
authorChristian Grothoff <christian@grothoff.org>
Wed, 7 Mar 2012 09:59:36 +0000 (09:59 +0000)
committerChristian Grothoff <christian@grothoff.org>
Wed, 7 Mar 2012 09:59:36 +0000 (09:59 +0000)
src/include/gnunet_namestore_service.h
src/namestore/gnunet-namestore.c
src/namestore/namestore_api.c
src/namestore/namestore_common.c

index a50243d093a806a276f852a536ae19f5bcc87f17..f2f314bfc25a0ed871d252700781a228335afeaa 100644 (file)
@@ -74,15 +74,6 @@ struct GNUNET_NAMESTORE_ZoneIterator;
 #define GNUNET_NAMESTORE_MAX_VALUE_SIZE (63 * 1024)
 
 
-/**
- * Convert a type name (i.e. "AAAA") to the corresponding number.
- *
- * @param typename name to convert
- * @return corresponding number, UINT32_MAX on error
- */
-uint32_t
-GNUNET_NAMESTORE_typename_to_number (const char *typename);
-
 
 /**
  * Connect to the namestore service.
@@ -450,6 +441,58 @@ GNUNET_NAMESTORE_records_deserialize (size_t len,
                                      struct GNUNET_NAMESTORE_RecordData *dest);
 
 
+
+/**
+ * Convert the 'value' of a record to a string.
+ *
+ * @param type type of the record
+ * @param data value in binary encoding
+ * @param data_size number of bytes in data
+ * @return NULL on error, otherwise human-readable representation of the value
+ */
+char *
+GNUNET_NAMESTORE_value_to_string (uint32_t type,
+                                 const void *data,
+                                 size_t data_size);
+
+
+/**
+ * Convert human-readable version of a 'value' of a record to the binary
+ * representation.
+ *
+ * @param type type of the record
+ * @param s human-readable string
+ * @param data set to value in binary encoding (will be allocated)
+ * @param data_size set to number of bytes in data
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_NAMESTORE_string_to_value (uint32_t type,
+                                 const char *s,
+                                 void **data,
+                                 size_t *data_size);
+
+
+/**
+ * Convert a type name (i.e. "AAAA") to the corresponding number.
+ *
+ * @param typename name to convert
+ * @return corresponding number, UINT32_MAX on error
+ */
+uint32_t
+GNUNET_NAMESTORE_typename_to_number (const char *typename);
+
+
+/**
+ * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
+ *
+ * @param type number of a type to convert
+ * @return corresponding typestring, NULL on error
+ */
+const char *
+GNUNET_NAMESTORE_number_to_typename (uint32_t type);
+
+
 #if 0                           /* keep Emacsens' auto-indent happy */
 {
 #endif
index a80f880520d6aec3d66b48c70033c6c648dd8f9f..f48d1c23dc96e77e13db0f0210e7cfa42c4386e8 100644 (file)
@@ -205,6 +205,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 +217,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,10 +254,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;
 
@@ -294,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,
@@ -348,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_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,
@@ -442,6 +406,7 @@ run (void *cls, char *const *args, const char *cfgfile,
                                                     &display_record,
                                                     NULL);
   }
+  GNUNET_free_non_null (data);
 }
 
 
index fbf4711aed9963c3c46b9370bd3d7f0561222fde..64db49375e13384c5a9356b0f02d2cc34f381ccc 100644 (file)
@@ -203,41 +203,6 @@ struct GNUNET_NAMESTORE_SimpleRecord
 
 
 
-/**
- * Convert a type name (i.e. "AAAA") to the corresponding number.
- *
- * @param typename name to convert
- * @return corresponding number, UINT32_MAX on error
- */
-uint32_t
-GNUNET_NAMESTORE_typename_to_number (const char *typename)
-{
-  static struct { 
-    const char *name; 
-    uint32_t number; 
-  } map[] = {
-    { "A", GNUNET_DNSPARSER_TYPE_A },
-    { "NS", GNUNET_DNSPARSER_TYPE_NS },
-    { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
-    { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
-    { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
-    { "MX", GNUNET_DNSPARSER_TYPE_MX },
-    { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
-    { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
-    { "PKEY",  GNUNET_NAMESTORE_TYPE_PKEY },
-    { "PSEU",  GNUNET_NAMESTORE_TYPE_PSEU },
-    { NULL, UINT32_MAX }
-  };
-  unsigned int i;
-
-  i=0;
-  while ( (map[i].name != NULL) &&
-         (0 != strcasecmp (typename, map[i].name)) )
-    i++;
-  return map[i].number;  
-}
-
-
 /**
  * Disconnect from service and then reconnect.
  *
index aa4603232d0769c91b0012a1ccd060bab6ecedd9..b865b0b79e63624867a8ff34818fac1eebe51a0d 100644 (file)
@@ -31,6 +31,7 @@
 #include "gnunet_signatures.h"
 #include "gnunet_arm_service.h"
 #include "gnunet_namestore_service.h"
+#include "gnunet_dnsparser_lib.h"
 #include "namestore.h"
 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
 
@@ -251,4 +252,156 @@ GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key
   return sig;
 }
 
-/* end of namestore_api.c */
+
+/**
+ * Convert the 'value' of a record to a string.
+ *
+ * @param type type of the record
+ * @param data value in binary encoding
+ * @param data_size number of bytes in data
+ * @return NULL on error, otherwise human-readable representation of the value
+ */
+char *
+GNUNET_NAMESTORE_value_to_string (uint32_t type,
+                                  const void *data,
+                                  size_t data_size)
+{
+  GNUNET_break (0); // not implemented
+  return NULL;
+}
+
+
+/**
+ * Convert human-readable version of a 'value' of a record to the binary
+ * representation.
+ *
+ * @param type type of the record
+ * @param s human-readable string
+ * @param data set to value in binary encoding (will be allocated)
+ * @param data_size set to number of bytes in data
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_NAMESTORE_string_to_value (uint32_t type,
+                                 const char *s,
+                                 void **data,
+                                 size_t *data_size)
+{
+  struct in_addr value_a;
+  struct in6_addr value_aaaa;
+
+  switch (type)
+  {
+  case 0:
+    return GNUNET_SYSERR;
+    break;
+  case GNUNET_DNSPARSER_TYPE_A:
+    if (1 != inet_pton (AF_INET, s, &value_a))
+      return GNUNET_SYSERR;
+    *data = GNUNET_malloc (sizeof (struct in_addr));
+    memcpy (*data, &value_a, sizeof (value_a));
+    *data_size = sizeof (value_a);
+    break;
+  case GNUNET_DNSPARSER_TYPE_NS:
+    *data = GNUNET_strdup (s);
+    *data_size = strlen (s);
+    break;
+  case GNUNET_DNSPARSER_TYPE_CNAME:
+    *data = GNUNET_strdup (s);
+    *data_size = strlen (s);
+    break;
+  case GNUNET_DNSPARSER_TYPE_SOA:
+    GNUNET_break (0);
+    // FIXME
+    return GNUNET_SYSERR;
+  case GNUNET_DNSPARSER_TYPE_PTR:
+    GNUNET_break (0);
+    // FIXME
+    return GNUNET_SYSERR;
+  case GNUNET_DNSPARSER_TYPE_MX:
+    GNUNET_break (0);
+    // FIXME
+    return GNUNET_SYSERR;
+  case GNUNET_DNSPARSER_TYPE_TXT:
+    *data = GNUNET_strdup (s);
+    *data_size = strlen (s);
+    break;
+  case GNUNET_DNSPARSER_TYPE_AAAA:
+    if (1 != inet_pton (AF_INET6, s, &value_aaaa))    
+      return GNUNET_SYSERR;    
+    *data = GNUNET_malloc (sizeof (struct in6_addr));
+    memcpy (*data, &value_aaaa, sizeof (value_aaaa));
+    break;
+  case GNUNET_NAMESTORE_TYPE_PKEY:
+    GNUNET_break (0);
+    // FIXME
+    return GNUNET_SYSERR;
+  case GNUNET_NAMESTORE_TYPE_PSEU:
+    *data = GNUNET_strdup (s);
+    *data_size = strlen (s);
+    break;
+  default:
+    GNUNET_break (0);
+  }
+  return GNUNET_SYSERR;
+}
+
+
+static struct { 
+  const char *name; 
+  uint32_t number; 
+} name_map[] = {
+  { "A", GNUNET_DNSPARSER_TYPE_A },
+  { "NS", GNUNET_DNSPARSER_TYPE_NS },
+  { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
+  { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
+  { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
+  { "MX", GNUNET_DNSPARSER_TYPE_MX },
+  { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
+  { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
+  { "PKEY",  GNUNET_NAMESTORE_TYPE_PKEY },
+  { "PSEU",  GNUNET_NAMESTORE_TYPE_PSEU },
+  { NULL, UINT32_MAX }
+};
+
+
+/**
+ * Convert a type name (i.e. "AAAA") to the corresponding number.
+ *
+ * @param typename name to convert
+ * @return corresponding number, UINT32_MAX on error
+ */
+uint32_t
+GNUNET_NAMESTORE_typename_to_number (const char *typename)
+{
+  unsigned int i;
+
+  i=0;
+  while ( (name_map[i].name != NULL) &&
+         (0 != strcasecmp (typename, name_map[i].name)) )
+    i++;
+  return name_map[i].number;  
+}
+
+
+/**
+ * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
+ *
+ * @param type number of a type to convert
+ * @return corresponding typestring, NULL on error
+ */
+const char *
+GNUNET_NAMESTORE_number_to_typename (uint32_t type)
+{
+  unsigned int i;
+
+  i=0;
+  while ( (name_map[i].name != NULL) &&
+         (type != name_map[i].number) )
+    i++;
+  return name_map[i].name;  
+}
+
+
+
+/* end of namestore_common.c */