blob: 5078fbb2c13342a04a4ff606ba385e3d03826aa8 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_NAT_SERVER_H_
12#define RTC_BASE_NAT_SERVER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <map>
15#include <set>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
Steve Anton10542f22019-01-11 09:11:00 -080017#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"
Markus Handell18523c32020-07-08 17:55:58 +020023#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
26namespace 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.
31struct RouteCmp {
32 explicit RouteCmp(NAT* nat);
33 size_t operator()(const SocketAddressPair& r) const;
Yves Gerey665174f2018-06-19 15:03:05 +020034 bool operator()(const SocketAddressPair& r1,
35 const SocketAddressPair& r2) const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020036
37 bool symmetric;
38};
39
40// Changes how addresses are compared based on the filtering rules of the NAT.
41struct 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
59const int NAT_SERVER_UDP_PORT = 4237;
60const int NAT_SERVER_TCP_PORT = 4238;
61
62class NATServer : public sigslot::has_slots<> {
63 public:
Yves Gerey665174f2018-06-19 15:03:05 +020064 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);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070 ~NATServer() override;
71
72 SocketAddress internal_udp_address() const {
73 return udp_server_socket_->GetLocalAddress();
74 }
75
76 SocketAddress internal_tcp_address() const {
77 return tcp_proxy_server_->GetServerAddress();
78 }
79
80 // Packets received on one of the networks.
Yves Gerey665174f2018-06-19 15:03:05 +020081 void OnInternalUDPPacket(AsyncPacketSocket* socket,
82 const char* buf,
83 size_t size,
84 const SocketAddress& addr,
Niels Möllere6933812018-11-05 13:01:41 +010085 const int64_t& packet_time_us);
Yves Gerey665174f2018-06-19 15:03:05 +020086 void OnExternalUDPPacket(AsyncPacketSocket* socket,
87 const char* buf,
88 size_t size,
89 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010090 const int64_t& packet_time_us);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091
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
Mirko Bonadeic3efe1a2020-06-30 14:24:09 +0200100 void AllowlistInsert(const SocketAddress& addr);
101 bool AllowlistContains(const SocketAddress& ext_addr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102
103 SocketAddressPair route;
104 AsyncUDPSocket* socket;
Mirko Bonadeic3efe1a2020-06-30 14:24:09 +0200105 AddressSet* allowlist;
Markus Handell18523c32020-07-08 17:55:58 +0200106 webrtc::Mutex mutex_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 };
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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129
Steve Anton10542f22019-01-11 09:11:00 -0800130#endif // RTC_BASE_NAT_SERVER_H_