-
Notifications
You must be signed in to change notification settings - Fork 25
/
method-mdns.go
183 lines (151 loc) · 3.21 KB
/
method-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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"net"
"strings"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
const (
mdnsPeriod = 200 * time.Millisecond
)
type methodMdns struct {
p *program
listen chan []byte
}
func newMethodMdns(p *program) error {
mm := &methodMdns{
p: p,
listen: make(chan []byte),
}
p.mm = mm
return nil
}
func (mm *methodMdns) run() {
go mm.runListener()
if !mm.p.passiveMode {
// continuously poll mdns in order to detect changes or skipped hosts
go mm.runPeriodicRequests()
}
}
func (mm *methodMdns) runListener() {
var decodedLayers []gopacket.LayerType
var eth layers.Ethernet
var ip layers.IPv4
var udp layers.UDP
var mdns layerMdns
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet,
ð,
&ip,
&udp,
&mdns)
parse := func(raw []byte) {
if err := parser.DecodeLayers(raw, &decodedLayers); err != nil {
return
}
if udp.DstPort != mdnsPort && udp.SrcPort != mdnsPort {
return
}
if len(mdns.Answers) == 0 {
return
}
srcMac := copyMac(eth.SrcMAC)
srcIP := copyIP(ip.SrcIP)
domainName := func() string {
for _, a := range mdns.Answers {
domainName := a.DomainName
if a.Type != 12 { // PTR
continue
}
m := reMdnsQueryLocal.FindStringSubmatch(a.Query)
if m == nil {
continue
}
// accept only if mdns ip matches with sender ip
mdnsIP := net.ParseIP(fmt.Sprintf("%s.%s.%s.%s", m[4], m[3], m[2], m[1])).To4()
if !mdnsIP.Equal(srcIP) {
continue
}
return domainName
}
return ""
}()
if domainName == "" {
return
}
domainName = strings.TrimSuffix(domainName, ".local")
mm.p.mdns <- mdnsReq{
srcMac: srcMac,
srcIP: srcIP,
domainName: domainName,
}
}
for raw := range mm.listen {
parse(raw)
mm.p.ls.listenDone <- struct{}{}
}
}
func (mm *methodMdns) request(destIP net.IP) {
mac, _ := net.ParseMAC("01:00:5e:00:00:fb")
eth := layers.Ethernet{
SrcMAC: mm.p.intf.HardwareAddr,
DstMAC: mac,
EthernetType: layers.EthernetTypeIPv4,
}
v, err := randUint16()
if err != nil {
panic(err)
}
ip := layers.IPv4{
Version: 4,
TTL: 255,
Id: v,
Protocol: layers.IPProtocolUDP,
SrcIP: mm.p.ownIP,
DstIP: net.ParseIP("224.0.0.251"), // TODO: provare unicast
}
udp := layers.UDP{
SrcPort: mdnsPort,
DstPort: mdnsPort,
}
err = udp.SetNetworkLayerForChecksum(&ip)
if err != nil {
panic(err)
}
mdns := layerMdns{
TransactionID: 0,
Questions: []mdnsQuestion{
{
Query: fmt.Sprintf("%d.%d.%d.%d.in-addr.arpa", destIP[3], destIP[2], destIP[1], destIP[0]),
Type: 0x0C, // domain pointer
Class: 1, // IN
},
},
}
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
err = gopacket.SerializeLayers(buf, opts, ð, &ip, &udp, &mdns)
if err != nil {
panic(err)
}
err = mm.p.ls.socket.Write(buf.Bytes())
if err != nil {
panic(err)
}
}
func (mm *methodMdns) runPeriodicRequests() {
for {
ips, err := randAvailableIPs(mm.p.ownIP)
if err != nil {
panic(err)
}
for _, dstAddr := range ips {
mm.request(dstAddr)
time.Sleep(mdnsPeriod) // about 1 minute for a full scan
}
}
}