forked from ti-mo/netfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
40 lines (28 loc) · 939 Bytes
/
message.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
package netfilter
import "github.com/mdlayher/netlink"
// UnmarshalNetlink unmarshals a netlink.Message into a Netfilter Header and Attributes.
func UnmarshalNetlink(msg netlink.Message) (Header, []Attribute, error) {
var h Header
err := h.unmarshal(msg)
if err != nil {
return Header{}, nil, err
}
attrs, err := unmarshalAttributes(msg.Data[nfHeaderLen:])
if err != nil {
return Header{}, nil, err
}
return h, attrs, nil
}
// MarshalNetlink takes a Netfilter Header and Attributes and returns a netlink.Message.
func MarshalNetlink(h Header, attrs []Attribute) (netlink.Message, error) {
ba, err := marshalAttributes(attrs)
if err != nil {
return netlink.Message{}, err
}
// initialize with 4 bytes of Data before unmarshal
nlm := netlink.Message{Data: make([]byte, 4)}
// marshal error ignored, safe to do if msg Data is initialized
h.marshal(&nlm)
nlm.Data = append(nlm.Data, ba...)
return nlm, nil
}