forked from remyoudompheng/go-netlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
72 lines (60 loc) · 1.69 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package netlink
import (
"bytes"
"encoding/binary"
"os"
"syscall"
)
var (
SystemEndianness = binary.LittleEndian
globalSeq = uint32(0)
)
type NetlinkMsg interface {
toRawMsg() syscall.NetlinkMessage
}
// A netlink message with unparsed data
type RawNetlinkMessage syscall.NetlinkMessage
func (m RawNetlinkMessage) toRawMsg() syscall.NetlinkMessage {
return syscall.NetlinkMessage(m)
}
// Higher level implementation: let's suppose we're on a little-endian platform
// Write a netlink message to a socket
func WriteMessage(s *NetlinkConn, m NetlinkMsg) error {
w := bytes.NewBuffer(nil)
msg := m.toRawMsg()
msg.Header.Len = uint32(syscall.NLMSG_HDRLEN + len(msg.Data))
msg.Header.Seq = globalSeq
msg.Header.Pid = uint32(os.Getpid())
globalSeq++
binary.Write(w, SystemEndianness, msg.Header) // 16 bytes
_, er := w.Write(msg.Data)
_, er = s.Write(w.Bytes())
return er
}
// Reads a netlink message from a socket
func ReadMessage(s *NetlinkConn) (msg syscall.NetlinkMessage, er error) {
binary.Read(s.rbuf, SystemEndianness, &msg.Header)
msg.Data = make([]byte, msg.Header.Len-syscall.NLMSG_HDRLEN)
_, er = s.rbuf.Read(msg.Data)
return msg, er
}
type Attr struct {
Len uint16
Type uint16
}
type ParsedNetlinkMessage interface{}
// The structure of netlink error messages
type ErrorMessage struct {
Header syscall.NlMsghdr
Errno int32
WrongHeader syscall.NlMsghdr
}
// Parses a netlink error message
func ParseErrorMessage(msg syscall.NetlinkMessage) ErrorMessage {
var parsed ErrorMessage
parsed.Header = msg.Header
buf := bytes.NewBuffer(msg.Data)
binary.Read(buf, SystemEndianness, &parsed.Errno)
binary.Read(buf, SystemEndianness, &parsed.WrongHeader)
return parsed
}