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/. */
6
7 // Original author: ekr@rtfm.com
8
9 // Some of this code is cut-and-pasted from nICEr. Copyright is:
10
11 /*
12 Copyright (c) 2007, Adobe Systems, Incorporated
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
18
19 * Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21
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.
25
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.
29
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 */
42
43 #include <string>
44 #include <vector>
45
46 #include "nsCOMPtr.h"
47 #include "nsComponentManagerUtils.h"
48 #include "nsError.h"
49 #include "nsNetCID.h"
50 #include "nsComponentManagerUtils.h"
51 #include "nsServiceManagerUtils.h"
52
53 // nICEr includes
54 extern "C" {
55 #include "nr_api.h"
56 #include "registry.h"
57 #include "async_timer.h"
58 #include "ice_util.h"
59 #include "transport_addr.h"
60 #include "nr_crypto.h"
61 #include "nr_socket.h"
62 #include "nr_socket_local.h"
63 #include "stun_client_ctx.h"
64 #include "stun_server_ctx.h"
65 #include "ice_ctx.h"
66 #include "ice_candidate.h"
67 #include "ice_handler.h"
68 }
69
70 // Local includes
71 #include "logging.h"
72 #include "nricectx.h"
73 #include "nricemediastream.h"
74 #include "transportflow.h"
75 #include "transportlayerice.h"
76
77 namespace mozilla {
78
79 #ifdef ERROR
80 # undef ERROR
81 #endif
82
83 MOZ_MTLOG_MODULE("mtransport")
84
TransportLayerIce()85 TransportLayerIce::TransportLayerIce() : stream_(nullptr), component_(0) {
86 // setup happens later
87 }
88
~TransportLayerIce()89 TransportLayerIce::~TransportLayerIce() {
90 // No need to do anything here, since we use smart pointers
91 }
92
SetParameters(RefPtr<NrIceMediaStream> stream,int component)93 void TransportLayerIce::SetParameters(RefPtr<NrIceMediaStream> stream,
94 int component) {
95 // Stream could be null in the case of some badly written js that causes
96 // us to be in an ICE restart case, but not have valid streams due to
97 // not calling PeerConnectionMedia::EnsureTransports if
98 // PeerConnectionImpl::SetSignalingState_m thinks the conditions were
99 // not correct. We also solved a case where an incoming answer was
100 // incorrectly beginning an ICE restart when the offer did not indicate one.
101 if (!stream) {
102 MOZ_ASSERT(false);
103 return;
104 }
105
106 stream_ = stream;
107 component_ = component;
108
109 PostSetup();
110 }
111
PostSetup()112 void TransportLayerIce::PostSetup() {
113 stream_->SignalReady.connect(this, &TransportLayerIce::IceReady);
114 stream_->SignalFailed.connect(this, &TransportLayerIce::IceFailed);
115 stream_->SignalPacketReceived.connect(this,
116 &TransportLayerIce::IcePacketReceived);
117 if (stream_->state() == NrIceMediaStream::ICE_OPEN) {
118 TL_SET_STATE(TS_OPEN);
119 }
120 }
121
SendPacket(MediaPacket & packet)122 TransportResult TransportLayerIce::SendPacket(MediaPacket& packet) {
123 CheckThread();
124 SignalPacketSending(this, packet);
125 nsresult res = stream_->SendPacket(component_, packet.data(), packet.len());
126
127 if (!NS_SUCCEEDED(res)) {
128 return (res == NS_BASE_STREAM_WOULD_BLOCK) ? TE_WOULDBLOCK : TE_ERROR;
129 }
130
131 MOZ_MTLOG(ML_DEBUG,
132 LAYER_INFO << " SendPacket(" << packet.len() << ") succeeded");
133
134 return packet.len();
135 }
136
IceCandidate(NrIceMediaStream * stream,const std::string &)137 void TransportLayerIce::IceCandidate(NrIceMediaStream* stream,
138 const std::string&) {
139 // NO-OP for now
140 }
141
IceReady(NrIceMediaStream * stream)142 void TransportLayerIce::IceReady(NrIceMediaStream* stream) {
143 CheckThread();
144 // only handle the current stream (not the old stream during restart)
145 if (stream != stream_) {
146 return;
147 }
148 MOZ_MTLOG(ML_INFO, LAYER_INFO << "ICE Ready(" << stream->name() << ","
149 << component_ << ")");
150 TL_SET_STATE(TS_OPEN);
151 }
152
IceFailed(NrIceMediaStream * stream)153 void TransportLayerIce::IceFailed(NrIceMediaStream* stream) {
154 CheckThread();
155 // only handle the current stream (not the old stream during restart)
156 if (stream != stream_) {
157 return;
158 }
159 MOZ_MTLOG(ML_INFO, LAYER_INFO << "ICE Failed(" << stream->name() << ","
160 << component_ << ")");
161 TL_SET_STATE(TS_ERROR);
162 }
163
IcePacketReceived(NrIceMediaStream * stream,int component,const unsigned char * data,int len)164 void TransportLayerIce::IcePacketReceived(NrIceMediaStream* stream,
165 int component,
166 const unsigned char* data, int len) {
167 CheckThread();
168 // We get packets for both components, so ignore the ones that aren't
169 // for us.
170 if (component_ != component) return;
171
172 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "PacketReceived(" << stream->name() << ","
173 << component << "," << len << ")");
174 // Might be useful to allow MediaPacket to borrow a buffer (ie; not take
175 // ownership, but copy it if the MediaPacket is moved). This could be a
176 // footgun though with MediaPackets that end up on the heap.
177 MediaPacket packet;
178 packet.Copy(data, len);
179 packet.Categorize();
180
181 SignalPacketReceived(this, packet);
182 }
183
184 } // namespace mozilla
185