Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // Original author: ekr@rtfm.com
9 // Some of this code is cut-and-pasted from nICEr. Copyright is:
11 /*
12 Copyright (c) 2007, Adobe Systems, Incorporated
13 All rights reserved.
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
19 * Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
22 * Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
26 * Neither the name of Adobe Systems, Network Resonance nor the names of its
27 contributors may be used to endorse or promote products derived from
28 this software without specific prior written permission.
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
43 /* This Source Code Form is subject to the terms of the Mozilla Public
44 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
45 * You can obtain one at http://mozilla.org/MPL/2.0/. */
47 // Original author: ekr@rtfm.com
49 // This is a wrapper around the nICEr ICE stack
50 #ifndef nricectx_h__
51 #define nricectx_h__
53 #include <vector>
55 #include "sigslot.h"
57 #include "prnetdb.h"
59 #include "mozilla/RefPtr.h"
60 #include "mozilla/Scoped.h"
61 #include "nsAutoPtr.h"
62 #include "nsIEventTarget.h"
63 #include "nsITimer.h"
65 #include "m_cpp_utils.h"
67 typedef struct nr_ice_ctx_ nr_ice_ctx;
68 typedef struct nr_ice_peer_ctx_ nr_ice_peer_ctx;
69 typedef struct nr_ice_media_stream_ nr_ice_media_stream;
70 typedef struct nr_ice_handler_ nr_ice_handler;
71 typedef struct nr_ice_handler_vtbl_ nr_ice_handler_vtbl;
72 typedef struct nr_ice_candidate_ nr_ice_candidate;
73 typedef struct nr_ice_cand_pair_ nr_ice_cand_pair;
74 typedef struct nr_ice_stun_server_ nr_ice_stun_server;
75 typedef struct nr_ice_turn_server_ nr_ice_turn_server;
76 typedef struct nr_resolver_ nr_resolver;
78 typedef void* NR_SOCKET;
80 namespace mozilla {
82 class NrIceMediaStream;
84 extern const char kNrIceTransportUdp[];
85 extern const char kNrIceTransportTcp[];
87 class NrIceStunServer {
88 public:
89 NrIceStunServer(const PRNetAddr& addr) : has_addr_(true) {
90 memcpy(&addr_, &addr, sizeof(addr));
91 }
93 // The main function to use. Will take either an address or a hostname.
94 static NrIceStunServer* Create(const std::string& addr, uint16_t port) {
95 ScopedDeletePtr<NrIceStunServer> server(
96 new NrIceStunServer());
98 nsresult rv = server->Init(addr, port);
99 if (NS_FAILED(rv))
100 return nullptr;
102 return server.forget();
103 }
105 nsresult ToNicerStunStruct(nr_ice_stun_server* server,
106 const std::string& transport =
107 kNrIceTransportUdp) const;
109 protected:
110 NrIceStunServer() : addr_() {}
112 nsresult Init(const std::string& addr, uint16_t port) {
113 PRStatus status = PR_StringToNetAddr(addr.c_str(), &addr_);
114 if (status == PR_SUCCESS) {
115 // Parseable as an address
116 addr_.inet.port = PR_htons(port);
117 port_ = port;
118 has_addr_ = true;
119 return NS_OK;
120 }
121 else if (addr.size() < 256) {
122 // Apparently this is a hostname.
123 host_ = addr;
124 port_ = port;
125 has_addr_ = false;
126 return NS_OK;
127 }
129 return NS_ERROR_FAILURE;
130 }
132 bool has_addr_;
133 std::string host_;
134 uint16_t port_;
135 PRNetAddr addr_;
136 };
138 class NrIceTurnServer : public NrIceStunServer {
139 public:
140 static NrIceTurnServer *Create(const std::string& addr, uint16_t port,
141 const std::string& username,
142 const std::vector<unsigned char>& password,
143 const char *transport = kNrIceTransportUdp) {
144 ScopedDeletePtr<NrIceTurnServer> server(
145 new NrIceTurnServer(username, password, transport));
147 nsresult rv = server->Init(addr, port);
148 if (NS_FAILED(rv))
149 return nullptr;
151 return server.forget();
152 }
154 nsresult ToNicerTurnStruct(nr_ice_turn_server *server) const;
156 private:
157 NrIceTurnServer(const std::string& username,
158 const std::vector<unsigned char>& password,
159 const char *transport) :
160 username_(username), password_(password), transport_(transport) {}
162 std::string username_;
163 std::vector<unsigned char> password_;
164 std::string transport_;
165 };
167 class NrIceCtx {
168 public:
169 enum ConnectionState { ICE_CTX_INIT,
170 ICE_CTX_CHECKING,
171 ICE_CTX_OPEN,
172 ICE_CTX_FAILED
173 };
175 enum GatheringState { ICE_CTX_GATHER_INIT,
176 ICE_CTX_GATHER_STARTED,
177 ICE_CTX_GATHER_COMPLETE
178 };
180 enum Controlling { ICE_CONTROLLING,
181 ICE_CONTROLLED
182 };
184 static RefPtr<NrIceCtx> Create(const std::string& name,
185 bool offerer,
186 bool set_interface_priorities = true);
187 virtual ~NrIceCtx();
189 nr_ice_ctx *ctx() { return ctx_; }
190 nr_ice_peer_ctx *peer() { return peer_; }
192 // Testing only.
193 void destroy_peer_ctx();
195 // Create a media stream
196 RefPtr<NrIceMediaStream> CreateStream(const std::string& name,
197 int components);
199 // The name of the ctx
200 const std::string& name() const { return name_; }
202 // Current state
203 ConnectionState connection_state() const {
204 return connection_state_;
205 }
207 // Current state
208 GatheringState gathering_state() const {
209 return gathering_state_;
210 }
212 // Get the global attributes
213 std::vector<std::string> GetGlobalAttributes();
215 // Set the other side's global attributes
216 nsresult ParseGlobalAttributes(std::vector<std::string> attrs);
218 // Set whether we are controlling or not.
219 nsresult SetControlling(Controlling controlling);
221 // Set the STUN servers. Must be called before StartGathering
222 // (if at all).
223 nsresult SetStunServers(const std::vector<NrIceStunServer>& stun_servers);
225 // Set the TURN servers. Must be called before StartGathering
226 // (if at all).
227 nsresult SetTurnServers(const std::vector<NrIceTurnServer>& turn_servers);
229 // Provide the resolution provider. Must be called before
230 // StartGathering.
231 nsresult SetResolver(nr_resolver *resolver);
233 // Start ICE gathering
234 nsresult StartGathering();
236 // Start checking
237 nsresult StartChecks();
239 // Finalize the ICE negotiation. I.e., there will be no
240 // more forking.
241 nsresult Finalize();
243 // Are we trickling?
244 bool generating_trickle() const { return trickle_; }
246 // Signals to indicate events. API users can (and should)
247 // register for these.
248 sigslot::signal2<NrIceCtx*, NrIceCtx::GatheringState>
249 SignalGatheringStateChange;
250 sigslot::signal2<NrIceCtx*, NrIceCtx::ConnectionState>
251 SignalConnectionStateChange;
253 // The thread to direct method calls to
254 nsCOMPtr<nsIEventTarget> thread() { return sts_target_; }
256 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrIceCtx)
258 private:
259 NrIceCtx(const std::string& name,
260 bool offerer)
261 : connection_state_(ICE_CTX_INIT),
262 gathering_state_(ICE_CTX_GATHER_INIT),
263 name_(name),
264 offerer_(offerer),
265 streams_(),
266 ctx_(nullptr),
267 peer_(nullptr),
268 ice_handler_vtbl_(nullptr),
269 ice_handler_(nullptr),
270 trickle_(true) {
271 // XXX: offerer_ will be used eventually; placate clang in the meantime.
272 (void)offerer_;
273 }
275 DISALLOW_COPY_ASSIGN(NrIceCtx);
277 // Callbacks for nICEr
278 static void initialized_cb(NR_SOCKET s, int h, void *arg); // ICE initialized
280 // Handler implementation
281 static int select_pair(void *obj,nr_ice_media_stream *stream,
282 int component_id, nr_ice_cand_pair **potentials,
283 int potential_ct);
284 static int stream_ready(void *obj, nr_ice_media_stream *stream);
285 static int stream_failed(void *obj, nr_ice_media_stream *stream);
286 static int ice_completed(void *obj, nr_ice_peer_ctx *pctx);
287 static int msg_recvd(void *obj, nr_ice_peer_ctx *pctx,
288 nr_ice_media_stream *stream, int component_id,
289 unsigned char *msg, int len);
290 static void trickle_cb(void *arg, nr_ice_ctx *ctx, nr_ice_media_stream *stream,
291 int component_id, nr_ice_candidate *candidate);
293 // Find a media stream by stream ptr. Gross
294 RefPtr<NrIceMediaStream> FindStream(nr_ice_media_stream *stream);
296 // Set the state
297 void SetConnectionState(ConnectionState state);
299 // Set the state
300 void SetGatheringState(GatheringState state);
302 ConnectionState connection_state_;
303 GatheringState gathering_state_;
304 const std::string name_;
305 bool offerer_;
306 std::vector<RefPtr<NrIceMediaStream> > streams_;
307 nr_ice_ctx *ctx_;
308 nr_ice_peer_ctx *peer_;
309 nr_ice_handler_vtbl* ice_handler_vtbl_; // Must be pointer
310 nr_ice_handler* ice_handler_; // Must be pointer
311 bool trickle_;
312 nsCOMPtr<nsIEventTarget> sts_target_; // The thread to run on
313 };
316 } // close namespace
317 #endif