forked from RoganDawes/P4wnP1_aloa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
53 lines (41 loc) · 1017 Bytes
/
message.go
File metadata and controls
53 lines (41 loc) · 1017 Bytes
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
package mgenetlink
import (
"github.com/mame82/P4wnP1_aloa/mnetlink"
"golang.org/x/sys/unix"
"errors"
)
var (
EInvalidMessage = errors.New("Invalid generic netlink message")
)
type Message struct {
Cmd uint8
Version uint8
Reserved uint16
Data []byte
}
func (m *Message) MarshalBinary() (data []byte, err error) {
data = make([]byte, unix.GENL_HDRLEN + len(m.Data))
data[0] = m.Cmd
data[1] = m.Version
copy(data[unix.GENL_HDRLEN:], m.Data)
return
}
func (m *Message) UnmarshalBinary(src []byte) (err error) {
if len(src) < unix.GENL_HDRLEN {
return EInvalidMessage
}
m.Cmd = src[0]
m.Version = src[1]
m.Data = src[unix.GENL_HDRLEN:]
return
}
func (m Message) AttributesFromData() (attrs []mnetlink.Attr, err error) {
for offs := 0; len(m.Data[offs:]) >= unix.NLA_HDRLEN; {
attr := mnetlink.Attr{}
err = attr.UnmarshalBinary(m.Data[offs:])
if err != nil { return nil, err }
attrs = append(attrs, attr)
offs += mnetlink.AlignAttr(int(attr.Len))
}
return attrs, nil
}