- fix for 2192
[oweals/gnunet.git] / src / namestore / namestore_api.c
index ff2cbae9fcc125cc93e6c4317fa24eda6cf0f5bf..e4dae859bfd3b61159eddeda1cd35bfeb4850b47 100644 (file)
 #include "gnunet_crypto_lib.h"
 #include "gnunet_constants.h"
 #include "gnunet_arm_service.h"
+#include "gnunet_signatures.h"
 #include "gnunet_namestore_service.h"
 #include "namestore.h"
 
-#include "platform.h"
-#include <gcrypt.h>
-#include "gnunet_common.h"
-#include "gnunet_crypto_lib.h"
-#include "gnunet_disk_lib.h"
-
 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
 
 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
@@ -321,29 +316,30 @@ handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
               "RECORD_CREATE_RESPONSE");
 
   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
-  int res = GNUNET_OK;
 
-  if (ntohs (msg->op_result) == GNUNET_OK)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' %i\n",
+              "RECORD_CREATE_RESPONSE", ntohs (msg->op_result));
+  if (ntohs (msg->op_result) == GNUNET_YES)
   {
-    res = GNUNET_OK;
     if (qe->cont != NULL)
     {
-      qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
+      qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore added record successfully"));
     }
 
   }
   else if (ntohs (msg->op_result) == GNUNET_NO)
   {
-    res = GNUNET_SYSERR;
     if (qe->cont != NULL)
     {
-      qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
+      qe->cont (qe->cont_cls, GNUNET_NO, _("Namestore record already existed"));
     }
   }
   else
   {
-    GNUNET_break_op (0);
-    return;
+    if (qe->cont != NULL)
+    {
+      qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record\n"));
+    }
   }
 
   /* Operation done, remove */
@@ -747,23 +743,21 @@ GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
   return h;
 }
 
+struct DisconnectContext
+{
+  struct GNUNET_NAMESTORE_Handle *h;
+  int drop;
+};
 
-/**
- * Disconnect from the namestore service (and free associated
- * resources).
- *
- * @param h handle to the namestore
- * @param drop set to GNUNET_YES to delete all data in namestore (!)
- */
-void
-GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
+static void
+clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct PendingMessage *p;
   struct GNUNET_NAMESTORE_QueueEntry *q;
   struct GNUNET_NAMESTORE_ZoneIterator *z;
-
+  struct GNUNET_NAMESTORE_Handle *h = cls;
   GNUNET_assert (h != NULL);
-
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
   while (NULL != (p = h->pending_head))
   {
     GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
@@ -772,6 +766,7 @@ GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
 
   while (NULL != (q = h->op_head))
   {
+    GNUNET_break (0);
     GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
     GNUNET_free (q);
   }
@@ -794,6 +789,71 @@ GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
   }
   GNUNET_free(h);
   h = NULL;
+};
+
+static size_t
+transmit_disconnect_to_namestore (void *cls, size_t size, void *buf)
+{
+  struct DisconnectContext * d_ctx = cls;
+  struct DisconnectMessage d_msg;
+  struct GNUNET_NAMESTORE_Handle *h = d_ctx->h;
+  int res;
+
+  d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_DISCONNECT);
+  d_msg.header.size = htons (sizeof (struct DisconnectMessage));
+  d_msg.drop = htonl (d_ctx->drop);
+
+  h->th = NULL;
+  if ((size == 0) || (buf == NULL) || (size < sizeof (struct DisconnectMessage)))
+  {
+    GNUNET_break (0);
+    res = 0;
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to service \n", "NAMESTORE_DISCONNECT");
+    memcpy (buf, &d_msg, sizeof (struct DisconnectMessage));
+    res = sizeof (struct DisconnectMessage);
+  }
+
+  GNUNET_SCHEDULER_add_now (&clean_up_task, h);
+  GNUNET_free (d_ctx);
+  return res;
+}
+
+/**
+ * Disconnect from the namestore service (and free associated
+ * resources).
+ *
+ * @param h handle to the namestore
+ * @param drop set to GNUNET_YES to delete all data in namestore (!)
+ */
+void
+GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
+{
+  if (h->th != NULL)
+  {
+    GNUNET_CLIENT_notify_transmit_ready_cancel(h->th);
+    h->th = NULL;
+  }
+  if (h->client != NULL)
+  {
+    struct DisconnectContext *d_ctx = GNUNET_malloc (sizeof (struct DisconnectContext));
+    d_ctx->h = h;
+    d_ctx->drop = drop;
+
+    h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, sizeof (struct DisconnectMessage),
+                                           GNUNET_TIME_UNIT_FOREVER_REL,
+                                           GNUNET_NO, &transmit_disconnect_to_namestore,
+                                           d_ctx);
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not send disconnect notification to namestore service, we are not connected!\n");
+    if (GNUNET_YES == drop)
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NAMESTORE will not drop content\n");
+    GNUNET_SCHEDULER_add_now (&clean_up_task, h);
+  }
 }
 
 
@@ -920,7 +980,36 @@ GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinary
                                   const struct GNUNET_NAMESTORE_RecordData *rd,
                                   const struct GNUNET_CRYPTO_RsaSignature *signature)
 {
-  return GNUNET_SYSERR;
+  int res = GNUNET_SYSERR;
+  size_t rd_ser_len = 0;
+  size_t name_len = 0;
+  char * name_tmp;
+  char * rd_tmp;
+  struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
+
+  GNUNET_assert (public_key != NULL);
+  GNUNET_assert (name != NULL);
+  GNUNET_assert (signature != NULL);
+
+  rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
+  char rd_ser[rd_ser_len];
+  GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
+
+  name_len = strlen (name) + 1;
+
+  sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
+  sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
+  sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
+  name_tmp = (char *) &sig_purpose[1];
+  rd_tmp = &name_tmp[name_len];
+  memcpy (name_tmp, name, name_len);
+  memcpy (rd_tmp, rd_ser, rd_ser_len);
+
+  res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
+
+  GNUNET_free (sig_purpose);
+
+  return res;
 }
 
 /**
@@ -1096,6 +1185,8 @@ GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
   memcpy (name_tmp, name, name_len);
   memcpy (rd_tmp, rd_ser, rd_ser_len);
 
+  GNUNET_free (pkey_enc);
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
 
   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);