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