Skip to content

Commit 4a2b060

Browse files
committed
add NAPTR record support
1 parent 3c382de commit 4a2b060

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Table of Contents
2828
* [TYPE_TXT](#type_txt)
2929
* [TYPE_AAAA](#type_aaaa)
3030
* [TYPE_SRV](#type_srv)
31+
* [TYPE_NAPTR](#type_naptr)
3132
* [TYPE_SPF](#type_spf)
3233
* [CLASS_IN](#class_in)
3334
* [SECTION_AN](#section_an)
@@ -373,6 +374,16 @@ See RFC 2782 for details.
373374

374375
[Back to TOC](#table-of-contents)
375376

377+
TYPE_NAPTR
378+
----------
379+
`syntax: typ = r.TYPE_NAPTR`
380+
381+
The `NAPTR` resource record type, equal to the decimal number `35`.
382+
383+
See RFC 2915 for details.
384+
385+
[Back to TOC](#table-of-contents)
386+
376387
TYPE_SPF
377388
---------
378389
`syntax: typ = r.TYPE_SPF`

lib/resty/dns/resolver.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ local TYPE_MX = 15
4848
local TYPE_TXT = 16
4949
local TYPE_AAAA = 28
5050
local TYPE_SRV = 33
51+
local TYPE_NAPTR = 35
5152
local TYPE_SPF = 99
5253

5354
local CLASS_IN = 1
@@ -67,6 +68,7 @@ local _M = {
6768
TYPE_TXT = TYPE_TXT,
6869
TYPE_AAAA = TYPE_AAAA,
6970
TYPE_SRV = TYPE_SRV,
71+
TYPE_NAPTR = TYPE_NAPTR,
7072
TYPE_SPF = TYPE_SPF,
7173
CLASS_IN = CLASS_IN,
7274
SECTION_AN = SECTION_AN,
@@ -210,6 +212,21 @@ local function _encode_name(s)
210212
end
211213

212214

215+
local function _decode_string(buf, pos)
216+
local slen = byte(buf, pos)
217+
218+
if slen == 0 then
219+
return "", pos + 1
220+
end
221+
222+
if pos + 1 + slen >= #buf then
223+
return nil, 'truncated'
224+
end
225+
226+
return sub(buf, pos + 1, pos + slen), pos + slen + 1
227+
end
228+
229+
213230
local function _decode_name(buf, pos)
214231
local labels = {}
215232
local nptrs = 0
@@ -484,6 +501,50 @@ local function parse_section(answers, section, buf, start_pos, size,
484501

485502
pos = p
486503

504+
elseif typ == TYPE_NAPTR then
505+
if len < 7 then
506+
return nil, "bad NAPTR record value length: " .. len
507+
end
508+
509+
local order_hi = byte(buf, pos)
510+
local order_lo = byte(buf, pos + 1)
511+
ans.order = lshift(order_hi, 8) + order_lo
512+
513+
local preference_hi = byte(buf, pos + 2)
514+
local preference_lo = byte(buf, pos + 3)
515+
ans.preference = lshift(preference_hi, 8) + preference_lo
516+
517+
local flags_str, p = _decode_string(buf, pos + 4)
518+
if not flags_str then
519+
return nil, pos
520+
end
521+
ans.flags = flags_str
522+
523+
local services_str, p = _decode_string(buf, p)
524+
if not services_str then
525+
return nil, pos
526+
end
527+
ans.services = services_str
528+
529+
local regexp_str, p = _decode_string(buf, p)
530+
if not regexp_str then
531+
return nil, pos
532+
end
533+
ans.regexp = regexp_str
534+
535+
local replacements_str,p = _decode_name(buf, p)
536+
if not replacements_str
537+
then return nil, pos
538+
end
539+
ans.replacements = replacements_str
540+
541+
if p - pos ~= len then
542+
return nil, format("bad NAPTR record length: %d ~= %d",
543+
p - pos, len)
544+
end
545+
546+
pos = p
547+
487548
elseif typ == TYPE_NS then
488549

489550
local name, p = _decode_name(buf, pos)

0 commit comments

Comments
 (0)