Shortened ReadMsg using ReadMsgBytes, added a constant.

This commit is contained in:
Alex Sergeyev 2015-06-28 20:20:16 -04:00
parent 2f3bcbd506
commit 53dfadf090
2 changed files with 14 additions and 21 deletions

View File

@ -189,25 +189,12 @@ func (c *Client) exchange(m *Msg, a string) (r *Msg, rtt time.Duration, err erro
// If the received message contains a TSIG record the transaction
// signature is verified.
func (co *Conn) ReadMsg() (*Msg, error) {
var p []byte
m := new(Msg)
if _, ok := co.Conn.(*net.TCPConn); ok {
p = make([]byte, MaxMsgSize)
} else {
if co.UDPSize > MinMsgSize {
p = make([]byte, co.UDPSize)
} else {
p = make([]byte, MinMsgSize)
}
}
n, err := co.Read(p)
p, err := co.ReadMsgBytes(nil)
if err != nil {
return nil, err
} else if n < 12 {
return nil, ErrShortRead
}
p = p[:n]
m := new(Msg)
if err := m.Unpack(p); err != nil {
return nil, err
}
@ -221,15 +208,17 @@ func (co *Conn) ReadMsg() (*Msg, error) {
return m, err
}
// ReadMsgBytes reads a message bytes from the connection. Parses and fills
// dns message wire header (passing nil would skip header parsing) and
// returns message bytes to process them later.
// ReadMsgBytes reads DNS packet, parses and fills wire header (passing nil
// would cause skipping that). Returns message in byte format to parse with
// Msg.Unpack later on.
//
// Note that this function would not be able to report TSIG error or
// check it got actual DNS payload.
func (co *Conn) ReadMsgBytes(hdr *Header) ([]byte, error) {
var p []byte
if _, ok := co.Conn.(*net.TCPConn); ok {
// we got two byte
p = make([]byte, MaxMsgSize)
} else {
if co.UDPSize > MinMsgSize {
@ -242,7 +231,7 @@ func (co *Conn) ReadMsgBytes(hdr *Header) ([]byte, error) {
n, err := co.Read(p)
if err != nil {
return nil, err
} else if n < 12 {
} else if n < _HBytes {
return nil, ErrShortRead
}

View File

@ -158,6 +158,9 @@ type Header struct {
}
const (
// Header Size
_HBytes = 12
// Header.Bits
_QR = 1 << 15 // query/response (response=1)
_AA = 1 << 10 // authoritative
@ -1568,8 +1571,9 @@ type CAA struct {
func (rr *CAA) Header() *RR_Header { return &rr.Hdr }
func (rr *CAA) copy() RR { return &CAA{*rr.Hdr.copyHeader(), rr.Flag, rr.Tag, rr.Value} }
func (rr *CAA) len() int { return rr.Hdr.len() + 1 + len(rr.Tag) + len(rr.Value)/2 }
func (rr *CAA) String() string { return rr.Hdr.String() + strconv.Itoa(int(rr.Flag)) + " " + rr.Tag + " " + sprintCAAValue(rr.Value) }
func (rr *CAA) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Flag)) + " " + rr.Tag + " " + sprintCAAValue(rr.Value)
}
type UID struct {
Hdr RR_Header