- Act malicious API complete
[oweals/gnunet.git] / src / dht / gnunet-service-dht_datacache.c
index 4d1dd6f68ed37f3018f8391e85188f63ac0d36a5..0e6daa120acdc3ca9bbe1e1bc13ad38d83c7ba7d 100644 (file)
@@ -31,6 +31,8 @@
 #include "gnunet-service-dht_routing.h"
 #include "gnunet-service-dht.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "dht-dtcache",__VA_ARGS__)
+
 
 /**
  * Handle to the datacache service (for inserting/retrieving data)
 static struct GNUNET_DATACACHE_Handle *datacache;
 
 
-/**
- * Entry for inserting data into datacache from the DHT.
- */
-struct DHTPutEntry
-{
-  /**
-   * Size of data.
-   */
-  uint16_t data_size;
-
-  /**
-   * Length of recorded path.
-   */
-  uint16_t path_length;
-
-  /* PATH ENTRIES */
-
-  /* PUT DATA */
-
-};
-
-
 /**
  * Handle a datum we've received from another peer.  Cache if
  * possible.
@@ -74,20 +54,15 @@ struct DHTPutEntry
  */
 void
 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
-                          const GNUNET_HashCode * key,
+                          const struct GNUNET_HashCode * key,
                           unsigned int put_path_length,
                           const struct GNUNET_PeerIdentity *put_path,
                           enum GNUNET_BLOCK_Type type, size_t data_size,
                           const void *data)
 {
-  size_t plen =
-      data_size + put_path_length * sizeof (struct GNUNET_PeerIdentity) +
-      sizeof (struct DHTPutEntry);
-  char buf[plen];
-  struct DHTPutEntry *pe;
-  struct GNUNET_PeerIdentity *pp;
+  int r;
 
-  if (datacache == NULL)
+  if (NULL == datacache)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 _("%s request received, but have no datacache!\n"), "PUT");
@@ -102,14 +77,11 @@ GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
   GNUNET_STATISTICS_update (GDS_stats,
                             gettext_noop ("# ITEMS stored in datacache"), 1,
                             GNUNET_NO);
-  pe = (struct DHTPutEntry *) buf;
-  pe->data_size = htons (data_size);
-  pe->path_length = htons ((uint16_t) put_path_length);
-  pp = (struct GNUNET_PeerIdentity *) &pe[1];
-  memcpy (pp, put_path, put_path_length * sizeof (struct GNUNET_PeerIdentity));
-  memcpy (&pp[put_path_length], data, data_size);
-  (void) GNUNET_DATACACHE_put (datacache, key, plen, (const char *) pe, type,
-                               expiration);
+  r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration,
+                            put_path_length, put_path);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
+       GNUNET_h2s (key), data_size, r, put_path_length);
 }
 
 
@@ -131,7 +103,7 @@ struct GetRequestContext
   /**
    * The key this request was about
    */
-  GNUNET_HashCode key;
+  struct GNUNET_HashCode key;
 
   /**
    * Number of bytes in xquery.
@@ -159,58 +131,44 @@ struct GetRequestContext
  * @param size the size of the data identified by key
  * @param data the actual data
  * @param type the type of the data
- *
+ * @param put_path_length number of peers in 'put_path'
+ * @param put_path path the reply took on put
  * @return GNUNET_OK to continue iteration, anything else
  * to stop iteration.
  */
 static int
-datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
-                        const GNUNET_HashCode * key, size_t size,
-                        const char *data, enum GNUNET_BLOCK_Type type)
+datacache_get_iterator (void *cls,
+                        const struct GNUNET_HashCode * key, size_t size,
+                        const char *data, enum GNUNET_BLOCK_Type type,
+                       struct GNUNET_TIME_Absolute exp,
+                       unsigned int put_path_length,
+                       const struct GNUNET_PeerIdentity *put_path)
 {
   struct GetRequestContext *ctx = cls;
-  const struct DHTPutEntry *pe;
-  const struct GNUNET_PeerIdentity *pp;
-  const char *rdata;
-  size_t rdata_size;
-  uint16_t put_path_length;
   enum GNUNET_BLOCK_EvaluationResult eval;
 
-  pe = (const struct DHTPutEntry *) data;
-  put_path_length = ntohs (pe->path_length);
-  rdata_size = ntohs (pe->data_size);
-
-  if (size !=
-      sizeof (struct DHTPutEntry) + rdata_size +
-      (put_path_length * sizeof (struct GNUNET_PeerIdentity)))
-  {
-    GNUNET_break (0);
-    return GNUNET_OK;
-  }
-  pp = (const struct GNUNET_PeerIdentity *) &pe[1];
-  rdata = (const char *) &pp[put_path_length];
   eval =
       GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf,
                              ctx->reply_bf_mutator, ctx->xquery,
-                             ctx->xquery_size, rdata, rdata_size);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Found reply for query %s in datacache, evaluation result is %d\n",
-              GNUNET_h2s (key), (int) eval);
+                             ctx->xquery_size, data, size);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Found reply for query %s in datacache, evaluation result is %d\n",
+       GNUNET_h2s (key), (int) eval);
   ctx->eval = eval;
   switch (eval)
   {
-  case GNUNET_BLOCK_EVALUATION_OK_LAST:
   case GNUNET_BLOCK_EVALUATION_OK_MORE:
+  case GNUNET_BLOCK_EVALUATION_OK_LAST:
     /* forward to local clients */
     GNUNET_STATISTICS_update (GDS_stats,
                               gettext_noop
                               ("# Good RESULTS found in datacache"), 1,
                               GNUNET_NO);
-    GDS_CLIENTS_handle_reply (exp, key, 0, NULL, put_path_length, pp, type,
-                              rdata_size, rdata);
+    GDS_CLIENTS_handle_reply (exp, key, 0, NULL, put_path_length, put_path,
+                              type, size, data);
     /* forward to other peers */
-    GDS_ROUTING_process (type, exp, key, put_path_length, pp, 0, NULL, rdata,
-                         rdata_size);
+    GDS_ROUTING_process (type, exp, key, put_path_length, put_path, 0, NULL,
+                         data, size);
     break;
   case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
     GNUNET_STATISTICS_update (GDS_stats,
@@ -224,6 +182,12 @@ datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
                               ("# Invalid RESULTS found in datacache"), 1,
                               GNUNET_NO);
     break;
+  case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
+    GNUNET_STATISTICS_update (GDS_stats,
+                              gettext_noop
+                              ("# Irrelevant RESULTS found in datacache"), 1,
+                              GNUNET_NO);
+    break;
   case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
     GNUNET_break (0);
     break;
@@ -255,13 +219,14 @@ datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
  * @return evaluation result for the local replies
  */
 enum GNUNET_BLOCK_EvaluationResult
-GDS_DATACACHE_handle_get (const GNUNET_HashCode * key,
+GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
                           enum GNUNET_BLOCK_Type type, const void *xquery,
                           size_t xquery_size,
                           struct GNUNET_CONTAINER_BloomFilter **reply_bf,
                           uint32_t reply_bf_mutator)
 {
   struct GetRequestContext ctx;
+  unsigned int r;
 
   if (datacache == NULL)
     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
@@ -274,8 +239,11 @@ GDS_DATACACHE_handle_get (const GNUNET_HashCode * key,
   ctx.xquery_size = xquery_size;
   ctx.reply_bf = reply_bf;
   ctx.reply_bf_mutator = reply_bf_mutator;
-  (void) GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
-                               &ctx);
+  r = GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
+                            &ctx);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "DATACACHE GET for key %s completed (%d). %u results found.\n",
+       GNUNET_h2s (key), ctx.eval, r);
   return ctx.eval;
 }