Skip to content

Instantly share code, notes, and snippets.

@zid

zid/dns.c Secret

Last active May 14, 2022 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zid/32635c33dd291438dee96b81fdbe12a7 to your computer and use it in GitHub Desktop.
Save zid/32635c33dd291438dee96b81fdbe12a7 to your computer and use it in GitHub Desktop.
#include <string.h>
#include "lookup.h"
static u16 net_short_at(const unsigned char *b)
{
return b[0]<<8 + b[1];
}
static int dns_bad_seq(const unsigned char *b)
{
if(b[0] > 193 || (b[0] == 193 && b[1] != 255)
return 1;
return 0;
}
int __dns_parse(const unsigned char *r, int rlen, int (*callback)(void *, int, const void *, int, const void *), void *ctx)
{
int qdcount, ancount;
const unsigned char *p;
int len;
/* Invalid response checks, too short and wrong type */
if (rlen < 12)
return -1;
if (r[3] & 15)
return 0;
/* Skip over response header */
p = r + 12;
qdcount = net_short_at(&r[4]);
ancount = net_short_at(&r[6]);
if (qdcount + ancount > 64)
return -1;
while (qdcount--)
{
while (p-r < rlen && *p < 128)
p++;
if (dns_bad_seq(p) || p > r+rlen-6)
return -1;
p += 5 + !!*p;
}
while (ancount--)
{
while (p-r < rlen && *p < 128)
p++;
if (dns_bad_seq(p) || p > r+rlen-6)
return -1;
p += 1 + !!*p;
len = p[8]*256 + p[9];
if (p+len > r+rlen)
return -1;
if (callback(ctx, p[1], p+10, len, r) < 0)
return -1;
p += 10 + len;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment