Commit Graph

3969 Commits

Author SHA1 Message Date
Tom Thorogood b0835fab5e Reduce allocations in ReverseAddr (#872)
* Add ReverseAddr benchmarks

* Eliminate buffer allocation in ReverseAddr for v6

When the size of a slice is constant it can be allocated on the stack
rather than the heap.

name                old time/op    new time/op    delta
ReverseAddr/IP4-12     175ns ± 5%     173ns ± 4%     ~     (p=0.323 n=10+10)
ReverseAddr/IP6-12     364ns ±14%     218ns ±24%  -40.12%  (p=0.000 n=10+10)

name                old alloc/op   new alloc/op   delta
ReverseAddr/IP4-12     51.0B ± 0%     51.0B ± 0%     ~     (all equal)
ReverseAddr/IP6-12      176B ± 0%       96B ± 0%  -45.45%  (p=0.000 n=10+10)

name                old allocs/op  new allocs/op  delta
ReverseAddr/IP4-12      3.00 ± 0%      3.00 ± 0%     ~     (all equal)
ReverseAddr/IP6-12      3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.000 n=10+10)

* Reduce allocations in ReverseAddr for v4

name                old time/op    new time/op    delta
ReverseAddr/IP4-12     173ns ± 4%     140ns ±13%  -18.97%  (p=0.000 n=10+10)
ReverseAddr/IP6-12     218ns ±24%     218ns ±22%     ~     (p=0.838 n=10+10)

name                old alloc/op   new alloc/op   delta
ReverseAddr/IP4-12     51.0B ± 0%     48.0B ± 0%   -5.88%  (p=0.000 n=10+10)
ReverseAddr/IP6-12     96.0B ± 0%     96.0B ± 0%     ~     (all equal)

name                old allocs/op  new allocs/op  delta
ReverseAddr/IP4-12      3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ReverseAddr/IP6-12      2.00 ± 0%      2.00 ± 0%     ~     (all equal)

