| 1 |
# Even more alternative approach to a DNS library (version 2) |
| 2 |
|
| 3 |
Version 1 of miekg/dns didn't have any development guidelines, and although this went remarkably well for |
| 4 |
years it is nice to give some guidance to new contributors and to lay out some of the design decisions. |
| 5 |
|
| 6 |
## Source Layout |
| 7 |
|
| 8 |
The main codebase sits in _dns_ and package _rdata_, which defines the rdata for all RRs. Helper |
| 9 |
functions are put in _dnsutil_, unless this is impossible because of cyclic dependencies. A dnsutil |
| 10 |
function/method that is _also_ useful in _dns_ should be put in `dnsutil/shared.go`; this file is "go |
| 11 |
generated" to various other locations. |
| 12 |
|
| 13 |
Useful helper function? -> _dnsutil_. |
| 14 |
Useful helper function, but no way to put it in subpackage (cyclic dependencie) -> _dns_. |
| 15 |
Useful helper function, that can help with testing (and other things) -> _dnstest_. |
| 16 |
Helper function(s) -> new package in _internal/..._. |
| 17 |
|
| 18 |
## Struct Tags |
| 19 |
|
| 20 |
The DNS types in `rdata/rdata.go` carry struct tags to specify what kind of string it is so we can pack/unpack |
| 21 |
this correct, amongs other we have: |
| 22 |
|
| 23 |
- `name`: a plain name. |
| 24 |
- `cname`: a name that can be used for compression. |
| 25 |
- `mname`: the SOA's mbox name. Can contain `\.`. |
| 26 |
- `ttl`: the number can be a duration just like a TTL, 4D, 5W, etc. |
| 27 |
- `base64`: string is encoded as base64. |
| 28 |
- `base32`: string is encoded as base32. |
| 29 |
- `hex`: string is encoded as hex. |
| 30 |
- `size-hex:XxxYY`: string is encoded as hex, and the length is put in field named: XxxYY. |
| 31 |
|
| 32 |
## Go Generate |
| 33 |
|
| 34 |
Al lot of things are generated from the RR struct tags, see `dns.go` for ~12 we currently have. They all reuse |
| 35 |
the _internal/generate_ package. |
| 36 |
|
| 37 |
### Cloner Interface |
| 38 |
|
| 39 |
When adding an RR run a `go generate ./...`, this might fail on `zclone.go`, in that case remove the file |
| 40 |
zclone.go and in dns.go remove `Cloner` from then `RR` interface. Run go generate, and re-add `Cloner` again |
| 41 |
when done. |
| 42 |
|
| 43 |
## "Big" RRs |
| 44 |
|
| 45 |
RR that have a lot of different rdata "types", like the SVCB record a sub-package should be created where |
| 46 |
most of the types and methods should be located. For SVCB, the _svcb_ package exist. Each sub-type should |
| 47 |
be capitalized, as-if it is an RR. The public API for these sub-types should match the `RR` interface: |
| 48 |
|
| 49 |
- Header() \*Header |
| 50 |
- String() string |
| 51 |
- Len() int |
| 52 |
|
| 53 |
Due to cyclic dependencies this creates some friction, but in the end it will be easier for end-users. It's |
| 54 |
important to put as much of the details in this sub-package. The top-level RR should be put in types.go. |
| 55 |
|
| 56 |
The sub-types in that RR should all capital letters as their name, as-if they are (also) RRs. For the |
| 57 |
in-progress DELEG RR, a _deleg_ package exists which houses most of the complexity. |
| 58 |
|
| 59 |
## Custom types for uint8/16 |
| 60 |
|
| 61 |
The `type Key uint16` looks nice and _is_ more type-safe, but then you need to convert to and from uint16 all |
| 62 |
over the place - negating the type safety entirely. It might be helpful for documenting a type, but that |
| 63 |
uint16 is probably not the most important details of your new resource record. |
| 64 |
|
| 65 |
## Naming |
| 66 |
|
| 67 |
### Values like Rcode, Class etc. |
| 68 |
|
| 69 |
If you have a bunch of values that certain types can take the are named: `ValueThing` and will need a |
| 70 |
`ValueToString`/`StringToValue` map or function. `Thing` may or may not be capitalized. E.g. we have |
| 71 |
`RcodeScucces` and `ClassINET`. |
| 72 |
|
| 73 |
Naming constants for RRs needs to have the RR's mnemonic prefixed and in upper case letters, i.e. |
| 74 |
ZONEMDSchemeSimple, for a constant used in the ZONEMD RR. |
| 75 |
|
| 76 |
Methods on RR types have `rr` as the receiver's name. For EDNS0 "RRs", the receiver is named `o`. For DSO `d` |
| 77 |
is used. Methods on `Msg` use `m`. |
| 78 |
|
| 79 |
Hexadecimal numbers are written in uppercase. |
| 80 |
|
| 81 |
### RR Types |
| 82 |
|
| 83 |
The naming of types follows the RFCs. EDNS0 types are similarly named, for instance, DHU (DS Hash Understood). |
| 84 |
If there is a clash between an actual RR's and an EDNS0 one, the EDNS0 type will get an 'E' as prefix, e.g. |
| 85 |
EDHU. This will also be done if the RR was named later than the EDNS0 option! The same is the for DSO (DNS |
| 86 |
Stateful Operations), when clashing those types will be prefixed with a 'D'. If EDNS0 and DSO clash, EDNS0 |
| 87 |
wins. See PADDING and DPADDING as an example. |
| 88 |
|
| 89 |
### Exporting more Functions/Methods |
| 90 |
|
| 91 |
In dnsv1, `packRR` was exported as `PackRR`, this made the compression map (`map[string]int`), part of the API, |
| 92 |
meaning the more efficient `map[string]uint16` could never be properly used. In this library we are going to |
| 93 |
be very hesitant to add to the API, `packRR` is a private function again. |
| 94 |
|
| 95 |
### Tests |
| 96 |
|
| 97 |
For tests name them after the sub-system and the something more specific. This makes it easy to just run the |
| 98 |
tests for that sub-system. `TestZoneParserXXX`, `TestMsgXXX`, etc. |
| 99 |
|
| 100 |
Most tests are table driven with (optional) sub-tests, the main tests are usually put in a struct called |
| 101 |
`testcases` and while ranging over them a test is named `tc`. |
| 102 |
|
| 103 |
Preferably add a new test (case) to an _existing_ test instead of making a completely new one that just tests |
| 104 |
your feature. This reduces test sprawling into all kinds of files. The current tests a written in a fairly |
| 105 |
generic way to make this possible. |
| 106 |
|
| 107 |
There are also many more Example tests to aid in documenting this package. Consider adding one for your new |
| 108 |
feature. |
| 109 |
|
| 110 |
### Benchmarking |
| 111 |
|
| 112 |
There are various benchmarking tests you can run. In cmd/reflect there is a tiny reflection DNS server that is |
| 113 |
used for benchmarking, with `make old.txt` you create the current baseline, then in your branch you run `make |
| 114 |
new.txt` and `make stat` to get a sense of how efficient your change is. |