Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions lib/resty/dns/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ local TYPE_MX = 15
local TYPE_TXT = 16
local TYPE_AAAA = 28
local TYPE_SRV = 33
local TYPE_OPT = 41
local TYPE_SPF = 99
local TYPE_ANY = 255

Expand Down Expand Up @@ -267,10 +268,10 @@ function _M.decode_request(self, req)
self.pos = self.pos + 2
local opt_ver_hi, opt_ver_lo = byte(self.buf, self.pos - 1, self.pos)
local opt_ver = lshift(opt_ver_hi, 8) + opt_ver_lo
if opt_ver ~= 0 then
self.response.header.rcode = RCODE_BADVERS
return nil, "bad EDNS0 opt version(" .. opt_ver .. ")"
end
-- if opt_ver ~= 0 then
-- self.response.header.rcode = RCODE_BADVERS
-- return nil, "bad EDNS0 opt version(" .. opt_ver .. ")"
-- end

-- parse RDLENGTH(describes RDATA)
self.pos = self.pos + 2
Expand All @@ -280,7 +281,11 @@ function _M.decode_request(self, req)
-- parse RDATA(OPTION)
-- rfc7871, 6. Option Format

local rdata = nil
if rdlen > 0 then
-- get whole rdata
rdata = strsub(self.buf, self.pos +1 , self.pos + rdlen)

-- parse OPTION-CODE
self.pos = self.pos + 2
local opt_code_hi, opt_code_lo = byte(self.buf, self.pos - 1, self.pos)
Expand Down Expand Up @@ -325,11 +330,17 @@ function _M.decode_request(self, req)
end
address = concat(ipv6, ":")
end

self.request.subnet[#self.request.subnet + 1] = {address = address,
mask = source_prefix_len,
family = opt_family}
mask = source_prefix_len,
family = opt_family}
end
self.request.additionals[#self.request.additionals + 1] = { name="",
type = opt_type,
class = udp_size,
ttl = ext_rcode,
rdlength = rdlen,
rdata = rdata }

else
ngx.log(ngx.WARN, "parse EDNS0 error. qname_len: ",
qname_len, " opt_type: ", opt_type)
Expand Down Expand Up @@ -375,7 +386,6 @@ local function _encode_4byt(x)
return char(hi_hi, hi_lo, lo_hi, lo_lo)
end


local function _encode_2byt(x)
local hi = band(rshift(x, 8), 0x00FF)
local lo = band(x, 0x00FF)
Expand Down Expand Up @@ -461,12 +471,14 @@ function _M.encode_response(self)
end

for i = 1, self.response.header.arcount do
buf = buf .. _encode_name(self.response.arsections[i].name)
buf = buf .. _encode_name(self.response.arsections[i].name)
buf = buf .. _encode_2byt(self.response.arsections[i].type)
buf = buf .. _encode_2byt(self.response.arsections[i].class)
buf = buf .. _encode_4byt(self.response.arsections[i].ttl or 0x258)
buf = buf .. _encode_2byt(self.response.arsections[i].rdlength)
buf = buf .. self.response.arsections[i].rdata
if self.response.arsections[i].rdlength > 0 then
buf = buf .. self.response.arsections[i].rdata
end
end

return buf
Expand Down Expand Up @@ -520,6 +532,17 @@ function _M.create_a_answer(self, name, ttl, ipv4)
return nil
end

function _M.replay_additional_opts(self)

for i = 1, #self.request.additionals do
local additional = self.request.additionals[i]
self.response.header.arcount = self.response.header.arcount + 1
self.response.arsections[self.response.header.arcount] = additional
end

return nil
end


function _M.create_cname_answer(self, name, ttl, cname)
if not name or #name == 0 then
Expand Down