* Compare returned address in BenchmarkReverseAddr
2018-12-30 10:28:48 +00:00
Miek Gieben 56516cf4de
Add NULL record (#840)
Sorely missing from this library. Add it. As there is no presentation
format the String method for this type puts a comment in front of it.

Signed-off-by: Miek Gieben <miek@miek.nl>
2018-12-30 09:45:18 +00:00
cesarkuroiwa 5f5f2380fc Don't reject Nscount > 0 (#868)
* Don't reject Nscount > 0

IXFR request could have a SOA RR in the NS section
RFC 1995, section 3: https://tools.ietf.org/html/rfc1995

* Only one RR in the NS section is acceptable

* Remove URL from comment
2018-12-19 16:28:26 +00:00
Miek Gieben 450ab7d57f
Simplify TKEY presentation format (#856)
* Simplify TKEY presentation format

Just put add ";" in front of it, instead of the whole pseudo option
text.

Fixes #855

Signed-off-by: Miek Gieben <miek@miek.nl>

* Add more fields to presentation format - convert time using the RRSIG routines

Signed-off-by: Miek Gieben <miek@miek.nl>
2018-12-04 07:31:40 +00:00
Tom Thorogood 870a59089c Prevent timeout in TestConcurrentExchanges (#862)
This eliminates the possibility of deadlocking the handler and also
simplifies the test considerably.
2018-12-04 07:29:08 +00:00
Tom Thorogood 65b2aa0a63 Make TestTimeout less flaky (#863)
This test currently fails on occasion due to what appears to be
goroutine scheduling differences. This should eliminate those failures.
2018-12-03 07:40:52 +00:00
Miek Gieben 7586a3cbe8 Release 1.1.1 2018-12-02 09:00:23 +00:00
Tom Thorogood ff7d445081 Avoid setting the Rdlength field when packing records (#859)
* Avoid setting the Rdlength field when packing

The offset of the end of the header is now returned from the RR.pack
method, with the RDLENGTH record field being written in packRR.

To maintain compatability with callers of PackRR who might be relying
on this old behaviour, PackRR will now set rr.Header().Rdlength for
external callers. Care must be taken by callers to ensure this won't
cause a data-race.

* Prevent panic if TestClientLocalAddress fails

This came up during testing of the previous change.

* Change style of overflow check in packRR
2018-12-02 08:23:35 +00:00
Tom Thorogood 470f08e191
Reduce compression memory use with map[string]uint16 (#852)
* Reduce compression memory use with map[string]uint16

map[string]uint16 uses 25% less memory per-entry than a map[string]int
(16+2)/(16+8) = 0.75. All entries in the compression map are bound by
maxCompressionOffset which is 14-bits and fits within a uint16.

* Add PackMsg benchmark with more RRs

* Add a comment to the compressionMap struct
2018-12-02 08:50:51 +10:30
Tom Thorogood 5c9c0e7818 Pretty print test compression map differences (#853)
* 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
2018-12-01 08:30:40 +00:00
Tom Thorogood 6b6e08b48c
Stop compressing names in RT records (#847)
* Stop compressing names in RT records

Although RFC 1183 allows names in the RT record to be compressed with:
 "The concrete encoding is identical to the MX RR."

RFC 3597 specifically prohibits compressing names in any record not
defined in RFC 1035.

* Add comment to RT struct regarding compression
2018-11-30 22:50:24 +10:30
Tom Thorogood 8d24af5fb5 Fix compressed length calculations for escaped names (#850)
* Fix compressed length calculations for escaped names

* Add Len benchmark for escaped name

* Fix length with escaping after compression point

* Avoid calling escapedNameLen multiple times in domainNameLen

* Use regular quotes for question in TestMsgCompressLengthEscaped
2018-11-30 07:50:49 +00:00
Tom Thorogood 6ade5b5fff Move compress=false out of packDataDomainNames into caller (#849)
This makes packDataDomainNames more consistent with PackDomainName where
it will only be called with compress = true for `dns:"cdomain-name"`.
2018-11-30 07:49:06 +00:00
Tom Thorogood 778aa4f83d
Properly calculate compressed message lengths (#833)
* Remove fullSize return from compressionLenSearch

This wasn't used anywhere but TestCompressionLenSearch, and was very
wrong.

* Add generated compressedLen functions and use them

This replaces the confusing and complicated compressionLenSlice
function.

* Use compressedLenWithCompressionMap even for uncompressed

This leaves the len() functions unused and they'll soon be removed.

This also fixes the off-by-one error of compressedLen when a (Q)NAME
is ".".

* Use Len helper instead of RR.len private method

* Merge len and compressedLen functions

* Merge compressedLen helper into Msg.Len

* Remove compress bool from compressedLenWithCompressionMap

* Merge map insertion into compressionLenSearch

This eliminates the need to loop over the domain name twice when we're
compressing the name.

* Use compressedNameLen for NSEC.NextDomain

This was a mistake.

* Remove compress from RR.len

* Add test case for multiple questions length

* Add test case for MINFO and SOA compression

These are the only RRs with multiple compressible names within the same
RR, and they were previously broken.

* Rename compressedNameLen to domainNameLen

It also handles the length of uncompressed domain names.

* Use off directly instead of len(s[:off])

* Move initial maxCompressionOffset check out of compressionLenMapInsert

This should allow us to avoid the call overhead of
compressionLenMapInsert in certain limited cases and may result in a
slight performance increase.

compressionLenMapInsert still has a maxCompressionOffset check inside
the for loop.

* Rename compressedLenWithCompressionMap to msgLenWithCompressionMap

This better reflects that it also calculates the uncompressed length.

* Merge TestMsgCompressMINFO with TestMsgCompressSOA

They're both testing the same thing.

* Remove compressionLenMapInsert

compressionLenSearch does everything compressionLenMapInsert did anyway.

* Only call compressionLenSearch in one place in domainNameLen

* Split if statement in domainNameLen

The last two commits worsened the performance of domainNameLen
noticably, this change restores it's original performance.

name                            old time/op    new time/op    delta
MsgLength-12                       550ns ±13%     510ns ±21%    ~     (p=0.050 n=10+10)
MsgLengthNoCompression-12         26.9ns ± 2%    27.0ns ± 1%    ~     (p=0.198 n=9+10)
MsgLengthPack-12                  2.30µs ±12%    2.26µs ±16%    ~     (p=0.739 n=10+10)
MsgLengthMassive-12               32.9µs ± 7%    32.0µs ±10%    ~     (p=0.243 n=9+10)
MsgLengthOnlyQuestion-12          9.60ns ± 1%    9.20ns ± 1%  -4.16%  (p=0.000 n=9+9)

* Remove stray newline from TestMsgCompressionMultipleQuestions

* Remove stray newline in length_test.go

This was introduced when resolving merge conflicts.
2018-11-30 10:03:41 +10:30
Tom Thorogood 2c039114d2 Use a table lookup for escaping unprintable bytes (#846) 2018-11-29 19:57:48 +00:00
Tom Thorogood c0747f060e Reduce allocations in UnpackDomainName by better sizing slice (#844)
* 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
2018-11-29 19:55:51 +00:00
Tom Thorogood b1bf6f1b9b Simplify unprintable handling in UnpackDomainName (#845)
* Avoid the for loop for unprintable octets in UnpackDomainName

* Unify unprintable checking with writeTXTStringByte

These constants are identical.
2018-11-29 07:32:13 +00:00
Miek Gieben ed726faad6
Merge pull request #843 from tmthrgd/hashname
Reduce memory consumption in HashName
2018-11-29 07:19:50 +00:00
Tom Thorogood 71bbe52b67
Hoist SHA1 check in HashName
That's all that's supported by this function, and moving it makes the
code cleaner.
2018-11-29 10:44:01 +10:30
Tom Thorogood c2889aea81
Reduce wireSalt allocation in HashName 2018-11-29 10:30:02 +10:30
Tom Thorogood 2f8cf50b32
Add a HashName benchmark 2018-11-29 10:28:29 +10:30
Tom Thorogood 1afd10068a
Rename wire to wireSalt in HashName
This is slightly clearer.
2018-11-29 10:25:34 +10:30
Tom Thorogood 4058ac87fa
Remove saltWireFmt from nsecx.go
This isn't really needed and doesn't make the code any clearer.
2018-11-29 10:24:25 +10:30
Miek Gieben fa589750ad
Merge pull request #842 from tmthrgd/compression-map-escaped
Put escaped names into compression map
2018-11-28 23:39:22 +00:00
Tom Thorogood 56118562d7
Fix typo in TestMsgCompressLengthEscapingMatch comment 2018-11-29 09:58:18 +10:30
Tom Thorogood d27f0d3482
Add a test case to cover escaping in the compression map 2018-11-29 09:53:00 +10:30
Tom Thorogood 07ae768ab1
Put escaped names into compression map in PackDomainName 2018-11-29 09:49:18 +10:30
Tom Thorogood c1ad186588
Use compressionMapsEqual in TestPackDomainNameCompressionMap 2018-11-29 09:48:02 +10:30
Miek Gieben a220737569 Release 1.1.0 2018-11-28 23:08:28 +00:00
Miek Gieben 1c92765836
Merge pull request #830 from miekg/passfunc
Add MsgAcceptFunc
2018-11-28 23:07:35 +00:00
Miek Gieben f92da6fc6e Code review
Signed-off-by: Miek Gieben <miek@miek.nl>
2018-11-28 22:40:08 +00:00
Miek Gieben fef7963e99 remove newlines
Signed-off-by: Miek Gieben <miek@miek.nl>
2018-11-28 22:32:50 +00:00
Miek Gieben ab67d69d9b review
Signed-off-by: Miek Gieben <miek@miek.nl>
2018-11-28 22:25:28 +00:00
Tom Thorogood 6aa28be819
Bail early from UnpackDomainName when name is too long (#839)
* 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
2018-11-29 08:26:30 +10:30
Miek Gieben 091d66a39f
Merge pull request #818 from tmthrgd/comp-opt
Improve PackDomainName performance
2018-11-28 18:53:23 +00:00
Miek Gieben db37038897 fix docs
Signed-off-by: Miek Gieben <miek@miek.nl>
2018-11-28 18:45:19 +00:00
Miek Gieben 74dbfccc11 Code Review
Signed-off-by: Miek Gieben <miek@miek.nl>
2018-11-28 18:42:48 +00:00
Miek Gieben 2c18e7259a Add MsgAcceptFunc in server
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>
2018-11-28 18:42:48 +00:00
Miek Gieben 6bf402f3c4
Fix "too many compression points" for valid message (#835)
* Increase the maximum number of allowed compression pointers

* Add a Pack+Unpack test case for many compression pointers

* Clarify maxCompressionPointers comment
2018-11-28 11:45:22 +00:00
Tom Thorogood 64a73613cd Use range loop in packBufferWithCompressionMap (#837)
* 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
2018-11-28 11:44:23 +00:00
Tom Thorogood d193d08243
Clarify maxCompressionPointers comment 2018-11-28 21:38:37 +10:30
Tom Thorogood 32d8e33ba2
Add a Pack+Unpack test case for many compression pointers 2018-11-28 19:53:00 +10:30
Tom Thorogood c567cfc2bb
Increase the maximum number of allowed compression pointers 2018-11-28 19:52:41 +10:30
Tom Thorogood 7ae05cdcf8
Use map[string]struct{} for compression map in Len (#820)
* 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
2018-11-28 08:02:08 +10:30
Tom Thorogood 34d23c00e1
Add bounds check comment to dddToByte 2018-11-28 07:42:44 +10:30
Miek Gieben a7e7488e1d
doc: Clean up README and doc.go (#817)
Cleans this up a bit.

Signed-off-by: Miek Gieben <miek@miek.nl>
2018-11-27 14:39:19 +00:00
Tom Thorogood 03d7306558 Fix NotImp RCode string (#819)
* Fix NOTIMP typo in RcodeToString

RFC 6895 lists RCODE 4 as NotImp.

* Accept legacy NOTIMPL spelling in StringToRcode
2018-11-27 14:38:33 +00:00
Tom Thorogood c03bc41f33 Remove pointless cast from unpackUint48 (#827) 2018-11-27 14:34:23 +00:00
Tom Thorogood e2f69345fd Avoid creating compression map for question only Msg (#823)
* 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
2018-11-27 14:34:07 +00:00
Tom Thorogood 11fb61cb84 Use copy instead of loop in EDNS0_SUBNET.unpack (#825) 2018-11-27 14:32:41 +00:00