-
Notifications
You must be signed in to change notification settings - Fork 25
/
layer-mdns.go
163 lines (133 loc) · 3.63 KB
/
layer-mdns.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
160
161
162
163
package main
import (
"encoding/binary"
"fmt"
"regexp"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
const mdnsPort = 5353
var reMdnsQueryLocal = regexp.MustCompile(`^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.in-addr\.arpa$`)
var layerTypeMdns gopacket.LayerType
type layerMdns struct {
layers.BaseLayer
TransactionID uint16
IsResponse bool
Opcode uint8
Questions []mdnsQuestion
Answers []mdnsAnswer
AuthorityCount uint16
AdditionalCount uint16
}
type mdnsQuestion struct {
Query string
Type uint16
Class uint16
}
type mdnsAnswer struct {
Query string
Type uint16
Class uint16
TTL uint32
DomainName string
}
func layerMdnsInit() {
layerTypeMdns = gopacket.RegisterLayerType(
2501,
gopacket.LayerTypeMetadata{
Name: "Mdns",
Decoder: gopacket.DecodeFunc(layerMdnsDecode),
},
)
layers.RegisterUDPPortLayerType(mdnsPort, layerTypeMdns)
}
func layerMdnsDecode(data []byte, p gopacket.PacketBuilder) error {
l := &layerMdns{}
err := l.DecodeFromBytes(data, p)
if err != nil {
return err
}
p.AddLayer(l)
p.SetApplicationLayer(l)
return nil
}
func (l *layerMdns) LayerType() gopacket.LayerType {
return layerTypeMdns
}
func (l *layerMdns) CanDecode() gopacket.LayerClass {
return layerTypeMdns
}
func (l *layerMdns) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypeZero
}
func (l *layerMdns) Payload() []byte {
return nil
}
func (l *layerMdns) DecodeFromBytes(data []byte, _ gopacket.DecodeFeedback) error {
l.BaseLayer = layers.BaseLayer{Contents: data}
l.TransactionID = binary.BigEndian.Uint16(data[0:2])
l.IsResponse = (data[3] >> 7) == 0x01
l.Opcode = (data[3] >> 3) & 0x0F
questionCount := binary.BigEndian.Uint16(data[4:6])
answerCount := binary.BigEndian.Uint16(data[6:8])
l.AuthorityCount = binary.BigEndian.Uint16(data[8:10])
l.AdditionalCount = binary.BigEndian.Uint16(data[10:12])
pos := 12
if questionCount > 0 {
return fmt.Errorf("is question, unsupported")
}
l.Answers = nil
for i := uint16(0); i < answerCount; i++ {
a := mdnsAnswer{}
var read int
a.Query, read = dnsQueryDecode(data, pos)
if read <= 0 {
return fmt.Errorf("answer query: invalid string (%v)", data)
}
pos += read
a.Type = binary.BigEndian.Uint16(data[pos : pos+2])
a.Class = binary.BigEndian.Uint16(data[pos+2 : pos+4])
a.TTL = binary.BigEndian.Uint32(data[pos+4 : pos+8])
dataLen := binary.BigEndian.Uint16(data[pos+8 : pos+10])
pos += 10
if a.Type == 12 { // PTR
a.DomainName, read = dnsQueryDecode(data, pos)
if read <= 0 {
return fmt.Errorf("domain name: invalid string")
}
if uint16(read) != dataLen {
return fmt.Errorf("read != dataLen, %d, %d", read, dataLen)
}
}
pos += int(dataLen)
l.Answers = append(l.Answers, a)
}
return nil
}
func (l *layerMdns) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
data, err := b.AppendBytes(12)
if err != nil {
panic(err)
}
binary.BigEndian.PutUint16(data[0:2], l.TransactionID)
if l.IsResponse {
data[3] |= 0x01 << 7
}
data[3] |= l.Opcode << 3
binary.BigEndian.PutUint16(data[4:6], uint16(len(l.Questions)))
binary.BigEndian.PutUint16(data[6:8], uint16(len(l.Answers)))
binary.BigEndian.PutUint16(data[8:10], l.AuthorityCount)
binary.BigEndian.PutUint16(data[10:12], l.AdditionalCount)
for _, q := range l.Questions {
enc := dnsQueryEncode(q.Query)
data, err := b.AppendBytes(len(enc) + 4)
if err != nil {
panic(err)
}
copy(data[:len(enc)], enc)
data = data[len(enc):]
binary.BigEndian.PutUint16(data[0:2], q.Type)
binary.BigEndian.PutUint16(data[2:4], q.Class)
}
return nil
}