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);
#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) {
+++ /dev/null
-/*
- * 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;
-}