media/mtransport/nriceresolverfake.h

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 #ifndef nriceresolverfake_h__
michael@0 45 #define nriceresolverfake_h__
michael@0 46
michael@0 47 #include <map>
michael@0 48 #include <string>
michael@0 49
michael@0 50 #include "nspr.h"
michael@0 51 #include "prnetdb.h"
michael@0 52 #include "mozilla/NullPtr.h"
michael@0 53
michael@0 54 typedef struct nr_resolver_ nr_resolver;
michael@0 55 typedef struct nr_resolver_vtbl_ nr_resolver_vtbl;
michael@0 56 typedef struct nr_transport_addr_ nr_transport_addr;
michael@0 57 typedef struct nr_resolver_resource_ nr_resolver_resource;
michael@0 58
michael@0 59 namespace mozilla {
michael@0 60
michael@0 61 class NrIceResolverFake {
michael@0 62 public:
michael@0 63 NrIceResolverFake();
michael@0 64 ~NrIceResolverFake();
michael@0 65
michael@0 66 void SetAddr(const std::string& hostname, const PRNetAddr& addr) {
michael@0 67 addrs_[hostname] = addr;
michael@0 68 }
michael@0 69
michael@0 70 nr_resolver *AllocateResolver();
michael@0 71
michael@0 72 void DestroyResolver();
michael@0 73
michael@0 74 private:
michael@0 75 // Implementations of vtbl functions
michael@0 76 static int destroy(void **objp);
michael@0 77 static int resolve(void *obj,
michael@0 78 nr_resolver_resource *resource,
michael@0 79 int (*cb)(void *cb_arg,
michael@0 80 nr_transport_addr *addr),
michael@0 81 void *cb_arg,
michael@0 82 void **handle);
michael@0 83 static void resolve_cb(NR_SOCKET s, int how, void *cb_arg);
michael@0 84 static int cancel(void *obj, void *handle);
michael@0 85
michael@0 86 // Get an address.
michael@0 87 const PRNetAddr *Resolve(const std::string& hostname) {
michael@0 88 if (!addrs_.count(hostname))
michael@0 89 return nullptr;
michael@0 90
michael@0 91 return &addrs_[hostname];
michael@0 92 }
michael@0 93
michael@0 94
michael@0 95 struct PendingResolution {
michael@0 96 PendingResolution(NrIceResolverFake *resolver,
michael@0 97 const std::string& hostname,
michael@0 98 uint16_t port,
michael@0 99 int transport,
michael@0 100 int (*cb)(void *cb_arg, nr_transport_addr *addr),
michael@0 101 void *cb_arg) :
michael@0 102 resolver_(resolver),
michael@0 103 hostname_(hostname),
michael@0 104 port_(port),
michael@0 105 transport_(transport),
michael@0 106 cb_(cb), cb_arg_(cb_arg) {}
michael@0 107
michael@0 108 NrIceResolverFake *resolver_;
michael@0 109 std::string hostname_;
michael@0 110 uint16_t port_;
michael@0 111 int transport_;
michael@0 112 int (*cb_)(void *cb_arg, nr_transport_addr *addr);
michael@0 113 void *cb_arg_;
michael@0 114 void *timer_handle_;
michael@0 115 };
michael@0 116
michael@0 117 nr_resolver_vtbl* vtbl_;
michael@0 118 std::map<std::string, PRNetAddr> addrs_;
michael@0 119 uint32_t delay_ms_;
michael@0 120 int allocated_resolvers_;
michael@0 121 };
michael@0 122
michael@0 123 } // End of namespace mozilla
michael@0 124
michael@0 125 #endif

mercurial