ipset.lua 1.1 KB
Newer Older
1 2
--[[
Ipset file dumper for Alpine Wall
3
Copyright (C) 2012-2014 Kaarle Ritvanen
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
4
See LICENSE file for license details
5 6
]]--

7 8 9 10

local sortedkeys = require('awall.util').sortedkeys


11
local IPSet = require('awall.class')()
12

13
function IPSet:init(config) self.config = config or {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
14

15 16
function IPSet:dumpfile(name, ipsfile)
   ipsfile:write('# ipset '..name..'\n')
17
   ipsfile:write(table.concat(self.config[name].options, ' '))
18
   ipsfile:write('\n')
19 20
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
21
function IPSet:create()
22 23
   for name, ipset in pairs(self.config) do
      local pid = lpc.run('ipset', '-!', 'create', name,
24
			  unpack(ipset.options))
25
      if lpc.wait(pid) ~= 0 then
26
	 io.stderr:write('ipset creation failed: '..name)
27 28 29 30
      end
   end
end

31
function IPSet:print()
32
   for _, name in sortedkeys(self.config) do
33 34 35
      self:dumpfile(name, io.stdout)
      io.stdout:write('\n')
   end
36 37
end

38
function IPSet:dump(prefix)
39
   for name, ipset in pairs(self.config) do
40
      local fname = prefix..name
41 42 43 44 45 46 47
      local file = io.open(fname)
      if not file then
	 file = io.open(fname, 'w')
	 self:dumpfile(name, file)
      end
      file:close()
   end
48
end
49 50

return IPSet