mirror of
https://github.com/gortc/stun.git
synced 2024-05-03 14:44:55 +08:00
attrs: add CheckSize function
This commit is contained in:
parent
5ef9d69e1d
commit
6a26d7295e
@ -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
11
checksize.go
Normal 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
15
checksize_debug.go
Normal 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,
|
||||
}
|
||||
}
|
@ -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")
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user