=encoding utf-8 =head1 Name lua-resty-dns - Lua DNS resolver for the ngx_lua based on the cosocket API This library is considered production ready. =head1 Description This Lua library provies a DNS resolver for the ngx_lua nginx module: https://github.com/openresty/lua-nginx-module/#readme This Lua library takes advantage of ngx_lua's cosocket API, which ensures 100% nonblocking behavior. Note that at least L or L is required. Also, the L is also required. If you're using LuaJIT 2.0 with ngx_lua, then the C library is already available by default. Note that, this library is bundled and enabled by default in the L. =head1 Synopsis lua_package_path "/path/to/lua-resty-dns/lib/?.lua;;"; server { location = /dns { content_by_lua_block { local resolver = require "resty.dns.resolver" local r, err = resolver:new{ nameservers = {"8.8.8.8", {"8.8.4.4", 53} }, retrans = 5, -- 5 retransmissions on receive timeout timeout = 2000, -- 2 sec } if not r then ngx.say("failed to instantiate the resolver: ", err) return end local answers, err = r:query("www.google.com") if not answers then ngx.say("failed to query the DNS server: ", err) return end if answers.errcode then ngx.say("server returned error code: ", answers.errcode, ": ", answers.errstr) end for i, ans in ipairs(answers) do ngx.say(ans.name, " ", ans.address or ans.cname, " type:", ans.type, " class:", ans.class, " ttl:", ans.ttl) end } } } =head1 Methods =head2 new C Creates a dns.resolver object. Returns C and an message string on error. It accepts a C table argument. The following options are supported: =over =item * C =back a list of nameservers to be used. Each nameserver entry can be either a single hostname string or a table holding both the hostname string and the port number. The nameserver is picked up by a simple round-robin algorithm for each C method call. This option is required. =over =item * C =back the total number of times of retransmitting the DNS request when receiving a DNS response times out according to the C setting. Defaults to C<5> times. When trying to retransmit the query, the next nameserver according to the round-robin algorithm will be picked up. =over =item * C =back the time in milliseconds for waiting for the respond for a single attempt of request transmition. note that this is ''not'' the maximal total waiting time before giving up, the maximal total waiting time can be calculated by the expression C. The C setting can also be changed by calling the C method. The default C setting is 2000 milliseconds, or 2 seconds. =over =item * C =back a boolean flag controls whether to disable the "recursion desired" (RD) flag in the UDP request. Defaults to C. =head2 query C Performs a DNS standard query to the nameservers specified by the C method, and returns all the answer records in an array-like Lua table. In case of errors, it will return C and a string describing the error instead. If the server returns a non-zero error code, the fields C and C will be set accordingly in the Lua table returned. Each entry in the C returned table value is also a hash-like Lua table which usually takes some of the following fields: =over =item * C =back The resource record name. =over =item * C =back The current resource record type, possible values are C<1> (C), C<5> (C), C<28> (C), and any other values allowed by RFC 1035. =over =item * C
=back The IPv4 or IPv6 address in their textual representations when the resource record type is either C<1> (C) or C<28> (C), respectively. Secussesive 16-bit zero groups in IPv6 addresses will not be compressed by default, if you want that, you need to call the C static method instead. =over =item * C
=back The identifier of the section that the current answer record belongs to. Possible values are C<1> (C), C<2> (C), and C<3> (C). =over =item * C =back The (decoded) record data value for C resource records. Only present for C records. =over =item * C =back The time-to-live (TTL) value in seconds for the current resource record. =over =item * C =back The current resource record class, possible values are C<1> (C) or any other values allowed by RFC 1035. =over =item * C =back The preference integer number for C resource records. Only present for C type records. =over =item * C =back The exchange domain name for C resource records. Only present for C type records. =over =item * C =back A domain-name which specifies a host which should be authoritative for the specified class and domain. Usually present for C type records. =over =item * C =back The raw resource data (RDATA) for resource records that are not recognized. =over =item * C =back The record value for C records. When there is only one character string in this record, then this field takes a single Lua string. Otherwise this field takes a Lua table holding all the strings. =over =item * C =back The record value for C records. This method also takes an optional C argument table, which takes the following fields: =over =item * C =back The type of the question. Possible values are C<1> (C), C<5> (C), C<28> (C), or any other QTYPE value specified by RFC 1035 and RFC 3596. Default to C<1> (C). =over =item * C =back When set to a true value, the C return value includes the C section of the DNS response. Default to C. =over =item * C =back When set to a true value, the C return value includes the C section of the DNS response. Default to C. When data truncation happens, the resolver will automatically retry using the TCP transport mode to query the current nameserver. All TCP connections are short lived. =head2 tcp_query C Just like the C method, but enforce the TCP transport mode instead of UDP. All TCP connections are short lived. Here is an example: local resolver = require "resty.dns.resolver" local r, err = resolver:new{ nameservers = { "8.8.8.8" } } if not r then ngx.say("failed to instantiate resolver: ", err) return end local ans, err = r:tcp_query("www.google.com", { qtype = r.TYPE_A }) if not ans then ngx.say("failed to query: ", err) return end local cjson = require "cjson" ngx.say("records: ", cjson.encode(ans)) =head2 set_timeout C Overrides the current C setting by the C