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)
require 'awall'
require 'awall.host'
require 'awall.util'
require 'awall.object'
require 'awall.optfrag'
local util = awall.util
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(...)
local inst = arg[1] and arg[1] or {}
cls.morph(inst)
return inst
function ConfigObject:init(context)
if context then
self.context = context
self.root = context.input
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
Object = class()
function Object:init() end
function Object:trules() return {} end
function ConfigObject:trules() return {} end
Zone = class(Object)
Zone = class(ConfigObject)
function Zone:optfrags(dir)
local iopt, aopt, iprop, aprop
......@@ -80,10 +64,12 @@ end
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
self[prop] = self[prop] and util.maplist(self[prop],
function(z)
......
......@@ -16,8 +16,8 @@ local util = awall.util
local NATRule = model.class(model.Rule)
function NATRule:init()
model.Rule.init(self)
function NATRule:init(context)
model.Rule.init(self, context)
if util.contains({self['in'], self.out}, fwzone) then
error('NAT rules not allowed for firewall zone')
end
......@@ -53,8 +53,8 @@ end
local DNATRule = model.class(NATRule)
function DNATRule:init()
NATRule.init(self)
function DNATRule:init(context)
NATRule.init(self, context)
self.params = {forbidif='out', subject='destination',
chain='PREROUTING', target='DNAT'}
end
......@@ -62,8 +62,8 @@ end
local SNATRule = model.class(NATRule)
function SNATRule:init()
NATRule.init(self)
function SNATRule:init(context)
NATRule.init(self, context)
self.params = {forbidif='in', subject='source',
chain='POSTROUTING', target='SNAT'}
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