@@ -6,82 +6,158 @@ import (
6
6
"strings"
7
7
)
8
8
9
- //DNSResourceType resource type 表示资源类型
10
- type DNSResourceType uint16
11
-
12
- //DNSop 表示dns header 操作码
13
- type DNSop uint16
14
9
15
10
//ADCOUNT question 实体数量占2个字节
16
11
//ANCOUNT answer 资源数量占2个字节
17
12
//NSCOUNT authority 部分包含的资源数量 2byte
18
13
//ARCOUNT additional 部分包含的资源梳理 2byte
19
14
15
+ //DNSResourceType resource type 表示资源类型
16
+ type DNSResourceType uint16
17
+
18
+ const (
19
+ A DNSResourceType = iota + 1 // name = hostname value = ipaddress
20
+ NS //name = ,value = dns hostname
21
+ MD
22
+ MF
23
+ CNAME //name = hostname
24
+ SOA
25
+ MB
26
+ MG
27
+ MR
28
+ NULL
29
+ WKS
30
+ PTR
31
+ HINFO
32
+ MINFO
33
+ MX
34
+ )
20
35
21
36
const (
22
37
ID = 0
23
38
OP = 2
24
39
QDCOUNT = 4
25
40
ANCOUNT = 6
26
41
NSCOUNT = 8
27
- QACOUNT = 10
42
+ ARCOUNT = 10
28
43
29
44
DOMAIN = 12
30
45
)
31
-
46
+ //DNSQuestion
32
47
type DNSQuestion struct {
33
48
QuestionType uint16
34
49
QuestionClass uint16
35
50
}
36
51
52
+ //DNSResource ansower,authority,additional
53
+ type DNSResource struct {
54
+ Name uint16
55
+ Type DNSResourceType
56
+ Class uint16
57
+ TTL uint32
58
+ RDlen uint16
59
+ RData []byte
60
+ Address string
61
+ }
62
+
63
+
37
64
//DNS 报文的封装
38
65
type DNS []byte
39
66
67
+ //GetId
68
+ func (d DNS ) GetId () uint16 {
69
+ return binary .BigEndian .Uint16 (d [ID :OP ])
70
+ }
71
+ //GetQDCount
72
+ func (d DNS ) GetQDCount ()uint16 {
73
+ return binary .BigEndian .Uint16 (d [QDCOUNT :QDCOUNT + 2 ])
74
+ }
75
+ //GetANCount
76
+ func (d DNS ) GetANCount ()uint16 {
77
+ return binary .BigEndian .Uint16 (d [ANCOUNT :ANCOUNT + 2 ])
78
+ }
79
+ //GetNSCount
80
+ func (d DNS ) GetNSCount () uint16 {
81
+ return binary .BigEndian .Uint16 (d [NSCOUNT :NSCOUNT + 2 ])
82
+ }
83
+ //GetQACount
84
+ func (d DNS ) GetARCount () uint16 {
85
+ return binary .BigEndian .Uint16 (d [ARCOUNT :ARCOUNT + 2 ])
86
+ }
87
+
88
+ //GetAnswer
89
+ func (d DNS ) GetAnswer (domain string ) * []DNSResource {
90
+ //answer 起始地址
91
+ asLen := DOMAIN + len (d .getDomain (domain )) + 4
92
+
93
+ answer := []DNSResource {}
94
+ for i := 0 ; i < (int (d .GetANCount () + d .GetNSCount () + d .GetARCount ())) ;i ++ {
95
+ rs := DNSResource {}
96
+ //判断是不是指针 pointer地址
97
+ if checkP := d [asLen ]; checkP >> 6 == 3 {
98
+ //pointer := (d[asLen] & 0x3F << 8) + d[asLen+1]
99
+ rs .Name = binary .BigEndian .Uint16 (d [asLen :asLen + 2 ])
100
+ asLen += 2
101
+ rs .Type = DNSResourceType (binary .BigEndian .Uint16 (d [asLen :asLen + 2 ]))
102
+ asLen += 2
103
+ rs .Class = binary .BigEndian .Uint16 (d [asLen :asLen + 2 ])
104
+ asLen += 2
105
+ rs .TTL = binary .BigEndian .Uint32 (d [asLen :asLen + 4 ])
106
+ asLen += 4
107
+ rs .RDlen = binary .BigEndian .Uint16 (d [asLen :asLen + 2 ])
108
+ asLen += 2
109
+ rs .RData = d [asLen :asLen + int (rs .RDlen )]
110
+ asLen += int (rs .RDlen )
111
+ answer = append (answer ,rs )
112
+ }
113
+ }
114
+ return & answer
115
+ }
116
+
40
117
//Setheader
41
118
func (d DNS ) Setheader (id uint16 ){
42
119
d .setID (id )
43
120
d .setFlag (0 ,0 ,0 ,0 ,1 ,0 ,0 )
44
121
}
45
- //SetID
46
- func (d DNS )setID (id uint16 ){
47
- //set id
48
- binary .BigEndian .PutUint16 (d [ID :], id )
49
- }
50
- //SetQdcount
51
- func (d DNS )SetQdcount (qd uint16 ){
52
-
122
+ //SetCount
123
+ func (d DNS ) SetCount (qd ,an ,ns ,qa uint16 ) {
124
+ //SetQdcount
53
125
binary .BigEndian .PutUint16 (d [QDCOUNT :], qd )
54
- }
55
- //SetAncount
56
- func (d DNS )SetAncount (an uint16 ){
126
+ //SetAncount
57
127
binary .BigEndian .PutUint16 (d [ANCOUNT :] ,an )
58
- }
59
- //SetNscount
60
- func (d DNS )SetNscount (ns uint16 ){
128
+ //SetNscount
61
129
binary .BigEndian .PutUint16 (d [NSCOUNT :],ns )
130
+ //SetQAcount
131
+ binary .BigEndian .PutUint16 (d [ARCOUNT :],qa )
62
132
}
63
- //SetQAcount
64
- func (d DNS )SetQAcount (qa uint16 ){
65
- binary .BigEndian .PutUint16 (d [QACOUNT :],qa )
133
+ //setID
134
+ func (d DNS )setID (id uint16 ){
135
+ //set id
136
+ binary .BigEndian .PutUint16 (d [ID :], id )
66
137
}
67
- //SetDomain
68
- func (d * DNS )SetDomain (domain string ) {
138
+ //setDomain
139
+ func (d * DNS )getDomain (domain string ) [] byte {
69
140
var (
70
141
buffer bytes.Buffer
71
142
segments []string = strings .Split (domain , "." )
72
143
)
73
- binary .Write (& buffer ,binary .BigEndian ,* d )
74
-
75
144
for _ , seg := range segments {
76
145
binary .Write (& buffer , binary .BigEndian , byte (len (seg )))
77
146
binary .Write (& buffer , binary .BigEndian , []byte (seg ))
78
147
}
79
148
binary .Write (& buffer , binary .BigEndian , byte (0x00 ))
80
149
81
- * d = buffer .Bytes ()
82
- //return buffer.Bytes()
150
+ return buffer .Bytes ()
83
151
}
84
- func (d * DNS )SetQuestion (qtype ,qclass uint16 ){
152
+ //SetQuestion query field
153
+ //domain url
154
+ //qtype type
155
+ //qclass class
156
+ func (d * DNS )SetQuestion (domain string ,qtype ,qclass uint16 ){
157
+ for _ ,b := range d .getDomain (domain ) {
158
+ * d = append ((* d ),b )
159
+ }
160
+ //d.setDomain(domain)
85
161
q := DNSQuestion {
86
162
QuestionType : qtype ,
87
163
QuestionClass : qclass ,
0 commit comments