/
dns.go
106 lines (86 loc) · 2.38 KB
/
dns.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package protos
import (
"net"
"github.com/google/gopacket/layers"
"github.com/negbie/logp"
"github.com/segmentio/encoding/json"
)
type DNS struct {
ID uint16 `json:"id"`
QR bool `json:"qr"`
OpCode layers.DNSOpCode `json:"opcode"`
AA bool `json:"aa"` // Authoritative answer
TC bool `json:"tc"` // Truncated
RD bool `json:"rd"` // Recursion desired
RA bool `json:"ra"` // Recursion available
Z uint8 `json:"z"` // For future use
ResponseCode layers.DNSResponseCode `json:"rescode"`
QDCount uint16 `json:"qdc"` // Expected questions
ANCount uint16 `json:"anc"` // Expected answers
NSCount uint16 `json:"nsc"` // Expected authorities
ARCount uint16 `json:"arc"` // Expected additional records
Questions []DNSQuestion `json:"questions,omitempty"`
Answers []DNSResourceRecord `json:"answers,omitempty"`
}
type DNSQuestion struct {
Name string `json:"name"`
Type layers.DNSType `json:"type"`
Class layers.DNSClass `json:"class"`
}
type DNSResourceRecord struct {
Name string `json:"name"`
Type layers.DNSType `json:"type"`
Class layers.DNSClass `json:"class"`
TTL uint32 `json:"ttl"`
IP net.IP `json:"ip,omitempty"`
NS string `json:"ns,omitempty"`
CNAME string `json:"cname,omitempty"`
}
func newDNS(dns *layers.DNS) (d *DNS) {
d = &DNS{}
d.ID = dns.ID
d.QR = dns.QR
d.OpCode = dns.OpCode
d.AA = dns.AA
d.TC = dns.TC
d.RD = dns.RD
d.RA = dns.RA
d.Z = dns.Z
d.ResponseCode = dns.ResponseCode
d.QDCount = dns.QDCount
d.ANCount = dns.ANCount
d.NSCount = dns.NSCount
d.ARCount = dns.ARCount
for _, q := range dns.Questions {
qs := DNSQuestion{}
qs.Name = string(q.Name)
qs.Type = q.Type
qs.Class = q.Class
d.Questions = append(d.Questions, qs)
}
for _, r := range dns.Answers {
res := DNSResourceRecord{}
res.Name = string(r.Name)
res.Type = r.Type
res.Class = r.Class
res.TTL = r.TTL
res.IP = r.IP
res.NS = string(r.NS)
res.CNAME = string(r.CNAME)
d.Answers = append(d.Answers, res)
}
return d
}
func (d *DNS) MarshalJSON() ([]byte, error) {
bytes, err := json.Marshal(*d)
return bytes, err
}
func ParseDNS(d *layers.DNS) []byte {
dns, err := newDNS(d).MarshalJSON()
if err != nil {
logp.Warn("%v", err)
return nil
}
logp.Debug("dns", "Payload=%v", string(dns))
return dns
}