You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_NATSERVER_H_
  28. #define TALK_BASE_NATSERVER_H_
  29. #include <map>
  30. #include <set>
  31. #include "talk/base/asyncudpsocket.h"
  32. #include "talk/base/socketaddresspair.h"
  33. #include "talk/base/thread.h"
  34. #include "talk/base/socketfactory.h"
  35. #include "talk/base/nattypes.h"
  36. namespace talk_base {
  37. // Change how routes (socketaddress pairs) are compared based on the type of
  38. // NAT. The NAT server maintains a hashtable of the routes that it knows
  39. // about. So these affect which routes are treated the same.
  40. struct RouteCmp {
  41. explicit RouteCmp(NAT* nat);
  42. size_t operator()(const SocketAddressPair& r) const;
  43. bool operator()(
  44. const SocketAddressPair& r1, const SocketAddressPair& r2) const;
  45. bool symmetric;
  46. };
  47. // Changes how addresses are compared based on the filtering rules of the NAT.
  48. struct AddrCmp {
  49. explicit AddrCmp(NAT* nat);
  50. size_t operator()(const SocketAddress& r) const;
  51. bool operator()(const SocketAddress& r1, const SocketAddress& r2) const;
  52. bool use_ip;
  53. bool use_port;
  54. };
  55. // Implements the NAT device. It listens for packets on the internal network,
  56. // translates them, and sends them out over the external network.
  57. const int NAT_SERVER_PORT = 4237;
  58. class NATServer : public sigslot::has_slots<> {
  59. public:
  60. NATServer(
  61. NATType type, SocketFactory* internal, const SocketAddress& internal_addr,
  62. SocketFactory* external, const SocketAddress& external_ip);
  63. ~NATServer();
  64. SocketAddress internal_address() const {
  65. return server_socket_->GetLocalAddress();
  66. }
  67. // Packets received on one of the networks.
  68. void OnInternalPacket(AsyncPacketSocket* socket, const char* buf,
  69. size_t size, const SocketAddress& addr);
  70. void OnExternalPacket(AsyncPacketSocket* socket, const char* buf,
  71. size_t size, const SocketAddress& remote_addr);
  72. private:
  73. typedef std::set<SocketAddress, AddrCmp> AddressSet;
  74. /* Records a translation and the associated external socket. */
  75. struct TransEntry {
  76. TransEntry(const SocketAddressPair& r, AsyncUDPSocket* s, NAT* nat);
  77. ~TransEntry();
  78. SocketAddressPair route;
  79. AsyncUDPSocket* socket;
  80. AddressSet* whitelist;
  81. };
  82. typedef std::map<SocketAddressPair, TransEntry*, RouteCmp> InternalMap;
  83. typedef std::map<SocketAddress, TransEntry*> ExternalMap;
  84. /* Creates a new entry that translates the given route. */
  85. void Translate(const SocketAddressPair& route);
  86. /* Determines whether the NAT would filter out a packet from this address. */
  87. bool Filter(TransEntry* entry, const SocketAddress& ext_addr);
  88. NAT* nat_;
  89. SocketFactory* internal_;
  90. SocketFactory* external_;
  91. SocketAddress external_ip_;
  92. AsyncUDPSocket* server_socket_;
  93. AsyncSocket* tcp_server_socket_;
  94. InternalMap* int_map_;
  95. ExternalMap* ext_map_;
  96. DISALLOW_EVIL_CONSTRUCTORS(NATServer);
  97. };
  98. } // namespace talk_base
  99. #endif // TALK_BASE_NATSERVER_H_