Skip to content
Browse files

feature: added support for PTR queries and records.

  • Loading branch information...
1 parent d40f290 commit e984a632efae15229ac14080d5872f555885207f @agentzh agentzh committed
Showing with 64 additions and 7 deletions.
  1. +13 −7 README.markdown
  2. +19 −0 lib/resty/dns/resolver.lua
  3. +32 −0 t/sanity.t
View
20 README.markdown
@@ -113,6 +113,8 @@ which usually takes some of the following fields:
The raw resource data (RDATA) for resource records that are not recognized.
* `txt`
The record value for `TXT` records.
+* `ptrdname`
+ The record value for `PTR` records.
This method also takes an optional `options` argument table, which takes the following fields:
@@ -179,20 +181,25 @@ TYPE_A
The `A` resource record type, equal to the decimal number `1`.
+TYPE_NS
+-------
+
+The `NS` resource record type, equal to the decimal number `2`.
+
TYPE_CNAME
----------
The `CNAME` resource record type, equal to the decimal number `5`.
-TYPE_MX
--------
+TYPE_PTR
+--------
-The `MX` resource record type, equal to the decimal number `15`.
+The `PTR` resource record type, equal to the decimal number `12`.
-TYPE_NS
+TYPE_MX
-------
-The `NS` resource record type, equal to the decimal number `2`.
+The `MX` resource record type, equal to the decimal number `15`.
TYPE_TXT
--------
@@ -229,8 +236,7 @@ TODO
====
* Concurrent (or parallel) query mode
-* Better support for other resource record types like `PTR`.
-* Support for the DNS inverse queries
+* Better support for other resource record types like `SPF`.
Author
======
View
19 lib/resty/dns/resolver.lua
@@ -34,6 +34,7 @@ local DEBUG = ngx.DEBUG
TYPE_A = 1
TYPE_NS = 2
TYPE_CNAME = 5
+TYPE_PTR = 12
TYPE_MX = 15
TYPE_TXT = 16
TYPE_AAAA = 28
@@ -515,6 +516,24 @@ local function parse_response(buf, id)
ans.txt = substr(buf, pos, pos + len - 1)
pos = pos + len
+ elseif typ == TYPE_PTR then
+
+ local name, p = decode_name(buf, pos)
+ if not name then
+ return nil, pos
+ end
+
+ if p - pos ~= len then
+ return nil, format("bad cname record length: %d ~= %d",
+ p - pos, len)
+ end
+
+ pos = p
+
+ -- print("name: ", name)
+
+ ans.ptrdname = name
+
else
-- for unknown types, just forward the raw value
View
32 t/sanity.t
@@ -306,3 +306,35 @@ GET /t
--- no_error_log
[error]
+
+
+=== TEST 10: PTR query
+--- http_config eval: $::HttpConfig
+--- config
+ location /t {
+ content_by_lua '
+ local resolver = require "resty.dns.resolver"
+
+ local r, err = resolver:new{ nameservers = { "$TEST_NGINX_RESOLVER" } }
+ if not r then
+ ngx.say("failed to instantiate resolver: ", err)
+ return
+ end
+
+ local ans, err = r:query("4.4.8.8.in-addr.arpa", { qtype = r.TYPE_PTR })
+ if not ans then
+ ngx.say("failed to query: ", err)
+ return
+ end
+
+ local cjson = require "cjson"
+ ngx.say("records: ", cjson.encode(ans))
+ ';
+ }
+--- request
+GET /t
+--- response_body_like chop
+^records: \[\{.*?"ptrdname":"google-public-dns-b\.google\.com".*?\}\]$
+--- no_error_log
+[error]
+

0 comments on commit e984a63

Please sign in to comment.
Something went wrong with that request. Please try again.