- fixed string to address parsing
[oweals/gnunet.git] / src / fs / fs_search.c
index b2ccc0e16cc476e0354a110ae9a305f8f416ebda..94a77e13645837b5246932af3b8f4e63acdd4ba7 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2001-2006, 2008-2011 Christian Grothoff (and other contributing authors)
+     (C) 2001-2006, 2008-2012 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -192,7 +192,7 @@ signal_probe_result (struct GNUNET_FS_SearchResult *sr)
   pi.value.search.specifics.update.availability_certainty =
       sr->availability_trials;
   pi.value.search.specifics.update.applicability_rank = sr->optional_support;
-  sr->sc->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
+  sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
   GNUNET_FS_search_start_probe_ (sr);
 }
 
@@ -513,28 +513,32 @@ process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
 
 
 /**
- * Process a keyword-search result.
+ * Decrypt a block using a 'keyword' as the passphrase.  Given the
+ * KSK public key derived from the keyword, this function looks up
+ * the original keyword in the search context and decrypts the
+ * given ciphertext block.
  *
- * @param sc our search context
- * @param kb the kblock
- * @param size size of kb
+ * @param sc search context with the keywords
+ * @param public_key public key to use to lookup the keyword
+ * @param edata encrypted data
+ * @param edata_size number of bytes in 'edata' (and 'data')
+ * @param data where to store the plaintext
+ * @return keyword index on success, GNUNET_SYSERR on error (no such 
+ *        keyword, internal error)
  */
-static void
-process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb,
-                size_t size)
-{
-  unsigned int i;
-  size_t j;
+static int
+decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
+                           const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
+                           const void *edata,
+                           size_t edata_size,
+                           char *data)
+{ 
   GNUNET_HashCode q;
-  char pt[size - sizeof (struct KBlock)];
   struct GNUNET_CRYPTO_AesSessionKey skey;
   struct GNUNET_CRYPTO_AesInitializationVector iv;
-  const char *eos;
-  struct GNUNET_CONTAINER_MetaData *meta;
-  struct GNUNET_FS_Uri *uri;
-  char *emsg;
+  int i;
 
-  GNUNET_CRYPTO_hash (&kb->keyspace,
+  GNUNET_CRYPTO_hash (public_key,
                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
                       &q);
   /* find key */
@@ -545,17 +549,46 @@ process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb,
   {
     /* oops, does not match any of our keywords!? */
     GNUNET_break (0);
-    return;
+    return GNUNET_SYSERR;
   }
   /* decrypt */
   GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
   if (-1 ==
-      GNUNET_CRYPTO_aes_decrypt (&kb[1], size - sizeof (struct KBlock), &skey,
-                                 &iv, pt))
+      GNUNET_CRYPTO_aes_decrypt (edata, edata_size, &skey,
+                                 &iv, data))
   {
     GNUNET_break (0);
-    return;
+    return GNUNET_SYSERR;
   }
+  return i;
+}
+
+
+/**
+ * Process a keyword-search result.
+ *
+ * @param sc our search context
+ * @param kb the kblock
+ * @param size size of kb
+ */
+static void
+process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb,
+                size_t size)
+{
+  size_t j;
+  char pt[size - sizeof (struct KBlock)];
+  const char *eos;
+  struct GNUNET_CONTAINER_MetaData *meta;
+  struct GNUNET_FS_Uri *uri;
+  char *emsg;
+  int i;
+
+  if (-1 == (i = decrypt_block_with_keyword (sc,
+                                            &kb->keyspace,
+                                            &kb[1],
+                                            size - sizeof (struct KBlock),
+                                            pt)))
+    return;
   /* parse */
   eos = memchr (pt, 0, sizeof (pt));
   if (NULL == eos)
@@ -601,39 +634,20 @@ static void
 process_nblock (struct GNUNET_FS_SearchContext *sc, const struct NBlock *nb,
                 size_t size)
 {
-  unsigned int i;
   size_t j;
-  GNUNET_HashCode q;
   char pt[size - sizeof (struct NBlock)];
-  struct GNUNET_CRYPTO_AesSessionKey skey;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
   const char *eos;
   struct GNUNET_CONTAINER_MetaData *meta;
   struct GNUNET_FS_Uri *uri;
   char *uris;
+  int i;
 
-  GNUNET_CRYPTO_hash (&nb->keyspace,
-                      sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
-                      &q);
-  /* find key */
-  for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
-    if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode)))
-      break;
-  if (i == sc->uri->data.ksk.keywordCount)
-  {
-    /* oops, does not match any of our keywords!? */
-    GNUNET_break (0);
+  if (-1 == (i = decrypt_block_with_keyword (sc,
+                                            &nb->keyspace,
+                                            &nb[1],
+                                            size - sizeof (struct NBlock),
+                                            pt)))
     return;
-  }
-  /* decrypt */
-  GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
-  if (-1 ==
-      GNUNET_CRYPTO_aes_decrypt (&nb[1], size - sizeof (struct NBlock), &skey,
-                                 &iv, pt))
-  {
-    GNUNET_break (0);
-    return;
-  }
   /* parse */
   eos = memchr (pt, 0, sizeof (pt));
   if (NULL == eos)
@@ -1442,6 +1456,30 @@ GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
 }
 
 
+/**
+ * Signal stop for the given search result.
+ *
+ * @param cls the global FS handle
+ * @param key the key for the search result (unused)
+ * @param value the search result to free
+ * @return GNUNET_OK
+ */
+static int
+search_result_stop (void *cls, const GNUNET_HashCode * key, void *value)
+{
+  struct GNUNET_FS_SearchContext *sc = cls;
+  struct GNUNET_FS_SearchResult *sr = value;
+  struct GNUNET_FS_ProgressInfo pi;
+
+  pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
+  pi.value.search.specifics.result_stopped.cctx = sr->client_info;
+  pi.value.search.specifics.result_stopped.meta = sr->meta;
+  pi.value.search.specifics.result_stopped.uri = sr->uri;
+  sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
+  return GNUNET_OK;
+}
+
+
 /**
  * Free the given search result.
  *
@@ -1480,11 +1518,6 @@ search_result_free (void *cls, const GNUNET_HashCode * key, void *value)
     GNUNET_FS_search_stop (sr->update_search);
     GNUNET_assert (sr->update_search == NULL);
   }
-  pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
-  pi.value.search.specifics.result_stopped.cctx = sr->client_info;
-  pi.value.search.specifics.result_stopped.meta = sr->meta;
-  pi.value.search.specifics.result_stopped.uri = sr->uri;
-  sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
   GNUNET_break (NULL == sr->client_info);
   GNUNET_free_non_null (sr->serialization);
   GNUNET_FS_uri_destroy (sr->uri);
@@ -1512,6 +1545,8 @@ GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
 
   if (sc->top != NULL)
     GNUNET_FS_end_top (sc->h, sc->top);
+  GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
+                                         &search_result_stop, sc);
   if (sc->psearch_result != NULL)
     sc->psearch_result->update_search = NULL;
   if (sc->serialization != NULL)