-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsPacket.go
72 lines (59 loc) · 1.61 KB
/
dnsPacket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
type DnsPacket struct {
header DnsHeader
questions []DnsQuestion
answers []DnsRecord
authorities []DnsRecord
resources []DnsRecord
}
func NewDnsPacket() *DnsPacket {
dnsPacket := &DnsPacket{
header: *NewDnsHeader(),
questions: []DnsQuestion{},
answers: []DnsRecord{},
authorities: []DnsRecord{},
resources: []DnsRecord{},
}
return dnsPacket
}
func FromBuffer(buf *Packet) *DnsPacket {
result := NewDnsPacket()
result.header.read(buf)
for i := 0; i < int(result.header.questions); i++ {
question := NewDnsQuestion("", UNKNOWN)
question.read(buf)
result.questions = append(result.questions, question)
}
for i := 0; i < int(result.header.answers); i++ {
answers := ReadDnsRecord(buf)
result.answers = append(result.answers, answers)
}
for i := 0; i < int(result.header.authoritativeEntries); i++ {
authority := ReadDnsRecord(buf)
result.authorities = append(result.authorities, authority)
}
for i := 0; i < int(result.header.resourceEntries); i++ {
resource := ReadDnsRecord(buf)
result.resources = append(result.resources, resource)
}
return result
}
func (d *DnsPacket) toBuffer(buf *Packet) {
d.header.questions = uint16(len(d.questions))
d.header.answers = uint16(len(d.answers))
d.header.authoritativeEntries = uint16(len(d.authorities))
d.header.resourceEntries = uint16(len(d.resources))
d.header.write(buf)
for _, q := range d.questions {
q.write(buf)
}
for _, a := range d.answers {
a.writeDnsRecord(buf)
}
for _, a := range d.authorities {
a.writeDnsRecord(buf)
}
for _, r := range d.resources {
r.writeDnsRecord(buf)
}
}