-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
92 lines (77 loc) · 2.64 KB
/
parse.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"encoding/binary"
)
func ParseDNSResponse(b []byte) DNSMessage {
header := DNSHeader{
ID: binary.BigEndian.Uint16(b[0:2]),
Flags: binary.BigEndian.Uint16(b[2:4]),
QCount: binary.BigEndian.Uint16(b[4:6]),
AnsCount: binary.BigEndian.Uint16(b[6:8]),
RRCount: binary.BigEndian.Uint16(b[8:10]),
AddnlRRCount: binary.BigEndian.Uint16(b[10:12]),
}
questions, offset := ParseDNSQuestions(b, header)
answers, offset := ParseDNSAnswers(b, header, offset)
return DNSMessage{Header: header, Questions: questions, Answers: answers}
}
func ParseDNSQuestions(b []byte, header DNSHeader) ([]DNSQuestion, int) {
questions := []DNSQuestion{}
offset := 12
for i := uint16(0); i < header.QCount; i++ {
question := DNSQuestion{}
question.Name, offset = ParseLabel(b, offset)
question.Type = binary.BigEndian.Uint16(b[offset : offset+2])
question.Class = binary.BigEndian.Uint16(b[offset+2 : offset+4])
question.Namestr = LabelToString(question.Name)
offset += 4
questions = append(questions, question)
}
return questions, offset
}
func ParseDNSAnswers(b []byte, header DNSHeader, offset int) ([]DNSAnswer, int) {
answers := []DNSAnswer{}
for i := 0; i < int(header.AnsCount); i++ {
answer := DNSAnswer{}
if b[offset]&0xc0 == 0xc0 {
labelOffset := ConvPointerToOffset(b, offset)
name, _ := ParseLabel(b, labelOffset)
answer.Name = b[offset : offset+2]
answer.Namestr = LabelToString(name)
} else {
answer.Name, offset = ParseLabel(b, offset)
answer.Namestr = LabelToString(answer.Name)
}
answer.Type = binary.BigEndian.Uint16(b[offset+2 : offset+4])
answer.Class = binary.BigEndian.Uint16(b[offset+4 : offset+6])
answer.TTL = binary.BigEndian.Uint32(b[offset+6 : offset+10])
answer.RDLength = binary.BigEndian.Uint16(b[offset+10 : offset+12])
answer.RData = b[offset+12 : offset+12+int(answer.RDLength)]
answers = append(answers, answer)
offset += 12 + int(answer.RDLength)
}
return answers, offset
}
func ParseLabel(b []byte, offset int) ([]byte, int) {
res := []byte{}
for ; b[offset] != 0x00; offset += int(b[offset]) + 1 {
res = append(res, b[offset:offset+int(b[offset])+1]...)
}
res = append(res, 0x00)
offset++
return res, offset
}
func ConvPointerToOffset(b []byte, offset int) int {
labelOffset := int(binary.BigEndian.Uint16(b[offset:offset+2]) & 0x3FFF)
return labelOffset
}
func LabelToString(label []byte) string {
res := []byte{}
for offset := 0; label[offset] != 0x00; offset += int(label[offset]) + 1 {
if len(res) > 0 {
res = append(res, 0x2E)
}
res = append(res, label[offset+1:offset+int(label[offset])+1]...)
}
return string(res)
}