net: support all PacketConn and Conn returned by Resolver.Dial

Allow the Resolver.Dial func to return instances of Conn other than
*TCPConn and *UDPConn. If the Conn is also a PacketConn, assume DNS
messages transmitted over the Conn adhere to section 4.2.1. "UDP usage".
Otherwise, follow section 4.2.2. "TCP usage".

Provides a hook mechanism so that DNS queries generated by the net
package may be answered or modified before being sent to over the
network.

Updates #19910

Change-Id: Ib089a28ad4a1848bbeaf624ae889f1e82d56655b
Reviewed-on: https://go-review.googlesource.com/45153
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Ben Burkert 2017-06-08 13:19:28 -07:00 committed by Brad Fitzpatrick
parent d55d7b9397
commit d8a7990ffa
4 changed files with 86 additions and 34 deletions

View file

@ -8,8 +8,6 @@ package net
import (
"context"
"errors"
"reflect"
"sync"
)
@ -70,12 +68,10 @@ func (r *Resolver) dial(ctx context.Context, network, server string) (dnsConn, e
if err != nil {
return nil, mapErr(err)
}
dc, ok := c.(dnsConn)
if !ok {
c.Close()
return nil, errors.New("net: Resolver.Dial returned unsupported connection type " + reflect.TypeOf(c).String())
if _, ok := c.(PacketConn); ok {
return &dnsPacketConn{c}, nil
}
return dc, nil
return &dnsStreamConn{c}, nil
}
func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string, err error) {