-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
133 lines (124 loc) · 3.51 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy ofthe License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specificlanguage governing permissions and
* limitations under the License.
*
*/
package layers
import (
"errors"
"github.com/google/gopacket/layers"
)
func newDNSRecordFromLayersRecord(rr *layers.DNSResourceRecord) DNSResourceRecord {
return DNSResourceRecord{
Name: string(rr.Name),
Type: rr.Type.String(),
Class: rr.Class.String(),
TTL: rr.TTL,
DataLength: rr.DataLength,
}
}
func toFlowDNSResourceRecord(rr *layers.DNSResourceRecord) (DNSResourceRecord, error) {
switch rr.Type {
case layers.DNSTypeA, layers.DNSTypeAAAA:
record := newDNSRecordFromLayersRecord(rr)
record.IP = rr.IP.String()
return record, nil
case layers.DNSTypeSOA:
record := newDNSRecordFromLayersRecord(rr)
record.SOA = &DNSSOA{
MName: string(rr.SOA.MName),
RName: string(rr.SOA.RName),
Serial: rr.SOA.Serial,
Refresh: rr.SOA.Refresh,
Retry: rr.SOA.Retry,
Expire: rr.SOA.Expire,
Minimum: rr.SOA.Minimum,
}
return record, nil
case layers.DNSTypeSRV:
record := newDNSRecordFromLayersRecord(rr)
record.SRV = &DNSSRV{
Priority: rr.SRV.Priority,
Weight: rr.SRV.Weight,
Port: rr.SRV.Port,
Name: string(rr.SRV.Name),
}
return record, nil
case layers.DNSTypeMX:
record := newDNSRecordFromLayersRecord(rr)
record.MX = &DNSMX{
Preference: rr.MX.Preference,
Name: string(rr.MX.Name),
}
return record, nil
case layers.DNSTypeTXT, layers.DNSTypeHINFO:
record := newDNSRecordFromLayersRecord(rr)
TXTs := make([]string, len(rr.TXTs))
for i, v := range rr.TXTs {
TXTs[i] = string(v)
}
record.TXTs = TXTs
return record, nil
case layers.DNSTypeNS:
record := newDNSRecordFromLayersRecord(rr)
record.NS = string(rr.NS)
return record, nil
case layers.DNSTypeCNAME:
record := newDNSRecordFromLayersRecord(rr)
record.CNAME = string(rr.CNAME)
return record, nil
case layers.DNSTypePTR:
record := newDNSRecordFromLayersRecord(rr)
record.PTR = string(rr.PTR)
return record, nil
case layers.DNSTypeOPT:
record := newDNSRecordFromLayersRecord(rr)
OPTs := make([]*DNSOPT, len(rr.OPT))
for i, v := range rr.OPT {
OPTs[i].Code = v.Code.String()
OPTs[i].Data = string(v.Data)
}
record.OPT = OPTs
return record, nil
}
return DNSResourceRecord{}, errors.New("No Matching Type")
}
// GetDNSQuestions returns the dns questions from a given packet
func GetDNSQuestions(dq []layers.DNSQuestion) []DNSQuestion {
if len(dq) > 0 {
questions := make([]DNSQuestion, len(dq))
for i, v := range dq {
questions[i] = DNSQuestion{
Name: string(v.Name),
Type: v.Type.String(),
Class: v.Class.String(),
}
}
return questions
}
return nil
}
// GetDNSRecords returns a dns record
func GetDNSRecords(records []layers.DNSResourceRecord) []DNSResourceRecord {
if len(records) > 0 {
holders := make([]DNSResourceRecord, 0, len(records))
for _, v := range records {
if resource, err := toFlowDNSResourceRecord(&v); err == nil {
holders = append(holders, resource)
}
}
return holders
}
return nil
}