Commit e7bc5922 authored by Kaarle Ritvanen's avatar Kaarle Ritvanen

host: resolveunique function

parent f154fb75
......@@ -8,6 +8,7 @@ See LICENSE file for license details
local M = {}
local util = require('awall.util')
local listpairs = util.listpairs
local familypatterns = {
......@@ -23,7 +24,7 @@ end
local dnscache = {}
function M.resolve(host, context, network)
local function resolve(host, context, network)
local family = getfamily(host, context)
if family == 'domain' then
......@@ -63,11 +64,27 @@ end
function M.resolvelist(list, context, network)
local res = {}
for _, host in util.listpairs(list) do
util.extend(res, M.resolve(host, context, network))
for _, host in listpairs(list) do
util.extend(res, resolve(host, context, network))
end
return ipairs(res)
end
function M.resolveunique(list, families, context)
local res = {}
for _, addr in M.resolvelist(list, self) do
local family = addr[1]
if util.contains(families, family) then
if res[family] then context:error('Address must be unique') end
res[family] = addr[2]
end
end
for _, family in listpairs(families) do
if not res[family] then
context:error('No address provided for family '..family)
end
end
return res
end
return M
......@@ -7,7 +7,7 @@ See LICENSE file for license details
local loadclass = require('awall').loadclass
local FAMILIES = require('awall.family').ALL
local resolve = require('awall.host').resolve
local resolveunique = require('awall.host').resolveunique
local model = require('awall.model')
local class = model.class
......@@ -107,22 +107,7 @@ function TranslatingRule:init(...)
end
if type(self.dnat) == 'string' then self.dnat = {addr=self.dnat} end
local dnataddr
for _, addr in ipairs(resolve(self.dnat.addr, self)) do
if addr[1] == 'inet' then
if dnataddr then
self:error(
self.dnat.addr..' resolves to multiple IPv4 addresses'
)
end
dnataddr = addr[2]
end
end
if not dnataddr then
self:error(self.dnat.addr..' does not resolve to any IPv4 address')
end
self.dnat.addr = dnataddr
self.dnat.addr = resolveunique(self.dnat.addr, 'inet', self).inet
end
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