forked from dandyvica/dqy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dig.lua
117 lines (105 loc) · 2.98 KB
/
dig.lua
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-- a lua script to mimic dig output
-- all RRs field names
require 'rdata'
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k, v in pairs(o) do
if type(k) ~= 'number' then k = '"' .. k .. '"' end
s = s .. '[' .. k .. '] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
-- display flags if they are set
local function bitlags(flags)
local s = ""
if flags.authorative_answer then
s = s .. "aa "
end
if flags.truncation then
s = s .. "tc "
end
if flags.recursion_desired then
s = s .. "rd "
end
if flags.recursion_available then
s = s .. "ra "
end
if flags.authentic_data then
s = s .. "ad "
end
if flags.checking_disabled then
s = s .. "cd "
end
return s
end
-- dump query
local function dump_query(query, resp)
print(";; Got answer:")
print(
string.format(";; ->>HEADER<<- opcode: %s, status: %s, id: %d",
string.upper(query.header.flags.op_code),
string.upper(query.header.flags.response_code),
query.header.id
)
)
print(
string.format(";; flags: %s; QUERY: %d, ANSWER: %d, AUTHORITY: %d, ADDITIONAL: %d",
bitlags(resp.header.flags),
resp.header.qd_count,
resp.header.an_count,
resp.header.ns_count,
resp.header.ar_count
)
)
print("\n;; QUESTION SECTION:")
print(
string.format(";%s %s %s",
query.question.qname,
query.question.qtype,
query.question.qclass
)
)
end
-- dump response
local function dump_response(resp)
-- ANSWER section depends on ns_count
print("\n;; ANSWER SECTION:")
for i = 1, resp.header.an_count do
print(
string.format("%s %d %s %s %s",
resp.answer[i].name,
resp.answer[i].ttl,
resp.answer[i].class,
resp.answer[i].type,
RData.format(resp.answer[i].type, resp.answer[i].rdata)
)
)
end
-- AUTHORITY section depends on ns_count
print("\n;; AUTHORITY SECTION:")
for i = 1, resp.header.ns_count do
print(
string.format("%s %d %s %s %s",
resp.authority[1].name,
resp.authority[1].ttl,
resp.authority[1].class,
resp.authority[1].type,
RData.format(resp.authority[1].type, resp.authority[1].rdata)
)
)
end
end
-- dump info structure
local function dump_info(i)
print(string.format("\n;; Query time: %d", i.elapsed))
print(string.format(";; SERVER: %s (%s)", i.endpoint, i.mode))
print(string.format(";; MSG SIZE rcvd: %d", i.bytes_received))
end
-- only 1 answer
dump_query(dns[1].query, dns[1].response)
dump_response(dns[1].response)
dump_info(info)