aboutsummaryrefslogtreecommitdiffstats
path: root/testing/lua-resty-dns/add-naptr.patch
blob: 6a2294a0d46eabcf96b9f8437c1fca3da30fb0aa (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
From 4a2b0606cf3757ef98413b8986602b60a8913890 Mon Sep 17 00:00:00 2001
From: Sergey Lukin <sergey.lukin@inbox.lv>
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)