Tweaks algorithm so it's safe with an empty list and adds a PQ test.

pull/1813/head
James Phillips 9 years ago
parent 3a25860270
commit 5c80647e34

@ -487,22 +487,20 @@ func (d *DNSServer) formatNodeRecord(node *structs.Node, addr, qName string, qTy
return records return records
} }
// trimAnswers makes sure a UDP response is not longer than allowed by RFC 1035 // trimAnswers makes sure a UDP response is not longer than allowed by RFC 1035.
// We first enforce an arbitrary limit, and then make sure the response doesn't
// exceed 512 bytes.
func trimAnswers(resp *dns.Msg) (trimmed bool) { func trimAnswers(resp *dns.Msg) (trimmed bool) {
numAnswers := len(resp.Answer) numAnswers := len(resp.Answer)
// This cuts UDP responses to a useful but limited number of responses.
if numAnswers > maxServiceResponses { if numAnswers > maxServiceResponses {
resp.Answer = resp.Answer[:maxServiceResponses] resp.Answer = resp.Answer[:maxServiceResponses]
} }
// Check that the response isn't more than 512 bytes // This enforces the hard limit of 512 bytes per the RFC.
for respBytes := resp.Len(); respBytes > 512; respBytes = resp.Len() { for len(resp.Answer) > 0 && resp.Len() > 512 {
resp.Answer = resp.Answer[:len(resp.Answer)-1] resp.Answer = resp.Answer[:len(resp.Answer)-1]
if len(resp.Answer) == 0 {
// We've done all we can
break
}
} }
return len(resp.Answer) < numAnswers return len(resp.Answer) < numAnswers

@ -1991,8 +1991,33 @@ func TestDNS_ServiceLookup_LargeResponses(t *testing.T) {
} }
} }
// Register an equivalent prepared query.
{
args := &structs.PreparedQueryRequest{
Datacenter: "dc1",
Op: structs.PreparedQueryCreate,
Query: &structs.PreparedQuery{
Name: longServiceName,
Service: structs.ServiceQuery{
Service: longServiceName,
Tags: []string{"master"},
},
},
}
var id string
if err := srv.agent.RPC("PreparedQuery.Apply", args, &id); err != nil {
t.Fatalf("err: %v", err)
}
}
// Look up the service directly and via prepared query.
questions := []string{
"_" + longServiceName + "._master.service.consul.",
longServiceName + ".query.consul.",
}
for _, question := range questions {
m := new(dns.Msg) m := new(dns.Msg)
m.SetQuestion("_"+longServiceName+"._master.service.consul.", dns.TypeSRV) m.SetQuestion(question, dns.TypeSRV)
c := new(dns.Client) c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS) addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
@ -2016,6 +2041,7 @@ func TestDNS_ServiceLookup_LargeResponses(t *testing.T) {
t.Fatalf("should have truncate bit") t.Fatalf("should have truncate bit")
} }
} }
}
func TestDNS_ServiceLookup_MaxResponses(t *testing.T) { func TestDNS_ServiceLookup_MaxResponses(t *testing.T) {
dir, srv := makeDNSServer(t) dir, srv := makeDNSServer(t)

Loading…
Cancel
Save