summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.in50
-rw-r--r--src/addrfam.c646
-rw-r--r--src/adns.h1067
-rw-r--r--src/adns.make24
-rw-r--r--src/check.c224
-rw-r--r--src/config.h.in98
-rw-r--r--src/dlist.h53
-rw-r--r--src/event.c732
-rw-r--r--src/general.c384
-rw-r--r--src/internal.h947
-rw-r--r--src/parse.c261
-rw-r--r--src/poll.c130
-rw-r--r--src/query.c677
-rw-r--r--src/reply.c390
-rw-r--r--src/setup.c843
-rw-r--r--src/transmit.c289
-rw-r--r--src/tvarith.h40
-rw-r--r--src/types.c1712
18 files changed, 8567 insertions, 0 deletions
diff --git a/src/Makefile.in b/src/Makefile.in
new file mode 100644
index 0000000..6557f59
--- /dev/null
+++ b/src/Makefile.in
@@ -0,0 +1,50 @@
+# src/Makefile[.in] - library main Makefile
+#
+# This file is part of adns, which is
+# Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+# Copyright (C) 2014 Mark Wooding
+# Copyright (C) 1999-2000,2003,2006 Tony Finch
+# Copyright (C) 1991 Massachusetts Institute of Technology
+# (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.
+
+srcdir= @srcdir@
+VPATH= @srcdir@
+
+TARGETS= libadns.a
+include ../settings.make
+include $(srcdir)/adns.make
+
+DIRCFLAGS= -I. -I$(srcdir)
+
+install:
+ mkdir -p $(libdir) $(includedir)
+ set -xe; for f in $(TARGETS); \
+ do $(INSTALL_DATA) $$f $(libdir)/$$f; done
+ $(INSTALL_DATA) $(srcdir)/../src/adns.h $(includedir)/adns.h
+
+uninstall:
+ for f in $(TARGETS); do rm -f $(libdir)/$$f; done
+ rm -f $(includedir)/adns.h
+
+ALLOBJS= $(LIBOBJS)
+
+libadns.a: $(LIBOBJS)
+ rm -f $@
+ $(AR) cqv $@.new $(LIBOBJS)
+ $(RANLIB) $@.new
+ mv -f $@.new $@
+
+$(LIBOBJS): adns.h internal.h config.h
diff --git a/src/addrfam.c b/src/addrfam.c
new file mode 100644
index 0000000..5067048
--- /dev/null
+++ b/src/addrfam.c
@@ -0,0 +1,646 @@
+/*
+ * addrfam.c
+ * - address-family specific code
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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 <stdlib.h>
+#include <errno.h>
+#include <limits.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <stddef.h>
+#include <stdbool.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <net/if.h>
+
+#include "internal.h"
+
+/*
+ * General address-family operations.
+ */
+
+#define SIN(cnst, sa) ((void)(sa)->sa_family, (cnst struct sockaddr_in *)(sa))
+#define SIN6(cnst, sa) ((void)(sa)->sa_family, (cnst struct sockaddr_in6 *)(sa))
+
+static void unknown_af(int af) NONRETURNING;
+static void unknown_af(int af) {
+ fprintf(stderr, "ADNS INTERNAL: unknown address family %d\n", af);
+ abort();
+}
+
+/*
+ * SOCKADDR_IN_IN6(CNST, struct sockaddr *sa, SIN, {
+ * // struct sockaddr_in *const SIN; // implicitly
+ * code for inet;
+ * }, {
+ * // struct sockaddr_in6 *const SIN6; // implicitly
+ * code for inet6;
+ * })
+ *
+ * SOCKADDR_IN_IN6_PAIR(CNST, struct sockaddr *sa, SINA,
+ * struct sockaddr *sb, SINB, {
+ * // struct sockaddr_in *const SINA; // implicitly
+ * // struct sockaddr_in *const SINB; // implicitly
+ * code for inet;
+ * },{
+ * // struct sockaddr_in6 *const SINA6; // implicitly
+ * // struct sockaddr_in6 *const SINB6; // implicitly
+ * code for inet6;
+ * });
+ *
+ * SOCKADDR_IN_IN6_OTHER(CNST, struct sockaddr *sa, SIN, { in }, { in6 }, {
+ * code for other address family
+ * })
+ *
+ * AF_IN_IN6_OTHER(af, { in }, { in6 }, { other })
+ *
+ * Executes the first or second block according to the AF in sa. CNST
+ * may be `const' or empty. For _PAIR, sa and sb must be same AF.
+ *
+ * All except _OTHER handle unknown AFs with unknown_af.
+ *
+ * Code blocks may not contain , outside parens.
+ */
+#define AF_IN_IN6_OTHER(af, for_inet, for_inet6, other) \
+ if ((af) == AF_INET) { \
+ for_inet \
+ } else if ((af) == AF_INET6) { \
+ for_inet6 \
+ } else { \
+ other \
+ }
+#define SOCKADDR_IN_IN6_OTHER(cnst, sa, sin, for_inet, for_inet6, other) \
+ AF_IN_IN6_OTHER((sa)->sa_family, { \
+ cnst struct sockaddr_in *const sin = SIN(cnst,(sa)); \
+ for_inet \
+ }, { \
+ cnst struct sockaddr_in6 *const sin##6 = SIN6(cnst,(sa)); \
+ for_inet6 \
+ }, \
+ other \
+ )
+#define SOCKADDR_IN_IN6(cnst, sa, sin, for_inet, for_inet6) \
+ SOCKADDR_IN_IN6_OTHER(cnst, sa, sin, for_inet, for_inet6, { \
+ unknown_af((sa)->sa_family); \
+ })
+#define SOCKADDR_IN_IN6_PAIR(cnst, sa, sina, sb, sinb, for_inet, for_inet6) \
+ do{ \
+ assert((sa)->sa_family == (sb)->sa_family); \
+ SOCKADDR_IN_IN6(cnst, sa, sina, { \
+ cnst struct sockaddr_in *const sinb = SIN(cnst,(sb)); \
+ for_inet \
+ }, { \
+ cnst struct sockaddr_in6 *const sinb##6 = SIN6(cnst,(sb)); \
+ for_inet6 \
+ }); \
+ }while(0)
+
+int adns__addrs_equal_raw(const struct sockaddr *a,
+ int bf, const void *b) {
+ if (a->sa_family != bf) return 0;
+
+ SOCKADDR_IN_IN6(const, a, sin, {
+ return sin->sin_addr.s_addr == ((const struct in_addr*)b)->s_addr;
+ }, {
+ return !memcmp(&sin6->sin6_addr, b, sizeof(struct in6_addr));
+ });
+}
+
+int adns__addrs_equal(const adns_sockaddr *a, const adns_sockaddr *b) {
+ return adns__addrs_equal_raw(&a->sa, b->sa.sa_family,
+ adns__sockaddr_addr(&b->sa));
+}
+
+int adns__sockaddrs_equal(const struct sockaddr *sa,
+ const struct sockaddr *sb) {
+ if (!adns__addrs_equal_raw(sa, sb->sa_family, adns__sockaddr_addr(sb)))
+ return 0;
+ SOCKADDR_IN_IN6_PAIR(const, sa, sina, sb, sinb, {
+ return sina->sin_port == sinb->sin_port;
+ }, {
+ return sina6->sin6_port == sinb6->sin6_port &&
+ sina6->sin6_scope_id == sinb6->sin6_scope_id;
+ });
+}
+
+int adns__addr_width(int af) {
+ AF_IN_IN6_OTHER(af, {
+ return 32;
+ }, {
+ return 128;
+ }, {
+ unknown_af(af);
+ });
+}
+
+void adns__prefix_mask(adns_sockaddr *sa, int len) {
+ SOCKADDR_IN_IN6(, &sa->sa, sin, {
+ assert(len <= 32);
+ sin->sin_addr.s_addr= htonl(!len ? 0 : 0xffffffff << (32-len));
+ }, {
+ int i= len/8;
+ int j= len%8;
+ unsigned char *m= sin6->sin6_addr.s6_addr;
+ assert(len <= 128);
+ memset(m, 0xff, i);
+ if (j) m[i++]= (0xff << (8-j)) & 0xff;
+ memset(m+i, 0, 16-i);
+ });
+}
+
+int adns__guess_prefix_length(const adns_sockaddr *sa) {
+ SOCKADDR_IN_IN6(const, &sa->sa, sin, {
+ unsigned a= (ntohl(sin->sin_addr.s_addr) >> 24) & 0xff;
+ if (a < 128) return 8;
+ else if (a < 192) return 16;
+ else if (a < 224) return 24;
+ else return -1;
+ }, {
+ (void)sin6;
+ return 64;
+ });
+}
+
+int adns__addr_matches(int af, const void *addr,
+ const adns_sockaddr *base, const adns_sockaddr *mask)
+{
+ if (af != base->sa.sa_family) return 0;
+ SOCKADDR_IN_IN6_PAIR(const, &base->sa, sbase, &mask->sa, smask, {
+ const struct in_addr *v4 = addr;
+ return (v4->s_addr & smask->sin_addr.s_addr)
+ == sbase->sin_addr.s_addr;
+ }, {
+ int i;
+ const char *a= addr;
+ const char *b= sbase6->sin6_addr.s6_addr;
+ const char *m= smask6->sin6_addr.s6_addr;
+ for (i = 0; i < 16; i++)
+ if ((a[i] & m[i]) != b[i]) return 0;
+ return 1;
+ });
+}
+
+const void *adns__sockaddr_addr(const struct sockaddr *sa) {
+ SOCKADDR_IN_IN6(const, sa, sin, {
+ return &sin->sin_addr;
+ }, {
+ return &sin6->sin6_addr;
+ });
+}
+
+void adns__addr_inject(const void *a, adns_sockaddr *sa) {
+ SOCKADDR_IN_IN6( , &sa->sa, sin, {
+ memcpy(&sin->sin_addr, a, sizeof(sin->sin_addr));
+ }, {
+ memcpy(&sin6->sin6_addr, a, sizeof(sin6->sin6_addr));
+ });
+}
+
+/*
+ * addr2text and text2addr
+ */
+
+#define ADDRFAM_DEBUG
+#ifdef ADDRFAM_DEBUG
+static void af_debug_func(const char *fmt, ...) {
+ int esave= errno;
+ va_list al;
+ va_start(al,fmt);
+ vfprintf(stderr,fmt,al);
+ va_end(al);
+ errno= esave;
+}
+# define af_debug(fmt,...) \
+ (af_debug_func("%s: " fmt "\n", __func__, __VA_ARGS__))
+#else
+# define af_debug(fmt,...) ((void)("" fmt "", __VA_ARGS__))
+#endif
+
+static bool addrtext_our_errno(int e) {
+ return
+ e==EAFNOSUPPORT ||
+ e==EINVAL ||
+ e==ENOSPC ||
+ e==ENOSYS;
+}
+
+static bool addrtext_scope_use_ifname(const struct sockaddr *sa) {
+ const struct in6_addr *in6= &SIN6(const,sa)->sin6_addr;
+ return
+ IN6_IS_ADDR_LINKLOCAL(in6) ||
+ IN6_IS_ADDR_MC_LINKLOCAL(in6);
+}
+
+static int textaddr_check_qf(adns_queryflags flags) {
+ if (flags & ~(adns_queryflags)(adns_qf_addrlit_scope_forbid|
+ adns_qf_addrlit_scope_numeric|
+ adns_qf_addrlit_ipv4_quadonly|
+ 0x40000000))
+ return ENOSYS;
+ return 0;
+}
+
+int adns_text2addr(const char *text, uint16_t port, adns_queryflags flags,
+ struct sockaddr *sa, socklen_t *salen_io) {
+ int r, af;
+ char copybuf[INET6_ADDRSTRLEN];
+ const char *parse=text;
+ const char *scopestr=0;
+ socklen_t needlen;
+ void *dst;
+ uint16_t *portp;
+
+ r= textaddr_check_qf(flags); if (r) return r;
+
+#define INVAL(how) do{ \
+ af_debug("invalid: %s: `%s'", how, text); \
+ return EINVAL; \
+}while(0)
+
+#define AFCORE(INETx,SINx,sinx) \
+ af= AF_##INETx; \
+ dst = &SINx(,sa)->sinx##_addr; \
+ portp = &SINx(,sa)->sinx##_port; \
+ needlen= sizeof(*SINx(,sa));
+
+ if (!strchr(text, ':')) { /* INET */
+
+ AFCORE(INET,SIN,sin);
+
+ } else { /* INET6 */
+
+ AFCORE(INET6,SIN6,sin6);
+
+ const char *percent= strchr(text, '%');
+ if (percent) {
+ ptrdiff_t lhslen = percent - text;
+ if (lhslen >= INET6_ADDRSTRLEN) INVAL("scoped addr lhs too long");
+ memcpy(copybuf, text, lhslen);
+ copybuf[lhslen]= 0;
+
+ parse= copybuf;
+ scopestr= percent+1;
+
+ af_debug("will parse scoped addr `%s' %% `%s'", parse, scopestr);
+ }
+
+ }
+
+#undef AFCORE
+
+ if (scopestr && (flags & adns_qf_addrlit_scope_forbid))
+ INVAL("scoped addr but _scope_forbid");
+
+ if (*salen_io < needlen) {
+ *salen_io = needlen;
+ return ENOSPC;
+ }
+
+ memset(sa, 0, needlen);
+
+ sa->sa_family= af;
+ *portp = htons(port);
+
+ if (af == AF_INET && !(flags & adns_qf_addrlit_ipv4_quadonly)) {
+ /* we have to use inet_aton to deal with non-dotted-quad literals */
+ int r= inet_aton(parse,&SIN(,sa)->sin_addr);
+ if (!r) INVAL("inet_aton rejected");
+ } else {
+ int r= inet_pton(af,parse,dst);
+ if (!r) INVAL("inet_pton rejected");
+ assert(r>0);
+ }
+
+ if (scopestr) {
+ errno=0;
+ char *ep;
+ unsigned long scope= strtoul(scopestr,&ep,10);
+ if (errno==ERANGE) INVAL("numeric scope id too large for unsigned long");
+ assert(!errno);
+ if (!*ep) {
+ if (scope > ~(uint32_t)0)
+ INVAL("numeric scope id too large for uint32_t");
+ } else { /* !!*ep */
+ if (flags & adns_qf_addrlit_scope_numeric)
+ INVAL("non-numeric scope but _scope_numeric");
+ if (!addrtext_scope_use_ifname(sa)) {
+ af_debug("cannot convert non-numeric scope"
+ " in non-link-local addr `%s'", text);
+ return ENOSYS;
+ }
+ errno= 0;
+ scope= if_nametoindex(scopestr);
+ if (!scope) {
+ /* RFC3493 says "No errors are defined". It's not clear
+ * whether that is supposed to mean if_nametoindex "can't
+ * fail" (other than by the supplied name not being that of an
+ * interface) which seems unrealistic, or that it conflates
+ * all its errors together by failing to set errno, or simply
+ * that they didn't bother to document the errors.
+ *
+ * glibc, FreeBSD and OpenBSD all set errno (to ENXIO when
+ * appropriate). See Debian bug #749349.
+ *
+ * We attempt to deal with this by clearing errno to start
+ * with, and then perhaps mapping the results. */
+ af_debug("if_nametoindex rejected scope name (errno=%s)",
+ strerror(errno));
+ if (errno==0) {
+ return ENXIO;
+ } else if (addrtext_our_errno(errno)) {
+ /* we use these for other purposes, urgh. */
+ perror("adns: adns_text2addr: if_nametoindex"
+ " failed with unexpected error");
+ return EIO;
+ } else {
+ return errno;
+ }
+ } else { /* ix>0 */
+ if (scope > ~(uint32_t)0) {
+ fprintf(stderr,"adns: adns_text2addr: if_nametoindex"
+ " returned an interface index >=2^32 which will not fit"
+ " in sockaddr_in6.sin6_scope_id");
+ return EIO;
+ }
+ }
+ } /* else; !!*ep */
+
+ SIN6(,sa)->sin6_scope_id= scope;
+ } /* if (scopestr) */
+
+ *salen_io = needlen;
+ return 0;
+}
+
+int adns_addr2text(const struct sockaddr *sa, adns_queryflags flags,
+ char *buffer, int *buflen_io, int *port_r) {
+ const void *src;
+ int r, port;
+
+ r= textaddr_check_qf(flags); if (r) return r;
+
+ if (*buflen_io < ADNS_ADDR2TEXT_BUFLEN) {
+ *buflen_io = ADNS_ADDR2TEXT_BUFLEN;
+ return ENOSPC;
+ }
+
+ SOCKADDR_IN_IN6_OTHER(const, sa, sin, {
+ src= &sin->sin_addr; port= sin->sin_port;
+ }, {
+ src= &sin6->sin6_addr; port= sin6->sin6_port;
+ }, {
+ return EAFNOSUPPORT;
+ });
+
+ const char *ok= inet_ntop(sa->sa_family, src, buffer, *buflen_io);
+ assert(ok);
+
+ if (sa->sa_family == AF_INET6) {
+ uint32_t scope = SIN6(const,sa)->sin6_scope_id;
+ if (scope) {
+ if (flags & adns_qf_addrlit_scope_forbid)
+ return EINVAL;
+ int scopeoffset = strlen(buffer);
+ int remain = *buflen_io - scopeoffset;
+ char *scopeptr = buffer + scopeoffset;
+ assert(remain >= IF_NAMESIZE+1/*%*/);
+ *scopeptr++= '%'; remain--;
+ bool parsedname = 0;
+ af_debug("will print scoped addr `%.*s' %% %"PRIu32"",
+ scopeoffset,buffer, scope);
+ if (scope <= UINT_MAX /* so we can pass it to if_indextoname */
+ && !(flags & adns_qf_addrlit_scope_numeric)
+ && addrtext_scope_use_ifname(sa)) {
+ parsedname = if_indextoname(scope, scopeptr);
+ if (!parsedname) {
+ af_debug("if_indextoname rejected scope (errno=%s)",
+ strerror(errno));
+ if (errno==ENXIO) {
+ /* fair enough, show it as a number then */
+ } else if (addrtext_our_errno(errno)) {
+ /* we use these for other purposes, urgh. */
+ perror("adns: adns_addr2text: if_indextoname"
+ " failed with unexpected error");
+ return EIO;
+ } else {
+ return errno;
+ }
+ }
+ }
+ if (!parsedname) {
+ int r = snprintf(scopeptr, remain,
+ "%"PRIu32"", scope);
+ assert(r < *buflen_io - scopeoffset);
+ }
+ af_debug("printed scoped addr `%s'", buffer);
+ }
+ }
+
+ if (port_r) *port_r= ntohs(port);
+ return 0;
+}
+
+char *adns__sockaddr_ntoa(const struct sockaddr *sa, char *buf) {
+ int err;
+ int len= ADNS_ADDR2TEXT_BUFLEN;
+
+ err= adns_addr2text(sa, 0, buf, &len, 0);
+ if (err == EIO)
+ err= adns_addr2text(sa, adns_qf_addrlit_scope_numeric, buf, &len, 0);
+ assert(!err);
+ return buf;
+}
+
+/*
+ * Reverse-domain parsing and construction.
+ */
+
+int adns__make_reverse_domain(const struct sockaddr *sa, const char *zone,
+ char **buf_io, size_t bufsz,
+ char **buf_free_r) {
+ size_t req;
+ char *p;
+ unsigned c, y;
+ unsigned long aa;
+ const unsigned char *ap;
+ int i, j;
+
+ AF_IN_IN6_OTHER(sa->sa_family, {
+ req= 4 * 4;
+ if (!zone) zone= "in-addr.arpa";
+ }, {
+ req = 2 * 32;
+ if (!zone) zone= "ip6.arpa";
+ }, {
+ return ENOSYS;
+ });
+
+ req += strlen(zone) + 1;
+ if (req <= bufsz)
+ p= *buf_io;
+ else {
+ p= malloc(req); if (!p) return errno;
+ *buf_free_r = p;
+ }
+
+ *buf_io= p;
+ SOCKADDR_IN_IN6(const, sa, sin, {
+ aa= ntohl(sin->sin_addr.s_addr);
+ for (i=0; i<4; i++) {
+ p += sprintf(p, "%d", (int)(aa & 0xff));
+ *p++= '.';
+ aa >>= 8;
+ }
+ }, {
+ ap= sin6->sin6_addr.s6_addr + 16;
+ for (i=0; i<16; i++) {
+ c= *--ap;
+ for (j=0; j<2; j++) {
+ y= c & 0xf;
+ *p++= (y < 10) ? y + '0' : y - 10 + 'a';
+ c >>= 4;
+ *p++= '.';
+ }
+ }
+ });
+
+ strcpy(p, zone);
+ return 0;
+}
+
+
+#define REVPARSE_P_L(labnum) \
+ const char *p= dgram + rps->labstart[labnum]; \
+ int l= rps->lablen[labnum]
+ /*
+ * REVPARSE_P_L(int labnum);
+ * expects:
+ * const char *dgram;
+ * const struct revparse_state *rps;
+ * produces:
+ * const char *p; // start of label labnum in dgram
+ * int l; // length of label in dgram
+ */
+
+static bool revparse_check_tail(struct revparse_state *rps,
+ const char *dgram, int nlabels,
+ int bodylen, const char *inarpa) {
+ int i;
+
+ if (nlabels != bodylen+2) return 0;
+ for (i=0; i<2; i++) {
+ REVPARSE_P_L(bodylen+i);
+ const char *want= !i ? inarpa : "arpa";
+ if (!adns__labels_equal(p,l, want,strlen(want))) return 0;
+ }
+ return 1;
+}
+
+static bool revparse_atoi(const char *p, int l, int base,
+ unsigned max, unsigned *v_r) {
+ if (l>3) return 0;
+ if (l>1 && p[0]=='0') return 0;
+ unsigned v=0;
+ while (l-- > 0) {
+ int tv;
+ int c= ctype_toupper(*p++);
+ if ('0'<=c && c<='9') tv = c-'0';
+ else if ('A'<=c && c<='Z') tv = c-'A'+10;
+ else return 0;
+ if (tv >= base) return 0;
+ v *= base;
+ v += tv;
+ }
+ if (v>max) return 0;
+ *v_r= v;
+ return 1;
+}
+
+static bool revparse_inet(struct revparse_state *rps,
+ const char *dgram, int nlabels,
+ adns_rrtype *rrtype_r, adns_sockaddr *addr_r) {
+ if (!revparse_check_tail(rps,dgram,nlabels,4,"in-addr")) return 0;
+
+ uint32_t a=0;
+ int i;
+ for (i=3; i>=0; i--) {
+ REVPARSE_P_L(i);
+ unsigned v;
+ if (!revparse_atoi(p,l,10,255,&v)) return 0;
+ a <<= 8;
+ a |= v;
+ }
+ *rrtype_r= adns_r_a;
+ addr_r->inet.sin_family= AF_INET;
+ addr_r->inet.sin_addr.s_addr= htonl(a);
+ return 1;
+}
+
+static bool revparse_inet6(struct revparse_state *rps,
+ const char *dgram, int nlabels,
+ adns_rrtype *rrtype_r, adns_sockaddr *addr_r) {
+ if (!revparse_check_tail(rps,dgram,nlabels,32,"ip6")) return 0;
+
+ int i, j;
+ memset(addr_r,0,sizeof(*addr_r));
+ unsigned char *a= addr_r->inet6.sin6_addr.s6_addr+16;
+ for (i=0; i<32; ) { /* i incremented in inner loop */
+ unsigned b=0;
+ for (j=0; j<2; j++, i++) {
+ REVPARSE_P_L(i);
+ unsigned v;
+ if (!revparse_atoi(p,l,16,15,&v)) return 0;
+ b >>= 4;
+ b |= v << 4;
+ }
+ *--a= b;
+ }
+ *rrtype_r= adns_r_aaaa;
+ addr_r->inet.sin_family= AF_INET6;
+ return 1;
+}
+
+bool adns__revparse_label(struct revparse_state *rps, int labnum,
+ const char *dgram, int labstart, int lablen) {
+ if (labnum >= MAXREVLABELS)
+ return 0;
+
+ assert(labstart <= 65535);
+ assert(lablen <= 255);
+ rps->labstart[labnum] = labstart;
+ rps->lablen[labnum] = lablen;
+ return 1;
+}
+
+bool adns__revparse_done(struct revparse_state *rps,
+ const char *dgram, int nlabels,
+ adns_rrtype *rrtype_r, adns_sockaddr *addr_r) {
+ return
+ revparse_inet(rps,dgram,nlabels,rrtype_r,addr_r) ||
+ revparse_inet6(rps,dgram,nlabels,rrtype_r,addr_r);
+}
diff --git a/src/adns.h b/src/adns.h
new file mode 100644
index 0000000..a6599f6
--- /dev/null
+++ b/src/adns.h
@@ -0,0 +1,1067 @@
+/*
+ * adns.h
+ * - adns user-visible API
+ */
+/*
+ *
+ * This file is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ *
+ * It is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ *
+ * 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.
+ *
+ *
+ * For the benefit of certain LGPL'd `omnibus' software which
+ * provides a uniform interface to various things including adns, I
+ * make the following additional licence. I do this because the GPL
+ * would otherwise force either the omnibus software to be GPL'd or
+ * the adns-using part to be distributed separately.
+ *
+ * So: you may also redistribute and/or modify adns.h (but only the
+ * public header file adns.h and not any other part of adns) under the
+ * terms of the GNU Library General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * Note that adns itself is GPL'd. Authors of adns-using applications
+ * with GPL-incompatible licences, and people who distribute adns with
+ * applications where the whole distribution is not GPL'd, are still
+ * likely to be in violation of the GPL. Anyone who wants to do this
+ * should contact Ian Jackson. Please note that to avoid encouraging
+ * people to infringe the GPL as it applies to the body of adns, Ian
+ * thinks that if you take advantage of the special exception to
+ * redistribute just adns.h under the LGPL, you should retain this
+ * paragraph in its place in the appropriate copyright statements.
+ *
+ *
+ * You should have received a copy of the GNU General Public License,
+ * or the GNU Library General Public License, as appropriate, along
+ * with this program; if not, write to the Free Software Foundation.
+ *
+ */
+
+#ifndef ADNS_H_INCLUDED
+#define ADNS_H_INCLUDED
+
+#include <stdio.h>
+#include <stdarg.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <net/if.h>
+
+#ifdef __cplusplus
+extern "C" { /* I really dislike this - iwj. */
+#endif
+
+/* Whether to support address families other than IPv4 in responses which use
+ * the `adns_rr_addr' structure. This is a source-compatibility issue: old
+ * clients may not expect to find address families other than AF_INET in
+ * their query results. There's a separate binary compatibility issue to do
+ * with the size of the `adns_rr_addr' structure, but we'll assume you can
+ * cope with that because you have this header file. Define
+ * `ADNS_FEATURE_IPV4ONLY' if you only want to see AF_INET addresses by
+ * default, or `ADNS_FEATURE_MANYAF' to allow multiple address families; the
+ * default is currently to stick with AF_INET only, but this is likely to
+ * change in a later release of ADNS. Note that any adns_qf_want_... flags
+ * in your query are observed: this setting affects only the default address
+ * families.
+ */
+#if !defined(ADNS_FEATURE_IPV4ONLY) && !defined(ADNS_FEATURE_MANYAF)
+# define ADNS_FEATURE_IPV4ONLY
+#elif defined(ADNS_FEATURE_IPV4ONLY) && defined(ADNS_FEATURE_MANYAF)
+# error "Feature flags ADNS_FEATURE_IPV4ONLY and ..._MANYAF are incompatible"
+#endif
+
+/* All struct in_addr anywhere in adns are in NETWORK byte order. */
+
+typedef struct adns__state *adns_state;
+typedef struct adns__query *adns_query;
+
+typedef enum { /* In general, or together the desired flags: */
+ adns_if_none= 0x0000,/* no flags. nicer than 0 for some compilers */
+ adns_if_noenv= 0x0001,/* do not look at environment */
+ adns_if_noerrprint= 0x0002,/* never print to stderr (_debug overrides) */
+ adns_if_noserverwarn=0x0004,/* do not warn to stderr about duff servers etc */
+ adns_if_debug= 0x0008,/* enable all output to stderr plus debug msgs */
+ adns_if_logpid= 0x0080,/* include pid in diagnostic output */
+ adns_if_noautosys= 0x0010,/* do not make syscalls at every opportunity */
+ adns_if_eintr= 0x0020,/* allow _wait and _synchronous to return EINTR */
+ adns_if_nosigpipe= 0x0040,/* applic has SIGPIPE ignored, do not protect */
+ adns_if_checkc_entex=0x0100,/* consistency checks on entry/exit to adns fns */
+ adns_if_checkc_freq= 0x0300,/* consistency checks very frequently (slow!) */
+
+ adns_if_permit_ipv4= 0x0400,/* allow _addr queries to return IPv4 addresses */
+ adns_if_permit_ipv6= 0x0800,/* allow _addr queries to return IPv6 addresses */
+ adns_if_afmask= 0x0c00,
+ /* These are policy flags, and overridden by the adns_af:... option in
+ * resolv.conf. If the adns_qf_want_... query flags are incompatible with
+ * these settings (in the sense that no address families are permitted at
+ * all) then the query flags take precedence; otherwise only records which
+ * satisfy all of the stated requirements are allowed.
+ */
+ adns__if_sizeforce= 0x7fff,
+} adns_initflags;
+
+typedef enum { /* In general, or together the desired flags: */
+ adns_qf_none= 0x00000000,/* no flags */
+ adns_qf_search= 0x00000001,/* use the searchlist */
+ adns_qf_usevc= 0x00000002,/* use a virtual circuit (TCP conn) */
+ adns_qf_owner= 0x00000004,/* fill in the owner field in the answer */
+ adns_qf_quoteok_query= 0x00000010,/* allow special chars in query domain */
+ adns_qf_quoteok_cname= 0x00000000,/* ... in CNAME we go via (now default) */
+ adns_qf_quoteok_anshost=0x00000040,/* ... in things supposedly hostnames */
+ adns_qf_quotefail_cname=0x00000080,/* refuse if quote-req chars in CNAME we go via */
+ adns_qf_cname_loose= 0x00000100,/* allow refs to CNAMEs - without, get _s_cname */
+ adns_qf_cname_strict= 0x00010000,/* forbid CNAME refs (default, currently) */
+ adns_qf_cname_forbid= 0x00000200,/* don't follow CNAMEs, instead give _s_cname */
+
+ adns_qf_want_ipv4= 0x00000400,/* try to return IPv4 addresses */
+ adns_qf_want_ipv6= 0x00000800,/* try to return IPv6 addresses */
+ adns_qf_want_allaf= 0x00000c00,/* all the above flag bits */
+ /* Without any of the _qf_want_... flags, _qtf_deref queries try to return
+ * all address families permitted by _if_permit_... (as overridden by the
+ * `adns_af:...' configuration option). Set flags to restrict the
+ * returned address families to the ones selected.
+ */
+ adns_qf_ipv6_mapv4= 0x00001000,/* ... return IPv4 addresses as v6-mapped */
+
+ adns_qf_addrlit_scope_forbid=0x00002000,/* forbid %<scope> in IPv6 literals */
+ adns_qf_addrlit_scope_numeric=0x00004000,/* %<scope> may only be numeric */
+ adns_qf_addrlit_ipv4_quadonly=0x00008000,/* reject non-dotted-quad ipv4 */
+
+ adns__qf_internalmask= 0x0ff00000,
+ adns__qf_sizeforce= 0x7fffffff
+} adns_queryflags;
+
+typedef enum {
+ adns_rrt_typemask= 0x0ffff,
+ adns_rrt_reprmask= 0xffffff,
+ adns__qtf_deref_bit=0x10000,/* internal version of ..._deref below */
+ adns__qtf_mail822= 0x20000,/* return mailboxes in RFC822 rcpt field fmt */
+
+ adns__qtf_bigaddr=0x1000000,/* use the new larger sockaddr union */
+ adns__qtf_manyaf= 0x2000000,/* permitted to return multiple address families */
+
+ adns__qtf_deref= adns__qtf_deref_bit|adns__qtf_bigaddr
+#ifdef ADNS_FEATURE_MANYAF
+ |adns__qtf_manyaf
+#endif
+ ,/* dereference domains; perhaps get extra data */
+
+ adns_r_unknown= 0x40000,
+ /* To use this, ask for records of type <rr-type-code>|adns_r_unknown.
+ * adns will not process the RDATA - you'll get adns_rr_byteblocks,
+ * where the int is the length and the unsigned char* points to the
+ * data. String representation of the RR data (by adns_rrinfo) is as in
+ * RFC3597. adns_rr_info will not return the type name in *rrtname_r
+ * (due to memory management problems); *fmtname_r will be set to
+ * "unknown".
+ *
+ * Do not specify adns_r_unknown along with a known RR type which
+ * requires domain name uncompression (see RFC3597 s4); domain names
+ * will not be uncompressed and the resulting data would be useless.
+ * Asking for meta-RR types via adns_r_unknown will not work properly
+ * either and may make adns complain about server misbehaviour, so don't
+ * do that.
+ *
+ * Don't forget adns_qf_quoteok if that's what you want. */
+
+ adns_r_none= 0,
+
+ adns_r_a= 1,
+
+ adns_r_ns_raw= 2,
+ adns_r_ns= adns_r_ns_raw|adns__qtf_deref,
+
+ adns_r_cname= 5,
+
+ adns_r_soa_raw= 6,
+ adns_r_soa= adns_r_soa_raw|adns__qtf_mail822,
+
+ adns_r_ptr_raw= 12, /* do not mind PTR with wrong or missing addr */
+ adns_r_ptr= adns_r_ptr_raw|adns__qtf_deref,
+
+ adns_r_hinfo= 13,
+
+ adns_r_mx_raw= 15,
+ adns_r_mx= adns_r_mx_raw|adns__qtf_deref,
+
+ adns_r_txt= 16,
+
+ adns_r_rp_raw= 17,
+ adns_r_rp= adns_r_rp_raw|adns__qtf_mail822,
+
+ adns_r_aaaa= 28,
+
+ /* For SRV records, query domain without _qf_quoteok_query must look
+ * as expected from SRV RFC with hostname-like Name. _With_
+ * _quoteok_query, any query domain is allowed. */
+ adns_r_srv_raw= 33,
+ adns_r_srv= adns_r_srv_raw|adns__qtf_deref,
+
+ adns_r_addr= adns_r_a|adns__qtf_deref,
+
+ adns__rrt_sizeforce= 0x7fffffff,
+
+} adns_rrtype;
+
+/*
+ * In queries without qf_quoteok_*, all domains must have standard
+ * legal syntax, or you get adns_s_querydomainvalid (if the query
+ * domain contains bad characters) or adns_s_answerdomaininvalid (if
+ * the answer contains bad characters).
+ *
+ * In queries _with_ qf_quoteok_*, domains in the query or response
+ * may contain any characters, quoted according to RFC1035 5.1. On
+ * input to adns, the char* is a pointer to the interior of a "
+ * delimited string, except that " may appear in it unquoted. On
+ * output, the char* is a pointer to a string which would be legal
+ * either inside or outside " delimiters; any character which isn't
+ * legal in a hostname (ie alphanumeric or hyphen) or one of _ / +
+ * (the three other punctuation characters commonly abused in domain
+ * names) will be quoted, as \X if it is a printing ASCII character or
+ * \DDD otherwise.
+ *
+ * If the query goes via a CNAME then the canonical name (ie, the
+ * thing that the CNAME record refers to) is usually allowed to
+ * contain any characters, which will be quoted as above. With
+ * adns_qf_quotefail_cname you get adns_s_answerdomaininvalid when
+ * this happens. (This is a change from version 0.4 and earlier, in
+ * which failing the query was the default, and you had to say
+ * adns_qf_quoteok_cname to avoid this; that flag is now deprecated.)
+ *
+ * In version 0.4 and earlier, asking for _raw records containing
+ * mailboxes without specifying _qf_quoteok_anshost was silly. This
+ * is no longer the case. In this version only parts of responses
+ * that are actually supposed to be hostnames will be refused by
+ * default if quote-requiring characters are found.
+ */
+
+/*
+ * If you ask for an RR which contains domains which are actually
+ * encoded mailboxes, and don't ask for the _raw version, then adns
+ * returns the mailbox formatted suitably for an RFC822 recipient
+ * header field. The particular format used is that if the mailbox
+ * requires quoting according to the rules in RFC822 then the
+ * local-part is quoted in double quotes, which end at the next
+ * unescaped double quote (\ is the escape char, and is doubled, and
+ * is used to escape only \ and "). If the local-part is legal
+ * without quoting according to RFC822, it is presented as-is. In any
+ * case the local-part is followed by an @ and the domain. The domain
+ * will not contain any characters not legal in hostnames.
+ *
+ * Unquoted local-parts may contain any printing 7-bit ASCII
+ * except the punctuation characters ( ) < > @ , ; : \ " [ ]
+ * I.e. they may contain alphanumerics, and the following
+ * punctuation characters: ! # % ^ & * - _ = + { } .
+ *
+ * adns will reject local parts containing control characters (byte
+ * values 0-31, 127-159, and 255) - these appear to be legal according
+ * to RFC822 (at least 0-127) but are clearly a bad idea. RFC1035
+ * syntax does not make any distinction between a single RFC822
+ * quoted-string containing full stops, and a series of quoted-strings
+ * separated by full stops; adns will return anything that isn't all
+ * valid atoms as a single quoted-string. RFC822 does not allow
+ * high-bit-set characters at all, but adns does allow them in
+ * local-parts, treating them as needing quoting.
+ *
+ * If you ask for the domain with _raw then _no_ checking is done
+ * (even on the host part, regardless of adns_qf_quoteok_anshost), and
+ * you just get the domain name in master file format.
+ *
+ * If no mailbox is supplied the returned string will be `.' in either
+ * case.
+ */
+
+typedef enum {
+ adns_s_ok,
+
+ /* locally induced errors */
+ adns_s_nomemory,
+ adns_s_unknownrrtype,
+ adns_s_systemfail,
+
+ adns_s_max_localfail= 29,
+
+ /* remotely induced errors, detected locally */
+ adns_s_timeout,
+ adns_s_allservfail,
+ adns_s_norecurse,
+ adns_s_invalidresponse,
+ adns_s_unknownformat,
+
+ adns_s_max_remotefail= 59,
+
+ /* remotely induced errors, reported by remote server to us */
+ adns_s_rcodeservfail,
+ adns_s_rcodeformaterror,
+ adns_s_rcodenotimplemented,
+ adns_s_rcoderefused,
+ adns_s_rcodeunknown,
+
+ adns_s_max_tempfail= 99,
+
+ /* remote configuration errors */
+ adns_s_inconsistent, /* PTR gives domain whose addr is missing or mismatch */
+ adns_s_prohibitedcname, /* CNAME, but eg A expected (not if _qf_cname_loose) */
+ adns_s_answerdomaininvalid,
+ adns_s_answerdomaintoolong,
+ adns_s_invaliddata,
+
+ adns_s_max_misconfig= 199,
+
+ /* permanent problems with the query */
+ adns_s_querydomainwrong,
+ adns_s_querydomaininvalid,
+ adns_s_querydomaintoolong,
+
+ adns_s_max_misquery= 299,
+
+ /* permanent errors */
+ adns_s_nxdomain,
+ adns_s_nodata,
+
+ adns_s_max_permfail= 499
+
+} adns_status;
+
+typedef union {
+ struct sockaddr sa;
+ struct sockaddr_in inet;
+} adns_sockaddr_v4only;
+
+typedef union {
+ struct sockaddr sa;
+ struct sockaddr_in inet;
+ struct sockaddr_in6 inet6;
+} adns_sockaddr;
+
+typedef struct {
+ int len;
+ adns_sockaddr addr;
+} adns_rr_addr;
+
+typedef struct {
+ /* the old v4-only structure; handy if you have complicated binary
+ * compatibility problems. */
+ int len;
+ adns_sockaddr_v4only addr;
+} adns_rr_addr_v4only;
+
+typedef struct {
+ char *host;
+ adns_status astatus;
+ int naddrs; /* temp fail => -1, perm fail => 0, s_ok => >0 */
+ adns_rr_addr *addrs;
+} adns_rr_hostaddr;
+
+typedef struct {
+ char *(array[2]);
+} adns_rr_strpair;
+
+typedef struct {
+ int i;
+ adns_rr_hostaddr ha;
+} adns_rr_inthostaddr;
+
+typedef struct {
+ /* Used both for mx_raw, in which case i is the preference and str
+ * the domain, and for txt, in which case each entry has i for the
+ * `text' length, and str for the data (which will have had an extra
+ * nul appended so that if it was plain text it is now a
+ * null-terminated string).
+ */
+ int i;
+ char *str;
+} adns_rr_intstr;
+
+typedef struct {
+ adns_rr_intstr array[2];
+} adns_rr_intstrpair;
+
+typedef struct {
+ char *mname, *rname;
+ unsigned long serial, refresh, retry, expire, minimum;
+} adns_rr_soa;
+
+typedef struct {
+ int priority, weight, port;
+ char *host;
+} adns_rr_srvraw;
+
+typedef struct {
+ int priority, weight, port;
+ adns_rr_hostaddr ha;
+} adns_rr_srvha;
+
+typedef struct {
+ int len;
+ unsigned char *data;
+} adns_rr_byteblock;
+
+typedef struct {
+ adns_status status;
+ char *cname; /* always NULL if query was for CNAME records */
+ char *owner; /* only set if req'd in query flags; maybe 0 on error anyway */
+ adns_rrtype type; /* guaranteed to be same as in query */
+ time_t expires;/*abs time. def only if _s_ok, nxdomain or nodata. NOT TTL!*/
+ int nrrs, rrsz; /* nrrs is 0 if an error occurs */
+ union {
+ void *untyped;
+ unsigned char *bytes;
+ char *(*str); /* ns_raw, cname, ptr, ptr_raw */
+ adns_rr_intstr *(*manyistr); /* txt (list strs ends with i=-1, str=0)*/
+ adns_rr_addr *addr; /* addr */
+ struct in_addr *inaddr; /* a */
+ struct in6_addr *in6addr; /* aaaa */
+ adns_rr_hostaddr *hostaddr; /* ns */
+ adns_rr_intstrpair *intstrpair; /* hinfo */
+ adns_rr_strpair *strpair; /* rp, rp_raw */
+ adns_rr_inthostaddr *inthostaddr;/* mx */
+ adns_rr_intstr *intstr; /* mx_raw */
+ adns_rr_soa *soa; /* soa, soa_raw */
+ adns_rr_srvraw *srvraw; /* srv_raw */
+ adns_rr_srvha *srvha;/* srv */
+ adns_rr_byteblock *byteblock; /* ...|unknown */
+ } rrs;
+} adns_answer;
+
+/* Memory management:
+ * adns_state and adns_query are actually pointers to malloc'd state;
+ * On submission questions are copied, including the owner domain;
+ * Answers are malloc'd as a single piece of memory; pointers in the
+ * answer struct point into further memory in the answer.
+ * query_io:
+ * Must always be non-null pointer;
+ * If *query_io is 0 to start with then any query may be returned;
+ * If *query_io is !0 adns_query then only that query may be returned.
+ * If the call is successful, *query_io, *answer_r, and *context_r
+ * will all be set.
+ * Errors:
+ * Return values are 0 or an errno value.
+ *
+ * For _init, _init_strcfg, _submit and _synchronous, system errors
+ * (eg, failure to create sockets, malloc failure, etc.) return errno
+ * values. EINVAL from _init et al means the configuration file
+ * is erroneous and cannot be parsed.
+ *
+ * For _wait and _check failures are reported in the answer
+ * structure, and only 0, ESRCH or (for _check) EAGAIN is
+ * returned: if no (appropriate) requests are done adns_check returns
+ * EAGAIN; if no (appropriate) requests are outstanding both
+ * adns_query and adns_wait return ESRCH.
+ *
+ * Additionally, _wait can return EINTR if you set adns_if_eintr.
+ *
+ * All other errors (nameserver failure, timed out connections, &c)
+ * are returned in the status field of the answer. After a
+ * successful _wait or _check, if status is nonzero then nrrs will be
+ * 0, otherwise it will be >0. type will always be the type
+ * requested.
+ */
+
+/* Threads:
+ * adns does not use any static modifiable state, so it
+ * is safe to call adns_init several times and then use the
+ * resulting adns_states concurrently.
+ * However, it is NOT safe to make simultaneous calls into
+ * adns using the same adns_state; a single adns_state must be used
+ * only by one thread at a time. You can solve this problem by
+ * having one adns_state per thread, or if that isn't feasible, you
+ * could maintain a pool of adns_states. Unfortunately neither of
+ * these approaches has optimal performance.
+ */
+
+int adns_init(adns_state *newstate_r, adns_initflags flags,
+ FILE *diagfile /*0=>stderr*/);
+
+int adns_init_strcfg(adns_state *newstate_r, adns_initflags flags,
+ FILE *diagfile /*0=>discard*/, const char *configtext);
+
+typedef void adns_logcallbackfn(adns_state ads, void *logfndata,
+ const char *fmt, va_list al);
+ /* Will be called perhaps several times for each message; when the
+ * message is complete, the string implied by fmt and al will end in
+ * a newline. Log messages start with `adns debug:' or `adns
+ * warning:' or `adns:' (for errors), or `adns debug [PID]:'
+ * etc. if adns_if_logpid is set. */
+
+int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
+ const char *configtext /*0=>use default config files*/,
+ adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
+ void *logfndata /*0 with logfn==0 => discard*/);
+
+/* Configuration:
+ * adns_init reads /etc/resolv.conf, which is expected to be (broadly
+ * speaking) in the format expected by libresolv, and then
+ * /etc/resolv-adns.conf if it exists. adns_init_strcfg is instead
+ * passed a string which is interpreted as if it were the contents of
+ * resolv.conf or resolv-adns.conf. In general, configuration which
+ * is set later overrides any that is set earlier.
+ *
+ * Standard directives understood in resolv[-adns].conf:
+ *
+ * nameserver <address>
+ * Must be followed by the IP address of a nameserver. Several
+ * nameservers may be specified, and they will be tried in the order
+ * found. There is a compiled in limit, currently 5, on the number
+ * of nameservers. (libresolv supports only 3 nameservers.)
+ *
+ * search <domain> ...
+ * Specifies the search list for queries which specify
+ * adns_qf_search. This is a list of domains to append to the query
+ * domain. The query domain will be tried as-is either before all
+ * of these or after them, depending on the ndots option setting
+ * (see below).
+ *
+ * domain <domain>
+ * This is present only for backward compatibility with obsolete
+ * versions of libresolv. It should not be used, and is interpreted
+ * by adns as if it were `search' - note that this is subtly
+ * different to libresolv's interpretation of this directive.
+ *
+ * sortlist <addr>/<mask> ...
+ * Should be followed by a sequence of IP-address and netmask pairs,
+ * separated by spaces. They may be specified as
+ * eg. 172.30.206.0/24 or 172.30.206.0/255.255.255.0. Currently up
+ * to 15 pairs may be specified (but note that libresolv only
+ * supports up to 10).
+ *
+ * options
+ * Should followed by one or more options, separated by spaces.
+ * Each option consists of an option name, followed by optionally
+ * a colon and a value. Options are listed below.
+ *
+ * Non-standard directives understood in resolv[-adns].conf:
+ *
+ * clearnameservers
+ * Clears the list of nameservers, so that further nameserver lines
+ * start again from the beginning.
+ *
+ * include <filename>
+ * The specified file will be read.
+ *
+ * Additionally, adns will ignore lines in resolv[-adns].conf which
+ * start with a #.
+ *
+ * Standard options understood:
+ *
+ * debug
+ * Enables debugging output from the resolver, which will be written
+ * to stderr.
+ *
+ * ndots:<count>
+ * Affects whether queries with adns_qf_search will be tried first
+ * without adding domains from the searchlist, or whether the bare
+ * query domain will be tried last. Queries which contain at least
+ * <count> dots will be tried bare first. The default is 1.
+ *
+ * Non-standard options understood:
+ *
+ * adns_checkc:none
+ * adns_checkc:entex
+ * adns_checkc:freq
+ * Changes the consistency checking frequency; this overrides the
+ * setting of adns_if_check_entex, adns_if_check_freq, or neither,
+ * in the flags passed to adns_init.
+ *
+ * adns_af:{ipv4,ipv6},... adns_af:any
+ * Determines which address families ADNS looks up (either as an
+ * adns_r_addr query, or when dereferencing an answer yielding hostnames
+ * (e.g., adns_r_mx). The argument is a comma-separated list: only the
+ * address families listed will be looked up. The default is `any'.
+ * Lookups occur (logically) concurrently; use the `sortlist' directive to
+ * control the relative order of addresses in answers. This option
+ * overrides the corresponding init flags (covered by adns_if_afmask).
+ *
+ * adns_ignoreunkcfg
+ * Ignore unknown options and configuration directives, rather than
+ * logging them. To be effective, appear in the configuration
+ * before the unknown options. ADNS_RES_OPTIONS is generally early
+ * enough.
+ *
+ * There are a number of environment variables which can modify the
+ * behaviour of adns. They take effect only if adns_init is used, and
+ * the caller of adns_init can disable them using adns_if_noenv. In
+ * each case there is both a FOO and an ADNS_FOO; the latter is
+ * interpreted later so that it can override the former. Unless
+ * otherwise stated, environment variables are interpreted after
+ * resolv[-adns].conf are read, in the order they are listed here.
+ *
+ * RES_CONF, ADNS_RES_CONF
+ * A filename, whose contets are in the format of resolv.conf.
+ *
+ * RES_CONF_TEXT, ADNS_RES_CONF_TEXT
+ * A string in the format of resolv.conf.
+ *
+ * RES_OPTIONS, ADNS_RES_OPTIONS
+ * These are parsed as if they appeared in the `options' line of a
+ * resolv.conf. In addition to being parsed at this point in the
+ * sequence, they are also parsed at the very beginning before
+ * resolv.conf or any other environment variables are read, so that
+ * any debug option can affect the processing of the configuration.
+ *
+ * LOCALDOMAIN, ADNS_LOCALDOMAIN
+ * These are interpreted as if their contents appeared in a `search'
+ * line in resolv.conf.
+ */
+
+int adns_synchronous(adns_state ads,
+ const char *owner,
+ adns_rrtype type,
+ adns_queryflags flags,
+ adns_answer **answer_r);
+
+/* NB: if you set adns_if_noautosys then _submit and _check do not
+ * make any system calls; you must use some of the asynch-io event
+ * processing functions to actually get things to happen.
+ */
+
+int adns_submit(adns_state ads,
+ const char *owner,
+ adns_rrtype type,
+ adns_queryflags flags,
+ void *context,
+ adns_query *query_r);
+
+/* The owner should be quoted in master file format. */
+
+int adns_check(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer_r,
+ void **context_r);
+
+int adns_wait(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer_r,
+ void **context_r);
+
+/* same as adns_wait but uses poll(2) internally */
+int adns_wait_poll(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer_r,
+ void **context_r);
+
+void adns_cancel(adns_query query);
+
+/* The adns_query you get back from _submit is valid (ie, can be
+ * legitimately passed into adns functions) until it is returned by
+ * adns_check or adns_wait, or passed to adns_cancel. After that it
+ * must not be used. You can rely on it not being reused until the
+ * first adns_submit or _transact call using the same adns_state after
+ * it became invalid, so you may compare it for equality with other
+ * query handles until you next call _query or _transact.
+ *
+ * _submit and _synchronous return ENOSYS if they don't understand the
+ * query type.
+ */
+
+int adns_submit_reverse(adns_state ads,
+ const struct sockaddr *addr,
+ adns_rrtype type,
+ adns_queryflags flags,
+ void *context,
+ adns_query *query_r);
+/* type must be _r_ptr or _r_ptr_raw. _qf_search is ignored.
+ * addr->sa_family must be AF_INET or you get ENOSYS.
+ */
+
+int adns_submit_reverse_any(adns_state ads,
+ const struct sockaddr *addr,
+ const char *rzone,
+ adns_rrtype type,
+ adns_queryflags flags,
+ void *context,
+ adns_query *query_r);
+/* For RBL-style reverse `zone's; look up
+ * <reversed-address>.<zone>
+ * Any type is allowed. _qf_search is ignored.
+ * addr->sa_family must be AF_INET or you get ENOSYS.
+ */
+
+void adns_finish(adns_state ads);
+/* You may call this even if you have queries outstanding;
+ * they will be cancelled.
+ */
+
+#define ADNS_ADDR2TEXT_BUFLEN \
+ (INET6_ADDRSTRLEN + 1/*%*/ \
+ + ((IF_NAMESIZE-1) > 9 ? (IF_NAMESIZE-1) : 9/*uint32*/) \
+ + 1/* nul; included in IF_NAMESIZE */)
+
+int adns_text2addr(const char *text, uint16_t port, adns_queryflags flags,
+ struct sockaddr *sa_r,
+ socklen_t *salen_io /* updated iff OK or ENOSPC */);
+int adns_addr2text(const struct sockaddr *sa, adns_queryflags flags,
+ char *buffer, int *buflen_io /* updated ONLY on ENOSPC */,
+ int *port_r /* may be 0 */);
+ /*
+ * port is always in host byte order and is simply copied to and
+ * from the appropriate sockaddr field (byteswapped as necessary).
+ *
+ * The only flags supported are adns_qf_addrlit_...
+ *
+ * Error return values are:
+ *
+ * ENOSPC Output buffer is too small. Can only happen if
+ * *buflen_io < ADNS_ADDR2TEXT_BUFLEN or
+ * *salen_io < sizeof(adns_sockaddr). On return,
+ * *buflen_io or *salen_io has been updated by adns.
+ *
+ * EINVAL text has invalid syntax.
+ *
+ * text represents an address family not supported by
+ * this version of adns.
+ *
+ * Scoped address supplied (text contained "%" or
+ * sin6_scope_id nonzero) but caller specified
+ * adns_qf_addrlit_scope_forbid.
+ *
+ * Scope name (rather than number) supplied in text but
+ * caller specified adns_qf_addrlit_scope_numeric.
+ *
+ * EAFNOSUPPORT sa->sa_family is not supported (addr2text only).
+ *
+ * ENOSYS Unsupported flags set.
+ *
+ * Only if neither adns_qf_addrlit_scope_forbid nor
+ * adns_qf_addrlit_scope_numeric are set:
+ *
+ * ENOSYS Scope name supplied in text but IPv6 address part of
+ * sockaddr is not a link local address.
+ *
+ * ENXIO Scope name supplied in text but if_nametoindex
+ * said it wasn't a valid local interface name.
+ *
+ * EIO Scoped address supplied but if_nametoindex failed
+ * in an unexpected way; adns has printed a message to
+ * stderr.
+ *
+ * any other if_nametoindex failed in a more-or-less expected way.
+ */
+
+void adns_forallqueries_begin(adns_state ads);
+adns_query adns_forallqueries_next(adns_state ads, void **context_r);
+/* Iterator functions, which you can use to loop over the outstanding
+ * (submitted but not yet successfuly checked/waited) queries.
+ *
+ * You can only have one iteration going at once. You may call _begin
+ * at any time; after that, an iteration will be in progress. You may
+ * only call _next when an iteration is in progress - anything else
+ * may coredump. The iteration remains in progress until _next
+ * returns 0, indicating that all the queries have been walked over,
+ * or ANY other adns function is called with the same adns_state (or a
+ * query in the same adns_state). There is no need to explicitly
+ * finish an iteration.
+ *
+ * context_r may be 0. *context_r may not be set when _next returns 0.
+ */
+
+void adns_checkconsistency(adns_state ads, adns_query qu);
+/* Checks the consistency of adns's internal data structures.
+ * If any error is found, the program will abort().
+ * You may pass 0 for qu; if you pass non-null then additional checks
+ * are done to make sure that qu is a valid query.
+ */
+
+/*
+ * Example expected/legal calling sequence for submit/check/wait:
+ * adns_init
+ * adns_submit 1
+ * adns_submit 2
+ * adns_submit 3
+ * adns_wait 1
+ * adns_check 3 -> EAGAIN
+ * adns_wait 2
+ * adns_wait 3
+ * ....
+ * adns_finish
+ */
+
+/*
+ * Entrypoints for generic asynch io:
+ * (these entrypoints are not very useful except in combination with *
+ * some of the other I/O model calls which can tell you which fds to
+ * be interested in):
+ *
+ * Note that any adns call may cause adns to open and close fds, so
+ * you must call beforeselect or beforepoll again just before
+ * blocking, or you may not have an up-to-date list of it's fds.
+ */
+
+int adns_processany(adns_state ads);
+/* Gives adns flow-of-control for a bit. This will never block, and
+ * can be used with any threading/asynch-io model. If some error
+ * occurred which might cause an event loop to spin then the errno
+ * value is returned.
+ */
+
+int adns_processreadable(adns_state ads, int fd, const struct timeval *now);
+int adns_processwriteable(adns_state ads, int fd, const struct timeval *now);
+int adns_processexceptional(adns_state ads, int fd, const struct timeval *now);
+/* Gives adns flow-of-control so that it can process incoming data
+ * from, or send outgoing data via, fd. Very like _processany. If it
+ * returns zero then fd will no longer be readable or writeable
+ * (unless of course more data has arrived since). adns will _only_
+ * use that fd and only in the manner specified, regardless of whether
+ * adns_if_noautosys was specified.
+ *
+ * adns_processexceptional should be called when select(2) reports an
+ * exceptional condition, or poll(2) reports POLLPRI.
+ *
+ * It is fine to call _processreabable or _processwriteable when the
+ * fd is not ready, or with an fd that doesn't belong to adns; it will
+ * then just return 0.
+ *
+ * If some error occurred which might prevent an event loop to spin
+ * then the errno value is returned.
+ */
+
+void adns_processtimeouts(adns_state ads, const struct timeval *now);
+/* Gives adns flow-of-control so that it can process any timeouts
+ * which might have happened. Very like _processreadable/writeable.
+ *
+ * now may be 0; if it isn't, *now must be the current time, recently
+ * obtained from gettimeofday.
+ */
+
+void adns_firsttimeout(adns_state ads,
+ struct timeval **tv_mod, struct timeval *tv_buf,
+ struct timeval now);
+/* Asks adns when it would first like the opportunity to time
+ * something out. now must be the current time, from gettimeofday.
+ *
+ * If tv_mod points to 0 then tv_buf must be non-null, and
+ * _firsttimeout will fill in *tv_buf with the time until the first
+ * timeout, and make *tv_mod point to tv_buf. If adns doesn't have
+ * anything that might need timing out it will leave *tv_mod as 0.
+ *
+ * If *tv_mod is not 0 then tv_buf is not used. adns will update
+ * *tv_mod if it has any earlier timeout, and leave it alone if it
+ * doesn't.
+ *
+ * This call will not actually do any I/O, or change the fds that adns
+ * is using. It always succeeds and never blocks.
+ */
+
+void adns_globalsystemfailure(adns_state ads);
+/* If serious problem(s) happen which globally affect your ability to
+ * interact properly with adns, or adns's ability to function
+ * properly, you or adns can call this function.
+ *
+ * All currently outstanding queries will be made to fail with
+ * adns_s_systemfail, and adns will close any stream sockets it has
+ * open.
+ *
+ * This is used by adns, for example, if gettimeofday() fails.
+ * Without this the program's event loop might start to spin !
+ *
+ * This call will never block.
+ */
+
+/*
+ * Entrypoints for select-loop based asynch io:
+ */
+
+void adns_beforeselect(adns_state ads, int *maxfd, fd_set *readfds,
+ fd_set *writefds, fd_set *exceptfds,
+ struct timeval **tv_mod, struct timeval *tv_buf,
+ const struct timeval *now);
+/* Find out file descriptors adns is interested in, and when it would
+ * like the opportunity to time something out. If you do not plan to
+ * block then tv_mod may be 0. Otherwise, tv_mod and tv_buf are as
+ * for adns_firsttimeout. readfds, writefds, exceptfds and maxfd_io may
+ * not be 0.
+ *
+ * If tv_mod is 0 on entry then this will never actually do any I/O,
+ * or change the fds that adns is using or the timeouts it wants. In
+ * any case it won't block, and it will set the timeout to zero if a
+ * query finishes in _beforeselect.
+ */
+
+void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
+ const fd_set *writefds, const fd_set *exceptfds,
+ const struct timeval *now);
+/* Gives adns flow-of-control for a bit; intended for use after
+ * select. This is just a fancy way of calling adns_processreadable/
+ * writeable/timeouts as appropriate, as if select had returned the
+ * data being passed. Always succeeds.
+ */
+
+/*
+ * Example calling sequence:
+ *
+ * adns_init _noautosys
+ * loop {
+ * adns_beforeselect
+ * select
+ * adns_afterselect
+ * ...
+ * adns_submit / adns_check
+ * ...
+ * }
+ */
+
+/*
+ * Entrypoints for poll-loop based asynch io:
+ */
+
+struct pollfd;
+/* In case your system doesn't have it or you forgot to include
+ * <sys/poll.h>, to stop the following declarations from causing
+ * problems. If your system doesn't have poll then the following
+ * entrypoints will not be defined in libadns. Sorry !
+ */
+
+int adns_beforepoll(adns_state ads, struct pollfd *fds,
+ int *nfds_io, int *timeout_io,
+ const struct timeval *now);
+/* Finds out which fd's adns is interested in, and when it would like
+ * to be able to time things out. This is in a form suitable for use
+ * with poll(2).
+ *
+ * On entry, usually fds should point to at least *nfds_io structs.
+ * adns will fill up to that many structs will information for poll,
+ * and record in *nfds_io how many structs it filled. If it wants to
+ * listen for more structs then *nfds_io will be set to the number
+ * required and _beforepoll will return ERANGE.
+ *
+ * You may call _beforepoll with fds==0 and *nfds_io 0, in which case
+ * adns will fill in the number of fds that it might be interested in
+ * in *nfds_io, and always return either 0 (if it is not interested in
+ * any fds) or ERANGE (if it is).
+ *
+ * NOTE that (unless now is 0) adns may acquire additional fds
+ * from one call to the next, so you must put adns_beforepoll in a
+ * loop, rather than assuming that the second call (with the buffer
+ * size requested by the first) will not return ERANGE.
+ *
+ * adns only ever sets POLLIN, POLLOUT and POLLPRI in its pollfd
+ * structs, and only ever looks at those bits. POLLPRI is required to
+ * detect TCP Urgent Data (which should not be used by a DNS server)
+ * so that adns can know that the TCP stream is now useless.
+ *
+ * In any case, *timeout_io should be a timeout value as for poll(2),
+ * which adns will modify downwards as required. If the caller does
+ * not plan to block then *timeout_io should be 0 on entry, or
+ * alternatively, timeout_io may be 0. (Alternatively, the caller may
+ * use _beforeselect with timeout_io==0 to find out about file
+ * descriptors, and use _firsttimeout is used to find out when adns
+ * might want to time something out.)
+ *
+ * adns_beforepoll will return 0 on success, and will not fail for any
+ * reason other than the fds buffer being too small (ERANGE).
+ *
+ * This call will never actually do any I/O. If you supply the
+ * current time it will not change the fds that adns is using or the
+ * timeouts it wants.
+ *
+ * In any case this call won't block.
+ */
+
+#define ADNS_POLLFDS_RECOMMENDED 3
+/* If you allocate an fds buf with at least RECOMMENDED entries then
+ * you are unlikely to need to enlarge it. You are recommended to do
+ * so if it's convenient. However, you must be prepared for adns to
+ * require more space than this.
+ */
+
+void adns_afterpoll(adns_state ads, const struct pollfd *fds, int nfds,
+ const struct timeval *now);
+/* Gives adns flow-of-control for a bit; intended for use after
+ * poll(2). fds and nfds should be the results from poll(). pollfd
+ * structs mentioning fds not belonging to adns will be ignored.
+ */
+
+
+adns_status adns_rr_info(adns_rrtype type,
+ const char **rrtname_r, const char **fmtname_r,
+ int *len_r,
+ const void *datap, char **data_r);
+/*
+ * Get information about a query type, or convert reply data to a
+ * textual form. type must be specified, and the official name of the
+ * corresponding RR type will be returned in *rrtname_r, and
+ * information about the processing style in *fmtname_r. The length
+ * of the table entry in an answer for that type will be returned in
+ * in *len_r. Any or all of rrtname_r, fmtname_r and len_r may be 0.
+ * If fmtname_r is non-null then *fmtname_r may be null on return,
+ * indicating that no special processing is involved.
+ *
+ * data_r be must be non-null iff datap is. In this case *data_r will
+ * be set to point to a string pointing to a representation of the RR
+ * data in master file format. (The owner name, timeout, class and
+ * type will not be present - only the data part of the RR.) The
+ * memory will have been obtained from malloc() and must be freed by
+ * the caller.
+ *
+ * Usually this routine will succeed. Possible errors include:
+ * adns_s_nomemory
+ * adns_s_rrtypeunknown
+ * adns_s_invaliddata (*datap contained garbage)
+ * If an error occurs then no memory has been allocated,
+ * and *rrtname_r, *fmtname_r, *len_r and *data_r are undefined.
+ *
+ * There are some adns-invented data formats which are not official
+ * master file formats. These include:
+ *
+ * Mailboxes if __qtf_mail822: these are just included as-is.
+ *
+ * Addresses (adns_rr_addr): these may be of pretty much any type.
+ * The representation is in two parts: first, a word for the address
+ * family (ie, in AF_XXX, the XXX), and then one or more items for the
+ * address itself, depending on the format. For an IPv4 address the
+ * syntax is INET followed by the dotted quad (from inet_ntoa).
+ * Currently only IPv4 is supported.
+ *
+ * Text strings (as in adns_rr_txt) appear inside double quotes, and
+ * use \" and \\ to represent " and \, and \xHH to represent
+ * characters not in the range 32-126.
+ *
+ * Hostname with addresses (adns_rr_hostaddr): this consists of the
+ * hostname, as usual, followed by the adns_status value, as an
+ * abbreviation, and then a descriptive string (encoded as if it were
+ * a piece of text), for the address lookup, followed by zero or more
+ * addresses enclosed in ( and ). If the result was a temporary
+ * failure, then a single ? appears instead of the ( ). If the
+ * result was a permanent failure then an empty pair of parentheses
+ * appears (which a space in between). For example, one of the NS
+ * records for greenend.org.uk comes out like
+ * ns.chiark.greenend.org.uk ok "OK" ( INET 195.224.76.132 )
+ * an MX referring to a nonexistent host might come out like:
+ * 50 sun2.nsfnet-relay.ac.uk nxdomain "No such domain" ( )
+ * and if nameserver information is not available you might get:
+ * dns2.spong.dyn.ml.org timeout "DNS query timed out" ?
+ */
+
+const char *adns_strerror(adns_status st);
+const char *adns_errabbrev(adns_status st);
+const char *adns_errtypeabbrev(adns_status st);
+/* Like strerror but for adns_status values. adns_errabbrev returns
+ * the abbreviation of the error - eg, for adns_s_timeout it returns
+ * "timeout". adns_errtypeabbrev returns the abbreviation of the
+ * error class: ie, for values up to adns_s_max_XXX it will return the
+ * string XXX. You MUST NOT call these functions with status values
+ * not returned by the same adns library.
+ */
+
+#ifdef __cplusplus
+} /* end of extern "C" */
+#endif
+#endif
diff --git a/src/adns.make b/src/adns.make
new file mode 100644
index 0000000..8c2af75
--- /dev/null
+++ b/src/adns.make
@@ -0,0 +1,24 @@
+# src/adns.make - library definitions, including list of object files
+#
+# This file is part of adns, which is
+# Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+# Copyright (C) 2014 Mark Wooding
+# Copyright (C) 1999-2000,2003,2006 Tony Finch
+# Copyright (C) 1991 Massachusetts Institute of Technology
+# (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.
+
+LIBOBJS= types.o event.o query.o reply.o general.o setup.o transmit.o \
+ parse.o poll.o check.o addrfam.o
diff --git a/src/check.c b/src/check.c
new file mode 100644
index 0000000..08fe163
--- /dev/null
+++ b/src/check.c
@@ -0,0 +1,224 @@
+/*
+ * check.c
+ * - consistency checks
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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"
+
+void adns_checkconsistency(adns_state ads, adns_query qu) {
+ adns__consistency(ads,qu,cc_user);
+}
+
+#define DLIST_CHECK(list, nodevar, part, body) \
+ if ((list).head) { \
+ assert(! (list).head->part back); \
+ for ((nodevar)= (list).head; \
+ (nodevar); \
+ (nodevar)= (nodevar)->part next) { \
+ assert((nodevar)->part next \
+ ? (nodevar) == (nodevar)->part next->part back \
+ : (nodevar) == (list).tail); \
+ body \
+ } \
+ }
+
+#define DLIST_ASSERTON(node, nodevar, list, part) \
+ do { \
+ for ((nodevar)= (list).head; \
+ (nodevar) != (node); \
+ (nodevar)= (nodevar)->part next) { \
+ assert((nodevar)); \
+ } \
+ } while(0)
+
+static void checkc_query_alloc(adns_state ads, adns_query qu) {
+ allocnode *an;
+
+ DLIST_CHECK(qu->allocations, an, , {
+ });
+}
+
+static void checkc_query(adns_state ads, adns_query qu) {
+ adns_query child;
+
+ assert(qu->udpnextserver < ads->nservers);
+ assert(!(qu->udpsent & (~0UL << ads->nservers)));
+ assert(qu->search_pos <= ads->nsearchlist);
+ if (qu->parent) DLIST_ASSERTON(qu, child, qu->parent->children, siblings.);
+}
+
+static void checkc_notcpbuf(adns_state ads) {
+ assert(!ads->tcpsend.used);
+ assert(!ads->tcprecv.used);
+ assert(!ads->tcprecv_skip);
+}
+
+static void checkc_global(adns_state ads) {
+ const struct sortlist *sl;
+ int i;
+
+ assert(ads->udpsockets >= 0);
+
+ for (i=0; i<ads->nsortlist; i++) {
+ sl= &ads->sortlist[i];
+ assert(adns__addr_matches(sl->base.sa.sa_family,
+ adns__sockaddr_addr(&sl->base.sa),
+ &sl->base,&sl->mask));
+ }
+
+ assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers);
+
+ switch (ads->tcpstate) {
+ case server_connecting:
+ assert(ads->tcpsocket >= 0);
+ checkc_notcpbuf(ads);
+ break;
+ case server_disconnected:
+ case server_broken:
+ assert(ads->tcpsocket == -1);
+ checkc_notcpbuf(ads);
+ break;
+ case server_ok:
+ assert(ads->tcpsocket >= 0);
+ assert(ads->tcprecv_skip <= ads->tcprecv.used);
+ break;
+ default:
+ assert(!"ads->tcpstate value");
+ }
+
+ assert(ads->searchlist || !ads->nsearchlist);
+}
+
+static void checkc_queue_udpw(adns_state ads) {
+ adns_query qu;
+
+ DLIST_CHECK(ads->udpw, qu, , {
+ assert(qu->state==query_tosend);
+ assert(qu->retries <= UDPMAXRETRIES);
+ assert(qu->udpsent);
+ assert(!qu->children.head && !qu->children.tail);
+ checkc_query(ads,qu);
+ checkc_query_alloc(ads,qu);
+ });
+}
+
+static void checkc_queue_tcpw(adns_state ads) {
+ adns_query qu;
+
+ DLIST_CHECK(ads->tcpw, qu, , {
+ assert(qu->state==query_tcpw);
+ assert(!qu->children.head && !qu->children.tail);
+ assert(qu->retries <= ads->nservers+1);
+ checkc_query(ads,qu);
+ checkc_query_alloc(ads,qu);
+ });
+}
+
+static void checkc_queue_childw(adns_state ads) {
+ adns_query parent, child;
+
+ DLIST_CHECK(ads->childw, parent, , {
+ assert(parent->state == query_childw);
+ assert(parent->children.head);
+ DLIST_CHECK(parent->children, child, siblings., {
+ assert(child->parent == parent);
+ assert(child->state != query_done);
+ });
+ checkc_query(ads,parent);
+ checkc_query_alloc(ads,parent);
+ });
+}
+
+static void checkc_query_done(adns_state ads, adns_query qu) {
+ assert(qu->state == query_done);
+ assert(!qu->children.head && !qu->children.tail);
+ checkc_query(ads,qu);
+}
+
+static void checkc_queue_output(adns_state ads) {
+ adns_query qu;
+
+ DLIST_CHECK(ads->output, qu, , {
+ assert(!qu->parent);
+ assert(!qu->allocations.head && !qu->allocations.tail);
+ checkc_query_done(ads,qu);
+ });
+}
+
+static void checkc_queue_intdone(adns_state ads) {
+ adns_query qu;
+
+ DLIST_CHECK(ads->intdone, qu, , {
+ assert(qu->parent);
+ assert(qu->ctx.callback);
+ checkc_query_done(ads,qu);
+ });
+}
+
+void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) {
+ adns_query search;
+
+ switch (cc) {
+ case cc_user:
+ break;
+ case cc_entex:
+ if (!(ads->iflags & adns_if_checkc_entex)) return;
+ assert(!ads->intdone.head);
+ break;
+ case cc_freq:
+ if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return;
+ break;
+ default:
+ abort();
+ }
+
+ checkc_global(ads);
+ checkc_queue_udpw(ads);
+ checkc_queue_tcpw(ads);
+ checkc_queue_childw(ads);
+ checkc_queue_output(ads);
+ checkc_queue_intdone(ads);
+
+ if (qu) {
+ switch (qu->state) {
+ case query_tosend:
+ DLIST_ASSERTON(qu, search, ads->udpw, );
+ break;
+ case query_tcpw:
+ DLIST_ASSERTON(qu, search, ads->tcpw, );
+ break;
+ case query_childw:
+ DLIST_ASSERTON(qu, search, ads->childw, );
+ break;
+ case query_done:
+ if (qu->parent)
+ DLIST_ASSERTON(qu, search, ads->intdone, );
+ else
+ DLIST_ASSERTON(qu, search, ads->output, );
+ break;
+ default:
+ assert(!"specific query state");
+ }
+ }
+}
diff --git a/src/config.h.in b/src/config.h.in
new file mode 100644
index 0000000..bd36686
--- /dev/null
+++ b/src/config.h.in
@@ -0,0 +1,98 @@
+/* src/config.h.in. Generated automatically from configure.in by autoheader 2.13. */
+
+/* Define if inline functions a la GCC are available. */
+#undef HAVE_INLINE
+
+/* Define if function attributes a la GCC 2.5 and higher are available. */
+#undef HAVE_GNUC25_ATTRIB
+
+/* Define if constant functions a la GCC 2.5 and higher are available. */
+#undef HAVE_GNUC25_CONST
+
+/* Define if nonreturning functions a la GCC 2.5 and higher are available. */
+#undef HAVE_GNUC25_NORETURN
+
+/* Define if printf-format argument lists a la GCC are available. */
+#undef HAVE_GNUC25_PRINTFFORMAT
+
+/* Define if we want to include rpc/types.h. Crap BSDs put INADDR_LOOPBACK there. */
+#undef HAVEUSE_RPCTYPES_H
+
+/* Define if you have the poll function. */
+#undef HAVE_POLL
+
+/* Define if you have the nsl library (-lnsl). */
+#undef HAVE_LIBNSL
+
+/* Define if you have the socket library (-lsocket). */
+#undef HAVE_LIBSOCKET
+
+/* Use the definitions: */
+
+#ifndef HAVE_INLINE
+#define inline
+#endif
+
+#ifdef HAVE_POLL
+#include <sys/poll.h>
+#else
+/* kludge it up */
+struct pollfd { int fd; short events; short revents; };
+#define POLLIN 1
+#define POLLPRI 2
+#define POLLOUT 4
+#endif
+
+/* GNU C attributes. */
+#ifndef FUNCATTR
+#ifdef HAVE_GNUC25_ATTRIB
+#define FUNCATTR(x) __attribute__(x)
+#else
+#define FUNCATTR(x)
+#endif
+#endif
+
+/* GNU C printf formats, or null. */
+#ifndef ATTRPRINTF
+#ifdef HAVE_GNUC25_PRINTFFORMAT
+#define ATTRPRINTF(si,tc) format(printf,si,tc)
+#else
+#define ATTRPRINTF(si,tc)
+#endif
+#endif
+#ifndef PRINTFFORMAT
+#define PRINTFFORMAT(si,tc) FUNCATTR((ATTRPRINTF(si,tc)))
+#endif
+
+/* GNU C nonreturning functions, or null. */
+#ifndef ATTRNORETURN
+#ifdef HAVE_GNUC25_NORETURN
+#define ATTRNORETURN noreturn
+#else
+#define ATTRNORETURN
+#endif
+#endif
+#ifndef NONRETURNING
+#define NONRETURNING FUNCATTR((ATTRNORETURN))
+#endif
+
+/* Combination of both the above. */
+#ifndef NONRETURNPRINTFFORMAT
+#define NONRETURNPRINTFFORMAT(si,tc) FUNCATTR((ATTRPRINTF(si,tc),ATTRNORETURN))
+#endif
+
+/* GNU C constant functions, or null. */
+#ifndef ATTRCONST
+#ifdef HAVE_GNUC25_CONST
+#define ATTRCONST const
+#else
+#define ATTRCONST
+#endif
+#endif
+#ifndef CONSTANT
+#define CONSTANT FUNCATTR((ATTRCONST))
+#endif
+
+#ifdef HAVEUSE_RPCTYPES_H
+#include <rpc/types.h>
+#endif
diff --git a/src/dlist.h b/src/dlist.h
new file mode 100644
index 0000000..db95c60
--- /dev/null
+++ b/src/dlist.h
@@ -0,0 +1,53 @@
+/*
+ * dlist.h
+ * - macros for handling doubly linked lists
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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.
+ */
+
+#ifndef ADNS_DLIST_H_INCLUDED
+#define ADNS_DLIST_H_INCLUDED
+
+#define LIST_INIT(list) ((list).head= (list).tail= 0)
+#define LINK_INIT(link) ((link).next= (link).back= 0)
+
+#define LIST_UNLINK_PART(list,node,part) \
+ do { \
+ if ((node)->part back) (node)->part back->part next= (node)->part next; \
+ else (list).head= (node)->part next; \
+ if ((node)->part next) (node)->part next->part back= (node)->part back; \
+ else (list).tail= (node)->part back; \
+ } while(0)
+
+#define LIST_LINK_TAIL_PART(list,node,part) \
+ do { \
+ (node)->part next= 0; \
+ (node)->part back= (list).tail; \
+ if ((list).tail) (list).tail->part next= (node); \
+ else (list).head= (node); \
+ (list).tail= (node); \
+ } while(0)
+
+#define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
+#define LIST_LINK_TAIL(list,node) LIST_LINK_TAIL_PART(list,node,)
+
+#endif
diff --git a/src/event.c b/src/event.c
new file mode 100644
index 0000000..3dd927b
--- /dev/null
+++ b/src/event.c
@@ -0,0 +1,732 @@
+/*
+ * event.c
+ * - event loop core
+ * - TCP connection management
+ * - user-visible check/wait and event-loop-related functions
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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 <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "internal.h"
+#include "tvarith.h"
+
+/* TCP connection management. */
+
+static void tcp_close(adns_state ads) {
+ close(ads->tcpsocket);
+ ads->tcpsocket= -1;
+ ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
+}
+
+void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
+ int serv;
+ adns_query qu;
+
+ assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
+ serv= ads->tcpserver;
+ if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why);
+
+ if (ads->tcpstate == server_connecting) {
+ /* Counts as a retry for all the queries waiting for TCP. */
+ for (qu= ads->tcpw.head; qu; qu= qu->next)
+ qu->retries++;
+ }
+
+ tcp_close(ads);
+ ads->tcpstate= server_broken;
+ ads->tcpserver= (serv+1)%ads->nservers;
+}
+
+static void tcp_connected(adns_state ads, struct timeval now) {
+ adns_query qu, nqu;
+
+ adns__debug(ads,ads->tcpserver,0,"TCP connected");
+ ads->tcpstate= server_ok;
+ for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
+ nqu= qu->next;
+ assert(qu->state == query_tcpw);
+ adns__querysend_tcp(qu,now);
+ }
+}
+
+static void tcp_broken_events(adns_state ads) {
+ adns_query qu, nqu;
+
+ assert(ads->tcpstate == server_broken);
+ for (qu= ads->tcpw.head; qu; qu= nqu) {
+ nqu= qu->next;
+ assert(qu->state == query_tcpw);
+ if (qu->retries > ads->nservers) {
+ LIST_UNLINK(ads->tcpw,qu);
+ adns__query_fail(qu,adns_s_allservfail);
+ }
+ }
+ ads->tcpstate= server_disconnected;
+}
+
+void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
+ int r, fd, tries;
+ adns_rr_addr *addr;
+ struct protoent *proto;
+
+ for (tries=0; tries<ads->nservers; tries++) {
+ switch (ads->tcpstate) {
+ case server_connecting:
+ case server_ok:
+ case server_broken:
+ return;
+ case server_disconnected:
+ break;
+ default:
+ abort();
+ }
+
+ assert(!ads->tcpsend.used);
+ assert(!ads->tcprecv.used);
+ assert(!ads->tcprecv_skip);
+
+ proto= getprotobyname("tcp");
+ if (!proto) {
+ adns__diag(ads,-1,0,"unable to find protocol no. for TCP !");
+ return;
+ }
+ addr = &ads->servers[ads->tcpserver];
+ fd= socket(addr->addr.sa.sa_family, SOCK_STREAM, proto->p_proto);
+ if (fd<0) {
+ adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
+ return;
+ }
+ r= adns__setnonblock(ads,fd);
+ if (r) {
+ adns__diag(ads,-1,0,"cannot make TCP socket nonblocking:"
+ " %s",strerror(r));
+ close(fd);
+ return;
+ }
+ r= connect(fd,&addr->addr.sa,addr->len);
+ ads->tcpsocket= fd;
+ ads->tcpstate= server_connecting;
+ if (r==0) { tcp_connected(ads,now); return; }
+ if (errno == EWOULDBLOCK || errno == EINPROGRESS) {
+ ads->tcptimeout= now;
+ timevaladd(&ads->tcptimeout,TCPCONNMS);
+ return;
+ }
+ adns__tcp_broken(ads,"connect",strerror(errno));
+ tcp_broken_events(ads);
+ }
+}
+
+/* Timeout handling functions. */
+
+void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
+ struct timeval *tv_buf) {
+ const struct timeval *now;
+ int r;
+
+ now= *now_io;
+ if (now) return;
+ r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
+ adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
+ adns_globalsystemfailure(ads);
+ return;
+}
+
+static void inter_immed(struct timeval **tv_io, struct timeval *tvbuf) {
+ struct timeval *rbuf;
+
+ if (!tv_io) return;
+
+ rbuf= *tv_io;
+ if (!rbuf) { *tv_io= rbuf= tvbuf; }
+
+ timerclear(rbuf);
+}
+
+static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval maxto) {
+ struct timeval *rbuf;
+
+ if (!tv_io) return;
+ rbuf= *tv_io;
+ if (!rbuf) {
+ *tvbuf= maxto; *tv_io= tvbuf;
+ } else {
+ if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
+ }
+/*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
+ maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
+}
+
+static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval now, struct timeval maxtime) {
+ /* tv_io may be 0 */
+ ldiv_t dr;
+
+/*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
+ now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
+ if (!tv_io) return;
+ maxtime.tv_sec -= (now.tv_sec+2);
+ maxtime.tv_usec -= (now.tv_usec-2000000);
+ dr= ldiv(maxtime.tv_usec,1000000);
+ maxtime.tv_sec += dr.quot;
+ maxtime.tv_usec -= dr.quot*1000000;
+ if (maxtime.tv_sec<0) timerclear(&maxtime);
+ inter_maxto(tv_io,tvbuf,maxtime);
+}
+
+static void timeouts_queue(adns_state ads, int act,
+ struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval now, struct query_queue *queue) {
+ adns_query qu, nqu;
+
+ for (qu= queue->head; qu; qu= nqu) {
+ nqu= qu->next;
+ if (!timercmp(&now,&qu->timeout,>)) {
+ inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
+ } else {
+ if (!act) { inter_immed(tv_io,tvbuf); return; }
+ LIST_UNLINK(*queue,qu);
+ if (qu->state != query_tosend) {
+ adns__query_fail(qu,adns_s_timeout);
+ } else {
+ adns__query_send(qu,now);
+ }
+ nqu= queue->head;
+ }
+ }
+}
+
+static void tcp_events(adns_state ads, int act,
+ struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval now) {
+ for (;;) {
+ switch (ads->tcpstate) {
+ case server_broken:
+ if (!act) { inter_immed(tv_io,tvbuf); return; }
+ tcp_broken_events(ads);
+ case server_disconnected: /* fall through */
+ if (!ads->tcpw.head) return;
+ if (!act) { inter_immed(tv_io,tvbuf); return; }
+ adns__tcp_tryconnect(ads,now);
+ break;
+ case server_ok:
+ if (ads->tcpw.head) return;
+ if (!ads->tcptimeout.tv_sec) {
+ assert(!ads->tcptimeout.tv_usec);
+ ads->tcptimeout= now;
+ timevaladd(&ads->tcptimeout,TCPIDLEMS);
+ }
+ case server_connecting: /* fall through */
+ if (!act || !timercmp(&now,&ads->tcptimeout,>)) {
+ inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout);
+ return;
+ } {
+ /* TCP timeout has happened */
+ switch (ads->tcpstate) {
+ case server_connecting: /* failed to connect */
+ adns__tcp_broken(ads,"unable to make connection","timed out");
+ break;
+ case server_ok: /* idle timeout */
+ tcp_close(ads);
+ ads->tcpstate= server_disconnected;
+ return;
+ default:
+ abort();
+ }
+ }
+ break;
+ default:
+ abort();
+ }
+ }
+ return;
+}
+
+void adns__timeouts(adns_state ads, int act,
+ struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval now) {
+ timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw);
+ timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw);
+ tcp_events(ads,act,tv_io,tvbuf,now);
+}
+
+void adns_firsttimeout(adns_state ads,
+ struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval now) {
+ adns__consistency(ads,0,cc_entex);
+ adns__timeouts(ads, 0, tv_io,tvbuf, now);
+ adns__returning(ads,0);
+}
+
+void adns_processtimeouts(adns_state ads, const struct timeval *now) {
+ struct timeval tv_buf;
+
+ adns__consistency(ads,0,cc_entex);
+ adns__must_gettimeofday(ads,&now,&tv_buf);
+ if (now) adns__timeouts(ads, 1, 0,0, *now);
+ adns__returning(ads,0);
+}
+
+/* fd handling functions. These are the top-level of the real work of
+ * reception and often transmission.
+ */
+
+int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
+ /* Returns the number of entries filled in. Always zeroes revents. */
+ int nwanted=0;
+#define ADD_POLLFD(wantfd, wantevents) do{ \
+ pollfds_buf[nwanted].fd= (wantfd); \
+ pollfds_buf[nwanted].events= (wantevents); \
+ pollfds_buf[nwanted].revents= 0; \
+ nwanted++; \
+ }while(0)
+
+ int i;
+
+ assert(MAX_POLLFDS == MAXUDP + 1);
+
+ for (i=0; i<ads->nudpsockets; i++)
+ ADD_POLLFD(ads->udpsockets[i].fd, POLLIN);
+
+ switch (ads->tcpstate) {
+ case server_disconnected:
+ case server_broken:
+ break;
+ case server_connecting:
+ ADD_POLLFD(ads->tcpsocket, POLLOUT);
+ break;
+ case server_ok:
+ ADD_POLLFD(ads->tcpsocket,
+ ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI);
+ break;
+ default:
+ abort();
+ }
+ assert(nwanted<=MAX_POLLFDS);
+#undef ADD_POLLFD
+ return nwanted;
+}
+
+int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
+ int want, dgramlen, r, i, udpaddrlen, serv, old_skip;
+ byte udpbuf[DNS_MAXUDP];
+ char addrbuf[ADNS_ADDR2TEXT_BUFLEN];
+ struct udpsocket *udp;
+ adns_sockaddr udpaddr;
+
+ adns__consistency(ads,0,cc_entex);
+
+ switch (ads->tcpstate) {
+ case server_disconnected:
+ case server_broken:
+ case server_connecting:
+ break;
+ case server_ok:
+ if (fd != ads->tcpsocket) break;
+ assert(!ads->tcprecv_skip);
+ do {
+ if (ads->tcprecv.used >= ads->tcprecv_skip+2) {
+ dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) |
+ ads->tcprecv.buf[ads->tcprecv_skip+1]);
+ if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) {
+ old_skip= ads->tcprecv_skip;
+ ads->tcprecv_skip += 2+dgramlen;
+ adns__procdgram(ads, ads->tcprecv.buf+old_skip+2,
+ dgramlen, ads->tcpserver, 1,*now);
+ continue;
+ } else {
+ want= 2+dgramlen;
+ }
+ } else {
+ want= 2;
+ }
+ ads->tcprecv.used -= ads->tcprecv_skip;
+ memmove(ads->tcprecv.buf, ads->tcprecv.buf+ads->tcprecv_skip,
+ ads->tcprecv.used);
+ ads->tcprecv_skip= 0;
+ if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
+ assert(ads->tcprecv.used <= ads->tcprecv.avail);
+ if (ads->tcprecv.used == ads->tcprecv.avail) continue;
+ r= read(ads->tcpsocket,
+ ads->tcprecv.buf+ads->tcprecv.used,
+ ads->tcprecv.avail-ads->tcprecv.used);
+ if (r>0) {
+ ads->tcprecv.used+= r;
+ } else {
+ if (r) {
+ if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
+ if (errno==EINTR) continue;
+ if (errno_resources(errno)) { r= errno; goto xit; }
+ }
+ adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
+ }
+ } while (ads->tcpstate == server_ok);
+ r= 0; goto xit;
+ default:
+ abort();
+ }
+ for (i=0; i<ads->nudpsockets; i++) {
+ udp= &ads->udpsockets[i];
+ if (fd != udp->fd) continue;
+ for (;;) {
+ udpaddrlen= sizeof(udpaddr);
+ r= recvfrom(fd,udpbuf,sizeof(udpbuf),0, &udpaddr.sa,&udpaddrlen);
+ if (r<0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
+ if (errno == EINTR) continue;
+ if (errno_resources(errno)) { r= errno; goto xit; }
+ adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
+ r= 0; goto xit;
+ }
+ for (serv= 0;
+ serv < ads->nservers &&
+ !adns__sockaddrs_equal(&udpaddr.sa,
+ &ads->servers[serv].addr.sa);
+ serv++);
+ if (serv >= ads->nservers) {
+ adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
+ adns__sockaddr_ntoa(&udpaddr.sa, addrbuf));
+ continue;
+ }
+ adns__procdgram(ads,udpbuf,r,serv,0,*now);
+ }
+ break;
+ }
+ r= 0;
+xit:
+ adns__returning(ads,0);
+ return r;
+}
+
+int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
+ int r;
+
+ adns__consistency(ads,0,cc_entex);
+
+ switch (ads->tcpstate) {
+ case server_disconnected:
+ case server_broken:
+ break;
+ case server_connecting:
+ if (fd != ads->tcpsocket) break;
+ assert(ads->tcprecv.used==0);
+ assert(ads->tcprecv_skip==0);
+ for (;;) {
+ if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
+ r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
+ if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
+ tcp_connected(ads,*now);
+ r= 0; goto xit;
+ }
+ if (r>0) {
+ adns__tcp_broken(ads,"connect/read","sent data before first request");
+ r= 0; goto xit;
+ }
+ if (errno==EINTR) continue;
+ if (errno_resources(errno)) { r= errno; goto xit; }
+ adns__tcp_broken(ads,"connect/read",strerror(errno));
+ r= 0; goto xit;
+ } /* not reached */
+ case server_ok:
+ if (fd != ads->tcpsocket) break;
+ while (ads->tcpsend.used) {
+ adns__sigpipe_protect(ads);
+ r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
+ adns__sigpipe_unprotect(ads);
+ if (r<0) {
+ if (errno==EINTR) continue;
+ if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
+ if (errno_resources(errno)) { r= errno; goto xit; }
+ adns__tcp_broken(ads,"write",strerror(errno));
+ r= 0; goto xit;
+ } else if (r>0) {
+ ads->tcpsend.used -= r;
+ memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
+ }
+ }
+ r= 0;
+ goto xit;
+ default:
+ abort();
+ }
+ r= 0;
+xit:
+ adns__returning(ads,0);
+ return r;
+}
+
+int adns_processexceptional(adns_state ads, int fd,
+ const struct timeval *now) {
+ adns__consistency(ads,0,cc_entex);
+ switch (ads->tcpstate) {
+ case server_disconnected:
+ case server_broken:
+ break;
+ case server_connecting:
+ case server_ok:
+ if (fd != ads->tcpsocket) break;
+ adns__tcp_broken(ads,"poll/select","exceptional condition detected");
+ break;
+ default:
+ abort();
+ }
+ adns__returning(ads,0);
+ return 0;
+}
+
+static void fd_event(adns_state ads, int fd,
+ int revent, int pollflag,
+ int maxfd, const fd_set *fds,
+ int (*func)(adns_state, int fd,
+ const struct timeval *now),
+ struct timeval now, int *r_r) {
+ int r;
+
+ if (!(revent & pollflag)) return;
+ if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
+ r= func(ads,fd,&now);
+ if (r) {
+ if (r_r) {
+ *r_r= r;
+ } else {
+ adns__diag(ads,-1,0,"process fd failed after select:"
+ " %s",strerror(errno));
+ adns_globalsystemfailure(ads);
+ }
+ }
+}
+
+void adns__fdevents(adns_state ads,
+ const struct pollfd *pollfds, int npollfds,
+ int maxfd, const fd_set *readfds,
+ const fd_set *writefds, const fd_set *exceptfds,
+ struct timeval now, int *r_r) {
+ int i, fd, revents;
+
+ for (i=0; i<npollfds; i++) {
+ fd= pollfds[i].fd;
+ if (fd >= maxfd) maxfd= fd+1;
+ revents= pollfds[i].revents;
+#define EV(pollfl,fds,how) \
+ fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
+ EV( POLLIN, readfds, readable );
+ EV( POLLOUT, writefds, writeable );
+ EV( POLLPRI, exceptfds, exceptional );
+#undef EV
+ }
+}
+
+/* Wrappers for select(2). */
+
+void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
+ fd_set *writefds_io, fd_set *exceptfds_io,
+ struct timeval **tv_mod, struct timeval *tv_tobuf,
+ const struct timeval *now) {
+ struct timeval tv_nowbuf;
+ struct pollfd pollfds[MAX_POLLFDS];
+ int i, fd, maxfd, npollfds;
+
+ adns__consistency(ads,0,cc_entex);
+
+ if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
+ /* The caller is planning to sleep. */
+ adns__must_gettimeofday(ads,&now,&tv_nowbuf);
+ if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
+ adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
+ }
+
+ npollfds= adns__pollfds(ads,pollfds);
+ maxfd= *maxfd_io;
+ for (i=0; i<npollfds; i++) {
+ fd= pollfds[i].fd;
+ if (fd >= maxfd) maxfd= fd+1;
+ if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
+ if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
+ if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
+ }
+ *maxfd_io= maxfd;
+
+xit:
+ adns__returning(ads,0);
+}
+
+void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
+ const fd_set *writefds, const fd_set *exceptfds,
+ const struct timeval *now) {
+ struct timeval tv_buf;
+ struct pollfd pollfds[MAX_POLLFDS];
+ int npollfds, i;
+
+ adns__consistency(ads,0,cc_entex);
+ adns__must_gettimeofday(ads,&now,&tv_buf);
+ if (!now) goto xit;
+ adns_processtimeouts(ads,now);
+
+ npollfds= adns__pollfds(ads,pollfds);
+ for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
+ adns__fdevents(ads,
+ pollfds,npollfds,
+ maxfd,readfds,writefds,exceptfds,
+ *now, 0);
+xit:
+ adns__returning(ads,0);
+}
+
+/* General helpful functions. */
+
+void adns_globalsystemfailure(adns_state ads) {
+ adns__consistency(ads,0,cc_entex);
+
+ while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail);
+ while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail);
+
+ switch (ads->tcpstate) {
+ case server_connecting:
+ case server_ok:
+ adns__tcp_broken(ads,0,0);
+ break;
+ case server_disconnected:
+ case server_broken:
+ break;
+ default:
+ abort();
+ }
+ adns__returning(ads,0);
+}
+
+int adns_processany(adns_state ads) {
+ int r, i;
+ struct timeval now;
+ struct pollfd pollfds[MAX_POLLFDS];
+ int npollfds;
+
+ adns__consistency(ads,0,cc_entex);
+
+ r= gettimeofday(&now,0);
+ if (!r) adns_processtimeouts(ads,&now);
+
+ /* We just use adns__fdevents to loop over the fd's trying them.
+ * This seems more sensible than calling select, since we're most
+ * likely just to want to do a read on one or two fds anyway.
+ */
+ npollfds= adns__pollfds(ads,pollfds);
+ for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
+ adns__fdevents(ads,
+ pollfds,npollfds,
+ 0,0,0,0,
+ now,&r);
+
+ adns__returning(ads,0);
+ return 0;
+}
+
+void adns__autosys(adns_state ads, struct timeval now) {
+ if (ads->iflags & adns_if_noautosys) return;
+ adns_processany(ads);
+}
+
+int adns__internal_check(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer,
+ void **context_r) {
+ adns_query qu;
+
+ qu= *query_io;
+ if (!qu) {
+ if (ads->output.head) {
+ qu= ads->output.head;
+ } else if (ads->udpw.head || ads->tcpw.head) {
+ return EAGAIN;
+ } else {
+ return ESRCH;
+ }
+ } else {
+ if (qu->id>=0) return EAGAIN;
+ }
+ LIST_UNLINK(ads->output,qu);
+ *answer= qu->answer;
+ if (context_r) *context_r= qu->ctx.ext;
+ *query_io= qu;
+ free(qu);
+ return 0;
+}
+
+int adns_wait(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer_r,
+ void **context_r) {
+ int r, maxfd, rsel;
+ fd_set readfds, writefds, exceptfds;
+ struct timeval tvbuf, *tvp;
+
+ adns__consistency(ads,*query_io,cc_entex);
+ for (;;) {
+ r= adns__internal_check(ads,query_io,answer_r,context_r);
+ if (r != EAGAIN) break;
+ maxfd= 0; tvp= 0;
+ FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
+ adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
+ assert(tvp);
+ rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
+ if (rsel==-1) {
+ if (errno == EINTR) {
+ if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
+ } else {
+ adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
+ adns_globalsystemfailure(ads);
+ }
+ } else {
+ assert(rsel >= 0);
+ adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
+ }
+ }
+ adns__returning(ads,0);
+ return r;
+}
+
+int adns_check(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer_r,
+ void **context_r) {
+ struct timeval now;
+ int r;
+
+ adns__consistency(ads,*query_io,cc_entex);
+ r= gettimeofday(&now,0);
+ if (!r) adns__autosys(ads,now);
+
+ r= adns__internal_check(ads,query_io,answer_r,context_r);
+ adns__returning(ads,0);
+ return r;
+}
diff --git a/src/general.c b/src/general.c
new file mode 100644
index 0000000..b1ae2be
--- /dev/null
+++ b/src/general.c
@@ -0,0 +1,384 @@
+/*
+ * general.c
+ * - diagnostic functions
+ * - vbuf handling
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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 <stdlib.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "internal.h"
+
+/* Core diagnostic functions */
+
+void adns__vlprintf(adns_state ads, const char *fmt, va_list al) {
+ ads->logfn(ads,ads->logfndata,fmt,al);
+}
+
+void adns__lprintf(adns_state ads, const char *fmt, ...) {
+ va_list al;
+ va_start(al,fmt);
+ adns__vlprintf(ads,fmt,al);
+ va_end(al);
+}
+
+void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent,
+ int serv, adns_query qu, const char *fmt, va_list al) {
+ char buf[ADNS_ADDR2TEXT_BUFLEN];
+ const char *bef, *aft;
+ vbuf vb;
+
+ if (!ads->logfn ||
+ (!(ads->iflags & adns_if_debug)
+ && (!prevent || (ads->iflags & prevent))))
+ return;
+
+ if (ads->iflags & adns_if_logpid) {
+ adns__lprintf(ads,"adns%s [%ld]: ",pfx,(long)getpid());
+ } else {
+ adns__lprintf(ads,"adns%s: ",pfx);
+ }
+
+ adns__vlprintf(ads,fmt,al);
+
+ bef= " (";
+ aft= "\n";
+
+ if (qu && qu->query_dgram) {
+ adns__vbuf_init(&vb);
+ adns__lprintf(ads,"%sQNAME=%s, QTYPE=%s",
+ bef,
+ adns__diag_domain(qu->ads,-1,0, &vb,
+ qu->query_dgram,qu->query_dglen,DNS_HDRSIZE),
+ qu->typei ? qu->typei->rrtname : "<unknown>");
+ if (qu->typei && qu->typei->fmtname)
+ adns__lprintf(ads,"(%s)",qu->typei->fmtname);
+ bef=", "; aft=")\n";
+ adns__vbuf_free(&vb);
+ }
+
+ if (serv>=0) {
+ adns__lprintf(ads,"%sNS=%s",bef,
+ adns__sockaddr_ntoa(&ads->servers[serv].addr.sa, buf));
+ bef=", "; aft=")\n";
+ }
+
+ adns__lprintf(ads,"%s",aft);
+}
+
+void adns__debug(adns_state ads, int serv, adns_query qu,
+ const char *fmt, ...) {
+ va_list al;
+
+ va_start(al,fmt);
+ adns__vdiag(ads," debug",0,serv,qu,fmt,al);
+ va_end(al);
+}
+
+void adns__warn(adns_state ads, int serv, adns_query qu,
+ const char *fmt, ...) {
+ va_list al;
+
+ va_start(al,fmt);
+ adns__vdiag(ads," warning",
+ adns_if_noerrprint|adns_if_noserverwarn, serv,qu,fmt,al);
+ va_end(al);
+}
+
+void adns__diag(adns_state ads, int serv, adns_query qu,
+ const char *fmt, ...) {
+ va_list al;
+
+ va_start(al,fmt);
+ adns__vdiag(ads,"",adns_if_noerrprint,serv,qu,fmt,al);
+ va_end(al);
+}
+
+/* vbuf functions */
+
+void adns__vbuf_init(vbuf *vb) {
+ vb->used= vb->avail= 0; vb->buf= 0;
+}
+
+int adns__vbuf_ensure(vbuf *vb, int want) {
+ void *nb;
+
+ if (vb->avail >= want) return 1;
+ nb= realloc(vb->buf,want); if (!nb) return 0;
+ vb->buf= nb;
+ vb->avail= want;
+ return 1;
+}
+
+void adns__vbuf_appendq(vbuf *vb, const byte *data, int len) {
+ memcpy(vb->buf+vb->used,data,len);
+ vb->used+= len;
+}
+
+int adns__vbuf_append(vbuf *vb, const byte *data, int len) {
+ int newlen;
+ void *nb;
+
+ newlen= vb->used+len;
+ if (vb->avail < newlen) {
+ if (newlen<20) newlen= 20;
+ newlen <<= 1;
+ nb= realloc(vb->buf,newlen);
+ if (!nb) { newlen= vb->used+len; nb= realloc(vb->buf,newlen); }
+ if (!nb) return 0;
+ vb->buf= nb;
+ vb->avail= newlen;
+ }
+ adns__vbuf_appendq(vb,data,len);
+ return 1;
+}
+
+int adns__vbuf_appendstr(vbuf *vb, const char *data) {
+ int l;
+ l= strlen(data);
+ return adns__vbuf_append(vb,data,l);
+}
+
+void adns__vbuf_free(vbuf *vb) {
+ free(vb->buf);
+ adns__vbuf_init(vb);
+}
+
+/* Additional diagnostic functions */
+
+const char *adns__diag_domain(adns_state ads, int serv, adns_query qu,
+ vbuf *vb, const byte *dgram,
+ int dglen, int cbyte) {
+ adns_status st;
+
+ st= adns__parse_domain(ads,serv,qu,vb, pdf_quoteok,
+ dgram,dglen,&cbyte,dglen);
+ if (st == adns_s_nomemory) {
+ return "<cannot report domain... out of memory>";
+ }
+ if (st) {
+ vb->used= 0;
+ if (!(adns__vbuf_appendstr(vb,"<bad format... ") &&
+ adns__vbuf_appendstr(vb,adns_strerror(st)) &&
+ adns__vbuf_appendstr(vb,">") &&
+ adns__vbuf_append(vb,"",1))) {
+ return "<cannot report bad format... out of memory>";
+ }
+ }
+ if (!vb->used) {
+ adns__vbuf_appendstr(vb,"<truncated ...>");
+ adns__vbuf_append(vb,"",1);
+ }
+ return vb->buf;
+}
+
+int adns__getrrsz_default(const typeinfo *typei, adns_rrtype type)
+ { return typei->fixed_rrsz; }
+
+adns_status adns_rr_info(adns_rrtype type,
+ const char **rrtname_r, const char **fmtname_r,
+ int *len_r,
+ const void *datap, char **data_r) {
+ const typeinfo *typei;
+ vbuf vb;
+ adns_status st;
+
+ typei= adns__findtype(type);
+ if (!typei) return adns_s_unknownrrtype;
+
+ if (rrtname_r) *rrtname_r= typei->rrtname;
+ if (fmtname_r) *fmtname_r= typei->fmtname;
+ if (len_r) *len_r= typei->getrrsz(typei, type);
+
+ if (!datap) return adns_s_ok;
+
+ adns__vbuf_init(&vb);
+ st= typei->convstring(&vb,datap);
+ if (st) goto x_freevb;
+ if (!adns__vbuf_append(&vb,"",1)) { st= adns_s_nomemory; goto x_freevb; }
+ assert(strlen(vb.buf) == vb.used-1);
+ *data_r= realloc(vb.buf,vb.used);
+ if (!*data_r) *data_r= vb.buf;
+ return adns_s_ok;
+
+ x_freevb:
+ adns__vbuf_free(&vb);
+ return st;
+}
+
+
+#define SINFO(n,s) { adns_s_##n, #n, s }
+
+static const struct sinfo {
+ adns_status st;
+ const char *abbrev;
+ const char *string;
+} sinfos[]= {
+ SINFO( ok, "OK" ),
+
+ SINFO( nomemory, "Out of memory" ),
+ SINFO( unknownrrtype, "Query not implemented in DNS library" ),
+ SINFO( systemfail, "General resolver or system failure" ),
+
+ SINFO( timeout, "DNS query timed out" ),
+ SINFO( allservfail, "All nameservers failed" ),
+ SINFO( norecurse, "Recursion denied by nameserver" ),
+ SINFO( invalidresponse, "Nameserver sent bad response" ),
+ SINFO( unknownformat, "Nameserver used unknown format" ),
+
+ SINFO( rcodeservfail, "Nameserver reports failure" ),
+ SINFO( rcodeformaterror, "Query not understood by nameserver" ),
+ SINFO( rcodenotimplemented, "Query not implemented by nameserver" ),
+ SINFO( rcoderefused, "Query refused by nameserver" ),
+ SINFO( rcodeunknown, "Nameserver sent unknown response code" ),
+
+ SINFO( inconsistent, "Inconsistent resource records in DNS" ),
+ SINFO( prohibitedcname, "DNS alias found where canonical name wanted" ),
+ SINFO( answerdomaininvalid, "Found syntactically invalid domain name" ),
+ SINFO( answerdomaintoolong, "Found overly-long domain name" ),
+ SINFO( invaliddata, "Found invalid DNS data" ),
+
+ SINFO( querydomainwrong, "Domain invalid for particular DNS query type" ),
+ SINFO( querydomaininvalid, "Domain name is syntactically invalid" ),
+ SINFO( querydomaintoolong, "Domain name or component is too long" ),
+
+ SINFO( nxdomain, "No such domain" ),
+ SINFO( nodata, "No such data" )
+};
+
+static int si_compar(const void *key, const void *elem) {
+ const adns_status *st= key;
+ const struct sinfo *si= elem;
+
+ return *st < si->st ? -1 : *st > si->st ? 1 : 0;
+}
+
+static const struct sinfo *findsinfo(adns_status st) {
+ return bsearch(&st,sinfos, sizeof(sinfos)/sizeof(*sinfos),
+ sizeof(*sinfos), si_compar);
+}
+
+const char *adns_strerror(adns_status st) {
+ const struct sinfo *si;
+
+ si= findsinfo(st);
+ return si->string;
+}
+
+const char *adns_errabbrev(adns_status st) {
+ const struct sinfo *si;
+
+ si= findsinfo(st);
+ return si->abbrev;
+}
+
+
+#define STINFO(max) { adns_s_max_##max, #max }
+
+static const struct stinfo {
+ adns_status stmax;
+ const char *abbrev;
+} stinfos[]= {
+ { adns_s_ok, "ok" },
+ STINFO( localfail ),
+ STINFO( remotefail ),
+ STINFO( tempfail ),
+ STINFO( misconfig ),
+ STINFO( misquery ),
+ STINFO( permfail )
+};
+
+static int sti_compar(const void *key, const void *elem) {
+ const adns_status *st= key;
+ const struct stinfo *sti= elem;
+
+ adns_status here, min, max;
+
+ here= *st;
+ min= (sti==stinfos) ? 0 : sti[-1].stmax+1;
+ max= sti->stmax;
+
+ return here < min ? -1 : here > max ? 1 : 0;
+}
+
+const char *adns_errtypeabbrev(adns_status st) {
+ const struct stinfo *sti;
+
+ sti= bsearch(&st,stinfos, sizeof(stinfos)/sizeof(*stinfos),
+ sizeof(*stinfos), sti_compar);
+ return sti->abbrev;
+}
+
+
+void adns__isort(void *array, int nobjs, int sz, void *tempbuf,
+ int (*needswap)(void *context, const void *a, const void *b),
+ void *context) {
+ byte *data= array;
+ int i, place;
+
+ for (i=0; i<nobjs; i++) {
+ for (place= i;
+ place>0 && needswap(context, data + (place-1)*sz, data + i*sz);
+ place--);
+ if (place != i) {
+ memcpy(tempbuf, data + i*sz, sz);
+ memmove(data + (place+1)*sz, data + place*sz, (i-place)*sz);
+ memcpy(data + place*sz, tempbuf, sz);
+ }
+ }
+}
+
+/* SIGPIPE protection. */
+
+void adns__sigpipe_protect(adns_state ads) {
+ sigset_t toblock;
+ struct sigaction sa;
+ int r;
+
+ if (ads->iflags & adns_if_nosigpipe) return;
+
+ sigfillset(&toblock);
+ sigdelset(&toblock,SIGPIPE);
+
+ sa.sa_handler= SIG_IGN;
+ sigfillset(&sa.sa_mask);
+ sa.sa_flags= 0;
+
+ r= sigprocmask(SIG_SETMASK,&toblock,&ads->stdsigmask); assert(!r);
+ r= sigaction(SIGPIPE,&sa,&ads->stdsigpipe); assert(!r);
+}
+
+void adns__sigpipe_unprotect(adns_state ads) {
+ int r;
+
+ if (ads->iflags & adns_if_nosigpipe) return;
+
+ r= sigaction(SIGPIPE,&ads->stdsigpipe,0); assert(!r);
+ r= sigprocmask(SIG_SETMASK,&ads->stdsigmask,0); assert(!r);
+}
diff --git a/src/internal.h b/src/internal.h
new file mode 100644
index 0000000..9334a6f
--- /dev/null
+++ b/src/internal.h
@@ -0,0 +1,947 @@
+/*
+ * internal.h
+ * - declarations of private objects with external linkage (adns__*)
+ * - definitons of internal macros
+ * - comments regarding library data structures
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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.
+ */
+
+#ifndef ADNS_INTERNAL_H_INCLUDED
+#define ADNS_INTERNAL_H_INCLUDED
+
+#include "config.h"
+typedef unsigned char byte;
+
+#include <stdarg.h>
+#include <assert.h>
+#include <unistd.h>
+#include <signal.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include <sys/time.h>
+
+#define ADNS_FEATURE_MANYAF
+#include "adns.h"
+#include "dlist.h"
+
+#ifdef ADNS_REGRESS_TEST
+# include "hredirect.h"
+#endif
+
+/* Configuration and constants */
+
+#define MAXSERVERS 5
+#define MAXSORTLIST 15
+#define UDPMAXRETRIES 15
+#define UDPRETRYMS 2000
+#define TCPWAITMS 30000
+#define TCPCONNMS 14000
+#define TCPIDLEMS 30000
+#define MAXTTLBELIEVE (7*86400) /* any TTL > 7 days is capped */
+
+#define DNS_PORT 53
+#define DNS_MAXUDP 512
+#define DNS_MAXLABEL 63
+#define DNS_MAXDOMAIN 255
+#define DNS_HDRSIZE 12
+#define DNS_IDOFFSET 0
+#define DNS_CLASS_IN 1
+
+#define MAX_POLLFDS ADNS_POLLFDS_RECOMMENDED
+
+/* Some preprocessor hackery */
+
+#define GLUE(x, y) GLUE_(x, y)
+#define GLUE_(x, y) x##y
+
+/* C99 macro `...' must match at least one argument, so the naive definition
+ * `#define CAR(car, ...) car' won't work. But it's easy to arrange for the
+ * tail to be nonempty if we're just going to discard it anyway. */
+#define CAR(...) CAR_(__VA_ARGS__, _)
+#define CAR_(car, ...) car
+
+/* Extracting the tail of an argument list is rather more difficult. The
+ * following trick is based on one by Laurent Deniau to count the number of
+ * arguments to a macro, simplified in two ways: (a) it only handles up to
+ * eight arguments, and (b) it only needs to distinguish the one-argument
+ * case from many arguments. */
+#define CDR(...) CDR_(__VA_ARGS__, m, m, m, m, m, m, m, 1, _)(__VA_ARGS__)
+#define CDR_(_1, _2, _3, _4, _5, _6, _7, _8, n, ...) CDR_##n
+#define CDR_1(_)
+#define CDR_m(_, ...) __VA_ARGS__
+
+typedef enum {
+ cc_user,
+ cc_entex,
+ cc_freq
+} consistency_checks;
+
+typedef enum {
+ rcode_noerror,
+ rcode_formaterror,
+ rcode_servfail,
+ rcode_nxdomain,
+ rcode_notimp,
+ rcode_refused
+} dns_rcode;
+
+enum {
+ adns__qf_addr_answer= 0x01000000,/* addr query received an answer */
+ adns__qf_addr_cname = 0x02000000 /* addr subquery performed on cname */
+};
+
+/* Shared data structures */
+
+typedef struct {
+ int used, avail;
+ byte *buf;
+} vbuf;
+
+typedef struct {
+ adns_state ads;
+ adns_query qu;
+ int serv;
+ const byte *dgram;
+ int dglen, nsstart, nscount, arcount;
+ struct timeval now;
+} parseinfo;
+
+#define MAXREVLABELS 34 /* keep in sync with addrfam! */
+struct revparse_state {
+ uint16_t labstart[MAXREVLABELS];
+ uint8_t lablen[MAXREVLABELS];
+};
+
+union checklabel_state {
+ struct revparse_state ptr;
+};
+
+typedef struct {
+ void *ext;
+ void (*callback)(adns_query parent, adns_query child);
+
+ union {
+ struct {
+ adns_rrtype rev_rrtype;
+ adns_sockaddr addr;
+ } ptr;
+ struct {
+ unsigned want, have;
+ } addr;
+ } tinfo; /* type-specific state for the query itself: zero-init if you
+ * don't know better. */
+
+ union {
+ adns_rr_hostaddr *hostaddr;
+ } pinfo; /* state for use by parent's callback function */
+} qcontext;
+
+typedef struct typeinfo {
+ adns_rrtype typekey;
+ const char *rrtname;
+ const char *fmtname;
+ int fixed_rrsz;
+
+ void (*makefinal)(adns_query qu, void *data);
+ /* Change memory management of *data.
+ * Previously, used alloc_interim, now use alloc_final.
+ */
+
+ adns_status (*convstring)(vbuf *vb, const void *data);
+ /* Converts the RR data to a string representation in vbuf.
+ * vbuf will be appended to (it must have been initialised),
+ * and will not be null-terminated by convstring.
+ */
+
+ adns_status (*parse)(const parseinfo *pai, int cbyte,
+ int max, void *store_r);
+ /* Parse one RR, in dgram of length dglen, starting at cbyte and
+ * extending until at most max.
+ *
+ * The RR should be stored at *store_r, of length qu->typei->getrrsz().
+ *
+ * If there is an overrun which might indicate truncation, it should set
+ * *rdstart to -1; otherwise it may set it to anything else positive.
+ *
+ * nsstart is the offset of the authority section.
+ */
+
+ int (*diff_needswap)(adns_state ads,const void *datap_a,const void *datap_b);
+ /* Returns !0 if RR a should be strictly after RR b in the sort order,
+ * 0 otherwise. Must not fail.
+ */
+
+ adns_status (*checklabel)(adns_state ads, adns_queryflags flags,
+ union checklabel_state *cls, qcontext *ctx,
+ int labnum, const char *dgram,
+ int labstart, int lablen);
+ /* Check a label from the query domain string. The label is not
+ * necessarily null-terminated. The hook can refuse the query's submission
+ * by returning a nonzero status. State can be stored in *cls between
+ * calls, and useful information can be stashed in ctx->tinfo, to be stored
+ * with the query (e.g., it will be available to the parse hook). This
+ * hook can detect a first call because labnum is zero, and a final call
+ * because lablen is zero.
+ */
+
+ void (*postsort)(adns_state ads, void *array, int nrrs, int rrsz,
+ const struct typeinfo *typei);
+ /* Called immediately after the RRs have been sorted, and may rearrange
+ * them. (This is really for the benefit of SRV's bizarre weighting
+ * stuff.) May be 0 to mean nothing needs to be done.
+ */
+
+ int (*getrrsz)(const struct typeinfo *typei, adns_rrtype type);
+ /* Return the output resource-record element size; if this is null, then
+ * the rrsz member can be used.
+ */
+
+ void (*query_send)(adns_query qu, struct timeval now);
+ /* Send the query to nameservers, and hook it into the appropriate queue.
+ * Normal behaviour is to call adns__query_send, but this can be overridden
+ * for special effects.
+ */
+} typeinfo;
+
+adns_status adns__ckl_hostname(adns_state ads, adns_queryflags flags,
+ union checklabel_state *cls,
+ qcontext *ctx, int labnum,
+ const char *dgram, int labstart, int lablen);
+ /* implemented in query.c, used by types.c as default
+ * and as part of implementation for some fancier types
+ * doesn't require any state */
+
+typedef struct allocnode {
+ struct allocnode *next, *back;
+ size_t sz;
+} allocnode;
+
+union maxalign {
+ byte d[1];
+ struct in_addr ia;
+ long l;
+ void *p;
+ void (*fp)(void);
+ union maxalign *up;
+} data;
+
+struct adns__query {
+ adns_state ads;
+ enum { query_tosend, query_tcpw, query_childw, query_done } state;
+ adns_query back, next, parent;
+ struct { adns_query head, tail; } children;
+ struct { adns_query back, next; } siblings;
+ struct { allocnode *head, *tail; } allocations;
+ int interim_allocd, preserved_allocd;
+ void *final_allocspace;
+
+ const typeinfo *typei;
+ byte *query_dgram;
+ int query_dglen;
+
+ vbuf vb;
+ /* General-purpose messing-about buffer.
+ * Wherever a `big' interface is crossed, this may be corrupted/changed
+ * unless otherwise specified.
+ */
+
+ adns_answer *answer;
+ /* This is allocated when a query is submitted, to avoid being unable
+ * to relate errors to queries if we run out of memory. During
+ * query processing status, rrs is 0. cname is set if
+ * we found a cname (this corresponds to cname_dgram in the query
+ * structure). type is set from the word go. nrrs and rrs
+ * are set together, when we find how many rrs there are.
+ * owner is set during querying unless we're doing searchlist,
+ * in which case it is set only when we find an answer.
+ */
+
+ byte *cname_dgram;
+ int cname_dglen, cname_begin;
+ /* If non-0, has been allocated using . */
+
+ vbuf search_vb;
+ int search_origlen, search_pos, search_doneabs;
+ /* Used by the searching algorithm. The query domain in textual form
+ * is copied into the vbuf, and _origlen set to its length. Then
+ * we walk the searchlist, if we want to. _pos says where we are
+ * (next entry to try), and _doneabs says whether we've done the
+ * absolute query yet (0=not yet, 1=done, -1=must do straight away,
+ * but not done yet). If flags doesn't have adns_qf_search then
+ * the vbuf is initialised but empty and everything else is zero.
+ */
+
+ int id, flags, retries;
+ int udpnextserver;
+ unsigned long udpsent; /* bitmap indexed by server */
+ struct timeval timeout;
+ time_t expires; /* Earliest expiry time of any record we used. */
+
+ qcontext ctx;
+
+ /* Possible states:
+ *
+ * state Queue child id nextudpserver udpsent tcpfailed
+ *
+ * tosend NONE null >=0 0 zero zero
+ * tosend udpw null >=0 any nonzero zero
+ * tosend NONE null >=0 any nonzero zero
+ *
+ * tcpw tcpw null >=0 irrelevant any any
+ *
+ * child childw set >=0 irrelevant irrelevant irrelevant
+ * child NONE null >=0 irrelevant irrelevant irrelevant
+ * done output null -1 irrelevant irrelevant irrelevant
+ *
+ * Queries are only not on a queue when they are actually being processed.
+ * Queries in state tcpw/tcpw have been sent (or are in the to-send buffer)
+ * iff the tcp connection is in state server_ok.
+ *
+ * Internal queries (from adns__submit_internal) end up on intdone
+ * instead of output, and the callbacks are made on the way out of
+ * adns, to avoid reentrancy hazards.
+ *
+ * +------------------------+
+ * START -----> | tosend/NONE |
+ * +------------------------+
+ * / |\ \
+ * too big for UDP / UDP timeout \ \ send via UDP
+ * send via TCP / more retries \ \
+ * when conn'd / desired \ \
+ * | | |
+ * v | v
+ * +-----------+ +-------------+
+ * | tcpw/tcpw | ________ | tosend/udpw |
+ * +-----------+ \ +-------------+
+ * | | | UDP timeout | |
+ * | | | no more | |
+ * | | | retries | |
+ * \ | TCP died | desired | |
+ * \ \ no more | | |
+ * \ \ servers | TCP / |
+ * \ \ to try | timeout / |
+ * got \ \ v |_ | got
+ * reply \ _| +------------------+ / reply
+ * \ | done/output FAIL | /
+ * \ +------------------+ /
+ * \ /
+ * _| |_
+ * (..... got reply ....)
+ * / \
+ * need child query/ies / \ no child query
+ * / \
+ * |_ _|
+ * +---------------+ +----------------+
+ * | childw/childw | ----------------> | done/output OK |
+ * +---------------+ children done +----------------+
+ */
+};
+
+struct query_queue { adns_query head, tail; };
+
+#define MAXUDP 2
+
+struct adns__state {
+ adns_initflags iflags;
+ adns_logcallbackfn *logfn;
+ void *logfndata;
+ int configerrno;
+ struct query_queue udpw, tcpw, childw, output, intdone;
+ adns_query forallnext;
+ int nextid, tcpsocket;
+ struct udpsocket { int af; int fd; } udpsockets[MAXUDP];
+ int nudpsockets;
+ vbuf tcpsend, tcprecv;
+ int nservers, nsortlist, nsearchlist, searchndots, tcpserver, tcprecv_skip;
+ enum adns__tcpstate {
+ server_disconnected, server_connecting,
+ server_ok, server_broken
+ } tcpstate;
+ struct timeval tcptimeout;
+ /* This will have tv_sec==0 if it is not valid. It will always be
+ * valid if tcpstate _connecting. When _ok, it will be nonzero if
+ * we are idle (ie, tcpw queue is empty), in which case it is the
+ * absolute time when we will close the connection.
+ */
+ struct sigaction stdsigpipe;
+ sigset_t stdsigmask;
+ struct pollfd pollfds_buf[MAX_POLLFDS];
+ adns_rr_addr servers[MAXSERVERS];
+ struct sortlist {
+ adns_sockaddr base, mask;
+ } sortlist[MAXSORTLIST];
+ char **searchlist;
+ unsigned config_report_unknown:1;
+ unsigned short rand48xsubi[3];
+};
+
+/* From addrfam.c: */
+
+extern int adns__addrs_equal_raw(const struct sockaddr *a,
+ int bf, const void *b);
+/* Returns nonzero a's family is bf and a's protocol address field
+ * refers to the same protocol address as that stored at ba.
+ */
+
+extern int adns__addrs_equal(const adns_sockaddr *a,
+ const adns_sockaddr *b);
+/* Returns nonzero if the two refer to the same protocol address
+ * (disregarding port, IPv6 scope, etc).
+ */
+
+extern int adns__sockaddrs_equal(const struct sockaddr *sa,
+ const struct sockaddr *sb);
+/* Return nonzero if the two socket addresses are equal (in all significant
+ * respects).
+ */
+
+extern int adns__addr_width(int af);
+/* Return the width of addresses of family af, in bits. */
+
+extern void adns__prefix_mask(adns_sockaddr *sa, int len);
+/* Stores in sa's protocol address field an address mask for address
+ * family af, whose first len bits are set and the remainder are
+ * clear. On entry, sa's af field must be set. This is what you want
+ * for converting a prefix length into a netmask.
+ */
+
+extern int adns__guess_prefix_length(const adns_sockaddr *addr);
+/* Given a network base address, guess the appropriate prefix length based on
+ * the appropriate rules for the address family (e.g., for IPv4, this uses
+ * the old address classes).
+ */
+
+extern int adns__addr_matches(int af, const void *addr,
+ const adns_sockaddr *base,
+ const adns_sockaddr *mask);
+/* Return nonzero if the protocol address specified by af and addr
+ * lies within the network specified by base and mask.
+ */
+
+extern void adns__addr_inject(const void *a, adns_sockaddr *sa);
+/* Injects the protocol address *a into the socket adress sa. Assumes
+ * that sa->sa_family is already set correctly.
+ */
+
+extern const void *adns__sockaddr_addr(const struct sockaddr *sa);
+/* Returns the address of the protocol address field in sa.
+ */
+
+char *adns__sockaddr_ntoa(const struct sockaddr *sa, char *buf);
+/* Converts sa to a string, and writes it to buf, which must be at
+ * least ADNS_ADDR2TEXT_BUFLEN bytes long (unchecked). Returns buf;
+ * can't fail.
+ */
+
+extern int adns__make_reverse_domain(const struct sockaddr *sa,
+ const char *zone,
+ char **buf_io, size_t bufsz,
+ char **buf_free_r);
+/* Construct a reverse domain string, given a socket address and a parent
+ * zone. If zone is null, then use the standard reverse-lookup zone for the
+ * address family. If the length of the resulting string is no larger than
+ * bufsz, then the result is stored starting at *buf_io; otherwise a new
+ * buffer is allocated is used, and a pointer to it is stored in both *buf_io
+ * and *buf_free_r (the latter of which should be null on entry). If
+ * something goes wrong, then an errno value is returned: ENOSYS if the
+ * address family of sa isn't recognized, or ENOMEM if the attempt to
+ * allocate an output buffer failed.
+ */
+
+extern bool adns__revparse_label(struct revparse_state *rps, int labnum,
+ const char *dgram,
+ int labstart, int lablen);
+/* Parse a label in a reverse-domain name, given its index labnum (starting
+ * from zero), a pointer to its contents (which need not be null-terminated),
+ * and its length. The state in *rps is initialized implicitly when labnum
+ * is zero.
+ *
+ * Returns 1 if the parse is proceeding successfully, 0 if the domain
+ * name is definitely invalid and the parse must be abandoned.
+ */
+
+extern bool adns__revparse_done(struct revparse_state *rps,
+ const char *dgram, int nlabels,
+ adns_rrtype *rrtype_r, adns_sockaddr *addr_r);
+/* Finishes parsing a reverse-domain name, given the total number of
+ * labels in the name. On success, fills in the af and protocol
+ * address in *addr_r, and the forward query type in *rrtype_r
+ * (because that turns out to be useful). Returns 1 if the parse
+ * was successful.
+ */
+
+/* From setup.c: */
+
+int adns__setnonblock(adns_state ads, int fd); /* => errno value */
+
+/* From general.c: */
+
+void adns__vlprintf(adns_state ads, const char *fmt, va_list al);
+void adns__lprintf(adns_state ads, const char *fmt,
+ ...) PRINTFFORMAT(2,3);
+
+void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent,
+ int serv, adns_query qu, const char *fmt, va_list al);
+
+void adns__debug(adns_state ads, int serv, adns_query qu,
+ const char *fmt, ...) PRINTFFORMAT(4,5);
+void adns__warn(adns_state ads, int serv, adns_query qu,
+ const char *fmt, ...) PRINTFFORMAT(4,5);
+void adns__diag(adns_state ads, int serv, adns_query qu,
+ const char *fmt, ...) PRINTFFORMAT(4,5);
+
+int adns__vbuf_ensure(vbuf *vb, int want);
+int adns__vbuf_appendstr(vbuf *vb, const char *data); /* doesn't include nul */
+int adns__vbuf_append(vbuf *vb, const byte *data, int len);
+/* 1=>success, 0=>realloc failed */
+void adns__vbuf_appendq(vbuf *vb, const byte *data, int len);
+void adns__vbuf_init(vbuf *vb);
+void adns__vbuf_free(vbuf *vb);
+
+const char *adns__diag_domain(adns_state ads, int serv, adns_query qu,
+ vbuf *vb,
+ const byte *dgram, int dglen, int cbyte);
+/* Unpicks a domain in a datagram and returns a string suitable for
+ * printing it as. Never fails - if an error occurs, it will
+ * return some kind of string describing the error.
+ *
+ * serv may be -1 and qu may be 0. vb must have been initialised,
+ * and will be left in an arbitrary consistent state.
+ *
+ * Returns either vb->buf, or a pointer to a string literal. Do not modify
+ * vb before using the return value.
+ */
+
+int adns__getrrsz_default(const typeinfo *typei, adns_rrtype type);
+/* Default function for the `getrrsz' type hook; returns the `fixed_rrsz'
+ * value from the typeinfo entry.
+ */
+
+void adns__isort(void *array, int nobjs, int sz, void *tempbuf,
+ int (*needswap)(void *context, const void *a, const void *b),
+ void *context);
+/* Does an insertion sort of array which must contain nobjs objects
+ * each sz bytes long. tempbuf must point to a buffer at least
+ * sz bytes long. needswap should return !0 if a>b (strictly, ie
+ * wrong order) 0 if a<=b (ie, order is fine).
+ */
+
+void adns__sigpipe_protect(adns_state);
+void adns__sigpipe_unprotect(adns_state);
+/* If SIGPIPE protection is not disabled, will block all signals except
+ * SIGPIPE, and set SIGPIPE's disposition to SIG_IGN. (And then restore.)
+ * Each call to _protect must be followed by a call to _unprotect before
+ * any significant amount of code gets to run, since the old signal mask
+ * is stored in the adns structure.
+ */
+
+/* From transmit.c: */
+
+adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
+ const char *owner, int ol,
+ const typeinfo *typei, adns_rrtype type,
+ adns_queryflags flags);
+/* Assembles a query packet in vb. A new id is allocated and returned.
+ */
+
+adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
+ const byte *qd_dgram, int qd_dglen,
+ int qd_begin,
+ adns_rrtype type, adns_queryflags flags);
+/* Same as adns__mkquery, but takes the owner domain from an existing datagram.
+ * That domain must be correct and untruncated.
+ */
+
+void adns__querysend_tcp(adns_query qu, struct timeval now);
+/* Query must be in state tcpw/tcpw; it will be sent if possible and
+ * no further processing can be done on it for now. The connection
+ * might be broken, but no reconnect will be attempted.
+ */
+
+struct udpsocket *adns__udpsocket_by_af(adns_state ads, int af);
+/* Find the UDP socket structure in ads which has the given address family.
+ * Return null if there isn't one.
+ *
+ * This is used during initialization, so ads is only partially filled in.
+ * The requirements are that nudp is set, and that udpsocket[i].af are
+ * defined for 0<=i<nudp.
+ */
+
+void adns__query_send(adns_query qu, struct timeval now);
+/* Query must be in state tosend/NONE; it will be moved to a new state,
+ * and no further processing can be done on it for now.
+ * (Resulting state is one of udp/timew, tcpwait/timew (if server not
+ * connected), tcpsent/timew, child/childw or done/output.)
+ * __query_send may decide to use either UDP or TCP depending whether
+ * _qf_usevc is set (or has become set) and whether the query is too
+ * large.
+ */
+
+/* From query.c: */
+
+adns_status adns__internal_submit(adns_state ads, adns_query *query_r,
+ adns_query parent,
+ const typeinfo *typei, adns_rrtype type,
+ vbuf *qumsg_vb, int id,
+ adns_queryflags flags, struct timeval now,
+ qcontext *ctx);
+/* Submits a query (for internal use, called during external submits).
+ *
+ * The new query is returned in *query_r, or we return adns_s_nomemory.
+ *
+ * The query datagram should already have been assembled in qumsg_vb;
+ * the memory for it is _taken over_ by this routine whether it
+ * succeeds or fails (if it succeeds, the vbuf is reused for qu->vb).
+ *
+ * *ctx is copied byte-for-byte into the query. Before doing this, its tinfo
+ * field may be modified by type hooks.
+ *
+ * When the child query is done, ctx->callback will be called. The
+ * child will already have been taken off both the global list of
+ * queries in ads and the list of children in the parent. The child
+ * will be freed when the callback returns. The parent will have been
+ * taken off the global childw queue.
+ *
+ * The callback should either call adns__query_done, if it is
+ * complete, or adns__query_fail, if an error has occurred, in which
+ * case the other children (if any) will be cancelled. If the parent
+ * has more unfinished children (or has just submitted more) then the
+ * callback may choose to wait for them - it must then put the parent
+ * back on the childw queue.
+ */
+
+void adns__search_next(adns_state ads, adns_query qu, struct timeval now);
+/* Walks down the searchlist for a query with adns_qf_search.
+ * The query should have just had a negative response, or not had
+ * any queries sent yet, and should not be on any queue.
+ * The query_dgram if any will be freed and forgotten and a new
+ * one constructed from the search_* members of the query.
+ *
+ * Cannot fail (in case of error, calls adns__query_fail).
+ */
+
+void *adns__alloc_interim(adns_query qu, size_t sz);
+void *adns__alloc_preserved(adns_query qu, size_t sz);
+/* Allocates some memory, and records which query it came from
+ * and how much there was.
+ *
+ * If an error occurs in the query, all the memory from _interim is
+ * simply freed. If the query succeeds, one large buffer will be made
+ * which is big enough for all these allocations, and then
+ * adns__alloc_final will get memory from this buffer.
+ *
+ * _alloc_interim can fail (and return 0).
+ * The caller must ensure that the query is failed.
+ *
+ * The memory from _preserved is is kept and transferred into the
+ * larger buffer - unless we run out of memory, in which case it too
+ * is freed. When you use _preserved you have to add code to the
+ * x_nomem error exit case in adns__makefinal_query to clear out the
+ * pointers you made to those allocations, because that's when they're
+ * thrown away; you should also make a note in the declaration of
+ * those pointer variables, to note that they are _preserved rather
+ * than _interim. If they're in the answer, note it here:
+ * answer->cname and answer->owner are _preserved.
+ */
+
+void adns__transfer_interim(adns_query from, adns_query to, void *block);
+/* Transfers an interim allocation from one query to another, so that
+ * the `to' query will have room for the data when we get to makefinal
+ * and so that the free will happen when the `to' query is freed
+ * rather than the `from' query.
+ *
+ * It is legal to call adns__transfer_interim with a null pointer; this
+ * has no effect.
+ *
+ * _transfer_interim also ensures that the expiry time of the `to' query
+ * is no later than that of the `from' query, so that child queries'
+ * TTLs get inherited by their parents.
+ */
+
+void adns__free_interim(adns_query qu, void *p);
+/* Forget about a block allocated by adns__alloc_interim.
+ */
+
+void *adns__alloc_mine(adns_query qu, size_t sz);
+/* Like _interim, but does not record the length for later
+ * copying into the answer. This just ensures that the memory
+ * will be freed when we're done with the query.
+ */
+
+void *adns__alloc_final(adns_query qu, size_t sz);
+/* Cannot fail, and cannot return 0.
+ */
+
+void adns__makefinal_block(adns_query qu, void **blpp, size_t sz);
+void adns__makefinal_str(adns_query qu, char **strp);
+
+void adns__reset_preserved(adns_query qu);
+/* Resets all of the memory management stuff etc. to take account of
+ * only the _preserved stuff from _alloc_preserved. Used when we find
+ * an error somewhere and want to just report the error (with perhaps
+ * CNAME, owner, etc. info), and also when we're halfway through RRs
+ * in a datagram and discover that we need to retry the query.
+ */
+
+void adns__cancel(adns_query qu);
+void adns__query_done(adns_query qu);
+void adns__query_fail(adns_query qu, adns_status st);
+void adns__cancel_children(adns_query qu);
+
+void adns__returning(adns_state ads, adns_query qu);
+/* Must be called before returning from adns any time that we have
+ * progressed (including made, finished or destroyed) queries.
+ *
+ * Might reenter adns via internal query callbacks, so
+ * external-faciing functions which call adns__returning should
+ * normally be avoided in internal code. */
+
+/* From reply.c: */
+
+void adns__procdgram(adns_state ads, const byte *dgram, int len,
+ int serv, int viatcp, struct timeval now);
+/* This function is allowed to cause new datagrams to be constructed
+ * and sent, or even new queries to be started. However,
+ * query-sending functions are not allowed to call any general event
+ * loop functions in case they accidentally call this.
+ *
+ * Ie, receiving functions may call sending functions.
+ * Sending functions may NOT call receiving functions.
+ */
+
+/* From types.c: */
+
+const typeinfo *adns__findtype(adns_rrtype type);
+
+/* From parse.c: */
+
+typedef struct {
+ adns_state ads;
+ adns_query qu;
+ int serv;
+ const byte *dgram;
+ int dglen, max, cbyte, namelen;
+ int *dmend_r;
+} findlabel_state;
+
+void adns__findlabel_start(findlabel_state *fls, adns_state ads,
+ int serv, adns_query qu,
+ const byte *dgram, int dglen, int max,
+ int dmbegin, int *dmend_rlater);
+/* Finds labels in a domain in a datagram.
+ *
+ * Call this routine first.
+ * dmend_rlater may be null. ads (and of course fls) may not be.
+ * serv may be -1, qu may be null - they are for error reporting.
+ */
+
+adns_status adns__findlabel_next(findlabel_state *fls,
+ int *lablen_r, int *labstart_r);
+/* Then, call this one repeatedly.
+ *
+ * It will return adns_s_ok if all is well, and tell you the length
+ * and start of successive labels. labstart_r may be null, but
+ * lablen_r must not be.
+ *
+ * After the last label, it will return with *lablen_r zero.
+ * Do not then call it again; instead, just throw away the findlabel_state.
+ *
+ * *dmend_rlater will have been set to point to the next part of
+ * the datagram after the label (or after the uncompressed part,
+ * if compression was used). *namelen_rlater will have been set
+ * to the length of the domain name (total length of labels plus
+ * 1 for each intervening dot).
+ *
+ * If the datagram appears to be truncated, *lablen_r will be -1.
+ * *dmend_rlater, *labstart_r and *namelen_r may contain garbage.
+ * Do not call _next again.
+ *
+ * There may also be errors, in which case *dmend_rlater,
+ * *namelen_rlater, *lablen_r and *labstart_r may contain garbage.
+ * Do not then call findlabel_next again.
+ */
+
+typedef enum {
+ pdf_quoteok= 0x001
+} parsedomain_flags;
+
+adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu,
+ vbuf *vb, parsedomain_flags flags,
+ const byte *dgram, int dglen, int *cbyte_io,
+ int max);
+/* vb must already have been initialised; it will be reset if necessary.
+ * If there is truncation, vb->used will be set to 0; otherwise
+ * (if there is no error) vb will be null-terminated.
+ * If there is an error vb and *cbyte_io may be left indeterminate.
+ *
+ * serv may be -1 and qu may be 0 - they are used for error reporting only.
+ */
+
+adns_status adns__parse_domain_more(findlabel_state *fls, adns_state ads,
+ adns_query qu, vbuf *vb,
+ parsedomain_flags flags,
+ const byte *dgram);
+/* Like adns__parse_domain, but you pass it a pre-initialised findlabel_state,
+ * for continuing an existing domain or some such of some kind. Also, unlike
+ * _parse_domain, the domain data will be appended to vb, rather than replacing
+ * the existing contents.
+ */
+
+adns_status adns__findrr(adns_query qu, int serv,
+ const byte *dgram, int dglen, int *cbyte_io,
+ int *type_r, int *class_r, unsigned long *ttl_r,
+ int *rdlen_r, int *rdstart_r,
+ int *ownermatchedquery_r);
+/* Finds the extent and some of the contents of an RR in a datagram
+ * and does some checks. The datagram is *dgram, length dglen, and
+ * the RR starts at *cbyte_io (which is updated afterwards to point
+ * to the end of the RR).
+ *
+ * The type, class, TTL and RRdata length and start are returned iff
+ * the corresponding pointer variables are not null. type_r, class_r
+ * and ttl_r may not be null. The TTL will be capped.
+ *
+ * If ownermatchedquery_r != 0 then the owner domain of this
+ * RR will be compared with that in the query (or, if the query
+ * has gone to a CNAME lookup, with the canonical name).
+ * In this case, *ownermatchedquery_r will be set to 0 or 1.
+ * The query datagram (or CNAME datagram) MUST be valid and not truncated.
+ *
+ * If there is truncation then *type_r will be set to -1 and
+ * *cbyte_io, *class_r, *rdlen_r, *rdstart_r and *eo_matched_r will be
+ * undefined.
+ *
+ * qu must obviously be non-null.
+ *
+ * If an error is returned then *type_r will be undefined too.
+ */
+
+adns_status adns__findrr_anychk(adns_query qu, int serv,
+ const byte *dgram, int dglen, int *cbyte_io,
+ int *type_r, int *class_r,
+ unsigned long *ttl_r,
+ int *rdlen_r, int *rdstart_r,
+ const byte *eo_dgram, int eo_dglen,
+ int eo_cbyte, int *eo_matched_r);
+/* Like adns__findrr_checked, except that the datagram and
+ * owner to compare with can be specified explicitly.
+ *
+ * If the caller thinks they know what the owner of the RR ought to
+ * be they can pass in details in eo_*: this is another (or perhaps
+ * the same datagram), and a pointer to where the putative owner
+ * starts in that datagram. In this case *eo_matched_r will be set
+ * to 1 if the datagram matched or 0 if it did not. Either
+ * both eo_dgram and eo_matched_r must both be non-null, or they
+ * must both be null (in which case eo_dglen and eo_cbyte will be ignored).
+ * The eo datagram and contained owner domain MUST be valid and
+ * untruncated.
+ */
+
+void adns__update_expires(adns_query qu, unsigned long ttl,
+ struct timeval now);
+/* Updates the `expires' field in the query, so that it doesn't exceed
+ * now + ttl.
+ */
+
+bool adns__labels_equal(const byte *a, int al, const byte *b, int bl);
+
+/* From event.c: */
+
+void adns__tcp_broken(adns_state ads, const char *what, const char *why);
+/* what and why may be both 0, or both non-0. */
+
+void adns__tcp_tryconnect(adns_state ads, struct timeval now);
+
+void adns__autosys(adns_state ads, struct timeval now);
+/* Make all the system calls we want to if the application wants us to.
+ * Must not be called from within adns internal processing functions,
+ * lest we end up in recursive descent !
+ */
+
+void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
+ struct timeval *tv_buf);
+/* Call with care - might reentrantly cause queries to be completed! */
+
+int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]);
+void adns__fdevents(adns_state ads,
+ const struct pollfd *pollfds, int npollfds,
+ int maxfd, const fd_set *readfds,
+ const fd_set *writefds, const fd_set *exceptfds,
+ struct timeval now, int *r_r);
+int adns__internal_check(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer,
+ void **context_r);
+
+void adns__timeouts(adns_state ads, int act,
+ struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval now);
+/* If act is !0, then this will also deal with the TCP connection
+ * if previous events broke it or require it to be connected.
+ */
+
+/* From check.c: */
+
+void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc);
+
+/* Useful static inline functions: */
+
+static inline int ctype_whitespace(int c) {
+ return c==' ' || c=='\n' || c=='\t';
+}
+static inline int ctype_digit(int c) { return c>='0' && c<='9'; }
+static inline int ctype_alpha(int c) {
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
+}
+static inline int ctype_toupper(int c) {
+ return ctype_alpha(c) ? (c & ~32) : c;
+}
+static inline int ctype_822special(int c) {
+ return strchr("()<>@,;:\\\".[]",c) != 0;
+}
+static inline int ctype_domainunquoted(int c) {
+ return ctype_alpha(c) || ctype_digit(c) || (strchr("-_/+",c) != 0);
+}
+
+static inline int errno_resources(int e) { return e==ENOMEM || e==ENOBUFS; }
+
+/* Useful macros */
+
+#define MEM_ROUND(sz) \
+ (( ((sz)+sizeof(union maxalign)-1) / sizeof(union maxalign) ) \
+ * sizeof(union maxalign) )
+
+#define GETIL_B(cb) (((dgram)[(cb)++]) & 0x0ff)
+#define GET_B(cb,tv) ((tv)= GETIL_B((cb)))
+#define GET_W(cb,tv) ((tv)=0,(tv)|=(GETIL_B((cb))<<8), (tv)|=GETIL_B(cb), (tv))
+#define GET_L(cb,tv) ( (tv)=0, \
+ (tv)|=(GETIL_B((cb))<<24), \
+ (tv)|=(GETIL_B((cb))<<16), \
+ (tv)|=(GETIL_B((cb))<<8), \
+ (tv)|=GETIL_B(cb), \
+ (tv) )
+
+#endif
diff --git a/src/parse.c b/src/parse.c
new file mode 100644
index 0000000..caedf15
--- /dev/null
+++ b/src/parse.c
@@ -0,0 +1,261 @@
+/*
+ * parse.c
+ * - parsing assistance functions (mainly for domains inside datagrams)
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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"
+
+static int vbuf_append_quoted1035(vbuf *vb, const byte *buf, int len) {
+ char qbuf[10];
+ int i, ch;
+
+ while (len) {
+ qbuf[0]= 0;
+ for (i=0; i<len; i++) {
+ ch= buf[i];
+ if (ch <= ' ' || ch >= 127) {
+ sprintf(qbuf,"\\%03o",ch);
+ break;
+ } else if (!ctype_domainunquoted(ch)) {
+ sprintf(qbuf,"\\%c",ch);
+ break;
+ }
+ }
+ if (!adns__vbuf_append(vb,buf,i) ||
+ !adns__vbuf_append(vb,qbuf,strlen(qbuf)))
+ return 0;
+ if (i<len) i++;
+ buf+= i;
+ len-= i;
+ }
+ return 1;
+}
+
+void adns__findlabel_start(findlabel_state *fls, adns_state ads,
+ int serv, adns_query qu,
+ const byte *dgram, int dglen, int max,
+ int dmbegin, int *dmend_rlater) {
+ fls->ads= ads;
+ fls->qu= qu;
+ fls->serv= serv;
+ fls->dgram= dgram;
+ fls->dglen= dglen;
+ fls->max= max;
+ fls->cbyte= dmbegin;
+ fls->namelen= 0;
+ fls->dmend_r= dmend_rlater;
+}
+
+adns_status adns__findlabel_next(findlabel_state *fls,
+ int *lablen_r, int *labstart_r) {
+ int lablen, jumpto;
+ const char *dgram;
+
+ dgram= fls->dgram;
+ for (;;) {
+ if (fls->cbyte >= fls->dglen) goto x_truncated;
+ if (fls->cbyte >= fls->max) goto x_badresponse;
+ GET_B(fls->cbyte,lablen);
+ if (!(lablen & 0x0c0)) break;
+ if ((lablen & 0x0c0) != 0x0c0) return adns_s_unknownformat;
+ if (fls->cbyte >= fls->dglen) goto x_truncated;
+ if (fls->cbyte >= fls->max) goto x_badresponse;
+ GET_B(fls->cbyte,jumpto);
+ jumpto |= (lablen&0x3f)<<8;
+ if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
+ fls->cbyte= jumpto;
+ fls->dmend_r= 0; fls->max= fls->dglen+1;
+ }
+ if (labstart_r) *labstart_r= fls->cbyte;
+ if (lablen) {
+ if (fls->namelen) fls->namelen++;
+ fls->namelen+= lablen;
+ if (fls->namelen > DNS_MAXDOMAIN) return adns_s_answerdomaintoolong;
+ fls->cbyte+= lablen;
+ if (fls->cbyte > fls->dglen) goto x_truncated;
+ if (fls->cbyte > fls->max) goto x_badresponse;
+ } else {
+ if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
+ }
+ *lablen_r= lablen;
+ return adns_s_ok;
+
+ x_truncated:
+ *lablen_r= -1;
+ return adns_s_ok;
+
+ x_badresponse:
+ adns__diag(fls->ads,fls->serv,fls->qu,
+ "label in domain runs beyond end of domain");
+ return adns_s_invalidresponse;
+}
+
+adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu,
+ vbuf *vb, parsedomain_flags flags,
+ const byte *dgram, int dglen, int *cbyte_io,
+ int max) {
+ findlabel_state fls;
+
+ adns__findlabel_start(&fls,ads, serv,qu, dgram,dglen,max,
+ *cbyte_io,cbyte_io);
+ vb->used= 0;
+ return adns__parse_domain_more(&fls,ads,qu, vb,flags,dgram);
+}
+
+adns_status adns__parse_domain_more(findlabel_state *fls, adns_state ads,
+ adns_query qu, vbuf *vb,
+ parsedomain_flags flags,
+ const byte *dgram) {
+ int lablen, labstart, i, ch, first;
+ adns_status st;
+
+ first= 1;
+ for (;;) {
+ st= adns__findlabel_next(fls,&lablen,&labstart);
+ if (st) return st;
+ if (lablen<0) { vb->used=0; return adns_s_ok; }
+ if (!lablen) break;
+ if (first) {
+ first= 0;
+ } else {
+ if (!adns__vbuf_append(vb,".",1)) return adns_s_nomemory;
+ }
+ if (flags & pdf_quoteok) {
+ if (!vbuf_append_quoted1035(vb,dgram+labstart,lablen))
+ return adns_s_nomemory;
+ } else {
+ ch= dgram[labstart];
+ if (!ctype_alpha(ch) && !ctype_digit(ch))
+ return adns_s_answerdomaininvalid;
+ for (i= labstart+1; i<labstart+lablen; i++) {
+ ch= dgram[i];
+ if (ch != '-' && !ctype_alpha(ch) && !ctype_digit(ch))
+ return adns_s_answerdomaininvalid;
+ }
+ if (!adns__vbuf_append(vb,dgram+labstart,lablen))
+ return adns_s_nomemory;
+ }
+ }
+ if (!adns__vbuf_append(vb,"",1)) return adns_s_nomemory;
+ return adns_s_ok;
+}
+
+bool adns__labels_equal(const byte *a, int al, const byte *b, int bl) {
+ if (al != bl) return 0;
+ while (al-- > 0) {
+ int ac= ctype_toupper(*a++);
+ int bc= ctype_toupper(*b++);
+ if (ac != bc) return 0;
+ }
+ return 1;
+}
+
+adns_status adns__findrr_anychk(adns_query qu, int serv,
+ const byte *dgram, int dglen, int *cbyte_io,
+ int *type_r, int *class_r,
+ unsigned long *ttl_r,
+ int *rdlen_r, int *rdstart_r,
+ const byte *eo_dgram, int eo_dglen,
+ int eo_cbyte, int *eo_matched_r) {
+ findlabel_state fls, eo_fls_buf;
+ findlabel_state *eo_fls; /* 0 iff we know it's not matching eo_... */
+ int cbyte;
+
+ int tmp, rdlen;
+ unsigned long ttl;
+ int lablen, labstart;
+ int eo_lablen, eo_labstart;
+ adns_status st;
+
+ cbyte= *cbyte_io;
+
+ adns__findlabel_start(&fls,qu->ads, serv,qu, dgram,dglen,dglen,cbyte,&cbyte);
+ if (eo_dgram) {
+ eo_fls= &eo_fls_buf;
+ adns__findlabel_start(eo_fls,qu->ads, -1,0,
+ eo_dgram,eo_dglen,eo_dglen,eo_cbyte,0);
+ } else {
+ eo_fls= 0;
+ }
+
+ for (;;) {
+ st= adns__findlabel_next(&fls,&lablen,&labstart);
+ if (st) return st;
+ if (lablen<0) goto x_truncated;
+
+ if (eo_fls) {
+ st= adns__findlabel_next(eo_fls,&eo_lablen,&eo_labstart);
+ assert(!st); assert(eo_lablen>=0);
+ if (!adns__labels_equal(dgram+labstart, lablen,
+ eo_dgram+eo_labstart, eo_lablen))
+ eo_fls= 0;
+ }
+ if (!lablen) break;
+ }
+ if (eo_matched_r) *eo_matched_r= !!eo_fls;
+
+ if (cbyte+10>dglen) goto x_truncated;
+ GET_W(cbyte,tmp); *type_r= tmp;
+ GET_W(cbyte,tmp); *class_r= tmp;
+
+ GET_L(cbyte,ttl);
+ if (ttl > MAXTTLBELIEVE) ttl= MAXTTLBELIEVE;
+ *ttl_r= ttl;
+
+ GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= rdlen;
+ if (rdstart_r) *rdstart_r= cbyte;
+ cbyte+= rdlen;
+ if (cbyte>dglen) goto x_truncated;
+ *cbyte_io= cbyte;
+ return adns_s_ok;
+
+ x_truncated:
+ *type_r= -1;
+ return 0;
+}
+
+adns_status adns__findrr(adns_query qu, int serv,
+ const byte *dgram, int dglen, int *cbyte_io,
+ int *type_r, int *class_r, unsigned long *ttl_r,
+ int *rdlen_r, int *rdstart_r,
+ int *ownermatchedquery_r) {
+ if (!ownermatchedquery_r) {
+ return adns__findrr_anychk(qu,serv,
+ dgram,dglen,cbyte_io,
+ type_r,class_r,ttl_r,rdlen_r,rdstart_r,
+ 0,0,0, 0);
+ } else if (!qu->cname_dgram) {
+ return adns__findrr_anychk(qu,serv,
+ dgram,dglen,cbyte_io,
+ type_r,class_r,ttl_r,rdlen_r,rdstart_r,
+ qu->query_dgram,qu->query_dglen,DNS_HDRSIZE,
+ ownermatchedquery_r);
+ } else {
+ return adns__findrr_anychk(qu,serv,
+ dgram,dglen,cbyte_io,
+ type_r,class_r,ttl_r,rdlen_r,rdstart_r,
+ qu->cname_dgram,qu->cname_dglen,qu->cname_begin,
+ ownermatchedquery_r);
+ }
+}
diff --git a/src/poll.c b/src/poll.c
new file mode 100644
index 0000000..305b275
--- /dev/null
+++ b/src/poll.c
@@ -0,0 +1,130 @@
+/*
+ * poll.c
+ * - wrappers for poll(2)
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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 <limits.h>
+#include <string.h>
+
+#include "internal.h"
+
+#ifdef HAVE_POLL
+
+int adns_beforepoll(adns_state ads, struct pollfd *fds, int *nfds_io,
+ int *timeout_io, const struct timeval *now) {
+ struct timeval tv_nowbuf, tv_tobuf, *tv_to;
+ int space, found, timeout_ms, r;
+ struct pollfd fds_tmp[MAX_POLLFDS];
+
+ adns__consistency(ads,0,cc_entex);
+
+ if (timeout_io) {
+ adns__must_gettimeofday(ads,&now,&tv_nowbuf);
+ if (!now) { *nfds_io= 0; r= 0; goto xit; }
+
+ timeout_ms= *timeout_io;
+ if (timeout_ms == -1) {
+ tv_to= 0;
+ } else {
+ tv_tobuf.tv_sec= timeout_ms / 1000;
+ tv_tobuf.tv_usec= (timeout_ms % 1000)*1000;
+ tv_to= &tv_tobuf;
+ }
+
+ adns__timeouts(ads, 0, &tv_to,&tv_tobuf, *now);
+
+ if (tv_to) {
+ assert(tv_to == &tv_tobuf);
+ timeout_ms= (tv_tobuf.tv_usec+999)/1000;
+ assert(tv_tobuf.tv_sec < (INT_MAX-timeout_ms)/1000);
+ timeout_ms += tv_tobuf.tv_sec*1000;
+ } else {
+ timeout_ms= -1;
+ }
+ *timeout_io= timeout_ms;
+ }
+
+ space= *nfds_io;
+ if (space >= MAX_POLLFDS) {
+ found= adns__pollfds(ads,fds);
+ *nfds_io= found;
+ } else {
+ found= adns__pollfds(ads,fds_tmp);
+ *nfds_io= found;
+ if (space < found) { r= ERANGE; goto xit; }
+ memcpy(fds,fds_tmp,sizeof(struct pollfd)*found);
+ }
+ r= 0;
+xit:
+ adns__returning(ads,0);
+ return r;
+}
+
+void adns_afterpoll(adns_state ads, const struct pollfd *fds, int nfds,
+ const struct timeval *now) {
+ struct timeval tv_buf;
+
+ adns__consistency(ads,0,cc_entex);
+ adns__must_gettimeofday(ads,&now,&tv_buf);
+ if (now) {
+ adns__timeouts(ads, 1, 0,0, *now);
+ adns__fdevents(ads, fds,nfds, 0,0,0,0, *now,0);
+ }
+ adns__returning(ads,0);
+}
+
+int adns_wait_poll(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer_r,
+ void **context_r) {
+ int r, nfds, to;
+ struct pollfd fds[MAX_POLLFDS];
+
+ adns__consistency(ads,0,cc_entex);
+
+ for (;;) {
+ r= adns__internal_check(ads,query_io,answer_r,context_r);
+ if (r != EAGAIN) goto xit;
+ nfds= MAX_POLLFDS; to= -1;
+ adns_beforepoll(ads,fds,&nfds,&to,0);
+ r= poll(fds,nfds,to);
+ if (r == -1) {
+ if (errno == EINTR) {
+ if (ads->iflags & adns_if_eintr) { r= EINTR; goto xit; }
+ } else {
+ adns__diag(ads,-1,0,"poll failed in wait: %s",strerror(errno));
+ adns_globalsystemfailure(ads);
+ }
+ } else {
+ assert(r >= 0);
+ adns_afterpoll(ads,fds,nfds,0);
+ }
+ }
+
+ xit:
+ adns__returning(ads,0);
+ return r;
+}
+
+#endif
diff --git a/src/query.c b/src/query.c
new file mode 100644
index 0000000..ff56fc3
--- /dev/null
+++ b/src/query.c
@@ -0,0 +1,677 @@
+/*
+ * query.c
+ * - overall query management (allocation, completion)
+ * - per-query memory management
+ * - query submission and cancellation (user-visible and internal)
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <sys/time.h>
+
+#include "internal.h"
+
+static adns_query query_alloc(adns_state ads,
+ const typeinfo *typei, adns_rrtype type,
+ adns_queryflags flags, struct timeval now) {
+ /* Allocate a virgin query and return it. */
+ adns_query qu;
+
+ qu= malloc(sizeof(*qu)); if (!qu) return 0;
+ qu->answer= malloc(sizeof(*qu->answer));
+ if (!qu->answer) { free(qu); return 0; }
+
+ qu->ads= ads;
+ qu->state= query_tosend;
+ qu->back= qu->next= qu->parent= 0;
+ LIST_INIT(qu->children);
+ LINK_INIT(qu->siblings);
+ LIST_INIT(qu->allocations);
+ qu->interim_allocd= 0;
+ qu->preserved_allocd= 0;
+ qu->final_allocspace= 0;
+
+ qu->typei= typei;
+ qu->query_dgram= 0;
+ qu->query_dglen= 0;
+ adns__vbuf_init(&qu->vb);
+
+ qu->cname_dgram= 0;
+ qu->cname_dglen= qu->cname_begin= 0;
+
+ adns__vbuf_init(&qu->search_vb);
+ qu->search_origlen= qu->search_pos= qu->search_doneabs= 0;
+
+ qu->id= -2; /* will be overwritten with real id before we leave adns */
+ qu->flags= flags;
+ qu->retries= 0;
+ qu->udpnextserver= 0;
+ qu->udpsent= 0;
+ timerclear(&qu->timeout);
+ qu->expires= now.tv_sec + MAXTTLBELIEVE;
+
+ memset(&qu->ctx,0,sizeof(qu->ctx));
+
+ qu->answer->status= adns_s_ok;
+ qu->answer->cname= qu->answer->owner= 0;
+ qu->answer->type= type;
+ qu->answer->expires= -1;
+ qu->answer->nrrs= 0;
+ qu->answer->rrs.untyped= 0;
+ qu->answer->rrsz= typei->getrrsz(typei,type);
+
+ return qu;
+}
+
+static void query_submit(adns_state ads, adns_query qu,
+ const typeinfo *typei, vbuf *qumsg_vb, int id,
+ adns_queryflags flags, struct timeval now) {
+ /* Fills in the query message in for a previously-allocated query,
+ * and submits it. Cannot fail. Takes over the memory for qumsg_vb.
+ */
+
+ qu->vb= *qumsg_vb;
+ adns__vbuf_init(qumsg_vb);
+
+ qu->query_dgram= malloc(qu->vb.used);
+ if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; }
+
+ qu->id= id;
+ qu->query_dglen= qu->vb.used;
+ memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used);
+
+ typei->query_send(qu,now);
+}
+
+adns_status adns__ckl_hostname(adns_state ads, adns_queryflags flags,
+ union checklabel_state *cls,
+ qcontext *ctx, int labnum,
+ const char *dgram, int labstart, int lablen)
+{
+ int i, c;
+ const char *label = dgram+labstart;
+
+ if (flags & adns_qf_quoteok_query) return adns_s_ok;
+ for (i=0; i<lablen; i++) {
+ c= label[i];
+ if (c == '-') {
+ if (!i) return adns_s_querydomaininvalid;
+ } else if (!ctype_alpha(c) && !ctype_digit(c)) {
+ return adns_s_querydomaininvalid;
+ }
+ }
+ return adns_s_ok;
+}
+
+static adns_status check_domain_name(adns_state ads, adns_queryflags flags,
+ qcontext *ctx, const typeinfo *typei,
+ const byte *dgram, int dglen)
+{
+ findlabel_state fls;
+ adns_status st;
+ int labnum= 0, labstart, lablen;
+ union checklabel_state cls;
+
+ adns__findlabel_start(&fls,ads, -1,0, dgram,dglen,dglen, DNS_HDRSIZE,0);
+ do {
+ st= adns__findlabel_next(&fls, &lablen,&labstart);
+ assert(!st); assert(lablen >= 0);
+ st= typei->checklabel(ads,flags, &cls,ctx,
+ labnum++, dgram,labstart,lablen);
+ if (st) return st;
+ } while (lablen);
+ return adns_s_ok;
+}
+
+adns_status adns__internal_submit(adns_state ads, adns_query *query_r,
+ adns_query parent,
+ const typeinfo *typei, adns_rrtype type,
+ vbuf *qumsg_vb, int id,
+ adns_queryflags flags, struct timeval now,
+ qcontext *ctx) {
+ adns_query qu;
+ adns_status st;
+
+ st= check_domain_name(ads, flags,ctx,typei, qumsg_vb->buf,qumsg_vb->used);
+ if (st) goto x_err;
+ qu= query_alloc(ads,typei,type,flags,now);
+ if (!qu) { st = adns_s_nomemory; goto x_err; }
+ *query_r= qu;
+
+ qu->parent= parent;
+ LIST_LINK_TAIL_PART(parent->children,qu,siblings.);
+ memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
+ query_submit(ads,qu, typei,qumsg_vb,id,flags,now);
+
+ return adns_s_ok;
+
+x_err:
+ adns__vbuf_free(qumsg_vb);
+ return st;
+}
+
+static void query_simple(adns_state ads, adns_query qu,
+ const char *owner, int ol,
+ const typeinfo *typei, adns_queryflags flags,
+ struct timeval now) {
+ vbuf vb_new;
+ int id;
+ adns_status st;
+
+ st= adns__mkquery(ads,&qu->vb,&id, owner,ol,
+ typei,qu->answer->type, flags);
+ if (st) {
+ if (st == adns_s_querydomaintoolong && (flags & adns_qf_search)) {
+ adns__search_next(ads,qu,now);
+ return;
+ } else {
+ adns__query_fail(qu,st);
+ return;
+ }
+ }
+
+ st= check_domain_name(ads, flags,&qu->ctx,typei, qu->vb.buf,qu->vb.used);
+ if (st) { adns__query_fail(qu,st); return; }
+
+ vb_new= qu->vb;
+ adns__vbuf_init(&qu->vb);
+ query_submit(ads,qu, typei,&vb_new,id, flags,now);
+}
+
+void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
+ const char *nextentry;
+ adns_status st;
+
+ if (qu->search_doneabs<0) {
+ nextentry= 0;
+ qu->search_doneabs= 1;
+ } else {
+ if (qu->search_pos >= ads->nsearchlist) {
+ if (qu->search_doneabs) {
+ qu->search_vb.used= qu->search_origlen;
+ st= adns_s_nxdomain; goto x_fail;
+ } else {
+ nextentry= 0;
+ qu->search_doneabs= 1;
+ }
+ } else {
+ nextentry= ads->searchlist[qu->search_pos++];
+ }
+ }
+
+ qu->search_vb.used= qu->search_origlen;
+ if (nextentry) {
+ if (!adns__vbuf_append(&qu->search_vb,".",1) ||
+ !adns__vbuf_appendstr(&qu->search_vb,nextentry))
+ goto x_nomemory;
+ }
+
+ free(qu->query_dgram);
+ qu->query_dgram= 0; qu->query_dglen= 0;
+
+ query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used,
+ qu->typei, qu->flags, now);
+ return;
+
+x_nomemory:
+ st= adns_s_nomemory;
+x_fail:
+ adns__query_fail(qu,st);
+}
+
+static int save_owner(adns_query qu, const char *owner, int ol) {
+ /* Returns 1 if OK, otherwise there was no memory. */
+ adns_answer *ans;
+
+ if (!(qu->flags & adns_qf_owner)) return 1;
+
+ ans= qu->answer;
+ assert(!ans->owner);
+
+ ans->owner= adns__alloc_preserved(qu,ol+1); if (!ans->owner) return 0;
+
+ memcpy(ans->owner,owner,ol);
+ ans->owner[ol]= 0;
+ return 1;
+}
+
+int adns_submit(adns_state ads,
+ const char *owner,
+ adns_rrtype type,
+ adns_queryflags flags,
+ void *context,
+ adns_query *query_r) {
+ int r, ol, ndots;
+ adns_status st;
+ const typeinfo *typei;
+ struct timeval now;
+ adns_query qu;
+ const char *p;
+
+ adns__consistency(ads,0,cc_entex);
+
+ if (flags & ~(adns_queryflags)0x4009ffff)
+ /* 0x40080000 are reserved for `harmless' future expansion
+ * 0x00000020 used to be adns_qf_quoteok_cname, now the default;
+ * see also addrfam.c:textaddr_check_qf */
+ return ENOSYS;
+
+ typei= adns__findtype(type);
+ if (!typei) return ENOSYS;
+
+ r= gettimeofday(&now,0); if (r) goto x_errno;
+ qu= query_alloc(ads,typei,type,flags,now); if (!qu) goto x_errno;
+
+ qu->ctx.ext= context;
+ qu->ctx.callback= 0;
+ memset(&qu->ctx.pinfo,0,sizeof(qu->ctx.pinfo));
+ memset(&qu->ctx.tinfo,0,sizeof(qu->ctx.tinfo));
+
+ *query_r= qu;
+
+ ol= strlen(owner);
+ if (!ol) { st= adns_s_querydomaininvalid; goto x_adnsfail; }
+ if (ol>DNS_MAXDOMAIN+1) { st= adns_s_querydomaintoolong; goto x_adnsfail; }
+
+ if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) {
+ flags &= ~adns_qf_search;
+ qu->flags= flags;
+ ol--;
+ }
+
+ if (flags & adns_qf_search) {
+ r= adns__vbuf_append(&qu->search_vb,owner,ol);
+ if (!r) { st= adns_s_nomemory; goto x_adnsfail; }
+
+ for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
+ qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
+ qu->search_origlen= ol;
+ adns__search_next(ads,qu,now);
+ } else {
+ if (flags & adns_qf_owner) {
+ if (!save_owner(qu,owner,ol)) { st= adns_s_nomemory; goto x_adnsfail; }
+ }
+ query_simple(ads,qu, owner,ol, typei,flags, now);
+ }
+ adns__autosys(ads,now);
+ adns__returning(ads,qu);
+ return 0;
+
+ x_adnsfail:
+ adns__query_fail(qu,st);
+ adns__returning(ads,qu);
+ return 0;
+
+ x_errno:
+ r= errno;
+ assert(r);
+ adns__returning(ads,0);
+ return r;
+}
+
+int adns_submit_reverse_any(adns_state ads,
+ const struct sockaddr *addr,
+ const char *zone,
+ adns_rrtype type,
+ adns_queryflags flags,
+ void *context,
+ adns_query *query_r) {
+ char *buf, *buf_free = 0;
+ char shortbuf[100];
+ int r;
+
+ flags &= ~adns_qf_search;
+
+ buf = shortbuf;
+ r= adns__make_reverse_domain(addr,zone, &buf,sizeof(shortbuf),&buf_free);
+ if (r) return r;
+ r= adns_submit(ads,buf,type,flags,context,query_r);
+ free(buf_free);
+ return r;
+}
+
+int adns_submit_reverse(adns_state ads,
+ const struct sockaddr *addr,
+ adns_rrtype type,
+ adns_queryflags flags,
+ void *context,
+ adns_query *query_r) {
+ if (((type^adns_r_ptr) & adns_rrt_reprmask) &&
+ ((type^adns_r_ptr_raw) & adns_rrt_reprmask))
+ return EINVAL;
+ return adns_submit_reverse_any(ads,addr,0,type,flags,context,query_r);
+}
+
+int adns_synchronous(adns_state ads,
+ const char *owner,
+ adns_rrtype type,
+ adns_queryflags flags,
+ adns_answer **answer_r) {
+ adns_query qu;
+ int r;
+
+ r= adns_submit(ads,owner,type,flags,0,&qu);
+ if (r) return r;
+
+ r= adns_wait(ads,&qu,answer_r,0);
+ if (r) adns_cancel(qu);
+
+ return r;
+}
+
+static void *alloc_common(adns_query qu, size_t sz) {
+ allocnode *an;
+
+ if (!sz) return qu; /* Any old pointer will do */
+ assert(!qu->final_allocspace);
+ an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
+ if (!an) return 0;
+ LIST_LINK_TAIL(qu->allocations,an);
+ an->sz= sz;
+ return (byte*)an + MEM_ROUND(sizeof(*an));
+}
+
+void *adns__alloc_interim(adns_query qu, size_t sz) {
+ void *rv;
+
+ sz= MEM_ROUND(sz);
+ rv= alloc_common(qu,sz);
+ if (!rv) return 0;
+ qu->interim_allocd += sz;
+ return rv;
+}
+
+void *adns__alloc_preserved(adns_query qu, size_t sz) {
+ void *rv;
+
+ sz= MEM_ROUND(sz);
+ rv= adns__alloc_interim(qu,sz);
+ if (!rv) return 0;
+ qu->preserved_allocd += sz;
+ return rv;
+}
+
+static allocnode *alloc__info(adns_query qu, void *p, size_t *sz_r) {
+ allocnode *an;
+
+ if (!p || p == qu) { *sz_r= 0; return 0; }
+ an= (allocnode *)((byte *)p - MEM_ROUND(sizeof(allocnode)));
+ *sz_r= MEM_ROUND(an->sz);
+ return an;
+}
+
+void adns__free_interim(adns_query qu, void *p) {
+ size_t sz;
+ allocnode *an= alloc__info(qu, p, &sz);
+
+ if (!an) return;
+ assert(!qu->final_allocspace);
+ LIST_UNLINK(qu->allocations, an);
+ free(an);
+ qu->interim_allocd -= sz;
+ assert(!qu->interim_allocd >= 0);
+}
+
+void *adns__alloc_mine(adns_query qu, size_t sz) {
+ return alloc_common(qu,MEM_ROUND(sz));
+}
+
+void adns__transfer_interim(adns_query from, adns_query to, void *block) {
+ size_t sz;
+ allocnode *an= alloc__info(from, block, &sz);
+
+ if (!an) return;
+
+ assert(!to->final_allocspace);
+ assert(!from->final_allocspace);
+
+ LIST_UNLINK(from->allocations,an);
+ LIST_LINK_TAIL(to->allocations,an);
+
+ from->interim_allocd -= sz;
+ to->interim_allocd += sz;
+
+ if (to->expires > from->expires) to->expires= from->expires;
+}
+
+void *adns__alloc_final(adns_query qu, size_t sz) {
+ /* When we're in the _final stage, we _subtract_ from interim_alloc'd
+ * each allocation, and use final_allocspace to point to the next free
+ * bit.
+ */
+ void *rp;
+
+ sz= MEM_ROUND(sz);
+ rp= qu->final_allocspace;
+ assert(rp);
+ qu->interim_allocd -= sz;
+ assert(qu->interim_allocd>=0);
+ qu->final_allocspace= (byte*)rp + sz;
+ return rp;
+}
+
+void adns__cancel_children(adns_query qu) {
+ adns_query cqu, ncqu;
+
+ for (cqu= qu->children.head; cqu; cqu= ncqu) {
+ ncqu= cqu->siblings.next;
+ adns__cancel(cqu);
+ }
+}
+
+void adns__reset_preserved(adns_query qu) {
+ assert(!qu->final_allocspace);
+ adns__cancel_children(qu);
+ qu->answer->nrrs= 0;
+ qu->answer->rrs.untyped= 0;
+ qu->interim_allocd= qu->preserved_allocd;
+}
+
+static void free_query_allocs(adns_query qu) {
+ allocnode *an, *ann;
+
+ adns__cancel_children(qu);
+ for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
+ LIST_INIT(qu->allocations);
+ adns__vbuf_free(&qu->vb);
+ adns__vbuf_free(&qu->search_vb);
+ free(qu->query_dgram);
+ qu->query_dgram= 0;
+}
+
+void adns__returning(adns_state ads, adns_query qu_for_caller) {
+ while (ads->intdone.head) {
+ adns_query iq= ads->intdone.head;
+ adns_query parent= iq->parent;
+ LIST_UNLINK_PART(parent->children,iq,siblings.);
+ LIST_UNLINK(iq->ads->childw,parent);
+ LIST_UNLINK(ads->intdone,iq);
+ iq->ctx.callback(parent,iq);
+ free_query_allocs(iq);
+ free(iq->answer);
+ free(iq);
+ }
+ adns__consistency(ads,qu_for_caller,cc_entex);
+}
+
+void adns__cancel(adns_query qu) {
+ adns_state ads;
+
+ ads= qu->ads;
+ adns__consistency(ads,qu,cc_freq);
+ if (qu->parent) LIST_UNLINK_PART(qu->parent->children,qu,siblings.);
+ switch (qu->state) {
+ case query_tosend:
+ LIST_UNLINK(ads->udpw,qu);
+ break;
+ case query_tcpw:
+ LIST_UNLINK(ads->tcpw,qu);
+ break;
+ case query_childw:
+ LIST_UNLINK(ads->childw,qu);
+ break;
+ case query_done:
+ if (qu->parent)
+ LIST_UNLINK(ads->intdone,qu);
+ else
+ LIST_UNLINK(ads->output,qu);
+ break;
+ default:
+ abort();
+ }
+ free_query_allocs(qu);
+ free(qu->answer);
+ free(qu);
+}
+
+void adns_cancel(adns_query qu) {
+ adns_state ads;
+
+ assert(!qu->parent);
+ ads= qu->ads;
+ adns__consistency(ads,qu,cc_entex);
+ adns__cancel(qu);
+ adns__returning(ads,0);
+}
+
+void adns__update_expires(adns_query qu, unsigned long ttl,
+ struct timeval now) {
+ time_t max;
+
+ assert(ttl <= MAXTTLBELIEVE);
+ max= now.tv_sec + ttl;
+ if (qu->expires < max) return;
+ qu->expires= max;
+}
+
+static void makefinal_query(adns_query qu) {
+ adns_answer *ans;
+ int rrn;
+
+ ans= qu->answer;
+
+ if (qu->interim_allocd) {
+ ans= realloc(qu->answer,
+ MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
+ if (!ans) goto x_nomem;
+ qu->answer= ans;
+ }
+
+ qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
+ adns__makefinal_str(qu,&ans->cname);
+ adns__makefinal_str(qu,&ans->owner);
+
+ if (ans->nrrs) {
+ adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
+
+ for (rrn=0; rrn<ans->nrrs; rrn++)
+ qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
+ }
+
+ free_query_allocs(qu);
+ return;
+
+ x_nomem:
+ qu->preserved_allocd= 0;
+ qu->answer->cname= 0;
+ qu->answer->owner= 0;
+ adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */
+
+ qu->answer->status= adns_s_nomemory;
+ free_query_allocs(qu);
+}
+
+void adns__query_done(adns_query qu) {
+ adns_state ads=qu->ads;
+ adns_answer *ans;
+
+ adns__cancel_children(qu);
+
+ qu->id= -1;
+ ans= qu->answer;
+
+ if (qu->flags & adns_qf_search && ans->status != adns_s_nomemory) {
+ if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) {
+ adns__query_fail(qu,adns_s_nomemory);
+ return;
+ }
+ }
+
+ if (ans->nrrs && qu->typei->diff_needswap) {
+ if (!adns__vbuf_ensure(&qu->vb,qu->answer->rrsz)) {
+ adns__query_fail(qu,adns_s_nomemory);
+ return;
+ }
+ adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
+ qu->vb.buf,
+ (int(*)(void*, const void*, const void*))
+ qu->typei->diff_needswap,
+ qu->ads);
+ }
+ if (ans->nrrs && qu->typei->postsort) {
+ qu->typei->postsort(qu->ads, ans->rrs.bytes,
+ ans->nrrs,ans->rrsz, qu->typei);
+ }
+
+ ans->expires= qu->expires;
+ qu->state= query_done;
+ if (qu->parent) {
+ LIST_LINK_TAIL(ads->intdone,qu);
+ } else {
+ makefinal_query(qu);
+ LIST_LINK_TAIL(qu->ads->output,qu);
+ }
+}
+
+void adns__query_fail(adns_query qu, adns_status st) {
+ adns__reset_preserved(qu);
+ qu->answer->status= st;
+ adns__query_done(qu);
+}
+
+void adns__makefinal_str(adns_query qu, char **strp) {
+ int l;
+ char *before, *after;
+
+ before= *strp;
+ if (!before) return;
+ l= strlen(before)+1;
+ after= adns__alloc_final(qu,l);
+ memcpy(after,before,l);
+ *strp= after;
+}
+
+void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
+ void *before, *after;
+
+ before= *blpp;
+ if (!before) return;
+ after= adns__alloc_final(qu,sz);
+ memcpy(after,before,sz);
+ *blpp= after;
+}
diff --git a/src/reply.c b/src/reply.c
new file mode 100644
index 0000000..7d11327
--- /dev/null
+++ b/src/reply.c
@@ -0,0 +1,390 @@
+/*
+ * reply.c
+ * - main handling and parsing routine for received datagrams
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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 <stdlib.h>
+
+#include "internal.h"
+
+void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
+ int serv, int viatcp, struct timeval now) {
+ int cbyte, rrstart, wantedrrs, rri, foundsoa, foundns, cname_here;
+ int id, f1, f2, qdcount, ancount, nscount, arcount;
+ int flg_ra, flg_rd, flg_tc, flg_qr, opcode;
+ int rrtype, rrclass, rdlength, rdstart;
+ int anstart, nsstart;
+ int ownermatched, l, nrrs;
+ unsigned long ttl, soattl;
+ const typeinfo *typei;
+ adns_query qu, nqu;
+ dns_rcode rcode;
+ adns_status st;
+ vbuf tempvb;
+ byte *newquery, *rrsdata;
+ parseinfo pai;
+
+ if (dglen<DNS_HDRSIZE) {
+ adns__diag(ads,serv,0,"received datagram"
+ " too short for message header (%d)",dglen);
+ return;
+ }
+ cbyte= 0;
+ GET_W(cbyte,id);
+ GET_B(cbyte,f1);
+ GET_B(cbyte,f2);
+ GET_W(cbyte,qdcount);
+ GET_W(cbyte,ancount);
+ GET_W(cbyte,nscount);
+ GET_W(cbyte,arcount);
+ assert(cbyte == DNS_HDRSIZE);
+
+ flg_qr= f1&0x80;
+ opcode= (f1&0x78)>>3;
+ flg_tc= f1&0x02;
+ flg_rd= f1&0x01;
+ flg_ra= f2&0x80;
+ rcode= (f2&0x0f);
+
+ cname_here= 0;
+
+ if (!flg_qr) {
+ adns__diag(ads,serv,0,"server sent us a query, not a response");
+ return;
+ }
+ if (opcode) {
+ adns__diag(ads,serv,0,"server sent us unknown opcode"
+ " %d (wanted 0=QUERY)",opcode);
+ return;
+ }
+
+ qu= 0;
+ /* See if we can find the relevant query, or leave qu=0 otherwise ... */
+
+ if (qdcount == 1) {
+ for (qu= viatcp ? ads->tcpw.head : ads->udpw.head; qu; qu= nqu) {
+ nqu= qu->next;
+ if (qu->id != id) continue;
+ if (dglen < qu->query_dglen) continue;
+ if (memcmp(qu->query_dgram+DNS_HDRSIZE,
+ dgram+DNS_HDRSIZE,
+ qu->query_dglen-DNS_HDRSIZE))
+ continue;
+ if (viatcp) {
+ assert(qu->state == query_tcpw);
+ } else {
+ assert(qu->state == query_tosend);
+ if (!(qu->udpsent & (1<<serv))) continue;
+ }
+ break;
+ }
+ if (qu) {
+ /* We're definitely going to do something with this query now */
+ if (viatcp) LIST_UNLINK(ads->tcpw,qu);
+ else LIST_UNLINK(ads->udpw,qu);
+ }
+ }
+
+ /* If we're going to ignore the packet, we return as soon as we have
+ * failed the query (if any) and printed the warning message (if
+ * any).
+ */
+ switch (rcode) {
+ case rcode_noerror:
+ case rcode_nxdomain:
+ break;
+ case rcode_formaterror:
+ adns__warn(ads,serv,qu,"server cannot understand our query"
+ " (Format Error)");
+ if (qu) adns__query_fail(qu,adns_s_rcodeformaterror);
+ return;
+ case rcode_servfail:
+ if (qu) adns__query_fail(qu,adns_s_rcodeservfail);
+ else adns__debug(ads,serv,qu,"server failure on unidentifiable query");
+ return;
+ case rcode_notimp:
+ adns__warn(ads,serv,qu,"server claims not to implement our query");
+ if (qu) adns__query_fail(qu,adns_s_rcodenotimplemented);
+ return;
+ case rcode_refused:
+ adns__debug(ads,serv,qu,"server refused our query");
+ if (qu) adns__query_fail(qu,adns_s_rcoderefused);
+ return;
+ default:
+ adns__warn(ads,serv,qu,"server gave unknown response code %d",rcode);
+ if (qu) adns__query_fail(qu,adns_s_rcodeunknown);
+ return;
+ }
+
+ if (!qu) {
+ if (!qdcount) {
+ adns__diag(ads,serv,0,"server sent reply without quoting our question");
+ } else if (qdcount>1) {
+ adns__diag(ads,serv,0,"server claimed to answer %d"
+ " questions with one message", qdcount);
+ } else if (ads->iflags & adns_if_debug) {
+ adns__vbuf_init(&tempvb);
+ adns__debug(ads,serv,0,"reply not found, id %02x, query owner %s",
+ id, adns__diag_domain(ads,serv,0,&tempvb,
+ dgram,dglen,DNS_HDRSIZE));
+ adns__vbuf_free(&tempvb);
+ }
+ return;
+ }
+
+ /* We're definitely going to do something with this packet and this
+ * query now. */
+
+ anstart= qu->query_dglen;
+
+ /* Now, take a look at the answer section, and see if it is complete.
+ * If it has any CNAMEs we stuff them in the answer.
+ */
+ wantedrrs= 0;
+ cbyte= anstart;
+ for (rri= 0; rri<ancount; rri++) {
+ rrstart= cbyte;
+ st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
+ &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
+ &ownermatched);
+ if (st) { adns__query_fail(qu,st); return; }
+ if (rrtype == -1) goto x_truncated;
+
+ if (rrclass != DNS_CLASS_IN) {
+ adns__diag(ads,serv,qu,"ignoring answer RR with wrong class %d"
+ " (expected IN=%d)", rrclass,DNS_CLASS_IN);
+ continue;
+ }
+ if (!ownermatched) {
+ if (ads->iflags & adns_if_debug) {
+ adns__debug(ads,serv,qu,"ignoring RR with an unexpected owner %s",
+ adns__diag_domain(ads,serv,qu, &qu->vb,
+ dgram,dglen,rrstart));
+ }
+ continue;
+ }
+ if (rrtype == adns_r_cname &&
+ (qu->answer->type & adns_rrt_typemask) != adns_r_cname) {
+ if (qu->flags & adns_qf_cname_forbid) {
+ adns__query_fail(qu,adns_s_prohibitedcname);
+ return;
+ } else if (qu->cname_dgram) { /* Ignore second and subsequent CNAME(s) */
+ adns__debug(ads,serv,qu,"allegedly canonical name %s"
+ " is actually alias for %s", qu->answer->cname,
+ adns__diag_domain(ads,serv,qu, &qu->vb,
+ dgram,dglen,rdstart));
+ adns__query_fail(qu,adns_s_prohibitedcname);
+ return;
+ } else if (wantedrrs) { /* Ignore CNAME(s) after RR(s). */
+ adns__debug(ads,serv,qu,"ignoring CNAME (to %s) coexisting with RR",
+ adns__diag_domain(ads,serv,qu, &qu->vb,
+ dgram,dglen,rdstart));
+ } else {
+ qu->cname_begin= rdstart;
+ qu->cname_dglen= dglen;
+ st= adns__parse_domain(ads,serv,qu, &qu->vb,
+ qu->flags & adns_qf_quotefail_cname
+ ? 0 : pdf_quoteok,
+ dgram,dglen, &rdstart,rdstart+rdlength);
+ if (!qu->vb.used) goto x_truncated;
+ if (st) { adns__query_fail(qu,st); return; }
+ l= strlen(qu->vb.buf)+1;
+ qu->answer->cname= adns__alloc_preserved(qu,l);
+ if (!qu->answer->cname) {
+ adns__query_fail(qu,adns_s_nomemory);
+ return;
+ }
+
+ qu->cname_dgram= adns__alloc_mine(qu,dglen);
+ memcpy(qu->cname_dgram,dgram,dglen);
+
+ memcpy(qu->answer->cname,qu->vb.buf,l);
+ cname_here= 1;
+ adns__update_expires(qu,ttl,now);
+ /* If we find the answer section truncated after this point we restart
+ * the query at the CNAME; if beforehand then we obviously have to use
+ * TCP. If there is no truncation we can use the whole answer if
+ * it contains the relevant info.
+ */
+ }
+ } else if (rrtype == (qu->answer->type & adns_rrt_typemask)) {
+ wantedrrs++;
+ } else {
+ adns__debug(ads,serv,qu,"ignoring answer RR"
+ " with irrelevant type %d",rrtype);
+ }
+ }
+
+ /* We defer handling truncated responses here, in case there was a CNAME
+ * which we could use.
+ */
+ if (flg_tc) goto x_truncated;
+
+ nsstart= cbyte;
+
+ if (!wantedrrs) {
+ /* Oops, NODATA or NXDOMAIN or perhaps a referral
+ * (which would be a problem) */
+
+ /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records
+ * in authority section */
+ foundsoa= 0; soattl= 0; foundns= 0;
+ for (rri= 0; rri<nscount; rri++) {
+ rrstart= cbyte;
+ st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
+ &rrtype,&rrclass,&ttl, &rdlength,&rdstart, 0);
+ if (st) { adns__query_fail(qu,st); return; }
+ if (rrtype==-1) goto x_truncated;
+ if (rrclass != DNS_CLASS_IN) {
+ adns__diag(ads,serv,qu,
+ "ignoring authority RR with wrong class %d"
+ " (expected IN=%d)", rrclass,DNS_CLASS_IN);
+ continue;
+ }
+ if (rrtype == adns_r_soa_raw) { foundsoa= 1; soattl= ttl; break; }
+ else if (rrtype == adns_r_ns_raw) { foundns= 1; }
+ }
+
+ if (rcode == rcode_nxdomain) {
+ /* We still wanted to look for the SOA so we could find the TTL. */
+ adns__update_expires(qu,soattl,now);
+
+ if (qu->flags & adns_qf_search && !qu->cname_dgram) {
+ adns__search_next(ads,qu,now);
+ } else {
+ adns__query_fail(qu,adns_s_nxdomain);
+ }
+ return;
+ }
+
+ if (foundsoa || !foundns) {
+ /* Aha ! A NODATA response, good. */
+ adns__update_expires(qu,soattl,now);
+ adns__query_fail(qu,adns_s_nodata);
+ return;
+ }
+
+ /* Now what ? No relevant answers, no SOA, and at least some NS's.
+ * Looks like a referral. Just one last chance ... if we came across
+ * a CNAME in this datagram then we should probably do our own CNAME
+ * lookup now in the hope that we won't get a referral again.
+ */
+ if (cname_here) goto x_restartquery;
+
+ /* Bloody hell, I thought we asked for recursion ? */
+ if (!flg_ra) {
+ adns__diag(ads,serv,qu,"server is not willing"
+ " to do recursive lookups for us");
+ adns__query_fail(qu,adns_s_norecurse);
+ } else {
+ if (!flg_rd)
+ adns__diag(ads,serv,qu,"server thinks"
+ " we didn't ask for recursive lookup");
+ else
+ adns__debug(ads,serv,qu,"server claims to do recursion,"
+ " but gave us a referral");
+ adns__query_fail(qu,adns_s_invalidresponse);
+ }
+ return;
+ }
+
+ /* Now, we have some RRs which we wanted. */
+
+ qu->answer->rrs.untyped= adns__alloc_interim(qu,qu->answer->rrsz*wantedrrs);
+ if (!qu->answer->rrs.untyped) {
+ adns__query_fail(qu,adns_s_nomemory);
+ return;
+ }
+
+ typei= qu->typei;
+ cbyte= anstart;
+ rrsdata= qu->answer->rrs.bytes;
+
+ pai.ads= qu->ads;
+ pai.qu= qu;
+ pai.serv= serv;
+ pai.dgram= dgram;
+ pai.dglen= dglen;
+ pai.nsstart= nsstart;
+ pai.nscount= nscount;
+ pai.arcount= arcount;
+ pai.now= now;
+
+ for (rri=0, nrrs=0; rri<ancount; rri++) {
+ st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
+ &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
+ &ownermatched);
+ assert(!st); assert(rrtype != -1);
+ if (rrclass != DNS_CLASS_IN ||
+ rrtype != (qu->answer->type & adns_rrt_typemask) ||
+ !ownermatched)
+ continue;
+ adns__update_expires(qu,ttl,now);
+ st= typei->parse(&pai, rdstart,rdstart+rdlength,
+ rrsdata+nrrs*qu->answer->rrsz);
+ if (st) { adns__query_fail(qu,st); return; }
+ if (rdstart==-1) goto x_truncated;
+ nrrs++;
+ }
+ assert(nrrs==wantedrrs);
+ qu->answer->nrrs= nrrs;
+
+ /* This may have generated some child queries ... */
+ if (qu->children.head) {
+ qu->state= query_childw;
+ LIST_LINK_TAIL(ads->childw,qu);
+ return;
+ }
+ adns__query_done(qu);
+ return;
+
+ x_truncated:
+
+ if (!flg_tc) {
+ adns__diag(ads,serv,qu,"server sent datagram which points outside itself");
+ adns__query_fail(qu,adns_s_invalidresponse);
+ return;
+ }
+ qu->flags |= adns_qf_usevc;
+
+ x_restartquery:
+ if (qu->cname_dgram) {
+ st= adns__mkquery_frdgram(qu->ads,&qu->vb,&qu->id,
+ qu->cname_dgram,qu->cname_dglen,qu->cname_begin,
+ qu->answer->type, qu->flags);
+ if (st) { adns__query_fail(qu,st); return; }
+
+ newquery= realloc(qu->query_dgram,qu->vb.used);
+ if (!newquery) { adns__query_fail(qu,adns_s_nomemory); return; }
+
+ qu->query_dgram= newquery;
+ qu->query_dglen= qu->vb.used;
+ memcpy(newquery,qu->vb.buf,qu->vb.used);
+ }
+
+ if (qu->state == query_tcpw) qu->state= query_tosend;
+ qu->retries= 0;
+ adns__reset_preserved(qu);
+ adns__query_send(qu,now);
+}
diff --git a/src/setup.c b/src/setup.c
new file mode 100644
index 0000000..7171f5b
--- /dev/null
+++ b/src/setup.c
@@ -0,0 +1,843 @@
+/*
+ * setup.c
+ * - configuration file parsing
+ * - management of global state
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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 <stdlib.h>
+#include <errno.h>
+#include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/types.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "internal.h"
+
+static void readconfig(adns_state ads, const char *filename, int warnmissing);
+
+static void addserver(adns_state ads, const struct sockaddr *sa, int salen) {
+ int i;
+ adns_rr_addr *ss;
+ char buf[ADNS_ADDR2TEXT_BUFLEN];
+
+ for (i=0; i<ads->nservers; i++) {
+ if (adns__sockaddrs_equal(sa, &ads->servers[i].addr.sa)) {
+ adns__debug(ads,-1,0,"duplicate nameserver %s ignored",
+ adns__sockaddr_ntoa(sa, buf));
+ return;
+ }
+ }
+
+ if (ads->nservers>=MAXSERVERS) {
+ adns__diag(ads,-1,0,"too many nameservers, ignoring %s",
+ adns__sockaddr_ntoa(sa, buf));
+ return;
+ }
+
+ ss= ads->servers+ads->nservers;
+ assert(salen <= sizeof(ss->addr));
+ ss->len = salen;
+ memcpy(&ss->addr, sa, salen);
+ ads->nservers++;
+}
+
+static void freesearchlist(adns_state ads) {
+ if (ads->nsearchlist) free(*ads->searchlist);
+ free(ads->searchlist);
+}
+
+static void saveerr(adns_state ads, int en) {
+ if (!ads->configerrno) ads->configerrno= en;
+}
+
+static void configparseerr(adns_state ads, const char *fn, int lno,
+ const char *fmt, ...) {
+ va_list al;
+
+ saveerr(ads,EINVAL);
+ if (!ads->logfn || (ads->iflags & adns_if_noerrprint)) return;
+
+ if (lno==-1) adns__lprintf(ads,"adns: %s: ",fn);
+ else adns__lprintf(ads,"adns: %s:%d: ",fn,lno);
+ va_start(al,fmt);
+ adns__vlprintf(ads,fmt,al);
+ va_end(al);
+ adns__lprintf(ads,"\n");
+}
+
+static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
+ const char *p, *q;
+
+ p= *bufp_io;
+ while (ctype_whitespace(*p)) p++;
+ if (!*p) return 0;
+
+ q= p;
+ while (*q && !ctype_whitespace(*q)) q++;
+
+ *l_r= q-p;
+ *word_r= p;
+ *bufp_io= q;
+
+ return 1;
+}
+
+static void ccf_nameserver(adns_state ads, const char *fn,
+ int lno, const char *buf) {
+ adns_rr_addr a;
+ char addrbuf[ADNS_ADDR2TEXT_BUFLEN];
+ int err;
+
+ a.len= sizeof(a.addr);
+ err= adns_text2addr(buf,DNS_PORT, 0, &a.addr.sa,&a.len);
+ switch (err) {
+ case 0:
+ break;
+ case EINVAL:
+ configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
+ return;
+ default:
+ configparseerr(ads,fn,lno,"failed to parse nameserver address `%s': %s",
+ buf,strerror(err));
+ return;
+ }
+ adns__debug(ads,-1,0,"using nameserver %s",
+ adns__sockaddr_ntoa(&a.addr.sa, addrbuf));
+ addserver(ads,&a.addr.sa,a.len);
+}
+
+static void ccf_search(adns_state ads, const char *fn,
+ int lno, const char *buf) {
+ const char *bufp, *word;
+ char *newchars, **newptrs, **pp;
+ int count, tl, l;
+
+ if (!buf) return;
+
+ bufp= buf;
+ count= 0;
+ tl= 0;
+ while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
+
+ newptrs= malloc(sizeof(char*)*count);
+ if (!newptrs) { saveerr(ads,errno); return; }
+
+ newchars= malloc(tl);
+ if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
+
+ bufp= buf;
+ pp= newptrs;
+ while (nextword(&bufp,&word,&l)) {
+ *pp++= newchars;
+ memcpy(newchars,word,l);
+ newchars += l;
+ *newchars++ = 0;
+ }
+
+ freesearchlist(ads);
+ ads->nsearchlist= count;
+ ads->searchlist= newptrs;
+}
+
+static int gen_pton(const char *text, int want_af, adns_sockaddr *a) {
+ int err;
+ int len;
+
+ len= sizeof(*a);
+ err= adns_text2addr(text,0, adns_qf_addrlit_scope_forbid,
+ &a->sa, &len);
+ if (err) { assert(err == EINVAL); return 0; }
+ if (want_af != AF_UNSPEC && a->sa.sa_family != want_af) return 0;
+ return 1;
+}
+
+static void ccf_sortlist(adns_state ads, const char *fn,
+ int lno, const char *buf) {
+ const char *word;
+ char tbuf[200], *slash, *ep;
+ const char *maskwhat;
+ struct sortlist *sl;
+ int l;
+ int initial= -1;
+
+ if (!buf) return;
+
+ ads->nsortlist= 0;
+ while (nextword(&buf,&word,&l)) {
+ if (ads->nsortlist >= MAXSORTLIST) {
+ adns__diag(ads,-1,0,"too many sortlist entries,"
+ " ignoring %.*s onwards",l,word);
+ return;
+ }
+
+ if (l >= sizeof(tbuf)) {
+ configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
+ continue;
+ }
+
+ memcpy(tbuf,word,l); tbuf[l]= 0;
+ slash= strchr(tbuf,'/');
+ if (slash) *slash++= 0;
+
+ sl= &ads->sortlist[ads->nsortlist];
+ if (!gen_pton(tbuf, AF_UNSPEC, &sl->base)) {
+ configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
+ continue;
+ }
+
+ if (slash) {
+ if (slash[strspn(slash, "0123456789")]) {
+ maskwhat = "mask";
+ if (!gen_pton(slash, sl->base.sa.sa_family, &sl->mask)) {
+ configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
+ continue;
+ }
+ } else {
+ maskwhat = "prefix length";
+ initial= strtoul(slash,&ep,10);
+ if (*ep || initial>adns__addr_width(sl->base.sa.sa_family)) {
+ configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
+ continue;
+ }
+ sl->mask.sa.sa_family= sl->base.sa.sa_family;
+ adns__prefix_mask(&sl->mask, initial);
+ }
+ } else {
+ maskwhat = "implied prefix length";
+ initial= adns__guess_prefix_length(&sl->base);
+ if (initial < 0) {
+ configparseerr(ads,fn,lno, "network address `%s'"
+ " in sortlist is not in classed ranges,"
+ " must specify mask explicitly", tbuf);
+ continue;
+ }
+ sl->mask.sa.sa_family= sl->base.sa.sa_family;
+ adns__prefix_mask(&sl->mask, initial);
+ }
+
+ if (!adns__addr_matches(sl->base.sa.sa_family,
+ adns__sockaddr_addr(&sl->base.sa),
+ &sl->base,&sl->mask)) {
+ if (initial >= 0) {
+ configparseerr(ads,fn,lno, "%s %d in sortlist"
+ " overlaps address `%s'",maskwhat,initial,tbuf);
+ } else {
+ configparseerr(ads,fn,lno, "%s `%s' in sortlist"
+ " overlaps address `%s'",maskwhat,slash,tbuf);
+ }
+ continue;
+ }
+
+ ads->nsortlist++;
+ }
+}
+
+static void ccf_options(adns_state ads, const char *fn,
+ int lno, const char *buf) {
+ const char *opt, *word, *endword, *endopt;
+ char *ep;
+ unsigned long v;
+ int l;
+
+ if (!buf) return;
+
+#define WORD__IS(s,op) ((endword-word) op (sizeof(s)-1) && \
+ !memcmp(word,s,(sizeof(s)-1)))
+#define WORD_IS(s) (WORD__IS(s,==))
+#define WORD_STARTS(s) (WORD__IS(s,>=) ? ((word+=sizeof(s)-1)) : 0)
+
+ while (nextword(&buf,&word,&l)) {
+ opt=word;
+ endopt=endword=word+l;
+ if (WORD_IS("debug")) {
+ ads->iflags |= adns_if_debug;
+ continue;
+ }
+ if (WORD_STARTS("ndots:")) {
+ v= strtoul(word,&ep,10);
+ if (ep==word || ep != endword || v > INT_MAX) {
+ configparseerr(ads,fn,lno,"option `%.*s' malformed"
+ " or has bad value",l,opt);
+ continue;
+ }
+ ads->searchndots= v;
+ continue;
+ }
+ if (WORD_STARTS("adns_checkc:")) {
+ if (WORD_IS("none")) {
+ ads->iflags &= ~adns_if_checkc_freq;
+ ads->iflags |= adns_if_checkc_entex;
+ } else if (WORD_IS("entex")) {
+ ads->iflags &= ~adns_if_checkc_freq;
+ ads->iflags |= adns_if_checkc_entex;
+ } else if (WORD_IS("freq")) {
+ ads->iflags |= adns_if_checkc_freq;
+ } else {
+ configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
+ "(must be none, entex or freq", word);
+ }
+ continue;
+ }
+ if (WORD_STARTS("adns_af:")) {
+ ads->iflags &= ~adns_if_afmask;
+ if (!WORD_IS("any")) for (;;) {
+ const char *comma= memchr(word,',',endopt-word);
+ endword=comma?comma:endopt;
+ if (WORD_IS("ipv4"))
+ ads->iflags |= adns_if_permit_ipv4;
+ else if (WORD_IS("ipv6"))
+ ads->iflags |= adns_if_permit_ipv6;
+ else {
+ if (ads->config_report_unknown)
+ adns__diag(ads,-1,0,"%s:%d: "
+ "option adns_af has bad value or entry `%.*s' "
+ "(option must be `any', or list of `ipv4',`ipv6')",
+ fn,lno, (int)(endword-word),word);
+ break;
+ }
+ if (!comma) break;
+ word= comma+1;
+ }
+ continue;
+ }
+ if (WORD_IS("adns_ignoreunkcfg")) {
+ ads->config_report_unknown=0;
+ continue;
+ }
+ if (/* adns's query strategy is not configurable */
+ WORD_STARTS("timeout:") ||
+ WORD_STARTS("attempts:") ||
+ WORD_IS("rotate") ||
+ /* adns provides the application with knob for this */
+ WORD_IS("no-check-names") ||
+ /* adns normally does IPv6 if the application wants it; control
+ * this with the adns_af: option if you like */
+ WORD_IS("inet6") ||
+ /* adns does not do edns0 and this is not a problem */
+ WORD_IS("edns0"))
+ continue;
+ if (ads->config_report_unknown)
+ adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,opt);
+ }
+
+#undef WORD__IS
+#undef WORD_IS
+#undef WORD_STARTS
+}
+
+static void ccf_clearnss(adns_state ads, const char *fn,
+ int lno, const char *buf) {
+ ads->nservers= 0;
+}
+
+static void ccf_include(adns_state ads, const char *fn,
+ int lno, const char *buf) {
+ if (!*buf) {
+ configparseerr(ads,fn,lno,"`include' directive with no filename");
+ return;
+ }
+ readconfig(ads,buf,1);
+}
+
+static void ccf_lookup(adns_state ads, const char *fn, int lno,
+ const char *buf) {
+ int found_bind=0;
+ const char *word;
+ int l;
+
+ if (!*buf) {
+ configparseerr(ads,fn,lno,"`lookup' directive with no databases");
+ return;
+ }
+
+ while (nextword(&buf,&word,&l)) {
+ if (l==4 && !memcmp(word,"bind",4)) {
+ found_bind=1;
+ } else if (l==4 && !memcmp(word,"file",4)) {
+ /* ignore this and hope /etc/hosts is not essential */
+ } else if (l==2 && !memcmp(word,"yp",2)) {
+ adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
+ found_bind=-1;
+ } else {
+ if (ads->config_report_unknown)
+ adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
+ fn,lno, l,word);
+ found_bind=-1;
+ }
+ }
+ if (!found_bind)
+ adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
+}
+
+static void ccf_ignore(adns_state ads, const char *fn, int lno,
+ const char *buf) {
+}
+
+static const struct configcommandinfo {
+ const char *name;
+ void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
+} configcommandinfos[]= {
+ { "nameserver", ccf_nameserver },
+ { "domain", ccf_search },
+ { "search", ccf_search },
+ { "sortlist", ccf_sortlist },
+ { "options", ccf_options },
+ { "clearnameservers", ccf_clearnss },
+ { "include", ccf_include },
+ { "lookup", ccf_lookup }, /* OpenBSD */
+ { "lwserver", ccf_ignore }, /* BIND9 lwresd */
+ { 0 }
+};
+
+typedef union {
+ FILE *file;
+ const char *text;
+} getline_ctx;
+
+static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
+ int lno, char *buf, int buflen) {
+ FILE *file= src_io->file;
+ int c, i;
+ char *p;
+
+ p= buf;
+ buflen--;
+ i= 0;
+
+ for (;;) { /* loop over chars */
+ if (i == buflen) {
+ adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
+ goto x_badline;
+ }
+ c= getc(file);
+ if (!c) {
+ adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
+ goto x_badline;
+ } else if (c == '\n') {
+ break;
+ } else if (c == EOF) {
+ if (ferror(file)) {
+ saveerr(ads,errno);
+ adns__diag(ads,-1,0,"%s:%d: read error: %s",
+ filename,lno,strerror(errno));
+ return -1;
+ }
+ if (!i) return -1;
+ break;
+ } else {
+ *p++= c;
+ i++;
+ }
+ }
+
+ *p++= 0;
+ return i;
+
+ x_badline:
+ saveerr(ads,EINVAL);
+ while ((c= getc(file)) != EOF && c != '\n');
+ return -2;
+}
+
+static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
+ int lno, char *buf, int buflen) {
+ const char *cp= src_io->text;
+ int l;
+
+ if (!cp || !*cp) return -1;
+
+ if (*cp == ';' || *cp == '\n') cp++;
+ l= strcspn(cp,";\n");
+ src_io->text = cp+l;
+
+ if (l >= buflen) {
+ adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
+ saveerr(ads,EINVAL);
+ return -2;
+ }
+
+ memcpy(buf,cp,l);
+ buf[l]= 0;
+ return l;
+}
+
+static void readconfiggeneric(adns_state ads, const char *filename,
+ int (*getline)(adns_state ads, getline_ctx*,
+ const char *filename, int lno,
+ char *buf, int buflen),
+ /* Returns >=0 for success, -1 for EOF or error
+ * (error will have been reported), or -2 for
+ * bad line was encountered, try again.
+ */
+ getline_ctx gl_ctx) {
+ char linebuf[2000], *p, *q;
+ int lno, l, dirl;
+ const struct configcommandinfo *ccip;
+
+ for (lno=1;
+ (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
+ lno++) {
+ if (l == -2) continue;
+ while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
+ linebuf[l]= 0;
+ p= linebuf;
+ while (ctype_whitespace(*p)) p++;
+ if (*p == '#' || *p == ';' || !*p) continue;
+ q= p;
+ while (*q && !ctype_whitespace(*q)) q++;
+ dirl= q-p;
+ for (ccip=configcommandinfos;
+ ccip->name &&
+ !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
+ ccip++);
+ if (!ccip->name) {
+ if (ads->config_report_unknown)
+ adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
+ filename,lno,(int)(q-p),p);
+ continue;
+ }
+ while (ctype_whitespace(*q)) q++;
+ ccip->fn(ads,filename,lno,q);
+ }
+}
+
+static const char *instrum_getenv(adns_state ads, const char *envvar) {
+ const char *value;
+
+ value= getenv(envvar);
+ if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
+ else adns__debug(ads,-1,0,"environment variable %s"
+ " set to `%s'",envvar,value);
+ return value;
+}
+
+static void readconfig(adns_state ads, const char *filename, int warnmissing) {
+ getline_ctx gl_ctx;
+
+ gl_ctx.file= fopen(filename,"r");
+ if (!gl_ctx.file) {
+ if (errno == ENOENT) {
+ if (warnmissing)
+ adns__debug(ads,-1,0, "configuration file"
+ " `%s' does not exist",filename);
+ return;
+ }
+ saveerr(ads,errno);
+ adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
+ filename,strerror(errno));
+ return;
+ }
+
+ readconfiggeneric(ads,filename,gl_file,gl_ctx);
+
+ fclose(gl_ctx.file);
+}
+
+static void readconfigtext(adns_state ads, const char *text,
+ const char *showname) {
+ getline_ctx gl_ctx;
+
+ gl_ctx.text= text;
+ readconfiggeneric(ads,showname,gl_text,gl_ctx);
+}
+
+static void readconfigenv(adns_state ads, const char *envvar) {
+ const char *filename;
+
+ if (ads->iflags & adns_if_noenv) {
+ adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
+ return;
+ }
+ filename= instrum_getenv(ads,envvar);
+ if (filename) readconfig(ads,filename,1);
+}
+
+static void readconfigenvtext(adns_state ads, const char *envvar) {
+ const char *textdata;
+
+ if (ads->iflags & adns_if_noenv) {
+ adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
+ return;
+ }
+ textdata= instrum_getenv(ads,envvar);
+ if (textdata) readconfigtext(ads,textdata,envvar);
+}
+
+
+int adns__setnonblock(adns_state ads, int fd) {
+ int r;
+
+ r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
+ r |= O_NONBLOCK;
+ r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
+ return 0;
+}
+
+static int init_begin(adns_state *ads_r, adns_initflags flags,
+ adns_logcallbackfn *logfn, void *logfndata) {
+ adns_state ads;
+ pid_t pid;
+
+ if (flags & ~(adns_initflags)(0x4fff))
+ /* 0x4000 is reserved for `harmless' future expansion */
+ return ENOSYS;
+
+ ads= malloc(sizeof(*ads)); if (!ads) return errno;
+
+ ads->iflags= flags;
+ ads->logfn= logfn;
+ ads->logfndata= logfndata;
+ ads->configerrno= 0;
+ LIST_INIT(ads->udpw);
+ LIST_INIT(ads->tcpw);
+ LIST_INIT(ads->childw);
+ LIST_INIT(ads->output);
+ LIST_INIT(ads->intdone);
+ ads->forallnext= 0;
+ ads->nextid= 0x311f;
+ ads->nudpsockets= 0;
+ ads->tcpsocket= -1;
+ adns__vbuf_init(&ads->tcpsend);
+ adns__vbuf_init(&ads->tcprecv);
+ ads->tcprecv_skip= 0;
+ ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
+ ads->searchndots= 1;
+ ads->tcpstate= server_disconnected;
+ timerclear(&ads->tcptimeout);
+ ads->searchlist= 0;
+ ads->config_report_unknown=1;
+
+ pid= getpid();
+ ads->rand48xsubi[0]= pid;
+ ads->rand48xsubi[1]= (unsigned long)pid >> 16;
+ ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16);
+
+ *ads_r= ads;
+ return 0;
+}
+
+static int init_finish(adns_state ads) {
+ struct sockaddr_in sin;
+ struct protoent *proto;
+ struct udpsocket *udp;
+ int i;
+ int r;
+
+ if (!ads->nservers) {
+ if (ads->logfn && ads->iflags & adns_if_debug)
+ adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n");
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(DNS_PORT);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ addserver(ads,(struct sockaddr *)&sin, sizeof(sin));
+ }
+
+ proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
+ ads->nudpsockets= 0;
+ for (i=0; i<ads->nservers; i++) {
+ if (adns__udpsocket_by_af(ads, ads->servers[i].addr.sa.sa_family))
+ continue;
+ assert(ads->nudpsockets < MAXUDP);
+ udp= &ads->udpsockets[ads->nudpsockets];
+ udp->af= ads->servers[i].addr.sa.sa_family;
+ udp->fd= socket(udp->af,SOCK_DGRAM,proto->p_proto);
+ if (udp->fd < 0) { r= errno; goto x_free; }
+ ads->nudpsockets++;
+ r= adns__setnonblock(ads,udp->fd);
+ if (r) { r= errno; goto x_closeudp; }
+ }
+
+ return 0;
+
+ x_closeudp:
+ for (i=0; i<ads->nudpsockets; i++) close(ads->udpsockets[i].fd);
+ x_free:
+ free(ads);
+ return r;
+}
+
+static void init_abort(adns_state ads) {
+ if (ads->nsearchlist) {
+ free(ads->searchlist[0]);
+ free(ads->searchlist);
+ }
+ free(ads);
+}
+
+static void logfn_file(adns_state ads, void *logfndata,
+ const char *fmt, va_list al) {
+ vfprintf(logfndata,fmt,al);
+}
+
+static int init_files(adns_state *ads_r, adns_initflags flags,
+ adns_logcallbackfn *logfn, void *logfndata) {
+ adns_state ads;
+ const char *res_options, *adns_res_options;
+ int r;
+
+ r= init_begin(&ads, flags, logfn, logfndata);
+ if (r) return r;
+
+ res_options= instrum_getenv(ads,"RES_OPTIONS");
+ adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
+ ccf_options(ads,"RES_OPTIONS",-1,res_options);
+ ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
+
+ readconfig(ads,"/etc/resolv.conf",1);
+ readconfig(ads,"/etc/resolv-adns.conf",0);
+ readconfigenv(ads,"RES_CONF");
+ readconfigenv(ads,"ADNS_RES_CONF");
+
+ readconfigenvtext(ads,"RES_CONF_TEXT");
+ readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
+
+ ccf_options(ads,"RES_OPTIONS",-1,res_options);
+ ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
+
+ ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
+ ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
+
+ if (ads->configerrno && ads->configerrno != EINVAL) {
+ r= ads->configerrno;
+ init_abort(ads);
+ return r;
+ }
+
+ r= init_finish(ads);
+ if (r) return r;
+
+ adns__consistency(ads,0,cc_entex);
+ *ads_r= ads;
+ return 0;
+}
+
+int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
+ return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
+}
+
+static int init_strcfg(adns_state *ads_r, adns_initflags flags,
+ adns_logcallbackfn *logfn, void *logfndata,
+ const char *configtext) {
+ adns_state ads;
+ int r;
+
+ r= init_begin(&ads, flags, logfn, logfndata);
+ if (r) return r;
+
+ readconfigtext(ads,configtext,"<supplied configuration text>");
+ if (ads->configerrno) {
+ r= ads->configerrno;
+ init_abort(ads);
+ return r;
+ }
+
+ r= init_finish(ads); if (r) return r;
+ adns__consistency(ads,0,cc_entex);
+ *ads_r= ads;
+ return 0;
+}
+
+int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
+ FILE *diagfile, const char *configtext) {
+ return init_strcfg(ads_r, flags,
+ diagfile ? logfn_file : 0, diagfile,
+ configtext);
+}
+
+int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
+ const char *configtext /*0=>use default config files*/,
+ adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
+ void *logfndata /*0 with logfn==0 => discard*/) {
+ if (!logfn && logfndata)
+ logfn= logfn_file;
+ if (configtext)
+ return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
+ else
+ return init_files(newstate_r, flags, logfn, logfndata);
+}
+
+void adns_finish(adns_state ads) {
+ int i;
+ adns__consistency(ads,0,cc_entex);
+ for (;;) {
+ if (ads->udpw.head) adns__cancel(ads->udpw.head);
+ else if (ads->tcpw.head) adns__cancel(ads->tcpw.head);
+ else if (ads->childw.head) adns__cancel(ads->childw.head);
+ else if (ads->output.head) adns__cancel(ads->output.head);
+ else if (ads->intdone.head) adns__cancel(ads->output.head);
+ else break;
+ }
+ for (i=0; i<ads->nudpsockets; i++) close(ads->udpsockets[i].fd);
+ if (ads->tcpsocket >= 0) close(ads->tcpsocket);
+ adns__vbuf_free(&ads->tcpsend);
+ adns__vbuf_free(&ads->tcprecv);
+ freesearchlist(ads);
+ free(ads);
+}
+
+void adns_forallqueries_begin(adns_state ads) {
+ adns__consistency(ads,0,cc_entex);
+ ads->forallnext=
+ ads->udpw.head ? ads->udpw.head :
+ ads->tcpw.head ? ads->tcpw.head :
+ ads->childw.head ? ads->childw.head :
+ ads->output.head;
+}
+
+adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
+ adns_query qu, nqu;
+
+ adns__consistency(ads,0,cc_entex);
+ nqu= ads->forallnext;
+ for (;;) {
+ qu= nqu;
+ if (!qu) return 0;
+ if (qu->next) {
+ nqu= qu->next;
+ } else if (qu == ads->udpw.tail) {
+ nqu=
+ ads->tcpw.head ? ads->tcpw.head :
+ ads->childw.head ? ads->childw.head :
+ ads->output.head;
+ } else if (qu == ads->tcpw.tail) {
+ nqu=
+ ads->childw.head ? ads->childw.head :
+ ads->output.head;
+ } else if (qu == ads->childw.tail) {
+ nqu= ads->output.head;
+ } else {
+ nqu= 0;
+ }
+ if (!qu->parent) break;
+ }
+ ads->forallnext= nqu;
+ if (context_r) *context_r= qu->ctx.ext;
+ return qu;
+}
diff --git a/src/transmit.c b/src/transmit.c
new file mode 100644
index 0000000..9cef6db
--- /dev/null
+++ b/src/transmit.c
@@ -0,0 +1,289 @@
+/*
+ * transmit.c
+ * - construct queries
+ * - send queries
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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 <errno.h>
+
+#include <sys/types.h>
+#include <sys/uio.h>
+
+#include "internal.h"
+#include "tvarith.h"
+
+#define MKQUERY_START(vb) (rqp= (vb)->buf+(vb)->used)
+#define MKQUERY_ADDB(b) *rqp++= (b)
+#define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
+#define MKQUERY_STOP(vb) ((vb)->used= rqp-(vb)->buf)
+
+static adns_status mkquery_header(adns_state ads, vbuf *vb,
+ int *id_r, int qdlen) {
+ int id;
+ byte *rqp;
+
+ if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+qdlen+4)) return adns_s_nomemory;
+
+ vb->used= 0;
+ MKQUERY_START(vb);
+
+ *id_r= id= (ads->nextid++) & 0x0ffff;
+ MKQUERY_ADDW(id);
+ MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */
+ MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */
+ MKQUERY_ADDW(1); /* QDCOUNT=1 */
+ MKQUERY_ADDW(0); /* ANCOUNT=0 */
+ MKQUERY_ADDW(0); /* NSCOUNT=0 */
+ MKQUERY_ADDW(0); /* ARCOUNT=0 */
+
+ MKQUERY_STOP(vb);
+
+ return adns_s_ok;
+}
+
+static adns_status mkquery_footer(vbuf *vb, adns_rrtype type) {
+ byte *rqp;
+
+ MKQUERY_START(vb);
+ MKQUERY_ADDW(type & adns_rrt_typemask); /* QTYPE */
+ MKQUERY_ADDW(DNS_CLASS_IN); /* QCLASS=IN */
+ MKQUERY_STOP(vb);
+ assert(vb->used <= vb->avail);
+
+ return adns_s_ok;
+}
+
+static adns_status qdparselabel(adns_state ads,
+ const char **p_io, const char *pe,
+ char label_r[], int *ll_io,
+ adns_queryflags flags) {
+ int ll, c;
+ const char *p;
+
+ ll= 0;
+ p= *p_io;
+
+ while (p!=pe && (c= *p++)!='.') {
+ if (c=='\\') {
+ if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
+ if (ctype_digit(p[0])) {
+ if (p+1==pe || p+2==pe) return adns_s_querydomaininvalid;
+ if (ctype_digit(p[1]) && ctype_digit(p[2])) {
+ c= (*p++ - '0')*100;
+ c += (*p++ - '0')*10;
+ c += (*p++ - '0');
+ if (c >= 256) return adns_s_querydomaininvalid;
+ } else {
+ return adns_s_querydomaininvalid;
+ }
+ } else if (!(c= *p++)) {
+ return adns_s_querydomaininvalid;
+ }
+ }
+ if (ll == *ll_io) return adns_s_querydomaininvalid;
+ label_r[ll++]= c;
+ }
+
+ *p_io= p;
+ *ll_io= ll;
+ return adns_s_ok;
+}
+
+adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
+ const char *owner, int ol,
+ const typeinfo *typei, adns_rrtype type,
+ adns_queryflags flags) {
+ int ll, nbytes;
+ byte label[255];
+ byte *rqp;
+ const char *p, *pe;
+ adns_status st;
+
+ st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
+
+ MKQUERY_START(vb);
+
+ p= owner; pe= owner+ol;
+ nbytes= 0;
+ while (p!=pe) {
+ ll= sizeof(label);
+ st= qdparselabel(ads, &p,pe, label, &ll, flags);
+ if (st) return st;
+ if (!ll) return adns_s_querydomaininvalid;
+ if (ll > DNS_MAXLABEL) return adns_s_querydomaintoolong;
+ nbytes+= ll+1;
+ if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong;
+ MKQUERY_ADDB(ll);
+ memcpy(rqp,label,ll); rqp+= ll;
+ }
+ MKQUERY_ADDB(0);
+
+ MKQUERY_STOP(vb);
+
+ st= mkquery_footer(vb,type);
+
+ return adns_s_ok;
+}
+
+adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
+ const byte *qd_dgram, int qd_dglen,
+ int qd_begin,
+ adns_rrtype type, adns_queryflags flags) {
+ byte *rqp;
+ findlabel_state fls;
+ int lablen, labstart;
+ adns_status st;
+
+ st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
+
+ MKQUERY_START(vb);
+
+ adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
+ for (;;) {
+ st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
+ if (!lablen) break;
+ assert(lablen<255);
+ MKQUERY_ADDB(lablen);
+ memcpy(rqp,qd_dgram+labstart,lablen);
+ rqp+= lablen;
+ }
+ MKQUERY_ADDB(0);
+
+ MKQUERY_STOP(vb);
+
+ st= mkquery_footer(vb,type);
+
+ return adns_s_ok;
+}
+
+void adns__querysend_tcp(adns_query qu, struct timeval now) {
+ byte length[2];
+ struct iovec iov[2];
+ int wr, r;
+ adns_state ads;
+
+ if (qu->ads->tcpstate != server_ok) return;
+
+ assert(qu->state == query_tcpw);
+
+ length[0]= (qu->query_dglen&0x0ff00U) >>8;
+ length[1]= (qu->query_dglen&0x0ff);
+
+ ads= qu->ads;
+ if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2))
+ return;
+
+ qu->retries++;
+
+ /* Reset idle timeout. */
+ ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
+
+ if (ads->tcpsend.used) {
+ wr= 0;
+ } else {
+ iov[0].iov_base= length;
+ iov[0].iov_len= 2;
+ iov[1].iov_base= qu->query_dgram;
+ iov[1].iov_len= qu->query_dglen;
+ adns__sigpipe_protect(qu->ads);
+ wr= writev(qu->ads->tcpsocket,iov,2);
+ adns__sigpipe_unprotect(qu->ads);
+ if (wr < 0) {
+ if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
+ errno == ENOBUFS || errno == ENOMEM)) {
+ adns__tcp_broken(ads,"write",strerror(errno));
+ return;
+ }
+ wr= 0;
+ }
+ }
+
+ if (wr<2) {
+ r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
+ wr= 0;
+ } else {
+ wr-= 2;
+ }
+ if (wr<qu->query_dglen) {
+ r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr);
+ assert(r);
+ }
+}
+
+static void query_usetcp(adns_query qu, struct timeval now) {
+ qu->state= query_tcpw;
+ qu->timeout= now;
+ timevaladd(&qu->timeout,TCPWAITMS);
+ LIST_LINK_TAIL(qu->ads->tcpw,qu);
+ adns__querysend_tcp(qu,now);
+ adns__tcp_tryconnect(qu->ads,now);
+}
+
+struct udpsocket *adns__udpsocket_by_af(adns_state ads, int af) {
+ int i;
+ for (i=0; i<ads->nudpsockets; i++)
+ if (ads->udpsockets[i].af == af) return &ads->udpsockets[i];
+ return 0;
+}
+
+void adns__query_send(adns_query qu, struct timeval now) {
+ int serv, r;
+ adns_state ads;
+ struct udpsocket *udp;
+ adns_rr_addr *addr;
+
+ assert(qu->state == query_tosend);
+ if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
+ query_usetcp(qu,now);
+ return;
+ }
+
+ if (qu->retries >= UDPMAXRETRIES) {
+ adns__query_fail(qu,adns_s_timeout);
+ return;
+ }
+
+ ads= qu->ads;
+ serv= qu->udpnextserver;
+ addr= &ads->servers[serv];
+ udp= adns__udpsocket_by_af(ads, addr->addr.sa.sa_family);
+ assert(udp);
+
+ r= sendto(udp->fd,qu->query_dgram,qu->query_dglen,0,
+ &addr->addr.sa,addr->len);
+ if (r<0 && errno == EMSGSIZE) {
+ qu->retries= 0;
+ query_usetcp(qu,now);
+ return;
+ }
+ if (r<0 && errno != EAGAIN)
+ adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
+
+ qu->timeout= now;
+ timevaladd(&qu->timeout,UDPRETRYMS);
+ qu->udpsent |= (1<<serv);
+ qu->udpnextserver= (serv+1)%ads->nservers;
+ qu->retries++;
+ LIST_LINK_TAIL(ads->udpw,qu);
+}
diff --git a/src/tvarith.h b/src/tvarith.h
new file mode 100644
index 0000000..1492dd6
--- /dev/null
+++ b/src/tvarith.h
@@ -0,0 +1,40 @@
+/*
+ * tvarith.h
+ * - static inline functions for doing arithmetic on timevals
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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.
+ */
+
+#ifndef ADNS_TVARITH_H_INCLUDED
+#define ADNS_TVARITH_H_INCLUDED
+
+static inline void timevaladd(struct timeval *tv_io, long ms) {
+ struct timeval tmp;
+ assert(ms>=0);
+ tmp= *tv_io;
+ tmp.tv_usec += (ms%1000)*1000;
+ tmp.tv_sec += ms/1000;
+ if (tmp.tv_usec >= 1000000) { tmp.tv_sec++; tmp.tv_usec -= 1000000; }
+ *tv_io= tmp;
+}
+
+#endif
diff --git a/src/types.c b/src/types.c
new file mode 100644
index 0000000..d65e155
--- /dev/null
+++ b/src/types.c
@@ -0,0 +1,1712 @@
+/*
+ * types.c
+ * - RR-type-specific code, and the machinery to call it
+ */
+/*
+ * This file is part of adns, which is
+ * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
+ * Copyright (C) 1999-2000,2003,2006 Tony Finch
+ * Copyright (C) 1991 Massachusetts Institute of Technology
+ * (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 <stddef.h>
+#include <stdlib.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "internal.h"
+
+#define R_NOMEM return adns_s_nomemory
+#define CSP_ADDSTR(s) do { \
+ if (!adns__vbuf_appendstr(vb,(s))) R_NOMEM; \
+ } while (0)
+
+/*
+ * order of sections:
+ *
+ * _string (pap)
+ * _textdata, _qstring (csp)
+ * _str (mf,cs)
+ * _intstr (mf,csp,cs)
+ * _manyistr (mf,cs)
+ * _txt (pa)
+ * _inaddr (pa,di,cs
+ * +search_sortlist, dip_genaddr, csp_genaddr)
+ * _in6addr (pa,di,cs)
+ * _addr (pap,pa,di,div,csp,cs,gsz,qs
+ * +search_sortlist_sa, dip_sockaddr,
+ * addr_rrtypes, addr_submit, icb_addr)
+ * _domain (pap,csp,cs)
+ * _dom_raw (pa)
+ * _host_raw (pa)
+ * _hostaddr (pap,pa,dip,di,mfp,mf,csp,cs
+ * +pap_findaddrs, icb_hostaddr)
+ * _mx_raw (pa,di)
+ * _mx (pa,di)
+ * _inthostaddr (mf,cs)
+ * _inthost (cs)
+ * _ptr (ckl,pa +icb_ptr)
+ * _strpair (mf)
+ * _intstrpair (mf)
+ * _hinfo (pa)
+ * _mailbox (pap,csp +pap_mailbox822)
+ * _rp (pa,cs)
+ * _soa (pa,mf,cs)
+ * _srv* (ckl,(pap),pa*2,mf*2,di,(csp),cs*2,postsort)
+ * _byteblock (mf)
+ * _opaque (pa,cs)
+ * _flat (mf)
+ *
+ * within each section:
+ * ckl_*
+ * pap_*
+ * pa_*
+ * dip_*
+ * di_*
+ * mfp_*
+ * mf_*
+ * csp_*
+ * cs_*
+ * gsz_*
+ * postsort_*
+ * qs_*
+ */
+
+/*
+ * _qstring (pap,csp)
+ */
+
+static adns_status pap_qstring(const parseinfo *pai, int *cbyte_io, int max,
+ int *len_r, char **str_r) {
+ /* Neither len_r nor str_r may be null.
+ * End of datagram (overrun) is indicated by returning adns_s_invaliddata;
+ */
+ const byte *dgram= pai->dgram;
+ int l, cbyte;
+ char *str;
+
+ cbyte= *cbyte_io;
+
+ if (cbyte >= max) return adns_s_invaliddata;
+ GET_B(cbyte,l);
+ if (cbyte+l > max) return adns_s_invaliddata;
+
+ str= adns__alloc_interim(pai->qu, l+1);
+ if (!str) R_NOMEM;
+
+ str[l]= 0;
+ memcpy(str,dgram+cbyte,l);
+
+ *len_r= l;
+ *str_r= str;
+ *cbyte_io= cbyte+l;
+
+ return adns_s_ok;
+}
+
+static adns_status csp_qstring(vbuf *vb, const char *dp, int len) {
+ unsigned char ch;
+ char buf[10];
+ int cn;
+
+ CSP_ADDSTR("\"");
+ for (cn=0; cn<len; cn++) {
+ ch= *dp++;
+ if (ch == '\\') {
+ CSP_ADDSTR("\\\\");
+ } else if (ch == '"') {
+ CSP_ADDSTR("\\\"");
+ } else if (ch >= 32 && ch <= 126) {
+ if (!adns__vbuf_append(vb,&ch,1)) R_NOMEM;
+ } else {
+ sprintf(buf,"\\x%02x",ch);
+ CSP_ADDSTR(buf);
+ }
+ }
+ CSP_ADDSTR("\"");
+
+ return adns_s_ok;
+}
+
+/*
+ * _str (mf)
+ */
+
+static void mf_str(adns_query qu, void *datap) {
+ char **rrp= datap;
+
+ adns__makefinal_str(qu,rrp);
+}
+
+/*
+ * _intstr (mf)
+ */
+
+static void mf_intstr(adns_query qu, void *datap) {
+ adns_rr_intstr *rrp= datap;
+
+ adns__makefinal_str(qu,&rrp->str);
+}
+
+/*
+ * _manyistr (mf)
+ */
+
+static void mf_manyistr(adns_query qu, void *datap) {
+ adns_rr_intstr **rrp= datap;
+ adns_rr_intstr *te, *table;
+ void *tablev;
+ int tc;
+
+ for (tc=0, te= *rrp; te->i >= 0; te++, tc++);
+ tablev= *rrp;
+ adns__makefinal_block(qu,&tablev,sizeof(*te)*(tc+1));
+ *rrp= table= tablev;
+ for (te= *rrp; te->i >= 0; te++)
+ adns__makefinal_str(qu,&te->str);
+}
+
+/*
+ * _txt (pa,cs)
+ */
+
+static adns_status pa_txt(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ adns_rr_intstr **rrp= datap, *table, *te;
+ const byte *dgram= pai->dgram;
+ int ti, tc, l, startbyte;
+ adns_status st;
+
+ startbyte= cbyte;
+ if (cbyte >= max) return adns_s_invaliddata;
+ tc= 0;
+ while (cbyte < max) {
+ GET_B(cbyte,l);
+ cbyte+= l;
+ tc++;
+ }
+ if (cbyte != max || !tc) return adns_s_invaliddata;
+
+ table= adns__alloc_interim(pai->qu,sizeof(*table)*(tc+1));
+ if (!table) R_NOMEM;
+
+ for (cbyte=startbyte, ti=0, te=table; ti<tc; ti++, te++) {
+ st= pap_qstring(pai, &cbyte, max, &te->i, &te->str);
+ if (st) return st;
+ }
+ assert(cbyte == max);
+
+ te->i= -1;
+ te->str= 0;
+
+ *rrp= table;
+ return adns_s_ok;
+}
+
+static adns_status cs_txt(vbuf *vb, const void *datap) {
+ const adns_rr_intstr *const *rrp= datap;
+ const adns_rr_intstr *current;
+ adns_status st;
+ int spc;
+
+ for (current= *rrp, spc=0; current->i >= 0; current++, spc=1) {
+ if (spc) CSP_ADDSTR(" ");
+ st= csp_qstring(vb,current->str,current->i); if (st) return st;
+ }
+ return adns_s_ok;
+}
+
+/*
+ * _hinfo (cs)
+ */
+
+static adns_status cs_hinfo(vbuf *vb, const void *datap) {
+ const adns_rr_intstrpair *rrp= datap;
+ adns_status st;
+
+ st= csp_qstring(vb,rrp->array[0].str,rrp->array[0].i); if (st) return st;
+ CSP_ADDSTR(" ");
+ st= csp_qstring(vb,rrp->array[1].str,rrp->array[1].i); if (st) return st;
+ return adns_s_ok;
+}
+
+/*
+ * _inaddr (pa,di,cs +search_sortlist, dip_genaddr, csp_genaddr)
+ */
+
+static adns_status pa_inaddr(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ struct in_addr *storeto= datap;
+
+ if (max-cbyte != 4) return adns_s_invaliddata;
+ memcpy(storeto, pai->dgram + cbyte, 4);
+ return adns_s_ok;
+}
+
+static int search_sortlist(adns_state ads, int af, const void *ad) {
+ const struct sortlist *slp;
+ struct in_addr a4;
+ int i;
+ int v6mappedp= 0;
+
+ if (af == AF_INET6) {
+ const struct in6_addr *a6= ad;
+ if (IN6_IS_ADDR_V4MAPPED(a6)) {
+ a4.s_addr= htonl(((unsigned long)a6->s6_addr[12] << 24) |
+ ((unsigned long)a6->s6_addr[13] << 16) |
+ ((unsigned long)a6->s6_addr[14] << 8) |
+ ((unsigned long)a6->s6_addr[15] << 0));
+ v6mappedp= 1;
+ }
+ }
+
+ for (i=0, slp=ads->sortlist;
+ i<ads->nsortlist &&
+ !adns__addr_matches(af,ad, &slp->base,&slp->mask) &&
+ !(v6mappedp &&
+ adns__addr_matches(AF_INET,&a4, &slp->base,&slp->mask));
+ i++, slp++);
+ return i;
+}
+
+static int dip_genaddr(adns_state ads, int af, const void *a, const void *b) {
+ int ai, bi;
+
+ if (!ads->nsortlist) return 0;
+
+ ai= search_sortlist(ads,af,a);
+ bi= search_sortlist(ads,af,b);
+ return bi<ai;
+}
+
+static int di_inaddr(adns_state ads,
+ const void *datap_a, const void *datap_b) {
+ return dip_genaddr(ads,AF_INET,datap_a,datap_b);
+}
+
+static adns_status csp_genaddr(vbuf *vb, int af, const void *p) {
+ char buf[ADNS_ADDR2TEXT_BUFLEN];
+ int len= sizeof(buf);
+ adns_rr_addr a;
+ int err;
+
+ memset(&a, 0, sizeof(a));
+ a.addr.sa.sa_family= af;
+ adns__addr_inject(p, &a.addr);
+ err= adns_addr2text(&a.addr.sa,0, buf,&len, 0); assert(!err);
+ CSP_ADDSTR(buf);
+ return adns_s_ok;
+}
+
+static adns_status cs_inaddr(vbuf *vb, const void *datap) {
+ return csp_genaddr(vb, AF_INET,datap);
+}
+
+/*
+ * _in6addr (pa,di,cs)
+ */
+
+static adns_status pa_in6addr(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ struct in6_addr *storeto= datap;
+
+ if (max-cbyte != 16) return adns_s_invaliddata;
+ memcpy(storeto->s6_addr, pai->dgram + cbyte, 16);
+ return adns_s_ok;
+}
+
+static int di_in6addr(adns_state ads,
+ const void *datap_a, const void *datap_b) {
+ return dip_genaddr(ads,AF_INET6,datap_a,datap_b);
+}
+
+static adns_status cs_in6addr(vbuf *vb, const void *datap) {
+ return csp_genaddr(vb,AF_INET6,datap);
+}
+
+/*
+ * _addr (pap,pa,di,div,csp,cs,gsz,qs
+ * +search_sortlist_sa, dip_sockaddr, addr_rrtypes,
+ * addr_submit, icb_addr)
+ */
+
+static const typeinfo tinfo_addrsub;
+
+#define ADDR_RRTYPES(_) _(a) _(aaaa)
+
+static const adns_rrtype addr_all_rrtypes[] = {
+#define RRTY_CODE(ty) adns_r_##ty,
+ ADDR_RRTYPES(RRTY_CODE)
+#undef RRTY_CODE
+};
+
+enum {
+#define RRTY_INDEX(ty) addr__ri_##ty,
+ ADDR_RRTYPES(RRTY_INDEX)
+#undef RRTY_INDEX
+ addr_nrrtypes,
+#define RRTY_FLAG(ty) addr_rf_##ty = 1 << addr__ri_##ty,
+ ADDR_RRTYPES(RRTY_FLAG)
+ addr__rrty_eat_final_comma
+#undef RRTY_FLAG
+};
+
+static unsigned addr_rrtypeflag(adns_rrtype type) {
+ int i;
+
+ type &= adns_rrt_typemask;
+ for (i=0; i<addr_nrrtypes; i++)
+ if (type==addr_all_rrtypes[i])
+ return 1 << i;
+ return 0;
+}
+
+/* About CNAME handling in addr queries.
+ *
+ * A user-level addr query is translated into a number of protocol-level
+ * queries, and its job is to reassemble the results. This gets tricky if
+ * the answers aren't consistent. In particular, if the answers report
+ * inconsistent indirection via CNAME records (e.g., different CNAMEs, or
+ * some indirect via a CNAME, and some don't) then we have trouble.
+ *
+ * Once we've received an answer, even if it was NODATA, we set
+ * adns__qf_addr_answer on the parent query. This will let us detect a
+ * conflict between a no-CNAME-with-NODATA reply and a subsequent CNAME.
+ *
+ * If we detect a conflict of any kind, then at least one answer came back
+ * with a CNAME record, so we pick the first such answer (somewhat
+ * arbitrarily) as being the `right' canonical name, and set this in the
+ * parent query's answer->cname slot. We discard address records from the
+ * wrong name. And finally we cancel the outstanding child queries, and
+ * resubmit address queries for the address families we don't yet have, with
+ * adns__qf_addr_cname set so that we know that we're in the fixup state.
+ */
+
+static adns_status pap_addr(const parseinfo *pai, int in_rrty, size_t out_rrsz,
+ int *cbyte_io, int cbyte_max, adns_rr_addr *out) {
+ int in_addrlen;
+ int out_af, out_salen;
+ struct in6_addr v6map;
+
+ const void *use_addr= pai->dgram + *cbyte_io;
+
+ switch (in_rrty) {
+ case adns_r_a: in_addrlen= 4; out_af= AF_INET; break;
+ case adns_r_aaaa: in_addrlen= 16; out_af= AF_INET6; break;
+ default: abort();
+ }
+
+ if ((*cbyte_io + in_addrlen) != cbyte_max) return adns_s_invaliddata;
+
+ if (out_af==AF_INET &&
+ (pai->qu->flags & adns_qf_ipv6_mapv4) &&
+ (pai->qu->answer->type & adns__qtf_bigaddr)) {
+ memset(v6map.s6_addr + 0, 0x00, 10);
+ memset(v6map.s6_addr + 10, 0xff, 2);
+ memcpy(v6map.s6_addr + 12, use_addr, 4);
+ use_addr= v6map.s6_addr;
+ out_af= AF_INET6;
+ }
+
+ switch (out_af) {
+ case AF_INET: out_salen= sizeof(out->addr.inet); break;
+ case AF_INET6: out_salen= sizeof(out->addr.inet6); break;
+ default: abort();
+ }
+
+ assert(offsetof(adns_rr_addr, addr) + out_salen <= out_rrsz);
+
+ memset(&out->addr, 0, out_salen);
+ out->len= out_salen;
+ out->addr.sa.sa_family= out_af;
+ adns__addr_inject(use_addr, &out->addr);
+
+ *cbyte_io += in_addrlen;
+ return adns_s_ok;
+}
+
+static adns_status pa_addr(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ int err= pap_addr(pai, pai->qu->answer->type & adns_rrt_typemask,
+ pai->qu->answer->rrsz, &cbyte, max, datap);
+ if (err) return err;
+ if (cbyte != max) return adns_s_invaliddata;
+ return adns_s_ok;
+}
+
+static int search_sortlist_sa(adns_state ads, const struct sockaddr *sa) {
+ const void *pa = adns__sockaddr_addr(sa);
+ return search_sortlist(ads, sa->sa_family, pa);
+}
+
+static int dip_sockaddr(adns_state ads,
+ const struct sockaddr *sa,
+ const struct sockaddr *sb) {
+ if (!ads->sortlist) return 0;
+ return search_sortlist_sa(ads, sa) > search_sortlist_sa(ads, sb);
+}
+
+static int di_addr(adns_state ads, const void *datap_a, const void *datap_b) {
+ const adns_rr_addr *ap= datap_a, *bp= datap_b;
+ return dip_sockaddr(ads, &ap->addr.sa, &bp->addr.sa);
+}
+
+static int div_addr(void *context, const void *datap_a, const void *datap_b) {
+ const adns_state ads= context;
+
+ return di_addr(ads, datap_a, datap_b);
+}
+
+static adns_status csp_addr(vbuf *vb, const adns_rr_addr *rrp) {
+ char buf[ADNS_ADDR2TEXT_BUFLEN];
+ int len= sizeof(buf);
+ int err;
+
+ switch (rrp->addr.inet.sin_family) {
+ case AF_INET:
+ CSP_ADDSTR("INET ");
+ goto a2t;
+ case AF_INET6:
+ CSP_ADDSTR("INET6 ");
+ goto a2t;
+ a2t:
+ err= adns_addr2text(&rrp->addr.sa,0, buf,&len, 0); assert(!err);
+ CSP_ADDSTR(buf);
+ break;
+ default:
+ sprintf(buf,"AF=%u",rrp->addr.sa.sa_family);
+ CSP_ADDSTR(buf);
+ break;
+ }
+ return adns_s_ok;
+}
+
+static adns_status cs_addr(vbuf *vb, const void *datap) {
+ const adns_rr_addr *rrp= datap;
+
+ return csp_addr(vb,rrp);
+}
+
+static int gsz_addr(const typeinfo *typei, adns_rrtype type) {
+ return type & adns__qtf_bigaddr ?
+ sizeof(adns_rr_addr) : sizeof(adns_rr_addr_v4only);
+}
+
+static unsigned addr_rrtypes(adns_state ads, adns_rrtype type,
+ adns_queryflags qf) {
+ /* Return a mask of addr_rf_... flags indicating which address families are
+ * wanted, given a query type and flags.
+ */
+
+ adns_queryflags permitaf= 0;
+ unsigned want= 0;
+
+ if (!(type & adns__qtf_bigaddr))
+ qf= (qf & ~adns_qf_want_allaf) | adns_qf_want_ipv4;
+ else {
+ if (!(qf & adns_qf_want_allaf)) {
+ qf |= (type & adns__qtf_manyaf) ?
+ adns_qf_want_allaf : adns_qf_want_ipv4;
+ }
+ if (ads->iflags & adns_if_permit_ipv4) permitaf |= adns_qf_want_ipv4;
+ if (ads->iflags & adns_if_permit_ipv6) permitaf |= adns_qf_want_ipv6;
+ if (qf & permitaf) qf &= permitaf | ~adns_qf_want_allaf;
+ }
+
+ if (qf & adns_qf_want_ipv4) want |= addr_rf_a;
+ if (qf & adns_qf_want_ipv6) want |= addr_rf_aaaa;
+
+ return want;
+}
+
+static void icb_addr(adns_query parent, adns_query child);
+
+static void addr_subqueries(adns_query qu, struct timeval now,
+ adns_queryflags qf_extra,
+ const byte *qd_dgram, int qd_dglen) {
+ int i, err, id;
+ adns_query cqu;
+ adns_queryflags qf= (qu->flags & ~adns_qf_search) | qf_extra;
+ adns_rrtype qtf= qu->answer->type & adns__qtf_deref;
+ unsigned which= qu->ctx.tinfo.addr.want & ~qu->ctx.tinfo.addr.have;
+ qcontext ctx;
+
+ memset(&ctx, 0, sizeof(ctx));
+ ctx.callback= icb_addr;
+ for (i=0; i<addr_nrrtypes; i++) {
+ if (!(which & (1 << i))) continue;
+ err= adns__mkquery_frdgram(qu->ads, &qu->vb, &id, qd_dgram,qd_dglen,
+ DNS_HDRSIZE, addr_all_rrtypes[i], qf);
+ if (err) goto x_error;
+ err= adns__internal_submit(qu->ads, &cqu, qu, &tinfo_addrsub,
+ addr_all_rrtypes[i] | qtf,
+ &qu->vb, id, qf, now, &ctx);
+ if (err) goto x_error;
+ cqu->answer->rrsz= qu->answer->rrsz;
+ }
+ qu->state= query_childw;
+ LIST_LINK_TAIL(qu->ads->childw, qu);
+ return;
+
+x_error:
+ adns__query_fail(qu, err);
+}
+
+static adns_status addr_submit(adns_query parent, adns_query *query_r,
+ vbuf *qumsg_vb, int id, unsigned want,
+ adns_queryflags flags, struct timeval now,
+ qcontext *ctx) {
+ /* This is effectively a substitute for adns__internal_submit, intended for
+ * the case where the caller (possibly) only wants a subset of the
+ * available record types. The memory management and callback rules are
+ * the same as for adns__internal_submit.
+ *
+ * Some differences: the query is linked onto the parent's children
+ * list before exit (though the parent's state is not changed, and
+ * it is not linked into the childw list queue); and we set the
+ * `tinfo' portion of the context structure (yes, modifying *ctx),
+ * since this is, in fact, the main purpose of this function.
+ */
+
+ adns_state ads= parent->ads;
+ adns_query qu;
+ adns_status err;
+ adns_rrtype type= ((adns_r_addr & adns_rrt_reprmask) |
+ (parent->answer->type & ~adns_rrt_reprmask));
+
+ ctx->tinfo.addr.want= want;
+ ctx->tinfo.addr.have= 0;
+ err= adns__internal_submit(ads, &qu, parent, adns__findtype(adns_r_addr),
+ type, qumsg_vb, id, flags, now, ctx);
+ if (err) return err;
+
+ *query_r= qu;
+ return adns_s_ok;
+}
+
+static adns_status append_addrs(adns_query qu, size_t rrsz,
+ adns_rr_addr **dp, int *dlen,
+ const adns_rr_addr *sp, int slen) {
+ /* Append a vector of slen addr records, each of size rrsz, starting at ap,
+ * to a vector starting at *dp, of length *dlen. On successful completion,
+ * *dp and *dlen are updated.
+ */
+
+ size_t drrsz= *dlen*rrsz, srrsz= slen*rrsz;
+ byte *p;
+
+ if (!slen) return adns_s_ok;
+ p= adns__alloc_interim(qu, drrsz + srrsz);
+ if (!p) R_NOMEM;
+ if (*dlen) {
+ memcpy(p, *dp, drrsz);
+ adns__free_interim(qu, *dp);
+ }
+ memcpy(p + drrsz, sp, srrsz);
+ *dlen += slen;
+ *dp= (adns_rr_addr *)p;
+ return adns_s_ok;
+}
+
+static void propagate_ttl(adns_query to, adns_query from)
+ { if (to->expires > from->expires) to->expires= from->expires; }
+
+static adns_status copy_cname_from_child(adns_query parent, adns_query child) {
+ adns_answer *pans= parent->answer, *cans= child->answer;
+ size_t n= strlen(cans->cname) + 1;
+
+ pans->cname= adns__alloc_preserved(parent, n);
+ if (!pans->cname) R_NOMEM;
+ memcpy(pans->cname, cans->cname, n);
+ return adns_s_ok;
+}
+
+static void done_addr_type(adns_query qu, adns_rrtype type) {
+ unsigned f= addr_rrtypeflag(type);
+ assert(f); qu->ctx.tinfo.addr.have |= f;
+}
+
+static void icb_addr(adns_query parent, adns_query child) {
+ adns_state ads= parent->ads;
+ adns_answer *pans= parent->answer, *cans= child->answer;
+ struct timeval now;
+ adns_status err;
+ adns_queryflags qf;
+ int id, r;
+
+ propagate_ttl(parent, child);
+
+ if (!(child->flags & adns__qf_addr_cname) &&
+ (parent->flags & adns__qf_addr_answer) &&
+ (!!pans->cname != !!cans->cname ||
+ (pans->cname && strcmp(pans->cname, cans->cname)))) {
+ /* We've detected an inconsistency in CNAME records, and must deploy
+ * countermeasures.
+ */
+
+ if (!pans->cname) {
+ /* The child has a CNAME record, but the parent doesn't. We must
+ * discard all of the parent's addresses, and substitute the child's.
+ */
+
+ assert(pans->rrsz == cans->rrsz);
+ adns__free_interim(parent, pans->rrs.bytes);
+ adns__transfer_interim(child, parent, cans->rrs.bytes);
+ pans->rrs.bytes= cans->rrs.bytes;
+ pans->nrrs= cans->nrrs;
+ parent->ctx.tinfo.addr.have= 0;
+ done_addr_type(parent, cans->type);
+ err= copy_cname_from_child(parent, child); if (err) goto x_err;
+ }
+
+ /* We've settled on the CNAME (now) associated with the parent, which
+ * already has appropriate address records. Build a query datagram for
+ * this name so that we can issue child queries for the missing address
+ * families. The child's vbuf looks handy for this.
+ */
+ err= adns__mkquery(ads, &child->vb, &id, pans->cname,
+ strlen(pans->cname), &tinfo_addrsub,
+ adns_r_addr, parent->flags);
+ if (err) goto x_err;
+
+ /* Now cancel the remaining children, and try again with the CNAME we've
+ * settled on.
+ */
+ adns__cancel_children(parent);
+ r= gettimeofday(&now, 0); if (r) goto x_gtod;
+ qf= adns__qf_addr_cname;
+ if (!(parent->flags & adns_qf_cname_loose)) qf |= adns_qf_cname_forbid;
+ addr_subqueries(parent, now, qf, child->vb.buf, child->vb.used);
+ return;
+ }
+
+ if (cans->cname && !pans->cname) {
+ err= copy_cname_from_child(parent, child);
+ if (err) goto x_err;
+ }
+
+ if ((parent->flags & adns_qf_search) &&
+ !pans->cname && cans->status == adns_s_nxdomain) {
+ /* We're searching a list of suffixes, and the name doesn't exist. Try
+ * the next one.
+ */
+
+ adns__cancel_children(parent);
+ adns__free_interim(parent, pans->rrs.bytes);
+ pans->rrs.bytes= 0; pans->nrrs= 0;
+ r= gettimeofday(&now, 0); if (r) goto x_gtod;
+ adns__search_next(ads, parent, now);
+ return;
+ }
+
+ if (cans->status && cans->status != adns_s_nodata)
+ { err= cans->status; goto x_err; }
+
+ assert(pans->rrsz == cans->rrsz);
+ err= append_addrs(parent, pans->rrsz,
+ &pans->rrs.addr, &pans->nrrs,
+ cans->rrs.addr, cans->nrrs);
+ if (err) goto x_err;
+ done_addr_type(parent, cans->type);
+
+ if (parent->children.head) LIST_LINK_TAIL(ads->childw, parent);
+ else if (!pans->nrrs) adns__query_fail(parent, adns_s_nodata);
+ else adns__query_done(parent);
+ parent->flags |= adns__qf_addr_answer;
+ return;
+
+x_gtod:
+ /* We have our own error handling, because adns__must_gettimeofday
+ * handles errors by calling adns_globalsystemfailure, which would
+ * reenter the query processing logic. */
+ adns__diag(ads, -1, parent, "gettimeofday failed: %s", strerror(errno));
+ err= adns_s_systemfail;
+ goto x_err;
+
+x_err:
+ adns__query_fail(parent, err);
+}
+
+static void qs_addr(adns_query qu, struct timeval now) {
+ if (!qu->ctx.tinfo.addr.want) {
+ qu->ctx.tinfo.addr.want= addr_rrtypes(qu->ads, qu->answer->type,
+ qu->flags);
+ qu->ctx.tinfo.addr.have= 0;
+ }
+ addr_subqueries(qu, now, 0, qu->query_dgram, qu->query_dglen);
+}
+
+/*
+ * _domain (pap,csp,cs)
+ * _dom_raw (pa)
+ */
+
+static adns_status pap_domain(const parseinfo *pai, int *cbyte_io, int max,
+ char **domain_r, parsedomain_flags flags) {
+ adns_status st;
+ char *dm;
+
+ st= adns__parse_domain(pai->qu->ads, pai->serv, pai->qu, &pai->qu->vb, flags,
+ pai->dgram,pai->dglen, cbyte_io, max);
+ if (st) return st;
+ if (!pai->qu->vb.used) return adns_s_invaliddata;
+
+ dm= adns__alloc_interim(pai->qu, pai->qu->vb.used+1);
+ if (!dm) R_NOMEM;
+
+ dm[pai->qu->vb.used]= 0;
+ memcpy(dm,pai->qu->vb.buf,pai->qu->vb.used);
+
+ *domain_r= dm;
+ return adns_s_ok;
+}
+
+static adns_status csp_domain(vbuf *vb, const char *domain) {
+ CSP_ADDSTR(domain);
+ if (!*domain) CSP_ADDSTR(".");
+ return adns_s_ok;
+}
+
+static adns_status cs_domain(vbuf *vb, const void *datap) {
+ const char *const *domainp= datap;
+ return csp_domain(vb,*domainp);
+}
+
+static adns_status pa_dom_raw(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ char **rrp= datap;
+ adns_status st;
+
+ st= pap_domain(pai, &cbyte, max, rrp, pdf_quoteok);
+ if (st) return st;
+
+ if (cbyte != max) return adns_s_invaliddata;
+ return adns_s_ok;
+}
+
+/*
+ * _host_raw (pa)
+ */
+
+static adns_status pa_host_raw(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ char **rrp= datap;
+ adns_status st;
+
+ st= pap_domain(pai, &cbyte, max, rrp,
+ pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
+ if (st) return st;
+
+ if (cbyte != max) return adns_s_invaliddata;
+ return adns_s_ok;
+}
+
+/*
+ * _hostaddr (pap,pa,dip,di,mfp,mf,csp,cs +pap_findaddrs, icb_hostaddr)
+ */
+
+static adns_status pap_findaddrs(const parseinfo *pai, adns_rr_hostaddr *ha,
+ unsigned *want_io, size_t addrsz,
+ int *cbyte_io, int count, int dmstart) {
+ int rri, naddrs;
+ unsigned typef, want= *want_io, need= want;
+ int type, class, rdlen, rdend, rdstart, ownermatched;
+ unsigned long ttl;
+ adns_status st;
+
+ for (rri=0, naddrs=0; rri<count; rri++) {
+ st= adns__findrr_anychk(pai->qu, pai->serv, pai->dgram,
+ pai->dglen, cbyte_io,
+ &type, &class, &ttl, &rdlen, &rdstart,
+ pai->dgram, pai->dglen, dmstart, &ownermatched);
+ if (st) return st;
+ if (!ownermatched || class != DNS_CLASS_IN) continue;
+ typef= addr_rrtypeflag(type);
+ if (!(want & typef)) continue;
+ need &= ~typef;
+ if (!adns__vbuf_ensure(&pai->qu->vb, (naddrs+1)*addrsz)) R_NOMEM;
+ adns__update_expires(pai->qu,ttl,pai->now);
+ rdend= rdstart + rdlen;
+ st= pap_addr(pai, type, addrsz, &rdstart, rdend,
+ (adns_rr_addr *)(pai->qu->vb.buf + naddrs*addrsz));
+ if (st) return st;
+ if (rdstart != rdend) return adns_s_invaliddata;
+ naddrs++;
+ }
+ if (naddrs > 0) {
+ st= append_addrs(pai->qu, addrsz, &ha->addrs, &ha->naddrs,
+ (const adns_rr_addr *)pai->qu->vb.buf, naddrs);
+ if (st) return st;
+ ha->astatus= adns_s_ok;
+
+ if (!need) {
+ adns__isort(ha->addrs, naddrs, addrsz, pai->qu->vb.buf,
+ div_addr, pai->ads);
+ }
+ }
+ *want_io= need;
+ return adns_s_ok;
+}
+
+static void icb_hostaddr(adns_query parent, adns_query child) {
+ adns_answer *cans= child->answer;
+ adns_rr_hostaddr *rrp= child->ctx.pinfo.hostaddr;
+ adns_state ads= parent->ads;
+ adns_status st;
+ size_t addrsz= gsz_addr(0, parent->answer->type);
+
+ st= cans->status == adns_s_nodata ? adns_s_ok : cans->status;
+ if (st) goto done;
+ propagate_ttl(parent, child);
+
+ assert(addrsz == cans->rrsz);
+ st= append_addrs(parent, addrsz,
+ &rrp->addrs, &rrp->naddrs,
+ cans->rrs.addr, cans->nrrs);
+ if (st) goto done;
+ if (!rrp->naddrs) { st= adns_s_nodata; goto done; }
+
+ if (!adns__vbuf_ensure(&parent->vb, addrsz))
+ { st= adns_s_nomemory; goto done; }
+ adns__isort(rrp->addrs, rrp->naddrs, addrsz, parent->vb.buf,
+ div_addr, ads);
+
+done:
+ if (st) {
+ adns__free_interim(parent, rrp->addrs);
+ rrp->naddrs= (st>0 && st<=adns_s_max_tempfail) ? -1 : 0;
+ }
+
+ rrp->astatus= st;
+ if (parent->children.head) {
+ LIST_LINK_TAIL(ads->childw,parent);
+ } else {
+ adns__query_done(parent);
+ }
+}
+
+static adns_status pap_hostaddr(const parseinfo *pai, int *cbyte_io,
+ int max, adns_rr_hostaddr *rrp) {
+ adns_status st;
+ int dmstart, cbyte;
+ qcontext ctx;
+ int id;
+ adns_query nqu;
+ adns_queryflags nflags;
+ unsigned want;
+ size_t addrsz= gsz_addr(0, pai->qu->answer->type);
+
+ dmstart= cbyte= *cbyte_io;
+ st= pap_domain(pai, &cbyte, max, &rrp->host,
+ pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
+ if (st) return st;
+ *cbyte_io= cbyte;
+
+ rrp->astatus= adns_s_ok;
+ rrp->naddrs= 0;
+ rrp->addrs= 0;
+
+ cbyte= pai->nsstart;
+
+ want= addr_rrtypes(pai->ads, pai->qu->answer->type, pai->qu->flags);
+
+ st= pap_findaddrs(pai, rrp, &want, addrsz, &cbyte, pai->nscount, dmstart);
+ if (st) return st;
+ if (!want) return adns_s_ok;
+
+ st= pap_findaddrs(pai, rrp, &want, addrsz, &cbyte, pai->arcount, dmstart);
+ if (st) return st;
+ if (!want) return adns_s_ok;
+
+ st= adns__mkquery_frdgram(pai->ads, &pai->qu->vb, &id,
+ pai->dgram, pai->dglen, dmstart,
+ adns_r_addr, adns_qf_quoteok_query);
+ if (st) return st;
+
+ ctx.ext= 0;
+ ctx.callback= icb_hostaddr;
+ ctx.pinfo.hostaddr= rrp;
+
+ nflags= adns_qf_quoteok_query | (pai->qu->flags & (adns_qf_want_allaf |
+ adns_qf_ipv6_mapv4));
+ if (!(pai->qu->flags & adns_qf_cname_loose)) nflags |= adns_qf_cname_forbid;
+
+ st= addr_submit(pai->qu, &nqu, &pai->qu->vb, id, want,
+ nflags, pai->now, &ctx);
+ if (st) return st;
+
+ return adns_s_ok;
+}
+
+static adns_status pa_hostaddr(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ adns_rr_hostaddr *rrp= datap;
+ adns_status st;
+
+ st= pap_hostaddr(pai, &cbyte, max, rrp);
+ if (st) return st;
+ if (cbyte != max) return adns_s_invaliddata;
+
+ return adns_s_ok;
+}
+
+static int dip_hostaddr(adns_state ads,
+ const adns_rr_hostaddr *ap, const adns_rr_hostaddr *bp) {
+ if (ap->astatus != bp->astatus) return ap->astatus;
+ if (ap->astatus) return 0;
+
+ return dip_sockaddr(ads, &ap->addrs[0].addr.sa, &bp->addrs[0].addr.sa);
+}
+
+static int di_hostaddr(adns_state ads,
+ const void *datap_a, const void *datap_b) {
+ const adns_rr_hostaddr *ap= datap_a, *bp= datap_b;
+
+ return dip_hostaddr(ads, ap,bp);
+}
+
+static void mfp_hostaddr(adns_query qu, adns_rr_hostaddr *rrp) {
+ void *tablev;
+ size_t addrsz= gsz_addr(0, qu->answer->type);
+
+ adns__makefinal_str(qu,&rrp->host);
+ tablev= rrp->addrs;
+ adns__makefinal_block(qu, &tablev, rrp->naddrs*addrsz);
+ rrp->addrs= tablev;
+}
+
+static void mf_hostaddr(adns_query qu, void *datap) {
+ adns_rr_hostaddr *rrp= datap;
+
+ mfp_hostaddr(qu,rrp);
+}
+
+static adns_status csp_hostaddr(vbuf *vb, const adns_rr_hostaddr *rrp) {
+ const char *errstr;
+ adns_status st;
+ char buf[20];
+ int i;
+
+ st= csp_domain(vb,rrp->host); if (st) return st;
+
+ CSP_ADDSTR(" ");
+ CSP_ADDSTR(adns_errtypeabbrev(rrp->astatus));
+
+ sprintf(buf," %d ",rrp->astatus);
+ CSP_ADDSTR(buf);
+
+ CSP_ADDSTR(adns_errabbrev(rrp->astatus));
+ CSP_ADDSTR(" ");
+
+ errstr= adns_strerror(rrp->astatus);
+ st= csp_qstring(vb,errstr,strlen(errstr)); if (st) return st;
+
+ if (rrp->naddrs >= 0) {
+ CSP_ADDSTR(" (");
+ for (i=0; i<rrp->naddrs; i++) {
+ CSP_ADDSTR(" ");
+ st= csp_addr(vb,&rrp->addrs[i]);
+ }
+ CSP_ADDSTR(" )");
+ } else {
+ CSP_ADDSTR(" ?");
+ }
+ return adns_s_ok;
+}
+
+static adns_status cs_hostaddr(vbuf *vb, const void *datap) {
+ const adns_rr_hostaddr *rrp= datap;
+
+ return csp_hostaddr(vb,rrp);
+}
+
+/*
+ * _mx_raw (pa,di)
+ */
+
+static adns_status pa_mx_raw(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ const byte *dgram= pai->dgram;
+ adns_rr_intstr *rrp= datap;
+ adns_status st;
+ int pref;
+
+ if (cbyte+2 > max) return adns_s_invaliddata;
+ GET_W(cbyte,pref);
+ rrp->i= pref;
+ st= pap_domain(pai, &cbyte, max, &rrp->str,
+ pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
+ if (st) return st;
+
+ if (cbyte != max) return adns_s_invaliddata;
+ return adns_s_ok;
+}
+
+static int di_mx_raw(adns_state ads, const void *datap_a, const void *datap_b) {
+ const adns_rr_intstr *ap= datap_a, *bp= datap_b;
+
+ if (ap->i < bp->i) return 0;
+ if (ap->i > bp->i) return 1;
+ return 0;
+}
+
+/*
+ * _mx (pa,di)
+ */
+
+static adns_status pa_mx(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ const byte *dgram= pai->dgram;
+ adns_rr_inthostaddr *rrp= datap;
+ adns_status st;
+ int pref;
+
+ if (cbyte+2 > max) return adns_s_invaliddata;
+ GET_W(cbyte,pref);
+ rrp->i= pref;
+ st= pap_hostaddr(pai, &cbyte, max, &rrp->ha);
+ if (st) return st;
+
+ if (cbyte != max) return adns_s_invaliddata;
+ return adns_s_ok;
+}
+
+static int di_mx(adns_state ads, const void *datap_a, const void *datap_b) {
+ const adns_rr_inthostaddr *ap= datap_a, *bp= datap_b;
+
+ if (ap->i < bp->i) return 0;
+ if (ap->i > bp->i) return 1;
+ return dip_hostaddr(ads, &ap->ha, &bp->ha);
+}
+
+/*
+ * _inthostaddr (mf,cs)
+ */
+
+static void mf_inthostaddr(adns_query qu, void *datap) {
+ adns_rr_inthostaddr *rrp= datap;
+
+ mfp_hostaddr(qu,&rrp->ha);
+}
+
+static adns_status cs_inthostaddr(vbuf *vb, const void *datap) {
+ const adns_rr_inthostaddr *rrp= datap;
+ char buf[10];
+
+ sprintf(buf,"%u ",rrp->i);
+ CSP_ADDSTR(buf);
+
+ return csp_hostaddr(vb,&rrp->ha);
+}
+
+/*
+ * _inthost (cs)
+ */
+
+static adns_status cs_inthost(vbuf *vb, const void *datap) {
+ const adns_rr_intstr *rrp= datap;
+ char buf[10];
+
+ sprintf(buf,"%u ",rrp->i);
+ CSP_ADDSTR(buf);
+ return csp_domain(vb,rrp->str);
+}
+
+/*
+ * _ptr (ckl,pa +icb_ptr)
+ */
+
+static adns_status ckl_ptr(adns_state ads, adns_queryflags flags,
+ union checklabel_state *cls, qcontext *ctx,
+ int labnum, const char *dgram,
+ int labstart, int lablen) {
+ if (lablen) {
+ if (!adns__revparse_label(&cls->ptr, labnum, dgram,labstart,lablen))
+ return adns_s_querydomainwrong;
+ } else {
+ if (!adns__revparse_done(&cls->ptr, dgram, labnum,
+ &ctx->tinfo.ptr.rev_rrtype,
+ &ctx->tinfo.ptr.addr))
+ return adns_s_querydomainwrong;
+ }
+ return adns_s_ok;
+}
+
+static void icb_ptr(adns_query parent, adns_query child) {
+ adns_answer *cans= child->answer;
+ const adns_sockaddr *queried;
+ const unsigned char *found;
+ adns_state ads= parent->ads;
+ int i;
+
+ if (cans->status == adns_s_nxdomain || cans->status == adns_s_nodata) {
+ adns__query_fail(parent,adns_s_inconsistent);
+ return;
+ } else if (cans->status) {
+ adns__query_fail(parent,cans->status);
+ return;
+ }
+
+ queried= &parent->ctx.tinfo.ptr.addr;
+ for (i=0, found=cans->rrs.bytes; i<cans->nrrs; i++, found+=cans->rrsz) {
+ if (adns__addrs_equal_raw(&queried->sa,
+ parent->ctx.tinfo.ptr.addr.sa.sa_family,found)) {
+ if (!parent->children.head) {
+ adns__query_done(parent);
+ return;
+ } else {
+ LIST_LINK_TAIL(ads->childw,parent);
+ return;
+ }
+ }
+ }
+
+ adns__query_fail(parent,adns_s_inconsistent);
+}
+
+static adns_status pa_ptr(const parseinfo *pai, int dmstart,
+ int max, void *datap) {
+ char **rrp= datap;
+ adns_status st;
+ adns_rrtype rrtype= pai->qu->ctx.tinfo.ptr.rev_rrtype;
+ int cbyte, id;
+ adns_query nqu;
+ qcontext ctx;
+
+ cbyte= dmstart;
+ st= pap_domain(pai, &cbyte, max, rrp,
+ pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
+ if (st) return st;
+ if (cbyte != max) return adns_s_invaliddata;
+
+ st= adns__mkquery_frdgram(pai->ads, &pai->qu->vb, &id,
+ pai->dgram, pai->dglen, dmstart,
+ rrtype, adns_qf_quoteok_query);
+ if (st) return st;
+
+ ctx.ext= 0;
+ ctx.callback= icb_ptr;
+ memset(&ctx.pinfo,0,sizeof(ctx.pinfo));
+ memset(&ctx.tinfo,0,sizeof(ctx.tinfo));
+ st= adns__internal_submit(pai->ads, &nqu, pai->qu,
+ adns__findtype(rrtype),
+ rrtype, &pai->qu->vb, id,
+ adns_qf_quoteok_query, pai->now, &ctx);
+ if (st) return st;
+
+ return adns_s_ok;
+}
+
+/*
+ * _strpair (mf)
+ */
+
+static void mf_strpair(adns_query qu, void *datap) {
+ adns_rr_strpair *rrp= datap;
+
+ adns__makefinal_str(qu,&rrp->array[0]);
+ adns__makefinal_str(qu,&rrp->array[1]);
+}
+
+/*
+ * _intstrpair (mf)
+ */
+
+static void mf_intstrpair(adns_query qu, void *datap) {
+ adns_rr_intstrpair *rrp= datap;
+
+ adns__makefinal_str(qu,&rrp->array[0].str);
+ adns__makefinal_str(qu,&rrp->array[1].str);
+}
+
+/*
+ * _hinfo (pa)
+ */
+
+static adns_status pa_hinfo(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ adns_rr_intstrpair *rrp= datap;
+ adns_status st;
+ int i;
+
+ for (i=0; i<2; i++) {
+ st= pap_qstring(pai, &cbyte, max, &rrp->array[i].i, &rrp->array[i].str);
+ if (st) return st;
+ }
+
+ if (cbyte != max) return adns_s_invaliddata;
+
+ return adns_s_ok;
+}
+
+/*
+ * _mailbox (pap,cs +pap_mailbox822)
+ */
+
+static adns_status pap_mailbox822(const parseinfo *pai,
+ int *cbyte_io, int max, char **mb_r) {
+ int lablen, labstart, i, needquote, c, r, neednorm;
+ const unsigned char *p;
+ char *str;
+ findlabel_state fls;
+ adns_status st;
+ vbuf *vb;
+
+ vb= &pai->qu->vb;
+ vb->used= 0;
+ adns__findlabel_start(&fls, pai->ads,
+ -1, pai->qu,
+ pai->dgram, pai->dglen, max,
+ *cbyte_io, cbyte_io);
+ st= adns__findlabel_next(&fls,&lablen,&labstart);
+ if (!lablen) {
+ adns__vbuf_appendstr(vb,".");
+ goto x_ok;
+ }
+
+ neednorm= 1;
+ for (i=0, needquote=0, p= pai->dgram+labstart; i<lablen; i++) {
+ c= *p++;
+ if ((c&~128) < 32 || (c&~128) == 127) return adns_s_invaliddata;
+ if (c == '.' && !neednorm) neednorm= 1;
+ else if (c==' ' || c>=127 || ctype_822special(c)) needquote++;
+ else neednorm= 0;
+ }
+
+ if (needquote || neednorm) {
+ r= adns__vbuf_ensure(vb, lablen+needquote+4); if (!r) R_NOMEM;
+ adns__vbuf_appendq(vb,"\"",1);
+ for (i=0, needquote=0, p= pai->dgram+labstart; i<lablen; i++, p++) {
+ c= *p;
+ if (c == '"' || c=='\\') adns__vbuf_appendq(vb,"\\",1);
+ adns__vbuf_appendq(vb,p,1);
+ }
+ adns__vbuf_appendq(vb,"\"",1);
+ } else {
+ r= adns__vbuf_append(vb, pai->dgram+labstart, lablen); if (!r) R_NOMEM;
+ }
+
+ r= adns__vbuf_appendstr(vb,"@"); if (!r) R_NOMEM;
+
+ st= adns__parse_domain_more(&fls,pai->ads, pai->qu,vb,0, pai->dgram);
+ if (st) return st;
+
+ x_ok:
+ str= adns__alloc_interim(pai->qu, vb->used+1); if (!str) R_NOMEM;
+ memcpy(str,vb->buf,vb->used);
+ str[vb->used]= 0;
+ *mb_r= str;
+ return adns_s_ok;
+}
+
+static adns_status pap_mailbox(const parseinfo *pai, int *cbyte_io, int max,
+ char **mb_r) {
+ if (pai->qu->typei->typekey & adns__qtf_mail822) {
+ return pap_mailbox822(pai, cbyte_io, max, mb_r);
+ } else {
+ return pap_domain(pai, cbyte_io, max, mb_r, pdf_quoteok);
+ }
+}
+
+static adns_status csp_mailbox(vbuf *vb, const char *mailbox) {
+ return csp_domain(vb,mailbox);
+}
+
+/*
+ * _rp (pa,cs)
+ */
+
+static adns_status pa_rp(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ adns_rr_strpair *rrp= datap;
+ adns_status st;
+
+ st= pap_mailbox(pai, &cbyte, max, &rrp->array[0]);
+ if (st) return st;
+
+ st= pap_domain(pai, &cbyte, max, &rrp->array[1], pdf_quoteok);
+ if (st) return st;
+
+ if (cbyte != max) return adns_s_invaliddata;
+ return adns_s_ok;
+}
+
+static adns_status cs_rp(vbuf *vb, const void *datap) {
+ const adns_rr_strpair *rrp= datap;
+ adns_status st;
+
+ st= csp_mailbox(vb,rrp->array[0]); if (st) return st;
+ CSP_ADDSTR(" ");
+ st= csp_domain(vb,rrp->array[1]); if (st) return st;
+
+ return adns_s_ok;
+}
+
+/*
+ * _soa (pa,mf,cs)
+ */
+
+static adns_status pa_soa(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ adns_rr_soa *rrp= datap;
+ const byte *dgram= pai->dgram;
+ adns_status st;
+ int msw, lsw, i;
+
+ st= pap_domain(pai, &cbyte, max, &rrp->mname,
+ pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
+ if (st) return st;
+
+ st= pap_mailbox(pai, &cbyte, max, &rrp->rname);
+ if (st) return st;
+
+ if (cbyte+20 != max) return adns_s_invaliddata;
+
+ for (i=0; i<5; i++) {
+ GET_W(cbyte,msw);
+ GET_W(cbyte,lsw);
+ (&rrp->serial)[i]= (msw<<16) | lsw;
+ }
+
+ return adns_s_ok;
+}
+
+static void mf_soa(adns_query qu, void *datap) {
+ adns_rr_soa *rrp= datap;
+
+ adns__makefinal_str(qu,&rrp->mname);
+ adns__makefinal_str(qu,&rrp->rname);
+}
+
+static adns_status cs_soa(vbuf *vb, const void *datap) {
+ const adns_rr_soa *rrp= datap;
+ char buf[20];
+ int i;
+ adns_status st;
+
+ st= csp_domain(vb,rrp->mname); if (st) return st;
+ CSP_ADDSTR(" ");
+ st= csp_mailbox(vb,rrp->rname); if (st) return st;
+
+ for (i=0; i<5; i++) {
+ sprintf(buf," %lu",(&rrp->serial)[i]);
+ CSP_ADDSTR(buf);
+ }
+
+ return adns_s_ok;
+}
+
+/*
+ * _srv* (ckl,(pap),pa*2,mf*2,di,(csp),cs*2,postsort)
+ */
+
+static adns_status ckl_srv(adns_state ads, adns_queryflags flags,
+ union checklabel_state *cls, qcontext *ctx,
+ int labnum, const char *dgram,
+ int labstart, int lablen) {
+ const char *label = dgram+labstart;
+ if (labnum < 2) {
+ if (flags & adns_qf_quoteok_query) return adns_s_ok;
+ if (!lablen || label[0] != '_') return adns_s_querydomaininvalid;
+ return adns_s_ok;
+ }
+ return adns__ckl_hostname(ads,flags, cls,ctx, labnum, dgram,labstart,lablen);
+}
+
+static adns_status pap_srv_begin(const parseinfo *pai, int *cbyte_io, int max,
+ adns_rr_srvha *rrp
+ /* might be adns_rr_srvraw* */) {
+ const byte *dgram= pai->dgram;
+ int ti, cbyte;
+
+ cbyte= *cbyte_io;
+ if ((*cbyte_io += 6) > max) return adns_s_invaliddata;
+
+ rrp->priority= GET_W(cbyte, ti);
+ rrp->weight= GET_W(cbyte, ti);
+ rrp->port= GET_W(cbyte, ti);
+ return adns_s_ok;
+}
+
+static adns_status pa_srvraw(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ adns_rr_srvraw *rrp= datap;
+ adns_status st;
+
+ st= pap_srv_begin(pai,&cbyte,max,datap);
+ if (st) return st;
+
+ st= pap_domain(pai, &cbyte, max, &rrp->host,
+ pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
+ if (st) return st;
+
+ if (cbyte != max) return adns_s_invaliddata;
+ return adns_s_ok;
+}
+
+static adns_status pa_srvha(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ adns_rr_srvha *rrp= datap;
+ adns_status st;
+
+ st= pap_srv_begin(pai,&cbyte,max,datap); if (st) return st;
+ st= pap_hostaddr(pai, &cbyte, max, &rrp->ha); if (st) return st;
+ if (cbyte != max) return adns_s_invaliddata;
+ return adns_s_ok;
+}
+
+static void mf_srvraw(adns_query qu, void *datap) {
+ adns_rr_srvraw *rrp= datap;
+ adns__makefinal_str(qu, &rrp->host);
+}
+
+static void mf_srvha(adns_query qu, void *datap) {
+ adns_rr_srvha *rrp= datap;
+ mfp_hostaddr(qu,&rrp->ha);
+}
+
+static int di_srv(adns_state ads, const void *datap_a, const void *datap_b) {
+ const adns_rr_srvraw *ap= datap_a, *bp= datap_b;
+ /* might be const adns_rr_svhostaddr* */
+
+ if (ap->priority < bp->priority) return 0;
+ if (ap->priority > bp->priority) return 1;
+ return 0;
+}
+
+static adns_status csp_srv_begin(vbuf *vb, const adns_rr_srvha *rrp
+ /* might be adns_rr_srvraw* */) {
+ char buf[30];
+ sprintf(buf,"%u %u %u ", rrp->priority, rrp->weight, rrp->port);
+ CSP_ADDSTR(buf);
+ return adns_s_ok;
+}
+
+static adns_status cs_srvraw(vbuf *vb, const void *datap) {
+ const adns_rr_srvraw *rrp= datap;
+ adns_status st;
+
+ st= csp_srv_begin(vb,(const void*)rrp); if (st) return st;
+ return csp_domain(vb,rrp->host);
+}
+
+static adns_status cs_srvha(vbuf *vb, const void *datap) {
+ const adns_rr_srvha *rrp= datap;
+ adns_status st;
+
+ st= csp_srv_begin(vb,(const void*)datap); if (st) return st;
+ return csp_hostaddr(vb,&rrp->ha);
+}
+
+static void postsort_srv(adns_state ads, void *array, int nrrs,int rrsz,
+ const struct typeinfo *typei) {
+ /* we treat everything in the array as if it were an adns_rr_srvha
+ * even though the array might be of adns_rr_srvraw. That's OK
+ * because they have the same prefix, which is all we access.
+ * We use rrsz, too, rather than naive array indexing, of course.
+ */
+ char *workbegin, *workend, *search, *arrayend;
+ const adns_rr_srvha *rr;
+ union { adns_rr_srvha ha; adns_rr_srvraw raw; } rrtmp;
+ int cpriority, totalweight, runtotal;
+ long randval;
+
+ assert(rrsz <= sizeof(rrtmp));
+ for (workbegin= array, arrayend= workbegin + rrsz * nrrs;
+ workbegin < arrayend;
+ workbegin= workend) {
+ cpriority= (rr=(void*)workbegin)->priority;
+
+ for (workend= workbegin, totalweight= 0;
+ workend < arrayend && (rr=(void*)workend)->priority == cpriority;
+ workend += rrsz) {
+ totalweight += rr->weight;
+ }
+
+ /* Now workbegin..(workend-1) incl. are exactly all of the RRs of
+ * cpriority. From now on, workbegin points to the `remaining'
+ * records: we select one record at a time (RFC2782 `Usage rules'
+ * and `Format of the SRV RR' subsection `Weight') to place at
+ * workbegin (swapping with the one that was there, and then
+ * advance workbegin. */
+ for (;
+ workbegin + rrsz < workend; /* don't bother if just one */
+ workbegin += rrsz) {
+
+ randval= nrand48(ads->rand48xsubi);
+ randval %= (totalweight + 1);
+ /* makes it into 0..totalweight inclusive; with 2^10 RRs,
+ * totalweight must be <= 2^26 so probability nonuniformity is
+ * no worse than 1 in 2^(31-26) ie 1 in 2^5, ie
+ * abs(log(P_intended(RR_i) / P_actual(RR_i)) <= log(2^-5).
+ */
+
+ for (search=workbegin, runtotal=0;
+ (runtotal += (rr=(void*)search)->weight) < randval;
+ search += rrsz);
+ assert(search < arrayend);
+ totalweight -= rr->weight;
+ if (search != workbegin) {
+ memcpy(&rrtmp, workbegin, rrsz);
+ memcpy(workbegin, search, rrsz);
+ memcpy(search, &rrtmp, rrsz);
+ }
+ }
+ }
+ /* tests:
+ * dig -t srv _srv._tcp.test.iwj.relativity.greenend.org.uk.
+ * ./adnshost_s -t srv- _sip._udp.voip.net.cam.ac.uk.
+ * ./adnshost_s -t srv- _jabber._tcp.jabber.org
+ */
+}
+
+/*
+ * _byteblock (mf)
+ */
+
+static void mf_byteblock(adns_query qu, void *datap) {
+ adns_rr_byteblock *rrp= datap;
+ void *bytes= rrp->data;
+ adns__makefinal_block(qu,&bytes,rrp->len);
+ rrp->data= bytes;
+}
+
+/*
+ * _opaque (pa,cs)
+ */
+
+static adns_status pa_opaque(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ adns_rr_byteblock *rrp= datap;
+
+ rrp->len= max - cbyte;
+ rrp->data= adns__alloc_interim(pai->qu, rrp->len);
+ if (!rrp->data) R_NOMEM;
+ memcpy(rrp->data, pai->dgram + cbyte, rrp->len);
+ return adns_s_ok;
+}
+
+static adns_status cs_opaque(vbuf *vb, const void *datap) {
+ const adns_rr_byteblock *rrp= datap;
+ char buf[10];
+ int l;
+ unsigned char *p;
+
+ sprintf(buf,"\\# %d",rrp->len);
+ CSP_ADDSTR(buf);
+
+ for (l= rrp->len, p= rrp->data;
+ l>=4;
+ l -= 4, p += 4) {
+ sprintf(buf," %02x%02x%02x%02x",p[0],p[1],p[2],p[3]);
+ CSP_ADDSTR(buf);
+ }
+ for (;
+ l>0;
+ l--, p++) {
+ sprintf(buf," %02x",*p);
+ CSP_ADDSTR(buf);
+ }
+ return adns_s_ok;
+}
+
+/*
+ * _flat (mf)
+ */
+
+static void mf_flat(adns_query qu, void *data) { }
+
+/*
+ * Now the table.
+ */
+
+#define TYPESZ_M(member) (sizeof(*((adns_answer*)0)->rrs.member))
+
+#define DEEP_TYPE(code,rrt,fmt,memb,parser,comparer,/*printer*/...) \
+ { adns_r_##code&adns_rrt_reprmask, rrt,fmt,TYPESZ_M(memb), mf_##memb, \
+ GLUE(cs_, CAR(__VA_ARGS__)),pa_##parser,di_##comparer, \
+ adns__ckl_hostname, 0, adns__getrrsz_default, adns__query_send, \
+ CDR(__VA_ARGS__) }
+#define FLAT_TYPE(code,rrt,fmt,memb,parser,comparer,/*printer*/...) \
+ { adns_r_##code&adns_rrt_reprmask, rrt,fmt,TYPESZ_M(memb), mf_flat, \
+ GLUE(cs_, CAR(__VA_ARGS__)),pa_##parser,di_##comparer, \
+ adns__ckl_hostname, 0, adns__getrrsz_default, adns__query_send, \
+ CDR(__VA_ARGS__) }
+
+#define di_0 0
+
+static const typeinfo typeinfos[] = {
+/* Must be in ascending order of rrtype ! */
+/* mem-mgmt code rrt fmt member parser comparer printer */
+
+FLAT_TYPE(a, "A", 0, inaddr, inaddr, inaddr,inaddr ),
+DEEP_TYPE(ns_raw, "NS", "raw",str, host_raw,0, domain ),
+DEEP_TYPE(cname, "CNAME", 0, str, dom_raw, 0, domain ),
+DEEP_TYPE(soa_raw,"SOA", "raw",soa, soa, 0, soa ),
+DEEP_TYPE(ptr_raw,"PTR", "raw",str, host_raw,0, domain ),
+DEEP_TYPE(hinfo, "HINFO", 0, intstrpair,hinfo, 0, hinfo ),
+DEEP_TYPE(mx_raw, "MX", "raw",intstr, mx_raw, mx_raw,inthost ),
+DEEP_TYPE(txt, "TXT", 0, manyistr, txt, 0, txt ),
+DEEP_TYPE(rp_raw, "RP", "raw",strpair, rp, 0, rp ),
+FLAT_TYPE(aaaa, "AAAA", 0, in6addr, in6addr, in6addr,in6addr ),
+DEEP_TYPE(srv_raw,"SRV", "raw",srvraw , srvraw, srv, srvraw,
+ .checklabel= ckl_srv, .postsort= postsort_srv),
+
+FLAT_TYPE(addr, "A", "addr", addr, addr, addr, addr,
+ .getrrsz= gsz_addr, .query_send= qs_addr),
+DEEP_TYPE(ns, "NS", "+addr",hostaddr, hostaddr,hostaddr,hostaddr ),
+DEEP_TYPE(ptr, "PTR","checked",str, ptr, 0, domain,
+ .checklabel= ckl_ptr),
+DEEP_TYPE(mx, "MX", "+addr",inthostaddr,mx, mx, inthostaddr, ),
+DEEP_TYPE(srv, "SRV","+addr",srvha, srvha, srv, srvha,
+ .checklabel= ckl_srv, .postsort= postsort_srv),
+
+DEEP_TYPE(soa, "SOA","822", soa, soa, 0, soa ),
+DEEP_TYPE(rp, "RP", "822", strpair, rp, 0, rp ),
+};
+
+static const typeinfo tinfo_addrsub =
+FLAT_TYPE(none, "<addr>","sub",addr, addr, 0, addr,
+ .getrrsz= gsz_addr);
+
+static const typeinfo typeinfo_unknown=
+DEEP_TYPE(unknown,0, "unknown",byteblock,opaque, 0, opaque );
+
+const typeinfo *adns__findtype(adns_rrtype type) {
+ const typeinfo *begin, *end, *mid;
+
+ if (type & ~(adns_rrtype)0x63ffffff)
+ /* 0x60000000 is reserved for `harmless' future expansion */
+ return 0;
+
+ if (type & adns_r_unknown) return &typeinfo_unknown;
+ type &= adns_rrt_reprmask;
+
+ begin= typeinfos; end= typeinfos+(sizeof(typeinfos)/sizeof(typeinfo));
+
+ while (begin < end) {
+ mid= begin + ((end-begin)>>1);
+ if (mid->typekey == type) return mid;
+ if (type > mid->typekey) begin= mid+1;
+ else end= mid;
+ }
+ return 0;
+}