gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r20327 - in gnunet/src: include namestore


From: gnunet
Subject: [GNUnet-SVN] r20327 - in gnunet/src: include namestore
Date: Wed, 7 Mar 2012 10:59:36 +0100

Author: grothoff
Date: 2012-03-07 10:59:36 +0100 (Wed, 07 Mar 2012)
New Revision: 20327

Modified:
   gnunet/src/include/gnunet_namestore_service.h
   gnunet/src/namestore/gnunet-namestore.c
   gnunet/src/namestore/namestore_api.c
   gnunet/src/namestore/namestore_common.c
Log:
-additional namestore conversion APIs added

Modified: gnunet/src/include/gnunet_namestore_service.h
===================================================================
--- gnunet/src/include/gnunet_namestore_service.h       2012-03-07 09:33:59 UTC 
(rev 20326)
+++ gnunet/src/include/gnunet_namestore_service.h       2012-03-07 09:59:36 UTC 
(rev 20327)
@@ -74,16 +74,7 @@
 #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 @@
                                      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

Modified: gnunet/src/namestore/gnunet-namestore.c
===================================================================
--- gnunet/src/namestore/gnunet-namestore.c     2012-03-07 09:33:59 UTC (rev 
20326)
+++ gnunet/src/namestore/gnunet-namestore.c     2012-03-07 09:59:36 UTC (rev 
20327)
@@ -205,6 +205,10 @@
                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 @@
       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 @@
 {
   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,14 +314,11 @@
   }
   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))
+    if (GNUNET_OK !=
+       GNUNET_NAMESTORE_string_to_value (type,
+                                         value,
+                                         &data,
+                                         &data_size))
       {
        fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"), 
                 value,
@@ -309,59 +326,6 @@
        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))
-      {
-       fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"), 
-                value,
-                typestring);
-       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 @@
                                                     &display_record,
                                                     NULL);
   }
+  GNUNET_free_non_null (data);
 }
 
 

Modified: gnunet/src/namestore/namestore_api.c
===================================================================
--- gnunet/src/namestore/namestore_api.c        2012-03-07 09:33:59 UTC (rev 
20326)
+++ gnunet/src/namestore/namestore_api.c        2012-03-07 09:59:36 UTC (rev 
20327)
@@ -204,41 +204,6 @@
 
 
 /**
- * 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.
  *
  * @param h our handle

Modified: gnunet/src/namestore/namestore_common.c
===================================================================
--- gnunet/src/namestore/namestore_common.c     2012-03-07 09:33:59 UTC (rev 
20326)
+++ gnunet/src/namestore/namestore_common.c     2012-03-07 09:59:36 UTC (rev 
20327)
@@ -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 @@
   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 */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]