1 /*
2  *  Copyright 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "pc/media_protocol_names.h"
12 
13 #include <ctype.h>
14 #include <stddef.h>
15 
16 namespace cricket {
17 
18 // There are multiple variants of the RTP protocol stack, including
19 // UDP/TLS/RTP/SAVPF (WebRTC default), RTP/AVP, RTP/AVPF, RTP/SAVPF,
20 // TCP/DTLS/RTP/SAVPF and so on. We accept anything that has RTP/
21 // embedded in it somewhere as being an RTP protocol.
22 const char kMediaProtocolRtpPrefix[] = "RTP/";
23 
24 const char kMediaProtocolSctp[] = "SCTP";
25 const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP";
26 const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP";
27 const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP";
28 
IsDtlsSctp(const std::string & protocol)29 bool IsDtlsSctp(const std::string& protocol) {
30   return protocol == kMediaProtocolDtlsSctp ||
31          protocol == kMediaProtocolUdpDtlsSctp ||
32          protocol == kMediaProtocolTcpDtlsSctp;
33 }
34 
IsPlainSctp(const std::string & protocol)35 bool IsPlainSctp(const std::string& protocol) {
36   return protocol == kMediaProtocolSctp;
37 }
38 
IsRtpProtocol(const std::string & protocol)39 bool IsRtpProtocol(const std::string& protocol) {
40   if (protocol.empty()) {
41     return true;
42   }
43   size_t pos = protocol.find(cricket::kMediaProtocolRtpPrefix);
44   if (pos == std::string::npos) {
45     return false;
46   }
47   // RTP must be at the beginning of a string or not preceded by alpha
48   if (pos == 0 || !isalpha(protocol[pos - 1])) {
49     return true;
50   }
51   return false;
52 }
53 
IsSctpProtocol(const std::string & protocol)54 bool IsSctpProtocol(const std::string& protocol) {
55   return IsPlainSctp(protocol) || IsDtlsSctp(protocol);
56 }
57 
58 }  // namespace cricket
59