Skip to content

Commit

Permalink
Merge pull request #1125 from sanimej/bugs
Browse files Browse the repository at this point in the history
Fix a panic in handling forwarded queries
  • Loading branch information
mrjana committed Apr 22, 2016
2 parents 89c1cfa + 62ea95b commit 0dc3da5
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func (r *resolver) ServeDNS(w dns.ResponseWriter, query *dns.Msg) {
extConn net.Conn
resp *dns.Msg
err error
writer dns.ResponseWriter
)

if query == nil || len(query.Question) == 0 {
Expand Down Expand Up @@ -329,7 +330,9 @@ func (r *resolver) ServeDNS(w dns.ResponseWriter, query *dns.Msg) {
if resp.Len() > maxSize {
truncateResp(resp, maxSize, proto == "tcp")
}
writer = w
} else {
queryID := query.Id
for i := 0; i < maxExtDNS; i++ {
extDNS := &r.extDNSList[i]
if extDNS.ipStr == "" {
Expand Down Expand Up @@ -375,7 +378,7 @@ func (r *resolver) ServeDNS(w dns.ResponseWriter, query *dns.Msg) {

// forwardQueryStart stores required context to mux multiple client queries over
// one connection; and limits the number of outstanding concurrent queries.
if r.forwardQueryStart(w, query) == false {
if r.forwardQueryStart(w, query, queryID) == false {
old := r.tStamp
r.tStamp = time.Now()
if r.tStamp.Sub(old) > logInterval {
Expand Down Expand Up @@ -405,32 +408,33 @@ func (r *resolver) ServeDNS(w dns.ResponseWriter, query *dns.Msg) {

// Retrieves the context for the forwarded query and returns the client connection
// to send the reply to
w = r.forwardQueryEnd(w, resp)
if w == nil {
writer = r.forwardQueryEnd(w, resp)
if writer == nil {
continue
}

resp.Compress = true
break
}

if resp == nil || w == nil {
if resp == nil || writer == nil {
return
}
}

err = w.WriteMsg(resp)
if err != nil {
if writer == nil {
return
}
if err = writer.WriteMsg(resp); err != nil {
log.Errorf("error writing resolver resp, %s", err)
}
}

func (r *resolver) forwardQueryStart(w dns.ResponseWriter, msg *dns.Msg) bool {
func (r *resolver) forwardQueryStart(w dns.ResponseWriter, msg *dns.Msg, queryID uint16) bool {
proto := w.LocalAddr().Network()
dnsID := uint16(rand.Intn(maxDNSID))

cc := clientConn{
dnsID: msg.Id,
dnsID: queryID,
respWriter: w,
}

Expand Down

0 comments on commit 0dc3da5

Please sign in to comment.