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 | #ifndef stunserver_h__ |
michael@0 | 10 | #define stunserver_h__ |
michael@0 | 11 | |
michael@0 | 12 | #include <map> |
michael@0 | 13 | #include <string> |
michael@0 | 14 | #include "prio.h" |
michael@0 | 15 | #include "nsError.h" |
michael@0 | 16 | |
michael@0 | 17 | typedef struct nr_stun_server_ctx_ nr_stun_server_ctx; |
michael@0 | 18 | typedef struct nr_socket_ nr_socket; |
michael@0 | 19 | typedef struct nr_local_addr_ nr_local_addr; |
michael@0 | 20 | |
michael@0 | 21 | namespace mozilla { |
michael@0 | 22 | |
michael@0 | 23 | class TestStunServer { |
michael@0 | 24 | public: |
michael@0 | 25 | // Generally, you should only call API in this class from the same thread that |
michael@0 | 26 | // the initial |GetInstance| call was made from. |
michael@0 | 27 | static TestStunServer *GetInstance(); |
michael@0 | 28 | static void ShutdownInstance(); |
michael@0 | 29 | // |ConfigurePort| will only have an effect if called before the first call |
michael@0 | 30 | // to |GetInstance| (possibly following a |ShutdownInstance| call) |
michael@0 | 31 | static void ConfigurePort(uint16_t port); |
michael@0 | 32 | static TestStunServer *Create(); |
michael@0 | 33 | |
michael@0 | 34 | ~TestStunServer(); |
michael@0 | 35 | |
michael@0 | 36 | void SetActive(bool active); |
michael@0 | 37 | void SetDelay(uint32_t delay_ms); |
michael@0 | 38 | void SetDropInitialPackets(uint32_t count); |
michael@0 | 39 | const std::string& addr() const { return listen_addr_; } |
michael@0 | 40 | uint16_t port() const { return listen_port_; } |
michael@0 | 41 | |
michael@0 | 42 | // These should only be called from the same thread as the initial |
michael@0 | 43 | // |GetInstance| call. |
michael@0 | 44 | nsresult SetResponseAddr(nr_transport_addr *addr); |
michael@0 | 45 | nsresult SetResponseAddr(const std::string& addr, uint16_t port); |
michael@0 | 46 | |
michael@0 | 47 | void Reset(); |
michael@0 | 48 | |
michael@0 | 49 | private: |
michael@0 | 50 | TestStunServer() |
michael@0 | 51 | : listen_sock_(nullptr), |
michael@0 | 52 | send_sock_(nullptr), |
michael@0 | 53 | stun_server_(nullptr), |
michael@0 | 54 | active_(true), |
michael@0 | 55 | delay_ms_(0), |
michael@0 | 56 | initial_ct_(0), |
michael@0 | 57 | response_addr_(nullptr), |
michael@0 | 58 | timer_handle_(nullptr), |
michael@0 | 59 | listen_port_(0) {} |
michael@0 | 60 | |
michael@0 | 61 | void Process(const uint8_t *msg, size_t len, nr_transport_addr *addr_in); |
michael@0 | 62 | int TryOpenListenSocket(nr_local_addr* addr, uint16_t port); |
michael@0 | 63 | |
michael@0 | 64 | static void readable_cb(NR_SOCKET sock, int how, void *cb_arg); |
michael@0 | 65 | static void process_cb(NR_SOCKET sock, int how, void *cb_arg); |
michael@0 | 66 | |
michael@0 | 67 | nr_socket *listen_sock_; |
michael@0 | 68 | nr_socket *send_sock_; |
michael@0 | 69 | nr_stun_server_ctx *stun_server_; |
michael@0 | 70 | bool active_; |
michael@0 | 71 | uint32_t delay_ms_; |
michael@0 | 72 | uint32_t initial_ct_; |
michael@0 | 73 | nr_transport_addr *response_addr_; |
michael@0 | 74 | void *timer_handle_; |
michael@0 | 75 | std::map<std::string, uint32_t> received_ct_; |
michael@0 | 76 | std::string listen_addr_; |
michael@0 | 77 | uint16_t listen_port_; |
michael@0 | 78 | |
michael@0 | 79 | static TestStunServer* instance; |
michael@0 | 80 | static uint16_t instance_port; |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | } // End of namespace mozilla |
michael@0 | 84 | |
michael@0 | 85 | #endif |