* 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
Generalize the srv.Unsafe and make it pluggeable. Also add a default
accept function that allows to discard malformed DNS messages very early
on. Before we allocate and parse anything furher.
Also re-use the client's message when sending a reply.
Signed-off-by: Miek Gieben <miek@miek.nl>
* Increase the maximum number of allowed compression pointers
* Add a Pack+Unpack test case for many compression pointers
* Clarify maxCompressionPointers comment
* Use range loops in Msg.packBufferWithCompressionMap
* Remove rr set variables in Msg.packBufferWithCompressionMap
* Move Header var down in Msg.packBufferWithCompressionMap
* Move stripTsig comment into Msg.Unpack
* Use map[string]struct{} for compression map in Len
map[string]int requires 8 bytes per entry to store the unused position
information.
* Add MsgLength benchmark with more RRs
* Pass dns.Compress explicitly to packBufferWithCompressionMap
* Avoid creating compression map for question only Msg
This idea was inspired by:
"Skip dname compression for replies with no answers."
https://www.nlnetlabs.nl/bugs-script/show_bug.cgi?id=235
* Continue compressing multiple questions
* Remove ErrTruncated from the library
ErrTruncated is removed. This (correctly) assume that a truncated
message will be fully formed. Any message that isn't fully formed will
return (most likely) an unpack error.
Any program using ErrTruncated will fail to compile when they update to
this version: this is by design: you're doing it wrong. For checking if
a message was truncated you should checked the msg.Truncated boolean;
assuming the unpack didn't fail.
Fixes#814
Signed-off-by: Miek Gieben <miek@miek.nl>
* Restore tests
Signed-off-by: Miek Gieben <miek@miek.nl>
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.
When the dname is found in the compression map and compress is true,
this copy is as it will simply be overwritten later. This could provide
a very slight speedup.
* 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
* Simplify appendByte
* Add test case and benchmark for sprintName
* Add test case and benchmark for sprintTxtOctet
* Add test case and benchmark for sprintTxt
* Use strings.Builder for sprint* functions in types.go
* Use writeByte helper in unpackString
* Rename writeByte to writeEscapedByte
This better captures the purpose of this function.
These two functions were identical (sans-variable names) before I
optimized unpackString in 5debfeec63.
This will improve the performance of it's only caller unpackTxt and is
covered by the test and benchmark added in 5debfeec63.
* Remove redundant parenthesis
These were caught with:
gofmt -r '(a) -> a' -w *.go
This commit only includes the changes where the formatting makes the
ordering of operations clear.
* Remove more redundant parenthesis
These were caught with:
gofmt -r '(a) -> a' -w *.go
This commit includes the remaining changes where the formatting does not
make the ordering of operations as clear as the previous commit.
* Fixed len computation when size just goes beyond 14 bits
* Added bouds checks around 14bits
* Len() always right including when around 14bits boudaries
* Avoid splitting into labels when not applicable
* Fixed comments
* Added comments in code
* Added new test cases
* Fixed computation of Len() for SRV and all kind of records
* Fixed Sign that was relying on non-copy for Unit tests
* Removed unused padding
* Fixed typo in PackBuffer() function
* Added comment about packBufferWithCompressionMap() for testing purposes