Skip to content

Commit

Permalink
Merge pull request luvit#945 from n1tehawk/20170131_resolvebyip
Browse files Browse the repository at this point in the history
dns: Add the ability to handle numeric addresses
  • Loading branch information
creationix authored Jan 31, 2017
2 parents 6dbd079 + 211c59b commit ac46cbc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
17 changes: 16 additions & 1 deletion deps/dns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.
-- https://github.com/openresty/lua-resty-dns/blob/master/lib/resty/dns/resolver.lua
--[[lit-meta
name = "luvit/dns"
version = "2.0.2"
version = "2.0.3"
dependencies = {
"luvit/[email protected]",
"luvit/[email protected]",
Expand Down Expand Up @@ -51,6 +51,7 @@ local byte = string.byte
local gsub = string.gsub
local sub = string.sub
local format = string.format
local match = string.match
local band = bit.band
local bor = bit.bor
local rshift = bit.rshift
Expand Down Expand Up @@ -604,6 +605,20 @@ local function parse_response(buf, id, server)
end

local function _query(servers, name, dnsclass, qtype, callback)

-- Try to resolve IPs directly, without contacting DNS servers
if (qtype == TYPE_A and match(name, "^%d+%.%d+%.%d+%.%d+$")) -- numeric IPv4
or (qtype == TYPE_AAAA and match(name, "^[:%x]+$")) -- hexadecimal IPv6
then
-- Answer query 'locally', by passing suitable arguments to the callback
-- function. This reports a single address result, with no (`nil`) name,
-- an empty "server" table and a TTL of 0.
callback(nil, {
{address = name, type = qtype, class = CLASS_IN, server = {}, ttl = 0}
})
return
end

local tries, max_tries, server, tcp_iter, udp_iter, get_server_iter

tries = 1
Expand Down
2 changes: 1 addition & 1 deletion package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ return {
"luvit/[email protected]",
"luvit/[email protected]",
"luvit/[email protected]",
"luvit/[email protected].2",
"luvit/[email protected].3",
"luvit/[email protected]",
"luvit/[email protected]",
"luvit/[email protected]",
Expand Down
11 changes: 9 additions & 2 deletions tests/test-dns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,22 @@ require('tap')(function (test)
p(answers)
end))
end)
test("resolve4ip", function (expect)
dns.resolve4('127.0.0.1', expect(function(err, answers)
assert(not err)
assert(#answers > 0)
p(answers)
end))
end)
test("resolve6", function (expect)
dns.resolve6('luvit.io', expect(function(err, answers)
assert(not err)
p(answers)
assert(#answers > 0)
end))
end)
test("resolve6", function (expect)
dns.resolve6('luvit.io', expect(function(err, answers)
test("resolve6ip", function (expect)
dns.resolve6('::1', expect(function(err, answers)
assert(not err)
p(answers)
assert(#answers > 0)
Expand Down

0 comments on commit ac46cbc

Please sign in to comment.