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"
22 #include "webrtc/base/proxyserver.h"
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.
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.
56 
57 const int NAT_SERVER_UDP_PORT = 4237;
58 const int NAT_SERVER_TCP_PORT = 4238;
59 
60 class NATServer : public sigslot::has_slots<> {
61  public:
62   NATServer(
63       NATType type, SocketFactory* internal,
64       const SocketAddress& internal_udp_addr,
65       const SocketAddress& internal_tcp_addr,
66       SocketFactory* external, const SocketAddress& external_ip);
67   ~NATServer() override;
68 
internal_udp_address()69   SocketAddress internal_udp_address() const {
70     return udp_server_socket_->GetLocalAddress();
71   }
72 
internal_tcp_address()73   SocketAddress internal_tcp_address() const {
74     return tcp_proxy_server_->GetServerAddress();
75   }
76 
77   // Packets received on one of the networks.
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);
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* external_;
113   SocketAddress external_ip_;
114   AsyncUDPSocket* udp_server_socket_;
115   ProxyServer* tcp_proxy_server_;
116   InternalMap* int_map_;
117   ExternalMap* ext_map_;
118   RTC_DISALLOW_COPY_AND_ASSIGN(NATServer);
119 };
120 
121 }  // namespace rtc
122 
123 #endif  // WEBRTC_BASE_NATSERVER_H_
124