D. J. Bernstein
Internet publication
djbdns

The dns_packet library interface

     #include <dns.h>

     newpos = dns_packet_copy(buf,len,pos,out,outlen);

     char *buf;
     unsigned int len;
     unsigned int pos;
     unsigned int newpos;
     char *out;
     unsigned int outlen;
dns_packet_copy reads outlen bytes from position pos of a DNS packet stored at buf, copies the bytes into out, and returns pos+outlen. However, if reading the bytes would require reading past the first len bytes of the packet, dns_packet_copy returns 0, setting errno appropriately.

     #include <dns.h>

     newpos = dns_packet_getname(buf,len,pos,&dn);

     char *buf;
     unsigned int len;
     unsigned int pos;
     unsigned int newpos;
     char *dn = 0;
dns_packet_getname reads a compressed domain name from position pos of a DNS packet stored at buf, copies the name into dn, and returns the position in the packet immediately after the name.

If the name is misformatted, or if reading the name would require reading past the first len bytes of the packet, or if there is not enough memory for dn, dns_packet_getname returns 0, setting errno appropriately, and leaves dn alone.

     #include <dns.h>

     newpos = dns_packet_skipname(buf,len,pos);

     char *buf;
     unsigned int len;
     unsigned int pos;
     unsigned int newpos;
dns_packet_skipname is like dns_packet_getname but discards the resulting domain name. It does not allocate memory.

Beware that, even if dns_packet_skipname returns nonzero, a future dns_packet_getname from the same position may fail: it may run out of memory, or encounter a format error not detected by dns_packet_skipname.