├── .busted
├── .editorconfig
├── .gitignore
├── .luacheckrc
├── .travis.yml
├── LICENSE
├── README.md
├── config.ld
├── docs
├── index.html
├── ldoc.css
├── modules
│ ├── resty.dns.balancer.base.html
│ ├── resty.dns.balancer.consistent_hashing.html
│ ├── resty.dns.balancer.handle.html
│ ├── resty.dns.balancer.least_connections.html
│ ├── resty.dns.balancer.round_robin.html
│ ├── resty.dns.client.html
│ └── resty.dns.utils.html
└── topics
│ └── README.md.html
├── examples
├── client.lua
└── utils.lua
├── extra
├── README.md
└── clientlog.lua
├── lua-resty-dns-client-6.0.2-1.rockspec
├── rbusted
├── spec
├── balancer
│ ├── base_spec.lua
│ ├── consistent_hashing_spec.lua
│ ├── generic_spec.lua
│ ├── handle_spec.lua
│ ├── least_connections_spec.lua
│ └── round_robin_spec.lua
├── client_cache_spec.lua
├── client_spec.lua
├── resty-runner.lua
├── test_helpers.lua
└── utils_spec.lua
├── src
└── resty
│ └── dns
│ ├── balancer
│ ├── base.lua
│ ├── consistent_hashing.lua
│ ├── handle.lua
│ ├── least_connections.lua
│ └── round_robin.lua
│ ├── client.lua
│ └── utils.lua
└── t
├── 00-sanity.t
├── 01-phases.t
└── 02-timer-usage.t
/.busted:
--------------------------------------------------------------------------------
1 | return {
2 | default = {
3 | lua = "spec/resty-runner.lua",
4 | verbose = true,
5 | coverage = false,
6 | output = "gtest",
7 | },
8 | }
9 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | end_of_line = lf
5 | insert_final_newline = true
6 | trim_trailing_whitespace = true
7 | charset = utf-8
8 |
9 | [*.lua]
10 | indent_style = space
11 | indent_size = 2
12 |
13 | [Makefile]
14 | indent_style = tab
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_store
2 | *.rock
3 | t/servroot/
4 |
--------------------------------------------------------------------------------
/.luacheckrc:
--------------------------------------------------------------------------------
1 | std = "ngx_lua"
2 | unused_args = false
3 | redefined = false
4 | max_line_length = false
5 |
6 |
7 | globals = {
8 | --"_KONG",
9 | --"kong",
10 | --"ngx.IS_CLI",
11 | }
12 |
13 |
14 | not_globals = {
15 | "string.len",
16 | "table.getn",
17 | }
18 |
19 |
20 | ignore = {
21 | --"6.", -- ignore whitespace warnings
22 | }
23 |
24 |
25 | exclude_files = {
26 | --"spec/fixtures/invalid-module.lua",
27 | --"spec-old-api/fixtures/invalid-module.lua",
28 | }
29 |
30 |
31 | --files["kong/plugins/ldap-auth/*.lua"] = {
32 | -- read_globals = {
33 | -- "bit.mod",
34 | -- "string.pack",
35 | -- "string.unpack",
36 | -- },
37 | --}
38 |
39 |
40 | files["spec/**/*.lua"] = {
41 | std = "ngx_lua+busted",
42 | }
43 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: required
2 |
3 | language: c
4 |
5 | compiler: gcc
6 |
7 | notifications:
8 | email: false
9 |
10 | cache:
11 | directories:
12 | - download-cache
13 | - perl5
14 |
15 | env:
16 | global:
17 | - JOBS=2
18 | - LUAROCKS_VER=3.3.0
19 | matrix:
20 | - OPENRESTY_VER=1.15.8.3
21 | - OPENRESTY_VER=1.17.8.2
22 |
23 | install:
24 | - mkdir -p download-cache
25 | - if [ -z "$OPENRESTY_VER" ]; then export OPENRESTY_VER=1.15.8.3; fi
26 | - if [ ! -f download-cache/openresty-$OPENRESTY_VER.tar.gz ]; then wget -O download-cache/openresty-$OPENRESTY_VER.tar.gz http://openresty.org/download/openresty-$OPENRESTY_VER.tar.gz; fi
27 | - if [ ! -f download-cache/luarocks-$LUAROCKS_VER.tar.gz ]; then wget -O download-cache/luarocks-$LUAROCKS_VER.tar.gz https://luarocks.github.io/luarocks/releases/luarocks-$LUAROCKS_VER.tar.gz; fi
28 | - if [ ! -f download-cache/cpanm ]; then wget -O download-cache/cpanm https://cpanmin.us/; fi
29 | - chmod +x download-cache/cpanm
30 | - download-cache/cpanm --notest Test::Nginx >build.log 2>&1 || (cat build.log && exit 1)
31 | - download-cache/cpanm --notest --local-lib=$TRAVIS_BUILD_DIR/perl5 local::lib && eval $(perl -I $TRAVIS_BUILD_DIR/perl5/lib/perl5/ -Mlocal::lib)
32 | - tar -zxf download-cache/openresty-$OPENRESTY_VER.tar.gz
33 | - tar -zxf download-cache/luarocks-$LUAROCKS_VER.tar.gz
34 | - pushd openresty-$OPENRESTY_VER
35 | - export OPENRESTY_PREFIX=$TRAVIS_BUILD_DIR/openresty-$OPENRESTY_VER
36 | - ./configure --prefix=$OPENRESTY_PREFIX --without-http_ssl_module -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
37 | - make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
38 | - make install > build.log 2>&1 || (cat build.log && exit 1)
39 | - popd
40 | - pushd luarocks-$LUAROCKS_VER
41 | - export LUAROCKS_PREFIX=$TRAVIS_BUILD_DIR/luarocks-$LUAROCKS_VER
42 | - ./configure --with-lua=$OPENRESTY_PREFIX/luajit --with-lua-include=$OPENRESTY_PREFIX/luajit/include/luajit-2.1 --lua-suffix=jit
43 | - make build
44 | - sudo make install
45 | - popd
46 | - export PATH=$OPENRESTY_PREFIX/nginx/sbin:$OPENRESTY_PREFIX/bin:$LUAROCKS_PREFIX/bin:$PATH
47 | - sudo luarocks install luacheck > build.log 2>&1 || (cat build.log && exit 1)
48 | - sudo luarocks install busted > build.log 2>&1 || (cat build.log && exit 1)
49 | - sudo luarocks make
50 | - luarocks --version
51 | - nginx -V
52 |
53 | script:
54 | - luacheck ./src
55 | - busted
56 | - TEST_NGINX_RANDOMIZE=1 prove -v -r t
57 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright 2016-2022 Kong Inc.
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Overview
2 | ========
3 |
4 | Lua library containing a dns client, several utilities, and a load-balancer.
5 |
6 | The module is currently OpenResty only, and builds on top of the
7 | [`lua-resty-dns`](https://github.com/openresty/lua-resty-dns) library
8 |
9 | Features
10 | ========
11 |
12 | - resolves A, AAAA, CNAME and SRV records, including port
13 | - parses `/etc/hosts`
14 | - parses `/resolv.conf` and applies `LOCALDOMAIN` and `RES_OPTIONS` variables
15 | - caches dns query results in memory
16 | - synchronizes requests (a single request for many requestors, eg. when cached ttl expires under heavy load)
17 | - `toip` applies a local (weighted) round-robin scheme on the query results
18 | - (weighted) round-robin balancer
19 | - consistent-hashing balancer
20 | - least-connections balancer
21 |
22 |
23 | Copyright and license
24 | =====================
25 |
26 | Copyright: (c) 2016-2021 Kong, Inc.
27 |
28 | Author: Thijs Schreijer
29 |
30 | License: [Apache 2.0](https://opensource.org/licenses/Apache-2.0)
31 |
32 | Testing
33 | =======
34 |
35 | Tests are executed using `busted`, but because they run inside the `resty` cli tool, you must
36 | use the `rbusted` script.
37 |
38 | For troubleshooting purposes: see the `/extra` folder for how to parse logs
39 |
40 | History
41 | =======
42 |
43 | Versioning is strictly based on [Semantic Versioning](https://semver.org/)
44 |
45 | Release process:
46 |
47 | 1. update the changelog below
48 | 2. update the rockspec file
49 | 3. generate the docs using `ldoc .`
50 | 4. commit and tag the release
51 | 5. upload rock to LuaRocks
52 |
53 | ### 6.0.2 (05-Jul-2021)
54 |
55 | - Fix: `validTtl` should not be used for host-file entries.
56 | [PR 134](https://github.com/Kong/lua-resty-dns-client/pull/134)
57 |
58 | ### 6.0.1 (22-Jun-2021)
59 |
60 | - Performance: reduce amount of timers on init_worker. [PR 130](https://github.com/Kong/lua-resty-dns-client/pull/130)
61 |
62 | ### 6.0.0 (05-Apr-2021)
63 |
64 | - BREAKING: the round-robin balancing algorithm is now implemented in
65 | `resty.dns.round_robin` and has no consistent-hashing algorithm features,
66 | which are now all restricted to `resty.dns.consistent_hashing`.
67 | [PR 123](https://github.com/Kong/lua-resty-dns-client/pull/123)
68 | - Added: option to enable or disable nameservers randomization. Note: this
69 | feature depends on
70 | [the to-be-released feature](https://github.com/openresty/lua-resty-dns/commit/ad4a51c8cae8c3fb8f712fa91fda660ab8a89669)
71 | in [lua-resty-dns](https://github.com/openresty/lua-resty-dns).
72 | [PR 119](https://github.com/Kong/lua-resty-dns-client/pull/119)
73 |
74 |
75 | ### 5.2.3 (19-Mar-2021)
76 |
77 | - Fix: potential synchronisation issue in the least-connections balancer.
78 | [PR 126](https://github.com/Kong/lua-resty-dns-client/pull/126)
79 |
80 | ### 5.2.2 (11-Mar-2021)
81 |
82 | - Fix: do not iterate over all the search domains when resolving an unambiguous
83 | fully-qualified domain name (FQDN), i.e. ended in a dot.
84 | [PR 122](https://github.com/Kong/lua-resty-dns-client/pull/122)
85 |
86 | ### 5.2.1 (21-Jan-2021)
87 |
88 | - Fix: balancer DNS updates could go into a busy loop upon renewal. Reported as
89 | [Kong issue #6739](https://github.com/Kong/kong/issues/6739),
90 | fixed with [PR 116](https://github.com/Kong/lua-resty-dns-client/pull/116).
91 |
92 | ### 5.2.0 (7-Jan-2021)
93 |
94 | - Fix: now a single timer is used to check for expired records instead of one
95 | per host, significantly reducing the number of resources required for DNS
96 | resolution. [PR 112](https://github.com/Kong/lua-resty-dns-client/pull/112)
97 |
98 | ### 5.1.1 (7-Oct-2020)
99 |
100 | - Dependency: Bump lua-resty-timer to 1.0
101 |
102 | ### 5.1.0 (28-Sep-2020)
103 |
104 | - Fix: workaround for LuaJIT/ARM bug, see [Issue 93](https://github.com/Kong/lua-resty-dns-client/issues/93).
105 | - Fix: table reduction was calculated wrong. Not a "functional" bug, just causing
106 | slightly less agressive memory releasing.
107 | - Added: alternative implementation of the consistent-hashing balancing algorithm,
108 | which does not rely on the addresses addition and removal order to build the
109 | same request distribution among different instances. See
110 | [PR 97](https://github.com/Kong/lua-resty-dns-client/pull/97).
111 |
112 | ### 5.0.0 (14-May-2020)
113 |
114 | - BREAKING: `getPeer` now returns the host-header value instead of the hostname
115 | that was used to add the address. This is only breaking if a host was added through
116 | `addHost` with an ip-address. In that case `getPeer` will no longer return the
117 | ip-address as the hostname, but will now return `nil`. See
118 | [PR 89](https://github.com/Kong/lua-resty-dns-client/pull/89).
119 | - Added: option `useSRVname`, if truthy then `getPeer` will return the name as found
120 | in the SRV record, instead of the hostname as added to the balancer.
121 | See [PR 89](https://github.com/Kong/lua-resty-dns-client/pull/89).
122 | - Added: callback return an extra parameter; the host-header for the address added/removed.
123 | - Fix: using the module instance instead of the passed one for dns resolution
124 | in the balancer (only affected testing). See [PR 88](https://github.com/Kong/lua-resty-dns-client/pull/88).
125 |
126 | ### 4.2.0 (23-Mar-2020)
127 |
128 | - Change: export DNS source type on status report. See [PR 86](https://github.com/Kong/lua-resty-dns-client/pull/86).
129 |
130 | ### 4.1.3 (24-Jan-2020)
131 |
132 | - Fix: fix ttl-0 records issues with the balancer, see Kong issue
133 | https://github.com/Kong/kong/issues/5477
134 | * the previous record was not properly detected as a ttl=0 record
135 | by checking on the `__ttl0flag` we now do
136 | * since the "fake" SRV record wasn't updated with a new expiry
137 | time the expiry-check-timer would keep updating that record
138 | every second
139 |
140 | ### 4.1.2 (10-Dec-2019)
141 |
142 | - Fix: handle cases when `lastQuery` is `nil`, see [PR 81](https://github.com/Kong/lua-resty-dns-client/pull/81)
143 | and [PR 82](https://github.com/Kong/lua-resty-dns-client/pull/82).
144 |
145 | ### 4.1.1 (14-Nov-2019)
146 |
147 | - Fix: added logging of try-list to the TCP/UDP wrappers, see [PR 75](https://github.com/Kong/lua-resty-dns-client/pull/75).
148 | - Fix: reduce logging noise of the requery timer
149 |
150 | ### 4.1.0 (7-Aug-2019)
151 |
152 | - Fix: unhealthy balancers would not recover because they would not refresh the
153 | DNS records used. See [PR 73](https://github.com/Kong/lua-resty-dns-client/pull/73).
154 | - Added: automatic background resolving of hostnames, expiry will be checked
155 | every second, and if needed DNS (and balancer) will be updated. See [PR 73](https://github.com/Kong/lua-resty-dns-client/pull/73).
156 |
157 | ### 4.0.0 (26-Jun-2019)
158 |
159 | - BREAKING: the balancer callback is called with a new event; "health" whenever
160 | the health status of the balancer changes.
161 | - BREAKING: renamed `setPeerStatus` to `setAddressStatus` to be in line with the
162 | new `setHostStatus`, and prevent confusion.
163 | - Added: keep track of unavailable weight. Added the `getStatus` method to
164 | return health, of the entire balancer structure. Health itself is determined
165 | based on the new property `healthThreshold`.
166 | - Added: prevention of cascading failures when balancer is unhealthy. Use the
167 | `healthThreshold` value to set when the balancer is considered unhealthy.
168 | - Added: method `setHostStatus`, to set the availability/health state of all
169 | addresses belonging to a host at once.
170 | - Fix: when an asyncquery failed to create the timer, it would silently ignore
171 | the error. Error is now being logged.
172 |
173 | ### 3.0.2 (8-Mar-2019) Bugfix
174 |
175 | - Fix: callback for adding an address did not pass the address object, but
176 | instead passed the balancer object twice.
177 |
178 | ### 3.0.1 (5-Mar-2019) Bugfix
179 |
180 | - Fix: "balancer is nil" error, see issue #49.
181 |
182 | ### 3.0.0 (7-Nov-2018) Refactor & least-connections balancer
183 |
184 | - Refactor: split the balancer in a base class (handling DNS resolution) and
185 | the ring-balancer, implementing the algorithm.
186 | - Added: new least-connections balancer
187 | - Fix: since addresses could occasionally hold names instead of IP addresses,
188 | it could happen that a call to `setPeerStatus` was unsuccessful, because the
189 | IP address would not match the name in the `address` object. Now a
190 | `handle` is returned by `getPeer`.
191 | - BREAKING: `getPeer` signature (and return values) changed, making this a
192 | breaking change.
193 |
194 | ### 2.2.0 (28-Aug-2018) Fixes and a new option
195 |
196 | - Added: a new option `validTtl` that, if set, will forcefully override the
197 | `ttl` value of any valid answer received. [Issue 48](https://github.com/Kong/lua-resty-dns-client/issues/48).
198 | - Fix: remove multiline log entries, now encoded as single-line json. [Issue 52](https://github.com/Kong/lua-resty-dns-client/issues/52).
199 | - Fix: always inject a `localhost` value, even if not in `/etc/hosts`. [Issue 54](https://github.com/Kong/lua-resty-dns-client/issues/54).
200 | - Fix: added a workaround for Amazon Route 53 nameservers replying with a
201 | `ttl=0` whilst the record has a non-0 ttl. [Issue 56](https://github.com/Kong/lua-resty-dns-client/issues/56).
202 |
203 | ### 2.1.0 (21-May-2018) Fixes
204 |
205 | - Fix: the round-robin scheme for the balancer starts at a randomized position
206 | to prevent all workers from starting with the same peer.
207 | - Fix: the balancer no longer returns `port = 0` for SRV records without a
208 | port, the default port is now returned.
209 | - Fix: ipv6 nameservers with a scope in their address are not supported. This
210 | fix will simply skip them instead of throwing errors upon resolving. Fixes
211 | [issue 43](https://github.com/Kong/lua-resty-dns-client/issues/43).
212 | - Minor: improved logging in the balancer
213 | - Minor: relax requery default interval for failed dns queries from 1 to 30
214 | seconds.
215 |
216 | ### 2.0.0 (22-Feb-2018) Major performance improvement (balancer) and bugfixes
217 |
218 | - BREAKING: improved performance and memory footprint for large balancers.
219 | 80-85% less memory will be used, while creation time dropped by 85-90%. Since
220 | the `host:getPeer()` function signature changed, this is a breaking change.
221 | - Change: BREAKING the errors for cache-only lookup failures and empty records
222 | have been changed.
223 | - Fix: do not fail initialization without nameservers.
224 | - Fix: properly recognize IPv6 in square brackets from the /etc/hosts file.
225 | - Fix: do not set success-type to types we're not looking for. Fixes
226 | [Kong issue #3210](https://github.com/Kong/kong/issues/3210).
227 | - Fix: store records from the additional section in cache
228 | - Fix: do not overwrite stale data in the client cache with empty records
229 |
230 | ### 1.0.0 (14-Dec-2017) Fixes and IPv6
231 |
232 | - Change: BREAKING all IPv6 addresses are now returned with square brackets
233 | - Fix: properly recognize IPv6 addresses in square brackets
234 |
235 | ### 0.6.3 (27-Nov-2017) Fixes and flagging unhealthy peers
236 |
237 | - Added: flag to mark an address as failed/unhealthy, see `setPeerStatus`
238 | - Added: callback to receive balancer updates; addresses added-to/removed-from
239 | the balancer (after DNS updates for example).
240 | - fix: SRV record entries with a weight 0 are now supported
241 | - fix: failure of the last hostname to resolve (balancer)
242 |
243 | ### 0.6.2 (04-Sep-2017) Fixes and refactor
244 |
245 | - Fix: balancer not returning hostname for named SRV entries. See
246 | [issue #17](https://github.com/Kong/lua-resty-dns-client/issues/17)
247 | - Fix: fix an occasionally failing test
248 | - Refactor: remove metadata from the records, instead store it in its own cache
249 |
250 | ### 0.6.1 (28-Jul-2017) Randomization adjusted
251 |
252 | - Change: use a different randomizer for the ring-balancer to predictably
253 | recreate the balancer in the exact same state (adds the `lrandom` library as
254 | a new dependency)
255 |
256 | ### 0.6.0 (14-Jun-2017) Rewritten resolver core to resolve async
257 |
258 | - Added: resolution will be done async whenever possible. For this to work a new
259 | setting has been introduced `staleTtl` which determines for how long stale
260 | records will returned while a query is in progress in the background.
261 | - Change: BREAKING! several functions that previously returned and took a
262 | resolver object no longer do so.
263 | - Fix: no longer lookup ip adresses as names if the query type is not A or AAAA
264 | - Fix: normalize names to lowercase after query
265 | - Fix: set last-success types for hosts-file entries and ip-addresses
266 |
267 | ### 0.5.0 (25-Apr-2017) implement SEARCH and NDOTS
268 |
269 | - Removed: BREAKING! stdError function removed.
270 | - Added: implemented the `search` and `ndots` options.
271 | - Change: `resolve` no longer returns empty results or dns errors as a table
272 | but as lua errors (`nil + error`).
273 | - Change: `toip()` and `resolve()` have an extra result; history. A table with
274 | the list of tried names/types/results.
275 | - Fix: timeout and retrans options from `resolv.conf` were ignored by the
276 | `client` module.
277 | - Fix: nameservers with an ipv6 address would not be used properly. Also
278 | added a flag `enable_ipv6` (default == `false`) to enable the usage of
279 | ipv6 nameservers.
280 |
281 | ### 0.4.1 (21-Apr-2017) Bugfix
282 |
283 | - Fix: cname record caching causing excessive dns queries,
284 | see [Kong issue #2303](https://github.com/Kong/kong/issues/2303).
285 |
286 | ### 0.4.0 (30-Mar-2017) Bugfixes
287 |
288 | - Change: BREAKING! modified hash treatment, must now be an integer > 0
289 | - Added: BREAKING! a retry counter to fall-through on hashed-retries (changes
290 | the `getpeer` signature)
291 | - Fix: the MAXNS (3) was not honoured, so more than 3 nameservers would be parsed
292 | from the `resolv.conf` file. Fixes [Kong issue #2290](https://github.com/Kong/kong/issues/2290).
293 | - Added: two convenience hash functions
294 | - Performance: some improvements (pre-allocated tables for the slot lists)
295 |
296 | ### 0.3.2 (6-Mar-2017) Bugfixes
297 |
298 | - Fix: Cleanup disabled addresses but did not delete them, causing errors when
299 | they were repeatedly added/removed
300 | - Fix: potential racecondition when re-querying dns records
301 | - Fix: potential memoryleak when a balancer object was released with a running timer
302 |
303 | ### 0.3.1 (22-Feb-2017) Bugfixes
304 |
305 | - Kubernetes dns returns an SRV record for individual nodes, where the target
306 | is the same name again (hence causing a recursive loop). Now those entries
307 | will be removed, and if nothing is left, it will fail the SRV lookup, causing
308 | a fall-through to the next record type.
309 | - Kubernetes tends to return a port of 0 if none is provided/set, hence the
310 | `toip()` function now ignores a `port=0` and falls back on the port passed
311 | in.
312 |
313 | ### 0.3.0 (8-Nov-2016) Major breaking update
314 |
315 | - breaking: renamed a lot of things; method names, module names, etc. pretty
316 | much breaks everything... also releasing under a new name
317 | - feature: udp function `setpeername` added (client)
318 | - fix: do not synchronize dns queries for ttl=0 requests (client)
319 | - fix: full test coverage and accompanying fixes (ring-balancer)
320 | - feature: auto-retry for failed dns queries (ring-balancer)
321 | - feature: updating weights is now supported without removing/re-adding (ring-balancer)
322 | - change: auto-retry interval configurable for failed dns queries (ring-balancer)
323 | - change: max life-time interval configurable for ttl=0 dns records (ring-balancer)
324 |
325 | ### 0.2.1 (24-Oct-2016) Bugfix
326 |
327 | - fix: `toip()` failed on SRV records with only 1 entry
328 |
329 | ### 0.2 (18-Oct-2016) Added the balancer
330 |
331 | - fix: was creating resolver objects even if serving from cache
332 | - change: change resolver order (SRV is now first by default) for dns servers that create both SRV and A records for each entry
333 | - feature: make resolver order configurable
334 | - feature: ring-balancer (experimental, no full test coverage yet)
335 | - other: more test coverage for the dns client
336 |
337 | ### 0.1 (09-Sep-2016) Initial released version
338 |
--------------------------------------------------------------------------------
/config.ld:
--------------------------------------------------------------------------------
1 | project='lua-resty-dns-client'
2 | title='DNS client for OpenResty'
3 | description='DNS client, utilities, and load balancers for OpenResty'
4 | format='discount'
5 | file='./src/'
6 | dir='docs'
7 | readme='README.md'
8 | sort=true
9 | sort_modules=true
10 | all=false
11 | style='./docs/'
12 |
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 | DNS client for OpenResty
7 |
8 |
9 |
10 |
11 |
This balancer implements a consistent-hashing algorithm based on the
62 | Ketama algorithm.
63 |
This load balancer is designed to make sure that every time a load
64 | balancer object is built, it is built the same, no matter the order the
65 | process is done.
66 |
67 |
NOTE: This documentation only described the altered user
68 | methods/properties, see the user properties from the balancer_base
69 | for a complete overview.
130 | Adds a host to the balancer.
131 | This function checks if there is enough points to add more hosts and
132 | then call the base class's addHost().
133 | see addHost() from the balancer_base for more details.
134 |
135 |
136 |
Parameters:
137 |
138 |
hostname
139 |
140 |
141 |
142 |
143 |
port
144 |
145 |
146 |
147 |
148 |
weight
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 | afterHostUpdate (host)
163 |
164 |
165 | Actually adds the addresses to the continuum.
166 | This function should not be called directly, as it will called by
167 | addHost() after adding the new host.
168 | This function makes sure the continuum will be built identically every
169 | time, no matter the order the hosts are added.
170 |
171 |
172 |
191 | Gets an IP/port/hostname combo for the value to hash
192 | This function will hash the valueToHash param and use it as an index
193 | in the continuum. It will return the address that is at the hashed
194 | value or the first one found going counter-clockwise in the continuum.
195 |
196 |
197 |
Parameters:
198 |
199 |
cacheOnly
200 | If truthy, no dns lookups will be done, only cache.
201 |
202 |
handle
203 | the handle returned by a previous call to getPeer.
204 | This will retain some state over retries. See also setAddressStatus.
205 |
206 |
valueToHash
207 | value for consistent hashing. Please note that this
208 | value will be hashed, so no need to hash it prior to calling this
209 | function.
210 |
211 |
212 |
213 |
Returns:
214 |
215 |
216 | ip + port + hostheader + handle, or nil+error
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 | new (opts)
226 |
227 |
228 |
229 |
Creates a new balancer.
230 |
231 |
The balancer is based on a wheel (continuum) with a number of points
232 | between MINCONTINUUMSIZE and MAXCONTINUUMSIZE points. Key points
233 | will be assigned to addresses based on their IP and port. The number
234 | of points each address will be assigned is proportional to their weight.
235 |
236 |
The options table has the following fields, additional to the ones from
237 | the balancer_base:
238 |
239 |
240 |
hosts (optional) containing hostnames, ports, and weights. If
241 | omitted, ports and weights default respectively to 80 and 10. The list
242 | will be sorted before being added, so the order of entry is
243 | deterministic.
244 |
wheelSize (optional) for total number of positions in the
245 | continuum. If omitted DEFAULT_CONTINUUM_SIZE is used. It is important
246 | to have enough indices to fit all addresses entries, keep in mind that
247 | each address will use 160 entries in the continuum (more or less,
248 | proportional to its weight, but the total points will always be
249 | 160 * addresses). Consider the maximum number of targets expected, as
250 | new hosts can be dynamically added, and DNS renewals might yield
251 | larger record sets. The wheelSize cannot be altered, the object has
252 | to built again to change this value. On a similar note, making it too
253 | big will have a performance impact to get peers from the continuum, as
254 | the values will be too dispersed among them.
Implements handles to be used by the objBalancer:getPeer method. These
61 | implement a __gc method for tracking statistics and not leaking resources
62 | in case a connection gets aborted prematurely.
63 |
64 |
This module is only relevant when implementing your own balancer
65 | algorithms.
66 |
Info:
67 |
68 |
Copyright: 2016-2020 Kong Inc. All rights reserved.
102 | Gets a handle from the cache.
103 | The handle comes from the cache or it is newly created. A handle is just a
104 | table. It will have two special fields:
105 |
106 |
107 |
__udata: (read-only) a userdata used to track the lifetime of the handle
108 |
__gc: (read/write) this method will be called on GC.
109 |
110 |
111 |
NOTE: the __gc will only be called when the handle is garbage collected,
112 | not when it is returned by calling release.
113 |
114 |
115 |
Parameters:
116 |
117 |
__gc
118 | (optional, function) the method called when the handle is GC'ed.
119 |
local handle = _M
133 |
134 | local my_gc_handler = function(self)
135 | print(self.name .. " was deleted")
136 | end
137 |
138 | local h1 = handle.get(my_gc_handler)
139 | h1.name = "Obama"
140 | local h2 = handle.get(my_gc_handler)
141 | h2.name = "Trump"
142 |
143 | handle.release(h1) -- explicitly release it
144 | h1 = nil
145 | h2 = nil-- not released, will be GC'ed
146 | collectgarbage()
147 | collectgarbage() --> "Trump was deleted"
148 |
149 |
150 |
151 |
152 |
153 | release (handle)
154 |
155 |
156 | Returns a handle to the cache.
157 | The handle will be cleared, returned to the cache, and its __gc handle
158 | will NOT be called.
159 |
160 |
161 |
Parameters:
162 |
163 |
handle
164 | the handle to return to the cache
165 |
This balancer implements a least-connections algorithm. The balancer will
61 | honour the weights. See the base-balancer for details on how the weights
62 | are set.
63 |
64 |
NOTE: This documentation only described the altered user methods/properties
65 | from the base-balancer. See the user properties from the balancer_base for a
66 | complete overview.
67 |
Info:
68 |
69 |
Copyright: 2016-2020 Kong Inc. All rights reserved.
Creates a new balancer. The balancer is based on a binary heap tracking
97 | the number of active connections. The number of connections
98 | assigned will be relative to the weight.
99 |
100 |
The options table has the following fields, additional to the ones from
101 | the balancer_base;
102 |
103 |
104 |
hosts (optional) containing hostnames, ports and weights. If omitted,
105 | ports and weights default respectively to 80 and 10.
-- hosts example
128 | local hosts = {
129 | "konghq.com", -- name only, as string
130 | { name = "github.com" }, -- name only, as table
131 | { name = "getkong.org", port = 80, weight = 25 }, -- fully specified, as table
132 | }
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 | generated by LDoc 1.4.6
143 | Last updated 2021-07-06 11:55:24
144 |
Works with OpenResty only. Requires the lua-resty-dns module.
65 |
66 |
NOTES:
67 |
68 |
69 |
parsing the config files upon initialization uses blocking i/o, so use with
70 | care. See init for details.
71 |
All returned records are directly from the cache. So do not modify them!
72 | If you need to, copy them first.
73 |
TTL for records is the TTL returned by the server at the time of fetching
74 | and won't be updated while the client serves the records from its cache.
75 |
resolving IPv4 (A-type) and IPv6 (AAAA-type) addresses is explicitly supported. If
76 | the hostname to be resolved is a valid IP address, it will be cached with a ttl of
77 | 10 years. So the user doesn't have to check for ip adresses.
Implements udp-setpeername method with dns resolution.
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
Resolving
120 |
121 |
122 | When resolving names, queries will be synchronized, such that only a single
123 | query will be sent. If stale data is available, the request will return
124 | stale data immediately, whilst continuing to resolve the name in the
125 | background.
126 |
127 |
The dnsCacheOnly parameter found with resolve and toip can be used in
128 | contexts where the co-socket api is unavailable. When the flag is set
129 | only cached data is returned, but it will never use blocking io.
130 |
131 |
132 |
133 |
134 | init (options)
135 |
136 |
137 | Initialize the client. Can be called multiple times. When called again it
138 | will clear the cache.
139 |
140 |
141 |
Parameters:
142 |
143 |
options
144 | Same table as the OpenResty dns resolver,
145 | with some extra fields explained in the example below.
146 |
147 |
148 |
149 |
Returns:
150 |
151 |
152 | true on success, nil+error, or throw an error on bad input
153 |
154 |
155 |
156 |
157 |
Usage:
158 |
159 |
-- config files to parse
160 | -- hosts and resolvConf can both be a filename, or a table with file-contents
161 | -- The contents of the hosts file will be inserted in the cache.
162 | -- From resolv.conf the nameserver, search, ndots, attempts and timeout values will be used.
163 | local hosts = {} -- initialize without any blocking i/o
164 | local resolvConf = {} -- initialize without any blocking i/o
165 |
166 | -- when getting nameservers from resolv.conf, get ipv6 servers?
167 | local enable_ipv6 = false
168 |
169 | -- Order in which to try different dns record types when resolving
170 | -- 'last'; will try the last previously successful type for a hostname.
171 | local order = { "last", "SRV", "A", "AAAA", "CNAME" }
172 |
173 | -- Stale ttl for how long a stale record will be served from the cache
174 | -- while a background lookup is in progress.
175 | local staleTtl = 4.0-- in seconds (can have fractions)
176 |
177 | -- Cache ttl for empty and 'name error' (3) responses
178 | local emptyTtl = 30.0-- in seconds (can have fractions)
179 |
180 | -- Cache ttl for other error responses
181 | local badTtl = 1.0-- in seconds (can have fractions)
182 |
183 | -- Overriding ttl for valid queries, if given
184 | local validTtl = nil-- in seconds (can have fractions)
185 |
186 | -- ndots, same as the resolv.conf option, if not given it is taken from
187 | -- resolv.conf or otherwise set to 1
188 | local ndots = 1
189 |
190 | -- no_random, if set disables randomly picking the first nameserver, if not
191 | -- given it is taken from resolv.conf option rotate (inverted).
192 | -- Defaults to true.
193 | local no_random = true
194 |
195 | -- search, same as the resolv.conf option, if not given it is taken from
196 | -- resolv.conf, or set to the domain option, or no search is performed
197 | local search = {
198 | "mydomain.com",
199 | "site.domain.org",
200 | }
201 |
202 | -- Disables synchronization between queries, resulting in each lookup for the
203 | -- same name being executed in it's own query to the nameservers. The default
204 | -- (false) will synchronize multiple queries for the same name to a single
205 | -- query to the nameserver.
206 | noSynchronisation = false
207 |
208 | assert(client.init({
209 | hosts = hosts,
210 | resolvConf = resolvConf,
211 | ndots = ndots,
212 | no_random = no_random,
213 | search = search,
214 | order = order,
215 | badTtl = badTtl,
216 | emptyTtl = emptTtl,
217 | staleTtl = staleTtl,
218 | validTtl = validTtl,
219 | enable_ipv6 = enable_ipv6,
220 | noSynchronisation = noSynchronisation,
221 | })
222 | )
231 | Resolve a name.
232 | If r_opts.qtype is given, then it will fetch that specific type only. If
233 | r_opts.qtype is not provided, then it will try to resolve
234 | the name using the record types, in the order as provided to init.
235 |
236 |
Note that unless explictly requesting a CNAME record (by setting r_opts.qtype) this
237 | function will dereference the CNAME records.
238 |
239 |
So requesting my.domain.com (assuming to be an AAAA record, and default order) will try to resolve
240 | it (the first time) as;
241 |
242 |
243 |
SRV,
244 |
then A,
245 |
then AAAA (success),
246 |
then CNAME (after AAAA success, this will not be tried)
247 |
248 |
249 |
A second lookup will now try (assuming the cached entry expired);
250 |
251 |
252 |
AAAA (as it was the last successful lookup),
253 |
then SRV,
254 |
then A,
255 |
then CNAME.
256 |
257 |
258 |
The outer loop will be based on the search and ndots options. Within each of
259 | those, the inner loop will be the query/record type.
260 |
261 |
262 |
Parameters:
263 |
264 |
qname
265 | Name to resolve
266 |
267 |
r_opts
268 | Options table, see remark about the qtype field above and
269 | OpenResty docs for more options.
270 |
271 |
dnsCacheOnly
272 | Only check the cache, won't do server lookups
273 |
274 |
try_list
275 | (optional) list of tries to add to
276 |
277 |
278 |
279 |
Returns:
280 |
281 |
282 | list of records + nil + try_list, or nil + err + try_list.
283 |
284 |
285 |
286 |
287 |
288 |
294 | Resolves to an IP and port number.
295 | Builds on top of resolve, but will also further dereference SRV type records.
296 |
297 |
When calling multiple times on cached records, it will apply load-balancing
298 | based on a round-robin (RR) scheme. For SRV records this will be a weighted
299 | round-robin (WRR) scheme (because of the weights it will be randomized). It will
300 | apply the round-robin schemes on each level
301 | individually.
302 |
303 |
Example;
304 |
305 |
SRV record for "my.domain.com", containing 2 entries (this is the 1st level);
306 |
307 |
308 |
target = 127.0.0.1, port = 80, weight = 10
309 |
target = "other.domain.com", port = 8080, weight = 5
310 |
311 |
312 |
A record for "other.domain.com", containing 2 entries (this is the 2nd level);
313 |
314 |
315 |
ip = 127.0.0.2
316 |
ip = 127.0.0.3
317 |
318 |
319 |
Now calling local ip, port = toip("my.domain.com", 123) in a row 6 times will result in;
320 |
321 |
322 |
127.0.0.1, 80
323 |
127.0.0.2, 8080 (port from SRV, 1st IP from A record)
324 |
127.0.0.1, 80 (completes WRR 1st level, 1st run)
325 |
127.0.0.3, 8080 (port from SRV, 2nd IP from A record, completes RR 2nd level)
326 |
127.0.0.1, 80
327 |
127.0.0.1, 80 (completes WRR 1st level, 2nd run, with different order as WRR is randomized)
328 |
329 |
330 |
Debugging:
331 |
332 |
This function both takes and returns a try_list. This is an internal object
333 | representing the entire resolution history for a call. To prevent unnecessary
334 | string concatenations on a hot code path, it is not logged in this module.
335 | If you need to log it, just log tostring(try_list) from the caller code.
336 |
337 |
338 |
Parameters:
339 |
340 |
qname
341 | hostname to resolve
342 |
343 |
port
344 | (optional) default port number to return if none was found in
345 | the lookup chain (only SRV records carry port information, SRV with port=0 will be ignored)
346 |
347 |
dnsCacheOnly
348 | Only check the cache, won't do server lookups (will
349 | not invalidate any ttl expired data and will hence possibly return expired data)
350 |
351 |
try_list
352 | (optional) list of tries to add to
353 |
354 |
355 |
356 |
Returns:
357 |
358 |
359 | ip address + port + try_list, or in case of an error nil + error + try_list
360 |
361 |
362 |
363 |
364 |
365 |
375 | Implements tcp-connect method with dns resolution.
376 | This builds on top of toip. If the name resolves to an SRV record,
377 | the port returned by the DNS server will override the one provided.
378 |
379 |
NOTE: can also be used for other connect methods, eg. http/redis
380 | clients, as long as the argument order is the same
381 |
382 |
383 |
Parameters:
384 |
385 |
sock
386 | the tcp socket
387 |
388 |
host
389 | hostname to connect to
390 |
391 |
port
392 | port to connect to (will be overridden if toip returns a port)
393 |
414 | Implements udp-setpeername method with dns resolution.
415 | This builds on top of toip. If the name resolves to an SRV record,
416 | the port returned by the DNS server will override the one provided.
417 |
418 |
419 |
Parameters:
420 |
421 |
sock
422 | the udp socket
423 |
424 |
host
425 | hostname to connect to
426 |
427 |
port
428 | port to connect to (will be overridden if toip returns a port)
429 |
144 | Default filename to parse for the hosts file.
145 |
146 |
147 |
148 |
DEFAULT_HOSTS
149 | Defaults to /etc/hosts
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 | DEFAULT_RESOLV_CONF
161 |
162 |
163 | Default filename to parse for the resolv.conf file.
164 |
165 |
166 |
167 |
DEFAULT_RESOLV_CONF
168 | Defaults to /etc/resolv.conf
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 | MAXNS
180 |
181 |
182 | Maximum number of nameservers to parse from the resolv.conf file
183 |
184 |
185 |
186 |
MAXNS
187 | Defaults to 3
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 | MAXSEARCH
199 |
200 |
201 | Maximum number of entries to parse from search parameter in the resolv.conf file
202 |
203 |
204 |
205 |
MAXSEARCH
206 | Defaults to 6
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
Parsing configuration files and variables
217 |
218 |
219 |
220 |
221 | applyEnv (config)
222 |
223 |
224 | Will parse LOCALDOMAIN and RES_OPTIONS environment variables.
225 | It will insert them into the given resolv.conf based configuration table.
226 |
227 |
NOTE: if the input is nil+error it will return the input, to allow for
228 | pass-through error handling
229 |
230 |
231 |
Parameters:
232 |
233 |
config
234 | Options table, as parsed by parseResolvConf, or an empty table to get only the environment options
235 |
-- errors are passed through, so this;
253 | local config, err = utils.parseResolvConf()
254 | if config then
255 | config, err = utils.applyEnv(config)
256 | end
257 |
258 | -- Is identical to;
259 | local config, err = utils.applyEnv(utils.parseResolvConf())
260 |
261 |
262 |
263 |
264 |
265 | parseHosts (filename)
266 |
267 |
268 | Parses a hosts file or table.
269 | Does not check for correctness of ip addresses nor hostnames. Might return
270 | nil + error if the file cannot be read.
271 |
272 |
NOTE: All output will be normalized to lowercase, IPv6 addresses will
273 | always be returned in brackets.
274 |
275 |
276 |
Parameters:
277 |
278 |
filename
279 | (optional) Filename to parse, or a table with the file
280 | contents in lines (defaults to '/etc/hosts' if omitted)
281 |
282 |
283 |
284 |
Returns:
285 |
286 |
287 | 1; reverse lookup table, ip addresses (table with ipv4 and ipv6
288 | fields) indexed by their canonical names and aliases
289 |
290 | 2; list with all entries. Containing fields ip, canonical and family,
291 | and a list of aliasses
316 | Parses a resolv.conf file or table.
317 | Does not check for correctness of ip addresses nor hostnames, bad options
318 | will be ignored. Might return nil + error if the file cannot be read.
319 |
320 |
321 |
Parameters:
322 |
323 |
filename
324 | (optional) File to parse (defaults to '/etc/resolv.conf' if
325 | omitted) or a table with the file contents in lines.
326 |
423 | checks the hostname type; ipv4, ipv6, or name.
424 | Type is determined by exclusion, not by validation. So if it returns 'ipv6' then
425 | it can only be an ipv6, but it is not necessarily a valid ipv6 address.
426 |
427 |
428 |
Parameters:
429 |
430 |
name
431 | the string to check (this may contain a port number)
432 |
458 | parses a hostname with an optional port.
459 | Does not validate the name/ip. IPv6 addresses are always returned in
460 | square brackets, even if the input wasn't.
461 |
462 |
463 |
Parameters:
464 |
465 |
name
466 | the string to check (this may contain a port number)
467 |