media/mtransport/nrinterfaceprioritizer.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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 #include <map>
michael@0 5 #include <set>
michael@0 6 #include <string>
michael@0 7 #include "logging.h"
michael@0 8 #include "nrinterfaceprioritizer.h"
michael@0 9 #include "nsCOMPtr.h"
michael@0 10
michael@0 11 MOZ_MTLOG_MODULE("mtransport")
michael@0 12
michael@0 13 namespace {
michael@0 14
michael@0 15 class LocalAddress {
michael@0 16 public:
michael@0 17 LocalAddress()
michael@0 18 : key_(),
michael@0 19 is_vpn_(-1),
michael@0 20 estimated_speed_(-1),
michael@0 21 type_preference_(-1) {}
michael@0 22
michael@0 23 bool Init(const nr_local_addr& local_addr) {
michael@0 24 char buf[MAXIFNAME + 41];
michael@0 25 int r = nr_transport_addr_fmt_ifname_addr_string(&local_addr.addr, buf, sizeof(buf));
michael@0 26 if (r) {
michael@0 27 MOZ_MTLOG(PR_LOG_ERROR, "Error formatting interface address string.");
michael@0 28 return false;
michael@0 29 }
michael@0 30 key_ = buf;
michael@0 31 is_vpn_ = (local_addr.interface.type & NR_INTERFACE_TYPE_VPN) != 0 ? 1 : 0;
michael@0 32 estimated_speed_ = local_addr.interface.estimated_speed;
michael@0 33 type_preference_ = GetNetworkTypePreference(local_addr.interface.type);
michael@0 34 return true;
michael@0 35 }
michael@0 36
michael@0 37 bool operator<(const LocalAddress& rhs) const {
michael@0 38 // Interface that is "less" here is preferred.
michael@0 39 // If type preferences are different, we should simply sort by
michael@0 40 // |type_preference_|.
michael@0 41 if (type_preference_ != rhs.type_preference_) {
michael@0 42 return type_preference_ < rhs.type_preference_;
michael@0 43 }
michael@0 44
michael@0 45 // If type preferences are the same, the next thing we use to sort is vpn.
michael@0 46 // If two LocalAddress are different in |is_vpn_|, the LocalAddress that is
michael@0 47 // not in vpn gets priority.
michael@0 48 if (is_vpn_ != rhs.is_vpn_) {
michael@0 49 return is_vpn_ < rhs.is_vpn_;
michael@0 50 }
michael@0 51
michael@0 52 // Compare estimated speed.
michael@0 53 if (estimated_speed_ != rhs.estimated_speed_) {
michael@0 54 return estimated_speed_ > rhs.estimated_speed_;
michael@0 55 }
michael@0 56
michael@0 57 // All things above are the same, we can at least sort with key.
michael@0 58 return key_ < rhs.key_;
michael@0 59 }
michael@0 60
michael@0 61 const std::string& GetKey() const {
michael@0 62 return key_;
michael@0 63 }
michael@0 64
michael@0 65 private:
michael@0 66 // Getting the preference corresponding to a type. Getting lower number here
michael@0 67 // means the type of network is preferred.
michael@0 68 static inline int GetNetworkTypePreference(int type) {
michael@0 69 if (type & NR_INTERFACE_TYPE_WIRED) {
michael@0 70 return 1;
michael@0 71 }
michael@0 72 if (type & NR_INTERFACE_TYPE_WIFI) {
michael@0 73 return 2;
michael@0 74 }
michael@0 75 if (type & NR_INTERFACE_TYPE_MOBILE) {
michael@0 76 return 3;
michael@0 77 }
michael@0 78 return 4;
michael@0 79 }
michael@0 80
michael@0 81 std::string key_;
michael@0 82 int is_vpn_;
michael@0 83 int estimated_speed_;
michael@0 84 int type_preference_;
michael@0 85 };
michael@0 86
michael@0 87 class InterfacePrioritizer {
michael@0 88 public:
michael@0 89 InterfacePrioritizer()
michael@0 90 : local_addrs_(),
michael@0 91 preference_map_(),
michael@0 92 sorted_(false) {}
michael@0 93
michael@0 94 int add(const nr_local_addr *iface) {
michael@0 95 LocalAddress addr;
michael@0 96 if (!addr.Init(*iface)) {
michael@0 97 return R_FAILED;
michael@0 98 }
michael@0 99 std::pair<std::set<LocalAddress>::iterator, bool> r =
michael@0 100 local_addrs_.insert(addr);
michael@0 101 if (!r.second) {
michael@0 102 return R_ALREADY; // This address is already in the set.
michael@0 103 }
michael@0 104 sorted_ = false;
michael@0 105 return 0;
michael@0 106 }
michael@0 107
michael@0 108 int sort() {
michael@0 109 UCHAR tmp_pref = 127;
michael@0 110 preference_map_.clear();
michael@0 111 for (std::set<LocalAddress>::iterator i = local_addrs_.begin();
michael@0 112 i != local_addrs_.end(); ++i) {
michael@0 113 if (tmp_pref == 0) {
michael@0 114 return R_FAILED;
michael@0 115 }
michael@0 116 preference_map_.insert(make_pair(i->GetKey(), tmp_pref--));
michael@0 117 }
michael@0 118 sorted_ = true;
michael@0 119 return 0;
michael@0 120 }
michael@0 121
michael@0 122 int getPreference(const char *key, UCHAR *pref) {
michael@0 123 if (!sorted_) {
michael@0 124 return R_FAILED;
michael@0 125 }
michael@0 126 std::map<std::string, UCHAR>::iterator i = preference_map_.find(key);
michael@0 127 if (i == preference_map_.end()) {
michael@0 128 return R_NOT_FOUND;
michael@0 129 }
michael@0 130 *pref = i->second;
michael@0 131 return 0;
michael@0 132 }
michael@0 133
michael@0 134 private:
michael@0 135 std::set<LocalAddress> local_addrs_;
michael@0 136 std::map<std::string, UCHAR> preference_map_;
michael@0 137 bool sorted_;
michael@0 138 };
michael@0 139
michael@0 140 } // anonymous namespace
michael@0 141
michael@0 142 static int add_interface(void *obj, nr_local_addr *iface) {
michael@0 143 InterfacePrioritizer *ip = static_cast<InterfacePrioritizer*>(obj);
michael@0 144 return ip->add(iface);
michael@0 145 }
michael@0 146
michael@0 147 static int get_priority(void *obj, const char *key, UCHAR *pref) {
michael@0 148 InterfacePrioritizer *ip = static_cast<InterfacePrioritizer*>(obj);
michael@0 149 return ip->getPreference(key, pref);
michael@0 150 }
michael@0 151
michael@0 152 static int sort_preference(void *obj) {
michael@0 153 InterfacePrioritizer *ip = static_cast<InterfacePrioritizer*>(obj);
michael@0 154 return ip->sort();
michael@0 155 }
michael@0 156
michael@0 157 static int destroy(void **objp) {
michael@0 158 if (!objp || !*objp) {
michael@0 159 return 0;
michael@0 160 }
michael@0 161
michael@0 162 InterfacePrioritizer *ip = static_cast<InterfacePrioritizer*>(*objp);
michael@0 163 *objp = 0;
michael@0 164 delete ip;
michael@0 165
michael@0 166 return 0;
michael@0 167 }
michael@0 168
michael@0 169 static nr_interface_prioritizer_vtbl priorizer_vtbl = {
michael@0 170 add_interface,
michael@0 171 get_priority,
michael@0 172 sort_preference,
michael@0 173 destroy
michael@0 174 };
michael@0 175
michael@0 176 namespace mozilla {
michael@0 177
michael@0 178 nr_interface_prioritizer* CreateInterfacePrioritizer() {
michael@0 179 nr_interface_prioritizer *ip;
michael@0 180 int r = nr_interface_prioritizer_create_int(new InterfacePrioritizer(),
michael@0 181 &priorizer_vtbl,
michael@0 182 &ip);
michael@0 183 if (r != 0) {
michael@0 184 return nullptr;
michael@0 185 }
michael@0 186 return ip;
michael@0 187 }
michael@0 188
michael@0 189 } // namespace mozilla

mercurial