chiark / gitweb /
Use getentropy() for query ids if possible
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 11 Sep 2026 11:21:26 +0000 (12:21 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 11 Sep 2026 11:48:01 +0000 (12:48 +0100)
src/internal.h
src/nextid.c
src/nextid.h [deleted file]

index c95b9f4adf1305d717097459072856b0aca4d91b..90e63d68dd090c07532b9ac833c61c566560c467 100644 (file)
@@ -364,6 +364,12 @@ struct query_queue { adns_query head, tail; };
 
 struct nextid_state {
   unsigned fallback;
+
+#ifdef HAVE_GETENTROPY
+#define NEXTID_RANDOM_BUF_COUNT 64
+  uint16_t buf[NEXTID_RANDOM_BUF_COUNT];
+  int full;
+#endif /* HAVE_GETENTROPY */
 };
 
 extern void adns__nextid_init(struct nextid_state *nextid);
index dd7db38dcc76ab83f5d07ba490ba2fd38d29743d..d30eabcc9790281c43758eb9938a33cc0ac0ad61 100644 (file)
 
 #include "internal.h"
 
+#ifdef HAVE_GETENTROPY
+
+/* use getentropy */
+
+static void good_init(struct nextid_state *state) {
+  state->full = 0;
+}
+
+static int32_t good_nextid(adns_state ads) {
+  int r;
+
+  if (!ads->nextid.full) {
+    r= getentropy(ads->nextid.buf, sizeof(ads->nextid.buf));
+    if (r) {
+      // XXXX log a warning
+      return -1;
+    }
+    ads->nextid.full= NEXTID_RANDOM_BUF_COUNT;
+  }
+
+  return ads->nextid.buf[--ads->nextid.full];
+}
+
+#else /* ! HAVE_GETENTROPY */
+
 /* stubs */
 
 static void good_init(struct nextid_state *state) { }
 
 static int32_t good_nextid(adns_state ads) {
+  // XXXX log a warning
   return -1;
 }
 
+#endif /* ! HAVE_GETENTROPY */
+
 /* entrypoints */
 
 void adns__nextid_init(struct nextid_state *state) {
diff --git a/src/nextid.h b/src/nextid.h
deleted file mode 100644 (file)
index 33d4094..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * nextid.c
- * - obtain query ids (platform-dependent)
- */
-/*
- *  This file is part of adns, which is Copyright Ian Jackson
- *  and contributors (see the file INSTALL for full details).
- *  
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3, or (at your option)
- *  any later version.
- *  
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *  
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software Foundation.
- */
-
-#include "internal.h"
-
-struct nextid_state {
-  unsigned nextid;
-};
-
-static void adns__nextid_init(struct nextid_state *nextid) {
-  nextid->nextid= 0x311f;
-}
-
-static int adns__nextid_nextid(struct nextid_state *nextid) {
-  return (nextid->nextid++) & 0x0ffff;
-}