forked from calmh/ipfix
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfilter.go
119 lines (103 loc) · 2.03 KB
/
filter.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
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
package ipfix
//"errors"
type HeaderFilter struct {
Version uint16 //v9 or v10
versionActive bool
DomainID uint32
domainActive bool
}
type otherFilter struct {
uint16Bitmask
eid uint32
}
type Filter struct {
HeaderFilter
uint16Bitmask
baseEnabled bool
others []otherFilter
}
func (f *Filter) SetVersion(v uint16) {
f.versionActive = true
f.Version = v
}
func (f *Filter) ClearVersion() {
f.versionActive = false
}
func (f *Filter) SetDomainID(v uint32) {
f.domainActive = true
f.DomainID = v
}
func (f *Filter) ClearDomainID() {
f.domainActive = false
}
func (f *Filter) FilterHeader(did uint32, ver uint16) bool {
if f.domainActive && f.DomainID != did {
return true
} else if f.versionActive && f.Version != ver {
return true
}
return false
}
func (f *Filter) Set(eid uint32, id uint16) {
if eid == 0 {
f.set(id)
f.baseEnabled = true
return
} else {
for i := range f.others {
if f.others[i].eid == eid {
f.others[i].set(id)
return
}
}
}
//if we hit here, we need to add another bitmap filter
nf := otherFilter{eid: eid}
nf.set(id)
f.others = append(f.others, nf)
}
func (f *Filter) IsSet(eid uint32, id uint16) bool {
if eid == 0 {
if !f.baseEnabled {
return true
}
return (f.uint16Bitmask[id>>3] & byte(1<<byte(id&0x7))) != 0
} else {
for i := range f.others {
if f.others[i].eid == eid {
return f.others[i].isset(id)
}
}
}
//filter not set for this eid
return true
}
func (f *Filter) Clear(eid uint32, id uint16) {
if eid == 0 {
f.clear(id)
return
} else {
for i := range f.others {
if f.others[i].eid == eid {
f.others[i].clear(id)
return
}
}
}
}
type uint16Bitmask [0x2000]byte
func (u *uint16Bitmask) set(v uint16) {
mask := byte(1 << byte(v&0x7))
off := v >> 3
(*u)[off] |= mask
}
func (u *uint16Bitmask) clear(v uint16) {
mask := byte(1 << byte(v&0x7))
off := v >> 3
(*u)[off] &= (mask ^ 0xff)
}
func (u *uint16Bitmask) isset(v uint16) bool {
mask := byte(1 << byte(v&0x7))
off := v >> 3
return ((*u)[off] & mask) != 0
}