Skip to content

Commit dfe5a07

Browse files
committed
dns: update
1 parent 0c6b0b2 commit dfe5a07

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

include/packet/dns.h

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#ifndef LIBPACKET_DECODE_DNS_H
22
#define LIBPACKET_DECODE_DNS_H
33

4+
// See RFC 1035,
5+
// https://datatracker.ietf.org/doc/html/rfc1035
6+
7+
#include <string>
48
#include <cstdint>
9+
510
struct dns_stats {
611
uint64_t dns_tooshort;
712
uint64_t dns_too_many_queries; // > 256 DNS Questions
@@ -20,13 +25,8 @@ struct dns_header {
2025
uint16_t arcount;
2126
} __attribute__((packed));
2227

23-
struct dns_label {
24-
uint8_t *p;
25-
uint8_t len;
26-
};
27-
2828
struct dns_query {
29-
//struct dns_label labels[32];
29+
std::string label;
3030
// Store QTYPE and QCLASS (assuming a structure in Packet to store this information)
3131
uint16_t dns_qtype;
3232
uint16_t dns_qclass;
@@ -36,8 +36,7 @@ struct dns_answer {
3636
uint16_t dns_atype;
3737
uint16_t dns_aclass;
3838
uint16_t dns_ttl;
39-
uint16_t dns_rdlength;
40-
uint16_t dns_rdata;
39+
std::string data;
4140
};
4241

4342
struct dns {
@@ -46,6 +45,25 @@ struct dns {
4645
struct dns_answer answers[256];
4746
};
4847

48+
enum dns_types {
49+
TYPE_A, // 1
50+
TYPE_NS, // 2
51+
TYPE_MD, // 3
52+
TYPE_MF, // 4
53+
TYPE_CNAME, // 5
54+
TYPE_SOA, // 6
55+
TYPE_MB, // 7
56+
TYPE_MG, // 8
57+
TYPE_MR, // 9
58+
TYPE_NULL, // 10
59+
TYPE_WKS, // 11
60+
TYPE_PTR, // 12
61+
TYPE_HINFO, // 13
62+
TYPE_MINFO, // 14
63+
TYPE_MX, // 15
64+
TYPE_TXT // 16
65+
};
66+
4967
int decode_dns(uint8_t const *pkt, uint32_t const len, dns* dns);
5068

5169
#endif /* LIBPACKET_DECODE_DNS_H */

src/dns.cc

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//
2-
// libpacket/src/dns.c: DNS protocol decoder
1+
//
2+
// libpacket/src/dns.c: DNS protocol decoder
33
// Victor Roemer (wtfbbqhax), <[email protected]>.
44
//
55
#include <assert.h>
@@ -10,6 +10,9 @@
1010

1111
#include <arpa/inet.h>
1212

13+
#include <iostream>
14+
#include <string>
15+
1316
#include "packet_private.h"
1417
#include "packet/dns.h"
1518

@@ -27,7 +30,7 @@ decode_dns(uint8_t const * pkt, uint32_t const len, dns* dns)
2730
}
2831

2932
struct dns_header const* raw =
30-
(struct dns_header const*)pkt;
33+
(struct dns_header const*)pkt;
3134

3235
dns->h.id = ntohs(raw->id);
3336
dns->h.flags = ntohs(raw->flags);
@@ -48,8 +51,8 @@ decode_dns(uint8_t const * pkt, uint32_t const len, dns* dns)
4851
// Parsing Question Section
4952
for (int i = 0; i < dns->h.qdcount; i++)
5053
{
51-
struct dns_query *q = &dns->questions[i];
52-
54+
struct dns_query* q = &dns->questions[i];
55+
5356
// Parse QNAME
5457
while (remaining_len > 0 && *ptr != 0)
5558
{
@@ -63,10 +66,13 @@ decode_dns(uint8_t const * pkt, uint32_t const len, dns* dns)
6366
return -1;
6467
}
6568

69+
q->label.append(reinterpret_cast<char const*>(ptr), label_len);
70+
q->label.append(".");
71+
6672
ptr += label_len;
6773
remaining_len -= label_len;
6874
}
69-
75+
7076
// Null byte at the end of QNAME
7177
if (remaining_len == 0)
7278
{
@@ -152,8 +158,35 @@ decode_dns(uint8_t const * pkt, uint32_t const len, dns* dns)
152158
a->dns_atype = atype;
153159
a->dns_aclass = aclass;
154160
a->dns_ttl = ttl;
155-
a->dns_rdlength = rdlength;
156-
a->dns_rdata = *((uint16_t*)rdata);
161+
162+
// Parse rdata
163+
while (rdata < ptr)
164+
{
165+
uint16_t us = *reinterpret_cast<uint16_t const*>(rdata);
166+
if (us == name)
167+
{
168+
rdata += 2;
169+
rdlength -= 2;
170+
continue;
171+
}
172+
173+
uint8_t len = 0;
174+
if (rdlength >= 1)
175+
{
176+
len = *rdata;
177+
rdata += 1;
178+
}
179+
// abort if len == 0 && rdatalength > 0 // ANOMALY?
180+
181+
len = rdlength > len ? len : rdlength;
182+
183+
if (len)
184+
{
185+
a->data.append(reinterpret_cast<char const*>(rdata), len);
186+
a->data.append(".");
187+
}
188+
rdata += len;
189+
}
157190
}
158191

159192
return 0;

0 commit comments

Comments
 (0)