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 WEBRTC_P2P_BASE_TRANSPORTDESCRIPTION_H_
12 #define WEBRTC_P2P_BASE_TRANSPORTDESCRIPTION_H_
13 
14 #include <algorithm>
15 #include <string>
16 #include <vector>
17 
18 #include "webrtc/p2p/base/candidate.h"
19 #include "webrtc/p2p/base/constants.h"
20 #include "webrtc/base/scoped_ptr.h"
21 #include "webrtc/base/sslfingerprint.h"
22 
23 namespace cricket {
24 
25 // SEC_ENABLED and SEC_REQUIRED should only be used if the session
26 // was negotiated over TLS, to protect the inline crypto material
27 // exchange.
28 // SEC_DISABLED: No crypto in outgoing offer, ignore any supplied crypto.
29 // SEC_ENABLED:  Crypto in outgoing offer and answer (if supplied in offer).
30 // SEC_REQUIRED: Crypto in outgoing offer and answer. Fail any offer with absent
31 //               or unsupported crypto.
32 enum SecurePolicy {
33   SEC_DISABLED,
34   SEC_ENABLED,
35   SEC_REQUIRED
36 };
37 
38 // Whether our side of the call is driving the negotiation, or the other side.
39 enum IceRole {
40   ICEROLE_CONTROLLING = 0,
41   ICEROLE_CONTROLLED,
42   ICEROLE_UNKNOWN
43 };
44 
45 // ICE RFC 5245 implementation type.
46 enum IceMode {
47   ICEMODE_FULL,  // As defined in http://tools.ietf.org/html/rfc5245#section-4.1
48   ICEMODE_LITE   // As defined in http://tools.ietf.org/html/rfc5245#section-4.2
49 };
50 
51 // RFC 4145 - http://tools.ietf.org/html/rfc4145#section-4
52 // 'active':  The endpoint will initiate an outgoing connection.
53 // 'passive': The endpoint will accept an incoming connection.
54 // 'actpass': The endpoint is willing to accept an incoming
55 //            connection or to initiate an outgoing connection.
56 enum ConnectionRole {
57   CONNECTIONROLE_NONE = 0,
58   CONNECTIONROLE_ACTIVE,
59   CONNECTIONROLE_PASSIVE,
60   CONNECTIONROLE_ACTPASS,
61   CONNECTIONROLE_HOLDCONN,
62 };
63 
64 extern const char CONNECTIONROLE_ACTIVE_STR[];
65 extern const char CONNECTIONROLE_PASSIVE_STR[];
66 extern const char CONNECTIONROLE_ACTPASS_STR[];
67 extern const char CONNECTIONROLE_HOLDCONN_STR[];
68 
69 bool StringToConnectionRole(const std::string& role_str, ConnectionRole* role);
70 bool ConnectionRoleToString(const ConnectionRole& role, std::string* role_str);
71 
72 typedef std::vector<Candidate> Candidates;
73 
74 struct TransportDescription {
TransportDescriptionTransportDescription75   TransportDescription()
76       : ice_mode(ICEMODE_FULL),
77         connection_role(CONNECTIONROLE_NONE) {}
78 
TransportDescriptionTransportDescription79   TransportDescription(const std::vector<std::string>& transport_options,
80                        const std::string& ice_ufrag,
81                        const std::string& ice_pwd,
82                        IceMode ice_mode,
83                        ConnectionRole role,
84                        const rtc::SSLFingerprint* identity_fingerprint,
85                        const Candidates& candidates)
86       : transport_options(transport_options),
87         ice_ufrag(ice_ufrag),
88         ice_pwd(ice_pwd),
89         ice_mode(ice_mode),
90         connection_role(role),
91         identity_fingerprint(CopyFingerprint(identity_fingerprint)),
92         candidates(candidates) {}
TransportDescriptionTransportDescription93   TransportDescription(const std::string& ice_ufrag,
94                        const std::string& ice_pwd)
95       : ice_ufrag(ice_ufrag),
96         ice_pwd(ice_pwd),
97         ice_mode(ICEMODE_FULL),
98         connection_role(CONNECTIONROLE_NONE) {}
TransportDescriptionTransportDescription99   TransportDescription(const TransportDescription& from)
100       : transport_options(from.transport_options),
101         ice_ufrag(from.ice_ufrag),
102         ice_pwd(from.ice_pwd),
103         ice_mode(from.ice_mode),
104         connection_role(from.connection_role),
105         identity_fingerprint(CopyFingerprint(from.identity_fingerprint.get())),
106         candidates(from.candidates) {}
107 
108   TransportDescription& operator=(const TransportDescription& from) {
109     // Self-assignment
110     if (this == &from)
111       return *this;
112 
113     transport_options = from.transport_options;
114     ice_ufrag = from.ice_ufrag;
115     ice_pwd = from.ice_pwd;
116     ice_mode = from.ice_mode;
117     connection_role = from.connection_role;
118 
119     identity_fingerprint.reset(CopyFingerprint(
120         from.identity_fingerprint.get()));
121     candidates = from.candidates;
122     return *this;
123   }
124 
HasOptionTransportDescription125   bool HasOption(const std::string& option) const {
126     return (std::find(transport_options.begin(), transport_options.end(),
127                       option) != transport_options.end());
128   }
AddOptionTransportDescription129   void AddOption(const std::string& option) {
130     transport_options.push_back(option);
131   }
secureTransportDescription132   bool secure() const { return identity_fingerprint != NULL; }
133 
CopyFingerprintTransportDescription134   static rtc::SSLFingerprint* CopyFingerprint(
135       const rtc::SSLFingerprint* from) {
136     if (!from)
137       return NULL;
138 
139     return new rtc::SSLFingerprint(*from);
140   }
141 
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   rtc::scoped_ptr<rtc::SSLFingerprint> identity_fingerprint;
149   Candidates candidates;
150 };
151 
152 }  // namespace cricket
153 
154 #endif  // WEBRTC_P2P_BASE_TRANSPORTDESCRIPTION_H_
155