* Pretty print test compression map differences
* Use compressionMapsDifference in TestPackDomainNameCompressionMap
This isn't strictly needed as it only contains a small number of
entries, but is consistent nonetheless.
* Fix map ordering in compressionMapsDifference
* Reduce allocations in UnpackDomainName by better sizing slice
The maximum size of a domain name in presentation format is bounded by
the maximum length of a name in wire octet form and the maximum length
of a label. As s doesn't escape from UnpackDomainName, we can safely
give it the maximum capacity and it will never need to grow.
* Benchmark UnpackDomainName with lonest names possible
* Rename BenchmarkUnpackDomainNameLongestEscaped to match
* Improve maxDomainNamePresentationLength comment
* Further improve maxDomainNamePresentationLength comment
* Simplify maxDomainNameWireOctets checking in UnpackDomainName
* Don't return too long name in UnpackDomainName
* Simplify root domain return in UnpackDomainName
* Bail early from UnpackDomainName when name is too long
This drastically reduces the amount of garbage created
in UnpackDomainName for certain malicious names.
The wire formatted name
"\x3Faaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu\xC0\x00"
would previously generate 1936B of garbage (36112B since maxCompressionPointers
was raised) before returning the "too many compression pointers" error, while
it now generates just 384B of garbage.
* Change +1 budget comment to reflect spec
This better reflects what maxDomainNameWireOctets is actually measuring.
* Remove budget check from after loop in UnpackDomainName
This can never be tripped as budget is always checked immediately after
subtracting inside the loop.
* Improve UnpackDomainName documentation
When packDomainName is called with an escaped domain name and compress
being true, bs wasn't be truncated to the correct length and would
include garbage that would be included in the compression map.
* properly set extended rcode when packing
When calling `SetExtendedRcode`, we expect to get the full extended
rcode, not the rcode after we shift 4 bytes right.
* fix extended rcode
* fix TestOPTTtl test
* set error messages in TestPackExtendedBadCookie
* Set Rcode with extended rcode
* |=
* Set extended RCODE field to 0 when RCODE is not an extended one.
+ unittests
* Force setting extended rcode if we have an OPT available.
* go fmt + @tmthrgd comments
* comments and nits
* reformat comment
* do not modify dns.Rcode when packing to wire format
When the message has an EDNS0 option in the additional section and
dns.Msg.Rcode is set to an extended rcode, dns.Msg.PackBuffer() modifies
dns.Msg.Rcode.
If you were to `Pack` the message and log it after, the Rcode would show
NOERROR.
Running the test before the change would error with:
```
=== RUN TestPackNoSideEffect
--- FAIL: TestPackNoSideEffect (0.00s)
msg_test.go:51: after pack: Rcode is expected to be BADVERS
```
after fixing dns.Msg.PackBuffer(), all tests are still passing.
Fixes#674
* address comments from PR#675
* limiting domain names to 255/63 octets/labels (#463)
(cherry picked from commit 0b729df06c)
* account for \ and \xxx in presentation format
(cherry picked from commit a094f774892fb4305051d185c2488cb43200c4d9)
* go fmt
* Add tests for UnpackDomainName
Domain names must not exceed 255 octets in wire format.
Ref gh-463
Ref gh-469
* Fix UnpackDomainName
* Introduce a long-domain sentinel error
A typed error would be better, but inconsistent with this library.
cf. https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully