blob: 9b5cb5a214ff312e00faaa22a837a57c2d9b430e [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>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <map>
16#include <memory>
17#include <set>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/async_socket.h"
20#include "rtc_base/constructor_magic.h"
21#include "rtc_base/message_queue.h"
22#include "rtc_base/nat_server.h"
23#include "rtc_base/nat_types.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/socket_address.h"
26#include "rtc_base/socket_factory.h"
27#include "rtc_base/socket_server.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028
29namespace rtc {
30
31const size_t kNATEncodedIPv4AddressSize = 8U;
32const size_t kNATEncodedIPv6AddressSize = 20U;
33
34// Used by the NAT socket implementation.
35class NATInternalSocketFactory {
36 public:
37 virtual ~NATInternalSocketFactory() {}
Yves Gerey665174f2018-06-19 15:03:05 +020038 virtual AsyncSocket* CreateInternalSocket(int family,
39 int type,
40 const SocketAddress& local_addr,
41 SocketAddress* nat_addr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042};
43
44// Creates sockets that will send all traffic through a NAT, using an existing
45// NATServer instance running at nat_addr. The actual data is sent using sockets
46// from a socket factory, given to the constructor.
47class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
48 public:
Yves Gerey665174f2018-06-19 15:03:05 +020049 NATSocketFactory(SocketFactory* factory,
50 const SocketAddress& nat_udp_addr,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051 const SocketAddress& nat_tcp_addr);
52
53 // SocketFactory implementation
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 AsyncSocket* CreateAsyncSocket(int family, int type) override;
56
57 // NATInternalSocketFactory implementation
58 AsyncSocket* CreateInternalSocket(int family,
59 int type,
60 const SocketAddress& local_addr,
61 SocketAddress* nat_addr) override;
62
63 private:
64 SocketFactory* factory_;
65 SocketAddress nat_udp_addr_;
66 SocketAddress nat_tcp_addr_;
67 RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketFactory);
68};
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
109 SocketFactory* internal_factory() { return internal_factory_.get(); }
110 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_;
131 std::unique_ptr<SocketFactory> internal_factory_;
132 std::unique_ptr<NATServer> nat_server_;
133 TranslatorMap nats_;
134 std::set<SocketAddress> clients_;
135 };
136
137 explicit NATSocketServer(SocketServer* ss);
138
139 SocketServer* socketserver() { return server_; }
140 MessageQueue* queue() { return msg_queue_; }
141
142 Translator* GetTranslator(const SocketAddress& ext_ip);
143 Translator* AddTranslator(const SocketAddress& ext_ip,
Yves Gerey665174f2018-06-19 15:03:05 +0200144 const SocketAddress& int_ip,
145 NATType type);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200146 void RemoveTranslator(const SocketAddress& ext_ip);
147
148 // SocketServer implementation
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200149 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200150 AsyncSocket* CreateAsyncSocket(int family, int type) override;
151
152 void SetMessageQueue(MessageQueue* queue) override;
153 bool Wait(int cms, bool process_io) override;
154 void WakeUp() override;
155
156 // NATInternalSocketFactory implementation
157 AsyncSocket* CreateInternalSocket(int family,
158 int type,
159 const SocketAddress& local_addr,
160 SocketAddress* nat_addr) override;
161
162 private:
163 SocketServer* server_;
164 MessageQueue* msg_queue_;
165 TranslatorMap nats_;
166 RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketServer);
167};
168
169// Free-standing NAT helper functions.
Yves Gerey665174f2018-06-19 15:03:05 +0200170size_t PackAddressForNAT(char* buf,
171 size_t buf_size,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200172 const SocketAddress& remote_addr);
Yves Gerey665174f2018-06-19 15:03:05 +0200173size_t UnpackAddressFromNAT(const char* buf,
174 size_t buf_size,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200175 SocketAddress* remote_addr);
176} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177
Steve Anton10542f22019-01-11 09:11:00 -0800178#endif // RTC_BASE_NAT_SOCKET_FACTORY_H_