forked from grubern/netlink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.go
79 lines (71 loc) · 1.64 KB
/
socket.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
73
74
75
76
77
78
79
package netlink
import (
"fmt"
"os"
"syscall"
)
type NetlinkProtocol int
const (
CONNECTOR NetlinkProtocol = syscall.NETLINK_CONNECTOR
INET_DIAG NetlinkProtocol = syscall.NETLINK_INET_DIAG
)
type NetlinkSocket struct {
fd int
protocol NetlinkProtocol
groups uint32
}
func NewNetlinkSocket(protocol NetlinkProtocol, groups uint32) (*NetlinkSocket, error) {
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, int(protocol))
if err != nil {
return nil, err
}
ns := &NetlinkSocket{
fd: fd,
protocol: protocol,
groups: groups,
}
if ns.protocol == CONNECTOR {
sockaddr := &syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
Pid: uint32(os.Getpid()),
Groups: groups,
}
if err = syscall.Bind(fd, sockaddr); err != nil {
syscall.Close(fd)
return nil, err
}
}
return ns, nil
}
func (ns *NetlinkSocket) Close() {
syscall.Close(ns.fd)
}
func (ns *NetlinkSocket) Send(request *NetlinkRequest) error {
if ns.protocol == CONNECTOR {
if _, err := syscall.Write(ns.fd, request.Serialize()); err != nil {
return err
}
} else {
sockaddr := &syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
Pid: 0,
Groups: ns.groups,
}
if err := syscall.Sendto(ns.fd, request.Serialize(), 0, sockaddr); err != nil {
return err
}
}
return nil
}
func (s *NetlinkSocket) Receive() ([]syscall.NetlinkMessage, error) {
rb := make([]byte, syscall.Getpagesize())
nr, _, err := syscall.Recvfrom(s.fd, rb, 0)
if err != nil {
return nil, err
}
if nr < syscall.NLMSG_HDRLEN {
return nil, fmt.Errorf("Got short response from netlink")
}
rb = rb[:nr]
return syscall.ParseNetlinkMessage(rb)
}