-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathoui_encode.go
More file actions
191 lines (167 loc) · 4.25 KB
/
oui_encode.go
File metadata and controls
191 lines (167 loc) · 4.25 KB
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
191
package mactracker
import (
"bytes"
"compress/gzip"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"strconv"
)
// Binary format for OUI database:
//
// Header:
// 4 bytes magic "OUI\x01"
// 4 bytes count uint32 LE, number of entries
// Per entry:
// 6 bytes oui raw prefix bytes
// 1 byte mask CIDR mask width
// 2 bytes vLen uint16 LE vendor string length
// vLen vendor UTF-8
// 2 bytes aLen uint16 LE added-date string length
// aLen added UTF-8
// 2 bytes cLen uint16 LE country string length
// cLen country UTF-8
// 2 bytes dLen uint16 LE address string length
// dLen address UTF-8
var ouiMagic = [4]byte{'O', 'U', 'I', 0x01}
// EncodeOUIDB serializes an OuiDB into a gzip-compressed binary blob.
func EncodeOUIDB(db *OuiDB) ([]byte, error) {
var raw bytes.Buffer
// Magic
raw.Write(ouiMagic[:])
// Entry count
count := uint32(len(db.Blocks))
if err := binary.Write(&raw, binary.LittleEndian, count); err != nil {
return nil, err
}
for _, block := range db.Blocks {
if err := encodeBlock(&raw, block); err != nil {
return nil, err
}
}
// Gzip compress
var compressed bytes.Buffer
gz, err := gzip.NewWriterLevel(&compressed, gzip.BestCompression)
if err != nil {
return nil, err
}
if _, err := gz.Write(raw.Bytes()); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
return compressed.Bytes(), nil
}
func encodeBlock(w *bytes.Buffer, b *OuiBlock) error {
// OUI prefix: always 6 bytes (pad if shorter)
var oui [6]byte
copy(oui[:], b.Oui)
w.Write(oui[:])
// Mask
w.WriteByte(byte(b.Mask))
// Strings: vendor, added, country, address
for _, s := range []string{b.Vendor, b.Added, b.Country, b.Address} {
if err := writeString16(w, s); err != nil {
return err
}
}
return nil
}
func writeString16(w *bytes.Buffer, s string) error {
if len(s) > 65535 {
return fmt.Errorf("string too long: %d bytes", len(s))
}
if err := binary.Write(w, binary.LittleEndian, uint16(len(s))); err != nil {
return err
}
w.WriteString(s)
return nil
}
// DecodeOUIDB deserializes a gzip-compressed binary blob into an OuiDB Blocks map.
func DecodeOUIDB(data []byte) (map[string]*OuiBlock, error) {
gz, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("gzip open: %w", err)
}
defer gz.Close()
raw, err := io.ReadAll(gz)
if err != nil {
return nil, fmt.Errorf("gzip read: %w", err)
}
r := bytes.NewReader(raw)
// Magic
var magic [4]byte
if _, err := io.ReadFull(r, magic[:]); err != nil {
return nil, fmt.Errorf("read magic: %w", err)
}
if magic != ouiMagic {
return nil, fmt.Errorf("bad magic: %x", magic)
}
// Count
var count uint32
if err := binary.Read(r, binary.LittleEndian, &count); err != nil {
return nil, fmt.Errorf("read count: %w", err)
}
blocks := make(map[string]*OuiBlock, count)
for range count {
block, key, err := decodeBlock(r)
if err != nil {
return nil, err
}
blocks[key] = block
}
return blocks, nil
}
func decodeBlock(r *bytes.Reader) (*OuiBlock, string, error) {
// OUI prefix
var oui [6]byte
if _, err := io.ReadFull(r, oui[:]); err != nil {
return nil, "", fmt.Errorf("read oui: %w", err)
}
// Mask
maskByte, err := r.ReadByte()
if err != nil {
return nil, "", fmt.Errorf("read mask: %w", err)
}
mask := int(maskByte)
// Strings
vendor, err := readString16(r)
if err != nil {
return nil, "", fmt.Errorf("read vendor: %w", err)
}
added, err := readString16(r)
if err != nil {
return nil, "", fmt.Errorf("read added: %w", err)
}
country, err := readString16(r)
if err != nil {
return nil, "", fmt.Errorf("read country: %w", err)
}
address, err := readString16(r)
if err != nil {
return nil, "", fmt.Errorf("read address: %w", err)
}
key := hex.EncodeToString(oui[:]) + "/" + strconv.Itoa(mask)
block := &OuiBlock{
Oui: oui[:],
Mask: mask,
Vendor: vendor,
Added: added,
Country: country,
Address: address,
}
return block, key, nil
}
func readString16(r *bytes.Reader) (string, error) {
var length uint16
if err := binary.Read(r, binary.LittleEndian, &length); err != nil {
return "", err
}
buf := make([]byte, length)
if _, err := io.ReadFull(r, buf); err != nil {
return "", err
}
return string(buf), nil
}