nat_server.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_SERVER_H_
  11. #define RTC_BASE_NAT_SERVER_H_
  12. #include <map>
  13. #include <set>
  14. #include "rtc_base/async_udp_socket.h"
  15. #include "rtc_base/constructor_magic.h"
  16. #include "rtc_base/nat_types.h"
  17. #include "rtc_base/proxy_server.h"
  18. #include "rtc_base/socket_address_pair.h"
  19. #include "rtc_base/socket_factory.h"
  20. #include "rtc_base/synchronization/mutex.h"
  21. #include "rtc_base/thread.h"
  22. namespace rtc {
  23. // Change how routes (socketaddress pairs) are compared based on the type of
  24. // NAT. The NAT server maintains a hashtable of the routes that it knows
  25. // about. So these affect which routes are treated the same.
  26. struct RouteCmp {
  27. explicit RouteCmp(NAT* nat);
  28. size_t operator()(const SocketAddressPair& r) const;
  29. bool operator()(const SocketAddressPair& r1,
  30. const SocketAddressPair& r2) const;
  31. bool symmetric;
  32. };
  33. // Changes how addresses are compared based on the filtering rules of the NAT.
  34. struct AddrCmp {
  35. explicit AddrCmp(NAT* nat);
  36. size_t operator()(const SocketAddress& r) const;
  37. bool operator()(const SocketAddress& r1, const SocketAddress& r2) const;
  38. bool use_ip;
  39. bool use_port;
  40. };
  41. // Implements the NAT device. It listens for packets on the internal network,
  42. // translates them, and sends them out over the external network.
  43. //
  44. // TCP connections initiated from the internal side of the NAT server are
  45. // also supported, by making a connection to the NAT server's TCP address and
  46. // then sending the remote address in quasi-STUN format. The connection status
  47. // will be indicated back to the client as a 1 byte status code, where '0'
  48. // indicates success.
  49. const int NAT_SERVER_UDP_PORT = 4237;
  50. const int NAT_SERVER_TCP_PORT = 4238;
  51. class NATServer : public sigslot::has_slots<> {
  52. public:
  53. NATServer(NATType type,
  54. SocketFactory* internal,
  55. const SocketAddress& internal_udp_addr,
  56. const SocketAddress& internal_tcp_addr,
  57. SocketFactory* external,
  58. const SocketAddress& external_ip);
  59. ~NATServer() override;
  60. SocketAddress internal_udp_address() const {
  61. return udp_server_socket_->GetLocalAddress();
  62. }
  63. SocketAddress internal_tcp_address() const {
  64. return tcp_proxy_server_->GetServerAddress();
  65. }
  66. // Packets received on one of the networks.
  67. void OnInternalUDPPacket(AsyncPacketSocket* socket,
  68. const char* buf,
  69. size_t size,
  70. const SocketAddress& addr,
  71. const int64_t& packet_time_us);
  72. void OnExternalUDPPacket(AsyncPacketSocket* socket,
  73. const char* buf,
  74. size_t size,
  75. const SocketAddress& remote_addr,
  76. const int64_t& packet_time_us);
  77. private:
  78. typedef std::set<SocketAddress, AddrCmp> AddressSet;
  79. /* Records a translation and the associated external socket. */
  80. struct TransEntry {
  81. TransEntry(const SocketAddressPair& r, AsyncUDPSocket* s, NAT* nat);
  82. ~TransEntry();
  83. void AllowlistInsert(const SocketAddress& addr);
  84. bool AllowlistContains(const SocketAddress& ext_addr);
  85. SocketAddressPair route;
  86. AsyncUDPSocket* socket;
  87. AddressSet* allowlist;
  88. webrtc::Mutex mutex_;
  89. };
  90. typedef std::map<SocketAddressPair, TransEntry*, RouteCmp> InternalMap;
  91. typedef std::map<SocketAddress, TransEntry*> ExternalMap;
  92. /* Creates a new entry that translates the given route. */
  93. void Translate(const SocketAddressPair& route);
  94. /* Determines whether the NAT would filter out a packet from this address. */
  95. bool ShouldFilterOut(TransEntry* entry, const SocketAddress& ext_addr);
  96. NAT* nat_;
  97. SocketFactory* external_;
  98. SocketAddress external_ip_;
  99. AsyncUDPSocket* udp_server_socket_;
  100. ProxyServer* tcp_proxy_server_;
  101. InternalMap* int_map_;
  102. ExternalMap* ext_map_;
  103. RTC_DISALLOW_COPY_AND_ASSIGN(NATServer);
  104. };
  105. } // namespace rtc
  106. #endif // RTC_BASE_NAT_SERVER_H_