blob: 5adcaa5dfdf3fc1eedce5db801a81cb79ad2aadb [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 17:11:0011#ifndef RTC_BASE_NAT_SOCKET_FACTORY_H_
12#define RTC_BASE_NAT_SOCKET_FACTORY_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Yves Gerey3e707812018-11-28 15:47:4914#include <stddef.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Per K056782c2024-01-30 11:32:0516#include <cstdint>
Henrik Kjellanderec78f1c2017-06-29 05:52:5017#include <map>
18#include <memory>
19#include <set>
henrike@webrtc.orgf0488722014-05-13 18:00:2620
Per K056782c2024-01-30 11:32:0521#include "api/array_view.h"
22#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 17:11:0023#include "rtc_base/nat_server.h"
24#include "rtc_base/nat_types.h"
Yves Gerey3e707812018-11-28 15:47:4925#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 17:11:0026#include "rtc_base/socket_address.h"
27#include "rtc_base/socket_factory.h"
28#include "rtc_base/socket_server.h"
Sebastian Jansson290de822020-01-09 13:20:2329#include "rtc_base/thread.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5030
31namespace rtc {
32
33const size_t kNATEncodedIPv4AddressSize = 8U;
34const size_t kNATEncodedIPv6AddressSize = 20U;
35
36// Used by the NAT socket implementation.
37class NATInternalSocketFactory {
38 public:
39 virtual ~NATInternalSocketFactory() {}
Niels Möllerd0b88792021-08-12 08:32:3040 virtual Socket* CreateInternalSocket(int family,
41 int type,
42 const SocketAddress& local_addr,
43 SocketAddress* nat_addr) = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:5044};
45
46// Creates sockets that will send all traffic through a NAT, using an existing
47// NATServer instance running at nat_addr. The actual data is sent using sockets
48// from a socket factory, given to the constructor.
49class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
50 public:
Yves Gerey665174f2018-06-19 13:03:0551 NATSocketFactory(SocketFactory* factory,
52 const SocketAddress& nat_udp_addr,
Henrik Kjellanderec78f1c2017-06-29 05:52:5053 const SocketAddress& nat_tcp_addr);
54
Byoungchan Lee14af7622022-01-11 20:24:5855 NATSocketFactory(const NATSocketFactory&) = delete;
56 NATSocketFactory& operator=(const NATSocketFactory&) = delete;
57
Henrik Kjellanderec78f1c2017-06-29 05:52:5058 // SocketFactory implementation
Henrik Kjellanderec78f1c2017-06-29 05:52:5059 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 05:52:5060
61 // NATInternalSocketFactory implementation
Niels Möllerd0b88792021-08-12 08:32:3062 Socket* CreateInternalSocket(int family,
63 int type,
64 const SocketAddress& local_addr,
65 SocketAddress* nat_addr) override;
Henrik Kjellanderec78f1c2017-06-29 05:52:5066
67 private:
68 SocketFactory* factory_;
69 SocketAddress nat_udp_addr_;
70 SocketAddress nat_tcp_addr_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5071};
72
73// Creates sockets that will send traffic through a NAT depending on what
74// address they bind to. This can be used to simulate a client on a NAT sending
75// to a client that is not behind a NAT.
76// Note that the internal addresses of clients must be unique. This is because
77// there is only one socketserver per thread, and the Bind() address is used to
78// figure out which NAT (if any) the socket should talk to.
79//
80// Example with 3 NATs (2 cascaded), and 3 clients.
81// ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED);
82// ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)->
83// AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE);
84// ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2");
85// ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3");
86// ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")->
87// AddClient("192.168.1.2");
88class NATSocketServer : public SocketServer, public NATInternalSocketFactory {
89 public:
90 class Translator;
Yves Gerey3e707812018-11-28 15:47:4991
Henrik Kjellanderec78f1c2017-06-29 05:52:5092 // holds a list of NATs
93 class TranslatorMap : private std::map<SocketAddress, Translator*> {
94 public:
95 ~TranslatorMap();
96 Translator* Get(const SocketAddress& ext_ip);
97 Translator* Add(const SocketAddress& ext_ip, Translator*);
98 void Remove(const SocketAddress& ext_ip);
99 Translator* FindClient(const SocketAddress& int_ip);
100 };
101
102 // a specific NAT
103 class Translator {
104 public:
Yves Gerey665174f2018-06-19 13:03:05105 Translator(NATSocketServer* server,
106 NATType type,
107 const SocketAddress& int_addr,
Per Ka8cd2ba2023-12-13 07:10:06108 Thread& external_socket_thread,
Yves Gerey665174f2018-06-19 13:03:05109 SocketFactory* ext_factory,
Henrik Kjellanderec78f1c2017-06-29 05:52:50110 const SocketAddress& ext_addr);
111 ~Translator();
112
Niels Möller9bd24572021-04-19 10:18:27113 SocketFactory* internal_factory() { return internal_server_.get(); }
Henrik Kjellanderec78f1c2017-06-29 05:52:50114 SocketAddress internal_udp_address() const {
115 return nat_server_->internal_udp_address();
116 }
117 SocketAddress internal_tcp_address() const {
118 return SocketAddress(); // nat_server_->internal_tcp_address();
119 }
120
121 Translator* GetTranslator(const SocketAddress& ext_ip);
122 Translator* AddTranslator(const SocketAddress& ext_ip,
Yves Gerey665174f2018-06-19 13:03:05123 const SocketAddress& int_ip,
124 NATType type);
Henrik Kjellanderec78f1c2017-06-29 05:52:50125 void RemoveTranslator(const SocketAddress& ext_ip);
126
127 bool AddClient(const SocketAddress& int_ip);
128 void RemoveClient(const SocketAddress& int_ip);
129
130 // Looks for the specified client in this or a child NAT.
131 Translator* FindClient(const SocketAddress& int_ip);
132
133 private:
134 NATSocketServer* server_;
Niels Möller9bd24572021-04-19 10:18:27135 std::unique_ptr<SocketServer> internal_server_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50136 std::unique_ptr<NATServer> nat_server_;
137 TranslatorMap nats_;
138 std::set<SocketAddress> clients_;
139 };
140
141 explicit NATSocketServer(SocketServer* ss);
142
Byoungchan Lee14af7622022-01-11 20:24:58143 NATSocketServer(const NATSocketServer&) = delete;
144 NATSocketServer& operator=(const NATSocketServer&) = delete;
145
Henrik Kjellanderec78f1c2017-06-29 05:52:50146 SocketServer* socketserver() { return server_; }
Sebastian Jansson290de822020-01-09 13:20:23147 Thread* queue() { return msg_queue_; }
Henrik Kjellanderec78f1c2017-06-29 05:52:50148
149 Translator* GetTranslator(const SocketAddress& ext_ip);
150 Translator* AddTranslator(const SocketAddress& ext_ip,
Yves Gerey665174f2018-06-19 13:03:05151 const SocketAddress& int_ip,
152 NATType type);
Henrik Kjellanderec78f1c2017-06-29 05:52:50153 void RemoveTranslator(const SocketAddress& ext_ip);
154
155 // SocketServer implementation
Henrik Kjellanderec78f1c2017-06-29 05:52:50156 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 05:52:50157
Sebastian Jansson290de822020-01-09 13:20:23158 void SetMessageQueue(Thread* queue) override;
Markus Handell9a21c492022-08-25 11:40:13159 bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) override;
Henrik Kjellanderec78f1c2017-06-29 05:52:50160 void WakeUp() override;
161
162 // NATInternalSocketFactory implementation
Niels Möllerd0b88792021-08-12 08:32:30163 Socket* CreateInternalSocket(int family,
164 int type,
165 const SocketAddress& local_addr,
166 SocketAddress* nat_addr) override;
Henrik Kjellanderec78f1c2017-06-29 05:52:50167
168 private:
169 SocketServer* server_;
Sebastian Jansson290de822020-01-09 13:20:23170 Thread* msg_queue_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50171 TranslatorMap nats_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50172};
173
174// Free-standing NAT helper functions.
Yves Gerey665174f2018-06-19 13:03:05175size_t PackAddressForNAT(char* buf,
176 size_t buf_size,
Henrik Kjellanderec78f1c2017-06-29 05:52:50177 const SocketAddress& remote_addr);
Per K056782c2024-01-30 11:32:05178size_t UnpackAddressFromNAT(rtc::ArrayView<const uint8_t> buf,
Henrik Kjellanderec78f1c2017-06-29 05:52:50179 SocketAddress* remote_addr);
180} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26181
Steve Anton10542f22019-01-11 17:11:00182#endif // RTC_BASE_NAT_SOCKET_FACTORY_H_