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