media/mtransport/nriceresolverfake.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
michael@0 8 // Original author: ekr@rtfm.com
michael@0 9
michael@0 10 // Some of this code is cut-and-pasted from nICEr. Copyright is:
michael@0 11
michael@0 12 /*
michael@0 13 Copyright (c) 2007, Adobe Systems, Incorporated
michael@0 14 All rights reserved.
michael@0 15
michael@0 16 Redistribution and use in source and binary forms, with or without
michael@0 17 modification, are permitted provided that the following conditions are
michael@0 18 met:
michael@0 19
michael@0 20 * Redistributions of source code must retain the above copyright
michael@0 21 notice, this list of conditions and the following disclaimer.
michael@0 22
michael@0 23 * Redistributions in binary form must reproduce the above copyright
michael@0 24 notice, this list of conditions and the following disclaimer in the
michael@0 25 documentation and/or other materials provided with the distribution.
michael@0 26
michael@0 27 * Neither the name of Adobe Systems, Network Resonance nor the names of its
michael@0 28 contributors may be used to endorse or promote products derived from
michael@0 29 this software without specific prior written permission.
michael@0 30
michael@0 31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 32 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 33 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 34 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 35 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 36 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 37 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 38 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 39 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 40 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 41 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 42 */
michael@0 43
michael@0 44 #include "nspr.h"
michael@0 45 #include "prnetdb.h"
michael@0 46
michael@0 47 #include "mozilla/Assertions.h"
michael@0 48
michael@0 49 extern "C" {
michael@0 50 #include "nr_api.h"
michael@0 51 #include "async_timer.h"
michael@0 52 #include "nr_resolver.h"
michael@0 53 #include "transport_addr.h"
michael@0 54 }
michael@0 55
michael@0 56 #include "nriceresolverfake.h"
michael@0 57 #include "nr_socket_prsock.h"
michael@0 58
michael@0 59 namespace mozilla {
michael@0 60
michael@0 61 NrIceResolverFake::NrIceResolverFake() :
michael@0 62 vtbl_(new nr_resolver_vtbl), addrs_(), delay_ms_(100),
michael@0 63 allocated_resolvers_(0) {
michael@0 64 vtbl_->destroy = &NrIceResolverFake::destroy;
michael@0 65 vtbl_->resolve = &NrIceResolverFake::resolve;
michael@0 66 vtbl_->cancel = &NrIceResolverFake::cancel;
michael@0 67 }
michael@0 68
michael@0 69 NrIceResolverFake::~NrIceResolverFake() {
michael@0 70 MOZ_ASSERT(allocated_resolvers_ == 0);
michael@0 71 delete vtbl_;
michael@0 72 }
michael@0 73
michael@0 74
michael@0 75 nr_resolver *NrIceResolverFake::AllocateResolver() {
michael@0 76 nr_resolver *resolver;
michael@0 77
michael@0 78 int r = nr_resolver_create_int((void *)this,
michael@0 79 vtbl_, &resolver);
michael@0 80 MOZ_ASSERT(!r);
michael@0 81 if(r)
michael@0 82 return nullptr;
michael@0 83
michael@0 84 ++allocated_resolvers_;
michael@0 85
michael@0 86 return resolver;
michael@0 87 }
michael@0 88
michael@0 89 void NrIceResolverFake::DestroyResolver() {
michael@0 90 --allocated_resolvers_;
michael@0 91 }
michael@0 92
michael@0 93 int NrIceResolverFake::destroy(void **objp) {
michael@0 94 if (!objp || !*objp)
michael@0 95 return 0;
michael@0 96
michael@0 97 NrIceResolverFake *fake = static_cast<NrIceResolverFake *>(*objp);
michael@0 98 *objp = 0;
michael@0 99
michael@0 100 fake->DestroyResolver();
michael@0 101
michael@0 102 return 0;
michael@0 103 }
michael@0 104
michael@0 105 int NrIceResolverFake::resolve(void *obj,
michael@0 106 nr_resolver_resource *resource,
michael@0 107 int (*cb)(void *cb_arg,
michael@0 108 nr_transport_addr *addr),
michael@0 109 void *cb_arg,
michael@0 110 void **handle) {
michael@0 111 int r,_status;
michael@0 112
michael@0 113 MOZ_ASSERT(obj);
michael@0 114 NrIceResolverFake *fake = static_cast<NrIceResolverFake *>(obj);
michael@0 115
michael@0 116 MOZ_ASSERT(fake->allocated_resolvers_ > 0);
michael@0 117
michael@0 118 PendingResolution *pending =
michael@0 119 new PendingResolution(fake,
michael@0 120 resource->domain_name,
michael@0 121 resource->port ? resource->port : 3478,
michael@0 122 resource->transport_protocol ?
michael@0 123 resource->transport_protocol :
michael@0 124 IPPROTO_UDP,
michael@0 125 cb, cb_arg);
michael@0 126
michael@0 127 if ((r=NR_ASYNC_TIMER_SET(fake->delay_ms_,NrIceResolverFake::resolve_cb,
michael@0 128 (void *)pending, &pending->timer_handle_))) {
michael@0 129 delete pending;
michael@0 130 ABORT(r);
michael@0 131 }
michael@0 132 *handle = pending;
michael@0 133
michael@0 134 _status=0;
michael@0 135 abort:
michael@0 136 return(_status);
michael@0 137 }
michael@0 138
michael@0 139 void NrIceResolverFake::resolve_cb(NR_SOCKET s, int how, void *cb_arg) {
michael@0 140 MOZ_ASSERT(cb_arg);
michael@0 141 PendingResolution *pending = static_cast<PendingResolution *>(cb_arg);
michael@0 142
michael@0 143 const PRNetAddr *addr=pending->resolver_->Resolve(pending->hostname_);
michael@0 144
michael@0 145 if (addr) {
michael@0 146 nr_transport_addr transport_addr;
michael@0 147
michael@0 148 int r = nr_praddr_to_transport_addr(addr, &transport_addr,
michael@0 149 pending->transport_, 0);
michael@0 150 MOZ_ASSERT(!r);
michael@0 151 if (r)
michael@0 152 goto abort;
michael@0 153
michael@0 154 r=nr_transport_addr_set_port(&transport_addr, pending->port_);
michael@0 155 MOZ_ASSERT(!r);
michael@0 156 if (r)
michael@0 157 goto abort;
michael@0 158
michael@0 159 /* Fill in the address string */
michael@0 160 r=nr_transport_addr_fmt_addr_string(&transport_addr);
michael@0 161 MOZ_ASSERT(!r);
michael@0 162 if (r)
michael@0 163 goto abort;
michael@0 164
michael@0 165 pending->cb_(pending->cb_arg_, &transport_addr);
michael@0 166 delete pending;
michael@0 167 return;
michael@0 168 }
michael@0 169
michael@0 170 abort:
michael@0 171 // Resolution failed.
michael@0 172 pending->cb_(pending->cb_arg_, nullptr);
michael@0 173
michael@0 174 delete pending;
michael@0 175 }
michael@0 176
michael@0 177 int NrIceResolverFake::cancel(void *obj, void *handle) {
michael@0 178 MOZ_ASSERT(obj);
michael@0 179 MOZ_ASSERT(static_cast<NrIceResolverFake *>(obj)->allocated_resolvers_ > 0);
michael@0 180
michael@0 181 PendingResolution *pending = static_cast<PendingResolution *>(handle);
michael@0 182
michael@0 183 NR_async_timer_cancel(pending->timer_handle_);
michael@0 184 delete pending;
michael@0 185
michael@0 186 return(0);
michael@0 187 }
michael@0 188
michael@0 189
michael@0 190 } // End of namespace mozilla

mercurial