Commit c3126d2e authored by Kaarle Ritvanen's avatar Kaarle Ritvanen

class model generalized and moved to a self-contained module

parent 2a788938
...@@ -10,44 +10,28 @@ module(..., package.seeall) ...@@ -10,44 +10,28 @@ module(..., package.seeall)
require 'awall' require 'awall'
require 'awall.host' require 'awall.host'
require 'awall.util' require 'awall.util'
require 'awall.object'
require 'awall.optfrag' require 'awall.optfrag'
local util = awall.util local util = awall.util
local combinations = awall.optfrag.combinations local combinations = awall.optfrag.combinations
class = awall.object.class
function class(base)
local cls = {}
local mt = {__index = cls}
if base then setmetatable(cls, {__index = base}) end ConfigObject = class(awall.object.Object)
function cls.new(...) function ConfigObject:init(context)
local inst = arg[1] and arg[1] or {} if context then
cls.morph(inst) self.context = context
return inst self.root = context.input
end end
function cls:morph(context)
setmetatable(self, mt)
if context then
self.context = context
self.root = context.input
end
self:init()
end
return cls
end end
Object = class() function ConfigObject:trules() return {} end
function Object:init() end
function Object:trules() return {} end
Zone = class(Object) Zone = class(ConfigObject)
function Zone:optfrags(dir) function Zone:optfrags(dir)
local iopt, aopt, iprop, aprop local iopt, aopt, iprop, aprop
...@@ -80,10 +64,12 @@ end ...@@ -80,10 +64,12 @@ end
fwzone = Zone.new() fwzone = Zone.new()
Rule = class(Object) Rule = class(ConfigObject)
function Rule:init(context)
ConfigObject.init(self, context)
function Rule:init()
for i, prop in ipairs({'in', 'out'}) do for i, prop in ipairs({'in', 'out'}) do
self[prop] = self[prop] and util.maplist(self[prop], self[prop] = self[prop] and util.maplist(self[prop],
function(z) function(z)
......
...@@ -16,8 +16,8 @@ local util = awall.util ...@@ -16,8 +16,8 @@ local util = awall.util
local NATRule = model.class(model.Rule) local NATRule = model.class(model.Rule)
function NATRule:init() function NATRule:init(context)
model.Rule.init(self) model.Rule.init(self, context)
if util.contains({self['in'], self.out}, fwzone) then if util.contains({self['in'], self.out}, fwzone) then
error('NAT rules not allowed for firewall zone') error('NAT rules not allowed for firewall zone')
end end
...@@ -53,8 +53,8 @@ end ...@@ -53,8 +53,8 @@ end
local DNATRule = model.class(NATRule) local DNATRule = model.class(NATRule)
function DNATRule:init() function DNATRule:init(context)
NATRule.init(self) NATRule.init(self, context)
self.params = {forbidif='out', subject='destination', self.params = {forbidif='out', subject='destination',
chain='PREROUTING', target='DNAT'} chain='PREROUTING', target='DNAT'}
end end
...@@ -62,8 +62,8 @@ end ...@@ -62,8 +62,8 @@ end
local SNATRule = model.class(NATRule) local SNATRule = model.class(NATRule)
function SNATRule:init() function SNATRule:init(context)
NATRule.init(self) NATRule.init(self, context)
self.params = {forbidif='in', subject='source', self.params = {forbidif='in', subject='source',
chain='POSTROUTING', target='SNAT'} chain='POSTROUTING', target='SNAT'}
end end
......
--[[
Class model with inheritance and morphing support for Alpine Wall
Copyright (C) 2012 Kaarle Ritvanen
Licensed under the terms of GPL2
]]--
module(..., package.seeall)
function class(base)
local cls = {}
local mt = {__index = cls}
if base then setmetatable(cls, {__index = base}) end
function cls.new(...)
local inst = arg[1] and arg[1] or {}
cls.morph(inst)
return inst
end
function cls:morph(...)
setmetatable(self, mt)
self:init(unpack(arg))
end
return cls
end
Object = class()
function Object:init(...) end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment