Commit Graph

232 Commits

Author SHA1 Message Date
Tom Thorogood e6df8867af
Fix Rdlength related parsing bug in RFC3597 records (#1214)
* Set Rdlength in fromRFC3597

This was a bug found by oss-fuzz. My bad (#1211).

* Limit maximum length of Rdata in (*RFC3597).parse

RDATA must be a 16-bit unsigned integer.

* Validate Rdlength and off in UnpackRRWithHeader

* Revert "Validate Rdlength and off in UnpackRRWithHeader"

This reverts commit 2f6a8811b944b100af7605e53a6fb164944a6d65.

* Use hex.DecodedLen in (*RFC3597).fromRFC3597

While this isn't done elsewhere, it is clearer and more obvious.
2021-02-01 09:10:38 +01:00
Tom Thorogood 13238cb6ad
Support parsing known RR types in RFC 3597 format (#1211)
* Support parsing known RR types in RFC 3597 format

This is the format used for "Unknown DNS Resource Records", but it's
also useful to support parsing known RR types in this way.

RFC 3597 says:

   An implementation MAY also choose to represent some RRs of known type
   using the above generic representations for the type, class and/or
   RDATA, which carries the benefit of making the resulting master file
   portable to servers where these types are unknown.  Using the generic
   representation for the RDATA of an RR of known type can also be
   useful in the case of an RR type where the text format varies
   depending on a version, protocol, or similar field (or several)
   embedded in the RDATA when such a field has a value for which no text
   format is known, e.g., a LOC RR [RFC1876] with a VERSION other than
   0.

   Even though an RR of known type represented in the \# format is
   effectively treated as an unknown type for the purpose of parsing the
   RDATA text representation, all further processing by the server MUST
   treat it as a known type and take into account any applicable type-
   specific rules regarding compression, canonicalization, etc.

* Correct mistakes in TestZoneParserAddressAAAA

This was spotted when writing TestParseKnownRRAsRFC3597.

* Eliminate canParseAsRR

This has the advantage that concrete types will now be returned for
parsed ANY, NULL, OPT and TSIG records.

* Expand TestDynamicUpdateParsing for RFC 3597

This ensures we're properly handling empty RDATA for RFC 3597 parsed
records.
2021-01-30 14:05:25 +01:00
Tom Thorogood 25cacca8ca Prohibit newlines before record data in the ZoneParser (#979)
* Merge setRR into ZoneParser.Next

* Remove file argument from RR.parse

This was only used to fill in the ParseError file field. Instead we now
fill in that field in ZoneParser.Next.

* Move dynamic update check out of RR.parse

This consolidates all the dynamic update checks into one place.

* Check for unexpected newline before parsing RR data

* Move rr.parse call into if-statement

* Allow dynamic updates for TKEY and RFC3597 records

* Document that ParseError file field is unset from parse

* Inline allowDynamicUpdate into ZoneParser.Next

* Improve and simplify TestUnexpectedNewline
2019-06-10 07:38:54 +01:00
Tom Thorogood c5493ad28d
Use an interface method for IsDuplicate (#883)
* Prevent IsDuplicate from panicing on wrong Rrtype

* Replace the isDuplicateRdata switch with an interface method

This is substantially simpler and avoids the need to use reflect.

* Add equal RR case to TestDuplicateWrongRrtype

* Move RR_Header duplicate checking back into IsDuplicate

This restores the previous behaviour that RR_Header's were never
equal to each other.

* Revert "Move RR_Header duplicate checking back into IsDuplicate"

This reverts commit a3d7eba50825d546074d81084e639bbecf9cbb57.

* Run go fmt on package

This was a mistake when I merged the master branch into this PR.

* Move isDuplicate method to end of RR interface
2019-01-06 14:42:38 +10:30
Tom Thorogood db3d0ce13b
Use an interface method for parsing zone file records (#886)
* Eliminate Variable bool from parserFunc

Instead we now check whether the last token read from the zlexer was
a zNewline or zEOF. The error check above should be tripped for any
record that ends prematurely.

* Use an interface method for parsing zone file records

* Prevent panic in TestOmittedTTL if no regexp match

* Move slurpRemainder into fixed length parse functions

This is consistent with the original logic in setRR and avoids potential
edge cases.

* Parse synthetic records according to RFC 3597

These records lack a presentation format and cannot be parsed otherwise.
This behaviour is consistent with how this previously operated.
2019-01-06 14:36:16 +10:30
Tom Thorogood b955100a79
Move RR header packing out of generated code (#885) 2019-01-04 10:09:14 +10:30
Tom Thorogood 57b81e0614 Use an interface method for unpacking records (#884)
* Use an interface method for unpacking records

* Eliminate err var declaration from unpack functions

* Remove pointless r.Data assignment in PrivateRR.unpack
2019-01-03 17:35:32 +01: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 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
Miek Gieben a93f3e4f6b
copyHeader is redundant (#672)
copyHeader() is redundant, we allocate a header and then copy the
non-pointer elements into it; we don't need to do this, because if we
just asssign rr.Hdr to something else we get the same result.

Remove copyHeader() and the generation and use of it in ztypes.go.
2018-05-10 14:50:26 +01:00
Miek Gieben b38dc3dcb7
Cleanup: gofmt -w -s *.go (#548)
Some renames of internal names to make go lint happier.
2017-11-03 16:15:35 +00:00
Miek Gieben e6b37d00af Cleanup and removals (#377)
* Cleanup and removals

Gut rawmsg.go as most functions are not used. Reword some documentation.
Add more types to be checked for name compression.

* Yeah, we do use these

* Remove this function as well - only used one
2016-06-13 19:44:38 +01:00
Miek Gieben b51e305bc6 Remove reflection (#376)
Everything is generated. Remove all uses of packStruct/unpackStruct and
make the library reflectionless.
2016-06-12 21:06:46 +01:00
Miek Gieben 907a4aef57 Generate pack/unpack for all RRs (#360)
Add dns:txt parsing helper to prevent compile errors. This allows
us to generate all unpack/pack function.

Add pack to the RR interface definition and add this method to
PrivateRR.

We still use typeToUnpack to select which types don't use reflection.
2016-06-05 07:53:12 +01:00
Joe Farrell bd738b30b1 Fixed typo in docstring (#358) 2016-06-04 08:11:36 +01:00
Miek Gieben 9bf52083d1 golint fixes 2015-08-23 08:03:13 +01:00
Peter M. Goldstein 34a9377404 Update the value of MaxMsgSize 2015-03-09 13:51:11 -07:00
Miek Gieben 64fea017a2 Move all docs to docs.go
Another golint change.
2015-02-19 13:47:50 +00:00
Miek Gieben faa311bf55 golint: more docs 2015-02-19 11:26:46 +00:00
Miek Gieben 67945c119e A bunch of golint fixes
The proposed vars names are a nono, because they break the API.
Things left: document each RR and zscan_rr.go has some funcky if-then-elses.
2015-02-19 09:58:33 +00:00
Miek Gieben 5d8dbab4c8 Remove the fmt Formatters
Formatters are not needs you can access the members just fine.
However the rdata Field access function are handy and non-trivial,
extend them and add a basic test.
2015-01-13 10:20:34 +00:00
Miek Gieben aaa3aba4ed Implement custom verbs for fmt.Printf use
The dns package implements String() for all RR types, but sometimes you will
need more flexibility. The functions Printf, Sprintf, etc. implemented formatted I/O
for the RR type.

Printing

The verbs:

Generic part of RRs:

     %N	the owner name of the RR
     %C	the class: IN, CH, CLASS15, etc.
     %D	the TTL in seconds
     %Y	the type: MX, A, etc.

The rdata of each RR differs, we allow each field to be printed as a string.

Rdata:

     %0	the first rdata field
     %1	the second rdata field
     %2	the third rdata field
     ..	...
     %9      the nineth rdata field
     %R	all rdata fields

The rdata fields remain a TODO, but will be implemented using
reflection.
2015-01-11 17:23:02 +00:00
Miek Gieben 0aa3021a83 Remove all copyright notices
Use the central COPYRIGHT file.
2014-09-11 20:57:37 +01:00
Miek Gieben b6da65c4b0 typo in the docs 2014-09-11 08:49:16 +01:00
Miek Gieben 18c150f21a zoneMatch is not used 2014-08-30 15:09:01 +01:00
Miek Gieben 4dd48338af documentation tweaks 2014-07-30 07:35:06 +01:00
Miek Gieben b7a8c14d7d Add dns.Name as a type
This can be used for printing names.
2014-04-23 21:06:17 +01:00
Miek Gieben 6295f64c1c Make sprintDomain public as NameToString 2014-04-23 20:58:55 +01:00
Andrew Tunnell-Jones bd189318ed Document domain name and TXT escaping behaviour 2014-03-02 10:38:46 +00:00
Andrew Tunnell-Jones 3f834a04fb Update domain name and TXT string escape behaviour
Changes to domain name packing and unpacking:
* Escape dot, backslash, brackets, double-quote, semi-colon and space
* Tab, line feed and carriage return become \t, \n and \r

Changes to TXT string packing and unpacking:
* Escape backslash and double-quote
* Tab, line feed and carriage return become \t, \n and \r
* Other unprintables to \DDD

Stringers do the equivalent of putting domain names and TXT strings
to the wire and back.

There is some duplication of logic. I found performance suffered when
I broke the logic out into smaller functions. I think this may have
been due to functions not being inlined for various reasons.
2014-02-26 10:55:11 +00:00
Miek Gieben 87b380cad5 Revert "Use dLen for domain name length"
This reverts commit b595183834.

Fix up the tests too.
2014-02-14 20:20:22 +00:00
Miek Gieben b595183834 Use dLen for domain name length 2014-02-12 12:50:16 +00:00
Brandon Philips 870c049f52 fix(dns.go): Pref is now Preference
Simple mistake from, what I guess, was an earlier API change.
2014-01-25 15:57:16 -08:00
Miek Gieben 37356f5e68 Documenation fixes 2014-01-12 10:43:59 +00:00
Miek Gieben 1c343223b5 Go over the documentation again 2013-10-15 13:42:24 +00:00
Miek Gieben b06d42f1d7 correct documentation 2013-09-29 11:50:09 +01:00
Miek Gieben 9a38f97391 Fix timeouts, Tsig and EDNS0 update size 2013-09-29 11:21:18 +01:00
Miek Gieben 140e525195 More documentation 2013-09-28 22:05:48 +01:00
Miek Gieben 2b6e9122bd Use the better name: SingleInflight 2013-09-06 09:49:07 +00:00
Miek Gieben 0adc9a3afe Update doc and TODO 2013-09-06 09:45:30 +00:00
Miek Gieben 70f9c6b964 Make err non-exported 2013-06-20 07:25:29 +01:00
Miek Gieben 8e68ad38d6 Use new stuff here too 2013-06-08 14:24:20 +01:00
Miek Gieben 15d8cdc5f9 Fix: the *dns.TXT instead of *TXT 2013-06-04 11:37:11 +02:00
Miek Gieben 98b677a97c Update the copy right for the original GO files 2013-05-12 16:07:06 +02:00
Miek Gieben d53d9eab81 gofmt 2013-05-05 20:30:44 +02:00
Miek Gieben d2551c5c84 Docs and todo update 2013-04-17 08:47:07 +01:00
Miek Gieben cd10853288 Make Len() and Copy() private
I don't think anybody will needs these, msg.Len() is still
available. Severly cuts back on the amount of exported functions.
2013-02-09 08:35:17 +01:00
Miek Gieben 78619c08e4 Make it a method 2013-01-31 09:52:34 +01:00
Miek Gieben 9357ac3eff docs 2013-01-30 15:56:29 +01:00