-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac2vendor.go
41 lines (35 loc) · 830 Bytes
/
mac2vendor.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
package mac2vendor
import (
"github.com/pkg/errors"
"net"
"strings"
)
var (
errCannotResolveType = errors.New("cannot resolve type to mac address")
mapping = make(map[string]string)
)
// IsLoaded is a predicate to determine whether or not the mapping table was loaded
func IsLoaded() bool {
return len(mapping) > 0
}
// Lookup resolves the provided MAC address to the registered vendor
func Lookup(v interface{}) (string, error) {
var mac net.HardwareAddr
switch v.(type) {
case string:
var err error
mac, err = net.ParseMAC(v.(string))
if err != nil {
return "", err
}
case net.HardwareAddr:
mac = v.(net.HardwareAddr)
default:
return "", errCannotResolveType
}
prefix := mac[:3].String()
if val, ok := mapping[strings.ToLower(prefix)]; ok {
return val, nil
}
return "", nil
}