-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwhois.go
115 lines (110 loc) · 2.84 KB
/
whois.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
package bacnet
import "C"
import (
"github.com/NubeDev/bacnet/btypes"
"github.com/NubeDev/bacnet/encoding"
)
type WhoIsOpts struct {
Low int `json:"low"`
High int `json:"high"`
GlobalBroadcast bool `json:"global_broadcast"`
NetworkNumber uint16 `json:"network_number"`
}
// WhoIs finds all devices with ids between the provided low and high values.
// Use constant ArrayAll for both fields to scan the entire network at once.
// Using ArrayAll is highly discouraged for most networks since it can lead
// to a high congested network.
func (c *client) WhoIs(wh *WhoIsOpts) ([]btypes.Device, error) {
dest := *c.dataLink.GetBroadcastAddress()
enc := encoding.NewEncoder()
low := wh.Low
high := wh.High
if wh.GlobalBroadcast {
wh.NetworkNumber = btypes.GlobalBroadcast //65535
}
if low <= 0 {
low = 0
}
if high <= 0 {
high = 4194304 //max dev id
}
dest.Net = wh.NetworkNumber
npdu := &btypes.NPDU{
Version: btypes.ProtocolVersion,
Destination: &dest,
Source: c.dataLink.GetMyAddress(),
IsNetworkLayerMessage: false,
// We are not expecting a direct reply from a single destination
ExpectingReply: false,
Priority: btypes.Normal,
HopCount: btypes.DefaultHopCount,
}
enc.NPDU(npdu)
err := enc.WhoIs(int32(low), int32(high))
if err != nil {
return nil, err
}
// Subscribe to any changes in the range. If it is a broadcast,
var start, end int
if low == -1 || high == -1 {
start = 0
end = maxInt
} else {
start = low
end = high
}
// Run in parallel
errChan := make(chan error)
go func() {
_, err = c.Send(dest, npdu, enc.Bytes(), nil)
errChan <- err
}()
values, err := c.utsm.Subscribe(start, end)
if err != nil {
return nil, err
}
err = <-errChan
if err != nil {
return nil, err
}
// Weed out values that are not important such as non object type
// and that are not
uniqueMap := make(map[btypes.ObjectInstance]btypes.Device)
uniqueList := make([]btypes.Device, len(uniqueMap))
for _, v := range values {
r, ok := v.(btypes.IAm)
// Skip non I AM responses
if !ok {
continue
}
// Check to see if we are in the map before inserting
macMSTP := 0
if len(r.Addr.Adr) > 0 {
macMSTP = int(r.Addr.Adr[0])
}
networkNumber := 0
if r.Addr.Net > 0 {
networkNumber = int(r.Addr.Net)
}
if _, ok := uniqueMap[r.ID.Instance]; !ok {
dev := btypes.Device{
DeviceID: int(r.ID.Instance),
Addr: r.Addr,
ID: r.ID,
MaxApdu: r.MaxApdu,
Segmentation: r.Segmentation,
Vendor: r.Vendor,
MacMSTP: macMSTP,
NetworkNumber: networkNumber,
}
ip, err := r.Addr.UDPAddr()
if err == nil {
dev.Ip = ip.IP.String()
dev.Port = ip.Port
}
uniqueMap[r.ID.Instance] = dev
uniqueList = append(uniqueList, dev)
}
}
return uniqueList, err
}