nat_socket_factory.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_NAT_SOCKET_FACTORY_H_
  11. #define RTC_BASE_NAT_SOCKET_FACTORY_H_
  12. #include <stddef.h>
  13. #include <map>
  14. #include <memory>
  15. #include <set>
  16. #include "rtc_base/async_socket.h"
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/nat_server.h"
  19. #include "rtc_base/nat_types.h"
  20. #include "rtc_base/socket.h"
  21. #include "rtc_base/socket_address.h"
  22. #include "rtc_base/socket_factory.h"
  23. #include "rtc_base/socket_server.h"
  24. #include "rtc_base/thread.h"
  25. namespace rtc {
  26. const size_t kNATEncodedIPv4AddressSize = 8U;
  27. const size_t kNATEncodedIPv6AddressSize = 20U;
  28. // Used by the NAT socket implementation.
  29. class NATInternalSocketFactory {
  30. public:
  31. virtual ~NATInternalSocketFactory() {}
  32. virtual AsyncSocket* CreateInternalSocket(int family,
  33. int type,
  34. const SocketAddress& local_addr,
  35. SocketAddress* nat_addr) = 0;
  36. };
  37. // Creates sockets that will send all traffic through a NAT, using an existing
  38. // NATServer instance running at nat_addr. The actual data is sent using sockets
  39. // from a socket factory, given to the constructor.
  40. class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
  41. public:
  42. NATSocketFactory(SocketFactory* factory,
  43. const SocketAddress& nat_udp_addr,
  44. const SocketAddress& nat_tcp_addr);
  45. // SocketFactory implementation
  46. Socket* CreateSocket(int family, int type) override;
  47. AsyncSocket* CreateAsyncSocket(int family, int type) override;
  48. // NATInternalSocketFactory implementation
  49. AsyncSocket* CreateInternalSocket(int family,
  50. int type,
  51. const SocketAddress& local_addr,
  52. SocketAddress* nat_addr) override;
  53. private:
  54. SocketFactory* factory_;
  55. SocketAddress nat_udp_addr_;
  56. SocketAddress nat_tcp_addr_;
  57. RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketFactory);
  58. };
  59. // Creates sockets that will send traffic through a NAT depending on what
  60. // address they bind to. This can be used to simulate a client on a NAT sending
  61. // to a client that is not behind a NAT.
  62. // Note that the internal addresses of clients must be unique. This is because
  63. // there is only one socketserver per thread, and the Bind() address is used to
  64. // figure out which NAT (if any) the socket should talk to.
  65. //
  66. // Example with 3 NATs (2 cascaded), and 3 clients.
  67. // ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED);
  68. // ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)->
  69. // AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE);
  70. // ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2");
  71. // ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3");
  72. // ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")->
  73. // AddClient("192.168.1.2");
  74. class NATSocketServer : public SocketServer, public NATInternalSocketFactory {
  75. public:
  76. class Translator;
  77. // holds a list of NATs
  78. class TranslatorMap : private std::map<SocketAddress, Translator*> {
  79. public:
  80. ~TranslatorMap();
  81. Translator* Get(const SocketAddress& ext_ip);
  82. Translator* Add(const SocketAddress& ext_ip, Translator*);
  83. void Remove(const SocketAddress& ext_ip);
  84. Translator* FindClient(const SocketAddress& int_ip);
  85. };
  86. // a specific NAT
  87. class Translator {
  88. public:
  89. Translator(NATSocketServer* server,
  90. NATType type,
  91. const SocketAddress& int_addr,
  92. SocketFactory* ext_factory,
  93. const SocketAddress& ext_addr);
  94. ~Translator();
  95. SocketFactory* internal_factory() { return internal_factory_.get(); }
  96. SocketAddress internal_udp_address() const {
  97. return nat_server_->internal_udp_address();
  98. }
  99. SocketAddress internal_tcp_address() const {
  100. return SocketAddress(); // nat_server_->internal_tcp_address();
  101. }
  102. Translator* GetTranslator(const SocketAddress& ext_ip);
  103. Translator* AddTranslator(const SocketAddress& ext_ip,
  104. const SocketAddress& int_ip,
  105. NATType type);
  106. void RemoveTranslator(const SocketAddress& ext_ip);
  107. bool AddClient(const SocketAddress& int_ip);
  108. void RemoveClient(const SocketAddress& int_ip);
  109. // Looks for the specified client in this or a child NAT.
  110. Translator* FindClient(const SocketAddress& int_ip);
  111. private:
  112. NATSocketServer* server_;
  113. std::unique_ptr<SocketFactory> internal_factory_;
  114. std::unique_ptr<NATServer> nat_server_;
  115. TranslatorMap nats_;
  116. std::set<SocketAddress> clients_;
  117. };
  118. explicit NATSocketServer(SocketServer* ss);
  119. SocketServer* socketserver() { return server_; }
  120. Thread* queue() { return msg_queue_; }
  121. Translator* GetTranslator(const SocketAddress& ext_ip);
  122. Translator* AddTranslator(const SocketAddress& ext_ip,
  123. const SocketAddress& int_ip,
  124. NATType type);
  125. void RemoveTranslator(const SocketAddress& ext_ip);
  126. // SocketServer implementation
  127. Socket* CreateSocket(int family, int type) override;
  128. AsyncSocket* CreateAsyncSocket(int family, int type) override;
  129. void SetMessageQueue(Thread* queue) override;
  130. bool Wait(int cms, bool process_io) override;
  131. void WakeUp() override;
  132. // NATInternalSocketFactory implementation
  133. AsyncSocket* CreateInternalSocket(int family,
  134. int type,
  135. const SocketAddress& local_addr,
  136. SocketAddress* nat_addr) override;
  137. private:
  138. SocketServer* server_;
  139. Thread* msg_queue_;
  140. TranslatorMap nats_;
  141. RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketServer);
  142. };
  143. // Free-standing NAT helper functions.
  144. size_t PackAddressForNAT(char* buf,
  145. size_t buf_size,
  146. const SocketAddress& remote_addr);
  147. size_t UnpackAddressFromNAT(const char* buf,
  148. size_t buf_size,
  149. SocketAddress* remote_addr);
  150. } // namespace rtc
  151. #endif // RTC_BASE_NAT_SOCKET_FACTORY_H_