ttl.lua 941 Bytes
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
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
--[[
TTL adjustment module for Alpine Wall
Copyright (C) 2012-2016 Kaarle Ritvanen
See LICENSE file for license details
]]--


local model = require('awall.model')


local TTLRule = model.class(model.Rule)

function TTLRule:trules()
   local res = {}
   for _, rule in ipairs(TTLRule.super(self):trules()) do
      if rule.family == 'inet' then table.insert(res, rule) end
   end
   return res
end

function TTLRule:table() return 'mangle' end

function TTLRule:target()
   if not self.ttl then self:error('TTL not specified') end

   if type(self.ttl) == 'string' then
      if self.ttl:sub(1, 1) == '+' then
         return 'TTL --ttl-inc '..self.ttl:sub(2, -1)
      else self.ttl = tonumber(self.ttl) end
   end
   if type(self.ttl) ~= 'number' then
      self:error('Invalid TTL specification')
   end

   return 'TTL --ttl-'..(self.ttl < 0 and 'dec' or 'set')..' '..
      math.abs(self.ttl)
end

return {export={ttl={class=TTLRule}}}