diff --git a/api/stun1.txt b/api/stun1.txt index faa30a2..2f05693 100644 --- a/api/stun1.txt +++ b/api/stun1.txt @@ -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 diff --git a/checksize.go b/checksize.go new file mode 100644 index 0000000..0f376f5 --- /dev/null +++ b/checksize.go @@ -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 +} diff --git a/checksize_debug.go b/checksize_debug.go new file mode 100644 index 0000000..ee7328a --- /dev/null +++ b/checksize_debug.go @@ -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, + } +} diff --git a/errors.go b/errors.go index c05a42b..e121dab 100644 --- a/errors.go +++ b/errors.go @@ -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") diff --git a/fingerprint.go b/fingerprint.go index 575de99..a8c5d16 100644 --- a/fingerprint.go +++ b/fingerprint.go @@ -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)