-
Notifications
You must be signed in to change notification settings - Fork 24
/
method-nbns.go
123 lines (102 loc) · 2.11 KB
/
method-nbns.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
package main
import (
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
type methodNbns struct {
p *program
listen chan []byte
}
func newMethodNbns(p *program) error {
mn := &methodNbns{
p: p,
listen: make(chan []byte),
}
p.mn = mn
return nil
}
func (mn *methodNbns) run() {
var decodedLayers []gopacket.LayerType
var eth layers.Ethernet
var ip layers.IPv4
var udp layers.UDP
var nbns layerNbns
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet,
ð,
&ip,
&udp,
&nbns)
parse := func(raw []byte) {
if err := parser.DecodeLayers(raw, &decodedLayers); err != nil {
return
}
if udp.DstPort != nbnsPort && udp.SrcPort != nbnsPort {
return
}
if len(nbns.Answers) != 1 {
return
}
name := func() string {
for _, n := range nbns.Answers[0].Names {
if n.Type == 0x20 { // service name
return n.Name
}
}
return ""
}()
if name == "" {
return
}
srcMac := copyMac(eth.SrcMAC)
srcIP := copyIP(ip.SrcIP)
mn.p.nbns <- nbnsReq{
srcMac: srcMac,
srcIP: srcIP,
name: name,
}
}
for raw := range mn.listen {
parse(raw)
mn.p.ls.listenDone <- struct{}{}
}
}
func (mn *methodNbns) request(destIP net.IP) {
localAddr := &net.UDPAddr{}
remoteAddr := &net.UDPAddr{
IP: destIP,
Port: nbnsPort,
}
conn, err := net.DialUDP("udp", localAddr, remoteAddr)
if err != nil {
panic(err)
}
defer conn.Close() //nolint:errcheck
v, err := randUint16()
if err != nil {
panic(err)
}
nbns := layerNbns{
TransactionID: v,
Questions: []nbnsQuestion{
{
Query: "CKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
Type: 0x21, // NB_STAT
Class: 1, // IN
},
},
}
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
if err := gopacket.SerializeLayers(buf, opts, &nbns); err != nil {
panic(err)
}
if _, err := conn.Write(buf.Bytes()); err != nil {
panic(err)
}
// close immediately the connection even if this generates a "ICMP"
// "destination unreachable". Otherwise connection count would increment with time
}