forked from zhaojh329/lua-eco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenl.lua
148 lines (111 loc) · 3.22 KB
/
genl.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
-- SPDX-License-Identifier: MIT
-- Author: Jianhui Zhao <[email protected]>
local genl = require 'eco.core.genl'
local sys = require 'eco.sys'
local nl = require 'eco.nl'
local M = {}
local cache = {}
local function parse_grps(nest, groups)
for _, grp in pairs(nl.parse_attr_nested(nest) or {}) do
local attrs = nl.parse_attr_nested(grp)
groups[nl.attr_get_str(attrs[genl.CTRL_ATTR_MCAST_GRP_NAME])] = nl.attr_get_u32(attrs[genl.CTRL_ATTR_MCAST_GRP_ID])
end
end
local function __get_family_by(sock, params)
local msg = nl.nlmsg(genl.GENL_ID_CTRL, nl.NLM_F_REQUEST)
msg:put(genl.genlmsghdr({ cmd = genl.CTRL_CMD_GETFAMILY }))
if params.id then
msg:put_attr_u16(genl.CTRL_ATTR_FAMILY_ID, params.id)
else
msg:put_attr_strz(genl.CTRL_ATTR_FAMILY_NAME, params.name)
end
local ok, err = sock:send(msg)
if not ok then
return nil, err
end
msg, err = sock:recv()
if not msg then
return nil, err
end
local nlh = msg:next()
if not nlh then
return nil, 'no msg responsed'
end
if nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
return nil, sys.strerror(-err)
end
local attrs = msg:parse_attr(genl.GENLMSGHDR_SIZE)
local info = {groups = {}}
info.name = nl.attr_get_str(attrs[genl.CTRL_ATTR_FAMILY_NAME])
info.id = nl.attr_get_u16(attrs[genl.CTRL_ATTR_FAMILY_ID])
info.version = nl.attr_get_u32(attrs[genl.CTRL_ATTR_VERSION])
info.hdrsize = nl.attr_get_u32(attrs[genl.CTRL_ATTR_HDRSIZE])
info.maxattr = nl.attr_get_u32(attrs[genl.CTRL_ATTR_MAXATTR])
if attrs[genl.CTRL_ATTR_MCAST_GROUPS] then
parse_grps(attrs[genl.CTRL_ATTR_MCAST_GROUPS], info.groups)
end
cache[info.id] = info
cache[info.name] = info
return info
end
local function get_family_by(params)
local sock, err = nl.open(nl.NETLINK_GENERIC)
if not sock then
return nil, err
end
local res, err = __get_family_by(sock, params)
sock:close()
if res then
return res
end
return nil, err
end
function M.get_family_byid(id)
if type(id) ~= 'number' then
error('invalid id')
end
if cache[id] then
return cache[id]
end
return get_family_by({ id = id })
end
function M.get_family_byname(name)
if type(name) ~= 'string' then
error('invalid name')
end
if cache[name] then
return cache[name]
end
return get_family_by({ name = name })
end
function M.get_family_id(name)
if type(name) ~= 'string' then
error('invalid name')
end
if cache[name] then
return cache[name].id
end
local info, err = get_family_by({ name = name })
if not info then
return nil, err
end
return info.id
end
function M.get_group_id(family, group)
if type(family) ~= 'string' then
error('invalid family name')
end
if type(group) ~= 'string' then
error('invalid group name')
end
if cache[family] then
return cache[family].groups[group]
end
local info, err = get_family_by({ name = family })
if not info then
return nil, err
end
return info.groups[group]
end
return setmetatable(M, { __index = genl })