This makes `client.Readmsg` always return the message,
regardless if and how unpacking the message failed.
This commit is contained in:
Matthijs Mekking 2017-12-18 21:15:56 +01:00 committed by Miek Gieben
parent 3bbde607ac
commit cef5150e02
1 changed files with 7 additions and 8 deletions

View File

@ -192,8 +192,10 @@ func (c *Client) exchange(m *Msg, a string) (r *Msg, rtt time.Duration, err erro
} }
// ReadMsg reads a message from the connection co. // ReadMsg reads a message from the connection co.
// If the received message contains a TSIG record the transaction // If the received message contains a TSIG record the transaction signature
// signature is verified. // is verified. This method always tries to return the message, however if an
// error is returned there are no guarantees that the returned message is a
// valid representation of the packet read.
func (co *Conn) ReadMsg() (*Msg, error) { func (co *Conn) ReadMsg() (*Msg, error) {
p, err := co.ReadMsgHeader(nil) p, err := co.ReadMsgHeader(nil)
if err != nil { if err != nil {
@ -202,13 +204,10 @@ func (co *Conn) ReadMsg() (*Msg, error) {
m := new(Msg) m := new(Msg)
if err := m.Unpack(p); err != nil { if err := m.Unpack(p); err != nil {
// If ErrTruncated was returned, we still want to allow the user to use // If an error was returned, we still want to allow the user to use
// the message, but naively they can just check err if they don't want // the message, but naively they can just check err if they don't want
// to use a truncated message // to use an erroneous message
if err == ErrTruncated { return m, err
return m, err
}
return nil, err
} }
if t := m.IsTsig(); t != nil { if t := m.IsTsig(); t != nil {
if _, ok := co.TsigSecret[t.Hdr.Name]; !ok { if _, ok := co.TsigSecret[t.Hdr.Name]; !ok {