chiark / gitweb /
Break out code for selecting query ids
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 11 Sep 2026 09:43:07 +0000 (10:43 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 11 Sep 2026 11:48:01 +0000 (12:48 +0100)
src/adns.make
src/internal.h
src/nextid.c [new file with mode: 0644]
src/nextid.h [new file with mode: 0644]
src/setup.c
src/transmit.c

index 64a42f2dc433d8d8df5d5bd63997c9bb763425c8..1cdeb730c621a552aec332453c195b2d525913df 100644 (file)
@@ -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
index 083f429f4adbdb80dd24e4b2750d5f2651d34322..c95b9f4adf1305d717097459072856b0aca4d91b 100644 (file)
@@ -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 (file)
index 0000000..dd7db38
--- /dev/null
@@ -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 (file)
index 0000000..33d4094
--- /dev/null
@@ -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;
+}
index a10cbf8087f03bbe4e00a53c18d689d85bf02d18..dd72eac22c7855ce04fceb3de1de0063eb6fe153 100644 (file)
@@ -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);
index f18f8d8fbdac4e34339d5c7951bb02d770b56aa3..6ac8917bcd61ea3c7e5e81a1d2ea983097605c7c 100644 (file)
@@ -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) */