-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
78 lines (73 loc) · 1.67 KB
/
utils.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
package base
import (
"github.com/gcrahay/riprovision/arp"
log "github.com/sirupsen/logrus"
"net"
"strings"
)
func (h *Server) validPrefix(mac string) bool {
if len(h.MACPrefix) == 0 {
return true
}
for _, prefix := range h.MACPrefix {
if strings.HasPrefix(mac, strings.ToLower(prefix)) {
return true
}
}
return false
}
func (h *Server) ValidMAC(mac string) bool {
mac = strings.ToLower(mac)
logger := h.Log.WithFields(log.Fields{
"device": mac,
"component": "mac_validator",
})
if !h.validPrefix(mac) {
logger.Error("Invalid prefix")
return false
}
macEntries := arp.ReverseSearch(mac)
for _, macEntry := range macEntries {
if macEntry.Permanent != true {
logger.Infof("Non permanent MAC entry on interface: %s", macEntry.Iface)
continue
}
if !stringInSlice(macEntry.Iface, h.Provision.InterfaceNames) {
logger.Infof("Not a provisioning interface: %s", macEntry.Iface)
continue
}
netInterface, err := net.InterfaceByName(macEntry.Iface)
if err == nil {
addresses, err := netInterface.Addrs()
if err == nil {
for _, a := range addresses {
switch v := a.(type) {
case *net.IPNet:
if v.IP.To4() != nil {
break
}
default:
logger.Infof("Cannot guess interface address on interface: %s", macEntry.Iface)
continue
}
}
} else {
logger.Infof("Cannot find addresses for interfaces: %s", macEntry.Iface)
continue
}
} else {
logger.Infof("Cannot find interface %s on server", macEntry.Iface)
continue
}
return true
}
return false
}
func stringInSlice(str string, list []string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}