forked from grubern/netlink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest.go
57 lines (51 loc) · 1.09 KB
/
request.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 netlink
import (
"os"
"syscall"
"unsafe"
)
type NetlinkRequestData interface {
Len() int
Serialize() []byte
}
// linux/netlink.h
type NetlinkRequest struct {
syscall.NlMsghdr
Data []NetlinkRequestData
}
func (req *NetlinkRequest) Serialize() []byte {
length := syscall.SizeofNlMsghdr
dataBytes := make([][]byte, len(req.Data))
for i, data := range req.Data {
dataBytes[i] = data.Serialize()
length = length + len(dataBytes[i])
}
req.Len = uint32(length)
b := make([]byte, length)
hdr := (*(*[syscall.SizeofNlMsghdr]byte)(unsafe.Pointer(req)))[:]
next := syscall.SizeofNlMsghdr
copy(b[0:next], hdr)
for _, data := range dataBytes {
for _, dataByte := range data {
b[next] = dataByte
next = next + 1
}
}
return b
}
func (req *NetlinkRequest) AddData(data NetlinkRequestData) {
if data != nil {
req.Data = append(req.Data, data)
}
}
func NewNetlinkRequest() *NetlinkRequest {
return &NetlinkRequest{
NlMsghdr: syscall.NlMsghdr{
Len: uint32(0),
Type: uint16(syscall.NLMSG_DONE),
Flags: uint16(0),
Seq: uint32(0),
Pid: uint32(os.Getpid()),
},
}
}