blob: acbd62a09281c68e412583937df8fbd0094076bd [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"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/nat_types.h"
19#include "rtc_base/proxy_server.h"
20#include "rtc_base/socket_address_pair.h"
21#include "rtc_base/socket_factory.h"
Markus Handell18523c32020-07-08 17:55:58 +020022#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/thread.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024
25namespace 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.
30struct RouteCmp {
31 explicit RouteCmp(NAT* nat);
32 size_t operator()(const SocketAddressPair& r) const;
Yves Gerey665174f2018-06-19 15:03:05 +020033 bool operator()(const SocketAddressPair& r1,
34 const SocketAddressPair& r2) const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035
36 bool symmetric;
37};
38
39// Changes how addresses are compared based on the filtering rules of the NAT.
40struct 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
58const int NAT_SERVER_UDP_PORT = 4237;
59const int NAT_SERVER_TCP_PORT = 4238;
60
61class NATServer : public sigslot::has_slots<> {
62 public:
Yves Gerey665174f2018-06-19 15:03:05 +020063 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 Kjellanderec78f1c2017-06-29 07:52:50 +020069 ~NATServer() override;
70
Byoungchan Lee14af7622022-01-12 05:24:58 +090071 NATServer(const NATServer&) = delete;
72 NATServer& operator=(const NATServer&) = delete;
73
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074 SocketAddress internal_udp_address() const {
75 return udp_server_socket_->GetLocalAddress();
76 }
77
78 SocketAddress internal_tcp_address() const {
79 return tcp_proxy_server_->GetServerAddress();
80 }
81
82 // Packets received on one of the networks.
Yves Gerey665174f2018-06-19 15:03:05 +020083 void OnInternalUDPPacket(AsyncPacketSocket* socket,
84 const char* buf,
85 size_t size,
86 const SocketAddress& addr,
Niels Möllere6933812018-11-05 13:01:41 +010087 const int64_t& packet_time_us);
Yves Gerey665174f2018-06-19 15:03:05 +020088 void OnExternalUDPPacket(AsyncPacketSocket* socket,
89 const char* buf,
90 size_t size,
91 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010092 const int64_t& packet_time_us);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093
94 private:
95 typedef std::set<SocketAddress, AddrCmp> AddressSet;
96
97 /* Records a translation and the associated external socket. */
98 struct TransEntry {
99 TransEntry(const SocketAddressPair& r, AsyncUDPSocket* s, NAT* nat);
100 ~TransEntry();
101
Mirko Bonadeic3efe1a2020-06-30 14:24:09 +0200102 void AllowlistInsert(const SocketAddress& addr);
103 bool AllowlistContains(const SocketAddress& ext_addr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104
105 SocketAddressPair route;
106 AsyncUDPSocket* socket;
Mirko Bonadeic3efe1a2020-06-30 14:24:09 +0200107 AddressSet* allowlist;
Markus Handell18523c32020-07-08 17:55:58 +0200108 webrtc::Mutex mutex_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109 };
110
111 typedef std::map<SocketAddressPair, TransEntry*, RouteCmp> InternalMap;
112 typedef std::map<SocketAddress, TransEntry*> ExternalMap;
113
114 /* Creates a new entry that translates the given route. */
115 void Translate(const SocketAddressPair& route);
116
117 /* Determines whether the NAT would filter out a packet from this address. */
118 bool ShouldFilterOut(TransEntry* entry, const SocketAddress& ext_addr);
119
120 NAT* nat_;
121 SocketFactory* external_;
122 SocketAddress external_ip_;
123 AsyncUDPSocket* udp_server_socket_;
124 ProxyServer* tcp_proxy_server_;
125 InternalMap* int_map_;
126 ExternalMap* ext_map_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200127};
128
129} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130
Steve Anton10542f22019-01-11 09:11:00 -0800131#endif // RTC_BASE_NAT_SERVER_H_