Tweaks algorithm so it's safe with an empty list and adds a PQ test.
This commit is contained in:
parent
f4edb28ef3
commit
b132e8f21b
|
@ -487,22 +487,20 @@ func (d *DNSServer) formatNodeRecord(node *structs.Node, addr, qName string, qTy
|
|||
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) {
|
||||
numAnswers := len(resp.Answer)
|
||||
|
||||
// This cuts UDP responses to a useful but limited number of responses.
|
||||
if numAnswers > maxServiceResponses {
|
||||
resp.Answer = resp.Answer[:maxServiceResponses]
|
||||
}
|
||||
|
||||
// Check that the response isn't more than 512 bytes
|
||||
for respBytes := resp.Len(); respBytes > 512; respBytes = resp.Len() {
|
||||
// This enforces the hard limit of 512 bytes per the RFC.
|
||||
for len(resp.Answer) > 0 && resp.Len() > 512 {
|
||||
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
|
||||
|
|
|
@ -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.SetQuestion("_"+longServiceName+"._master.service.consul.", dns.TypeSRV)
|
||||
m.SetQuestion(question, dns.TypeSRV)
|
||||
|
||||
c := new(dns.Client)
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNS_ServiceLookup_MaxResponses(t *testing.T) {
|
||||
dir, srv := makeDNSServer(t)
|
||||
|
|
Loading…
Reference in New Issue