-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.go
More file actions
42 lines (37 loc) · 810 Bytes
/
Copy pathfilter.go
File metadata and controls
42 lines (37 loc) · 810 Bytes
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
package l2
import (
"bytes"
"encoding/hex"
"fmt"
)
// filterReader is a FrameReader which only allows through frames which match the list of
// frames is is supplied with.
type filter struct {
mac [][]byte
device FrameReader
}
// Construct a filter which only allows through the specified mac addresses
func NewFilter(dev FrameReader, mac ...[]byte) FrameReader {
return filter{mac, dev}
}
func (f filter) ReadFrame() (EthFrame, error) {
for {
p, err := f.device.ReadFrame()
if err != nil {
return p, err
}
for _, mac := range f.mac {
if bytes.Equal(EthFrame(p).Destination(), mac) {
return p, nil
}
}
}
}
func (f filter) String() string {
s := "Filter{" + fmt.Sprint(f.device)
for _, mac := range f.mac {
s += ", " + hex.EncodeToString(mac)
}
s += "}"
return s
}