--[[ @object-name: igmp @object-desc: This is the igmp api. --]] local utils = require "oui.utils" local ubus = require "oui.ubus" local uci = require "uci" local M = {} --[[ @method-type: call @method-name: get_config @method-desc: 获取igmp配置 @out bool enable 是否开启igmp @out number version igmp 版本[1/2/3]. @in-example: {"jsonrpc":"2.0","method":"call","params":["","igmp","get_config",{}],"id":1} @out-example: {"jsonrpc":"2.0","id":1,"result":{"enable":false,"version":3}} --]] M.get_config = function() local c = uci.cursor() local device = c:get("network", "lan", "device") local enable = false c:foreach("network", "device", function(s) if s.name == device then enable = c:get("network", s[".name"], "igmp_snooping") == "1" end end) local data = utils.readfile("/etc/sysctl.conf") or "" local version = data:match("force_igmp_version=(%d+)") if version then version = tonumber(version) else version = 3 end return { enable = enable, version = version } end --[[ @method-name: set_config @method-desc: 设置igmp @in bool enable 是否开启igmp. @in number version igmp 版本[1/2/3]. @out number ?err_code Error code,-1:参数错误. @out string ?err_msg Error message @in-example: {"jsonrpc":"2.0","method":"call","params":["","igmp","set_config",{"version":3,"enable":false}],"id":1} @out-example: {"jsonrpc":"2.0","id":1,"result":{}} --]] M.set_config = function(params) local c = uci.cursor() if params.version < 1 or params.version > 3 then return { err_code = -1, err_msg = "version error" } end local device = c:get("network", "lan", "device") c:foreach("network", "device", function(s) if s.name == device then c:set("network", s[".name"], "igmp_snooping", params.enable and 1 or 0) end end) c:set("network", "guest", "igmp_snooping", params.enable and 1 or 0) c:commit("network"); local lines = {} for l in io.lines("/etc/sysctl.conf") do if not l:match("force_igmp_version") then if not l:match("\n") then l = l .. "\n" end lines[#lines + 1] = l end end lines[#lines + 1] = "net.ipv4.conf.all.force_igmp_version=" .. params.version .. "\n" utils.writefile("/etc/sysctl.conf", table.concat(lines)) ubus.call("network", "reload") os.execute("/etc/init.d/sysctl reload") end return M