/
device.go
190 lines (172 loc) · 4.28 KB
/
device.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
184
185
186
187
188
189
190
package zone
import (
"encoding/json"
"github.com/miekg/dns"
"net"
"strings"
)
const (
LOC_EQUATOR = 1 << 31 // RFC 1876, Section 2.
LOC_PRIMEMERIDIAN = 1 << 31 // RFC 1876, Section 2.
LOC_DEGREES = 60.0 * 60.0 * 1000.0
LOC_ALTITUDEBASE = 100000.0
)
// Device describes the DNS stored equipment information.
// It is assumed that each piece of equipment has a single A record pointing
// to a definitive DNS Name and Address. Possible equipment Aliases
// can also be stored via CNAME lookups, likewise Mapping IP addresses can be
// stored via PTR records pointing to CNAME entries. Also stored are Place,
// Model details, instrument or site Codes and Place location information.
type Device struct {
Name string `json:"name"` // full dns name
IP net.IP `json:"ip"` // primary ip address (A)
Reverse []net.IP `json:"reverse"` // primary lookups (PTR)
Mapping map[string]net.IP `json:"mapping"` // secondary lookups (PTR/CNAME)
Aliases []string `json:"aliases"` // other names (CNAME)
Place string `json:"place"` // full place name (TXT)
Model string `json:"model"` // equipment model (HINFO)
Code string `json:"code"` // equipment site code (HINFO)
Latitude float64 `json:"latitude"` // place latitude (LOC)
Longitude float64 `json:"longitude"` // place longitude (LOC)
Height float64 `json:"height"` // place height (LOC)
}
func CopyIP(ip net.IP) net.IP {
p := make(net.IP, len(ip))
copy(p, ip)
return p
}
func (d *Device) SetLocation(lat, lon, alt uint32) {
if lat > LOC_EQUATOR {
d.Latitude = (float64)(lat-LOC_EQUATOR) / LOC_DEGREES
} else {
d.Latitude = (float64)(LOC_EQUATOR-lat) / -LOC_DEGREES
}
if lon > LOC_PRIMEMERIDIAN {
d.Longitude = (float64)(lon-LOC_PRIMEMERIDIAN) / LOC_DEGREES
} else {
d.Longitude = (float64)(LOC_PRIMEMERIDIAN-lon) / -LOC_DEGREES
}
d.Height = float64(alt)/100.0 - LOC_ALTITUDEBASE
}
func (d *Device) Location() (uint32, uint32, uint32) {
lat := uint32((d.Latitude * LOC_DEGREES) + LOC_EQUATOR)
lon := uint32((d.Longitude * LOC_DEGREES) + LOC_PRIMEMERIDIAN)
alt := uint32((d.Height + LOC_ALTITUDEBASE) * 100.0)
return lat, lon, alt
}
func (d *Device) Hostname() string {
l := dns.SplitDomainName(d.Name)
if len(l) > 0 {
return l[0]
}
return d.Name
}
func (d *Device) HasName(name string) bool {
return strings.EqualFold(d.Name, name)
}
func (d *Device) HasAddress(ip net.IP) bool {
if d.IP.Equal(ip) {
return true
}
for _, a := range d.Reverse {
if !a.Equal(ip) {
continue
}
return true
}
return false
}
func (d *Device) InNetwork(network net.IPNet) bool {
if network.Contains(d.IP) {
return true
}
for _, a := range d.Reverse {
if !network.Contains(a) {
continue
}
return true
}
return false
}
func (d *Device) AtPlace(place string) bool {
return strings.EqualFold(d.Place, place)
}
func (d *Device) AtLocation(lat, lon, alt uint32) bool {
la, lo, a := d.Location()
if la != lat {
return false
}
if lo != lon {
return false
}
if a != alt {
return false
}
return true
}
func (d *Device) HasCode(code string) bool {
return strings.EqualFold(d.Code, code)
}
func (d *Device) HasModel(model string) bool {
return d.Model == model
}
func (d *Device) Equal(device *Device) bool {
if !d.HasName(device.Name) {
return false
}
if !d.HasAddress(device.IP) {
return false
}
if !d.HasCode(device.Code) {
return false
}
if !d.HasModel(device.Model) {
return false
}
if !d.AtLocation(device.Location()) {
return false
}
return true
}
func (d *Device) HasAlias(alias string) bool {
for _, a := range d.Aliases {
if a != alias {
continue
}
return true
}
return false
}
func (d *Device) HasReverse(ip net.IP) bool {
for _, a := range d.Reverse {
if !a.Equal(ip) {
continue
}
return true
}
return false
}
func (d *Device) HasIP(ip net.IP) bool {
for _, a := range d.Mapping {
if !a.Equal(ip) {
continue
}
return true
}
return false
}
func (d *Device) HasMapping(name string, ip net.IP) bool {
for n, i := range d.Mapping {
if n == name && i.Equal(ip) {
return true
}
}
return false
}
func (d *Device) String() string {
m, err := json.Marshal(d)
if err != nil {
return ""
}
return (string)(m)
}