From: Ian Jackson Date: Fri, 11 Sep 2026 11:21:26 +0000 (+0100) Subject: Use getentropy() for query ids if possible X-Git-Tag: adns-1.7.0~9 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=65fe06ddac9a912486e023b96075bbc7d50bb76a;p=adns.git Use getentropy() for query ids if possible --- diff --git a/src/internal.h b/src/internal.h index c95b9f4..90e63d6 100644 --- a/src/internal.h +++ b/src/internal.h @@ -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); diff --git a/src/nextid.c b/src/nextid.c index dd7db38..d30eabc 100644 --- a/src/nextid.c +++ b/src/nextid.c @@ -22,14 +22,42 @@ #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 index 33d4094..0000000 --- a/src/nextid.h +++ /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; -}