blob: bbc109e9a18f936fe6c087989eeacbb3d158c0f7 [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/async_socket.h"
21#include "rtc_base/constructor_magic.h"
22#include "rtc_base/message_queue.h"
23#include "rtc_base/nat_server.h"
24#include "rtc_base/nat_types.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/socket_address.h"
27#include "rtc_base/socket_factory.h"
28#include "rtc_base/socket_server.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029
30namespace rtc {
31
32const size_t kNATEncodedIPv4AddressSize = 8U;
33const size_t kNATEncodedIPv6AddressSize = 20U;
34
35// Used by the NAT socket implementation.
36class NATInternalSocketFactory {
37 public:
38 virtual ~NATInternalSocketFactory() {}
Yves Gerey665174f2018-06-19 15:03:05 +020039 virtual AsyncSocket* CreateInternalSocket(int family,
40 int type,
41 const SocketAddress& local_addr,
42 SocketAddress* nat_addr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043};
44
45// Creates sockets that will send all traffic through a NAT, using an existing
46// NATServer instance running at nat_addr. The actual data is sent using sockets
47// from a socket factory, given to the constructor.
48class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
49 public:
Yves Gerey665174f2018-06-19 15:03:05 +020050 NATSocketFactory(SocketFactory* factory,
51 const SocketAddress& nat_udp_addr,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052 const SocketAddress& nat_tcp_addr);
53
54 // SocketFactory implementation
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056 AsyncSocket* CreateAsyncSocket(int family, int type) override;
57
58 // NATInternalSocketFactory implementation
59 AsyncSocket* CreateInternalSocket(int family,
60 int type,
61 const SocketAddress& local_addr,
62 SocketAddress* nat_addr) override;
63
64 private:
65 SocketFactory* factory_;
66 SocketAddress nat_udp_addr_;
67 SocketAddress nat_tcp_addr_;
68 RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketFactory);
69};
70
71// Creates sockets that will send traffic through a NAT depending on what
72// address they bind to. This can be used to simulate a client on a NAT sending
73// to a client that is not behind a NAT.
74// Note that the internal addresses of clients must be unique. This is because
75// there is only one socketserver per thread, and the Bind() address is used to
76// figure out which NAT (if any) the socket should talk to.
77//
78// Example with 3 NATs (2 cascaded), and 3 clients.
79// ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED);
80// ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)->
81// AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE);
82// ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2");
83// ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3");
84// ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")->
85// AddClient("192.168.1.2");
86class NATSocketServer : public SocketServer, public NATInternalSocketFactory {
87 public:
88 class Translator;
Yves Gerey3e707812018-11-28 16:47:49 +010089
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020090 // holds a list of NATs
91 class TranslatorMap : private std::map<SocketAddress, Translator*> {
92 public:
93 ~TranslatorMap();
94 Translator* Get(const SocketAddress& ext_ip);
95 Translator* Add(const SocketAddress& ext_ip, Translator*);
96 void Remove(const SocketAddress& ext_ip);
97 Translator* FindClient(const SocketAddress& int_ip);
98 };
99
100 // a specific NAT
101 class Translator {
102 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200103 Translator(NATSocketServer* server,
104 NATType type,
105 const SocketAddress& int_addr,
106 SocketFactory* ext_factory,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 const SocketAddress& ext_addr);
108 ~Translator();
109
110 SocketFactory* internal_factory() { return internal_factory_.get(); }
111 SocketAddress internal_udp_address() const {
112 return nat_server_->internal_udp_address();
113 }
114 SocketAddress internal_tcp_address() const {
115 return SocketAddress(); // nat_server_->internal_tcp_address();
116 }
117
118 Translator* GetTranslator(const SocketAddress& ext_ip);
119 Translator* AddTranslator(const SocketAddress& ext_ip,
Yves Gerey665174f2018-06-19 15:03:05 +0200120 const SocketAddress& int_ip,
121 NATType type);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 void RemoveTranslator(const SocketAddress& ext_ip);
123
124 bool AddClient(const SocketAddress& int_ip);
125 void RemoveClient(const SocketAddress& int_ip);
126
127 // Looks for the specified client in this or a child NAT.
128 Translator* FindClient(const SocketAddress& int_ip);
129
130 private:
131 NATSocketServer* server_;
132 std::unique_ptr<SocketFactory> internal_factory_;
133 std::unique_ptr<NATServer> nat_server_;
134 TranslatorMap nats_;
135 std::set<SocketAddress> clients_;
136 };
137
138 explicit NATSocketServer(SocketServer* ss);
139
140 SocketServer* socketserver() { return server_; }
141 MessageQueue* queue() { return msg_queue_; }
142
143 Translator* GetTranslator(const SocketAddress& ext_ip);
144 Translator* AddTranslator(const SocketAddress& ext_ip,
Yves Gerey665174f2018-06-19 15:03:05 +0200145 const SocketAddress& int_ip,
146 NATType type);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200147 void RemoveTranslator(const SocketAddress& ext_ip);
148
149 // SocketServer implementation
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200150 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200151 AsyncSocket* CreateAsyncSocket(int family, int type) override;
152
153 void SetMessageQueue(MessageQueue* queue) override;
154 bool Wait(int cms, bool process_io) override;
155 void WakeUp() override;
156
157 // NATInternalSocketFactory implementation
158 AsyncSocket* CreateInternalSocket(int family,
159 int type,
160 const SocketAddress& local_addr,
161 SocketAddress* nat_addr) override;
162
163 private:
164 SocketServer* server_;
165 MessageQueue* msg_queue_;
166 TranslatorMap nats_;
167 RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketServer);
168};
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_