From 5b9e09a174b6eaec23506f82c6c4adc1f7758122 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 11 Sep 2026 10:43:07 +0100 Subject: [PATCH] Break out code for selecting query ids --- src/adns.make | 2 +- src/internal.h | 9 ++++++++- src/nextid.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/nextid.h | 35 +++++++++++++++++++++++++++++++++++ src/setup.c | 2 +- src/transmit.c | 2 +- 6 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/nextid.c create mode 100644 src/nextid.h diff --git a/src/adns.make b/src/adns.make index 64a42f2..1cdeb73 100644 --- a/src/adns.make +++ b/src/adns.make @@ -17,4 +17,4 @@ # along with this program; if not, write to the Free Software Foundation. LIBOBJS= types.o event.o query.o reply.o general.o setup.o transmit.o \ - parse.o poll.o check.o addrfam.o + parse.o poll.o check.o addrfam.o nextid.o diff --git a/src/internal.h b/src/internal.h index 083f429..c95b9f4 100644 --- a/src/internal.h +++ b/src/internal.h @@ -362,6 +362,13 @@ struct query_queue { adns_query head, tail; }; #define MAXUDP 2 +struct nextid_state { + unsigned fallback; +}; + +extern void adns__nextid_init(struct nextid_state *nextid); +extern int adns__nextid_nextid(adns_state ads); + struct adns__state { adns_initflags iflags; adns_logcallbackfn *logfn; @@ -369,7 +376,7 @@ struct adns__state { int configerrno; struct query_queue udpw, tcpw, childw, output, intdone; adns_query forallnext; - unsigned nextid; + struct nextid_state nextid; int tcpsocket; struct udpsocket { int af; int fd; } udpsockets[MAXUDP]; int nudpsockets; diff --git a/src/nextid.c b/src/nextid.c new file mode 100644 index 0000000..dd7db38 --- /dev/null +++ b/src/nextid.c @@ -0,0 +1,47 @@ +/* + * 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" + +/* stubs */ + +static void good_init(struct nextid_state *state) { } + +static int32_t good_nextid(adns_state ads) { + return -1; +} + +/* entrypoints */ + +void adns__nextid_init(struct nextid_state *state) { + good_init(state); + state->fallback= 0x311f; +} + +int adns__nextid_nextid(adns_state ads) { + int r= good_nextid(ads); + if (r >= 0) return r; + + return (ads->nextid.fallback++) & 0x0ffff; +} + + diff --git a/src/nextid.h b/src/nextid.h new file mode 100644 index 0000000..33d4094 --- /dev/null +++ b/src/nextid.h @@ -0,0 +1,35 @@ +/* + * 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; +} diff --git a/src/setup.c b/src/setup.c index a10cbf8..dd72eac 100644 --- a/src/setup.c +++ b/src/setup.c @@ -627,7 +627,7 @@ static int init_begin(adns_state *ads_r, adns_initflags flags, LIST_INIT(ads->output); LIST_INIT(ads->intdone); ads->forallnext= 0; - ads->nextid= 0x311f; + adns__nextid_init(&ads->nextid); ads->nudpsockets= 0; ads->tcpsocket= -1; adns__vbuf_init(&ads->tcpsend); diff --git a/src/transmit.c b/src/transmit.c index f18f8d8..6ac8917 100644 --- a/src/transmit.c +++ b/src/transmit.c @@ -44,7 +44,7 @@ static adns_status mkquery_header(adns_state ads, vbuf *vb, vb->used= 0; MKQUERY_START(vb); - *id_r= id= (ads->nextid++) & 0x0ffff; + *id_r= id= adns__nextid_nextid(ads); MKQUERY_ADDW(id); MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */ MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */ -- 2.30.2