Skip to content

Commit

Permalink
Handle large DNS responses.
Browse files Browse the repository at this point in the history
Source: poorly-named qmail-103.patch
  • Loading branch information
icculus committed Dec 12, 2009
1 parent 7f1fc1e commit 5086288
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions dns.c
Expand Up @@ -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];
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit 5086288

Please sign in to comment.