media/mtransport/transportlayerice.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 // Original author: ekr@rtfm.com
michael@0 8
michael@0 9 // Some of this code is cut-and-pasted from nICEr. Copyright is:
michael@0 10
michael@0 11 /*
michael@0 12 Copyright (c) 2007, Adobe Systems, Incorporated
michael@0 13 All rights reserved.
michael@0 14
michael@0 15 Redistribution and use in source and binary forms, with or without
michael@0 16 modification, are permitted provided that the following conditions are
michael@0 17 met:
michael@0 18
michael@0 19 * Redistributions of source code must retain the above copyright
michael@0 20 notice, this list of conditions and the following disclaimer.
michael@0 21
michael@0 22 * Redistributions in binary form must reproduce the above copyright
michael@0 23 notice, this list of conditions and the following disclaimer in the
michael@0 24 documentation and/or other materials provided with the distribution.
michael@0 25
michael@0 26 * Neither the name of Adobe Systems, Network Resonance nor the names of its
michael@0 27 contributors may be used to endorse or promote products derived from
michael@0 28 this software without specific prior written permission.
michael@0 29
michael@0 30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 41 */
michael@0 42
michael@0 43
michael@0 44 #include <string>
michael@0 45 #include <vector>
michael@0 46
michael@0 47 #include "nsCOMPtr.h"
michael@0 48 #include "nsComponentManagerUtils.h"
michael@0 49 #include "nsError.h"
michael@0 50 #include "nsIEventTarget.h"
michael@0 51 #include "nsNetCID.h"
michael@0 52 #include "nsComponentManagerUtils.h"
michael@0 53 #include "nsServiceManagerUtils.h"
michael@0 54
michael@0 55 // nICEr includes
michael@0 56 extern "C" {
michael@0 57 #include "nr_api.h"
michael@0 58 #include "registry.h"
michael@0 59 #include "async_timer.h"
michael@0 60 #include "ice_util.h"
michael@0 61 #include "transport_addr.h"
michael@0 62 #include "nr_crypto.h"
michael@0 63 #include "nr_socket.h"
michael@0 64 #include "nr_socket_local.h"
michael@0 65 #include "stun_client_ctx.h"
michael@0 66 #include "stun_server_ctx.h"
michael@0 67 #include "ice_ctx.h"
michael@0 68 #include "ice_candidate.h"
michael@0 69 #include "ice_handler.h"
michael@0 70 }
michael@0 71
michael@0 72 // Local includes
michael@0 73 #include "logging.h"
michael@0 74 #include "nricectx.h"
michael@0 75 #include "nricemediastream.h"
michael@0 76 #include "transportflow.h"
michael@0 77 #include "transportlayerice.h"
michael@0 78
michael@0 79 namespace mozilla {
michael@0 80
michael@0 81 #ifdef ERROR
michael@0 82 #undef ERROR
michael@0 83 #endif
michael@0 84
michael@0 85 MOZ_MTLOG_MODULE("mtransport")
michael@0 86
michael@0 87 TransportLayerIce::TransportLayerIce(const std::string& name,
michael@0 88 RefPtr<NrIceCtx> ctx, RefPtr<NrIceMediaStream> stream,
michael@0 89 int component)
michael@0 90 : name_(name), ctx_(ctx), stream_(stream), component_(component) {
michael@0 91 target_ = ctx->thread();
michael@0 92
michael@0 93 stream_->SignalReady.connect(this, &TransportLayerIce::IceReady);
michael@0 94 stream_->SignalFailed.connect(this, &TransportLayerIce::IceFailed);
michael@0 95 stream_->SignalPacketReceived.connect(this,
michael@0 96 &TransportLayerIce::IcePacketReceived);
michael@0 97 if (stream_->state() == NrIceMediaStream::ICE_OPEN) {
michael@0 98 TL_SET_STATE(TS_OPEN);
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 TransportLayerIce::~TransportLayerIce() {
michael@0 103 // No need to do anything here, since we use smart pointers
michael@0 104 }
michael@0 105
michael@0 106 TransportResult TransportLayerIce::SendPacket(const unsigned char *data,
michael@0 107 size_t len) {
michael@0 108 CheckThread();
michael@0 109 nsresult res = stream_->SendPacket(component_, data, len);
michael@0 110
michael@0 111 if (!NS_SUCCEEDED(res)) {
michael@0 112 return (res == NS_BASE_STREAM_WOULD_BLOCK) ?
michael@0 113 TE_WOULDBLOCK : TE_ERROR;
michael@0 114 }
michael@0 115
michael@0 116 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << " SendPacket(" << len << ") succeeded");
michael@0 117
michael@0 118 return len;
michael@0 119 }
michael@0 120
michael@0 121
michael@0 122 void TransportLayerIce::IceCandidate(NrIceMediaStream *stream,
michael@0 123 const std::string&) {
michael@0 124 // NO-OP for now
michael@0 125 }
michael@0 126
michael@0 127 void TransportLayerIce::IceReady(NrIceMediaStream *stream) {
michael@0 128 CheckThread();
michael@0 129 TL_SET_STATE(TS_OPEN);
michael@0 130 }
michael@0 131
michael@0 132 void TransportLayerIce::IceFailed(NrIceMediaStream *stream) {
michael@0 133 CheckThread();
michael@0 134 TL_SET_STATE(TS_ERROR);
michael@0 135 }
michael@0 136
michael@0 137 void TransportLayerIce::IcePacketReceived(NrIceMediaStream *stream, int component,
michael@0 138 const unsigned char *data, int len) {
michael@0 139 CheckThread();
michael@0 140 // We get packets for both components, so ignore the ones that aren't
michael@0 141 // for us.
michael@0 142 if (component_ != component)
michael@0 143 return;
michael@0 144
michael@0 145 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "PacketReceived(" << stream->name() << ","
michael@0 146 << component << "," << len << ")");
michael@0 147 SignalPacketReceived(this, data, len);
michael@0 148 }
michael@0 149
michael@0 150 } // close namespace

mercurial