-
Notifications
You must be signed in to change notification settings - Fork 48
/
ematch_nbyte.go
57 lines (49 loc) · 1.19 KB
/
ematch_nbyte.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
package tc
import (
"encoding/binary"
"fmt"
)
// NByteMatch contains attributes of the Nbyte match discipline
type NByteMatch struct {
Offset uint16
Layer uint8
Needle []byte
}
type tcfEmNByte struct {
off uint16
len uint16
layer uint8
}
func unmarshalNByteMatch(data []byte, info *NByteMatch) error {
if len(data) < 8 {
return fmt.Errorf("unmarshalNByteMatch: incomplete data: %w",
ErrInvalidArg)
}
// We can not unmarshal elements of a non-public struct.
// So we extract the elements by hand.
info.Offset = binary.LittleEndian.Uint16(data[:2])
needleLen := binary.LittleEndian.Uint16(data[2:4])
info.Layer = uint8(data[4])
if len(data) < (8 + int(needleLen)) {
return fmt.Errorf("unmarshalNByteMatch: invalid needle: %w",
ErrInvalidArg)
}
info.Needle = data[8 : 8+needleLen]
return nil
}
func marshalNByteMatch(info *NByteMatch) ([]byte, error) {
if info == nil {
return []byte{}, fmt.Errorf("marshalNByteMatch: %w", ErrNoArg)
}
nbyte := tcfEmNByte{
off: info.Offset,
len: uint16(len(info.Needle)),
layer: info.Layer,
}
tmp, err := marshalAndAlignStruct(nbyte)
if err != nil {
return []byte{}, err
}
tmp = append(tmp, info.Needle...)
return tmp, nil
}