-
Notifications
You must be signed in to change notification settings - Fork 13
/
nl.lua
56 lines (41 loc) · 1.12 KB
/
nl.lua
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
-- SPDX-License-Identifier: MIT
-- Author: Jianhui Zhao <zhaojh329@gmail.com>
local socket = require 'eco.socket'
local nl = require 'eco.core.nl'
local M = {}
local nl_methods = {}
function nl_methods:bind(groups, pid)
local ok, err = self.sock:bind(groups, pid)
if not ok then
return nil, err
end
return true
end
function nl_methods:add_membership(group)
return self.sock:setoption('netlink_add_membership', group)
end
function nl_methods:drop_membership(group)
return self.sock:setoption('netlink_drop_membership', group)
end
function nl_methods:send(msg)
return self.sock:send(msg:binary())
end
function nl_methods:recv(n, timeout)
local data, err = self.sock:recv(n or 8192, timeout)
if not data then
return nil, err
end
return nl.nlmsg_ker(data)
end
function nl_methods:close()
self.sock:close()
end
local metatable = { __index = nl_methods }
function M.open(protocol)
local sock, err = socket.netlink(protocol)
if not sock then
return nil, err
end
return setmetatable({ sock = sock }, metatable)
end
return setmetatable(M, { __index = nl })