summaryrefslogtreecommitdiffstats
path: root/awall/host.lua
blob: 2e94c3d3b03cc2873852e2d4fd912b0e736c8579 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
--[[
Host address resolver for Alpine Wall
Copyright (C) 2012 Kaarle Ritvanen
See LICENSE file for license details
]]--


module(..., package.seeall)

local familypatterns = {inet='%d[%.%d/]+',
			inet6='[:%x/]+',
			domain='[%a-][%.%w-]*'}

local function getfamily(addr, context)
   for k, v in pairs(familypatterns) do
      if string.match(addr, '^'..v..'$') then return k end
   end
   context:error('Malformed host specification: '..addr)
end

local dnscache = {}

function resolve(host, context)
   local family = getfamily(host, context)
   if family == 'domain' then

      if not dnscache[host] then
	 dnscache[host] = {}
	 for rec in io.popen('dig -t ANY '..host):lines() do
	    local name, rtype, addr =
	       string.match(rec, '^('..familypatterns.domain..')%s+%d+%s+IN%s+(A+)%s+(.+)')

	    if name and string.sub(name, 1, string.len(host) + 1) == host..'.' then
	       if rtype == 'A' then family = 'inet'
	       elseif rtype == 'AAAA' then family = 'inet6'
	       else family = nil end

	       if family then
		  assert(getfamily(addr, context) == family)
		  table.insert(dnscache[host], {family, addr})
	       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

      return dnscache[host]
   end

   return {{family, host}}
end