aboutsummaryrefslogtreecommitdiffstats
path: root/testing/lua-resty-dns/add-naptr.patch
blob: a3bb991698eaf6aff5524c8b297ec36fa4221481 (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
From 5dcc817d96352b05f7468d058c39be3e6616d037 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

---
 lib/resty/dns/resolver.lua | 49 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/lib/resty/dns/resolver.lua b/lib/resty/dns/resolver.lua
index 7612b52..989d225 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,
@@ -209,6 +211,16 @@ local function _encode_name(s)
     return char(#s) .. 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 = {}
@@ -484,6 +496,43 @@ 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 hi = byte(buf, pos)
+            local lo = byte(buf, pos + 1)
+            ans.order = lshift(hi, 8) + lo
+
+            hi = byte(buf, pos + 2)
+            lo = byte(buf, pos + 3)
+            ans.preference = lshift(hi, 8) + lo
+
+            local str, p = _decode_string(buf, pos + 4)
+            if not str then return nil, pos end
+            ans.flags = str
+
+            str, p = _decode_string(buf, p)
+            if not str then return nil, pos end
+            ans.services = str
+
+            str, p = _decode_string(buf, p)
+            if not str then return nil, pos end
+            ans.regexp = str
+
+            if p - pos < len then
+                str,p = _decode_name(buf, p)
+                if not str then return nil, pos end
+                ans.replacements = str
+            end
+
+            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)