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: 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 source code here from nICEr. Copyright is: |
michael@0 | 10 | |
michael@0 | 11 | Copyright (c) 2007, Adobe Systems, Incorporated |
michael@0 | 12 | All rights reserved. |
michael@0 | 13 | |
michael@0 | 14 | Redistribution and use in source and binary forms, with or without |
michael@0 | 15 | modification, are permitted provided that the following conditions are |
michael@0 | 16 | met: |
michael@0 | 17 | |
michael@0 | 18 | * Redistributions of source code must retain the above copyright |
michael@0 | 19 | notice, this list of conditions and the following disclaimer. |
michael@0 | 20 | |
michael@0 | 21 | * Redistributions in binary form must reproduce the above copyright |
michael@0 | 22 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 23 | documentation and/or other materials provided with the distribution. |
michael@0 | 24 | |
michael@0 | 25 | * Neither the name of Adobe Systems, Network Resonance nor the names of its |
michael@0 | 26 | contributors may be used to endorse or promote products derived from |
michael@0 | 27 | this software without specific prior written permission. |
michael@0 | 28 | |
michael@0 | 29 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 30 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 31 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 32 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 33 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 34 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 35 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 36 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 37 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 38 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 39 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 40 | */ |
michael@0 | 41 | |
michael@0 | 42 | |
michael@0 | 43 | // Implementation of nICEr/nr_socket that is tied to the Gecko |
michael@0 | 44 | // SocketTransportService. |
michael@0 | 45 | |
michael@0 | 46 | #ifndef nr_socket_prsock__ |
michael@0 | 47 | #define nr_socket_prsock__ |
michael@0 | 48 | |
michael@0 | 49 | #include <queue> |
michael@0 | 50 | |
michael@0 | 51 | #include "nspr.h" |
michael@0 | 52 | #include "prio.h" |
michael@0 | 53 | |
michael@0 | 54 | #include "nsAutoPtr.h" |
michael@0 | 55 | #include "nsCOMPtr.h" |
michael@0 | 56 | #include "nsASocketHandler.h" |
michael@0 | 57 | #include "nsISocketTransportService.h" |
michael@0 | 58 | #include "nsXPCOM.h" |
michael@0 | 59 | #include "nsIEventTarget.h" |
michael@0 | 60 | #include "nsIUDPSocketChild.h" |
michael@0 | 61 | |
michael@0 | 62 | #include "databuffer.h" |
michael@0 | 63 | #include "m_cpp_utils.h" |
michael@0 | 64 | #include "mozilla/ReentrantMonitor.h" |
michael@0 | 65 | #include "mozilla/RefPtr.h" |
michael@0 | 66 | |
michael@0 | 67 | // Stub declaration for nICEr type |
michael@0 | 68 | typedef struct nr_socket_vtbl_ nr_socket_vtbl; |
michael@0 | 69 | |
michael@0 | 70 | namespace mozilla { |
michael@0 | 71 | |
michael@0 | 72 | namespace net { |
michael@0 | 73 | union NetAddr; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | class NrSocketBase { |
michael@0 | 77 | public: |
michael@0 | 78 | NrSocketBase() : connect_invoked_(false), poll_flags_(0) { |
michael@0 | 79 | memset(cbs_, 0, sizeof(cbs_)); |
michael@0 | 80 | memset(cb_args_, 0, sizeof(cb_args_)); |
michael@0 | 81 | memset(&my_addr_, 0, sizeof(my_addr_)); |
michael@0 | 82 | } |
michael@0 | 83 | virtual ~NrSocketBase() {} |
michael@0 | 84 | |
michael@0 | 85 | // the nr_socket APIs |
michael@0 | 86 | virtual int create(nr_transport_addr *addr) = 0; |
michael@0 | 87 | virtual int sendto(const void *msg, size_t len, |
michael@0 | 88 | int flags, nr_transport_addr *to) = 0; |
michael@0 | 89 | virtual int recvfrom(void * buf, size_t maxlen, |
michael@0 | 90 | size_t *len, int flags, |
michael@0 | 91 | nr_transport_addr *from) = 0; |
michael@0 | 92 | virtual int getaddr(nr_transport_addr *addrp) = 0; |
michael@0 | 93 | virtual void close() = 0; |
michael@0 | 94 | virtual int connect(nr_transport_addr *addr) = 0; |
michael@0 | 95 | virtual int write(const void *msg, size_t len, size_t *written) = 0; |
michael@0 | 96 | virtual int read(void* buf, size_t maxlen, size_t *len) = 0; |
michael@0 | 97 | |
michael@0 | 98 | // Implementations of the async_event APIs |
michael@0 | 99 | virtual int async_wait(int how, NR_async_cb cb, void *cb_arg, |
michael@0 | 100 | char *function, int line); |
michael@0 | 101 | virtual int cancel(int how); |
michael@0 | 102 | |
michael@0 | 103 | // nsISupport reference counted interface |
michael@0 | 104 | NS_IMETHOD_(MozExternalRefCountType) AddRef(void) = 0; |
michael@0 | 105 | NS_IMETHOD_(MozExternalRefCountType) Release(void) = 0; |
michael@0 | 106 | |
michael@0 | 107 | uint32_t poll_flags() { |
michael@0 | 108 | return poll_flags_; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | virtual nr_socket_vtbl *vtbl(); // To access in test classes. |
michael@0 | 112 | |
michael@0 | 113 | protected: |
michael@0 | 114 | void fire_callback(int how); |
michael@0 | 115 | |
michael@0 | 116 | bool connect_invoked_; |
michael@0 | 117 | nr_transport_addr my_addr_; |
michael@0 | 118 | |
michael@0 | 119 | private: |
michael@0 | 120 | NR_async_cb cbs_[NR_ASYNC_WAIT_WRITE + 1]; |
michael@0 | 121 | void *cb_args_[NR_ASYNC_WAIT_WRITE + 1]; |
michael@0 | 122 | uint32_t poll_flags_; |
michael@0 | 123 | }; |
michael@0 | 124 | |
michael@0 | 125 | class NrSocket : public NrSocketBase, |
michael@0 | 126 | public nsASocketHandler { |
michael@0 | 127 | public: |
michael@0 | 128 | NrSocket() : fd_(nullptr) {} |
michael@0 | 129 | virtual ~NrSocket() { |
michael@0 | 130 | PR_Close(fd_); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | // Implement nsASocket |
michael@0 | 134 | virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags); |
michael@0 | 135 | virtual void OnSocketDetached(PRFileDesc *fd); |
michael@0 | 136 | virtual void IsLocal(bool *aIsLocal); |
michael@0 | 137 | virtual uint64_t ByteCountSent() { return 0; } |
michael@0 | 138 | virtual uint64_t ByteCountReceived() { return 0; } |
michael@0 | 139 | |
michael@0 | 140 | // nsISupports methods |
michael@0 | 141 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 142 | |
michael@0 | 143 | // Implementations of the async_event APIs |
michael@0 | 144 | virtual int async_wait(int how, NR_async_cb cb, void *cb_arg, |
michael@0 | 145 | char *function, int line); |
michael@0 | 146 | virtual int cancel(int how); |
michael@0 | 147 | |
michael@0 | 148 | |
michael@0 | 149 | // Implementations of the nr_socket APIs |
michael@0 | 150 | virtual int create(nr_transport_addr *addr); // (really init, but it's called create) |
michael@0 | 151 | virtual int sendto(const void *msg, size_t len, |
michael@0 | 152 | int flags, nr_transport_addr *to); |
michael@0 | 153 | virtual int recvfrom(void * buf, size_t maxlen, |
michael@0 | 154 | size_t *len, int flags, |
michael@0 | 155 | nr_transport_addr *from); |
michael@0 | 156 | virtual int getaddr(nr_transport_addr *addrp); |
michael@0 | 157 | virtual void close(); |
michael@0 | 158 | virtual int connect(nr_transport_addr *addr); |
michael@0 | 159 | virtual int write(const void *msg, size_t len, size_t *written); |
michael@0 | 160 | virtual int read(void* buf, size_t maxlen, size_t *len); |
michael@0 | 161 | |
michael@0 | 162 | private: |
michael@0 | 163 | DISALLOW_COPY_ASSIGN(NrSocket); |
michael@0 | 164 | |
michael@0 | 165 | PRFileDesc *fd_; |
michael@0 | 166 | nsCOMPtr<nsIEventTarget> ststhread_; |
michael@0 | 167 | }; |
michael@0 | 168 | |
michael@0 | 169 | struct nr_udp_message { |
michael@0 | 170 | nr_udp_message(const PRNetAddr &from, nsAutoPtr<DataBuffer> &data) |
michael@0 | 171 | : from(from), data(data) { |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nr_udp_message); |
michael@0 | 175 | |
michael@0 | 176 | PRNetAddr from; |
michael@0 | 177 | nsAutoPtr<DataBuffer> data; |
michael@0 | 178 | |
michael@0 | 179 | private: |
michael@0 | 180 | DISALLOW_COPY_ASSIGN(nr_udp_message); |
michael@0 | 181 | }; |
michael@0 | 182 | |
michael@0 | 183 | class NrSocketIpc : public NrSocketBase, |
michael@0 | 184 | public nsIUDPSocketInternal { |
michael@0 | 185 | public: |
michael@0 | 186 | |
michael@0 | 187 | enum NrSocketIpcState { |
michael@0 | 188 | NR_INIT, |
michael@0 | 189 | NR_CONNECTING, |
michael@0 | 190 | NR_CONNECTED, |
michael@0 | 191 | NR_CLOSING, |
michael@0 | 192 | NR_CLOSED, |
michael@0 | 193 | }; |
michael@0 | 194 | |
michael@0 | 195 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 196 | NS_DECL_NSIUDPSOCKETINTERNAL |
michael@0 | 197 | |
michael@0 | 198 | NrSocketIpc(const nsCOMPtr<nsIEventTarget> &main_thread); |
michael@0 | 199 | virtual ~NrSocketIpc() {}; |
michael@0 | 200 | |
michael@0 | 201 | // Implementations of the NrSocketBase APIs |
michael@0 | 202 | virtual int create(nr_transport_addr *addr); |
michael@0 | 203 | virtual int sendto(const void *msg, size_t len, |
michael@0 | 204 | int flags, nr_transport_addr *to); |
michael@0 | 205 | virtual int recvfrom(void * buf, size_t maxlen, |
michael@0 | 206 | size_t *len, int flags, |
michael@0 | 207 | nr_transport_addr *from); |
michael@0 | 208 | virtual int getaddr(nr_transport_addr *addrp); |
michael@0 | 209 | virtual void close(); |
michael@0 | 210 | virtual int connect(nr_transport_addr *addr); |
michael@0 | 211 | virtual int write(const void *msg, size_t len, size_t *written); |
michael@0 | 212 | virtual int read(void* buf, size_t maxlen, size_t *len); |
michael@0 | 213 | |
michael@0 | 214 | private: |
michael@0 | 215 | DISALLOW_COPY_ASSIGN(NrSocketIpc); |
michael@0 | 216 | |
michael@0 | 217 | // Main thread executors of the NrSocketBase APIs |
michael@0 | 218 | void create_m(const nsACString &host, const uint16_t port); |
michael@0 | 219 | void sendto_m(const net::NetAddr &addr, nsAutoPtr<DataBuffer> buf); |
michael@0 | 220 | void close_m(); |
michael@0 | 221 | // STS thread executor |
michael@0 | 222 | void recv_callback_s(RefPtr<nr_udp_message> msg); |
michael@0 | 223 | |
michael@0 | 224 | bool err_; |
michael@0 | 225 | NrSocketIpcState state_; |
michael@0 | 226 | std::queue<RefPtr<nr_udp_message> > received_msgs_; |
michael@0 | 227 | |
michael@0 | 228 | nsCOMPtr<nsIUDPSocketChild> socket_child_; |
michael@0 | 229 | nsCOMPtr<nsIEventTarget> sts_thread_; |
michael@0 | 230 | const nsCOMPtr<nsIEventTarget> main_thread_; |
michael@0 | 231 | ReentrantMonitor monitor_; |
michael@0 | 232 | }; |
michael@0 | 233 | |
michael@0 | 234 | int nr_netaddr_to_transport_addr(const net::NetAddr *netaddr, |
michael@0 | 235 | nr_transport_addr *addr, |
michael@0 | 236 | int protocol); |
michael@0 | 237 | int nr_praddr_to_transport_addr(const PRNetAddr *praddr, |
michael@0 | 238 | nr_transport_addr *addr, |
michael@0 | 239 | int protocol, int keep); |
michael@0 | 240 | int nr_transport_addr_get_addrstring_and_port(nr_transport_addr *addr, |
michael@0 | 241 | nsACString *host, int32_t *port); |
michael@0 | 242 | } // close namespace |
michael@0 | 243 | #endif |