| 1 | # luaunbound |
| 2 | |
| 3 | This is a binding to [libunbound](https://unbound.net/) for |
| 4 | [Lua](https://www.lua.org/), allowing both asynchronous and |
| 5 | DNSSEC-secured DNS lookups of arbitrary DNS record types. |
| 6 | |
| 7 | It was created because [Prosody](https://prosody.im/) needs an |
| 8 | asynchronous DNS library with support for SRV records, and the ones |
| 9 | found at the time did one or the other, or was missing DNSSEC that |
| 10 | allowed implementing |
| 11 | [DANE](https://www.internetsociety.org/resources/deploy360/dane/). |
| 12 | |
| 13 | It originated out of a need in the [XMPP](https://xmpp.org/) server |
| 14 | software [Prosody](https://prosody.im/) for an async-capable resolver |
| 15 | library supporting SRV records, as well as a desire to experiment with |
| 16 | DNSSEC and new DNS records. |
| 17 | |
| 18 | ## Downloading |
| 19 | |
| 20 | Source can be downloaded with Mercurial from |
| 21 | <https://code.zash.se/luaunbound/>. |
| 22 | |
| 23 | Signed releases can be found at <https://code.zash.se/dl/luaunbound/>. |
| 24 | |
| 25 | It is also available from [luarocks](https://luarocks.org/) and can be |
| 26 | installed by |
| 27 | |
| 28 | luarocks install luaunbound |
| 29 | |
| 30 | ## Building |
| 31 | |
| 32 | make |
| 33 | |
| 34 | ## API |
| 35 | |
| 36 | ### Creating a new context |
| 37 | |
| 38 | The lunbound module has a single function, `new()` for creating a new |
| 39 | context. It takes a table with configuration as single optional |
| 40 | argument. If no argument is given the `config` table on the module will |
| 41 | be used. |
| 42 | |
| 43 | ### Config options |
| 44 | |
| 45 | `async` |
| 46 | : Uses threads if `true` or forks a process if `false`. |
| 47 | |
| 48 | `hoststxt` |
| 49 | : Path to `hosts.txt` file. If set to `true` then the default system |
| 50 | `hosts.txt` file. |
| 51 | |
| 52 | `resolvconf` |
| 53 | : Path to resolver configuration. If set to `true` then the default |
| 54 | system resolvers are used. Otherwise root hints are used. |
| 55 | |
| 56 | `forward` |
| 57 | : IP address of an upstream resolver(s) to use, a string or array of |
| 58 | strings. |
| 59 | |
| 60 | `trusted` |
| 61 | : DNSSEC root trust anchors, a string or array of strings. |
| 62 | |
| 63 | `trustfile` |
| 64 | : Path to a file containing DNSSEC root trust anchors. Can be |
| 65 | specified at compile-time (recommended for distributors). |
| 66 | |
| 67 | `options` |
| 68 | : Table allowing arbitrary settings from `unbound.conf`. |
| 69 | |
| 70 | The built-in defaults are as follows: |
| 71 | |
| 72 | ``` lua |
| 73 | local resolver = require"luaunbound".new({ |
| 74 | async = true; |
| 75 | hoststxt = true; |
| 76 | resolvconf = true; |
| 77 | }); |
| 78 | ``` |
| 79 | |
| 80 | ### Context methods |
| 81 | |
| 82 | `ctx:resolve(name, type, class)` |
| 83 | : Resolves name and returns a table with results. |
| 84 | |
| 85 | `ctx:resolve_async(callback, name, type, class)` |
| 86 | : Starts a query in async mode. Results are passed to the callback |
| 87 | when the query is completed. |
| 88 | |
| 89 | `ctx:fd()` |
| 90 | : Returns a file descriptor that will appear readable when there are |
| 91 | results available. |
| 92 | |
| 93 | `ctx:cancel(query)` |
| 94 | : Cancel a query. |
| 95 | |
| 96 | `ctx:cancelall()` |
| 97 | : Cancel all pending queries. |
| 98 | |
| 99 | `ctx:process()` |
| 100 | : Calls callbacks for all completed queries. |
| 101 | |
| 102 | `ctx:wait()` |
| 103 | : Blocks until all outstanding queries are completed and then calls |
| 104 | callbacks for all completed queries. |
| 105 | |
| 106 | `ctx:poll()` |
| 107 | : Returns `true` if new results are available. |
| 108 | |
| 109 | ### Result table |
| 110 | |
| 111 | The result table closely resembles libunbounds `struct ub_result`. |
| 112 | |
| 113 | `qname`, `qtype` and `qclass` |
| 114 | : Same as arguments to resolve methods. |
| 115 | |
| 116 | `canonname` |
| 117 | : The canonical name if the queried name was a CNAME. Note that full |
| 118 | CNAME chasing is done by libunbound. |
| 119 | |
| 120 | `rcode`, `havedata` and `nxdomain` |
| 121 | : The DNS status code and flags indicating if any data is available. |
| 122 | |
| 123 | `secure` and `bogus` |
| 124 | |
| 125 | : Indicates DNSSEC validation status. There are three possible |
| 126 | combinations: |
| 127 | |
| 128 | - Results are signed and validation succeeded, `secure` will be |
| 129 | `true`. |
| 130 | - Results are signed but validation failed, `secure` will be `false` |
| 131 | and `bogus` will be a string with an error message. |
| 132 | - The results were not signed. `secure` will be `false` and `bogus` |
| 133 | will be `nil`. |
| 134 | |
| 135 | The actual result data will be in the array part of the result table, in |
| 136 | the form of binary strings. It is your job to parse these into whatever |
| 137 | form you want. |