GNS: dns2gns now randomizes answers from GNS record set
authorMartin Schanzenbach <mschanzenbach@posteo.de>
Sun, 21 Jun 2020 20:06:35 +0000 (22:06 +0200)
committerMartin Schanzenbach <mschanzenbach@posteo.de>
Sun, 21 Jun 2020 20:06:35 +0000 (22:06 +0200)
src/gns/gnunet-dns2gns.c

index 1e88ef056b6e435a02a0a14efca566833ccd603d..5d8b6f1171d47400c93cb6e5ecaf0d9977f2e834 100644 (file)
@@ -190,6 +190,33 @@ do_shutdown (void *cls)
   }
 }
 
+/**
+ * Shuffle answers
+ * Fisher-Yates (aka Knuth) Shuffle
+ *
+ * @param request context for the request (with answers)
+ */
+static void
+shuffle_answers (struct Request *request)
+{
+  unsigned int idx = request->packet->num_answers;
+  unsigned int r_idx;
+  struct GNUNET_DNSPARSER_Record tmp_answer;
+
+  while (0 != idx)
+  {
+    r_idx = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                      request->packet->num_answers);
+    idx--;
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Swapping %u with %u\n", idx, r_idx);
+    tmp_answer = request->packet->answers[idx];
+    memcpy (&request->packet->answers[idx], &request->packet->answers[r_idx],
+            sizeof (struct GNUNET_DNSPARSER_Record));
+    memcpy (&request->packet->answers[r_idx], &tmp_answer,
+            sizeof (struct GNUNET_DNSPARSER_Record));
+  }
+}
 
 /**
  * Send the response for the given request and clean up.
@@ -203,6 +230,7 @@ send_response (struct Request *request)
   size_t size;
   ssize_t sret;
 
+  shuffle_answers (request);
   if (GNUNET_SYSERR ==
       GNUNET_DNSPARSER_pack (request->packet,
                              UINT16_MAX /* is this not too much? */,
@@ -334,8 +362,6 @@ result_processor (void *cls,
   // packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
   for (uint32_t i = 0; i < rd_count; i++)
   {
-    // FIXME: do we need to hanlde #GNUNET_GNSRECORD_RF_SHADOW_RECORD
-    // here? Or should we do this in libgnunetgns?
     rec.expiration_time.abs_value_us = rd[i].expiration_time;
     switch (rd[i].record_type)
     {