github.com/pion/webrtc/v4@v4.0.1/internal/mux/muxfunc.go (about)

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