muxfunc.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package mux
  4. // MatchFunc allows custom logic for mapping packets to an Endpoint
  5. type MatchFunc func([]byte) bool
  6. // MatchAll always returns true
  7. func MatchAll([]byte) bool {
  8. return true
  9. }
  10. // MatchRange returns true if the first byte of buf is in [lower..upper]
  11. func MatchRange(lower, upper byte, buf []byte) bool {
  12. if len(buf) < 1 {
  13. return false
  14. }
  15. b := buf[0]
  16. return b >= lower && b <= upper
  17. }
  18. // MatchFuncs as described in RFC7983
  19. // https://tools.ietf.org/html/rfc7983
  20. // +----------------+
  21. // | [0..3] -+--> forward to STUN
  22. // | |
  23. // | [16..19] -+--> forward to ZRTP
  24. // | |
  25. // packet --> | [20..63] -+--> forward to DTLS
  26. // | |
  27. // | [64..79] -+--> forward to TURN Channel
  28. // | |
  29. // | [128..191] -+--> forward to RTP/RTCP
  30. // +----------------+
  31. // MatchDTLS is a MatchFunc that accepts packets with the first byte in [20..63]
  32. // as defied in RFC7983
  33. func MatchDTLS(b []byte) bool {
  34. return MatchRange(20, 63, b)
  35. }
  36. // MatchSRTPOrSRTCP is a MatchFunc that accepts packets with the first byte in [128..191]
  37. // as defied in RFC7983
  38. func MatchSRTPOrSRTCP(b []byte) bool {
  39. return MatchRange(128, 191, b)
  40. }
  41. func isRTCP(buf []byte) bool {
  42. // Not long enough to determine RTP/RTCP
  43. if len(buf) < 4 {
  44. return false
  45. }
  46. return buf[1] >= 192 && buf[1] <= 223
  47. }
  48. // MatchSRTP is a MatchFunc that only matches SRTP and not SRTCP
  49. func MatchSRTP(buf []byte) bool {
  50. return MatchSRTPOrSRTCP(buf) && !isRTCP(buf)
  51. }
  52. // MatchSRTCP is a MatchFunc that only matches SRTCP and not SRTP
  53. func MatchSRTCP(buf []byte) bool {
  54. return MatchSRTPOrSRTCP(buf) && isRTCP(buf)
  55. }