Skip to content

Commit

Permalink
Check for mismatched query IDs when using TCP
Browse files Browse the repository at this point in the history
  • Loading branch information
AGWA committed Oct 13, 2020
1 parent d049866 commit 22a9a34
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
17 changes: 12 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,19 @@ func (c *Client) exchange(m *Msg, co *Conn) (r *Msg, rtt time.Duration, err erro
}

co.SetReadDeadline(time.Now().Add(c.getTimeoutForRequest(c.readTimeout())))
for {
if _, ok := co.Conn.(net.PacketConn); ok {
for {
r, err = co.ReadMsg()
// Ignore replies with mismatched IDs because they might be
// responses to earlier queries that timed out.
if err != nil || r.Id == m.Id {
break
}
}
} else {
r, err = co.ReadMsg()
// Ignore replies with mismatched IDs because they might be
// responses to earlier queries that timed out.
if err != nil || r.Id == m.Id {
break
if err == nil && r.Id != m.Id {
err = ErrId
}
}
rtt = time.Since(t)
Expand Down
21 changes: 21 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,27 @@ func TestClientSyncBadThenGoodID(t *testing.T) {
}
}

func TestClientSyncTCPBadID(t *testing.T) {
HandleFunc("miek.nl.", HelloServerBadID)
defer HandleRemove("miek.nl.")

s, addrstr, err := RunLocalTCPServer(":0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()

m := new(Msg)
m.SetQuestion("miek.nl.", TypeSOA)

c := &Client{
Net: "tcp",
}
if _, _, err := c.Exchange(m, addrstr); err != ErrId {
t.Errorf("did not find a bad Id")
}
}

func TestClientEDNS0(t *testing.T) {
HandleFunc("miek.nl.", HelloServer)
defer HandleRemove("miek.nl.")
Expand Down

0 comments on commit 22a9a34

Please sign in to comment.