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