attrs: add CheckSize function

This commit is contained in:
Aleksandr Razumov 2018-08-01 23:06:45 +03:00
parent 5ef9d69e1d
commit 6a26d7295e
No known key found for this signature in database
GPG Key ID: 1D14A82D2E311045
5 changed files with 35 additions and 6 deletions

View File

@ -103,6 +103,7 @@ pkg github.com/gortc/stun, const MethodSend Method
pkg github.com/gortc/stun, const TransactionIDSize = 12
pkg github.com/gortc/stun, const TransactionIDSize ideal-int
pkg github.com/gortc/stun, func Build(...Setter) (*Message, error)
pkg github.com/gortc/stun, func CheckSize(AttrType, int, int) error
pkg github.com/gortc/stun, func Decode([]uint8, *Message) error
pkg github.com/gortc/stun, func Dial(string, string) (*Client, error)
pkg github.com/gortc/stun, func FingerprintValue([]uint8) uint32
@ -313,6 +314,7 @@ pkg github.com/gortc/stun, var BindingError MessageType
pkg github.com/gortc/stun, var BindingRequest MessageType
pkg github.com/gortc/stun, var BindingSuccess MessageType
pkg github.com/gortc/stun, var ErrAgentClosed error
pkg github.com/gortc/stun, var ErrAttrSizeInvalid error
pkg github.com/gortc/stun, var ErrAttributeNotFound error
pkg github.com/gortc/stun, var ErrBadIPLength error
pkg github.com/gortc/stun, var ErrBadUnknownAttrsSize error

11
checksize.go Normal file
View File

@ -0,0 +1,11 @@
// +build !debug
package stun
// CheckSize returns ErrAttrSizeInvalid if got is not equal to expected.
func CheckSize(_ AttrType, got, expected int) error {
if got == expected {
return nil
}
return ErrAttrSizeInvalid
}

15
checksize_debug.go Normal file
View File

@ -0,0 +1,15 @@
// +build debug
package stun
// CheckSize returns *AttrLengthError if got is not equal to expected.
func CheckSize(a AttrType, got, expected int) error {
if got == expected {
return nil
}
return &AttrLengthErr{
Got: got,
Expected: expected,
Attr: a,
}
}

View File

@ -1,5 +1,7 @@
package stun
import "errors"
// DecodeErr records an error and place when it is occurred.
type DecodeErr struct {
Place DecodeErrPlace
@ -52,3 +54,6 @@ func newDecodeErr(parent, children, message string) *DecodeErr {
func newAttrDecodeErr(children, message string) *DecodeErr {
return newDecodeErr("attribute", children, message)
}
// ErrAttrSizeInvalid means that decoded attribute size is invalid.
var ErrAttrSizeInvalid = errors.New("attribute size is invalid")

View File

@ -67,12 +67,8 @@ func (FingerprintAttr) Check(m *Message) error {
if err != nil {
return err
}
if len(b) != fingerprintSize {
return &AttrLengthErr{
Expected: fingerprintSize,
Got: len(b),
Attr: AttrFingerprint,
}
if err = CheckSize(AttrFingerprint, len(b), fingerprintSize); err != nil {
return err
}
val := bin.Uint32(b)
attrStart := len(m.Raw) - (fingerprintSize + attributeHeaderSize)