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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_NAT_SERVER_H_ |
| 12 | #define RTC_BASE_NAT_SERVER_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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 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" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 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; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 33 | bool operator()(const SocketAddressPair& r1, |
| 34 | const SocketAddressPair& r2) const; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 63 | NATServer(NATType type, |
| 64 | SocketFactory* internal, |
| 65 | const SocketAddress& internal_udp_addr, |
| 66 | const SocketAddress& internal_tcp_addr, |
| 67 | SocketFactory* external, |
| 68 | const SocketAddress& external_ip); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 69 | ~NATServer() override; |
| 70 | |
| 71 | SocketAddress internal_udp_address() const { |
| 72 | return udp_server_socket_->GetLocalAddress(); |
| 73 | } |
| 74 | |
| 75 | SocketAddress internal_tcp_address() const { |
| 76 | return tcp_proxy_server_->GetServerAddress(); |
| 77 | } |
| 78 | |
| 79 | // Packets received on one of the networks. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 80 | void OnInternalUDPPacket(AsyncPacketSocket* socket, |
| 81 | const char* buf, |
| 82 | size_t size, |
| 83 | const SocketAddress& addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 84 | const int64_t& packet_time_us); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 85 | void OnExternalUDPPacket(AsyncPacketSocket* socket, |
| 86 | const char* buf, |
| 87 | size_t size, |
| 88 | const SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 89 | const int64_t& packet_time_us); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | typedef std::set<SocketAddress, AddrCmp> AddressSet; |
| 93 | |
| 94 | /* Records a translation and the associated external socket. */ |
| 95 | struct TransEntry { |
| 96 | TransEntry(const SocketAddressPair& r, AsyncUDPSocket* s, NAT* nat); |
| 97 | ~TransEntry(); |
| 98 | |
| 99 | void WhitelistInsert(const SocketAddress& addr); |
| 100 | bool WhitelistContains(const SocketAddress& ext_addr); |
| 101 | |
| 102 | SocketAddressPair route; |
| 103 | AsyncUDPSocket* socket; |
| 104 | AddressSet* whitelist; |
| 105 | CriticalSection crit_; |
| 106 | }; |
| 107 | |
| 108 | typedef std::map<SocketAddressPair, TransEntry*, RouteCmp> InternalMap; |
| 109 | typedef std::map<SocketAddress, TransEntry*> ExternalMap; |
| 110 | |
| 111 | /* Creates a new entry that translates the given route. */ |
| 112 | void Translate(const SocketAddressPair& route); |
| 113 | |
| 114 | /* Determines whether the NAT would filter out a packet from this address. */ |
| 115 | bool ShouldFilterOut(TransEntry* entry, const SocketAddress& ext_addr); |
| 116 | |
| 117 | NAT* nat_; |
| 118 | SocketFactory* external_; |
| 119 | SocketAddress external_ip_; |
| 120 | AsyncUDPSocket* udp_server_socket_; |
| 121 | ProxyServer* tcp_proxy_server_; |
| 122 | InternalMap* int_map_; |
| 123 | ExternalMap* ext_map_; |
| 124 | RTC_DISALLOW_COPY_AND_ASSIGN(NATServer); |
| 125 | }; |
| 126 | |
| 127 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 129 | #endif // RTC_BASE_NAT_SERVER_H_ |