checks.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. //go:build !debug
  4. // +build !debug
  5. package stun
  6. import (
  7. "errors"
  8. "github.com/pion/stun/internal/hmac"
  9. )
  10. // CheckSize returns ErrAttrSizeInvalid if got is not equal to expected.
  11. func CheckSize(_ AttrType, got, expected int) error {
  12. if got == expected {
  13. return nil
  14. }
  15. return ErrAttributeSizeInvalid
  16. }
  17. func checkHMAC(got, expected []byte) error {
  18. if hmac.Equal(got, expected) {
  19. return nil
  20. }
  21. return ErrIntegrityMismatch
  22. }
  23. func checkFingerprint(got, expected uint32) error {
  24. if got == expected {
  25. return nil
  26. }
  27. return ErrFingerprintMismatch
  28. }
  29. // IsAttrSizeInvalid returns true if error means that attribute size is invalid.
  30. func IsAttrSizeInvalid(err error) bool {
  31. return errors.Is(err, ErrAttributeSizeInvalid)
  32. }
  33. // CheckOverflow returns ErrAttributeSizeOverflow if got is bigger that max.
  34. func CheckOverflow(_ AttrType, got, max int) error {
  35. if got <= max {
  36. return nil
  37. }
  38. return ErrAttributeSizeOverflow
  39. }
  40. // IsAttrSizeOverflow returns true if error means that attribute size is too big.
  41. func IsAttrSizeOverflow(err error) bool {
  42. return errors.Is(err, ErrAttributeSizeOverflow)
  43. }