Skip to content

Commit

Permalink
Merge pull request #2 from Jigsaw-Code/bemasc-srvfail
Browse files Browse the repository at this point in the history
Fix query construction error
  • Loading branch information
Benjamin M. Schwartz authored Jun 26, 2020
2 parents a69b95c + 55db468 commit fa5f1bf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions choir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ func TestFormat(t *testing.T) {
if err := msg.Unpack(query); err != nil {
t.Fatal(err)
}
if msg.Header.OpCode != 0 {
t.Error("Message is not a question")
}
if !msg.Header.RecursionDesired {
t.Error("Query doesn't request recursion")
}
question := msg.Questions[0]
expectedQuestion := dnsmessage.Question{
Name: dnsmessage.MustNewName(name + "."),
Expand Down
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func formatQuery(name string) ([]byte, error) {
binary.BigEndian.PutUint16(ecsPayload[2:], ecsPrefixLength)

msg := &dnsmessage.Message{
Header: dnsmessage.Header{RecursionDesired: true},
Questions: []dnsmessage.Question{{
Name: n,
Type: dnsmessage.TypeTXT,
Expand Down
27 changes: 24 additions & 3 deletions example/example_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,31 @@ func (s udpDNSReportSender) Send(r choir.Report) error {
log.Printf("Querying %s via %s\n", name, s.serverAddress)
c, err := net.Dial("udp", s.serverAddress)
if err != nil {
log.Fatal("Failed to reach resolver:", err)
return fmt.Errorf("Failed to reach resolver: %w", err)
}
c.Write(query)
c.Close()
defer c.Close()

if _, err := c.Write(query); err != nil {
return fmt.Errorf("Query failed: %w", err)
}

var buf [4096]byte
c.SetReadDeadline(time.Now().Add(5 * time.Second))
n, err := c.Read(buf[:])
if err != nil {
return fmt.Errorf("Reading response failed: %w", err)
}

var response dnsmessage.Message
if err := response.Unpack(buf[:n]); err != nil {
return fmt.Errorf("Bad response: %w", err)
}
if response.Header.RCode != dnsmessage.RCodeNameError {
// We expect an NXDOMAIN response. Anything else is surprising.
return fmt.Errorf("Unexpected response: %v", response.Header.RCode)
}

log.Printf("Report complete")
return nil
}

Expand Down

0 comments on commit fa5f1bf

Please sign in to comment.