Commit Graph

108 Commits

Author SHA1 Message Date
Miek Gieben ab67d69d9b review
Signed-off-by: Miek Gieben <miek@miek.nl>
2018-11-28 22:25:28 +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
Tom Thorogood ec3443f85d Fix TCP connection tracking memory leak (#808)
* Add test that srv.conns is empty in checkInProgressQueriesAtShutdownServer

* Track ResponseWriter Close without nil-ing tcp

* Remove LocalAddr and RemoteAddr panic after Close

This is no longer needed as the tcp field is no longer set to nil in
Close.

* Add more explicit WriteMsg panic after Close

Previously this would panic with `dns: Write called after Close` which
is obviously less clear.

* Panic if Hijack is called after Close

Previously this worked, but later calls to Write would panic. This is
more explicit.

* Return an error if Close called multiple times

Neither io.Closer, nor ResponseWriter, provide any guarantees about the
behaviour of multiple calls to Close. This was made explicit in
https://golang.org/cl/8575043 and in practice implementations differ
wildly.

This matches ShutdownContext which returns an error if called multiple
times.

* Check map len under lock in checkInProgressQueriesAtShutdownServer

* Correct error message in checkInProgressQueriesAtShutdownServer

* Remove panic-after-Close from Hijack

* Return errors, not panic, on Write after Close
2018-11-03 09:44:07 +00:00
Tom Thorogood e6cede5dc8 Use an atomic int32 in checkInProgressQueriesAtShutdownServer (#779) 2018-10-09 18:43:08 +01:00
Tom Thorogood 39265ac07f Prevent a checkInProgressQueriesAtShutdownServer panic (#778)
* Prevent a checkInProgressQueriesAtShutdownServer panic

* Fix typo in comment
2018-10-09 18:41:42 +01:00
Tom Thorogood ac339476d7 Remove RunLocalUDPServerUnsafe test method (#777)
Instead of having a separate RunLocalUDPServerUnsafe, we can use the
functional options added to RunLocalUDPServerWithFinChan in ab16005053.
2018-10-09 18:41:23 +01:00
Tom Thorogood 008c8ca764 Explicitly panic after (*response).Close (#769)
* Explicitly panic after (*response).Close

* Prefix panics with package name

* Harden TestResponseAfterClose by comparing panic message
2018-10-04 07:39:21 +01:00
Tom Thorogood 7482521355 Replace the trigger type with chan in server_test.go (#760)
* Replace the trigger type with chan in server_test.go

This was a lot of code to do very little.

* Check the error from ActivateAndServe in TestHandlerCloseTCP

May as well add this missing error check in while we're here.
2018-09-26 21:04:11 +01:00
Tom Thorogood ead9678cbc
Run go fmt on package (#759)
This is go fmt from go1.11 and so it picks up the new map formatting
heuristic.

See golang/go@542ea5ad91.
2018-09-27 03:06:02 +09:30
Tom Thorogood 60d113313c Move ServeMux into seperate file (#753)
This reduces the clutter in server.go.
2018-09-26 10:20:48 +01:00
Daniel Selifonov ab16005053 Bugfix for miekg/dns#748 (#749)
* Bugfix for miekg/dns#748

w.msg was being prematurely cleared prior to use by TsigVerify

* Modified patch after feedback from tmthrgd

Added a disposeBuffer method to the server that's passed a response. This wipes the reference to and frees the buffer used to store the message after TSIG validation has occured, not before. Since the pool is an attribute of the server (and the logic refers to a server UDPSize attribute), it made sense to make this a function of the server rather than a function of the response.

* Added TestServerRoundtripTsig to server_test.go

This test generates a TSIG signed query, and makes sure that server TSIG validation does not produce an error.

* Fixed data races introduced by TestServerRoundtripTsig

* Simplified error signalling in TestServerRoundtripTsig

* RunLocalUDPServerWithFinChan variadic closure argument added

This (clever hack suggested by tmthrgd) allows specifying field values (like TsigSecret) on Server instances at test time without making the race detector grouchy, but is backwards compatible with existing invocations of RunLocalUDPServerWithFinChan.
2018-09-26 09:19:35 +01:00
chantra 833bf76c28 [tls] Carry TLS state within (possibly) response writer (#728)
* [tls] Carry TLS state within (possibly) response writer

This allows a server to make decision wether or not the link used to
connect to the DNS server is using TLS.
This can be used by the handler for instance to (but not limited to):
- log that the request was TLS vs TCP
- craft specific responsed knowing that the link is secured
- return custom answers based on client cert (if provided)
...

Fixes #711

* Address @tmthrgd comments:
- do not check whether w.tcp is nil
- create RR after setting txt value

* Address @miekg comments.

Attempt to make a TLS connection state specific test, it goes over
testing each individual server types (TLS, TCP, UDP) and validate that
tls.Connectionstate is only accessible when expected.

* ConnectionState() returns value instead of pointer

* * make ConnectionStater.ConnectionState() return a pointer again
* rename interface ConnectionState to ConnectionStater
* fix nits pointed by @tmthrgd

* @tmthrgd comment: Do not use concret type in `ConnectionState`
2018-09-22 18:34:55 +01:00
Tom Thorogood b0dc93d276
Make Shutdown wait for connections to terminate gracefully (#717)
* Make Shutdown wait for connections to terminate gracefully

* Add graceful shutdown test files from #713

* Tidy up graceful shutdown tests

* Call t.Error directly in checkInProgressQueriesAtShutdownServer

* Remove timeout arguments from RunLocal*ServerWithFinChan

* Merge defers together in (*Server).serve

This removes the defer from the UDP path, in favour of directly
calling (*sync.WaitGroup).Done after (*Serve).serveDNS has
returned.

* Replace checkInProgressQueriesAtShutdownServer implementation

This performs dialing, writing and reading as three seperate steps.

* Add sleep after writing shutdown test messages

* Avoid race condition when setting server timeouts

Server timeouts cannot be set after the server has started without
triggering the race detector. The timeout's are not strictly needed, so
remove them.

* Use a sync.Cond for testShutdownNotify

Using a chan erroneously triggered the race detector, using a sync.Cond
avoids that problem.

* Remove TestShutdownUDPWithContext

This doesn't really add anything.

* Move shutdown and conn into (*Server).init

* Only log ResponseWriter.WriteMsg error once

* Test that ShutdownContext waits for the reply

* Remove stray newline from diff

* Rename err to ctxErr in ShutdownContext

* Reword testShutdownNotify comment
2018-09-13 23:06:28 +09:30
Tom Thorogood e875a31a5c
Add SO_REUSEPORT support (#736)
* Use strings.TrimSuffix in ListenAndServe for TLS

This replaces the if/else statements with something simpler.

Interestingly, the first pull request I submitted to this library was
to fix the tcp6-tls case way back in 4744e915eb.

* Add SO_REUSEPORT implementation

Fixes #654

* Rename Reuseport field to ReusePort

* Rename supportsReuseport to match ReusePort

* Rename listenUDP and listenTCP file to listen_*.go
2018-09-10 20:12:54 +09:30
Tom Thorogood 8f0a42efa0 Fix TestServerStartStopRace calling t.Fatal on wrong goroutine (#739) 2018-09-09 20:47:16 +01:00
Tom Thorogood 3ce7efeace Fix Serve benchmark failures (#734)
* Fix Serve benchmark failures

At present, these benchmarks don't actually work or measure anything.
SetQuestion must have a fully qualified domain name (trailing dot) to
be valid. Because the question wasn't valid, the request never reached
the server and was rejected by the client.

With the error check added, the benchmarks started failing with:
--- FAIL: BenchmarkServe
    server_test.go:346: Exchange failed: dns: domain must be fully qualified

* Enable Serve6 benchmark

Currently this benchmark isn't run as it's not exported.

* Only enable BenchmarkServe6 when IPv6 is supported

The Serve6 benchmark has been disabled since 2014 (in 28d936c032)
because it doesn't play nice with Travis. We can just skip the benchmark
if it fails to bind to an IPv6 address.
2018-09-08 17:10:56 +01:00
Tom Thorogood 4d25966dce Test that Shutdown does not surface closed errors (#624)
* Test that Shutdown does not surface closed errors

This test checks that calling Shutdown does not cause ActivateAndServe
(via serveTCP and serveUDP) to return the underlying
'use of closed network connection' error.

This commit unifies TestShutdownTCP with TestShutdownUDP. After this
commit, both tests will check that ActivateAndServe returns a nil error
and that Shutdown succeeded.

This was previously broken for serveTCP.

* Add comment explaining why fin chan is buffered
2018-02-27 17:38:12 -08:00
Miek Gieben 2a67631d76
cleanup: remove debug.Printf from scanner (#573)
Remove the debug.Printf stuff from scanner and some other style nits.
2017-11-17 10:48:42 +00:00
Miek Gieben 9fc4eb252e
Server: drop inflight waitgroup (#561)
* Server: drop inflight waitgroup

This drops the waitgroup in Server, the suspicion is this can make the server
fail to stop; doing this make graceful shutdown not work.

Add test that tries to find a race between starting on stopping race;
there was a data race on srv.Inflight.

The coredns' TestReadme doesn't race anymore with this as it did with
the more evasive PR #546.
2017-11-10 10:33:17 +00:00
Miek Gieben 9cfd42f1df
Tests: add ListenAndServe tests (#562)
This increases the test coverage as these methods where not tested.
Add some cosmetic changes to the mix.
2017-11-10 10:11:23 +00:00
Miek Gieben 388f6eea29
Tests updates (#556)
Use :0 for loopback testing. This is more portable between testing environments.
Add testRR that calls NewRR and throws error away - apply it everywhere where needed.

It seems only Go 1.9 can deal with :0 being used. Disable 1.8 in travis.
2017-11-08 10:01:19 +00:00
Miek Gieben 348c84f37e
Test: remove all Logf/Log (#547)
Move some of them to Errorf and friends, but most of them are just
gone: This make go test -v actually readable.

Remove a bunch of test that used ipv6 on localhost as this does not work
on Travis.
2017-11-03 15:50:01 +00:00
insomniac aade52d68e Redesigned Client API around net.Dialer (#511)
* Redesigned Client API around net.Dialer

* Moved Dialer under Client; reverted msgHash; removed unneeded deprecation

* Reverted client_compat.go into client.go and renamed dialer variable

* Shortened comment lines

* Erroneously removed pointer declaration
2017-09-29 10:52:01 +01:00
Miek Gieben 5a15a35f5f Golint fixes (#456)
Fixes #370
2017-02-15 20:40:16 +00:00
Diep Pham 78bb99c423 adds tests for shutdown bind port (#400)
* adds tests for shutdown bind port

* closes file descriptors in for udp on linux

* adds sleep after shutdown in test

* minimizes defer usages

* reduces sleep time

* replaces sleep with notify channel

after starting DNS server
2016-12-15 16:20:35 +00:00
Rafael Dantas Justo 6fe70412bc Add option in server to allow DNS over TLS
We should allow the server to receive requests of an encrypted connection. This
is proposed on the document draft-ietf-dprive-dns-over-tls [1].

Now it is possible to initialize the DNS server to listen with TLS using
"tcp-tls" value in network parameter of ListenAndServe function, or passing a
listener initialized with tls.Listen to ActivateAndServe.

There's also an option in Server type to change the TLS confirguration, to
inform the certificates that are going to be used, or to change any other
desired option of tls.Config.

See #297

[1] http://tools.ietf.org/html/draft-ietf-dprive-dns-over-tls-02
2016-01-08 11:26:13 -02:00
Ryan Leavengood 7b51cba64a Set UDP conn to non-blocking on Linux to fix Shutdown()
The call to conn.File() causes Go to call dup() and then set the resulting FD
to be blocking. This sets the FD back to non-blocking, allowing Shutdown() to
work properly.

Fixes #279.
2015-12-01 17:15:42 -05:00
Miek Gieben f520760857 Lowercase all error msg from the tests 2015-11-26 14:12:38 +00:00
Miek Gieben 9e23f0c96e Fix data race 2015-11-26 14:03:51 +00:00
Andrew Tunnell-Jones cfa72d0862 Test closing TCP ResponseWriter in a handler 2015-11-26 08:04:38 +00:00
Filippo Valsorda e6885e3677 Set high timeouts in server_test.go not to fail on slow boxes and catch deadlocks 2015-10-07 00:13:40 +01:00
Miek Gieben 9bf52083d1 golint fixes 2015-08-23 08:03:13 +01:00
James DeFelice 8255b4a03f update docs; replace "data frame" refs with "raw message" 2015-08-06 17:55:37 -04:00
James DeFelice e148c23156 add docs and example for DNS frame read/write decorators 2015-08-04 09:04:40 -04:00
Michael Haro dddcd696ba Check that the query ID matches the answer ID.
Reduce some code duplication by making Exchange() use Client.Exchange().

When performing an Exchange if the query ID does not match the answer ID
return an error.  Also add a test for this condition.
2015-05-05 23:00:16 -07:00
Michael Haro 7f051930ff Use %v as the format arg for errors 2015-02-26 00:49:59 -08:00
Michael Haro 2fb2a25e84 More test clean up
Remove trailing \n from t.Log and t.Error messages as it's unnecessary.

In some instances, combine multiple t.Error()s into one

To provide more consistency across the tests, rename e to err and use %v
as the format arg for errors.

Replace Logf and Errorf with Log and Error when it made sense.  For
example t.Errorf("%v", err) to t.Error(err)
2015-02-25 22:14:21 -08:00
Michael Haro f995f1aff3 Convert tests from being t.Log(..) then t.Fail() to just t.Error(...) as
t.Error(...) does both and makes it more clear which messages are errors
vs information log messages.
2015-02-23 17:43:07 -08:00
Michael Haro 9e96f28311 Fix problems noted by 'go vet' 2015-02-19 00:08:28 -08:00
Filippo Valsorda 17a90cfd68 Tests: fix TestShutdownTCP race using NotifyStartedFunc 2014-12-17 13:46:50 +00:00
Filippo Valsorda e613ea6d07 Tests: silence a data race detected during server tests 2014-12-06 10:27:29 +00:00
Filippo Valsorda ddb0b0f565 Tests: wait for the server to be started before proceeding 2014-12-06 10:27:29 +00:00
Miek Gieben cd2048a15a Add Unsafe member to dns.Server
This stops it from checking if the incoming requests have the QR bit
unset, so be careful when enabling this. This can be useful in
combination with mDNS.

Also the check for only 1 question in the question section is relaxed
to be "at least one", even without setting Unsafe!

Also update TestServingResponse to test for Unsafe vs not using Unsafe.
2014-11-19 16:34:18 +00:00
Miek Gieben 5fc7e36983 go test -short
Added a bunch a long running test function to the list of skipped
tests when giving -short to go test. Tests are bascially *all*
DNSSEC key generation tests and 1 serving test.

PASS
ok      github.com/miekg/dns    0.782s

Compared to 13+ s, so quite a bit faster.
2014-11-12 10:19:20 +00:00
Miek Gieben d33af8db79 Update test and gofmt 2014-11-09 16:09:49 +00:00
Miek Gieben d8da4d2fa7 Fix: drop requests with *cough* qr set.
Err, yeah.
2014-10-19 09:36:26 +00:00
Miek Gieben 0aa3021a83 Remove all copyright notices
Use the central COPYRIGHT file.
2014-09-11 20:57:37 +01:00
Alex Sergeyev 3489112d72 Addressed #115 (default mux should be cleaned after tests) 2014-08-30 09:45:30 -04:00
Alex Sergeyev de77e9d14d Removed Goshed from RunXXXTestServer and...
only used it for shutdown tests where it makes sense.
2014-08-30 09:35:54 -04:00
Alex Sergeyev c618657d9b Dealt with race condition detector warnings:
* stopped obvious race condition with replacing handler in
  ServingLargeResponses test
* lowered probability of other race conditions with test code
  while test server is yet activating
* fixed errmessage in Shutdown
2014-08-29 14:10:05 -04:00