From 4a2b0606cf3757ef98413b8986602b60a8913890 Mon Sep 17 00:00:00 2001 From: Sergey Lukin Date: Fri, 5 Aug 2016 12:09:11 +0300 Subject: [PATCH] add NAPTR record support --- README.markdown | 11 +++++++++ lib/resty/dns/resolver.lua | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/README.markdown b/README.markdown index 2cf852e..7fb2974 100644 --- a/README.markdown +++ b/README.markdown @@ -28,6 +28,7 @@ Table of Contents * [TYPE_TXT](#type_txt) * [TYPE_AAAA](#type_aaaa) * [TYPE_SRV](#type_srv) + * [TYPE_NAPTR](#type_naptr) * [TYPE_SPF](#type_spf) * [CLASS_IN](#class_in) * [SECTION_AN](#section_an) @@ -373,6 +374,16 @@ See RFC 2782 for details. [Back to TOC](#table-of-contents) +TYPE_NAPTR +---------- +`syntax: typ = r.TYPE_NAPTR` + +The `NAPTR` resource record type, equal to the decimal number `35`. + +See RFC 2915 for details. + +[Back to TOC](#table-of-contents) + TYPE_SPF --------- `syntax: typ = r.TYPE_SPF` diff --git a/lib/resty/dns/resolver.lua b/lib/resty/dns/resolver.lua index 7612b52..6cb6fda 100644 --- a/lib/resty/dns/resolver.lua +++ b/lib/resty/dns/resolver.lua @@ -48,6 +48,7 @@ local TYPE_MX = 15 local TYPE_TXT = 16 local TYPE_AAAA = 28 local TYPE_SRV = 33 +local TYPE_NAPTR = 35 local TYPE_SPF = 99 local CLASS_IN = 1 @@ -67,6 +68,7 @@ local _M = { TYPE_TXT = TYPE_TXT, TYPE_AAAA = TYPE_AAAA, TYPE_SRV = TYPE_SRV, + TYPE_NAPTR = TYPE_NAPTR, TYPE_SPF = TYPE_SPF, CLASS_IN = CLASS_IN, SECTION_AN = SECTION_AN, @@ -210,6 +212,21 @@ local function _encode_name(s) end +local function _decode_string(buf, pos) + local slen = byte(buf, pos) + + if slen == 0 then + return "", pos + 1 + end + + if pos + 1 + slen >= #buf then + return nil, 'truncated' + end + + return sub(buf, pos + 1, pos + slen), pos + slen + 1 +end + + local function _decode_name(buf, pos) local labels = {} local nptrs = 0 @@ -484,6 +501,50 @@ local function parse_section(answers, section, buf, start_pos, size, pos = p + elseif typ == TYPE_NAPTR then + if len < 7 then + return nil, "bad NAPTR record value length: " .. len + end + + local order_hi = byte(buf, pos) + local order_lo = byte(buf, pos + 1) + ans.order = lshift(order_hi, 8) + order_lo + + local preference_hi = byte(buf, pos + 2) + local preference_lo = byte(buf, pos + 3) + ans.preference = lshift(preference_hi, 8) + preference_lo + + local flags_str, p = _decode_string(buf, pos + 4) + if not flags_str then + return nil, pos + end + ans.flags = flags_str + + local services_str, p = _decode_string(buf, p) + if not services_str then + return nil, pos + end + ans.services = services_str + + local regexp_str, p = _decode_string(buf, p) + if not regexp_str then + return nil, pos + end + ans.regexp = regexp_str + + local replacements_str,p = _decode_name(buf, p) + if not replacements_str + then return nil, pos + end + ans.replacements = replacements_str + + if p - pos ~= len then + return nil, format("bad NAPTR record length: %d ~= %d", + p - pos, len) + end + + pos = p + elseif typ == TYPE_NS then local name, p = _decode_name(buf, pos)