Commit 18654d1e authored by Kaarle Ritvanen's avatar Kaarle Ritvanen

host: merge resolve and resolvelist

parent e7bc5922
......@@ -24,55 +24,55 @@ end
local dnscache = {}
local function resolve(host, context, network)
local family = getfamily(host, context)
if family == 'domain' then
if not dnscache[host] then
dnscache[host] = {}
for family, rtype in pairs{inet='A', inet6='AAAA'} do
local answer
for rec in io.popen('drill '..host..' '..rtype):lines() do
if answer then
if rec == '' then break end
local addr = rec:match(
'^'..familypatterns.domain..'%s+%d+%s+IN%s+'..rtype..
'%s+(.+)'
)
if addr then
assert(getfamily(addr, context) == family)
table.insert(dnscache[host], {family, addr})
end
elseif rec == ';; ANSWER SECTION:' then answer = true end
function M.resolve(list, context, network)
local res = {}
for _, host in listpairs(list) do
local family = getfamily(host, context)
local entry
if family == 'domain' then
if not dnscache[host] then
dnscache[host] = {}
for family, rtype in pairs{inet='A', inet6='AAAA'} do
local answer
for rec in io.popen('drill '..host..' '..rtype):lines() do
if answer then
if rec == '' then break end
local addr = rec:match(
'^'..familypatterns.domain..'%s+%d+%s+IN%s+'..rtype..
'%s+(.+)'
)
if addr then
assert(getfamily(addr, context) == family)
table.insert(dnscache[host], {family, addr})
end
elseif rec == ';; ANSWER SECTION:' then answer = true end
end
end
if not dnscache[host][1] then
context:error('Invalid host name: '..host)
end
table.sort(dnscache[host], function(a, b) return a[2] < b[2] end)
end
if not dnscache[host][1] then
context:error('Invalid host name: '..host)
end
table.sort(dnscache[host], function(a, b) return a[2] < b[2] end)
end
return dnscache[host]
end
entry = dnscache[host]
if not network and host:find('/') then
context:error('Network address not allowed: '..host)
end
elseif not network and host:find('/') then
context:error('Network address not allowed: '..host)
return {{family, host}}
end
else entry = {{family, host}} end
function M.resolvelist(list, context, network)
local res = {}
for _, host in listpairs(list) do
util.extend(res, resolve(host, context, network))
util.extend(res, entry)
end
return ipairs(res)
end
function M.resolveunique(list, families, context)
local res = {}
for _, addr in M.resolvelist(list, self) do
for _, addr in M.resolve(list, self) do
local family = addr[1]
if util.contains(families, family) then
if res[family] then context:error('Address must be unique') end
......
......@@ -11,7 +11,7 @@ local M = {}
local loadclass = require('awall').loadclass
M.class = require('awall.class')
local FAMILIES = require('awall.family').ALL
local resolvelist = require('awall.host').resolvelist
local resolve = require('awall.host').resolve
local isbuiltin = require('awall.iptables').isbuiltin
local optfrag = require('awall.optfrag')
......@@ -125,7 +125,7 @@ function M.Zone:optfrags(dir)
local aopts = nil
if self.addr then
aopts = {}
for _, addr in resolvelist(self.addr, self, true) do
for _, addr in resolve(self.addr, self, true) do
table.insert(
aopts,
{family=addr[1], [aprop]=addr[2], match='-'..aopt..' '..addr[2]}
......
--[[
Packet logging module for Alpine Wall
Copyright (C) 2012-2018 Kaarle Ritvanen
Copyright (C) 2012-2020 Kaarle Ritvanen
See LICENSE file for license details
]]--
local resolvelist = require('awall.host').resolvelist
local resolve = require('awall.host').resolve
local model = require('awall.model')
local class = model.class
......@@ -87,7 +87,7 @@ function Log:optfrags()
)
end
for _, addr in resolvelist(self.mirror, self) do
for _, addr in resolve(self.mirror, self) do
table.insert(targets, {family=addr[1], target='TEE --gateway '..addr[2]})
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