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