Commit 09617e43 authored by Kaarle Ritvanen's avatar Kaarle Ritvanen

family: identify function

parent 488ad57f
--[[
Address family module for Alpine Wall
Copyright (C) 2012-2019 Kaarle Ritvanen
Copyright (C) 2012-2020 Kaarle Ritvanen
See LICENSE file for license details
]]--
local M = {ACTIVE={}, ALL={}}
local M = {
ACTIVE={},
ALL={},
PATTERNS={domain='[%a-][%.%w-]*', inet='%d[%.%d/-]+', inet6='[:%x/-]+'}
}
local stat = require('posix').stat
......@@ -13,4 +17,11 @@ for family, procfile in pairs{inet='raw', inet6='raw6'} do
if stat('/proc/net/'..procfile) then table.insert(M.ACTIVE, family) end
end
function M.identify(addr, context)
for k, v in pairs(M.PATTERNS) do
if addr:match('^'..v..'$') then return k end
end
context:error('Malformed host specification: '..addr)
end
return M
......@@ -7,28 +7,20 @@ See LICENSE file for license details
local M = {}
local family = require('awall.family')
local identify = family.identify
local util = require('awall.util')
local listpairs = util.listpairs
local familypatterns = {
inet='%d[%.%d/-]+', inet6='[:%x/-]+', domain='[%a-][%.%w-]*'
}
local function getfamily(addr, context)
for k, v in pairs(familypatterns) do
if addr:match('^'..v..'$') then return k end
end
context:error('Malformed host specification: '..addr)
end
local dnscache = {}
function M.resolve(list, context, allow)
local res = {}
for _, host in listpairs(list) do
local family = getfamily(host, context)
local family = identify(host, context)
local entry
if family == 'domain' then
......@@ -41,11 +33,11 @@ function M.resolve(list, context, allow)
if answer then
if rec == '' then break end
local addr = rec:match(
'^'..familypatterns.domain..'%s+%d+%s+IN%s+'..rtype..
'^'..family.PATTERNS.domain..'%s+%d+%s+IN%s+'..rtype..
'%s+(.+)'
)
if addr then
assert(getfamily(addr, context) == family)
assert(identify(addr, context) == family)
table.insert(dnscache[host], {family, addr})
end
elseif rec == ';; ANSWER SECTION:' then answer = true 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