From 0e5b2871ca6456b01d4bf037a6e68badf1ff1a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henryk=20Pl=C3=B6tz?= Date: Fri, 3 Oct 2014 19:58:52 +0200 Subject: Initial commit of djbdns-1.05.tar.gz Source was http://cr.yp.to/djbdns/djbdns-1.05.tar.gz, SHA1 2efdb3a039d0c548f40936aa9cb30829e0ce8c3d --- printpacket.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 printpacket.c (limited to 'printpacket.c') diff --git a/printpacket.c b/printpacket.c new file mode 100644 index 0000000..7571e08 --- /dev/null +++ b/printpacket.c @@ -0,0 +1,90 @@ +#include "uint16.h" +#include "uint32.h" +#include "error.h" +#include "byte.h" +#include "dns.h" +#include "printrecord.h" +#include "printpacket.h" + +static char *d; + +#define X(s) if (!stralloc_cats(out,s)) return 0; +#define NUM(u) if (!stralloc_catulong0(out,u,0)) return 0; + +unsigned int printpacket_cat(stralloc *out,char *buf,unsigned int len) +{ + uint16 numqueries; + uint16 numanswers; + uint16 numauthority; + uint16 numglue; + unsigned int pos; + char data[12]; + uint16 type; + + pos = dns_packet_copy(buf,len,0,data,12); if (!pos) return 0; + + uint16_unpack_big(data + 4,&numqueries); + uint16_unpack_big(data + 6,&numanswers); + uint16_unpack_big(data + 8,&numauthority); + uint16_unpack_big(data + 10,&numglue); + + NUM(len) + X(" bytes, ") + NUM(numqueries) + X("+") + NUM(numanswers) + X("+") + NUM(numauthority) + X("+") + NUM(numglue) + X(" records") + + if (data[2] & 128) X(", response") + if (data[2] & 120) X(", weird op") + if (data[2] & 4) X(", authoritative") + if (data[2] & 2) X(", truncated") + if (data[2] & 1) X(", weird rd") + if (data[3] & 128) X(", weird ra") + switch(data[3] & 15) { + case 0: X(", noerror"); break; + case 3: X(", nxdomain"); break; + case 4: X(", notimp"); break; + case 5: X(", refused"); break; + default: X(", weird rcode"); + } + if (data[3] & 112) X(", weird z") + + X("\n") + + while (numqueries) { + --numqueries; + X("query: ") + + pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; + pos = dns_packet_copy(buf,len,pos,data,4); if (!pos) return 0; + + if (byte_diff(data + 2,2,DNS_C_IN)) { + X("weird class") + } + else { + uint16_unpack_big(data,&type); + NUM(type) + X(" ") + if (!dns_domain_todot_cat(out,d)) return 0; + } + X("\n") + } + + for (;;) { + if (numanswers) { --numanswers; X("answer: ") } + else if (numauthority) { --numauthority; X("authority: ") } + else if (numglue) { --numglue; X("additional: ") } + else break; + + pos = printrecord_cat(out,buf,len,pos,0,0); + if (!pos) return 0; + } + + if (pos != len) { errno = error_proto; return 0; } + return 1; +} -- cgit v1.2.3