comparison mod_firewall/mod_firewall.lua @ 2078:11539785cb92

mod_firewall: Split compile function into two parts in order to separate their scopes
author Kim Alvefur <zash@zash.se>
date Fri, 11 Mar 2016 18:48:34 +0100
parents 368b091e723b
children a435db77a5e5
comparison
equal deleted inserted replaced
2077:368b091e723b 2078:11539785cb92
132 local rule = { conditions = {}, actions = {}, deps = {} }; 132 local rule = { conditions = {}, actions = {}, deps = {} };
133 table.insert(ruleset[chain], rule); 133 table.insert(ruleset[chain], rule);
134 return rule; 134 return rule;
135 end 135 end
136 136
137 local function compile_firewall_rules(filename) 137 local function parse_firewall_rules(filename)
138 local line_no = 0; 138 local line_no = 0;
139 139
140 local function errmsg(err) 140 local function errmsg(err)
141 return "Error compiling "..filename.." on line "..line_no..": "..err; 141 return "Error compiling "..filename.." on line "..line_no..": "..err;
142 end 142 end
264 for _, dep in ipairs(condition_deps or {}) do 264 for _, dep in ipairs(condition_deps or {}) do
265 table.insert(rule.deps, dep); 265 table.insert(rule.deps, dep);
266 end 266 end
267 end 267 end
268 end 268 end
269 269 return ruleset;
270 end
271
272 local function process_firewall_rules(ruleset)
270 -- Compile ruleset and return complete code 273 -- Compile ruleset and return complete code
271 274
272 local chain_handlers = {}; 275 local chain_handlers = {};
273 276
274 -- Loop through the chains in the parsed ruleset (e.g. incoming, outgoing) 277 -- Loop through the chains in the parsed ruleset (e.g. incoming, outgoing)
338 end 341 end
339 342
340 return chain_handlers; 343 return chain_handlers;
341 end 344 end
342 345
346 local function compile_firewall_rules(filename)
347 local ruleset, err = parse_firewall_rules(filename);
348 if not ruleset then return nil, err; end
349 local chain_handlers = process_firewall_rules(ruleset);
350 return chain_handlers;
351 end
352
343 local function compile_handler(code_string, filename) 353 local function compile_handler(code_string, filename)
344 -- Prepare event handler function 354 -- Prepare event handler function
345 local chunk, err = loadstring(code_string, "="..filename); 355 local chunk, err = loadstring(code_string, "="..filename);
346 if not chunk then 356 if not chunk then
347 return nil, "Error compiling (probably a compiler bug, please report): "..err; 357 return nil, "Error compiling (probably a compiler bug, please report): "..err;