-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdnsmessage_dnstap.go
149 lines (126 loc) · 3.41 KB
/
dnsmessage_dnstap.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package dnsutils
import (
"errors"
"net"
"strconv"
"github.com/dmachard/go-dnstap-protobuf"
"github.com/dmachard/go-netutils"
"google.golang.org/protobuf/proto"
)
func (dm *DNSMessage) ToDNSTap(extended bool) ([]byte, error) {
if len(dm.DNSTap.Payload) > 0 {
return dm.DNSTap.Payload, nil
}
dt := &dnstap.Dnstap{}
t := dnstap.Dnstap_MESSAGE
dt.Identity = []byte(dm.DNSTap.Identity)
dt.Version = []byte("-")
dt.Type = &t
mt := dnstap.Message_Type(dnstap.Message_Type_value[dm.DNSTap.Operation])
var sf dnstap.SocketFamily
if ipNet, valid := netutils.IPToInet[dm.NetworkInfo.Family]; valid {
sf = dnstap.SocketFamily(dnstap.SocketFamily_value[ipNet])
}
sp := dnstap.SocketProtocol(dnstap.SocketProtocol_value[dm.NetworkInfo.Protocol])
tsec := uint64(dm.DNSTap.TimeSec)
tnsec := uint32(dm.DNSTap.TimeNsec)
var rport uint32
var qport uint32
if dm.NetworkInfo.ResponsePort != "-" {
if port, err := strconv.Atoi(dm.NetworkInfo.ResponsePort); err != nil {
return nil, err
} else if port < 0 || port > 65535 {
return nil, errors.New("invalid response port value")
} else {
rport = uint32(port)
}
}
if dm.NetworkInfo.QueryPort != "-" {
if port, err := strconv.Atoi(dm.NetworkInfo.QueryPort); err != nil {
return nil, err
} else if port < 0 || port > 65535 {
return nil, errors.New("invalid query port value")
} else {
qport = uint32(port)
}
}
msg := &dnstap.Message{Type: &mt}
msg.SocketFamily = &sf
msg.SocketProtocol = &sp
reqIP := net.ParseIP(dm.NetworkInfo.QueryIP)
if dm.NetworkInfo.Family == netutils.ProtoIPv4 {
msg.QueryAddress = reqIP.To4()
} else {
msg.QueryAddress = reqIP.To16()
}
msg.QueryPort = &qport
rspIP := net.ParseIP(dm.NetworkInfo.ResponseIP)
if dm.NetworkInfo.Family == netutils.ProtoIPv4 {
msg.ResponseAddress = rspIP.To4()
} else {
msg.ResponseAddress = rspIP.To16()
}
msg.ResponsePort = &rport
if dm.DNS.Type == DNSQuery {
msg.QueryMessage = dm.DNS.Payload
msg.QueryTimeSec = &tsec
msg.QueryTimeNsec = &tnsec
} else {
msg.ResponseTimeSec = &tsec
msg.ResponseTimeNsec = &tnsec
msg.ResponseMessage = dm.DNS.Payload
}
dt.Message = msg
// add dnstap extra
if len(dm.DNSTap.Extra) > 0 {
dt.Extra = []byte(dm.DNSTap.Extra)
}
// contruct new dnstap field with all tranformations
// the original extra field is kept if exist
if extended {
ednstap := &ExtendedDnstap{}
// add original dnstap value if exist
if len(dm.DNSTap.Extra) > 0 {
ednstap.OriginalDnstapExtra = []byte(dm.DNSTap.Extra)
}
// add additionnals tags ?
if dm.ATags != nil {
ednstap.Atags = &ExtendedATags{
Tags: dm.ATags.Tags,
}
}
// add public suffix
if dm.PublicSuffix != nil {
ednstap.Normalize = &ExtendedNormalize{
Tld: dm.PublicSuffix.QnamePublicSuffix,
EtldPlusOne: dm.PublicSuffix.QnameEffectiveTLDPlusOne,
}
}
// add filtering
if dm.Filtering != nil {
ednstap.Filtering = &ExtendedFiltering{
SampleRate: uint32(dm.Filtering.SampleRate),
}
}
// add geo
if dm.Geo != nil {
ednstap.Geo = &ExtendedGeo{
City: dm.Geo.City,
Continent: dm.Geo.Continent,
Isocode: dm.Geo.CountryIsoCode,
AsNumber: dm.Geo.AutonomousSystemNumber,
AsOrg: dm.Geo.AutonomousSystemOrg,
}
}
extendedData, err := proto.Marshal(ednstap)
if err != nil {
return nil, err
}
dt.Extra = extendedData
}
data, err := proto.Marshal(dt)
if err != nil {
return nil, err
}
return data, nil
}