checks_debug.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //go:build debug
  2. // +build debug
  3. package stun
  4. import "github.com/pion/stun/internal/hmac"
  5. // CheckSize returns *AttrLengthError if got is not equal to expected.
  6. func CheckSize(a AttrType, got, expected int) error {
  7. if got == expected {
  8. return nil
  9. }
  10. return &AttrLengthErr{
  11. Got: got,
  12. Expected: expected,
  13. Attr: a,
  14. }
  15. }
  16. func checkHMAC(got, expected []byte) error {
  17. if hmac.Equal(got, expected) {
  18. return nil
  19. }
  20. return &IntegrityErr{
  21. Expected: expected,
  22. Actual: got,
  23. }
  24. }
  25. func checkFingerprint(got, expected uint32) error {
  26. if got == expected {
  27. return nil
  28. }
  29. return &CRCMismatch{
  30. Actual: got,
  31. Expected: expected,
  32. }
  33. }
  34. // IsAttrSizeInvalid returns true if error means that attribute size is invalid.
  35. func IsAttrSizeInvalid(err error) bool {
  36. _, ok := err.(*AttrLengthErr)
  37. return ok
  38. }
  39. // CheckOverflow returns *AttrOverflowErr if got is bigger that max.
  40. func CheckOverflow(t AttrType, got, max int) error {
  41. if got <= max {
  42. return nil
  43. }
  44. return &AttrOverflowErr{
  45. Type: t,
  46. Got: got,
  47. Max: max,
  48. }
  49. }
  50. // IsAttrSizeOverflow returns true if error means that attribute size is too big.
  51. func IsAttrSizeOverflow(err error) bool {
  52. _, ok := err.(*AttrOverflowErr)
  53. return ok
  54. }