Skip to content

Commit

Permalink
reverse DNS utitiies improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoe2k4 authored and agentzh committed Jan 28, 2016
1 parent bb7ce22 commit 760d523
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.markdown
Expand Up @@ -252,9 +252,9 @@ will yield `FF01::101` in the `new_addr` return value.

expand_ipv6_addr
------------------
`syntax: expandeded = resty.dns.resolver.expand_ipv6_addr(address)`
`syntax: expanded = resty.dns.resolver.expand_ipv6_addr(address)`

Expand the successive 16-bit zero groups in the textual format of the IPv6 address.
Expands the successive 16-bit zero groups in the textual format of the IPv6 address.

For example,

Expand Down
75 changes: 46 additions & 29 deletions lib/resty/dns/resolver.lua
Expand Up @@ -23,16 +23,22 @@ local log = ngx.log
local DEBUG = ngx.DEBUG
local randomseed = math.randomseed
local ngx_time = ngx.time
local unpack = unpack
local setmetatable = setmetatable
local type = type


local ok, new_tab = pcall(require, "table.new")
if not ok then
new_tab = function (narr, nrec) return {} end
end


local DOT_CHAR = byte(".")
local ZERO_CHAR = byte("0")
local COLON_CHAR = byte(":")

local IP6_ARPA = "ip6.arpa"

local TYPE_A = 1
local TYPE_NS = 2
Expand Down Expand Up @@ -74,6 +80,17 @@ local resolver_errstrs = {
local mt = { __index = _M }


local arpa_tmpl = new_tab(72, 0)

for i = 1, #IP6_ARPA do
arpa_tmpl[64 + i] = byte(IP6_ARPA, i)
end

for i = 2, 64, 2 do
arpa_tmpl[i] = DOT_CHAR
end


function _M.new(class, opts)
if not opts then
return nil, "no options table specified"
Expand Down Expand Up @@ -803,66 +820,66 @@ end


local function _expand_ipv6_addr(addr)
if find(addr, "::") then
local ncol = 0
if find(addr, "::", 1, true) then
local ncol, addrlen = 8, #addr

for i=1, #addr do
if sub(addr, i, i) == ":" then
ncol = ncol + 1
for i = 1, addrlen do
if byte(addr, i) == COLON_CHAR then
ncol = ncol - 1
end
end

if addr == "::" then
addr = "0::0"
elseif sub(addr, 1, 2) == "::" then
if byte(addr, 1) == COLON_CHAR then
addr = "0" .. addr
end

addr = re_sub(addr, "::", ":" .. rep("0:",8 - ncol), "jo")
if byte(addr, -1) == COLON_CHAR then
addr = addr .. "0"
end

addr = re_sub(addr, "::", ":" .. rep("0:", ncol), "jo")
end

return addr
end


function _M.expand_ipv6_addr = _expand_ipv6_addr
_M.expand_ipv6_addr = _expand_ipv6_addr


function _M.arpa_str(addr)
local arpa = ".in-addr.arpa"

if find(addr, ":") then
addr, arpa = _expand_ipv6_addr(addr), ".ip6.arpa"
local tmp, idx, hidx, addrlen = new_tab(32,0), 1, 1, #addr

for i = addrlen, 1, -1 do
local s = sub(addr, i, i)
if s == ":" then
if find(addr, ":", 1, true) then
addr = _expand_ipv6_addr(addr)
local idx, hidx, addrlen = 1, 1, #addr

for i = addrlen, 0, -1 do
local s = byte(addr, i)
if s == COLON_CHAR or not s then
for j = hidx, 4 do
tmp[idx] = "0"
idx = idx + 1
arpa_tmpl[idx] = ZERO_CHAR
idx = idx + 2
end
hidx = 1
else
tmp[idx] = s
idx = idx + 1
arpa_tmpl[idx] = s
idx = idx + 2
hidx = hidx + 1
end
end

addr = concat(tmp, ".")
addr = char(unpack(arpa_tmpl))
else
addr = re_sub(addr, [[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})]],
"$4.$3.$2.$1", "ajo")
addr = re_sub(addr, [[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})]],
"$4.$3.$2.$1.in-addr.arpa", "ajo")
end
return addr .. arpa

return addr
end


function _M.reverse_query(self, addr)
return self.query(self, self.arpa_str(addr),
{ qtype = self.TYPE_PTR })
{qtype = self.TYPE_PTR})
end


Expand Down

0 comments on commit 760d523

Please sign in to comment.