-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanufacturer.go
71 lines (63 loc) · 1.65 KB
/
manufacturer.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
package packet
import (
"bufio"
"bytes"
"compress/gzip"
_ "embed"
"encoding/hex"
"net"
"strings"
)
// Gzip commpressed file containing mac OUI and manufacturer name.
// Format as follows: 000019<tab>Applied Dynamics
// Get the latest from here:
// https://linuxnet.ca/ieee/oui/nmap-mac-prefixes
//
// Alternatively we could use the wireshart format, but it is much larger file
// https://gitlab.com/wireshark/wireshark/-/raw/master/manuf
//
//go:embed nmap-mac-prefixes.gz
var manufacturersFile []byte
var manufacturersMap = make(map[string]string)
// Uncmpress and load manufacturers file into a map during initialisation
func init() {
countErrors := 0
f, err := gzip.NewReader(bytes.NewReader(manufacturersFile))
if err != nil {
panic(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if line == "" || line[0] == '#' || len(line) < 8 {
continue
}
s := strings.Split(line, "\t")
if len(s) < 2 || len(s[0]) != 6 {
countErrors++
continue
}
d, err := hex.DecodeString(s[0])
if err != nil || len(d) != 3 {
countErrors++
continue
}
manufacturersMap[string(d)] = strings.Join(s[1:], " ")
}
if err := scanner.Err(); err != nil {
Logger.Msg("error reading manufacturing file").Int("count", countErrors).Write()
panic(err)
}
if countErrors != 0 {
Logger.Msg("error in manufacturing file").Int("count", countErrors).Write()
panic(err)
}
}
// FindManufacturer locates the manufacturer name using the first 24bits of the mac address.
func FindManufacturer(mac net.HardwareAddr) (name string) {
if len(mac) != 6 {
return ""
}
return manufacturersMap[string(mac[:3])]
}