Tue, 06 Jan 2015 21:39:09 +0100
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: 2; 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 | extern "C" { |
michael@0 | 7 | #include <arpa/inet.h> |
michael@0 | 8 | #include "r_types.h" |
michael@0 | 9 | #include "stun.h" |
michael@0 | 10 | #include "addrs.h" |
michael@0 | 11 | } |
michael@0 | 12 | |
michael@0 | 13 | #include <vector> |
michael@0 | 14 | #include <string> |
michael@0 | 15 | #include "nsINetworkManager.h" |
michael@0 | 16 | #include "nsINetworkInterfaceListService.h" |
michael@0 | 17 | #include "runnable_utils.h" |
michael@0 | 18 | #include "nsCOMPtr.h" |
michael@0 | 19 | #include "nsMemory.h" |
michael@0 | 20 | #include "nsThreadUtils.h" |
michael@0 | 21 | #include "nsServiceManagerUtils.h" |
michael@0 | 22 | #include "mozilla/SyncRunnable.h" |
michael@0 | 23 | |
michael@0 | 24 | namespace { |
michael@0 | 25 | struct NetworkInterface { |
michael@0 | 26 | struct sockaddr_in addr; |
michael@0 | 27 | std::string name; |
michael@0 | 28 | // See NR_INTERFACE_TYPE_* in nICEr/src/net/local_addrs.h |
michael@0 | 29 | int type; |
michael@0 | 30 | }; |
michael@0 | 31 | |
michael@0 | 32 | nsresult |
michael@0 | 33 | GetInterfaces(std::vector<NetworkInterface>* aInterfaces) |
michael@0 | 34 | { |
michael@0 | 35 | MOZ_ASSERT(aInterfaces); |
michael@0 | 36 | |
michael@0 | 37 | // Obtain network interfaces from network manager. |
michael@0 | 38 | nsresult rv; |
michael@0 | 39 | nsCOMPtr<nsINetworkInterfaceListService> listService = |
michael@0 | 40 | do_GetService("@mozilla.org/network/interface-list-service;1", &rv); |
michael@0 | 41 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 42 | |
michael@0 | 43 | int32_t flags = |
michael@0 | 44 | nsINetworkInterfaceListService::LIST_NOT_INCLUDE_SUPL_INTERFACES | |
michael@0 | 45 | nsINetworkInterfaceListService::LIST_NOT_INCLUDE_MMS_INTERFACES | |
michael@0 | 46 | nsINetworkInterfaceListService::LIST_NOT_INCLUDE_IMS_INTERFACES | |
michael@0 | 47 | nsINetworkInterfaceListService::LIST_NOT_INCLUDE_DUN_INTERFACES; |
michael@0 | 48 | nsCOMPtr<nsINetworkInterfaceList> networkList; |
michael@0 | 49 | NS_ENSURE_SUCCESS(listService->GetDataInterfaceList(flags, |
michael@0 | 50 | getter_AddRefs(networkList)), |
michael@0 | 51 | NS_ERROR_FAILURE); |
michael@0 | 52 | |
michael@0 | 53 | // Translate nsINetworkInterfaceList to NetworkInterface. |
michael@0 | 54 | int32_t listLength; |
michael@0 | 55 | NS_ENSURE_SUCCESS(networkList->GetNumberOfInterface(&listLength), |
michael@0 | 56 | NS_ERROR_FAILURE); |
michael@0 | 57 | aInterfaces->clear(); |
michael@0 | 58 | |
michael@0 | 59 | for (int32_t i = 0; i < listLength; i++) { |
michael@0 | 60 | nsCOMPtr<nsINetworkInterface> iface; |
michael@0 | 61 | if (NS_FAILED(networkList->GetInterface(i, getter_AddRefs(iface)))) { |
michael@0 | 62 | continue; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | char16_t **ips = nullptr; |
michael@0 | 66 | uint32_t *prefixs = nullptr; |
michael@0 | 67 | uint32_t count = 0; |
michael@0 | 68 | bool isAddressGot = false; |
michael@0 | 69 | NetworkInterface interface; |
michael@0 | 70 | memset(&(interface.addr), 0, sizeof(interface.addr)); |
michael@0 | 71 | interface.addr.sin_family = AF_INET; |
michael@0 | 72 | |
michael@0 | 73 | if (NS_FAILED(iface->GetAddresses(&ips, &prefixs, &count))) { |
michael@0 | 74 | continue; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | for (uint32_t j = 0; j < count; j++) { |
michael@0 | 78 | nsAutoString ip; |
michael@0 | 79 | |
michael@0 | 80 | ip.Assign(ips[j]); |
michael@0 | 81 | if (inet_pton(AF_INET, NS_ConvertUTF16toUTF8(ip).get(), |
michael@0 | 82 | &(interface.addr.sin_addr.s_addr)) == 1) { |
michael@0 | 83 | isAddressGot = true; |
michael@0 | 84 | break; |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | nsMemory::Free(prefixs); |
michael@0 | 89 | NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, ips); |
michael@0 | 90 | |
michael@0 | 91 | if (!isAddressGot) { |
michael@0 | 92 | continue; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | nsAutoString ifaceName; |
michael@0 | 96 | if (NS_FAILED(iface->GetName(ifaceName))) { |
michael@0 | 97 | continue; |
michael@0 | 98 | } |
michael@0 | 99 | interface.name = NS_ConvertUTF16toUTF8(ifaceName).get(); |
michael@0 | 100 | |
michael@0 | 101 | int32_t type; |
michael@0 | 102 | if (NS_FAILED(iface->GetType(&type))) { |
michael@0 | 103 | continue; |
michael@0 | 104 | } |
michael@0 | 105 | switch (type) { |
michael@0 | 106 | case nsINetworkInterface::NETWORK_TYPE_WIFI: |
michael@0 | 107 | interface.type = NR_INTERFACE_TYPE_WIFI; |
michael@0 | 108 | break; |
michael@0 | 109 | case nsINetworkInterface::NETWORK_TYPE_MOBILE: |
michael@0 | 110 | interface.type = NR_INTERFACE_TYPE_MOBILE; |
michael@0 | 111 | break; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | aInterfaces->push_back(interface); |
michael@0 | 115 | } |
michael@0 | 116 | return NS_OK; |
michael@0 | 117 | } |
michael@0 | 118 | } // anonymous namespace |
michael@0 | 119 | |
michael@0 | 120 | int |
michael@0 | 121 | nr_stun_get_addrs(nr_local_addr aAddrs[], int aMaxAddrs, |
michael@0 | 122 | int aDropLoopback, int* aCount) |
michael@0 | 123 | { |
michael@0 | 124 | nsresult rv; |
michael@0 | 125 | int r; |
michael@0 | 126 | |
michael@0 | 127 | // Get network interface list. |
michael@0 | 128 | std::vector<NetworkInterface> interfaces; |
michael@0 | 129 | nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); |
michael@0 | 130 | mozilla::SyncRunnable::DispatchToThread( |
michael@0 | 131 | mainThread.get(), |
michael@0 | 132 | mozilla::WrapRunnableNMRet(&GetInterfaces, &interfaces, &rv), |
michael@0 | 133 | false); |
michael@0 | 134 | if (NS_FAILED(rv)) { |
michael@0 | 135 | return R_FAILED; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | // Translate to nr_transport_addr. |
michael@0 | 139 | int32_t n = 0; |
michael@0 | 140 | size_t num_interface = std::min(interfaces.size(), (size_t)aMaxAddrs); |
michael@0 | 141 | for (size_t i = 0; i < num_interface; ++i) { |
michael@0 | 142 | NetworkInterface &interface = interfaces[i]; |
michael@0 | 143 | if (nr_sockaddr_to_transport_addr((sockaddr*)&(interface.addr), |
michael@0 | 144 | sizeof(struct sockaddr_in), |
michael@0 | 145 | IPPROTO_UDP, 0, &(aAddrs[n].addr))) { |
michael@0 | 146 | r_log(NR_LOG_STUN, LOG_WARNING, "Problem transforming address"); |
michael@0 | 147 | return R_FAILED; |
michael@0 | 148 | } |
michael@0 | 149 | strlcpy(aAddrs[n].addr.ifname, interface.name.c_str(), |
michael@0 | 150 | sizeof(aAddrs[n].addr.ifname)); |
michael@0 | 151 | aAddrs[n].interface.type = interface.type; |
michael@0 | 152 | aAddrs[n].interface.estimated_speed = 0; |
michael@0 | 153 | n++; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | *aCount = n; |
michael@0 | 157 | r = nr_stun_remove_duplicate_addrs(aAddrs, aDropLoopback, aCount); |
michael@0 | 158 | if (r != 0) { |
michael@0 | 159 | return r; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | for (int i = 0; i < *aCount; ++i) { |
michael@0 | 163 | char typestr[100]; |
michael@0 | 164 | nr_local_addr_fmt_info_string(aAddrs + i, typestr, sizeof(typestr)); |
michael@0 | 165 | r_log(NR_LOG_STUN, LOG_DEBUG, "Address %d: %s on %s, type: %s\n", |
michael@0 | 166 | i, aAddrs[i].addr.as_string, aAddrs[i].addr.ifname, typestr); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | return 0; |
michael@0 | 170 | } |