2024-05-15 23:15:00 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
//go:build !debug
|
2022-03-10 09:44:48 +00:00
|
|
|
// +build !debug
|
|
|
|
|
|
|
|
package stun
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/pion/stun/internal/hmac"
|
|
|
|
)
|
2022-03-10 09:44:48 +00:00
|
|
|
|
|
|
|
// CheckSize returns ErrAttrSizeInvalid if got is not equal to expected.
|
|
|
|
func CheckSize(_ AttrType, got, expected int) error {
|
|
|
|
if got == expected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrAttributeSizeInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkHMAC(got, expected []byte) error {
|
|
|
|
if hmac.Equal(got, expected) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrIntegrityMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkFingerprint(got, expected uint32) error {
|
|
|
|
if got == expected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrFingerprintMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAttrSizeInvalid returns true if error means that attribute size is invalid.
|
|
|
|
func IsAttrSizeInvalid(err error) bool {
|
2024-05-15 23:15:00 +00:00
|
|
|
return errors.Is(err, ErrAttributeSizeInvalid)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckOverflow returns ErrAttributeSizeOverflow if got is bigger that max.
|
|
|
|
func CheckOverflow(_ AttrType, got, max int) error {
|
|
|
|
if got <= max {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrAttributeSizeOverflow
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAttrSizeOverflow returns true if error means that attribute size is too big.
|
|
|
|
func IsAttrSizeOverflow(err error) bool {
|
2024-05-15 23:15:00 +00:00
|
|
|
return errors.Is(err, ErrAttributeSizeOverflow)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|