forked from typetypetype/conntrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetlink_attr.go
44 lines (37 loc) · 919 Bytes
/
netlink_attr.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
package conntrack
// Netlink attr parsing.
import (
"encoding/binary"
"errors"
)
const attrHdrLength = 4
type Attr struct {
Msg []byte
Typ int
IsNested bool
IsNetByteorder bool
}
func parseAttrs(b []byte, attrs []Attr) ([]Attr, error) {
for len(b) >= attrHdrLength {
var attr Attr
attr, b = parseAttr(b)
attrs = append(attrs, attr)
}
if len(b) != 0 {
return nil, errors.New("leftover attr bytes")
}
return attrs, nil
}
func parseAttr(b []byte) (Attr, []byte) {
l := binary.LittleEndian.Uint16(b[0:2])
// length is header + payload
l -= uint16(attrHdrLength)
typ := binary.LittleEndian.Uint16(b[2:4])
attr := Attr{
Msg: b[attrHdrLength : attrHdrLength+int(l)],
Typ: int(typ & NLA_TYPE_MASK),
IsNested: typ&NLA_F_NESTED > 0,
IsNetByteorder: typ&NLA_F_NET_BYTEORDER > 0,
}
return attr, b[rtaAlignOf(attrHdrLength+int(l)):]
}