Add IsMsg()

A quick validating function that checks if a buffer is a valid
DNS message.
The code is *far* from complete.
This commit is contained in:
Miek Gieben 2014-05-18 09:02:00 +01:00
parent 812ab5e755
commit 060e66250e
2 changed files with 15 additions and 2 deletions

View File

@ -5,14 +5,14 @@
package dns
import (
"errors"
"net"
"strconv"
)
const hexDigit = "0123456789abcdef"
// Everything is assumed in the ClassINET class. If
// you need other classes you are on your own.
// Everything is assumed in ClassINET.
// SetReply creates a reply packet from a request message.
func (dns *Msg) SetReply(request *Msg) *Msg {
@ -165,6 +165,18 @@ func IsSubDomain(parent, child string) bool {
return CompareDomainName(parent, child) == CountLabel(parent)
}
// IsMsg sanity checks buf and returns an error if it isn't a valid DNS packet.
// The checking is performed on the binary payload.
func IsMsg(buf []byte) error {
// Header
if len(buf) < 12 {
return errors.New("dns: bad message header")
}
// Header: Opcode
return nil
}
// IsFqdn checks if a domain name is fully qualified.
func IsFqdn(s string) bool {
l := len(s)

1
msg.go
View File

@ -1535,6 +1535,7 @@ func (dns *Msg) Unpack(msg []byte) (err error) {
// If we see a TC bit being set we return here, without
// an error, because technically it isn't an error. So return
// without parsing the potentially corrupt packet and hitting an error.
// TODO(miek): this isn't the best strategy!
if dns.Truncated {
dns.Answer = nil
dns.Ns = nil