blob: 97961d4f9ecbec8236d35bce0f3e974a6023eb0c [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_NATSOCKETFACTORY_H_
12#define RTC_BASE_NATSOCKETFACTORY_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <map>
15#include <memory>
16#include <set>
Yves Gerey665174f2018-06-19 15:03:05 +020017#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/constructormagic.h"
20#include "rtc_base/natserver.h"
21#include "rtc_base/socketaddress.h"
22#include "rtc_base/socketserver.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023
24namespace rtc {
25
26const size_t kNATEncodedIPv4AddressSize = 8U;
27const size_t kNATEncodedIPv6AddressSize = 20U;
28
29// Used by the NAT socket implementation.
30class NATInternalSocketFactory {
31 public:
32 virtual ~NATInternalSocketFactory() {}
Yves Gerey665174f2018-06-19 15:03:05 +020033 virtual AsyncSocket* CreateInternalSocket(int family,
34 int type,
35 const SocketAddress& local_addr,
36 SocketAddress* nat_addr) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037};
38
39// Creates sockets that will send all traffic through a NAT, using an existing
40// NATServer instance running at nat_addr. The actual data is sent using sockets
41// from a socket factory, given to the constructor.
42class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
43 public:
Yves Gerey665174f2018-06-19 15:03:05 +020044 NATSocketFactory(SocketFactory* factory,
45 const SocketAddress& nat_udp_addr,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020046 const SocketAddress& nat_tcp_addr);
47
48 // SocketFactory implementation
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050 AsyncSocket* CreateAsyncSocket(int family, int type) override;
51
52 // NATInternalSocketFactory implementation
53 AsyncSocket* CreateInternalSocket(int family,
54 int type,
55 const SocketAddress& local_addr,
56 SocketAddress* nat_addr) override;
57
58 private:
59 SocketFactory* factory_;
60 SocketAddress nat_udp_addr_;
61 SocketAddress nat_tcp_addr_;
62 RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketFactory);
63};
64
65// Creates sockets that will send traffic through a NAT depending on what
66// address they bind to. This can be used to simulate a client on a NAT sending
67// to a client that is not behind a NAT.
68// Note that the internal addresses of clients must be unique. This is because
69// there is only one socketserver per thread, and the Bind() address is used to
70// figure out which NAT (if any) the socket should talk to.
71//
72// Example with 3 NATs (2 cascaded), and 3 clients.
73// ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED);
74// ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)->
75// AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE);
76// ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2");
77// ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3");
78// ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")->
79// AddClient("192.168.1.2");
80class NATSocketServer : public SocketServer, public NATInternalSocketFactory {
81 public:
82 class Translator;
83 // holds a list of NATs
84 class TranslatorMap : private std::map<SocketAddress, Translator*> {
85 public:
86 ~TranslatorMap();
87 Translator* Get(const SocketAddress& ext_ip);
88 Translator* Add(const SocketAddress& ext_ip, Translator*);
89 void Remove(const SocketAddress& ext_ip);
90 Translator* FindClient(const SocketAddress& int_ip);
91 };
92
93 // a specific NAT
94 class Translator {
95 public:
Yves Gerey665174f2018-06-19 15:03:05 +020096 Translator(NATSocketServer* server,
97 NATType type,
98 const SocketAddress& int_addr,
99 SocketFactory* ext_factory,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100 const SocketAddress& ext_addr);
101 ~Translator();
102
103 SocketFactory* internal_factory() { return internal_factory_.get(); }
104 SocketAddress internal_udp_address() const {
105 return nat_server_->internal_udp_address();
106 }
107 SocketAddress internal_tcp_address() const {
108 return SocketAddress(); // nat_server_->internal_tcp_address();
109 }
110
111 Translator* GetTranslator(const SocketAddress& ext_ip);
112 Translator* AddTranslator(const SocketAddress& ext_ip,
Yves Gerey665174f2018-06-19 15:03:05 +0200113 const SocketAddress& int_ip,
114 NATType type);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115 void RemoveTranslator(const SocketAddress& ext_ip);
116
117 bool AddClient(const SocketAddress& int_ip);
118 void RemoveClient(const SocketAddress& int_ip);
119
120 // Looks for the specified client in this or a child NAT.
121 Translator* FindClient(const SocketAddress& int_ip);
122
123 private:
124 NATSocketServer* server_;
125 std::unique_ptr<SocketFactory> internal_factory_;
126 std::unique_ptr<NATServer> nat_server_;
127 TranslatorMap nats_;
128 std::set<SocketAddress> clients_;
129 };
130
131 explicit NATSocketServer(SocketServer* ss);
132
133 SocketServer* socketserver() { return server_; }
134 MessageQueue* queue() { return msg_queue_; }
135
136 Translator* GetTranslator(const SocketAddress& ext_ip);
137 Translator* AddTranslator(const SocketAddress& ext_ip,
Yves Gerey665174f2018-06-19 15:03:05 +0200138 const SocketAddress& int_ip,
139 NATType type);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200140 void RemoveTranslator(const SocketAddress& ext_ip);
141
142 // SocketServer implementation
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200143 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200144 AsyncSocket* CreateAsyncSocket(int family, int type) override;
145
146 void SetMessageQueue(MessageQueue* queue) override;
147 bool Wait(int cms, bool process_io) override;
148 void WakeUp() override;
149
150 // NATInternalSocketFactory implementation
151 AsyncSocket* CreateInternalSocket(int family,
152 int type,
153 const SocketAddress& local_addr,
154 SocketAddress* nat_addr) override;
155
156 private:
157 SocketServer* server_;
158 MessageQueue* msg_queue_;
159 TranslatorMap nats_;
160 RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketServer);
161};
162
163// Free-standing NAT helper functions.
Yves Gerey665174f2018-06-19 15:03:05 +0200164size_t PackAddressForNAT(char* buf,
165 size_t buf_size,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200166 const SocketAddress& remote_addr);
Yves Gerey665174f2018-06-19 15:03:05 +0200167size_t UnpackAddressFromNAT(const char* buf,
168 size_t buf_size,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200169 SocketAddress* remote_addr);
170} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200172#endif // RTC_BASE_NATSOCKETFACTORY_H_