summaryrefslogtreecommitdiff
path: root/dns.c
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-06-03 23:10:22 (GMT)
committerJohn Denker <jsd@av8n.com>2012-07-14 01:39:07 (GMT)
commitc29249ffc37f23433fd99df19029adfdb1bfb4ea (patch)
treefba657e13aebd53c9fd17bbd766cb2c45956ab60 /dns.c
parent3cfa63750d7e82e0e4b94f7f7b67639bdbd016af (diff)
Accommodate giant-sized DNS responses.
Credit: Christopher Davis
Diffstat (limited to 'dns.c')
-rw-r--r--dns.c29
1 files changed, 23 insertions, 6 deletions
diff --git a/dns.c b/dns.c
index e9faad7..c2f7d86 100644
--- a/dns.c
+++ b/dns.c
@@ -19,10 +19,12 @@ extern int res_search();
static unsigned short getshort(c) unsigned char *c;
{ unsigned short u; u = c[0]; return (u << 8) + c[1]; }
-static union { HEADER hdr; unsigned char buf[PACKETSZ]; } response;
+static struct { unsigned char *buf; } response;
+static int responsebuflen = 0;
static int responselen;
static unsigned char *responseend;
static unsigned char *responsepos;
+static u_long saveresoptions;
static int numanswers;
static char name[MAXDNAME];
@@ -43,18 +45,33 @@ int type;
errno = 0;
if (!stralloc_copy(&glue,domain)) return DNS_MEM;
if (!stralloc_0(&glue)) return DNS_MEM;
- responselen = lookup(glue.s,C_IN,type,response.buf,sizeof(response));
+ if (!responsebuflen)
+ if (response.buf = (unsigned char *)alloc(PACKETSZ+1))
+ responsebuflen = PACKETSZ+1;
+ else return DNS_MEM;
+
+ responselen = lookup(glue.s,C_IN,type,response.buf,responsebuflen);
+ if ((responselen >= responsebuflen) ||
+ (responselen > 0 && (((HEADER *)response.buf)->tc)))
+ {
+ if (responsebuflen < 65536)
+ if (alloc_re(&response.buf, responsebuflen, 65536))
+ responsebuflen = 65536;
+ else return DNS_MEM;
+ saveresoptions = _res.options;
+ _res.options |= RES_USEVC;
+ responselen = lookup(glue.s,C_IN,type,response.buf,responsebuflen);
+ _res.options = saveresoptions;
+ }
if (responselen <= 0)
{
if (errno == ECONNREFUSED) return DNS_SOFT;
if (h_errno == TRY_AGAIN) return DNS_SOFT;
return DNS_HARD;
}
- if (responselen >= sizeof(response))
- responselen = sizeof(response);
responseend = response.buf + responselen;
responsepos = response.buf + sizeof(HEADER);
- n = ntohs(response.hdr.qdcount);
+ n = ntohs(((HEADER *)response.buf)->qdcount);
while (n-- > 0)
{
i = dn_expand(response.buf,responseend,responsepos,name,MAXDNAME);
@@ -64,7 +81,7 @@ int type;
if (i < QFIXEDSZ) return DNS_SOFT;
responsepos += QFIXEDSZ;
}
- numanswers = ntohs(response.hdr.ancount);
+ numanswers = ntohs(((HEADER *)response.buf)->ancount);
return 0;
}