xref: /aosp_15_r20/external/webrtc/p2p/base/transport_description.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2012 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 #ifndef P2P_BASE_TRANSPORT_DESCRIPTION_H_
12 #define P2P_BASE_TRANSPORT_DESCRIPTION_H_
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "absl/algorithm/container.h"
19 #include "absl/strings/string_view.h"
20 #include "absl/types/optional.h"
21 #include "api/rtc_error.h"
22 #include "p2p/base/p2p_constants.h"
23 #include "rtc_base/ssl_fingerprint.h"
24 #include "rtc_base/system/rtc_export.h"
25 
26 namespace cricket {
27 
28 // SEC_ENABLED and SEC_REQUIRED should only be used if the session
29 // was negotiated over TLS, to protect the inline crypto material
30 // exchange.
31 // SEC_DISABLED: No crypto in outgoing offer, ignore any supplied crypto.
32 // SEC_ENABLED:  Crypto in outgoing offer and answer (if supplied in offer).
33 // SEC_REQUIRED: Crypto in outgoing offer and answer. Fail any offer with absent
34 //               or unsupported crypto.
35 // TODO(deadbeef): Remove this or rename it to something more appropriate, like
36 // SdesPolicy.
37 enum SecurePolicy { SEC_DISABLED, SEC_ENABLED, SEC_REQUIRED };
38 
39 // Whether our side of the call is driving the negotiation, or the other side.
40 enum IceRole { ICEROLE_CONTROLLING = 0, ICEROLE_CONTROLLED, ICEROLE_UNKNOWN };
41 
42 // ICE RFC 5245 implementation type.
43 enum IceMode {
44   ICEMODE_FULL,  // As defined in http://tools.ietf.org/html/rfc5245#section-4.1
45   ICEMODE_LITE   // As defined in http://tools.ietf.org/html/rfc5245#section-4.2
46 };
47 
48 // RFC 4145 - http://tools.ietf.org/html/rfc4145#section-4
49 // 'active':  The endpoint will initiate an outgoing connection.
50 // 'passive': The endpoint will accept an incoming connection.
51 // 'actpass': The endpoint is willing to accept an incoming
52 //            connection or to initiate an outgoing connection.
53 enum ConnectionRole {
54   CONNECTIONROLE_NONE = 0,
55   CONNECTIONROLE_ACTIVE,
56   CONNECTIONROLE_PASSIVE,
57   CONNECTIONROLE_ACTPASS,
58   CONNECTIONROLE_HOLDCONN,
59 };
60 
61 struct IceParameters {
62   // Constructs an IceParameters from a user-provided ufrag/pwd combination.
63   // Returns a SyntaxError if the ufrag or pwd are malformed.
64   static RTC_EXPORT webrtc::RTCErrorOr<IceParameters> Parse(
65       absl::string_view raw_ufrag,
66       absl::string_view raw_pwd);
67 
68   // TODO(honghaiz): Include ICE mode in this structure to match the ORTC
69   // struct:
70   // http://ortc.org/wp-content/uploads/2016/03/ortc.html#idl-def-RTCIceParameters
71   std::string ufrag;
72   std::string pwd;
73   bool renomination = false;
74   IceParameters() = default;
IceParametersIceParameters75   IceParameters(absl::string_view ice_ufrag,
76                 absl::string_view ice_pwd,
77                 bool ice_renomination)
78       : ufrag(ice_ufrag), pwd(ice_pwd), renomination(ice_renomination) {}
79 
80   bool operator==(const IceParameters& other) const {
81     return ufrag == other.ufrag && pwd == other.pwd &&
82            renomination == other.renomination;
83   }
84   bool operator!=(const IceParameters& other) const {
85     return !(*this == other);
86   }
87 
88   // Validate IceParameters, returns a SyntaxError if the ufrag or pwd are
89   // malformed.
90   webrtc::RTCError Validate() const;
91 };
92 
93 extern const char CONNECTIONROLE_ACTIVE_STR[];
94 extern const char CONNECTIONROLE_PASSIVE_STR[];
95 extern const char CONNECTIONROLE_ACTPASS_STR[];
96 extern const char CONNECTIONROLE_HOLDCONN_STR[];
97 
98 constexpr auto* ICE_OPTION_TRICKLE = "trickle";
99 constexpr auto* ICE_OPTION_RENOMINATION = "renomination";
100 
101 absl::optional<ConnectionRole> StringToConnectionRole(
102     absl::string_view role_str);
103 bool ConnectionRoleToString(const ConnectionRole& role, std::string* role_str);
104 
105 struct TransportDescription {
106   TransportDescription();
107   TransportDescription(const std::vector<std::string>& transport_options,
108                        absl::string_view ice_ufrag,
109                        absl::string_view ice_pwd,
110                        IceMode ice_mode,
111                        ConnectionRole role,
112                        const rtc::SSLFingerprint* identity_fingerprint);
113   TransportDescription(absl::string_view ice_ufrag, absl::string_view ice_pwd);
114   TransportDescription(const TransportDescription& from);
115   ~TransportDescription();
116 
117   TransportDescription& operator=(const TransportDescription& from);
118 
119   // TODO(deadbeef): Rename to HasIceOption, etc.
HasOptionTransportDescription120   bool HasOption(absl::string_view option) const {
121     return absl::c_linear_search(transport_options, option);
122   }
AddOptionTransportDescription123   void AddOption(absl::string_view option) {
124     transport_options.emplace_back(option);
125   }
secureTransportDescription126   bool secure() const { return identity_fingerprint != nullptr; }
127 
GetIceParametersTransportDescription128   IceParameters GetIceParameters() const {
129     return IceParameters(ice_ufrag, ice_pwd,
130                          HasOption(ICE_OPTION_RENOMINATION));
131   }
132 
CopyFingerprintTransportDescription133   static rtc::SSLFingerprint* CopyFingerprint(const rtc::SSLFingerprint* from) {
134     if (!from)
135       return NULL;
136 
137     return new rtc::SSLFingerprint(*from);
138   }
139 
140   // These are actually ICE options (appearing in the ice-options attribute in
141   // SDP).
142   // TODO(deadbeef): Rename to ice_options.
143   std::vector<std::string> transport_options;
144   std::string ice_ufrag;
145   std::string ice_pwd;
146   IceMode ice_mode;
147   ConnectionRole connection_role;
148 
149   std::unique_ptr<rtc::SSLFingerprint> identity_fingerprint;
150 };
151 
152 }  // namespace cricket
153 
154 #endif  // P2P_BASE_TRANSPORT_DESCRIPTION_H_
155