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