-
Notifications
You must be signed in to change notification settings - Fork 48
/
ematch_ipset.go
61 lines (51 loc) · 1.08 KB
/
ematch_ipset.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
package tc
// IPSetDir defines the packet direction.
type IPSetDir uint8
// Various IP packet directions.
const (
IPSetSrc = IPSetDir(1)
IPSetDst = IPSetDir(2)
)
// IPSetMatch contains attributes of the ipset match discipline
type IPSetMatch struct {
IPSetID uint16
Dir []IPSetDir
}
type ipsetMatch struct {
ID uint16
Dim uint8
Flags uint8
}
func unmarshalIPSetMatch(data []byte, info *IPSetMatch) error {
tmp := ipsetMatch{}
if err := unmarshalStruct(data, &tmp); err != nil {
return err
}
info.IPSetID = tmp.ID
for i := uint8(1); i <= tmp.Dim; i++ {
if (tmp.Flags & (1 << i)) == (1 << i) {
info.Dir = append(info.Dir, IPSetSrc)
} else {
info.Dir = append(info.Dir, IPSetDst)
}
}
return nil
}
func marshalIPSetMatch(info *IPSetMatch) ([]byte, error) {
if info == nil {
return []byte{}, ErrNoArg
}
tmp := ipsetMatch{
ID: info.IPSetID,
}
if len(info.Dir) == 0 || len(info.Dir) > 3 {
return nil, ErrInvalidArg
}
for _, dir := range info.Dir {
tmp.Dim++
if dir == IPSetSrc {
tmp.Flags |= (1 << tmp.Dim)
}
}
return marshalStruct(&tmp)
}