You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "He-Pin (via GitHub)" <gi...@apache.org> on 2023/06/07 12:59:14 UTC

[GitHub] [incubator-pekko] He-Pin commented on a diff in pull request #371: Generate random DNS id's

He-Pin commented on code in PR #371:
URL: https://github.com/apache/incubator-pekko/pull/371#discussion_r1221553158


##########
actor/src/main/scala/org/apache/pekko/io/dns/internal/AsyncDnsResolver.scala:
##########
@@ -85,11 +86,25 @@ private[io] final class AsyncDnsResolver(
     settings.SearchDomains,
     settings.NDots)
 
-  private var requestId: Short = 0
+  private var (idIndex, idIncrement) = {
+    val secureRandom = new SecureRandom()
+    (secureRandom.nextLong(), secureRandom.nextLong())
+  }
+
+  private var idCount = 1
 
+  // DNS id's need to be random, see https://datatracker.ietf.org/doc/html/rfc5452#section-4.3.
+  // Uses enhanced double hashing technique from
+  // https://en.wikipedia.org/wiki/Double_hashing#Enhanced_double_hashing
   private def nextId(): Short = {
-    requestId = (requestId + 1).toShort
-    requestId
+    val result = (0xFFFFFFFF & idIndex).asInstanceOf[Short]
+    idIndex -= idIncrement
+    // Incorporate the counter into the increment to create a
+    // tetrahedral number additional term.
+    idIncrement -= {
+      idCount += 1; idCount - 1
+    }
+    result

Review Comment:
   `nextId` can be called concurrently, so I think two thread can get the same result.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org