muxfunc.go 1.7 KB

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