+#include <netdb.h>
+
+/*
+ * Unfortunately, there are no portable functions for DNS RR parsing,
+ * so we will do the bit shuffling with our own bare hands.
+ */
+
+#define GET16(x) do { if (p+2 > end) goto err; x = (p[0] << 8) | p[1]; p += 2; } while (0)
+#define GET32(x) do { if (p+4 > end) goto err; x = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; p += 4; } while (0)
+
+enum dns_section {
+ DNS_SEC_QUESTION,
+ DNS_SEC_ANSWER,
+ DNS_SEC_AUTHORITY,
+ DNS_SEC_ADDITIONAL,
+ DNS_NUM_SECTIONS
+};
+
+struct dns_state {
+ u16 counts[DNS_NUM_SECTIONS];
+ byte *sections[DNS_NUM_SECTIONS+1];
+ byte *sec_ptr, *sec_end;
+
+ /* Result of dns_parse_rr(): */
+ u16 rr_type;
+ u16 rr_class;
+ u32 rr_ttl;
+ u16 rr_len;
+ byte *rr_data;
+};
+
+static byte *
+dns_skip_name(byte *p, byte *end)
+{
+ while (p < end)
+ {
+ unsigned int x = *p++;
+ if (!x)
+ return p;
+ switch (x & 0xc0)
+ {
+ case 0: /* Uncompressed: x = length */
+ p += x;
+ break;
+ case 0xc0: /* Indirection: 1 byte more for offset */
+ p++;
+ return (p < end) ? p : NULL;
+ default: /* RFU */
+ return NULL;
+ }
+ }
+ return NULL;
+}
+
+static int
+dns_parse_packet(struct dns_state *s, byte *p, unsigned int plen)
+{
+ byte *end = p + plen;
+ unsigned int i, j, len;
+ unsigned int UNUSED x;
+
+#if 0
+ /* Dump the packet */
+ for (i=0; i<plen; i++)
+ {
+ if (!(i%16)) printf("%04x:", i);
+ printf(" %02x", p[i]);
+ if ((i%16)==15 || i==plen-1) putchar('\n');
+ }
+#endif
+
+ GET32(x); /* ID and flags are ignored */
+ for (i=0; i<DNS_NUM_SECTIONS; i++)
+ GET16(s->counts[i]);
+ for (i=0; i<DNS_NUM_SECTIONS; i++)
+ {
+ s->sections[i] = p;
+ for (j=0; j < s->counts[i]; j++)
+ {
+ p = dns_skip_name(p, end); /* Name */
+ if (!p)
+ goto err;
+ GET32(x); /* Type and class */
+ if (i != DNS_SEC_QUESTION)
+ {
+ GET32(x); /* TTL */
+ GET16(len); /* Length of data */
+ p += len;
+ if (p > end)
+ goto err;
+ }
+ }
+ }
+ s->sections[i] = p;
+ return 0;
+
+err:
+ return -1;
+}
+
+static void
+dns_init_section(struct dns_state *s, int i)
+{
+ s->sec_ptr = s->sections[i];
+ s->sec_end = s->sections[i+1];
+}
+
+static int
+dns_parse_rr(struct dns_state *s)
+{
+ byte *p = s->sec_ptr;
+ byte *end = s->sec_end;
+
+ if (p == end)
+ return 0;
+ p = dns_skip_name(p, end);
+ if (!p)
+ goto err;
+ GET16(s->rr_type);
+ GET16(s->rr_class);
+ GET32(s->rr_ttl);
+ GET16(s->rr_len);
+ s->rr_data = p;
+ s->sec_ptr = p + s->rr_len;
+ return 1;
+
+err:
+ return -1;
+}