This is a clone/fork of https://github.com/miekg/dns
Go to file
Tom Thorogood 274da7d3ef
Add new ZoneParser API (#794)
* Improve ParseZone tests

* Add new ZoneParser API

* Use the ZoneParser API directly in ReadRR

* Merge parseZoneHelper into ParseZone

* Make generate string building slightly more efficient

* Add SetDefaultTTL method to ZoneParser

This makes it possible for external consumers to implement ReadRR.

* Make $INCLUDE directive opt-in

The $INCLUDE directive opens a user controlled file and parses it as
a DNS zone file. The error messages may reveal portions of sensitive
files, such as:
	/etc/passwd: dns: not a TTL: "root0:0:root:/root:/bin/bash" at line: 1:31
	/etc/shadow: dns: not a TTL: "root:$6$<redacted>::0:99999:7:::" at line: 1:125

Both ParseZone and ReadRR are currently opt-in for backward
compatibility.

* Disable $INCLUDE support in ReadRR

ReadRR and NewRR are often passed untrusted input. At the same time,
$INCLUDE isn't really useful for ReadRR as it only ever returns the
first record.

This is a breaking change, but it currently represents a slight
security risk.

* Document the need to drain the ParseZone chan

* Cleanup the documentation of NewRR, ReadRR and ParseZone

* Document the ZoneParser API

* Deprecated the ParseZone function

* Add whitespace to ZoneParser.Next

* Remove prevName field from ZoneParser

This doesn't track anything meaningful as both zp.prevName and h.Name
are only ever set at the same point and to the same value.

* Use uint8 for ZoneParser.include field

It has a maximum value of 7 which easily fits within uint8.

This reduces the size of ZoneParser from 160 bytes to 152 bytes.

* Add setParseError helper to ZoneParser

* Surface $INCLUDE os.Open error in error message

* Rename ZoneParser.include field to includeDepth

* Make maximum $INCLUDE depth a const

* Add ParseZone and ZoneParser benchmarks

* Parse $GENERATE directive with a single ZoneParser

This should be more efficient than calling NewRR for each generated
record.

* Run go fmt on generate_test.go

* Add a benchmark for $GENERATE directives

* Use a custom reader for generate

This avoids the overhead and memory usage of building the zone string.

name         old time/op    new time/op    delta
Generate-12     165µs ± 4%     157µs ± 2%   -5.06%  (p=0.000 n=25+25)

name         old alloc/op   new alloc/op   delta
Generate-12    42.1kB ± 0%    31.8kB ± 0%  -24.42%  (p=0.000 n=20+23)

name         old allocs/op  new allocs/op  delta
Generate-12     1.56k ± 0%     1.55k ± 0%   -0.38%  (p=0.000 n=25+25)

* Return correct ParseError from generateReader

The last commit made these regular errors while they had been
ParseErrors before.

* Return error message as string from modToPrintf

This is slightly simpler and they don't need to be errors.

* Skip setting includeDepth in generate

This sub parser isn't allowed to use $INCLUDE directives anyway.

Note: If generate is ever changed to allow $INCLUDE directives, then
      this line must be added back. Without doing that, it would be
      be possible to exceed maxIncludeDepth.

* Make generateReader errors sticky

ReadByte should not be called after an error has been returned, but
this is cheap insurance.

* Move file and lex fields to end of generateReader

These are only used for creating a ParseError and so are unlikely to be
accessed.

* Don't return offset with error in modToPrintf

Along for the ride, are some whitespace and style changes.

* Add whitespace to generate and simplify step

* Use a for loop instead of goto in generate

* Support $INCLUDE directives inside $GENERATE directives

This was previously supported and may be useful. This is now more
rigorous as the maximum include depth is respected and relative
$INCLUDE directives are now supported from within $GENERATE.

* Don't return any lexer tokens after read error

Without this, read errors are likely to be lost and become parse errors
of the remaining str. The $GENERATE code relies on surfacing errors from
the reader.

* Support $INCLUDE in NewRR and ReadRR

Removing $INCLUDE support from these is a breaking change and should
not be included in this pull request.

* Add test to ensure $GENERATE respects $INCLUDE support

* Unify TestZoneParserIncludeDisallowed with other tests

* Remove stray whitespace from TestGenerateSurfacesErrors

* Move ZoneParser SetX methods above Err method

* $GENERATE should not accept step of 0

If step is allowed to be 0, then generateReader (and the code it
replaced) will get stuck in an infinite loop.

This is a potential DOS vulnerability.

* Fix ReadRR comment for file argument

I missed this previosuly. The file argument is also used to
resolve relative $INCLUDE directives.

* Prevent test panics on nil error

* Rework ZoneParser.subNext

This is slightly cleaner and will close the underlying *os.File even
if an error occurs.

* Make ZoneParser.generate call subNext

This also moves the calls to setParseError into generate.

* Report errors when parsing rest of $GENERATE directive

* Report proper error location in $GENERATE directive

This makes error messages much clearer.

* Simplify modToPrintf func

Note: When width is 0, the leading 0 of the fmt string is now excluded.
      This should not alter the formatting of numbers in anyway.

* Add comment explaining sub field

* Remove outdated error comment from generate
2018-10-20 11:47:56 +10:30
dnsutil util.TrimDomainName() fails when origin doesn't end in dot (#559) 2017-11-08 18:19:10 +00:00
vendor/golang.org/x Run dep ensure -update (#770) 2018-10-05 18:29:45 +01:00
.codecov.yml Add codecov yaml to not fail the build (#600) 2017-12-06 10:36:23 +00:00
.gitignore gitignore update 2013-10-15 13:22:35 +00:00
.travis.yml Test coverage for all packages and merge Travis builds (#781) 2018-10-09 18:46:58 +01:00
AUTHORS add AUTHOR file 2014-02-10 17:15:49 +00:00
CONTRIBUTORS Correctly set the Source IP to the received Destination IP (#524) 2017-09-29 10:38:26 +01:00
COPYRIGHT Remove all copyright notices 2014-09-11 20:57:37 +01:00
Gopkg.lock Run dep ensure -update (#770) 2018-10-05 18:29:45 +01:00
Gopkg.toml Run dep ensure -update (#770) 2018-10-05 18:29:45 +01:00
LICENSE Update the copy right for the original GO files 2013-05-12 16:07:06 +02:00
Makefile.fuzz Add fuzzing framework (#580) 2017-11-23 09:34:31 +00:00
Makefile.release Release: plain push is also needed 2018-01-10 08:17:22 +00:00
README.md add user link (#790) 2018-10-12 22:15:26 +01:00
client.go Fix dominikh/go-tools nits (#758) 2018-09-27 04:02:05 +09:30
client_test.go Fix race on loop variable in TestConcurrentExchanges (#773) 2018-10-04 07:24:09 +01:00
clientconfig.go fix: panicing on options parsing. (#642) 2018-03-01 06:22:09 -08:00
clientconfig_test.go fix: panicing on options parsing. (#642) 2018-03-01 06:22:09 -08:00
compress_generate.go Fixed len computation when size just goes beyond 14 bits (#668) 2018-05-16 07:20:13 +01:00
dane.go Improve performance by addressing some low hanging fruit. (#444) 2017-02-02 07:33:49 +00:00
defaults.go ClassANY: don't convert CLASS255 to ANY (#618) 2018-01-07 17:57:04 +00:00
dns.go copyHeader is redundant (#672) 2018-05-10 14:50:26 +01:00
dns_bench_test.go Tests updates (#556) 2017-11-08 10:01:19 +00:00
dns_test.go Fix dominikh/go-tools nits (#758) 2018-09-27 04:02:05 +09:30
dnssec.go Remove redundant parenthesis (#727) 2018-08-16 17:05:27 +01:00
dnssec_keygen.go 458+dep (#591) 2017-11-27 10:49:53 +00:00
dnssec_keyscan.go Add new ZoneParser API (#794) 2018-10-20 11:47:56 +10:30
dnssec_privkey.go Use RFC 8032 functions added to x/crypto/ed25519 (#715) 2018-07-25 13:01:44 +01:00
dnssec_test.go Eliminate lexer goroutines (#792) 2018-10-15 17:42:31 +10:30
doc.go ExchangeWithDialer does not exist anymore (#666) 2018-04-06 12:05:09 +01:00
duplicate.go Add IsDuplicate function (#699) 2018-06-23 09:12:31 +01:00
duplicate_generate.go Add IsDuplicate function (#699) 2018-06-23 09:12:31 +01:00
duplicate_test.go Add IsDuplicate function (#699) 2018-06-23 09:12:31 +01:00
dyn_test.go Remove all copyright notices 2014-09-11 20:57:37 +01:00
edns.go Remove redundant parenthesis (#727) 2018-08-16 17:05:27 +01:00
edns_test.go Fix EDNS Extended RCODE (#517) 2017-10-13 17:21:46 +02:00
example_test.go fix a stupid mistake "undefined: zone" 2015-09-30 09:42:47 -07:00
format.go Remove WKS support 2016-06-05 08:23:44 +01:00
fuzz.go Add fuzzing framework (#580) 2017-11-23 09:34:31 +00:00
generate.go Add new ZoneParser API (#794) 2018-10-20 11:47:56 +10:30
generate_test.go Add new ZoneParser API (#794) 2018-10-20 11:47:56 +10:30
issue_test.go Tests updates (#556) 2017-11-08 10:01:19 +00:00
labels.go Remove redundant parenthesis (#727) 2018-08-16 17:05:27 +01:00
labels_test.go Cleanup: gofmt -w -s *.go (#548) 2017-11-03 16:15:35 +00:00
leak_test.go WIP: DNS-over-HTTPS support for Client.Exchange API (#671) 2018-05-16 08:54:01 +01:00
length_test.go Fixed len computation when size just goes beyond 14 bits (#668) 2018-05-16 07:20:13 +01:00
listen_go111.go Use correct build constraints for listen_*.go files (#750) 2018-09-26 09:12:54 +01:00
listen_go_not111.go Use correct build constraints for listen_*.go files (#750) 2018-09-26 09:12:54 +01:00
msg.go Optimise sprintX functions in types.go (#757) 2018-10-06 02:06:59 +09:30
msg_generate.go Lint: use ignore-this on generated files (#596) 2017-12-06 10:02:47 +00:00
msg_helpers.go Optimise sprintX functions in types.go (#757) 2018-10-06 02:06:59 +09:30
msg_helpers_test.go Fix unpackString bug: 127 DEL is unprintable (#755) 2018-09-27 07:47:48 +01:00
msg_test.go NSEC type bitmap packing bug (#768) 2018-10-04 07:39:45 +01:00
nsecx.go NSEC/3 Cover+Match cleanup (#479) 2017-04-12 19:47:48 +01:00
nsecx_test.go Handle empty NSEC3 salt in scanner (#677) 2018-05-14 20:07:52 +01:00
parse_test.go Eliminate lexer goroutines (#792) 2018-10-15 17:42:31 +10:30
privaterr.go Eliminate lexer goroutines (#792) 2018-10-15 17:42:31 +10:30
privaterr_test.go Test: remove all Logf/Log (#547) 2017-11-03 15:50:01 +00:00
rawmsg.go Cleanup and removals (#377) 2016-06-13 19:44:38 +01:00
remote_test.go Add remote test 2015-08-22 10:50:07 +01:00
reverse.go Golint fixes (#456) 2017-02-15 20:40:16 +00:00
rr_test.go Eliminate lexer goroutines (#792) 2018-10-15 17:42:31 +10:30
sanitize.go Add IsDuplicate function (#699) 2018-06-23 09:12:31 +01:00
sanitize_test.go Tests updates (#556) 2017-11-08 10:01:19 +00:00
scan.go Add new ZoneParser API (#794) 2018-10-20 11:47:56 +10:30
scan_rr.go Eliminate lexer goroutines (#792) 2018-10-15 17:42:31 +10:30
scan_test.go Add new ZoneParser API (#794) 2018-10-20 11:47:56 +10:30
serve_mux.go Rework and optimise ServeMux (#754) 2018-09-27 07:48:02 +01:00
serve_mux_test.go Rework and optimise ServeMux (#754) 2018-09-27 07:48:02 +01:00
server.go Hold srv.lock while calling SetReadDeadline (#780) 2018-10-09 18:46:15 +01:00
server_test.go Use an atomic int32 in checkInProgressQueriesAtShutdownServer (#779) 2018-10-09 18:43:08 +01:00
sig0.go Fix dominikh/go-tools nits (#758) 2018-09-27 04:02:05 +09:30
sig0_test.go Lowercase all error msg from the tests 2015-11-26 14:12:38 +00:00
singleinflight.go Implement outstanding query detection. 2013-08-24 02:28:58 +00:00
smimea.go Golint fixes (#456) 2017-02-15 20:40:16 +00:00
tlsa.go Fix for miekg/dns issue #289: support the SMIMEA record (#410) 2016-10-17 18:09:52 +01:00
tsig.go Fix TSIG bug releated to ID substitution (#504) 2017-08-12 20:21:44 +01:00
tsig_test.go Fix TSIG bug releated to ID substitution (#504) 2017-08-12 20:21:44 +01:00
types.go Optimise sprintX functions in types.go (#757) 2018-10-06 02:06:59 +09:30
types_generate.go copyHeader is redundant (#672) 2018-05-10 14:50:26 +01:00
types_test.go Optimise sprintX functions in types.go (#757) 2018-10-06 02:06:59 +09:30
udp.go Rollback PR #738 because it breaks compatibility with Windows (#765) 2018-09-29 10:25:59 +01:00
udp_test.go Rollback PR #738 because it breaks compatibility with Windows (#765) 2018-09-29 10:25:59 +01:00
udp_windows.go Rollback PR #738 because it breaks compatibility with Windows (#765) 2018-09-29 10:25:59 +01:00
update.go An update msg can have multiple, different prerequests and also multiple, 2016-02-27 12:58:10 +01:00
update_test.go ClassANY: don't convert CLASS255 to ANY (#618) 2018-01-07 17:57:04 +00:00
version.go Release 1.0.13 2018-10-10 07:39:49 +01:00
version_test.go Add semver (#595) 2017-12-06 09:46:54 +00:00
xfr.go TSIG name must be presented in canonical form (#574) 2017-11-17 13:17:47 +00:00
zcompress.go Remove compression from AFSDB (#704) 2018-06-23 09:43:19 +01:00
zduplicate.go Add IsDuplicate function (#699) 2018-06-23 09:12:31 +01:00
zmsg.go Remove compression from AFSDB (#704) 2018-06-23 09:43:19 +01:00
ztypes.go copyHeader is redundant (#672) 2018-05-10 14:50:26 +01:00

README.md

Build Status Code Coverage Go Report Card

Alternative (more granular) approach to a DNS library

Less is more.

Complete and usable DNS library. All widely used Resource Records are supported, including the DNSSEC types. It follows a lean and mean philosophy. If there is stuff you should know as a DNS programmer there isn't a convenience function for it. Server side and client side programming is supported, i.e. you can build servers and resolvers with it.

We try to keep the "master" branch as sane as possible and at the bleeding edge of standards, avoiding breaking changes wherever reasonable. We support the last two versions of Go.

Goals

  • KISS;
  • Fast;
  • Small API. If it's easy to code in Go, don't make a function for it.

Users

A not-so-up-to-date-list-that-may-be-actually-current:

Send pull request if you want to be listed here.

Features

  • UDP/TCP queries, IPv4 and IPv6;
  • RFC 1035 zone file parsing ($INCLUDE, $ORIGIN, $TTL and $GENERATE (for all record types) are supported;
  • Fast:
    • Reply speed around ~ 80K qps (faster hardware results in more qps);
    • Parsing RRs ~ 100K RR/s, that's 5M records in about 50 seconds;
  • Server side programming (mimicking the net/http package);
  • Client side programming;
  • DNSSEC: signing, validating and key generation for DSA, RSA, ECDSA and Ed25519;
  • EDNS0, NSID, Cookies;
  • AXFR/IXFR;
  • TSIG, SIG(0);
  • DNS over TLS: optional encrypted connection between client and server;
  • DNS name compression;
  • Depends only on the standard library.

Have fun!

Miek Gieben - 2010-2012 - miek@miek.nl

Building

Building is done with the go tool. If you have setup your GOPATH correctly, the following should work:

go get github.com/miekg/dns
go build github.com/miekg/dns

Examples

A short "how to use the API" is at the beginning of doc.go (this also will show when you call godoc github.com/miekg/dns).

Example programs can be found in the github.com/miekg/exdns repository.

Supported RFCs

all of them

  • 103{4,5} - DNS standard
  • 1348 - NSAP record (removed the record)
  • 1982 - Serial Arithmetic
  • 1876 - LOC record
  • 1995 - IXFR
  • 1996 - DNS notify
  • 2136 - DNS Update (dynamic updates)
  • 2181 - RRset definition - there is no RRset type though, just []RR
  • 2537 - RSAMD5 DNS keys
  • 2065 - DNSSEC (updated in later RFCs)
  • 2671 - EDNS record
  • 2782 - SRV record
  • 2845 - TSIG record
  • 2915 - NAPTR record
  • 2929 - DNS IANA Considerations
  • 3110 - RSASHA1 DNS keys
  • 3225 - DO bit (DNSSEC OK)
  • 340{1,2,3} - NAPTR record
  • 3445 - Limiting the scope of (DNS)KEY
  • 3597 - Unknown RRs
  • 403{3,4,5} - DNSSEC + validation functions
  • 4255 - SSHFP record
  • 4343 - Case insensitivity
  • 4408 - SPF record
  • 4509 - SHA256 Hash in DS
  • 4592 - Wildcards in the DNS
  • 4635 - HMAC SHA TSIG
  • 4701 - DHCID
  • 4892 - id.server
  • 5001 - NSID
  • 5155 - NSEC3 record
  • 5205 - HIP record
  • 5702 - SHA2 in the DNS
  • 5936 - AXFR
  • 5966 - TCP implementation recommendations
  • 6605 - ECDSA
  • 6725 - IANA Registry Update
  • 6742 - ILNP DNS
  • 6840 - Clarifications and Implementation Notes for DNS Security
  • 6844 - CAA record
  • 6891 - EDNS0 update
  • 6895 - DNS IANA considerations
  • 6975 - Algorithm Understanding in DNSSEC
  • 7043 - EUI48/EUI64 records
  • 7314 - DNS (EDNS) EXPIRE Option
  • 7477 - CSYNC RR
  • 7828 - edns-tcp-keepalive EDNS0 Option
  • 7553 - URI record
  • 7858 - DNS over TLS: Initiation and Performance Considerations
  • 7871 - EDNS0 Client Subnet
  • 7873 - Domain Name System (DNS) Cookies (draft-ietf-dnsop-cookies)
  • 8080 - EdDSA for DNSSEC

Loosely based upon

  • ldns
  • NSD
  • Net::DNS
  • GRONG