blob: 0b301b5844e6d31a059aad1d116e1120c21169d3 [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_SOCKET_FACTORY_H_
12#define RTC_BASE_NAT_SOCKET_FACTORY_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <map>
17#include <memory>
18#include <set>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/nat_server.h"
21#include "rtc_base/nat_types.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/socket_address.h"
24#include "rtc_base/socket_factory.h"
25#include "rtc_base/socket_server.h"
Sebastian Jansson290de822020-01-09 14:20:23 +010026#include "rtc_base/thread.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027
28namespace rtc {
29
30const size_t kNATEncodedIPv4AddressSize = 8U;
31const size_t kNATEncodedIPv6AddressSize = 20U;
32
33// Used by the NAT socket implementation.
34class NATInternalSocketFactory {
35 public:
36 virtual ~NATInternalSocketFactory() {}
Niels Möllerd0b88792021-08-12 10:32:30 +020037 virtual Socket* CreateInternalSocket(int family,
38 int type,
39 const SocketAddress& local_addr,
40 SocketAddress* nat_addr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041};
42
43// Creates sockets that will send all traffic through a NAT, using an existing
44// NATServer instance running at nat_addr. The actual data is sent using sockets
45// from a socket factory, given to the constructor.
46class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
47 public:
Yves Gerey665174f2018-06-19 15:03:05 +020048 NATSocketFactory(SocketFactory* factory,
49 const SocketAddress& nat_udp_addr,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050 const SocketAddress& nat_tcp_addr);
51
Byoungchan Lee14af7622022-01-12 05:24:58 +090052 NATSocketFactory(const NATSocketFactory&) = delete;
53 NATSocketFactory& operator=(const NATSocketFactory&) = delete;
54
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 // SocketFactory implementation
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057
58 // NATInternalSocketFactory implementation
Niels Möllerd0b88792021-08-12 10:32:30 +020059 Socket* CreateInternalSocket(int family,
60 int type,
61 const SocketAddress& local_addr,
62 SocketAddress* nat_addr) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063
64 private:
65 SocketFactory* factory_;
66 SocketAddress nat_udp_addr_;
67 SocketAddress nat_tcp_addr_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068};
69
70// Creates sockets that will send traffic through a NAT depending on what
71// address they bind to. This can be used to simulate a client on a NAT sending
72// to a client that is not behind a NAT.
73// Note that the internal addresses of clients must be unique. This is because
74// there is only one socketserver per thread, and the Bind() address is used to
75// figure out which NAT (if any) the socket should talk to.
76//
77// Example with 3 NATs (2 cascaded), and 3 clients.
78// ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED);
79// ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)->
80// AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE);
81// ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2");
82// ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3");
83// ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")->
84// AddClient("192.168.1.2");
85class NATSocketServer : public SocketServer, public NATInternalSocketFactory {
86 public:
87 class Translator;
Yves Gerey3e707812018-11-28 16:47:49 +010088
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089 // holds a list of NATs
90 class TranslatorMap : private std::map<SocketAddress, Translator*> {
91 public:
92 ~TranslatorMap();
93 Translator* Get(const SocketAddress& ext_ip);
94 Translator* Add(const SocketAddress& ext_ip, Translator*);
95 void Remove(const SocketAddress& ext_ip);
96 Translator* FindClient(const SocketAddress& int_ip);
97 };
98
99 // a specific NAT
100 class Translator {
101 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200102 Translator(NATSocketServer* server,
103 NATType type,
104 const SocketAddress& int_addr,
105 SocketFactory* ext_factory,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200106 const SocketAddress& ext_addr);
107 ~Translator();
108
Niels Möller9bd24572021-04-19 12:18:27 +0200109 SocketFactory* internal_factory() { return internal_server_.get(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110 SocketAddress internal_udp_address() const {
111 return nat_server_->internal_udp_address();
112 }
113 SocketAddress internal_tcp_address() const {
114 return SocketAddress(); // nat_server_->internal_tcp_address();
115 }
116
117 Translator* GetTranslator(const SocketAddress& ext_ip);
118 Translator* AddTranslator(const SocketAddress& ext_ip,
Yves Gerey665174f2018-06-19 15:03:05 +0200119 const SocketAddress& int_ip,
120 NATType type);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121 void RemoveTranslator(const SocketAddress& ext_ip);
122
123 bool AddClient(const SocketAddress& int_ip);
124 void RemoveClient(const SocketAddress& int_ip);
125
126 // Looks for the specified client in this or a child NAT.
127 Translator* FindClient(const SocketAddress& int_ip);
128
129 private:
130 NATSocketServer* server_;
Niels Möller9bd24572021-04-19 12:18:27 +0200131 std::unique_ptr<SocketServer> internal_server_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132 std::unique_ptr<NATServer> nat_server_;
133 TranslatorMap nats_;
134 std::set<SocketAddress> clients_;
135 };
136
137 explicit NATSocketServer(SocketServer* ss);
138
Byoungchan Lee14af7622022-01-12 05:24:58 +0900139 NATSocketServer(const NATSocketServer&) = delete;
140 NATSocketServer& operator=(const NATSocketServer&) = delete;
141
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200142 SocketServer* socketserver() { return server_; }
Sebastian Jansson290de822020-01-09 14:20:23 +0100143 Thread* queue() { return msg_queue_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200144
145 Translator* GetTranslator(const SocketAddress& ext_ip);
146 Translator* AddTranslator(const SocketAddress& ext_ip,
Yves Gerey665174f2018-06-19 15:03:05 +0200147 const SocketAddress& int_ip,
148 NATType type);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200149 void RemoveTranslator(const SocketAddress& ext_ip);
150
151 // SocketServer implementation
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200152 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200153
Sebastian Jansson290de822020-01-09 14:20:23 +0100154 void SetMessageQueue(Thread* queue) override;
Markus Handell9a21c492022-08-25 11:40:13 +0000155 bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200156 void WakeUp() override;
157
158 // NATInternalSocketFactory implementation
Niels Möllerd0b88792021-08-12 10:32:30 +0200159 Socket* CreateInternalSocket(int family,
160 int type,
161 const SocketAddress& local_addr,
162 SocketAddress* nat_addr) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200163
164 private:
165 SocketServer* server_;
Sebastian Jansson290de822020-01-09 14:20:23 +0100166 Thread* msg_queue_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200167 TranslatorMap nats_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200168};
169
170// Free-standing NAT helper functions.
Yves Gerey665174f2018-06-19 15:03:05 +0200171size_t PackAddressForNAT(char* buf,
172 size_t buf_size,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200173 const SocketAddress& remote_addr);
Yves Gerey665174f2018-06-19 15:03:05 +0200174size_t UnpackAddressFromNAT(const char* buf,
175 size_t buf_size,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200176 SocketAddress* remote_addr);
177} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178
Steve Anton10542f22019-01-11 09:11:00 -0800179#endif // RTC_BASE_NAT_SOCKET_FACTORY_H_