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.

183 lines
6.9 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_NATSOCKETFACTORY_H_
  28. #define TALK_BASE_NATSOCKETFACTORY_H_
  29. #include <string>
  30. #include <map>
  31. #include <set>
  32. #include "talk/base/natserver.h"
  33. #include "talk/base/socketaddress.h"
  34. #include "talk/base/socketserver.h"
  35. namespace talk_base {
  36. const size_t kNATEncodedIPv4AddressSize = 8U;
  37. const size_t kNATEncodedIPv6AddressSize = 20U;
  38. // Used by the NAT socket implementation.
  39. class NATInternalSocketFactory {
  40. public:
  41. virtual ~NATInternalSocketFactory() {}
  42. virtual AsyncSocket* CreateInternalSocket(int family, int type,
  43. const SocketAddress& local_addr, SocketAddress* nat_addr) = 0;
  44. };
  45. // Creates sockets that will send all traffic through a NAT, using an existing
  46. // NATServer instance running at nat_addr. The actual data is sent using sockets
  47. // from a socket factory, given to the constructor.
  48. class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
  49. public:
  50. NATSocketFactory(SocketFactory* factory, const SocketAddress& nat_addr);
  51. // SocketFactory implementation
  52. virtual Socket* CreateSocket(int type);
  53. virtual Socket* CreateSocket(int family, int type);
  54. virtual AsyncSocket* CreateAsyncSocket(int type);
  55. virtual AsyncSocket* CreateAsyncSocket(int family, int type);
  56. // NATInternalSocketFactory implementation
  57. virtual AsyncSocket* CreateInternalSocket(int family, int type,
  58. const SocketAddress& local_addr, SocketAddress* nat_addr);
  59. private:
  60. SocketFactory* factory_;
  61. SocketAddress nat_addr_;
  62. DISALLOW_EVIL_CONSTRUCTORS(NATSocketFactory);
  63. };
  64. // Creates sockets that will send traffic through a NAT depending on what
  65. // address they bind to. This can be used to simulate a client on a NAT sending
  66. // to a client that is not behind a NAT.
  67. // Note that the internal addresses of clients must be unique. This is because
  68. // there is only one socketserver per thread, and the Bind() address is used to
  69. // figure out which NAT (if any) the socket should talk to.
  70. //
  71. // Example with 3 NATs (2 cascaded), and 3 clients.
  72. // ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED);
  73. // ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)->
  74. // AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE);
  75. // ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2");
  76. // ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3");
  77. // ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")->
  78. // AddClient("192.168.1.2");
  79. class NATSocketServer : public SocketServer, public NATInternalSocketFactory {
  80. public:
  81. class Translator;
  82. // holds a list of NATs
  83. class TranslatorMap : private std::map<SocketAddress, Translator*> {
  84. public:
  85. ~TranslatorMap();
  86. Translator* Get(const SocketAddress& ext_ip);
  87. Translator* Add(const SocketAddress& ext_ip, Translator*);
  88. void Remove(const SocketAddress& ext_ip);
  89. Translator* FindClient(const SocketAddress& int_ip);
  90. };
  91. // a specific NAT
  92. class Translator {
  93. public:
  94. Translator(NATSocketServer* server, NATType type,
  95. const SocketAddress& int_addr, SocketFactory* ext_factory,
  96. const SocketAddress& ext_addr);
  97. SocketFactory* internal_factory() { return internal_factory_.get(); }
  98. SocketAddress internal_address() const {
  99. return nat_server_->internal_address();
  100. }
  101. SocketAddress internal_tcp_address() const {
  102. return SocketAddress(); // nat_server_->internal_tcp_address();
  103. }
  104. Translator* GetTranslator(const SocketAddress& ext_ip);
  105. Translator* AddTranslator(const SocketAddress& ext_ip,
  106. const SocketAddress& int_ip, NATType type);
  107. void RemoveTranslator(const SocketAddress& ext_ip);
  108. bool AddClient(const SocketAddress& int_ip);
  109. void RemoveClient(const SocketAddress& int_ip);
  110. // Looks for the specified client in this or a child NAT.
  111. Translator* FindClient(const SocketAddress& int_ip);
  112. private:
  113. NATSocketServer* server_;
  114. scoped_ptr<SocketFactory> internal_factory_;
  115. scoped_ptr<NATServer> nat_server_;
  116. TranslatorMap nats_;
  117. std::set<SocketAddress> clients_;
  118. };
  119. explicit NATSocketServer(SocketServer* ss);
  120. SocketServer* socketserver() { return server_; }
  121. MessageQueue* queue() { return msg_queue_; }
  122. Translator* GetTranslator(const SocketAddress& ext_ip);
  123. Translator* AddTranslator(const SocketAddress& ext_ip,
  124. const SocketAddress& int_ip, NATType type);
  125. void RemoveTranslator(const SocketAddress& ext_ip);
  126. // SocketServer implementation
  127. virtual Socket* CreateSocket(int type);
  128. virtual Socket* CreateSocket(int family, int type);
  129. virtual AsyncSocket* CreateAsyncSocket(int type);
  130. virtual AsyncSocket* CreateAsyncSocket(int family, int type);
  131. virtual void SetMessageQueue(MessageQueue* queue) {
  132. msg_queue_ = queue;
  133. server_->SetMessageQueue(queue);
  134. }
  135. virtual bool Wait(int cms, bool process_io) {
  136. return server_->Wait(cms, process_io);
  137. }
  138. virtual void WakeUp() {
  139. server_->WakeUp();
  140. }
  141. // NATInternalSocketFactory implementation
  142. virtual AsyncSocket* CreateInternalSocket(int family, int type,
  143. const SocketAddress& local_addr, SocketAddress* nat_addr);
  144. private:
  145. SocketServer* server_;
  146. MessageQueue* msg_queue_;
  147. TranslatorMap nats_;
  148. DISALLOW_EVIL_CONSTRUCTORS(NATSocketServer);
  149. };
  150. // Free-standing NAT helper functions.
  151. size_t PackAddressForNAT(char* buf, size_t buf_size,
  152. const SocketAddress& remote_addr);
  153. size_t UnpackAddressFromNAT(const char* buf, size_t buf_size,
  154. SocketAddress* remote_addr);
  155. } // namespace talk_base
  156. #endif // TALK_BASE_NATSOCKETFACTORY_H_