2015-12-24 04:51:32 -05:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-02-21 10:39:53 -05:00
|
|
|
"math/rand"
|
2015-12-24 04:51:32 -05:00
|
|
|
"net"
|
|
|
|
"strings"
|
2016-01-24 00:47:57 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
2015-12-24 04:51:32 -05:00
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-02-26 17:58:11 -05:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-12-24 04:51:32 -05:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Resolver represents the embedded DNS server in Docker. It operates
|
|
|
|
// by listening on container's loopback interface for DNS queries.
|
|
|
|
type Resolver interface {
|
|
|
|
// Start starts the name server for the container
|
|
|
|
Start() error
|
2016-01-14 12:56:32 -05:00
|
|
|
// Stop stops the name server for the container. Stopped resolver
|
|
|
|
// can be reused after running the SetupFunc again.
|
2015-12-24 04:51:32 -05:00
|
|
|
Stop()
|
|
|
|
// SetupFunc() provides the setup function that should be run
|
|
|
|
// in the container's network namespace.
|
|
|
|
SetupFunc() func()
|
|
|
|
// NameServer() returns the IP of the DNS resolver for the
|
|
|
|
// containers.
|
|
|
|
NameServer() string
|
2016-03-20 08:00:58 -04:00
|
|
|
// SetExtServers configures the external nameservers the resolver
|
|
|
|
// should use to forward queries
|
2015-12-24 04:51:32 -05:00
|
|
|
SetExtServers([]string)
|
2016-03-20 08:00:58 -04:00
|
|
|
// FlushExtServers clears the cached UDP connections to external
|
|
|
|
// nameservers
|
|
|
|
FlushExtServers()
|
2015-12-24 04:51:32 -05:00
|
|
|
// ResolverOptions returns resolv.conf options that should be set
|
|
|
|
ResolverOptions() []string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2016-02-21 10:39:53 -05:00
|
|
|
resolverIP = "127.0.0.11"
|
|
|
|
dnsPort = "53"
|
|
|
|
ptrIPv4domain = ".in-addr.arpa."
|
|
|
|
ptrIPv6domain = ".ip6.arpa."
|
|
|
|
respTTL = 600
|
|
|
|
maxExtDNS = 3 //max number of external servers to try
|
2016-04-07 10:32:43 -04:00
|
|
|
extIOTimeout = 4 * time.Second
|
2016-02-21 10:39:53 -05:00
|
|
|
defaultRespSize = 512
|
2016-04-18 22:55:19 -04:00
|
|
|
maxConcurrent = 100
|
2016-03-28 19:23:08 -04:00
|
|
|
logInterval = 2 * time.Second
|
2016-04-01 12:45:37 -04:00
|
|
|
maxDNSID = 65536
|
2015-12-24 04:51:32 -05:00
|
|
|
)
|
|
|
|
|
2016-04-01 12:45:37 -04:00
|
|
|
type clientConn struct {
|
|
|
|
dnsID uint16
|
|
|
|
respWriter dns.ResponseWriter
|
|
|
|
}
|
|
|
|
|
2016-01-24 00:47:57 -05:00
|
|
|
type extDNSEntry struct {
|
|
|
|
ipStr string
|
|
|
|
extConn net.Conn
|
|
|
|
extOnce sync.Once
|
|
|
|
}
|
|
|
|
|
2016-06-14 01:56:59 -04:00
|
|
|
type sboxQuery struct {
|
|
|
|
sboxID string
|
|
|
|
dnsID uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientConnGC struct {
|
|
|
|
toDelete bool
|
|
|
|
client clientConn
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
queryGCMutex sync.Mutex
|
|
|
|
queryGC map[sboxQuery]*clientConnGC
|
|
|
|
)
|
|
|
|
|
2015-12-24 04:51:32 -05:00
|
|
|
// resolver implements the Resolver interface
|
|
|
|
type resolver struct {
|
2016-01-24 00:47:57 -05:00
|
|
|
sb *sandbox
|
|
|
|
extDNSList [maxExtDNS]extDNSEntry
|
|
|
|
server *dns.Server
|
|
|
|
conn *net.UDPConn
|
|
|
|
tcpServer *dns.Server
|
|
|
|
tcpListen *net.TCPListener
|
|
|
|
err error
|
2016-03-28 19:23:08 -04:00
|
|
|
count int32
|
|
|
|
tStamp time.Time
|
|
|
|
queryLock sync.Mutex
|
2016-04-01 12:45:37 -04:00
|
|
|
client map[uint16]clientConn
|
2015-12-24 04:51:32 -05:00
|
|
|
}
|
|
|
|
|
2016-02-21 10:39:53 -05:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().Unix())
|
2016-06-14 01:56:59 -04:00
|
|
|
queryGC = make(map[sboxQuery]*clientConnGC)
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(1 * time.Minute)
|
|
|
|
for range ticker.C {
|
|
|
|
queryGCMutex.Lock()
|
|
|
|
for query, conn := range queryGC {
|
|
|
|
if !conn.toDelete {
|
|
|
|
conn.toDelete = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(queryGC, query)
|
|
|
|
}
|
|
|
|
queryGCMutex.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
2016-02-21 10:39:53 -05:00
|
|
|
}
|
|
|
|
|
2015-12-24 04:51:32 -05:00
|
|
|
// NewResolver creates a new instance of the Resolver
|
|
|
|
func NewResolver(sb *sandbox) Resolver {
|
|
|
|
return &resolver{
|
2016-04-01 12:45:37 -04:00
|
|
|
sb: sb,
|
|
|
|
err: fmt.Errorf("setup not done yet"),
|
|
|
|
client: make(map[uint16]clientConn),
|
2015-12-24 04:51:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolver) SetupFunc() func() {
|
|
|
|
return (func() {
|
|
|
|
var err error
|
|
|
|
|
2016-01-22 00:23:30 -05:00
|
|
|
// DNS operates primarily on UDP
|
2015-12-24 04:51:32 -05:00
|
|
|
addr := &net.UDPAddr{
|
|
|
|
IP: net.ParseIP(resolverIP),
|
|
|
|
}
|
|
|
|
|
|
|
|
r.conn, err = net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
r.err = fmt.Errorf("error in opening name server socket %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-22 00:23:30 -05:00
|
|
|
// Listen on a TCP as well
|
|
|
|
tcpaddr := &net.TCPAddr{
|
|
|
|
IP: net.ParseIP(resolverIP),
|
|
|
|
}
|
|
|
|
|
|
|
|
r.tcpListen, err = net.ListenTCP("tcp", tcpaddr)
|
|
|
|
if err != nil {
|
|
|
|
r.err = fmt.Errorf("error in opening name TCP server socket %v", err)
|
|
|
|
return
|
|
|
|
}
|
2015-12-24 04:51:32 -05:00
|
|
|
r.err = nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolver) Start() error {
|
|
|
|
// make sure the resolver has been setup before starting
|
|
|
|
if r.err != nil {
|
|
|
|
return r.err
|
|
|
|
}
|
2016-04-13 04:28:18 -04:00
|
|
|
|
|
|
|
if err := r.setupIPTable(); err != nil {
|
|
|
|
return fmt.Errorf("setting up IP table rules failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-12-24 04:51:32 -05:00
|
|
|
s := &dns.Server{Handler: r, PacketConn: r.conn}
|
|
|
|
r.server = s
|
|
|
|
go func() {
|
|
|
|
s.ActivateAndServe()
|
|
|
|
}()
|
2016-01-22 00:23:30 -05:00
|
|
|
|
|
|
|
tcpServer := &dns.Server{Handler: r, Listener: r.tcpListen}
|
|
|
|
r.tcpServer = tcpServer
|
|
|
|
go func() {
|
|
|
|
tcpServer.ActivateAndServe()
|
|
|
|
}()
|
2015-12-24 04:51:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-20 08:00:58 -04:00
|
|
|
func (r *resolver) FlushExtServers() {
|
2016-03-14 20:21:41 -04:00
|
|
|
for i := 0; i < maxExtDNS; i++ {
|
2016-04-10 04:26:00 -04:00
|
|
|
if r.extDNSList[i].extConn != nil {
|
|
|
|
r.extDNSList[i].extConn.Close()
|
|
|
|
}
|
|
|
|
|
2016-03-14 20:21:41 -04:00
|
|
|
r.extDNSList[i].extConn = nil
|
|
|
|
r.extDNSList[i].extOnce = sync.Once{}
|
|
|
|
}
|
2016-03-20 08:00:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolver) Stop() {
|
|
|
|
r.FlushExtServers()
|
2016-03-14 20:21:41 -04:00
|
|
|
|
2015-12-24 04:51:32 -05:00
|
|
|
if r.server != nil {
|
|
|
|
r.server.Shutdown()
|
|
|
|
}
|
2016-01-22 00:23:30 -05:00
|
|
|
if r.tcpServer != nil {
|
|
|
|
r.tcpServer.Shutdown()
|
|
|
|
}
|
2016-01-14 12:56:32 -05:00
|
|
|
r.conn = nil
|
2016-01-22 00:23:30 -05:00
|
|
|
r.tcpServer = nil
|
2016-01-14 12:56:32 -05:00
|
|
|
r.err = fmt.Errorf("setup not done yet")
|
2016-03-28 19:23:08 -04:00
|
|
|
r.tStamp = time.Time{}
|
|
|
|
r.count = 0
|
|
|
|
r.queryLock = sync.Mutex{}
|
2015-12-24 04:51:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolver) SetExtServers(dns []string) {
|
2016-01-24 00:47:57 -05:00
|
|
|
l := len(dns)
|
|
|
|
if l > maxExtDNS {
|
|
|
|
l = maxExtDNS
|
|
|
|
}
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
r.extDNSList[i].ipStr = dns[i]
|
|
|
|
}
|
2015-12-24 04:51:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolver) NameServer() string {
|
|
|
|
return resolverIP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolver) ResolverOptions() []string {
|
|
|
|
return []string{"ndots:0"}
|
|
|
|
}
|
|
|
|
|
2016-01-23 00:22:58 -05:00
|
|
|
func setCommonFlags(msg *dns.Msg) {
|
|
|
|
msg.RecursionAvailable = true
|
|
|
|
}
|
|
|
|
|
2016-02-21 10:39:53 -05:00
|
|
|
func shuffleAddr(addr []net.IP) []net.IP {
|
|
|
|
for i := len(addr) - 1; i > 0; i-- {
|
|
|
|
r := rand.Intn(i + 1)
|
|
|
|
addr[i], addr[r] = addr[r], addr[i]
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2016-03-19 06:07:08 -04:00
|
|
|
func createRespMsg(query *dns.Msg) *dns.Msg {
|
|
|
|
resp := new(dns.Msg)
|
|
|
|
resp.SetReply(query)
|
|
|
|
setCommonFlags(resp)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:20:31 -05:00
|
|
|
func (r *resolver) handleIPQuery(name string, query *dns.Msg, ipType int) (*dns.Msg, error) {
|
2016-03-19 06:07:08 -04:00
|
|
|
addr, ipv6Miss := r.sb.ResolveName(name, ipType)
|
|
|
|
if addr == nil && ipv6Miss {
|
|
|
|
// Send a reply without any Answer sections
|
|
|
|
log.Debugf("Lookup name %s present without IPv6 address", name)
|
|
|
|
resp := createRespMsg(query)
|
|
|
|
return resp, nil
|
|
|
|
}
|
2015-12-24 04:51:32 -05:00
|
|
|
if addr == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-02-21 10:39:53 -05:00
|
|
|
log.Debugf("Lookup for %s: IP %v", name, addr)
|
2015-12-24 04:51:32 -05:00
|
|
|
|
2016-03-19 06:07:08 -04:00
|
|
|
resp := createRespMsg(query)
|
2016-02-21 10:39:53 -05:00
|
|
|
if len(addr) > 1 {
|
|
|
|
addr = shuffleAddr(addr)
|
|
|
|
}
|
2016-02-26 17:58:11 -05:00
|
|
|
if ipType == types.IPv4 {
|
2016-01-12 23:20:31 -05:00
|
|
|
for _, ip := range addr {
|
|
|
|
rr := new(dns.A)
|
|
|
|
rr.Hdr = dns.RR_Header{Name: name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: respTTL}
|
|
|
|
rr.A = ip
|
|
|
|
resp.Answer = append(resp.Answer, rr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, ip := range addr {
|
|
|
|
rr := new(dns.AAAA)
|
|
|
|
rr.Hdr = dns.RR_Header{Name: name, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: respTTL}
|
|
|
|
rr.AAAA = ip
|
|
|
|
resp.Answer = append(resp.Answer, rr)
|
|
|
|
}
|
2016-02-21 10:39:53 -05:00
|
|
|
}
|
2015-12-24 04:51:32 -05:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolver) handlePTRQuery(ptr string, query *dns.Msg) (*dns.Msg, error) {
|
|
|
|
parts := []string{}
|
|
|
|
|
|
|
|
if strings.HasSuffix(ptr, ptrIPv4domain) {
|
|
|
|
parts = strings.Split(ptr, ptrIPv4domain)
|
|
|
|
} else if strings.HasSuffix(ptr, ptrIPv6domain) {
|
|
|
|
parts = strings.Split(ptr, ptrIPv6domain)
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("invalid PTR query, %v", ptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
host := r.sb.ResolveIP(parts[0])
|
|
|
|
if len(host) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Lookup for IP %s: name %s", parts[0], host)
|
|
|
|
fqdn := dns.Fqdn(host)
|
|
|
|
|
|
|
|
resp := new(dns.Msg)
|
|
|
|
resp.SetReply(query)
|
2016-01-23 00:22:58 -05:00
|
|
|
setCommonFlags(resp)
|
2015-12-24 04:51:32 -05:00
|
|
|
|
|
|
|
rr := new(dns.PTR)
|
|
|
|
rr.Hdr = dns.RR_Header{Name: ptr, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: respTTL}
|
|
|
|
rr.Ptr = fqdn
|
|
|
|
resp.Answer = append(resp.Answer, rr)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2016-05-08 03:48:04 -04:00
|
|
|
func (r *resolver) handleSRVQuery(svc string, query *dns.Msg) (*dns.Msg, error) {
|
|
|
|
srv, ip, err := r.sb.ResolveService(svc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(srv) != len(ip) {
|
|
|
|
return nil, fmt.Errorf("invalid reply for SRV query %s", svc)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := createRespMsg(query)
|
|
|
|
|
|
|
|
for i, r := range srv {
|
|
|
|
rr := new(dns.SRV)
|
|
|
|
rr.Hdr = dns.RR_Header{Name: svc, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: respTTL}
|
|
|
|
rr.Port = r.Port
|
|
|
|
rr.Target = r.Target
|
|
|
|
resp.Answer = append(resp.Answer, rr)
|
|
|
|
|
|
|
|
rr1 := new(dns.A)
|
|
|
|
rr1.Hdr = dns.RR_Header{Name: r.Target, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: respTTL}
|
|
|
|
rr1.A = ip[i]
|
|
|
|
resp.Extra = append(resp.Extra, rr1)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-21 10:39:53 -05:00
|
|
|
func truncateResp(resp *dns.Msg, maxSize int, isTCP bool) {
|
|
|
|
if !isTCP {
|
|
|
|
resp.Truncated = true
|
|
|
|
}
|
|
|
|
|
2016-05-08 03:48:04 -04:00
|
|
|
srv := resp.Question[0].Qtype == dns.TypeSRV
|
2016-02-21 10:39:53 -05:00
|
|
|
// trim the Answer RRs one by one till the whole message fits
|
|
|
|
// within the reply size
|
|
|
|
for resp.Len() > maxSize {
|
|
|
|
resp.Answer = resp.Answer[:len(resp.Answer)-1]
|
2016-05-08 03:48:04 -04:00
|
|
|
|
|
|
|
if srv && len(resp.Extra) > 0 {
|
|
|
|
resp.Extra = resp.Extra[:len(resp.Extra)-1]
|
|
|
|
}
|
2016-02-21 10:39:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 04:51:32 -05:00
|
|
|
func (r *resolver) ServeDNS(w dns.ResponseWriter, query *dns.Msg) {
|
|
|
|
var (
|
2016-01-24 00:47:57 -05:00
|
|
|
extConn net.Conn
|
|
|
|
resp *dns.Msg
|
|
|
|
err error
|
2016-04-16 02:12:27 -04:00
|
|
|
writer dns.ResponseWriter
|
2015-12-24 04:51:32 -05:00
|
|
|
)
|
|
|
|
|
2016-02-16 01:01:29 -05:00
|
|
|
if query == nil || len(query.Question) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2015-12-24 04:51:32 -05:00
|
|
|
name := query.Question[0].Name
|
2016-05-08 03:48:04 -04:00
|
|
|
|
|
|
|
switch query.Question[0].Qtype {
|
|
|
|
case dns.TypeA:
|
2016-02-26 17:58:11 -05:00
|
|
|
resp, err = r.handleIPQuery(name, query, types.IPv4)
|
2016-05-08 03:48:04 -04:00
|
|
|
case dns.TypeAAAA:
|
2016-02-26 17:58:11 -05:00
|
|
|
resp, err = r.handleIPQuery(name, query, types.IPv6)
|
2016-05-08 03:48:04 -04:00
|
|
|
case dns.TypePTR:
|
2015-12-24 04:51:32 -05:00
|
|
|
resp, err = r.handlePTRQuery(name, query)
|
2016-05-08 03:48:04 -04:00
|
|
|
case dns.TypeSRV:
|
|
|
|
resp, err = r.handleSRVQuery(name, query)
|
2015-12-24 04:51:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-24 00:47:57 -05:00
|
|
|
proto := w.LocalAddr().Network()
|
2016-02-21 10:39:53 -05:00
|
|
|
maxSize := 0
|
|
|
|
if proto == "tcp" {
|
|
|
|
maxSize = dns.MaxMsgSize - 1
|
|
|
|
} else if proto == "udp" {
|
|
|
|
optRR := query.IsEdns0()
|
|
|
|
if optRR != nil {
|
|
|
|
maxSize = int(optRR.UDPSize())
|
|
|
|
}
|
|
|
|
if maxSize < defaultRespSize {
|
|
|
|
maxSize = defaultRespSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp != nil {
|
|
|
|
if resp.Len() > maxSize {
|
|
|
|
truncateResp(resp, maxSize, proto == "tcp")
|
|
|
|
}
|
2016-04-16 02:12:27 -04:00
|
|
|
writer = w
|
2016-02-21 10:39:53 -05:00
|
|
|
} else {
|
2016-04-16 02:12:27 -04:00
|
|
|
queryID := query.Id
|
2016-06-14 01:56:59 -04:00
|
|
|
extQueryLoop:
|
2016-01-24 00:47:57 -05:00
|
|
|
for i := 0; i < maxExtDNS; i++ {
|
|
|
|
extDNS := &r.extDNSList[i]
|
|
|
|
if extDNS.ipStr == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
extConnect := func() {
|
|
|
|
addr := fmt.Sprintf("%s:%d", extDNS.ipStr, 53)
|
|
|
|
extConn, err = net.DialTimeout(proto, addr, extIOTimeout)
|
|
|
|
}
|
2015-12-24 04:51:32 -05:00
|
|
|
|
2016-01-24 00:47:57 -05:00
|
|
|
// For udp clients connection is persisted to reuse for further queries.
|
|
|
|
// Accessing extDNS.extConn be a race here between go rouines. Hence the
|
|
|
|
// connection setup is done in a Once block and fetch the extConn again
|
|
|
|
extConn = extDNS.extConn
|
|
|
|
if extConn == nil || proto == "tcp" {
|
|
|
|
if proto == "udp" {
|
|
|
|
extDNS.extOnce.Do(func() {
|
|
|
|
r.sb.execFunc(extConnect)
|
|
|
|
extDNS.extConn = extConn
|
|
|
|
})
|
|
|
|
extConn = extDNS.extConn
|
|
|
|
} else {
|
|
|
|
r.sb.execFunc(extConnect)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Connect failed, %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2016-03-15 05:05:38 -04:00
|
|
|
// If two go routines are executing in parralel one will
|
|
|
|
// block on the Once.Do and in case of error connecting
|
|
|
|
// to the external server it will end up with a nil err
|
|
|
|
// but extConn also being nil.
|
|
|
|
if extConn == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-04-07 10:32:43 -04:00
|
|
|
log.Debugf("Query %s[%d] from %s, forwarding to %s:%s", name, query.Question[0].Qtype,
|
|
|
|
extConn.LocalAddr().String(), proto, extDNS.ipStr)
|
2016-01-18 07:07:19 -05:00
|
|
|
|
2016-01-24 00:47:57 -05:00
|
|
|
// Timeout has to be set for every IO operation.
|
|
|
|
extConn.SetDeadline(time.Now().Add(extIOTimeout))
|
|
|
|
co := &dns.Conn{Conn: extConn}
|
|
|
|
|
2016-04-01 12:45:37 -04:00
|
|
|
// forwardQueryStart stores required context to mux multiple client queries over
|
|
|
|
// one connection; and limits the number of outstanding concurrent queries.
|
2016-04-16 02:12:27 -04:00
|
|
|
if r.forwardQueryStart(w, query, queryID) == false {
|
2016-03-28 19:23:08 -04:00
|
|
|
old := r.tStamp
|
|
|
|
r.tStamp = time.Now()
|
|
|
|
if r.tStamp.Sub(old) > logInterval {
|
2016-04-18 22:55:19 -04:00
|
|
|
log.Errorf("More than %v concurrent queries from %s", maxConcurrent, extConn.LocalAddr().String())
|
2016-03-28 19:23:08 -04:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-24 00:47:57 -05:00
|
|
|
defer func() {
|
|
|
|
if proto == "tcp" {
|
|
|
|
co.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err = co.WriteMsg(query)
|
|
|
|
if err != nil {
|
2016-04-01 12:45:37 -04:00
|
|
|
r.forwardQueryEnd(w, query)
|
2016-01-24 00:47:57 -05:00
|
|
|
log.Debugf("Send to DNS server failed, %s", err)
|
|
|
|
continue
|
2016-01-18 07:07:19 -05:00
|
|
|
}
|
2016-06-14 01:56:59 -04:00
|
|
|
for {
|
|
|
|
// If a reply comes after a read timeout it will remain in the socket buffer
|
|
|
|
// and will be read after sending next query. To ignore such stale replies
|
|
|
|
// save the query context in a GC queue when read timesout. On the next reply
|
|
|
|
// if the context is present in the GC queue its a old reply. Ignore it and
|
|
|
|
// read again
|
|
|
|
resp, err = co.ReadMsg()
|
|
|
|
if err != nil {
|
2016-07-23 05:56:31 -04:00
|
|
|
// Truncated DNS replies should be sent to the client so that the
|
|
|
|
// client can retry over TCP
|
|
|
|
if err == dns.ErrTruncated && resp != nil {
|
|
|
|
break
|
|
|
|
}
|
2016-06-14 01:56:59 -04:00
|
|
|
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
|
|
|
|
r.addQueryToGC(w, query)
|
|
|
|
}
|
|
|
|
r.forwardQueryEnd(w, query)
|
|
|
|
log.Debugf("Read from DNS server failed, %s", err)
|
|
|
|
continue extQueryLoop
|
|
|
|
}
|
2016-01-24 00:47:57 -05:00
|
|
|
|
2016-06-14 01:56:59 -04:00
|
|
|
if !r.checkRespInGC(w, resp) {
|
|
|
|
break
|
|
|
|
}
|
2016-01-24 00:47:57 -05:00
|
|
|
}
|
2016-04-01 12:45:37 -04:00
|
|
|
// Retrieves the context for the forwarded query and returns the client connection
|
|
|
|
// to send the reply to
|
2016-04-16 02:12:27 -04:00
|
|
|
writer = r.forwardQueryEnd(w, resp)
|
|
|
|
if writer == nil {
|
2016-04-01 12:45:37 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-24 00:47:57 -05:00
|
|
|
resp.Compress = true
|
|
|
|
break
|
2016-01-18 07:07:19 -05:00
|
|
|
}
|
2016-04-16 02:12:27 -04:00
|
|
|
if resp == nil || writer == nil {
|
2015-12-24 04:51:32 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 02:12:27 -04:00
|
|
|
if writer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = writer.WriteMsg(resp); err != nil {
|
2015-12-24 04:51:32 -05:00
|
|
|
log.Errorf("error writing resolver resp, %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-03-28 19:23:08 -04:00
|
|
|
|
2016-04-16 02:12:27 -04:00
|
|
|
func (r *resolver) forwardQueryStart(w dns.ResponseWriter, msg *dns.Msg, queryID uint16) bool {
|
2016-04-01 12:45:37 -04:00
|
|
|
proto := w.LocalAddr().Network()
|
|
|
|
dnsID := uint16(rand.Intn(maxDNSID))
|
|
|
|
|
|
|
|
cc := clientConn{
|
2016-04-16 02:12:27 -04:00
|
|
|
dnsID: queryID,
|
2016-04-01 12:45:37 -04:00
|
|
|
respWriter: w,
|
|
|
|
}
|
|
|
|
|
2016-03-28 19:23:08 -04:00
|
|
|
r.queryLock.Lock()
|
|
|
|
defer r.queryLock.Unlock()
|
2016-04-01 12:45:37 -04:00
|
|
|
|
2016-03-28 19:23:08 -04:00
|
|
|
if r.count == maxConcurrent {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
r.count++
|
2016-04-01 12:45:37 -04:00
|
|
|
|
|
|
|
switch proto {
|
|
|
|
case "tcp":
|
|
|
|
break
|
|
|
|
case "udp":
|
|
|
|
for ok := true; ok == true; dnsID = uint16(rand.Intn(maxDNSID)) {
|
|
|
|
_, ok = r.client[dnsID]
|
|
|
|
}
|
2016-04-18 22:55:19 -04:00
|
|
|
log.Debugf("client dns id %v, changed id %v", queryID, dnsID)
|
2016-04-01 12:45:37 -04:00
|
|
|
r.client[dnsID] = cc
|
|
|
|
msg.Id = dnsID
|
|
|
|
default:
|
|
|
|
log.Errorf("Invalid protocol..")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-03-28 19:23:08 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-14 01:56:59 -04:00
|
|
|
func (r *resolver) addQueryToGC(w dns.ResponseWriter, msg *dns.Msg) {
|
|
|
|
if w.LocalAddr().Network() != "udp" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.queryLock.Lock()
|
|
|
|
cc, ok := r.client[msg.Id]
|
|
|
|
r.queryLock.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
query := sboxQuery{
|
|
|
|
sboxID: r.sb.ID(),
|
|
|
|
dnsID: msg.Id,
|
|
|
|
}
|
|
|
|
clientGC := &clientConnGC{
|
|
|
|
client: cc,
|
|
|
|
}
|
|
|
|
queryGCMutex.Lock()
|
|
|
|
queryGC[query] = clientGC
|
|
|
|
queryGCMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolver) checkRespInGC(w dns.ResponseWriter, msg *dns.Msg) bool {
|
|
|
|
if w.LocalAddr().Network() != "udp" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
query := sboxQuery{
|
|
|
|
sboxID: r.sb.ID(),
|
|
|
|
dnsID: msg.Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
queryGCMutex.Lock()
|
|
|
|
defer queryGCMutex.Unlock()
|
|
|
|
if _, ok := queryGC[query]; ok {
|
|
|
|
delete(queryGC, query)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-01 12:45:37 -04:00
|
|
|
func (r *resolver) forwardQueryEnd(w dns.ResponseWriter, msg *dns.Msg) dns.ResponseWriter {
|
|
|
|
var (
|
|
|
|
cc clientConn
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
proto := w.LocalAddr().Network()
|
|
|
|
|
2016-03-28 19:23:08 -04:00
|
|
|
r.queryLock.Lock()
|
|
|
|
defer r.queryLock.Unlock()
|
2016-04-01 12:45:37 -04:00
|
|
|
|
2016-03-28 19:23:08 -04:00
|
|
|
if r.count == 0 {
|
2016-04-01 12:45:37 -04:00
|
|
|
log.Errorf("Invalid concurrent query count")
|
|
|
|
} else {
|
|
|
|
r.count--
|
2016-03-28 19:23:08 -04:00
|
|
|
}
|
2016-04-01 12:45:37 -04:00
|
|
|
|
|
|
|
switch proto {
|
|
|
|
case "tcp":
|
|
|
|
break
|
|
|
|
case "udp":
|
|
|
|
if cc, ok = r.client[msg.Id]; ok == false {
|
|
|
|
log.Debugf("Can't retrieve client context for dns id %v", msg.Id)
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-18 22:55:19 -04:00
|
|
|
log.Debugf("dns msg id %v, client id %v", msg.Id, cc.dnsID)
|
2016-04-01 12:45:37 -04:00
|
|
|
delete(r.client, msg.Id)
|
|
|
|
msg.Id = cc.dnsID
|
|
|
|
w = cc.respWriter
|
|
|
|
default:
|
|
|
|
log.Errorf("Invalid protocol")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return w
|
2016-03-28 19:23:08 -04:00
|
|
|
}
|