nat_server.h 4.2 KB

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