Created
July 29, 2012 10:04
-
-
Save aeden/3197209 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Example of using Ratchet to resolve DNS queries | |
-- Inspect function to print the results: | |
function inspect(prefix, t) | |
for k,v in pairs(t) do | |
if (type(v) == "table") then | |
inspect(string.format("%s (%s):", prefix, k), v) | |
else | |
print(prefix, string.format("%s: %s", k, v)) | |
end | |
end | |
end | |
-- Here are the questions to test. | |
-- NOTE: currently querying a TXT on dnsimple.com core dumps (dig dnsimple.com txt) | |
questions = {} | |
questions["dnsimple.com"] = { | |
[1] = "a", | |
[2] = "ns", | |
[3] = "mx", | |
[4] = "spf", -- There should be a result here but none is reported (dig dnsimple.com spf) | |
[5] = "srv" -- There should be a result here but none is reported (dig dnsimple.com srv) | |
-- [6] = "sshfp" -- This should work, but enabling it causes processing to stop when it is processed | |
-- [7=] = "txt" -- SEGFAULT | |
} | |
questions["www.dnsimple.com"] = { | |
[1] = "cname" | |
} | |
questions["ns1.dnsimple.com"] = { | |
[1] = "aaaa" | |
} | |
questions["naptr-test.dnsimple.com"] = { | |
[1] = "naptr" -- Not supported (dig naptr-test.dnsimple.com naptr) | |
} | |
-- The ratchet code. Switch out the name and type as needed. | |
require "ratchet" | |
kernel = ratchet.new(function() | |
for qname, qtypes in pairs(questions) do | |
for i, qtype in ipairs(qtypes) do | |
local rec, err | |
local status, ex = pcall(function() | |
rec, err = ratchet.dns.query(qname, qtype) | |
end) | |
if status then | |
if (type(err) == "nil") then | |
if (type(rec) == "nil") then | |
print(string.format("No records for %s (%s)", qname, qtype)) | |
else | |
inspect(string.format("%s (%s)", qname, qtype), rec) | |
end | |
else | |
print(string.format("Error for %s (%s): %s", qname, qtype, err)) | |
end | |
else | |
print(string.format("Exception for %s (%s): %s", qname, qtype, ex)) | |
end | |
end | |
end | |
end) | |
kernel:loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment