forked from coredns/coredns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
159 lines (142 loc) · 3.65 KB
/
msg.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
150
151
152
153
154
155
156
157
158
159
package msg
import (
"errors"
"net"
"strconv"
"time"
tap "github.com/dnstap/golang-dnstap"
"github.com/miekg/dns"
)
// Builder helps to build a Dnstap message.
type Builder struct {
Packed []byte
SocketProto tap.SocketProtocol
SocketFam tap.SocketFamily
Address net.IP
Port uint32
TimeSec uint64
TimeNsec uint32
err error
}
// New returns a new Builder
func New() *Builder {
return &Builder{}
}
// Addr adds the remote address to the message.
func (b *Builder) Addr(remote net.Addr) *Builder {
if b.err != nil {
return b
}
switch addr := remote.(type) {
case *net.TCPAddr:
b.Address = addr.IP
b.Port = uint32(addr.Port)
b.SocketProto = tap.SocketProtocol_TCP
case *net.UDPAddr:
b.Address = addr.IP
b.Port = uint32(addr.Port)
b.SocketProto = tap.SocketProtocol_UDP
default:
b.err = errors.New("unknown remote address type")
return b
}
if b.Address.To4() != nil {
b.SocketFam = tap.SocketFamily_INET
} else {
b.SocketFam = tap.SocketFamily_INET6
}
return b
}
// Msg adds the raw DNS message to the dnstap message.
func (b *Builder) Msg(m *dns.Msg) *Builder {
if b.err != nil {
return b
}
b.Packed, b.err = m.Pack()
return b
}
// HostPort adds the remote address as encoded by dnsutil.ParseHostPortOrFile to the message.
func (b *Builder) HostPort(addr string) *Builder {
ip, port, err := net.SplitHostPort(addr)
if err != nil {
b.err = err
return b
}
p, err := strconv.ParseUint(port, 10, 32)
if err != nil {
b.err = err
return b
}
b.Port = uint32(p)
if ip := net.ParseIP(ip); ip != nil {
b.Address = []byte(ip)
if ip := ip.To4(); ip != nil {
b.SocketFam = tap.SocketFamily_INET
} else {
b.SocketFam = tap.SocketFamily_INET6
}
return b
}
b.err = errors.New("not an ip address")
return b
}
// Time adds the timestamp to the message.
func (b *Builder) Time(ts time.Time) *Builder {
b.TimeSec = uint64(ts.Unix())
b.TimeNsec = uint32(ts.Nanosecond())
return b
}
// ToClientResponse transforms Data into a client response message.
func (b *Builder) ToClientResponse() (*tap.Message, error) {
t := tap.Message_CLIENT_RESPONSE
return &tap.Message{
Type: &t,
SocketFamily: &b.SocketFam,
SocketProtocol: &b.SocketProto,
ResponseTimeSec: &b.TimeSec,
ResponseTimeNsec: &b.TimeNsec,
ResponseMessage: b.Packed,
QueryAddress: b.Address,
QueryPort: &b.Port,
}, b.err
}
// ToClientQuery transforms Data into a client query message.
func (b *Builder) ToClientQuery() (*tap.Message, error) {
t := tap.Message_CLIENT_QUERY
return &tap.Message{
Type: &t,
SocketFamily: &b.SocketFam,
SocketProtocol: &b.SocketProto,
QueryTimeSec: &b.TimeSec,
QueryTimeNsec: &b.TimeNsec,
QueryMessage: b.Packed,
QueryAddress: b.Address,
QueryPort: &b.Port,
}, b.err
}
// ToOutsideQuery transforms the data into a forwarder or resolver query message.
func (b *Builder) ToOutsideQuery(t tap.Message_Type) (*tap.Message, error) {
return &tap.Message{
Type: &t,
SocketFamily: &b.SocketFam,
SocketProtocol: &b.SocketProto,
QueryTimeSec: &b.TimeSec,
QueryTimeNsec: &b.TimeNsec,
QueryMessage: b.Packed,
ResponseAddress: b.Address,
ResponsePort: &b.Port,
}, b.err
}
// ToOutsideResponse transforms the data into a forwarder or resolver response message.
func (b *Builder) ToOutsideResponse(t tap.Message_Type) (*tap.Message, error) {
return &tap.Message{
Type: &t,
SocketFamily: &b.SocketFam,
SocketProtocol: &b.SocketProto,
ResponseTimeSec: &b.TimeSec,
ResponseTimeNsec: &b.TimeNsec,
ResponseMessage: b.Packed,
ResponseAddress: b.Address,
ResponsePort: &b.Port,
}, b.err
}