test_nr_socket.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. */
  7. /*
  8. Based partially on original code from nICEr and nrappkit.
  9. nICEr copyright:
  10. Copyright (c) 2007, Adobe Systems, Incorporated
  11. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions are
  14. met:
  15. * Redistributions of source code must retain the above copyright
  16. notice, this list of conditions and the following disclaimer.
  17. * Redistributions in binary form must reproduce the above copyright
  18. notice, this list of conditions and the following disclaimer in the
  19. documentation and/or other materials provided with the distribution.
  20. * Neither the name of Adobe Systems, Network Resonance nor the names of its
  21. contributors may be used to endorse or promote products derived from
  22. this software without specific prior written permission.
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. nrappkit copyright:
  35. Copyright (C) 2001-2003, Network Resonance, Inc.
  36. Copyright (C) 2006, Network Resonance, Inc.
  37. All Rights Reserved
  38. Redistribution and use in source and binary forms, with or without
  39. modification, are permitted provided that the following conditions
  40. are met:
  41. 1. Redistributions of source code must retain the above copyright
  42. notice, this list of conditions and the following disclaimer.
  43. 2. Redistributions in binary form must reproduce the above copyright
  44. notice, this list of conditions and the following disclaimer in the
  45. documentation and/or other materials provided with the distribution.
  46. 3. Neither the name of Network Resonance, Inc. nor the name of any
  47. contributors to this software may be used to endorse or promote
  48. products derived from this software without specific prior written
  49. permission.
  50. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
  51. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  54. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  55. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  56. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  57. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  58. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  59. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  60. POSSIBILITY OF SUCH DAMAGE.
  61. ekr@rtfm.com Thu Dec 20 20:14:49 2001
  62. */
  63. // Original author: bcampen@mozilla.com [:bwc]
  64. #ifndef test_nr_socket__
  65. #define test_nr_socket__
  66. extern "C" {
  67. #include "transport_addr.h"
  68. }
  69. #include "nr_socket_prsock.h"
  70. extern "C" {
  71. #include "nr_socket.h"
  72. }
  73. #include <set>
  74. #include <vector>
  75. #include <map>
  76. #include <list>
  77. #include <string>
  78. #include "mozilla/UniquePtr.h"
  79. #include "prinrval.h"
  80. namespace mozilla {
  81. class TestNrSocket;
  82. /**
  83. * A group of TestNrSockets that behave as if they were behind the same NAT.
  84. * @note We deliberately avoid addref/release of TestNrSocket here to avoid
  85. * masking lifetime errors elsewhere.
  86. */
  87. class TestNat {
  88. public:
  89. typedef enum {
  90. /** For mapping, one port is used for all destinations.
  91. * For filtering, allow any external address/port. */
  92. ENDPOINT_INDEPENDENT,
  93. /** For mapping, one port for each destination address (for any port).
  94. * For filtering, allow incoming traffic from addresses that outgoing
  95. * traffic has been sent to. */
  96. ADDRESS_DEPENDENT,
  97. /** For mapping, one port for each destination address/port.
  98. * For filtering, allow incoming traffic only from addresses/ports that
  99. * outgoing traffic has been sent to. */
  100. PORT_DEPENDENT,
  101. } NatBehavior;
  102. TestNat() :
  103. enabled_(false),
  104. filtering_type_(ENDPOINT_INDEPENDENT),
  105. mapping_type_(ENDPOINT_INDEPENDENT),
  106. mapping_timeout_(30000),
  107. allow_hairpinning_(false),
  108. refresh_on_ingress_(false),
  109. block_udp_(false),
  110. block_stun_(false),
  111. delay_stun_resp_ms_(0),
  112. sockets_() {}
  113. bool has_port_mappings() const;
  114. // Helps determine whether we're hairpinning
  115. bool is_my_external_tuple(const nr_transport_addr &addr) const;
  116. bool is_an_internal_tuple(const nr_transport_addr &addr) const;
  117. int create_socket_factory(nr_socket_factory **factorypp);
  118. void insert_socket(TestNrSocket *socket) {
  119. sockets_.insert(socket);
  120. }
  121. void erase_socket(TestNrSocket *socket) {
  122. sockets_.erase(socket);
  123. }
  124. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestNat);
  125. static NatBehavior ToNatBehavior(const std::string& type);
  126. bool enabled_;
  127. TestNat::NatBehavior filtering_type_;
  128. TestNat::NatBehavior mapping_type_;
  129. uint32_t mapping_timeout_;
  130. bool allow_hairpinning_;
  131. bool refresh_on_ingress_;
  132. bool block_udp_;
  133. bool block_stun_;
  134. /* Note: this can only delay a single response so far (bug 1253657) */
  135. uint32_t delay_stun_resp_ms_;
  136. private:
  137. std::set<TestNrSocket*> sockets_;
  138. ~TestNat(){}
  139. };
  140. /**
  141. * Subclass of NrSocketBase that can simulate things like being behind a NAT,
  142. * packet loss, latency, packet rewriting, etc. Also exposes some stuff that
  143. * assists in diagnostics.
  144. * This is accomplished by wrapping an "internal" socket (that handles traffic
  145. * behind the NAT), and a collection of "external" sockets (that handle traffic
  146. * into/out of the NAT)
  147. */
  148. class TestNrSocket : public NrSocketBase {
  149. public:
  150. explicit TestNrSocket(TestNat *nat);
  151. bool has_port_mappings() const;
  152. bool is_my_external_tuple(const nr_transport_addr &addr) const;
  153. // Overrides of NrSocketBase
  154. int create(nr_transport_addr *addr) override;
  155. int sendto(const void *msg, size_t len,
  156. int flags, nr_transport_addr *to) override;
  157. int recvfrom(void * buf, size_t maxlen,
  158. size_t *len, int flags,
  159. nr_transport_addr *from) override;
  160. int getaddr(nr_transport_addr *addrp) override;
  161. void close() override;
  162. int connect(nr_transport_addr *addr) override;
  163. int write(const void *msg, size_t len, size_t *written) override;
  164. int read(void *buf, size_t maxlen, size_t *len) override;
  165. int listen(int backlog) override;
  166. int accept(nr_transport_addr *addrp, nr_socket **sockp) override;
  167. int async_wait(int how, NR_async_cb cb, void *cb_arg,
  168. char *function, int line) override;
  169. int cancel(int how) override;
  170. // Need override since this is virtual in NrSocketBase
  171. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestNrSocket, override)
  172. private:
  173. virtual ~TestNrSocket();
  174. class UdpPacket {
  175. public:
  176. UdpPacket(const void *msg, size_t len, const nr_transport_addr &addr) :
  177. buffer_(new DataBuffer(static_cast<const uint8_t*>(msg), len)) {
  178. // TODO(bug 1170299): Remove const_cast when no longer necessary
  179. nr_transport_addr_copy(&remote_address_,
  180. const_cast<nr_transport_addr*>(&addr));
  181. }
  182. nr_transport_addr remote_address_;
  183. UniquePtr<DataBuffer> buffer_;
  184. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(UdpPacket);
  185. private:
  186. ~UdpPacket(){}
  187. };
  188. class PortMapping {
  189. public:
  190. PortMapping(const nr_transport_addr &remote_address,
  191. const RefPtr<NrSocketBase> &external_socket);
  192. int sendto(const void *msg, size_t len, const nr_transport_addr &to);
  193. int async_wait(int how, NR_async_cb cb, void *cb_arg,
  194. char *function, int line);
  195. int cancel(int how);
  196. int send_from_queue();
  197. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PortMapping);
  198. PRIntervalTime last_used_;
  199. RefPtr<NrSocketBase> external_socket_;
  200. // For non-symmetric, most of the data here doesn't matter
  201. nr_transport_addr remote_address_;
  202. private:
  203. ~PortMapping() {
  204. external_socket_->close();
  205. }
  206. // If external_socket_ returns E_WOULDBLOCK, we don't want to propagate
  207. // that to the code using the TestNrSocket. We can also perhaps use this
  208. // to help simulate things like latency.
  209. std::list<RefPtr<UdpPacket>> send_queue_;
  210. };
  211. struct DeferredPacket {
  212. DeferredPacket(TestNrSocket *sock,
  213. const void *data, size_t len,
  214. int flags,
  215. nr_transport_addr *addr,
  216. RefPtr<NrSocketBase> internal_socket) :
  217. socket_(sock),
  218. buffer_(reinterpret_cast<const uint8_t *>(data), len),
  219. flags_(flags),
  220. internal_socket_(internal_socket) {
  221. nr_transport_addr_copy(&to_, addr);
  222. }
  223. TestNrSocket *socket_;
  224. DataBuffer buffer_;
  225. int flags_;
  226. nr_transport_addr to_;
  227. RefPtr<NrSocketBase> internal_socket_;
  228. };
  229. bool is_port_mapping_stale(const PortMapping &port_mapping) const;
  230. bool allow_ingress(const nr_transport_addr &from,
  231. PortMapping **port_mapping_used) const;
  232. void destroy_stale_port_mappings();
  233. static void socket_readable_callback(void *real_sock_v,
  234. int how,
  235. void *test_sock_v);
  236. void on_socket_readable(NrSocketBase *external_or_internal_socket);
  237. void fire_readable_callback();
  238. static void port_mapping_tcp_passthrough_callback(void *ext_sock_v,
  239. int how,
  240. void *test_sock_v);
  241. void cancel_port_mapping_async_wait(int how);
  242. static void port_mapping_writeable_callback(void *ext_sock_v,
  243. int how,
  244. void *test_sock_v);
  245. void write_to_port_mapping(NrSocketBase *external_socket);
  246. bool is_tcp_connection_behind_nat() const;
  247. PortMapping* get_port_mapping(const nr_transport_addr &remote_addr,
  248. TestNat::NatBehavior filter) const;
  249. PortMapping* create_port_mapping(
  250. const nr_transport_addr &remote_addr,
  251. const RefPtr<NrSocketBase> &external_socket) const;
  252. RefPtr<NrSocketBase> create_external_socket(
  253. const nr_transport_addr &remote_addr) const;
  254. static void process_delayed_cb(NR_SOCKET s, int how, void *cb_arg);
  255. RefPtr<NrSocketBase> readable_socket_;
  256. // The socket for the "internal" address; used to talk to stuff behind the
  257. // same nat.
  258. RefPtr<NrSocketBase> internal_socket_;
  259. RefPtr<TestNat> nat_;
  260. // Since our comparison logic is different depending on what kind of NAT
  261. // we simulate, and the STL does not make it very easy to switch out the
  262. // comparison function at runtime, and these lists are going to be very
  263. // small anyway, we just brute-force it.
  264. std::list<RefPtr<PortMapping>> port_mappings_;
  265. void *timer_handle_;
  266. };
  267. } // namespace mozilla
  268. #endif // test_nr_socket__